43f74238f00325dfbcf17f0ad389cb90e8deca75
[strongswan.git] / src / libcharon / encoding / message.c
1 /*
2 * Copyright (C) 2006-2014 Tobias Brunner
3 * Copyright (C) 2005-2010 Martin Willi
4 * Copyright (C) 2010 revosec AG
5 * Copyright (C) 2006 Daniel Roethlisberger
6 * Copyright (C) 2005 Jan Hutter
7 * Hochschule fuer Technik Rapperswil
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 */
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "message.h"
24
25 #include <library.h>
26 #include <bio/bio_writer.h>
27 #include <collections/array.h>
28 #include <daemon.h>
29 #include <sa/ikev1/keymat_v1.h>
30 #include <encoding/generator.h>
31 #include <encoding/parser.h>
32 #include <encoding/payloads/encodings.h>
33 #include <encoding/payloads/payload.h>
34 #include <encoding/payloads/hash_payload.h>
35 #include <encoding/payloads/encrypted_payload.h>
36 #include <encoding/payloads/encrypted_fragment_payload.h>
37 #include <encoding/payloads/unknown_payload.h>
38 #include <encoding/payloads/cp_payload.h>
39 #include <encoding/payloads/fragment_payload.h>
40
41 /**
42 * Max number of notify payloads per IKEv2 message
43 */
44 #define MAX_NOTIFY_PAYLOADS 20
45
46 /**
47 * Max number of delete payloads per IKEv2 message
48 */
49 #define MAX_DELETE_PAYLOADS 20
50
51 /**
52 * Max number of certificate payloads per IKEv2 message
53 */
54 #define MAX_CERT_PAYLOADS 8
55
56 /**
57 * Max number of vendor ID payloads per IKEv2 message
58 */
59 #define MAX_VID_PAYLOADS 20
60
61 /**
62 * Max number of certificate request payloads per IKEv1 message
63 */
64 #define MAX_CERTREQ_PAYLOADS 20
65
66 /**
67 * Max number of NAT-D payloads per IKEv1 message
68 */
69 #define MAX_NAT_D_PAYLOADS 10
70
71 /**
72 * Maximum packet size for fragmented packets (same as in sockets)
73 */
74 #define MAX_PACKET 10000
75
76 /**
77 * A payload rule defines the rules for a payload
78 * in a specific message rule. It defines if and how
79 * many times a payload must/can occur in a message
80 * and if it must be encrypted.
81 */
82 typedef struct {
83 /* Payload type */
84 payload_type_t type;
85 /* Minimal occurrence of this payload. */
86 size_t min_occurence;
87 /* Max occurrence of this payload. */
88 size_t max_occurence;
89 /* TRUE if payload must be encrypted */
90 bool encrypted;
91 /* If payload occurs, the message rule is fulfilled */
92 bool sufficient;
93 } payload_rule_t;
94
95 /**
96 * payload ordering structure allows us to reorder payloads according to RFC.
97 */
98 typedef struct {
99 /** payload type */
100 payload_type_t type;
101 /** notify type, if payload == PLV2_NOTIFY */
102 notify_type_t notify;
103 } payload_order_t;
104
105 /**
106 * A message rule defines the kind of a message,
107 * if it has encrypted contents and a list
108 * of payload ordering rules and payload parsing rules.
109 */
110 typedef struct {
111 /** Type of message. */
112 exchange_type_t exchange_type;
113 /** Is message a request or response. */
114 bool is_request;
115 /** Message contains encrypted payloads. */
116 bool encrypted;
117 /** Number of payload rules which will follow */
118 int rule_count;
119 /** Pointer to first payload rule */
120 payload_rule_t *rules;
121 /** Number of payload order rules */
122 int order_count;
123 /** payload ordering rules */
124 payload_order_t *order;
125 } message_rule_t;
126
127 /**
128 * Message rule for IKE_SA_INIT from initiator.
129 */
130 static payload_rule_t ike_sa_init_i_rules[] = {
131 /* payload type min max encr suff */
132 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
133 {PLV2_SECURITY_ASSOCIATION, 1, 1, FALSE, FALSE},
134 {PLV2_KEY_EXCHANGE, 1, 1, FALSE, FALSE},
135 {PLV2_NONCE, 1, 1, FALSE, FALSE},
136 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
137 };
138
139 /**
140 * payload order for IKE_SA_INIT initiator
141 */
142 static payload_order_t ike_sa_init_i_order[] = {
143 /* payload type notify type */
144 {PLV2_NOTIFY, COOKIE},
145 {PLV2_SECURITY_ASSOCIATION, 0},
146 {PLV2_KEY_EXCHANGE, 0},
147 {PLV2_NONCE, 0},
148 {PLV2_NOTIFY, NAT_DETECTION_SOURCE_IP},
149 {PLV2_NOTIFY, NAT_DETECTION_DESTINATION_IP},
150 {PLV2_NOTIFY, 0},
151 {PLV2_VENDOR_ID, 0},
152 };
153
154 /**
155 * Message rule for IKE_SA_INIT from responder.
156 */
157 static payload_rule_t ike_sa_init_r_rules[] = {
158 /* payload type min max encr suff */
159 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, TRUE},
160 {PLV2_SECURITY_ASSOCIATION, 1, 1, FALSE, FALSE},
161 {PLV2_KEY_EXCHANGE, 1, 1, FALSE, FALSE},
162 {PLV2_NONCE, 1, 1, FALSE, FALSE},
163 {PLV2_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
164 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
165 };
166
167 /**
168 * payload order for IKE_SA_INIT responder
169 */
170 static payload_order_t ike_sa_init_r_order[] = {
171 /* payload type notify type */
172 {PLV2_SECURITY_ASSOCIATION, 0},
173 {PLV2_KEY_EXCHANGE, 0},
174 {PLV2_NONCE, 0},
175 {PLV2_NOTIFY, NAT_DETECTION_SOURCE_IP},
176 {PLV2_NOTIFY, NAT_DETECTION_DESTINATION_IP},
177 {PLV2_NOTIFY, HTTP_CERT_LOOKUP_SUPPORTED},
178 {PLV2_CERTREQ, 0},
179 {PLV2_NOTIFY, 0},
180 {PLV2_VENDOR_ID, 0},
181 };
182
183 /**
184 * Message rule for IKE_AUTH from initiator.
185 */
186 static payload_rule_t ike_auth_i_rules[] = {
187 /* payload type min max encr suff */
188 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
189 {PLV2_EAP, 0, 1, TRUE, TRUE},
190 {PLV2_AUTH, 0, 1, TRUE, TRUE},
191 {PLV2_ID_INITIATOR, 0, 1, TRUE, FALSE},
192 {PLV2_CERTIFICATE, 0, MAX_CERT_PAYLOADS, TRUE, FALSE},
193 {PLV2_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, TRUE, FALSE},
194 {PLV2_ID_RESPONDER, 0, 1, TRUE, FALSE},
195 #ifdef ME
196 {PLV2_SECURITY_ASSOCIATION, 0, 1, TRUE, FALSE},
197 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
198 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
199 #else
200 {PLV2_SECURITY_ASSOCIATION, 0, 1, TRUE, FALSE},
201 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
202 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
203 #endif /* ME */
204 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
205 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
206 };
207
208 /**
209 * payload order for IKE_AUTH initiator
210 */
211 static payload_order_t ike_auth_i_order[] = {
212 /* payload type notify type */
213 {PLV2_ID_INITIATOR, 0},
214 {PLV2_CERTIFICATE, 0},
215 {PLV2_NOTIFY, INITIAL_CONTACT},
216 {PLV2_NOTIFY, HTTP_CERT_LOOKUP_SUPPORTED},
217 {PLV2_CERTREQ, 0},
218 {PLV2_ID_RESPONDER, 0},
219 {PLV2_AUTH, 0},
220 {PLV2_EAP, 0},
221 {PLV2_CONFIGURATION, 0},
222 {PLV2_NOTIFY, IPCOMP_SUPPORTED},
223 {PLV2_NOTIFY, USE_TRANSPORT_MODE},
224 {PLV2_NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
225 {PLV2_NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
226 {PLV2_SECURITY_ASSOCIATION, 0},
227 {PLV2_TS_INITIATOR, 0},
228 {PLV2_TS_RESPONDER, 0},
229 {PLV2_NOTIFY, MOBIKE_SUPPORTED},
230 {PLV2_NOTIFY, ADDITIONAL_IP4_ADDRESS},
231 {PLV2_NOTIFY, ADDITIONAL_IP6_ADDRESS},
232 {PLV2_NOTIFY, NO_ADDITIONAL_ADDRESSES},
233 {PLV2_NOTIFY, 0},
234 {PLV2_VENDOR_ID, 0},
235 };
236
237 /**
238 * Message rule for IKE_AUTH from responder.
239 */
240 static payload_rule_t ike_auth_r_rules[] = {
241 /* payload type min max encr suff */
242 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
243 {PLV2_EAP, 0, 1, TRUE, TRUE},
244 {PLV2_AUTH, 0, 1, TRUE, TRUE},
245 {PLV2_CERTIFICATE, 0, MAX_CERT_PAYLOADS, TRUE, FALSE},
246 {PLV2_ID_RESPONDER, 0, 1, TRUE, FALSE},
247 {PLV2_SECURITY_ASSOCIATION, 0, 1, TRUE, FALSE},
248 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
249 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
250 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
251 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
252 };
253
254 /**
255 * payload order for IKE_AUTH responder
256 */
257 static payload_order_t ike_auth_r_order[] = {
258 /* payload type notify type */
259 {PLV2_ID_RESPONDER, 0},
260 {PLV2_CERTIFICATE, 0},
261 {PLV2_AUTH, 0},
262 {PLV2_EAP, 0},
263 {PLV2_CONFIGURATION, 0},
264 {PLV2_NOTIFY, IPCOMP_SUPPORTED},
265 {PLV2_NOTIFY, USE_TRANSPORT_MODE},
266 {PLV2_NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
267 {PLV2_NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
268 {PLV2_SECURITY_ASSOCIATION, 0},
269 {PLV2_TS_INITIATOR, 0},
270 {PLV2_TS_RESPONDER, 0},
271 {PLV2_NOTIFY, AUTH_LIFETIME},
272 {PLV2_NOTIFY, MOBIKE_SUPPORTED},
273 {PLV2_NOTIFY, ADDITIONAL_IP4_ADDRESS},
274 {PLV2_NOTIFY, ADDITIONAL_IP6_ADDRESS},
275 {PLV2_NOTIFY, NO_ADDITIONAL_ADDRESSES},
276 {PLV2_NOTIFY, 0},
277 {PLV2_VENDOR_ID, 0},
278 };
279
280 /**
281 * Message rule for INFORMATIONAL from initiator.
282 */
283 static payload_rule_t informational_i_rules[] = {
284 /* payload type min max encr suff */
285 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
286 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
287 {PLV2_DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
288 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
289 };
290
291 /**
292 * payload order for INFORMATIONAL initiator
293 */
294 static payload_order_t informational_i_order[] = {
295 /* payload type notify type */
296 {PLV2_NOTIFY, UPDATE_SA_ADDRESSES},
297 {PLV2_NOTIFY, NAT_DETECTION_SOURCE_IP},
298 {PLV2_NOTIFY, NAT_DETECTION_DESTINATION_IP},
299 {PLV2_NOTIFY, COOKIE2},
300 {PLV2_NOTIFY, 0},
301 {PLV2_DELETE, 0},
302 {PLV2_CONFIGURATION, 0},
303 };
304
305 /**
306 * Message rule for INFORMATIONAL from responder.
307 */
308 static payload_rule_t informational_r_rules[] = {
309 /* payload type min max encr suff */
310 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
311 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
312 {PLV2_DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
313 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
314 };
315
316 /**
317 * payload order for INFORMATIONAL responder
318 */
319 static payload_order_t informational_r_order[] = {
320 /* payload type notify type */
321 {PLV2_NOTIFY, UPDATE_SA_ADDRESSES},
322 {PLV2_NOTIFY, NAT_DETECTION_SOURCE_IP},
323 {PLV2_NOTIFY, NAT_DETECTION_DESTINATION_IP},
324 {PLV2_NOTIFY, COOKIE2},
325 {PLV2_NOTIFY, 0},
326 {PLV2_DELETE, 0},
327 {PLV2_CONFIGURATION, 0},
328 };
329
330 /**
331 * Message rule for CREATE_CHILD_SA from initiator.
332 */
333 static payload_rule_t create_child_sa_i_rules[] = {
334 /* payload type min max encr suff */
335 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
336 {PLV2_SECURITY_ASSOCIATION, 1, 1, TRUE, FALSE},
337 {PLV2_NONCE, 1, 1, TRUE, FALSE},
338 {PLV2_KEY_EXCHANGE, 0, 1, TRUE, FALSE},
339 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
340 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
341 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
342 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
343 };
344
345 /**
346 * payload order for CREATE_CHILD_SA from initiator.
347 */
348 static payload_order_t create_child_sa_i_order[] = {
349 /* payload type notify type */
350 {PLV2_NOTIFY, REKEY_SA},
351 {PLV2_NOTIFY, IPCOMP_SUPPORTED},
352 {PLV2_NOTIFY, USE_TRANSPORT_MODE},
353 {PLV2_NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
354 {PLV2_NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
355 {PLV2_SECURITY_ASSOCIATION, 0},
356 {PLV2_NONCE, 0},
357 {PLV2_KEY_EXCHANGE, 0},
358 {PLV2_TS_INITIATOR, 0},
359 {PLV2_TS_RESPONDER, 0},
360 {PLV2_NOTIFY, 0},
361 };
362
363 /**
364 * Message rule for CREATE_CHILD_SA from responder.
365 */
366 static payload_rule_t create_child_sa_r_rules[] = {
367 /* payload type min max encr suff */
368 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
369 {PLV2_SECURITY_ASSOCIATION, 1, 1, TRUE, FALSE},
370 {PLV2_NONCE, 1, 1, TRUE, FALSE},
371 {PLV2_KEY_EXCHANGE, 0, 1, TRUE, FALSE},
372 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
373 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
374 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
375 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
376 };
377
378 /**
379 * payload order for CREATE_CHILD_SA from responder.
380 */
381 static payload_order_t create_child_sa_r_order[] = {
382 /* payload type notify type */
383 {PLV2_NOTIFY, IPCOMP_SUPPORTED},
384 {PLV2_NOTIFY, USE_TRANSPORT_MODE},
385 {PLV2_NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
386 {PLV2_NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
387 {PLV2_SECURITY_ASSOCIATION, 0},
388 {PLV2_NONCE, 0},
389 {PLV2_KEY_EXCHANGE, 0},
390 {PLV2_TS_INITIATOR, 0},
391 {PLV2_TS_RESPONDER, 0},
392 {PLV2_NOTIFY, ADDITIONAL_TS_POSSIBLE},
393 {PLV2_NOTIFY, 0},
394 };
395
396 #ifdef ME
397 /**
398 * Message rule for ME_CONNECT from initiator.
399 */
400 static payload_rule_t me_connect_i_rules[] = {
401 /* payload type min max encr suff */
402 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
403 {PLV2_ID_PEER, 1, 1, TRUE, FALSE},
404 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE}
405 };
406
407 /**
408 * payload order for ME_CONNECT from initiator.
409 */
410 static payload_order_t me_connect_i_order[] = {
411 /* payload type notify type */
412 {PLV2_NOTIFY, 0},
413 {PLV2_ID_PEER, 0},
414 {PLV2_VENDOR_ID, 0},
415 };
416
417 /**
418 * Message rule for ME_CONNECT from responder.
419 */
420 static payload_rule_t me_connect_r_rules[] = {
421 /* payload type min max encr suff */
422 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
423 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE}
424 };
425
426 /**
427 * payload order for ME_CONNECT from responder.
428 */
429 static payload_order_t me_connect_r_order[] = {
430 /* payload type notify type */
431 {PLV2_NOTIFY, 0},
432 {PLV2_VENDOR_ID, 0},
433 };
434 #endif /* ME */
435
436 #ifdef USE_IKEV1
437 /**
438 * Message rule for ID_PROT from initiator.
439 */
440 static payload_rule_t id_prot_i_rules[] = {
441 /* payload type min max encr suff */
442 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
443 {PLV1_SECURITY_ASSOCIATION, 0, 1, FALSE, FALSE},
444 {PLV1_KEY_EXCHANGE, 0, 1, FALSE, FALSE},
445 {PLV1_NONCE, 0, 1, FALSE, FALSE},
446 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
447 {PLV1_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
448 {PLV1_NAT_D, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
449 {PLV1_NAT_D_DRAFT_00_03, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
450 {PLV1_ID, 0, 1, TRUE, FALSE},
451 {PLV1_CERTIFICATE, 0, MAX_CERT_PAYLOADS, TRUE, FALSE},
452 {PLV1_SIGNATURE, 0, 1, TRUE, FALSE},
453 {PLV1_HASH, 0, 1, TRUE, FALSE},
454 {PLV1_FRAGMENT, 0, 1, FALSE, TRUE},
455 };
456
457 /**
458 * payload order for ID_PROT from initiator.
459 */
460 static payload_order_t id_prot_i_order[] = {
461 /* payload type notify type */
462 {PLV1_SECURITY_ASSOCIATION, 0},
463 {PLV1_KEY_EXCHANGE, 0},
464 {PLV1_NONCE, 0},
465 {PLV1_ID, 0},
466 {PLV1_CERTIFICATE, 0},
467 {PLV1_SIGNATURE, 0},
468 {PLV1_HASH, 0},
469 {PLV1_CERTREQ, 0},
470 {PLV1_NOTIFY, 0},
471 {PLV1_VENDOR_ID, 0},
472 {PLV1_NAT_D, 0},
473 {PLV1_NAT_D_DRAFT_00_03, 0},
474 {PLV1_FRAGMENT, 0},
475 };
476
477 /**
478 * Message rule for ID_PROT from responder.
479 */
480 static payload_rule_t id_prot_r_rules[] = {
481 /* payload type min max encr suff */
482 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
483 {PLV1_SECURITY_ASSOCIATION, 0, 1, FALSE, FALSE},
484 {PLV1_KEY_EXCHANGE, 0, 1, FALSE, FALSE},
485 {PLV1_NONCE, 0, 1, FALSE, FALSE},
486 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
487 {PLV1_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
488 {PLV1_NAT_D, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
489 {PLV1_NAT_D_DRAFT_00_03, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
490 {PLV1_ID, 0, 1, TRUE, FALSE},
491 {PLV1_CERTIFICATE, 0, MAX_CERT_PAYLOADS, TRUE, FALSE},
492 {PLV1_SIGNATURE, 0, 1, TRUE, FALSE},
493 {PLV1_HASH, 0, 1, TRUE, FALSE},
494 {PLV1_FRAGMENT, 0, 1, FALSE, TRUE},
495 };
496
497 /**
498 * payload order for ID_PROT from responder.
499 */
500 static payload_order_t id_prot_r_order[] = {
501 /* payload type notify type */
502 {PLV1_SECURITY_ASSOCIATION, 0},
503 {PLV1_KEY_EXCHANGE, 0},
504 {PLV1_NONCE, 0},
505 {PLV1_ID, 0},
506 {PLV1_CERTIFICATE, 0},
507 {PLV1_SIGNATURE, 0},
508 {PLV1_HASH, 0},
509 {PLV1_CERTREQ, 0},
510 {PLV1_NOTIFY, 0},
511 {PLV1_VENDOR_ID, 0},
512 {PLV1_NAT_D, 0},
513 {PLV1_NAT_D_DRAFT_00_03, 0},
514 {PLV1_FRAGMENT, 0},
515 };
516
517 /**
518 * Message rule for AGGRESSIVE from initiator.
519 */
520 static payload_rule_t aggressive_i_rules[] = {
521 /* payload type min max encr suff */
522 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
523 {PLV1_SECURITY_ASSOCIATION, 0, 1, FALSE, FALSE},
524 {PLV1_KEY_EXCHANGE, 0, 1, FALSE, FALSE},
525 {PLV1_NONCE, 0, 1, FALSE, FALSE},
526 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
527 {PLV1_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
528 {PLV1_NAT_D, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
529 {PLV1_NAT_D_DRAFT_00_03, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
530 {PLV1_ID, 0, 1, FALSE, FALSE},
531 {PLV1_CERTIFICATE, 0, 1, TRUE, FALSE},
532 {PLV1_SIGNATURE, 0, 1, TRUE, FALSE},
533 {PLV1_HASH, 0, 1, TRUE, FALSE},
534 {PLV1_FRAGMENT, 0, 1, FALSE, TRUE},
535 };
536
537 /**
538 * payload order for AGGRESSIVE from initiator.
539 */
540 static payload_order_t aggressive_i_order[] = {
541 /* payload type notify type */
542 {PLV1_SECURITY_ASSOCIATION, 0},
543 {PLV1_KEY_EXCHANGE, 0},
544 {PLV1_NONCE, 0},
545 {PLV1_ID, 0},
546 {PLV1_CERTIFICATE, 0},
547 {PLV1_NAT_D, 0},
548 {PLV1_NAT_D_DRAFT_00_03, 0},
549 {PLV1_SIGNATURE, 0},
550 {PLV1_HASH, 0},
551 {PLV1_CERTREQ, 0},
552 {PLV1_NOTIFY, 0},
553 {PLV1_VENDOR_ID, 0},
554 {PLV1_FRAGMENT, 0},
555 };
556
557 /**
558 * Message rule for AGGRESSIVE from responder.
559 */
560 static payload_rule_t aggressive_r_rules[] = {
561 /* payload type min max encr suff */
562 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
563 {PLV1_SECURITY_ASSOCIATION, 0, 1, FALSE, FALSE},
564 {PLV1_KEY_EXCHANGE, 0, 1, FALSE, FALSE},
565 {PLV1_NONCE, 0, 1, FALSE, FALSE},
566 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
567 {PLV1_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
568 {PLV1_NAT_D, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
569 {PLV1_NAT_D_DRAFT_00_03, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
570 {PLV1_ID, 0, 1, FALSE, FALSE},
571 {PLV1_CERTIFICATE, 0, 1, FALSE, FALSE},
572 {PLV1_SIGNATURE, 0, 1, FALSE, FALSE},
573 {PLV1_HASH, 0, 1, FALSE, FALSE},
574 {PLV1_FRAGMENT, 0, 1, FALSE, TRUE},
575 };
576
577 /**
578 * payload order for AGGRESSIVE from responder.
579 */
580 static payload_order_t aggressive_r_order[] = {
581 /* payload type notify type */
582 {PLV1_SECURITY_ASSOCIATION, 0},
583 {PLV1_KEY_EXCHANGE, 0},
584 {PLV1_NONCE, 0},
585 {PLV1_ID, 0},
586 {PLV1_CERTIFICATE, 0},
587 {PLV1_NAT_D, 0},
588 {PLV1_NAT_D_DRAFT_00_03, 0},
589 {PLV1_SIGNATURE, 0},
590 {PLV1_HASH, 0},
591 {PLV1_CERTREQ, 0},
592 {PLV1_NOTIFY, 0},
593 {PLV1_VENDOR_ID, 0},
594 {PLV1_FRAGMENT, 0},
595 };
596
597 /**
598 * Message rule for INFORMATIONAL_V1 from initiator.
599 */
600 static payload_rule_t informational_i_rules_v1[] = {
601 /* payload type min max encr suff */
602 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
603 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
604 {PLV1_DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
605 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
606 };
607
608 /**
609 * payload order for INFORMATIONAL_V1 from initiator.
610 */
611 static payload_order_t informational_i_order_v1[] = {
612 /* payload type notify type */
613 {PLV1_NOTIFY, 0},
614 {PLV1_DELETE, 0},
615 {PLV1_VENDOR_ID, 0},
616 };
617
618 /**
619 * Message rule for INFORMATIONAL_V1 from responder.
620 */
621 static payload_rule_t informational_r_rules_v1[] = {
622 /* payload type min max encr suff */
623 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
624 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
625 {PLV1_DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
626 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
627 };
628
629 /**
630 * payload order for INFORMATIONAL_V1 from responder.
631 */
632 static payload_order_t informational_r_order_v1[] = {
633 /* payload type notify type */
634 {PLV1_NOTIFY, 0},
635 {PLV1_DELETE, 0},
636 {PLV1_VENDOR_ID, 0},
637 };
638
639 /**
640 * Message rule for QUICK_MODE from initiator.
641 */
642 static payload_rule_t quick_mode_i_rules[] = {
643 /* payload type min max encr suff */
644 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
645 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
646 {PLV1_HASH, 0, 1, TRUE, FALSE},
647 {PLV1_SECURITY_ASSOCIATION, 0, 2, TRUE, FALSE},
648 {PLV1_NONCE, 0, 1, TRUE, FALSE},
649 {PLV1_KEY_EXCHANGE, 0, 1, TRUE, FALSE},
650 {PLV1_ID, 0, 2, TRUE, FALSE},
651 {PLV1_NAT_OA, 0, 2, TRUE, FALSE},
652 {PLV1_NAT_OA_DRAFT_00_03, 0, 2, TRUE, FALSE},
653 };
654
655 /**
656 * payload order for QUICK_MODE from initiator.
657 */
658 static payload_order_t quick_mode_i_order[] = {
659 /* payload type notify type */
660 {PLV1_NOTIFY, 0},
661 {PLV1_VENDOR_ID, 0},
662 {PLV1_HASH, 0},
663 {PLV1_SECURITY_ASSOCIATION, 0},
664 {PLV1_NONCE, 0},
665 {PLV1_KEY_EXCHANGE, 0},
666 {PLV1_ID, 0},
667 {PLV1_NAT_OA, 0},
668 {PLV1_NAT_OA_DRAFT_00_03, 0},
669 };
670
671 /**
672 * Message rule for QUICK_MODE from responder.
673 */
674 static payload_rule_t quick_mode_r_rules[] = {
675 /* payload type min max encr suff */
676 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
677 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
678 {PLV1_HASH, 0, 1, TRUE, FALSE},
679 {PLV1_SECURITY_ASSOCIATION, 0, 2, TRUE, FALSE},
680 {PLV1_NONCE, 0, 1, TRUE, FALSE},
681 {PLV1_KEY_EXCHANGE, 0, 1, TRUE, FALSE},
682 {PLV1_ID, 0, 2, TRUE, FALSE},
683 {PLV1_NAT_OA, 0, 2, TRUE, FALSE},
684 {PLV1_NAT_OA_DRAFT_00_03, 0, 2, TRUE, FALSE},
685 };
686
687 /**
688 * payload order for QUICK_MODE from responder.
689 */
690 static payload_order_t quick_mode_r_order[] = {
691 /* payload type notify type */
692 {PLV1_NOTIFY, 0},
693 {PLV1_VENDOR_ID, 0},
694 {PLV1_HASH, 0},
695 {PLV1_SECURITY_ASSOCIATION, 0},
696 {PLV1_NONCE, 0},
697 {PLV1_KEY_EXCHANGE, 0},
698 {PLV1_ID, 0},
699 {PLV1_NAT_OA, 0},
700 {PLV1_NAT_OA_DRAFT_00_03, 0},
701 };
702
703 /**
704 * Message rule for TRANSACTION.
705 */
706 static payload_rule_t transaction_payload_rules_v1[] = {
707 /* payload type min max encr suff */
708 {PLV1_HASH, 0, 1, TRUE, FALSE},
709 {PLV1_CONFIGURATION, 1, 1, FALSE, FALSE},
710 };
711
712 /**
713 * Payload order for TRANSACTION.
714 */
715 static payload_order_t transaction_payload_order_v1[] = {
716 /* payload type notify type */
717 {PLV1_HASH, 0},
718 {PLV1_CONFIGURATION, 0},
719 };
720
721 #endif /* USE_IKEV1 */
722
723 /**
724 * Message rules, defines allowed payloads.
725 */
726 static message_rule_t message_rules[] = {
727 {IKE_SA_INIT, TRUE, FALSE,
728 countof(ike_sa_init_i_rules), ike_sa_init_i_rules,
729 countof(ike_sa_init_i_order), ike_sa_init_i_order,
730 },
731 {IKE_SA_INIT, FALSE, FALSE,
732 countof(ike_sa_init_r_rules), ike_sa_init_r_rules,
733 countof(ike_sa_init_r_order), ike_sa_init_r_order,
734 },
735 {IKE_AUTH, TRUE, TRUE,
736 countof(ike_auth_i_rules), ike_auth_i_rules,
737 countof(ike_auth_i_order), ike_auth_i_order,
738 },
739 {IKE_AUTH, FALSE, TRUE,
740 countof(ike_auth_r_rules), ike_auth_r_rules,
741 countof(ike_auth_r_order), ike_auth_r_order,
742 },
743 {INFORMATIONAL, TRUE, TRUE,
744 countof(informational_i_rules), informational_i_rules,
745 countof(informational_i_order), informational_i_order,
746 },
747 {INFORMATIONAL, FALSE, TRUE,
748 countof(informational_r_rules), informational_r_rules,
749 countof(informational_r_order), informational_r_order,
750 },
751 {CREATE_CHILD_SA, TRUE, TRUE,
752 countof(create_child_sa_i_rules), create_child_sa_i_rules,
753 countof(create_child_sa_i_order), create_child_sa_i_order,
754 },
755 {CREATE_CHILD_SA, FALSE, TRUE,
756 countof(create_child_sa_r_rules), create_child_sa_r_rules,
757 countof(create_child_sa_r_order), create_child_sa_r_order,
758 },
759 #ifdef ME
760 {ME_CONNECT, TRUE, TRUE,
761 countof(me_connect_i_rules), me_connect_i_rules,
762 countof(me_connect_i_order), me_connect_i_order,
763 },
764 {ME_CONNECT, FALSE, TRUE,
765 countof(me_connect_r_rules), me_connect_r_rules,
766 countof(me_connect_r_order), me_connect_r_order,
767 },
768 #endif /* ME */
769 #ifdef USE_IKEV1
770 {ID_PROT, TRUE, FALSE,
771 countof(id_prot_i_rules), id_prot_i_rules,
772 countof(id_prot_i_order), id_prot_i_order,
773 },
774 {ID_PROT, FALSE, FALSE,
775 countof(id_prot_r_rules), id_prot_r_rules,
776 countof(id_prot_r_order), id_prot_r_order,
777 },
778 {AGGRESSIVE, TRUE, FALSE,
779 countof(aggressive_i_rules), aggressive_i_rules,
780 countof(aggressive_i_order), aggressive_i_order,
781 },
782 {AGGRESSIVE, FALSE, FALSE,
783 countof(aggressive_r_rules), aggressive_r_rules,
784 countof(aggressive_r_order), aggressive_r_order,
785 },
786 {INFORMATIONAL_V1, TRUE, TRUE,
787 countof(informational_i_rules_v1), informational_i_rules_v1,
788 countof(informational_i_order_v1), informational_i_order_v1,
789 },
790 {INFORMATIONAL_V1, FALSE, TRUE,
791 countof(informational_r_rules_v1), informational_r_rules_v1,
792 countof(informational_r_order_v1), informational_r_order_v1,
793 },
794 {QUICK_MODE, TRUE, TRUE,
795 countof(quick_mode_i_rules), quick_mode_i_rules,
796 countof(quick_mode_i_order), quick_mode_i_order,
797 },
798 {QUICK_MODE, FALSE, TRUE,
799 countof(quick_mode_r_rules), quick_mode_r_rules,
800 countof(quick_mode_r_order), quick_mode_r_order,
801 },
802 {TRANSACTION, TRUE, TRUE,
803 countof(transaction_payload_rules_v1), transaction_payload_rules_v1,
804 countof(transaction_payload_order_v1), transaction_payload_order_v1,
805 },
806 {TRANSACTION, FALSE, TRUE,
807 countof(transaction_payload_rules_v1), transaction_payload_rules_v1,
808 countof(transaction_payload_order_v1), transaction_payload_order_v1,
809 },
810 /* TODO-IKEv1: define rules for other exchanges */
811 #endif /* USE_IKEV1 */
812 };
813
814 /**
815 * Data for fragment reassembly.
816 */
817 typedef struct {
818
819 /**
820 * For IKEv1 the number of the last fragment (in case we receive them out
821 * of order), since the first one starts with 1 this defines the number of
822 * fragments we expect.
823 * For IKEv2 we store the total number of fragment we received last.
824 */
825 u_int16_t last;
826
827 /**
828 * Length of all currently received fragments.
829 */
830 size_t len;
831
832 /**
833 * Maximum length of a fragmented packet.
834 */
835 size_t max_packet;
836
837 } fragment_data_t;
838
839 typedef struct private_message_t private_message_t;
840
841 /**
842 * Private data of an message_t object.
843 */
844 struct private_message_t {
845
846 /**
847 * Public part of a message_t object.
848 */
849 message_t public;
850
851 /**
852 * Minor version of message.
853 */
854 u_int8_t major_version;
855
856 /**
857 * Major version of message.
858 */
859 u_int8_t minor_version;
860
861 /**
862 * First Payload in message.
863 */
864 payload_type_t first_payload;
865
866 /**
867 * Assigned exchange type.
868 */
869 exchange_type_t exchange_type;
870
871 /**
872 * TRUE if message is a request, FALSE if a reply.
873 */
874 bool is_request;
875
876 /**
877 * The message is encrypted (IKEv1)
878 */
879 bool is_encrypted;
880
881 /**
882 * Higher version supported?
883 */
884 bool version_flag;
885
886 /**
887 * Reserved bits in IKE header
888 */
889 bool reserved[2];
890
891 /**
892 * Sorting of message disabled?
893 */
894 bool sort_disabled;
895
896 /**
897 * Message ID of this message.
898 */
899 u_int32_t message_id;
900
901 /**
902 * ID of assigned IKE_SA.
903 */
904 ike_sa_id_t *ike_sa_id;
905
906 /**
907 * Assigned UDP packet, stores incoming packet or last generated one.
908 */
909 packet_t *packet;
910
911 /**
912 * Array of generated fragments (if any), as packet_t*.
913 * If defragmenting (i.e. frag != NULL) this contains fragment_t*
914 */
915 array_t *fragments;
916
917 /**
918 * Linked List where payload data are stored in.
919 */
920 linked_list_t *payloads;
921
922 /**
923 * Assigned parser to parse Header and Body of this message.
924 */
925 parser_t *parser;
926
927 /**
928 * The message rule for this message instance
929 */
930 message_rule_t *rule;
931
932 /**
933 * Data used to reassemble a fragmented message
934 */
935 fragment_data_t *frag;
936 };
937
938 /**
939 * A single fragment within a fragmented message
940 */
941 typedef struct {
942
943 /** fragment number */
944 u_int8_t num;
945
946 /** fragment data */
947 chunk_t data;
948
949 } fragment_t;
950
951 static void fragment_destroy(fragment_t *this)
952 {
953 chunk_free(&this->data);
954 free(this);
955 }
956
957 static void reset_defrag(private_message_t *this)
958 {
959 array_destroy_function(this->fragments, (void*)fragment_destroy, NULL);
960 this->fragments = NULL;
961 this->frag->last = 0;
962 this->frag->len = 0;
963 }
964
965 /**
966 * Get the message rule that applies to this message
967 */
968 static message_rule_t* get_message_rule(private_message_t *this)
969 {
970 int i;
971
972 for (i = 0; i < countof(message_rules); i++)
973 {
974 if ((this->exchange_type == message_rules[i].exchange_type) &&
975 (this->is_request == message_rules[i].is_request))
976 {
977 return &message_rules[i];
978 }
979 }
980 return NULL;
981 }
982
983 /**
984 * Look up a payload rule
985 */
986 static payload_rule_t* get_payload_rule(private_message_t *this,
987 payload_type_t type)
988 {
989 int i;
990
991 for (i = 0; i < this->rule->rule_count;i++)
992 {
993 if (this->rule->rules[i].type == type)
994 {
995 return &this->rule->rules[i];
996 }
997 }
998 return NULL;
999 }
1000
1001 METHOD(message_t, set_ike_sa_id, void,
1002 private_message_t *this,ike_sa_id_t *ike_sa_id)
1003 {
1004 DESTROY_IF(this->ike_sa_id);
1005 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
1006 }
1007
1008 METHOD(message_t, get_ike_sa_id, ike_sa_id_t*,
1009 private_message_t *this)
1010 {
1011 return this->ike_sa_id;
1012 }
1013
1014 METHOD(message_t, set_message_id, void,
1015 private_message_t *this,u_int32_t message_id)
1016 {
1017 this->message_id = message_id;
1018 }
1019
1020 METHOD(message_t, get_message_id, u_int32_t,
1021 private_message_t *this)
1022 {
1023 return this->message_id;
1024 }
1025
1026 METHOD(message_t, get_initiator_spi, u_int64_t,
1027 private_message_t *this)
1028 {
1029 return (this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
1030 }
1031
1032 METHOD(message_t, get_responder_spi, u_int64_t,
1033 private_message_t *this)
1034 {
1035 return (this->ike_sa_id->get_responder_spi(this->ike_sa_id));
1036 }
1037
1038 METHOD(message_t, set_major_version, void,
1039 private_message_t *this, u_int8_t major_version)
1040 {
1041 this->major_version = major_version;
1042 }
1043
1044 METHOD(message_t, get_major_version, u_int8_t,
1045 private_message_t *this)
1046 {
1047 return this->major_version;
1048 }
1049
1050 METHOD(message_t, set_minor_version, void,
1051 private_message_t *this,u_int8_t minor_version)
1052 {
1053 this->minor_version = minor_version;
1054 }
1055
1056 METHOD(message_t, get_minor_version, u_int8_t,
1057 private_message_t *this)
1058 {
1059 return this->minor_version;
1060 }
1061
1062 METHOD(message_t, set_exchange_type, void,
1063 private_message_t *this, exchange_type_t exchange_type)
1064 {
1065 this->exchange_type = exchange_type;
1066 }
1067
1068 METHOD(message_t, get_exchange_type, exchange_type_t,
1069 private_message_t *this)
1070 {
1071 return this->exchange_type;
1072 }
1073
1074 METHOD(message_t, get_first_payload_type, payload_type_t,
1075 private_message_t *this)
1076 {
1077 return this->first_payload;
1078 }
1079
1080 METHOD(message_t, set_request, void,
1081 private_message_t *this, bool request)
1082 {
1083 this->is_request = request;
1084 }
1085
1086 METHOD(message_t, get_request, bool,
1087 private_message_t *this)
1088 {
1089 return this->is_request;
1090 }
1091
1092 METHOD(message_t, set_version_flag, void,
1093 private_message_t *this)
1094 {
1095 this->version_flag = TRUE;
1096 }
1097
1098 METHOD(message_t, get_reserved_header_bit, bool,
1099 private_message_t *this, u_int nr)
1100 {
1101 if (nr < countof(this->reserved))
1102 {
1103 return this->reserved[nr];
1104 }
1105 return FALSE;
1106 }
1107
1108 METHOD(message_t, set_reserved_header_bit, void,
1109 private_message_t *this, u_int nr)
1110 {
1111 if (nr < countof(this->reserved))
1112 {
1113 this->reserved[nr] = TRUE;
1114 }
1115 }
1116
1117 METHOD(message_t, is_encoded, bool,
1118 private_message_t *this)
1119 {
1120 return this->packet->get_data(this->packet).ptr != NULL;
1121 }
1122
1123 METHOD(message_t, is_fragmented, bool,
1124 private_message_t *this)
1125 {
1126 return array_count(this->fragments) > 0;
1127 }
1128
1129 METHOD(message_t, add_payload, void,
1130 private_message_t *this, payload_t *payload)
1131 {
1132 payload_t *last_payload;
1133
1134 if (this->payloads->get_count(this->payloads) > 0)
1135 {
1136 this->payloads->get_last(this->payloads, (void **)&last_payload);
1137 last_payload->set_next_type(last_payload, payload->get_type(payload));
1138 }
1139 else
1140 {
1141 this->first_payload = payload->get_type(payload);
1142 }
1143 payload->set_next_type(payload, PL_NONE);
1144 this->payloads->insert_last(this->payloads, payload);
1145
1146 DBG2(DBG_ENC ,"added payload of type %N to message",
1147 payload_type_names, payload->get_type(payload));
1148 }
1149
1150 METHOD(message_t, add_notify, void,
1151 private_message_t *this, bool flush, notify_type_t type, chunk_t data)
1152 {
1153 notify_payload_t *notify;
1154 payload_t *payload;
1155
1156 if (flush)
1157 {
1158 while (this->payloads->remove_last(this->payloads,
1159 (void**)&payload) == SUCCESS)
1160 {
1161 payload->destroy(payload);
1162 }
1163 }
1164 if (this->major_version == IKEV2_MAJOR_VERSION)
1165 {
1166 notify = notify_payload_create(PLV2_NOTIFY);
1167 }
1168 else
1169 {
1170 notify = notify_payload_create(PLV1_NOTIFY);
1171 }
1172 notify->set_notify_type(notify, type);
1173 notify->set_notification_data(notify, data);
1174 add_payload(this, (payload_t*)notify);
1175 }
1176
1177 METHOD(message_t, set_source, void,
1178 private_message_t *this, host_t *host)
1179 {
1180 this->packet->set_source(this->packet, host);
1181 }
1182
1183 METHOD(message_t, set_destination, void,
1184 private_message_t *this, host_t *host)
1185 {
1186 this->packet->set_destination(this->packet, host);
1187 }
1188
1189 METHOD(message_t, get_source, host_t*,
1190 private_message_t *this)
1191 {
1192 return this->packet->get_source(this->packet);
1193 }
1194
1195 METHOD(message_t, get_destination, host_t*,
1196 private_message_t *this)
1197 {
1198 return this->packet->get_destination(this->packet);
1199 }
1200
1201 METHOD(message_t, create_payload_enumerator, enumerator_t*,
1202 private_message_t *this)
1203 {
1204 return this->payloads->create_enumerator(this->payloads);
1205 }
1206
1207 METHOD(message_t, remove_payload_at, void,
1208 private_message_t *this, enumerator_t *enumerator)
1209 {
1210 this->payloads->remove_at(this->payloads, enumerator);
1211 }
1212
1213 METHOD(message_t, get_payload, payload_t*,
1214 private_message_t *this, payload_type_t type)
1215 {
1216 payload_t *current, *found = NULL;
1217 enumerator_t *enumerator;
1218
1219 enumerator = create_payload_enumerator(this);
1220 while (enumerator->enumerate(enumerator, &current))
1221 {
1222 if (current->get_type(current) == type)
1223 {
1224 found = current;
1225 break;
1226 }
1227 }
1228 enumerator->destroy(enumerator);
1229 return found;
1230 }
1231
1232 METHOD(message_t, get_notify, notify_payload_t*,
1233 private_message_t *this, notify_type_t type)
1234 {
1235 enumerator_t *enumerator;
1236 notify_payload_t *notify = NULL;
1237 payload_t *payload;
1238
1239 enumerator = create_payload_enumerator(this);
1240 while (enumerator->enumerate(enumerator, &payload))
1241 {
1242 if (payload->get_type(payload) == PLV2_NOTIFY ||
1243 payload->get_type(payload) == PLV1_NOTIFY)
1244 {
1245 notify = (notify_payload_t*)payload;
1246 if (notify->get_notify_type(notify) == type)
1247 {
1248 break;
1249 }
1250 notify = NULL;
1251 }
1252 }
1253 enumerator->destroy(enumerator);
1254 return notify;
1255 }
1256
1257 /**
1258 * get a string representation of the message
1259 */
1260 static char* get_string(private_message_t *this, char *buf, int len)
1261 {
1262 enumerator_t *enumerator;
1263 payload_t *payload;
1264 int written;
1265 char *pos = buf;
1266
1267 memset(buf, 0, len);
1268 len--;
1269
1270 written = snprintf(pos, len, "%N %s %u [",
1271 exchange_type_names, this->exchange_type,
1272 this->is_request ? "request" : "response",
1273 this->message_id);
1274 if (written >= len || written < 0)
1275 {
1276 return "";
1277 }
1278 pos += written;
1279 len -= written;
1280
1281 enumerator = create_payload_enumerator(this);
1282 while (enumerator->enumerate(enumerator, &payload))
1283 {
1284 written = snprintf(pos, len, " %N", payload_type_short_names,
1285 payload->get_type(payload));
1286 if (written >= len || written < 0)
1287 {
1288 return buf;
1289 }
1290 pos += written;
1291 len -= written;
1292 if (payload->get_type(payload) == PLV2_NOTIFY ||
1293 payload->get_type(payload) == PLV1_NOTIFY)
1294 {
1295 notify_payload_t *notify;
1296 notify_type_t type;
1297 chunk_t data;
1298
1299 notify = (notify_payload_t*)payload;
1300 type = notify->get_notify_type(notify);
1301 data = notify->get_notification_data(notify);
1302 if (type == MS_NOTIFY_STATUS && data.len == 4)
1303 {
1304 written = snprintf(pos, len, "(%N(%d))", notify_type_short_names,
1305 type, untoh32(data.ptr));
1306 }
1307 else
1308 {
1309 written = snprintf(pos, len, "(%N)", notify_type_short_names,
1310 type);
1311 }
1312 if (written >= len || written < 0)
1313 {
1314 return buf;
1315 }
1316 pos += written;
1317 len -= written;
1318 }
1319 if (payload->get_type(payload) == PLV2_EAP)
1320 {
1321 eap_payload_t *eap = (eap_payload_t*)payload;
1322 u_int32_t vendor;
1323 eap_type_t type;
1324 char method[64] = "";
1325
1326 type = eap->get_type(eap, &vendor);
1327 if (type)
1328 {
1329 if (vendor)
1330 {
1331 snprintf(method, sizeof(method), "/%d-%d", type, vendor);
1332 }
1333 else
1334 {
1335 snprintf(method, sizeof(method), "/%N",
1336 eap_type_short_names, type);
1337 }
1338 }
1339 written = snprintf(pos, len, "/%N%s", eap_code_short_names,
1340 eap->get_code(eap), method);
1341 if (written >= len || written < 0)
1342 {
1343 return buf;
1344 }
1345 pos += written;
1346 len -= written;
1347 }
1348 if (payload->get_type(payload) == PLV2_CONFIGURATION ||
1349 payload->get_type(payload) == PLV1_CONFIGURATION)
1350 {
1351 cp_payload_t *cp = (cp_payload_t*)payload;
1352 enumerator_t *attributes;
1353 configuration_attribute_t *attribute;
1354 bool first = TRUE;
1355 char *pfx;
1356
1357 switch (cp->get_type(cp))
1358 {
1359 case CFG_REQUEST:
1360 pfx = "RQ(";
1361 break;
1362 case CFG_REPLY:
1363 pfx = "RP(";
1364 break;
1365 case CFG_SET:
1366 pfx = "S(";
1367 break;
1368 case CFG_ACK:
1369 pfx = "A(";
1370 break;
1371 default:
1372 pfx = "(";
1373 break;
1374 }
1375
1376 attributes = cp->create_attribute_enumerator(cp);
1377 while (attributes->enumerate(attributes, &attribute))
1378 {
1379 written = snprintf(pos, len, "%s%N", first ? pfx : " ",
1380 configuration_attribute_type_short_names,
1381 attribute->get_type(attribute));
1382 if (written >= len || written < 0)
1383 {
1384 return buf;
1385 }
1386 pos += written;
1387 len -= written;
1388 first = FALSE;
1389 }
1390 attributes->destroy(attributes);
1391 if (!first)
1392 {
1393 written = snprintf(pos, len, ")");
1394 if (written >= len || written < 0)
1395 {
1396 return buf;
1397 }
1398 pos += written;
1399 len -= written;
1400 }
1401 }
1402 }
1403 enumerator->destroy(enumerator);
1404
1405 /* remove last space */
1406 snprintf(pos, len, " ]");
1407 return buf;
1408 }
1409
1410 METHOD(message_t, disable_sort, void,
1411 private_message_t *this)
1412 {
1413 this->sort_disabled = TRUE;
1414 }
1415
1416 /**
1417 * reorder payloads depending on reordering rules
1418 */
1419 static void order_payloads(private_message_t *this)
1420 {
1421 linked_list_t *list;
1422 payload_t *payload;
1423 int i;
1424
1425 DBG2(DBG_ENC, "order payloads in message");
1426
1427 /* move to temp list */
1428 list = linked_list_create();
1429 while (this->payloads->remove_last(this->payloads,
1430 (void**)&payload) == SUCCESS)
1431 {
1432 list->insert_first(list, payload);
1433 }
1434 /* for each rule, ... */
1435 for (i = 0; i < this->rule->order_count; i++)
1436 {
1437 enumerator_t *enumerator;
1438 notify_payload_t *notify;
1439 payload_order_t order;
1440
1441 order = this->rule->order[i];
1442
1443 /* ... find all payload ... */
1444 enumerator = list->create_enumerator(list);
1445 while (enumerator->enumerate(enumerator, &payload))
1446 {
1447 /* ... with that type ... */
1448 if (payload->get_type(payload) == order.type)
1449 {
1450 notify = (notify_payload_t*)payload;
1451
1452 /**... and check notify for type. */
1453 if (order.type != PLV2_NOTIFY || order.notify == 0 ||
1454 order.notify == notify->get_notify_type(notify))
1455 {
1456 list->remove_at(list, enumerator);
1457 add_payload(this, payload);
1458 }
1459 }
1460 }
1461 enumerator->destroy(enumerator);
1462 }
1463 /* append all payloads without a rule to the end */
1464 while (list->remove_last(list, (void**)&payload) == SUCCESS)
1465 {
1466 /* do not complain about payloads in private use space */
1467 if (payload->get_type(payload) < 128)
1468 {
1469 DBG1(DBG_ENC, "payload %N has no ordering rule in %N %s",
1470 payload_type_names, payload->get_type(payload),
1471 exchange_type_names, this->rule->exchange_type,
1472 this->rule->is_request ? "request" : "response");
1473 }
1474 add_payload(this, payload);
1475 }
1476 list->destroy(list);
1477 }
1478
1479 /**
1480 * Wrap payloads in an encrypted payload
1481 */
1482 static encrypted_payload_t* wrap_payloads(private_message_t *this)
1483 {
1484 encrypted_payload_t *encrypted = NULL;
1485 linked_list_t *payloads;
1486 payload_t *current;
1487
1488 /* move all payloads to a temporary list */
1489 payloads = linked_list_create();
1490 while (this->payloads->remove_first(this->payloads,
1491 (void**)&current) == SUCCESS)
1492 {
1493 if (current->get_type(current) == PLV2_FRAGMENT)
1494 { /* treat encrypted fragment payload as encrypted payload */
1495 encrypted = (encrypted_payload_t*)current;
1496 }
1497 else
1498 {
1499 payloads->insert_last(payloads, current);
1500 }
1501 }
1502 if (encrypted)
1503 { /* simply adopt all the unencrypted payloads */
1504 this->payloads->destroy(this->payloads);
1505 this->payloads = payloads;
1506 return encrypted;
1507 }
1508
1509 if (this->is_encrypted)
1510 {
1511 encrypted = encrypted_payload_create(PLV1_ENCRYPTED);
1512 }
1513 else
1514 {
1515 encrypted = encrypted_payload_create(PLV2_ENCRYPTED);
1516 }
1517 while (payloads->remove_first(payloads, (void**)&current) == SUCCESS)
1518 {
1519 payload_rule_t *rule;
1520 payload_type_t type;
1521 bool encrypt = TRUE;
1522
1523 type = current->get_type(current);
1524 rule = get_payload_rule(this, type);
1525 if (rule)
1526 {
1527 encrypt = rule->encrypted;
1528 }
1529 if (encrypt || this->is_encrypted)
1530 { /* encryption is forced for IKEv1 */
1531 DBG2(DBG_ENC, "insert payload %N into encrypted payload",
1532 payload_type_names, type);
1533 encrypted->add_payload(encrypted, current);
1534 }
1535 else
1536 {
1537 DBG2(DBG_ENC, "insert payload %N unencrypted",
1538 payload_type_names, type);
1539 add_payload(this, current);
1540 }
1541 }
1542 payloads->destroy(payloads);
1543
1544 return encrypted;
1545 }
1546
1547 /**
1548 * Creates the IKE header for this message
1549 */
1550 static ike_header_t *create_header(private_message_t *this)
1551 {
1552 ike_header_t *ike_header;
1553 bool *reserved;
1554 int i;
1555
1556 ike_header = ike_header_create_version(this->major_version,
1557 this->minor_version);
1558 ike_header->set_exchange_type(ike_header, this->exchange_type);
1559 ike_header->set_message_id(ike_header, this->message_id);
1560 if (this->major_version == IKEV2_MAJOR_VERSION)
1561 {
1562 ike_header->set_response_flag(ike_header, !this->is_request);
1563 ike_header->set_version_flag(ike_header, this->version_flag);
1564 ike_header->set_initiator_flag(ike_header,
1565 this->ike_sa_id->is_initiator(this->ike_sa_id));
1566 }
1567 else
1568 {
1569 ike_header->set_encryption_flag(ike_header, this->is_encrypted);
1570 }
1571 ike_header->set_initiator_spi(ike_header,
1572 this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
1573 ike_header->set_responder_spi(ike_header,
1574 this->ike_sa_id->get_responder_spi(this->ike_sa_id));
1575
1576 for (i = 0; i < countof(this->reserved); i++)
1577 {
1578 reserved = payload_get_field(&ike_header->payload_interface,
1579 RESERVED_BIT, i);
1580 if (reserved)
1581 {
1582 *reserved = this->reserved[i];
1583 }
1584 }
1585 return ike_header;
1586 }
1587
1588 /**
1589 * Generates the message, if needed, wraps the payloads in an encrypted payload.
1590 *
1591 * The generator and the possible enrypted payload are returned. The latter
1592 * is not yet encrypted (but the transform is set). It is also not added to
1593 * the payload list (so unless there are unencrypted payloads that list will
1594 * be empty afterwards).
1595 */
1596 static status_t generate_message(private_message_t *this, keymat_t *keymat,
1597 generator_t **out_generator, encrypted_payload_t **encrypted)
1598 {
1599 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
1600 generator_t *generator;
1601 payload_type_t next_type;
1602 enumerator_t *enumerator;
1603 aead_t *aead = NULL;
1604 chunk_t hash = chunk_empty;
1605 char str[BUF_LEN];
1606 ike_header_t *ike_header;
1607 payload_t *payload, *next;
1608 bool encrypting = FALSE;
1609
1610 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
1611 {
1612 DBG1(DBG_ENC, "exchange type is not defined");
1613 return INVALID_STATE;
1614 }
1615
1616 if (this->packet->get_source(this->packet) == NULL ||
1617 this->packet->get_destination(this->packet) == NULL)
1618 {
1619 DBG1(DBG_ENC, "source/destination not defined");
1620 return INVALID_STATE;
1621 }
1622
1623 this->rule = get_message_rule(this);
1624 if (!this->rule)
1625 {
1626 DBG1(DBG_ENC, "no message rules specified for this message type");
1627 return NOT_SUPPORTED;
1628 }
1629
1630 if (!this->sort_disabled)
1631 {
1632 order_payloads(this);
1633 }
1634
1635 if (keymat && keymat->get_version(keymat) == IKEV1)
1636 {
1637 /* get a hash for this message, if any is required */
1638 if (keymat_v1->get_hash_phase2(keymat_v1, &this->public, &hash))
1639 { /* insert a HASH payload as first payload */
1640 hash_payload_t *hash_payload;
1641
1642 hash_payload = hash_payload_create(PLV1_HASH);
1643 hash_payload->set_hash(hash_payload, hash);
1644 this->payloads->insert_first(this->payloads, hash_payload);
1645 if (this->exchange_type == INFORMATIONAL_V1)
1646 {
1647 this->is_encrypted = encrypting = TRUE;
1648 }
1649 chunk_free(&hash);
1650 }
1651 }
1652
1653 if (this->major_version == IKEV2_MAJOR_VERSION)
1654 {
1655 encrypting = this->rule->encrypted;
1656 }
1657 else if (!encrypting)
1658 {
1659 /* If at least one payload requires encryption, encrypt the message.
1660 * If no key material is available, the flag will be reset below. */
1661 enumerator = this->payloads->create_enumerator(this->payloads);
1662 while (enumerator->enumerate(enumerator, (void**)&payload))
1663 {
1664 payload_rule_t *rule;
1665
1666 rule = get_payload_rule(this, payload->get_type(payload));
1667 if (rule && rule->encrypted)
1668 {
1669 this->is_encrypted = encrypting = TRUE;
1670 break;
1671 }
1672 }
1673 enumerator->destroy(enumerator);
1674 }
1675
1676 DBG1(DBG_ENC, "generating %s", get_string(this, str, sizeof(str)));
1677
1678 if (keymat)
1679 {
1680 aead = keymat->get_aead(keymat, FALSE);
1681 }
1682 if (aead && encrypting)
1683 {
1684 *encrypted = wrap_payloads(this);
1685 (*encrypted)->set_transform(*encrypted, aead);
1686 }
1687 else
1688 {
1689 DBG2(DBG_ENC, "not encrypting payloads");
1690 this->is_encrypted = FALSE;
1691 }
1692
1693 /* generate all payloads with proper next type */
1694 *out_generator = generator = generator_create();
1695 ike_header = create_header(this);
1696 payload = (payload_t*)ike_header;
1697 enumerator = create_payload_enumerator(this);
1698 while (enumerator->enumerate(enumerator, &next))
1699 {
1700 payload->set_next_type(payload, next->get_type(next));
1701 generator->generate_payload(generator, payload);
1702 payload = next;
1703 }
1704 enumerator->destroy(enumerator);
1705
1706 next_type = PL_NONE;
1707 if (this->is_encrypted)
1708 { /* for encrypted IKEv1 messages */
1709 next_type = (*encrypted)->payload_interface.get_next_type(
1710 (payload_t*)*encrypted);
1711 }
1712 else if (*encrypted)
1713 { /* use proper IKEv2 encrypted (fragment) payload type */
1714 next_type = (*encrypted)->payload_interface.get_type(
1715 (payload_t*)*encrypted);
1716 }
1717 payload->set_next_type(payload, next_type);
1718 generator->generate_payload(generator, payload);
1719 ike_header->destroy(ike_header);
1720 return SUCCESS;
1721 }
1722
1723 /**
1724 * Encrypts and adds the encrypted payload (if any) to the payload list and
1725 * finalizes the message generation. Destroys the given generator.
1726 */
1727 static status_t finalize_message(private_message_t *this, keymat_t *keymat,
1728 generator_t *generator, encrypted_payload_t *encrypted)
1729 {
1730 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
1731 chunk_t chunk;
1732 u_int32_t *lenpos;
1733
1734 if (encrypted)
1735 {
1736 if (this->is_encrypted)
1737 { /* for IKEv1 instead of associated data we provide the IV */
1738 if (!keymat_v1->get_iv(keymat_v1, this->message_id, &chunk))
1739 {
1740 generator->destroy(generator);
1741 encrypted->destroy(encrypted);
1742 return FAILED;
1743 }
1744 }
1745 else
1746 { /* build associated data (without header of encrypted payload) */
1747 chunk = generator->get_chunk(generator, &lenpos);
1748 /* fill in length, including encrypted payload */
1749 htoun32(lenpos, chunk.len + encrypted->get_length(encrypted));
1750 }
1751 this->payloads->insert_last(this->payloads, encrypted);
1752 if (encrypted->encrypt(encrypted, this->message_id, chunk) != SUCCESS)
1753 {
1754 generator->destroy(generator);
1755 return INVALID_STATE;
1756 }
1757 generator->generate_payload(generator, &encrypted->payload_interface);
1758 }
1759 chunk = generator->get_chunk(generator, &lenpos);
1760 htoun32(lenpos, chunk.len);
1761 this->packet->set_data(this->packet, chunk_clone(chunk));
1762 if (this->is_encrypted && this->exchange_type != INFORMATIONAL_V1)
1763 {
1764 /* update the IV for the next IKEv1 message */
1765 chunk_t last_block;
1766 aead_t *aead;
1767 size_t bs;
1768
1769 aead = keymat->get_aead(keymat, FALSE);
1770 bs = aead->get_block_size(aead);
1771 last_block = chunk_create(chunk.ptr + chunk.len - bs, bs);
1772 if (!keymat_v1->update_iv(keymat_v1, this->message_id, last_block) ||
1773 !keymat_v1->confirm_iv(keymat_v1, this->message_id))
1774 {
1775 generator->destroy(generator);
1776 return FAILED;
1777 }
1778 }
1779 generator->destroy(generator);
1780 return SUCCESS;
1781 }
1782
1783 METHOD(message_t, generate, status_t,
1784 private_message_t *this, keymat_t *keymat, packet_t **packet)
1785 {
1786 generator_t *generator = NULL;
1787 encrypted_payload_t *encrypted = NULL;
1788 status_t status;
1789
1790 status = generate_message(this, keymat, &generator, &encrypted);
1791 if (status != SUCCESS)
1792 {
1793 DESTROY_IF(generator);
1794 return status;
1795 }
1796 status = finalize_message(this, keymat, generator, encrypted);
1797 if (status != SUCCESS)
1798 {
1799 return status;
1800 }
1801 if (packet)
1802 {
1803 *packet = this->packet->clone(this->packet);
1804 }
1805 return SUCCESS;
1806 }
1807
1808 /**
1809 * Creates a (basic) clone of the given message
1810 */
1811 static message_t *clone_message(private_message_t *this)
1812 {
1813 message_t *message;
1814 host_t *src, *dst;
1815
1816 src = this->packet->get_source(this->packet);
1817 dst = this->packet->get_destination(this->packet);
1818
1819 message = message_create(this->major_version, this->minor_version);
1820 message->set_ike_sa_id(message, this->ike_sa_id);
1821 message->set_message_id(message, this->message_id);
1822 message->set_request(message, this->is_request);
1823 message->set_source(message, src->clone(src));
1824 message->set_destination(message, dst->clone(dst));
1825 message->set_exchange_type(message, this->exchange_type);
1826 memcpy(((private_message_t*)message)->reserved, this->reserved,
1827 sizeof(this->reserved));
1828 return message;
1829 }
1830
1831 /**
1832 * Create a single fragment with the given data
1833 */
1834 static message_t *create_fragment(private_message_t *this, payload_type_t next,
1835 u_int16_t num, u_int16_t count, chunk_t data)
1836 {
1837 enumerator_t *enumerator;
1838 payload_t *fragment, *payload;
1839 message_t *message;
1840 peer_cfg_t *peer_cfg;
1841 ike_sa_t *ike_sa;
1842
1843 message = clone_message(this);
1844 if (this->major_version == IKEV1_MAJOR_VERSION)
1845 {
1846 /* other implementations seem to just use 0 as message ID, so here we go */
1847 message->set_message_id(message, 0);
1848 /* always use the initial message type for fragments, even for quick mode
1849 * or transaction messages. */
1850 ike_sa = charon->bus->get_sa(charon->bus);
1851 if (ike_sa && (peer_cfg = ike_sa->get_peer_cfg(ike_sa)) &&
1852 peer_cfg->use_aggressive(peer_cfg))
1853 {
1854 message->set_exchange_type(message, AGGRESSIVE);
1855 }
1856 else
1857 {
1858 message->set_exchange_type(message, ID_PROT);
1859 }
1860 fragment = (payload_t*)fragment_payload_create_from_data(
1861 num, num == count, data);
1862 }
1863 else
1864 {
1865 fragment = (payload_t*)encrypted_fragment_payload_create_from_data(
1866 num, count, data);
1867 if (num == 1)
1868 {
1869 /* only in the first fragment is this set to the type of the first
1870 * payload in the encrypted payload */
1871 fragment->set_next_type(fragment, next);
1872 /* move unencrypted payloads to the first fragment */
1873 enumerator = this->payloads->create_enumerator(this->payloads);
1874 while (enumerator->enumerate(enumerator, &payload))
1875 {
1876 if (payload->get_type(payload) != PLV2_ENCRYPTED)
1877 {
1878 this->payloads->remove_at(this->payloads, enumerator);
1879 message->add_payload(message, payload);
1880 }
1881 }
1882 enumerator->destroy(enumerator);
1883 }
1884 }
1885 message->add_payload(message, (payload_t*)fragment);
1886 return message;
1887 }
1888
1889 /**
1890 * Destroy all fragments
1891 */
1892 static void clear_fragments(private_message_t *this)
1893 {
1894 array_destroy_offset(this->fragments, offsetof(packet_t, destroy));
1895 this->fragments = NULL;
1896 }
1897
1898 METHOD(message_t, fragment, status_t,
1899 private_message_t *this, keymat_t *keymat, size_t frag_len,
1900 enumerator_t **fragments)
1901 {
1902 encrypted_payload_t *encrypted = NULL;
1903 generator_t *generator = NULL;
1904 message_t *fragment;
1905 packet_t *packet;
1906 payload_type_t next = PL_NONE;
1907 u_int16_t num, count;
1908 host_t *src, *dst;
1909 chunk_t data;
1910 status_t status;
1911 u_int32_t *lenpos;
1912 size_t len;
1913
1914 src = this->packet->get_source(this->packet);
1915 dst = this->packet->get_destination(this->packet);
1916 if (!frag_len)
1917 {
1918 frag_len = (src->get_family(src) == AF_INET) ? 576 : 1280;
1919 }
1920 /* frag_len is the complete IP datagram length, account for overhead (we
1921 * assume no IP options/extension headers are used) */
1922 frag_len -= (src->get_family(src) == AF_INET) ? 20 : 40;
1923 /* 8 (UDP header) */
1924 frag_len -= 8;
1925 if (dst->get_port(dst) != IKEV2_UDP_PORT &&
1926 src->get_port(src) != IKEV2_UDP_PORT)
1927 { /* reduce length due to non-ESP marker */
1928 frag_len -= 4;
1929 }
1930
1931 if (is_encoded(this))
1932 {
1933 if (this->major_version == IKEV2_MAJOR_VERSION)
1934 {
1935 encrypted = (encrypted_payload_t*)get_payload(this, PLV2_ENCRYPTED);
1936 }
1937 data = this->packet->get_data(this->packet);
1938 len = data.len;
1939 }
1940 else
1941 {
1942 status = generate_message(this, keymat, &generator, &encrypted);
1943 if (status != SUCCESS)
1944 {
1945 DESTROY_IF(generator);
1946 return status;
1947 }
1948 data = generator->get_chunk(generator, &lenpos);
1949 len = data.len + (encrypted ? encrypted->get_length(encrypted) : 0);
1950 }
1951
1952 /* check if we actually need to fragment the message and if we have an
1953 * encrypted payload for IKEv2 */
1954 if (len <= frag_len ||
1955 (this->major_version == IKEV2_MAJOR_VERSION && !encrypted))
1956 {
1957 if (generator)
1958 {
1959 status = finalize_message(this, keymat, generator, encrypted);
1960 if (status != SUCCESS)
1961 {
1962 return status;
1963 }
1964 }
1965 *fragments = enumerator_create_single(this->packet, NULL);
1966 return SUCCESS;
1967 }
1968
1969 /* frag_len denoted the maximum IKE message size so far, later on it will
1970 * denote the maximum content size of a fragment payload, therefore,
1971 * account for IKE header */
1972 frag_len -= 28;
1973
1974 if (this->major_version == IKEV1_MAJOR_VERSION)
1975 {
1976 if (generator)
1977 {
1978 status = finalize_message(this, keymat, generator, encrypted);
1979 if (status != SUCCESS)
1980 {
1981 return status;
1982 }
1983 data = this->packet->get_data(this->packet);
1984 generator = NULL;
1985 }
1986 /* overhead for the fragmentation payload header */
1987 frag_len -= 8;
1988 }
1989 else
1990 {
1991 aead_t *aead;
1992
1993 if (generator)
1994 {
1995 generator->destroy(generator);
1996 generator = generator_create();
1997 }
1998 else
1999 { /* do not log again if it was generated previously */
2000 generator = generator_create_no_dbg();
2001 }
2002 next = encrypted->payload_interface.get_next_type((payload_t*)encrypted);
2003 encrypted->generate_payloads(encrypted, generator);
2004 data = generator->get_chunk(generator, &lenpos);
2005 if (!is_encoded(this))
2006 {
2007 encrypted->destroy(encrypted);
2008 }
2009 aead = keymat->get_aead(keymat, FALSE);
2010 /* overhead for the encrypted fragment payload */
2011 frag_len -= aead->get_iv_size(aead) + aead->get_icv_size(aead);
2012 frag_len -= 8 /* header */;
2013 /* padding and padding length */
2014 frag_len = round_down(frag_len, aead->get_block_size(aead)) - 1;
2015 /* TODO-FRAG: if there are unencrypted payloads, should we account for
2016 * their length in the first fragment? we still would have to add
2017 * an encrypted fragment payload (albeit empty), even so we couldn't
2018 * prevent IP fragmentation in every case */
2019 }
2020
2021 count = data.len / frag_len + (data.len % frag_len ? 1 : 0);
2022 this->fragments = array_create(0, count);
2023 DBG1(DBG_ENC, "splitting IKE message with length of %zu bytes into "
2024 "%hu fragments", len, count);
2025 for (num = 1; num <= count; num++)
2026 {
2027 len = min(data.len, frag_len);
2028 fragment = create_fragment(this, next, num, count,
2029 chunk_create(data.ptr, len));
2030 status = fragment->generate(fragment, keymat, &packet);
2031 fragment->destroy(fragment);
2032 if (status != SUCCESS)
2033 {
2034 DBG1(DBG_ENC, "failed to generate IKE fragment");
2035 clear_fragments(this);
2036 DESTROY_IF(generator);
2037 return FAILED;
2038 }
2039 array_insert(this->fragments, ARRAY_TAIL, packet);
2040 data = chunk_skip(data, len);
2041 }
2042 *fragments = array_create_enumerator(this->fragments);
2043 DESTROY_IF(generator);
2044 return SUCCESS;
2045 }
2046
2047 METHOD(message_t, get_packet, packet_t*,
2048 private_message_t *this)
2049 {
2050 return this->packet->clone(this->packet);
2051 }
2052
2053 METHOD(message_t, get_packet_data, chunk_t,
2054 private_message_t *this)
2055 {
2056 return this->packet->get_data(this->packet);
2057 }
2058
2059 METHOD(message_t, get_fragments, enumerator_t*,
2060 private_message_t *this)
2061 {
2062 return array_create_enumerator(this->fragments);
2063 }
2064
2065 METHOD(message_t, parse_header, status_t,
2066 private_message_t *this)
2067 {
2068 ike_header_t *ike_header;
2069 status_t status;
2070 bool *reserved;
2071 int i;
2072
2073 DBG2(DBG_ENC, "parsing header of message");
2074
2075 if (!this->parser)
2076 { /* reassembled IKEv2 message, header is inherited from fragments */
2077 return SUCCESS;
2078 }
2079 this->parser->reset_context(this->parser);
2080 status = this->parser->parse_payload(this->parser, PL_HEADER,
2081 (payload_t**)&ike_header);
2082 if (status != SUCCESS)
2083 {
2084 DBG1(DBG_ENC, "header could not be parsed");
2085 return status;
2086
2087 }
2088
2089 status = ike_header->payload_interface.verify(
2090 &ike_header->payload_interface);
2091 if (status != SUCCESS)
2092 {
2093 DBG1(DBG_ENC, "header verification failed");
2094 ike_header->destroy(ike_header);
2095 return status;
2096 }
2097
2098 DESTROY_IF(this->ike_sa_id);
2099 this->ike_sa_id = ike_sa_id_create(
2100 ike_header->get_maj_version(ike_header),
2101 ike_header->get_initiator_spi(ike_header),
2102 ike_header->get_responder_spi(ike_header),
2103 ike_header->get_initiator_flag(ike_header));
2104
2105 this->exchange_type = ike_header->get_exchange_type(ike_header);
2106 this->message_id = ike_header->get_message_id(ike_header);
2107 this->major_version = ike_header->get_maj_version(ike_header);
2108 this->minor_version = ike_header->get_min_version(ike_header);
2109 if (this->major_version == IKEV2_MAJOR_VERSION)
2110 {
2111 this->is_request = !ike_header->get_response_flag(ike_header);
2112 }
2113 else
2114 {
2115 this->is_encrypted = ike_header->get_encryption_flag(ike_header);
2116 }
2117 this->first_payload = ike_header->payload_interface.get_next_type(
2118 &ike_header->payload_interface);
2119 if (this->first_payload == PLV1_FRAGMENT && this->is_encrypted)
2120 { /* racoon sets the encrypted bit when sending a fragment, but these
2121 * messages are really not encrypted */
2122 this->is_encrypted = FALSE;
2123 }
2124
2125 for (i = 0; i < countof(this->reserved); i++)
2126 {
2127 reserved = payload_get_field(&ike_header->payload_interface,
2128 RESERVED_BIT, i);
2129 if (reserved)
2130 {
2131 this->reserved[i] = *reserved;
2132 }
2133 }
2134 ike_header->destroy(ike_header);
2135
2136 DBG2(DBG_ENC, "parsed a %N %s header", exchange_type_names,
2137 this->exchange_type, this->major_version == IKEV1_MAJOR_VERSION ?
2138 "message" : (this->is_request ? "request" : "response"));
2139 return SUCCESS;
2140 }
2141
2142 /**
2143 * Check if a payload is for a mediation extension connectivity check
2144 */
2145 static bool is_connectivity_check(private_message_t *this, payload_t *payload)
2146 {
2147 #ifdef ME
2148 if (this->exchange_type == INFORMATIONAL &&
2149 payload->get_type(payload) == PLV2_NOTIFY)
2150 {
2151 notify_payload_t *notify = (notify_payload_t*)payload;
2152
2153 switch (notify->get_notify_type(notify))
2154 {
2155 case ME_CONNECTID:
2156 case ME_ENDPOINT:
2157 case ME_CONNECTAUTH:
2158 return TRUE;
2159 default:
2160 break;
2161 }
2162 }
2163 #endif /* !ME */
2164 return FALSE;
2165 }
2166
2167 /**
2168 * Parses and verifies the unencrypted payloads contained in the message
2169 */
2170 static status_t parse_payloads(private_message_t *this)
2171 {
2172 payload_type_t type = this->first_payload;
2173 payload_t *payload;
2174 status_t status;
2175
2176 if (this->is_encrypted)
2177 { /* wrap the whole encrypted IKEv1 message in a special encrypted
2178 * payload which is then handled just like a regular payload */
2179 encrypted_payload_t *encryption;
2180
2181 status = this->parser->parse_payload(this->parser, PLV1_ENCRYPTED,
2182 (payload_t**)&encryption);
2183 if (status != SUCCESS)
2184 {
2185 DBG1(DBG_ENC, "failed to wrap encrypted IKEv1 message");
2186 return PARSE_ERROR;
2187 }
2188 encryption->payload_interface.set_next_type((payload_t*)encryption,
2189 this->first_payload);
2190 this->payloads->insert_last(this->payloads, encryption);
2191 return SUCCESS;
2192 }
2193
2194 while (type != PL_NONE)
2195 {
2196 DBG2(DBG_ENC, "starting parsing a %N payload",
2197 payload_type_names, type);
2198
2199 status = this->parser->parse_payload(this->parser, type, &payload);
2200 if (status != SUCCESS)
2201 {
2202 DBG1(DBG_ENC, "payload type %N could not be parsed",
2203 payload_type_names, type);
2204 return PARSE_ERROR;
2205 }
2206
2207 DBG2(DBG_ENC, "verifying payload of type %N", payload_type_names, type);
2208 status = payload->verify(payload);
2209 if (status != SUCCESS)
2210 {
2211 DBG1(DBG_ENC, "%N payload verification failed",
2212 payload_type_names, type);
2213 payload->destroy(payload);
2214 return VERIFY_ERROR;
2215 }
2216
2217 DBG2(DBG_ENC, "%N payload verified, adding to payload list",
2218 payload_type_names, type);
2219 this->payloads->insert_last(this->payloads, payload);
2220
2221 /* an encrypted (fragment) payload MUST be the last one, so STOP here.
2222 * decryption is done later */
2223 if (type == PLV2_ENCRYPTED || type == PLV2_FRAGMENT)
2224 {
2225 DBG2(DBG_ENC, "%N payload found, stop parsing",
2226 payload_type_names, type);
2227 break;
2228 }
2229 type = payload->get_next_type(payload);
2230 }
2231 return SUCCESS;
2232 }
2233
2234 /**
2235 * Decrypt an encrypted payload and extract all contained payloads.
2236 */
2237 static status_t decrypt_and_extract(private_message_t *this, keymat_t *keymat,
2238 payload_t *previous, encrypted_payload_t *encryption)
2239 {
2240 payload_t *encrypted;
2241 payload_type_t type;
2242 chunk_t chunk;
2243 aead_t *aead;
2244 size_t bs;
2245 status_t status = SUCCESS;
2246
2247 if (!keymat)
2248 {
2249 DBG1(DBG_ENC, "found encrypted payload, but no keymat");
2250 return INVALID_ARG;
2251 }
2252 aead = keymat->get_aead(keymat, TRUE);
2253 if (!aead)
2254 {
2255 DBG1(DBG_ENC, "found encrypted payload, but no transform set");
2256 return INVALID_ARG;
2257 }
2258 if (!this->parser)
2259 {
2260 /* reassembled IKEv2 messages are already decrypted, we still call
2261 * decrypt() to parse the contained payloads */
2262 status = encryption->decrypt(encryption, chunk_empty);
2263 }
2264 else
2265 {
2266 bs = aead->get_block_size(aead);
2267 encryption->set_transform(encryption, aead);
2268 chunk = this->packet->get_data(this->packet);
2269 if (chunk.len < encryption->get_length(encryption) ||
2270 chunk.len < bs)
2271 {
2272 DBG1(DBG_ENC, "invalid payload length");
2273 return VERIFY_ERROR;
2274 }
2275 if (keymat->get_version(keymat) == IKEV1)
2276 { /* instead of associated data we provide the IV, we also update
2277 * the IV with the last encrypted block */
2278 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
2279 chunk_t iv;
2280
2281 if (keymat_v1->get_iv(keymat_v1, this->message_id, &iv))
2282 {
2283 status = encryption->decrypt(encryption, iv);
2284 if (status == SUCCESS)
2285 {
2286 if (!keymat_v1->update_iv(keymat_v1, this->message_id,
2287 chunk_create(chunk.ptr + chunk.len - bs, bs)))
2288 {
2289 status = FAILED;
2290 }
2291 }
2292 }
2293 else
2294 {
2295 status = FAILED;
2296 }
2297 }
2298 else
2299 {
2300 chunk.len -= encryption->get_length(encryption);
2301 status = encryption->decrypt(encryption, chunk);
2302 }
2303 }
2304 if (status != SUCCESS)
2305 {
2306 return status;
2307 }
2308
2309 while ((encrypted = encryption->remove_payload(encryption)))
2310 {
2311 type = encrypted->get_type(encrypted);
2312 if (previous)
2313 {
2314 previous->set_next_type(previous, type);
2315 }
2316 else
2317 {
2318 this->first_payload = type;
2319 }
2320 DBG2(DBG_ENC, "insert decrypted payload of type %N at end of list",
2321 payload_type_names, type);
2322 this->payloads->insert_last(this->payloads, encrypted);
2323 previous = encrypted;
2324 }
2325 return SUCCESS;
2326 }
2327
2328 /**
2329 * Decrypt an encrypted fragment payload.
2330 */
2331 static status_t decrypt_fragment(private_message_t *this, keymat_t *keymat,
2332 encrypted_fragment_payload_t *fragment)
2333 {
2334 encrypted_payload_t *encrypted = (encrypted_payload_t*)fragment;
2335 chunk_t chunk;
2336 aead_t *aead;
2337 size_t bs;
2338
2339 if (!keymat)
2340 {
2341 DBG1(DBG_ENC, "found encrypted fragment payload, but no keymat");
2342 return INVALID_ARG;
2343 }
2344 aead = keymat->get_aead(keymat, TRUE);
2345 if (!aead)
2346 {
2347 DBG1(DBG_ENC, "found encrypted fragment payload, but no transform set");
2348 return INVALID_ARG;
2349 }
2350 bs = aead->get_block_size(aead);
2351 encrypted->set_transform(encrypted, aead);
2352 chunk = this->packet->get_data(this->packet);
2353 if (chunk.len < encrypted->get_length(encrypted) ||
2354 chunk.len < bs)
2355 {
2356 DBG1(DBG_ENC, "invalid payload length");
2357 return VERIFY_ERROR;
2358 }
2359 chunk.len -= encrypted->get_length(encrypted);
2360 return encrypted->decrypt(encrypted, chunk);
2361 }
2362
2363 /**
2364 * Do we accept unencrypted ID/HASH payloads in Main Mode, as seen from
2365 * some SonicWall boxes?
2366 */
2367 static bool accept_unencrypted_mm(private_message_t *this, payload_type_t type)
2368 {
2369 if (this->exchange_type == ID_PROT)
2370 {
2371 if (type == PLV1_ID || type == PLV1_HASH)
2372 {
2373 return lib->settings->get_bool(lib->settings,
2374 "%s.accept_unencrypted_mainmode_messages",
2375 FALSE, lib->ns);
2376 }
2377 }
2378 return FALSE;
2379 }
2380
2381 /**
2382 * Decrypt payload from the encrypted payload
2383 */
2384 static status_t decrypt_payloads(private_message_t *this, keymat_t *keymat)
2385 {
2386 payload_t *payload, *previous = NULL;
2387 enumerator_t *enumerator;
2388 payload_rule_t *rule;
2389 payload_type_t type;
2390 status_t status = SUCCESS;
2391 char *was_encrypted = NULL;
2392
2393 enumerator = this->payloads->create_enumerator(this->payloads);
2394 while (enumerator->enumerate(enumerator, &payload))
2395 {
2396 type = payload->get_type(payload);
2397
2398 DBG2(DBG_ENC, "process payload of type %N", payload_type_names, type);
2399
2400 if (type == PLV2_ENCRYPTED || type == PLV1_ENCRYPTED ||
2401 type == PLV2_FRAGMENT)
2402 {
2403 if (was_encrypted)
2404 {
2405 DBG1(DBG_ENC, "%s can't contain other payloads of type %N",
2406 was_encrypted, payload_type_names, type);
2407 status = VERIFY_ERROR;
2408 break;
2409 }
2410 }
2411
2412 if (type == PLV2_ENCRYPTED || type == PLV1_ENCRYPTED)
2413 {
2414 encrypted_payload_t *encryption;
2415
2416 DBG2(DBG_ENC, "found an encrypted payload");
2417 encryption = (encrypted_payload_t*)payload;
2418 this->payloads->remove_at(this->payloads, enumerator);
2419
2420 if (enumerator->enumerate(enumerator, NULL))
2421 {
2422 DBG1(DBG_ENC, "encrypted payload is not last payload");
2423 encryption->destroy(encryption);
2424 status = VERIFY_ERROR;
2425 break;
2426 }
2427 status = decrypt_and_extract(this, keymat, previous, encryption);
2428 encryption->destroy(encryption);
2429 if (status != SUCCESS)
2430 {
2431 break;
2432 }
2433 was_encrypted = "encrypted payload";
2434 }
2435 else if (type == PLV2_FRAGMENT)
2436 {
2437 encrypted_fragment_payload_t *fragment;
2438
2439 DBG2(DBG_ENC, "found an encrypted fragment payload");
2440 fragment = (encrypted_fragment_payload_t*)payload;
2441
2442 if (enumerator->enumerate(enumerator, NULL))
2443 {
2444 DBG1(DBG_ENC, "encrypted fragment payload is not last payload");
2445 status = VERIFY_ERROR;
2446 break;
2447 }
2448 status = decrypt_fragment(this, keymat, fragment);
2449 if (status != SUCCESS)
2450 {
2451 break;
2452 }
2453 was_encrypted = "encrypted fragment payload";
2454 }
2455
2456 if (payload_is_known(type) && !was_encrypted &&
2457 !is_connectivity_check(this, payload) &&
2458 this->exchange_type != AGGRESSIVE)
2459 {
2460 rule = get_payload_rule(this, type);
2461 if ((!rule || rule->encrypted) &&
2462 !accept_unencrypted_mm(this, type))
2463 {
2464 DBG1(DBG_ENC, "payload type %N was not encrypted",
2465 payload_type_names, type);
2466 status = FAILED;
2467 break;
2468 }
2469 }
2470 previous = payload;
2471 }
2472 enumerator->destroy(enumerator);
2473 return status;
2474 }
2475
2476 /**
2477 * Verify a message and all payload according to message/payload rules
2478 */
2479 static status_t verify(private_message_t *this)
2480 {
2481 bool complete = FALSE;
2482 int i;
2483
2484 DBG2(DBG_ENC, "verifying message structure");
2485
2486 /* check for payloads with wrong count */
2487 for (i = 0; i < this->rule->rule_count; i++)
2488 {
2489 enumerator_t *enumerator;
2490 payload_t *payload;
2491 payload_rule_t *rule;
2492 int found = 0;
2493
2494 rule = &this->rule->rules[i];
2495 enumerator = create_payload_enumerator(this);
2496 while (enumerator->enumerate(enumerator, &payload))
2497 {
2498 payload_type_t type;
2499
2500 type = payload->get_type(payload);
2501 if (type == rule->type)
2502 {
2503 found++;
2504 DBG2(DBG_ENC, "found payload of type %N",
2505 payload_type_names, type);
2506 if (found > rule->max_occurence)
2507 {
2508 DBG1(DBG_ENC, "payload of type %N more than %d times (%d) "
2509 "occurred in current message", payload_type_names,
2510 type, rule->max_occurence, found);
2511 enumerator->destroy(enumerator);
2512 return VERIFY_ERROR;
2513 }
2514 }
2515 }
2516 enumerator->destroy(enumerator);
2517
2518 if (!complete && found < rule->min_occurence)
2519 {
2520 DBG1(DBG_ENC, "payload of type %N not occurred %d times (%d)",
2521 payload_type_names, rule->type, rule->min_occurence, found);
2522 return VERIFY_ERROR;
2523 }
2524 if (found && rule->sufficient)
2525 {
2526 complete = TRUE;
2527 }
2528 }
2529 return SUCCESS;
2530 }
2531
2532 METHOD(message_t, parse_body, status_t,
2533 private_message_t *this, keymat_t *keymat)
2534 {
2535 status_t status = SUCCESS;
2536 char str[BUF_LEN];
2537
2538 DBG2(DBG_ENC, "parsing body of message, first payload is %N",
2539 payload_type_names, this->first_payload);
2540
2541 this->rule = get_message_rule(this);
2542 if (!this->rule)
2543 {
2544 DBG1(DBG_ENC, "no message rules specified for a %N %s",
2545 exchange_type_names, this->exchange_type,
2546 this->is_request ? "request" : "response");
2547 return NOT_SUPPORTED;
2548 }
2549
2550 /* reassembled IKEv2 messages are already parsed (except for the payloads
2551 * contained in the encrypted payload, which are handled below) */
2552 if (this->parser)
2553 {
2554 status = parse_payloads(this);
2555 if (status != SUCCESS)
2556 { /* error is already logged */
2557 return status;
2558 }
2559 }
2560
2561 status = decrypt_payloads(this, keymat);
2562 if (status != SUCCESS)
2563 {
2564 DBG1(DBG_ENC, "could not decrypt payloads");
2565 return status;
2566 }
2567
2568 status = verify(this);
2569 if (status != SUCCESS)
2570 {
2571 return status;
2572 }
2573
2574 DBG1(DBG_ENC, "parsed %s", get_string(this, str, sizeof(str)));
2575
2576 if (keymat && keymat->get_version(keymat) == IKEV1)
2577 {
2578 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
2579 chunk_t hash;
2580
2581 if (keymat_v1->get_hash_phase2(keymat_v1, &this->public, &hash))
2582 {
2583 hash_payload_t *hash_payload;
2584 chunk_t other_hash;
2585
2586 if (this->first_payload != PLV1_HASH)
2587 {
2588 if (this->exchange_type == INFORMATIONAL_V1)
2589 {
2590 DBG1(DBG_ENC, "ignoring unprotected INFORMATIONAL from %H",
2591 this->packet->get_source(this->packet));
2592 }
2593 else
2594 {
2595 DBG1(DBG_ENC, "expected HASH payload as first payload");
2596 }
2597 chunk_free(&hash);
2598 return VERIFY_ERROR;
2599 }
2600 hash_payload = (hash_payload_t*)get_payload(this, PLV1_HASH);
2601 other_hash = hash_payload->get_hash(hash_payload);
2602 DBG3(DBG_ENC, "HASH received %B\nHASH expected %B",
2603 &other_hash, &hash);
2604 if (!chunk_equals(hash, other_hash))
2605 {
2606 DBG1(DBG_ENC, "received HASH payload does not match");
2607 chunk_free(&hash);
2608 return FAILED;
2609 }
2610 chunk_free(&hash);
2611 }
2612 if (this->is_encrypted && this->exchange_type != INFORMATIONAL_V1)
2613 { /* message verified, confirm IV */
2614 if (!keymat_v1->confirm_iv(keymat_v1, this->message_id))
2615 {
2616 return FAILED;
2617 }
2618 }
2619 }
2620 return SUCCESS;
2621 }
2622
2623 /**
2624 * Store the fragment data for the fragment with the given fragment number.
2625 */
2626 static status_t add_fragment(private_message_t *this, u_int16_t num,
2627 chunk_t data)
2628 {
2629 fragment_t *fragment;
2630 int i, insert_at = -1;
2631
2632 for (i = 0; i < array_count(this->fragments); i++)
2633 {
2634 array_get(this->fragments, i, &fragment);
2635 if (fragment->num == num)
2636 {
2637 /* ignore a duplicate fragment */
2638 DBG1(DBG_ENC, "received duplicate fragment #%hu", num);
2639 return NEED_MORE;
2640 }
2641 if (fragment->num > num)
2642 {
2643 insert_at = i;
2644 break;
2645 }
2646 }
2647 this->frag->len += data.len;
2648 if (this->frag->len > this->frag->max_packet)
2649 {
2650 DBG1(DBG_ENC, "fragmented IKE message is too large");
2651 reset_defrag(this);
2652 return FAILED;
2653 }
2654 INIT(fragment,
2655 .num = num,
2656 .data = chunk_clone(data),
2657 );
2658 array_insert(this->fragments, insert_at, fragment);
2659 return SUCCESS;
2660 }
2661
2662 /**
2663 * Merge the cached fragment data and resets the defragmentation state.
2664 * Also updates the IP addresses to those of the last received fragment.
2665 */
2666 static chunk_t merge_fragments(private_message_t *this, message_t *last)
2667 {
2668 fragment_t *fragment;
2669 bio_writer_t *writer;
2670 host_t *src, *dst;
2671 chunk_t data;
2672 int i;
2673
2674 writer = bio_writer_create(this->frag->len);
2675 for (i = 0; i < array_count(this->fragments); i++)
2676 {
2677 array_get(this->fragments, i, &fragment);
2678 writer->write_data(writer, fragment->data);
2679 }
2680 data = writer->extract_buf(writer);
2681 writer->destroy(writer);
2682
2683 /* set addresses to those of the last fragment we received */
2684 src = last->get_source(last);
2685 dst = last->get_destination(last);
2686 this->packet->set_source(this->packet, src->clone(src));
2687 this->packet->set_destination(this->packet, dst->clone(dst));
2688
2689 reset_defrag(this);
2690 free(this->frag);
2691 this->frag = NULL;
2692 return data;
2693 }
2694
2695 METHOD(message_t, add_fragment_v1, status_t,
2696 private_message_t *this, message_t *message)
2697 {
2698 fragment_payload_t *payload;
2699 chunk_t data;
2700 u_int8_t num;
2701 status_t status;
2702
2703 if (!this->frag)
2704 {
2705 return INVALID_STATE;
2706 }
2707 payload = (fragment_payload_t*)message->get_payload(message, PLV1_FRAGMENT);
2708 if (!payload)
2709 {
2710 return INVALID_ARG;
2711 }
2712 if (!this->fragments || this->message_id != payload->get_id(payload))
2713 {
2714 reset_defrag(this);
2715 this->message_id = payload->get_id(payload);
2716 /* we don't know the total number of fragments, assume something */
2717 this->fragments = array_create(0, 4);
2718 }
2719
2720 num = payload->get_number(payload);
2721 data = payload->get_data(payload);
2722 if (!this->frag->last && payload->is_last(payload))
2723 {
2724 this->frag->last = num;
2725 }
2726 status = add_fragment(this, num, data);
2727 if (status != SUCCESS)
2728 {
2729 return status;
2730 }
2731
2732 if (array_count(this->fragments) != this->frag->last)
2733 {
2734 /* there are some fragments missing */
2735 DBG1(DBG_ENC, "received fragment #%hhu, waiting for complete IKE "
2736 "message", num);
2737 return NEED_MORE;
2738 }
2739
2740 DBG1(DBG_ENC, "received fragment #%hhu, reassembling fragmented IKE "
2741 "message", num);
2742
2743 data = merge_fragments(this, message);
2744 this->packet->set_data(this->packet, data);
2745 this->parser = parser_create(data);
2746
2747 if (parse_header(this) != SUCCESS)
2748 {
2749 DBG1(DBG_IKE, "failed to parse header of reassembled IKE message");
2750 return FAILED;
2751 }
2752 return SUCCESS;
2753 }
2754
2755 METHOD(message_t, add_fragment_v2, status_t,
2756 private_message_t *this, message_t *message)
2757 {
2758 encrypted_fragment_payload_t *encrypted_fragment;
2759 encrypted_payload_t *encrypted;
2760 payload_t *payload;
2761 enumerator_t *enumerator;
2762 chunk_t data;
2763 u_int16_t total, num;
2764 status_t status;
2765
2766 if (!this->frag)
2767 {
2768 return INVALID_STATE;
2769 }
2770 payload = message->get_payload(message, PLV2_FRAGMENT);
2771 if (!payload || this->message_id != message->get_message_id(message))
2772 {
2773 return INVALID_ARG;
2774 }
2775 encrypted_fragment = (encrypted_fragment_payload_t*)payload;
2776 total = encrypted_fragment->get_total_fragments(encrypted_fragment);
2777
2778 if (!this->fragments || total > this->frag->last)
2779 {
2780 reset_defrag(this);
2781 this->frag->last = total;
2782 this->fragments = array_create(0, total);
2783 }
2784 num = encrypted_fragment->get_fragment_number(encrypted_fragment);
2785 data = encrypted_fragment->get_content(encrypted_fragment);
2786 status = add_fragment(this, num, data);
2787 if (status != SUCCESS)
2788 {
2789 return status;
2790 }
2791
2792 if (num == 1)
2793 {
2794 /* the first fragment denotes the payload type of the first payload in
2795 * the original encrypted payload, cache that */
2796 this->first_payload = payload->get_next_type(payload);
2797 /* move all unencrypted payloads contained in the first fragment */
2798 enumerator = message->create_payload_enumerator(message);
2799 while (enumerator->enumerate(enumerator, &payload))
2800 {
2801 if (payload->get_type(payload) != PLV2_FRAGMENT)
2802 {
2803 message->remove_payload_at(message, enumerator);
2804 this->payloads->insert_last(this->payloads, payload);
2805 }
2806 }
2807 enumerator->destroy(enumerator);
2808 }
2809
2810 if (array_count(this->fragments) != total)
2811 {
2812 /* there are some fragments missing */
2813 DBG1(DBG_ENC, "received fragment #%hu of %hu, waiting for complete IKE "
2814 "message", num, total);
2815 return NEED_MORE;
2816 }
2817
2818 DBG1(DBG_ENC, "received fragment #%hu of %hu, reassembling fragmented IKE "
2819 "message", num, total);
2820
2821 data = merge_fragments(this, message);
2822 encrypted = encrypted_payload_create_from_plain(this->first_payload, data);
2823 this->payloads->insert_last(this->payloads, encrypted);
2824 /* update next payload type (could be an unencrypted payload) */
2825 this->payloads->get_first(this->payloads, (void**)&payload);
2826 this->first_payload = payload->get_type(payload);
2827 return SUCCESS;
2828 }
2829
2830 METHOD(message_t, destroy, void,
2831 private_message_t *this)
2832 {
2833 DESTROY_IF(this->ike_sa_id);
2834 DESTROY_IF(this->parser);
2835 this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
2836 this->packet->destroy(this->packet);
2837 if (this->frag)
2838 {
2839 reset_defrag(this);
2840 free(this->frag);
2841 }
2842 else
2843 {
2844 array_destroy_offset(this->fragments, offsetof(packet_t, destroy));
2845 }
2846 free(this);
2847 }
2848
2849 /*
2850 * Described in header.
2851 */
2852 message_t *message_create_from_packet(packet_t *packet)
2853 {
2854 private_message_t *this;
2855
2856 INIT(this,
2857 .public = {
2858 .set_major_version = _set_major_version,
2859 .get_major_version = _get_major_version,
2860 .set_minor_version = _set_minor_version,
2861 .get_minor_version = _get_minor_version,
2862 .set_message_id = _set_message_id,
2863 .get_message_id = _get_message_id,
2864 .get_initiator_spi = _get_initiator_spi,
2865 .get_responder_spi = _get_responder_spi,
2866 .set_ike_sa_id = _set_ike_sa_id,
2867 .get_ike_sa_id = _get_ike_sa_id,
2868 .set_exchange_type = _set_exchange_type,
2869 .get_exchange_type = _get_exchange_type,
2870 .get_first_payload_type = _get_first_payload_type,
2871 .set_request = _set_request,
2872 .get_request = _get_request,
2873 .set_version_flag = _set_version_flag,
2874 .get_reserved_header_bit = _get_reserved_header_bit,
2875 .set_reserved_header_bit = _set_reserved_header_bit,
2876 .add_payload = _add_payload,
2877 .add_notify = _add_notify,
2878 .disable_sort = _disable_sort,
2879 .generate = _generate,
2880 .is_encoded = _is_encoded,
2881 .is_fragmented = _is_fragmented,
2882 .fragment = _fragment,
2883 .add_fragment = _add_fragment_v2,
2884 .set_source = _set_source,
2885 .get_source = _get_source,
2886 .set_destination = _set_destination,
2887 .get_destination = _get_destination,
2888 .create_payload_enumerator = _create_payload_enumerator,
2889 .remove_payload_at = _remove_payload_at,
2890 .get_payload = _get_payload,
2891 .get_notify = _get_notify,
2892 .parse_header = _parse_header,
2893 .parse_body = _parse_body,
2894 .get_packet = _get_packet,
2895 .get_packet_data = _get_packet_data,
2896 .get_fragments = _get_fragments,
2897 .destroy = _destroy,
2898 },
2899 .exchange_type = EXCHANGE_TYPE_UNDEFINED,
2900 .is_request = TRUE,
2901 .first_payload = PL_NONE,
2902 .packet = packet,
2903 .payloads = linked_list_create(),
2904 .parser = parser_create(packet->get_data(packet)),
2905 );
2906
2907 return &this->public;
2908 }
2909
2910 /*
2911 * Described in header.
2912 */
2913 message_t *message_create(int major, int minor)
2914 {
2915 message_t *this = message_create_from_packet(packet_create());
2916
2917 this->set_major_version(this, major);
2918 this->set_minor_version(this, minor);
2919
2920 return this;
2921 }
2922
2923 /*
2924 * Described in header.
2925 */
2926 message_t *message_create_defrag(message_t *fragment)
2927 {
2928 private_message_t *this;
2929
2930 if (!fragment->get_payload(fragment, PLV1_FRAGMENT) &&
2931 !fragment->get_payload(fragment, PLV2_FRAGMENT))
2932 {
2933 return NULL;
2934 }
2935 this = (private_message_t*)clone_message((private_message_t*)fragment);
2936 /* we don't need a parser for IKEv2, the one for IKEv1 is created after
2937 * reassembling the original message */
2938 this->parser->destroy(this->parser);
2939 this->parser = NULL;
2940 if (fragment->get_major_version(fragment) == IKEV1_MAJOR_VERSION)
2941 {
2942 /* we store the fragment ID in the message ID field, which should be
2943 * zero for fragments, but make sure */
2944 this->message_id = 0;
2945 this->public.add_fragment = _add_fragment_v1;
2946 }
2947 INIT(this->frag,
2948 .max_packet = lib->settings->get_int(lib->settings,
2949 "%s.max_packet", MAX_PACKET, lib->ns),
2950 );
2951 return &this->public;
2952 }