message: Limit maximum number of IKEv2 fragments
[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 * A payload rule defines the rules for a payload
73 * in a specific message rule. It defines if and how
74 * many times a payload must/can occur in a message
75 * and if it must be encrypted.
76 */
77 typedef struct {
78 /* Payload type */
79 payload_type_t type;
80 /* Minimal occurrence of this payload. */
81 size_t min_occurence;
82 /* Max occurrence of this payload. */
83 size_t max_occurence;
84 /* TRUE if payload must be encrypted */
85 bool encrypted;
86 /* If payload occurs, the message rule is fulfilled */
87 bool sufficient;
88 } payload_rule_t;
89
90 /**
91 * payload ordering structure allows us to reorder payloads according to RFC.
92 */
93 typedef struct {
94 /** payload type */
95 payload_type_t type;
96 /** notify type, if payload == PLV2_NOTIFY */
97 notify_type_t notify;
98 } payload_order_t;
99
100 /**
101 * A message rule defines the kind of a message,
102 * if it has encrypted contents and a list
103 * of payload ordering rules and payload parsing rules.
104 */
105 typedef struct {
106 /** Type of message. */
107 exchange_type_t exchange_type;
108 /** Is message a request or response. */
109 bool is_request;
110 /** Message contains encrypted payloads. */
111 bool encrypted;
112 /** Number of payload rules which will follow */
113 int rule_count;
114 /** Pointer to first payload rule */
115 payload_rule_t *rules;
116 /** Number of payload order rules */
117 int order_count;
118 /** payload ordering rules */
119 payload_order_t *order;
120 } message_rule_t;
121
122 /**
123 * Message rule for IKE_SA_INIT from initiator.
124 */
125 static payload_rule_t ike_sa_init_i_rules[] = {
126 /* payload type min max encr suff */
127 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
128 {PLV2_SECURITY_ASSOCIATION, 1, 1, FALSE, FALSE},
129 {PLV2_KEY_EXCHANGE, 1, 1, FALSE, FALSE},
130 {PLV2_NONCE, 1, 1, FALSE, FALSE},
131 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
132 };
133
134 /**
135 * payload order for IKE_SA_INIT initiator
136 */
137 static payload_order_t ike_sa_init_i_order[] = {
138 /* payload type notify type */
139 {PLV2_NOTIFY, COOKIE},
140 {PLV2_SECURITY_ASSOCIATION, 0},
141 {PLV2_KEY_EXCHANGE, 0},
142 {PLV2_NONCE, 0},
143 {PLV2_NOTIFY, NAT_DETECTION_SOURCE_IP},
144 {PLV2_NOTIFY, NAT_DETECTION_DESTINATION_IP},
145 {PLV2_NOTIFY, 0},
146 {PLV2_VENDOR_ID, 0},
147 };
148
149 /**
150 * Message rule for IKE_SA_INIT from responder.
151 */
152 static payload_rule_t ike_sa_init_r_rules[] = {
153 /* payload type min max encr suff */
154 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, TRUE},
155 {PLV2_SECURITY_ASSOCIATION, 1, 1, FALSE, FALSE},
156 {PLV2_KEY_EXCHANGE, 1, 1, FALSE, FALSE},
157 {PLV2_NONCE, 1, 1, FALSE, FALSE},
158 {PLV2_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
159 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
160 };
161
162 /**
163 * payload order for IKE_SA_INIT responder
164 */
165 static payload_order_t ike_sa_init_r_order[] = {
166 /* payload type notify type */
167 {PLV2_SECURITY_ASSOCIATION, 0},
168 {PLV2_KEY_EXCHANGE, 0},
169 {PLV2_NONCE, 0},
170 {PLV2_NOTIFY, NAT_DETECTION_SOURCE_IP},
171 {PLV2_NOTIFY, NAT_DETECTION_DESTINATION_IP},
172 {PLV2_NOTIFY, HTTP_CERT_LOOKUP_SUPPORTED},
173 {PLV2_CERTREQ, 0},
174 {PLV2_NOTIFY, 0},
175 {PLV2_VENDOR_ID, 0},
176 };
177
178 /**
179 * Message rule for IKE_AUTH from initiator.
180 */
181 static payload_rule_t ike_auth_i_rules[] = {
182 /* payload type min max encr suff */
183 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
184 {PLV2_EAP, 0, 1, TRUE, TRUE},
185 {PLV2_AUTH, 0, 1, TRUE, TRUE},
186 {PLV2_ID_INITIATOR, 0, 1, TRUE, FALSE},
187 {PLV2_CERTIFICATE, 0, MAX_CERT_PAYLOADS, TRUE, FALSE},
188 {PLV2_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, TRUE, FALSE},
189 {PLV2_ID_RESPONDER, 0, 1, TRUE, FALSE},
190 #ifdef ME
191 {PLV2_SECURITY_ASSOCIATION, 0, 1, TRUE, FALSE},
192 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
193 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
194 #else
195 {PLV2_SECURITY_ASSOCIATION, 0, 1, TRUE, FALSE},
196 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
197 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
198 #endif /* ME */
199 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
200 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
201 };
202
203 /**
204 * payload order for IKE_AUTH initiator
205 */
206 static payload_order_t ike_auth_i_order[] = {
207 /* payload type notify type */
208 {PLV2_ID_INITIATOR, 0},
209 {PLV2_CERTIFICATE, 0},
210 {PLV2_NOTIFY, INITIAL_CONTACT},
211 {PLV2_NOTIFY, HTTP_CERT_LOOKUP_SUPPORTED},
212 {PLV2_CERTREQ, 0},
213 {PLV2_ID_RESPONDER, 0},
214 {PLV2_AUTH, 0},
215 {PLV2_EAP, 0},
216 {PLV2_CONFIGURATION, 0},
217 {PLV2_NOTIFY, IPCOMP_SUPPORTED},
218 {PLV2_NOTIFY, USE_TRANSPORT_MODE},
219 {PLV2_NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
220 {PLV2_NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
221 {PLV2_SECURITY_ASSOCIATION, 0},
222 {PLV2_TS_INITIATOR, 0},
223 {PLV2_TS_RESPONDER, 0},
224 {PLV2_NOTIFY, MOBIKE_SUPPORTED},
225 {PLV2_NOTIFY, ADDITIONAL_IP4_ADDRESS},
226 {PLV2_NOTIFY, ADDITIONAL_IP6_ADDRESS},
227 {PLV2_NOTIFY, NO_ADDITIONAL_ADDRESSES},
228 {PLV2_NOTIFY, 0},
229 {PLV2_VENDOR_ID, 0},
230 };
231
232 /**
233 * Message rule for IKE_AUTH from responder.
234 */
235 static payload_rule_t ike_auth_r_rules[] = {
236 /* payload type min max encr suff */
237 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
238 {PLV2_EAP, 0, 1, TRUE, TRUE},
239 {PLV2_AUTH, 0, 1, TRUE, TRUE},
240 {PLV2_CERTIFICATE, 0, MAX_CERT_PAYLOADS, TRUE, FALSE},
241 {PLV2_ID_RESPONDER, 0, 1, TRUE, FALSE},
242 {PLV2_SECURITY_ASSOCIATION, 0, 1, TRUE, FALSE},
243 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
244 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
245 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
246 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
247 };
248
249 /**
250 * payload order for IKE_AUTH responder
251 */
252 static payload_order_t ike_auth_r_order[] = {
253 /* payload type notify type */
254 {PLV2_ID_RESPONDER, 0},
255 {PLV2_CERTIFICATE, 0},
256 {PLV2_AUTH, 0},
257 {PLV2_EAP, 0},
258 {PLV2_CONFIGURATION, 0},
259 {PLV2_NOTIFY, IPCOMP_SUPPORTED},
260 {PLV2_NOTIFY, USE_TRANSPORT_MODE},
261 {PLV2_NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
262 {PLV2_NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
263 {PLV2_SECURITY_ASSOCIATION, 0},
264 {PLV2_TS_INITIATOR, 0},
265 {PLV2_TS_RESPONDER, 0},
266 {PLV2_NOTIFY, AUTH_LIFETIME},
267 {PLV2_NOTIFY, MOBIKE_SUPPORTED},
268 {PLV2_NOTIFY, ADDITIONAL_IP4_ADDRESS},
269 {PLV2_NOTIFY, ADDITIONAL_IP6_ADDRESS},
270 {PLV2_NOTIFY, NO_ADDITIONAL_ADDRESSES},
271 {PLV2_NOTIFY, 0},
272 {PLV2_VENDOR_ID, 0},
273 };
274
275 /**
276 * Message rule for INFORMATIONAL from initiator.
277 */
278 static payload_rule_t informational_i_rules[] = {
279 /* payload type min max encr suff */
280 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
281 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
282 {PLV2_DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
283 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
284 };
285
286 /**
287 * payload order for INFORMATIONAL initiator
288 */
289 static payload_order_t informational_i_order[] = {
290 /* payload type notify type */
291 {PLV2_NOTIFY, UPDATE_SA_ADDRESSES},
292 {PLV2_NOTIFY, NAT_DETECTION_SOURCE_IP},
293 {PLV2_NOTIFY, NAT_DETECTION_DESTINATION_IP},
294 {PLV2_NOTIFY, COOKIE2},
295 {PLV2_NOTIFY, 0},
296 {PLV2_DELETE, 0},
297 {PLV2_CONFIGURATION, 0},
298 };
299
300 /**
301 * Message rule for INFORMATIONAL from responder.
302 */
303 static payload_rule_t informational_r_rules[] = {
304 /* payload type min max encr suff */
305 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
306 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
307 {PLV2_DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
308 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
309 };
310
311 /**
312 * payload order for INFORMATIONAL responder
313 */
314 static payload_order_t informational_r_order[] = {
315 /* payload type notify type */
316 {PLV2_NOTIFY, UPDATE_SA_ADDRESSES},
317 {PLV2_NOTIFY, NAT_DETECTION_SOURCE_IP},
318 {PLV2_NOTIFY, NAT_DETECTION_DESTINATION_IP},
319 {PLV2_NOTIFY, COOKIE2},
320 {PLV2_NOTIFY, 0},
321 {PLV2_DELETE, 0},
322 {PLV2_CONFIGURATION, 0},
323 };
324
325 /**
326 * Message rule for CREATE_CHILD_SA from initiator.
327 */
328 static payload_rule_t create_child_sa_i_rules[] = {
329 /* payload type min max encr suff */
330 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
331 {PLV2_SECURITY_ASSOCIATION, 1, 1, TRUE, FALSE},
332 {PLV2_NONCE, 1, 1, TRUE, FALSE},
333 {PLV2_KEY_EXCHANGE, 0, 1, TRUE, FALSE},
334 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
335 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
336 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
337 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
338 };
339
340 /**
341 * payload order for CREATE_CHILD_SA from initiator.
342 */
343 static payload_order_t create_child_sa_i_order[] = {
344 /* payload type notify type */
345 {PLV2_NOTIFY, REKEY_SA},
346 {PLV2_NOTIFY, IPCOMP_SUPPORTED},
347 {PLV2_NOTIFY, USE_TRANSPORT_MODE},
348 {PLV2_NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
349 {PLV2_NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
350 {PLV2_SECURITY_ASSOCIATION, 0},
351 {PLV2_NONCE, 0},
352 {PLV2_KEY_EXCHANGE, 0},
353 {PLV2_TS_INITIATOR, 0},
354 {PLV2_TS_RESPONDER, 0},
355 {PLV2_NOTIFY, 0},
356 };
357
358 /**
359 * Message rule for CREATE_CHILD_SA from responder.
360 */
361 static payload_rule_t create_child_sa_r_rules[] = {
362 /* payload type min max encr suff */
363 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
364 {PLV2_SECURITY_ASSOCIATION, 1, 1, TRUE, FALSE},
365 {PLV2_NONCE, 1, 1, TRUE, FALSE},
366 {PLV2_KEY_EXCHANGE, 0, 1, TRUE, FALSE},
367 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
368 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
369 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
370 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
371 };
372
373 /**
374 * payload order for CREATE_CHILD_SA from responder.
375 */
376 static payload_order_t create_child_sa_r_order[] = {
377 /* payload type notify type */
378 {PLV2_NOTIFY, IPCOMP_SUPPORTED},
379 {PLV2_NOTIFY, USE_TRANSPORT_MODE},
380 {PLV2_NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
381 {PLV2_NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
382 {PLV2_SECURITY_ASSOCIATION, 0},
383 {PLV2_NONCE, 0},
384 {PLV2_KEY_EXCHANGE, 0},
385 {PLV2_TS_INITIATOR, 0},
386 {PLV2_TS_RESPONDER, 0},
387 {PLV2_NOTIFY, ADDITIONAL_TS_POSSIBLE},
388 {PLV2_NOTIFY, 0},
389 };
390
391 #ifdef ME
392 /**
393 * Message rule for ME_CONNECT from initiator.
394 */
395 static payload_rule_t me_connect_i_rules[] = {
396 /* payload type min max encr suff */
397 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
398 {PLV2_ID_PEER, 1, 1, TRUE, FALSE},
399 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE}
400 };
401
402 /**
403 * payload order for ME_CONNECT from initiator.
404 */
405 static payload_order_t me_connect_i_order[] = {
406 /* payload type notify type */
407 {PLV2_NOTIFY, 0},
408 {PLV2_ID_PEER, 0},
409 {PLV2_VENDOR_ID, 0},
410 };
411
412 /**
413 * Message rule for ME_CONNECT from responder.
414 */
415 static payload_rule_t me_connect_r_rules[] = {
416 /* payload type min max encr suff */
417 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
418 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE}
419 };
420
421 /**
422 * payload order for ME_CONNECT from responder.
423 */
424 static payload_order_t me_connect_r_order[] = {
425 /* payload type notify type */
426 {PLV2_NOTIFY, 0},
427 {PLV2_VENDOR_ID, 0},
428 };
429 #endif /* ME */
430
431 #ifdef USE_IKEV1
432 /**
433 * Message rule for ID_PROT from initiator.
434 */
435 static payload_rule_t id_prot_i_rules[] = {
436 /* payload type min max encr suff */
437 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
438 {PLV1_SECURITY_ASSOCIATION, 0, 1, FALSE, FALSE},
439 {PLV1_KEY_EXCHANGE, 0, 1, FALSE, FALSE},
440 {PLV1_NONCE, 0, 1, FALSE, FALSE},
441 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
442 {PLV1_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
443 {PLV1_NAT_D, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
444 {PLV1_NAT_D_DRAFT_00_03, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
445 {PLV1_ID, 0, 1, TRUE, FALSE},
446 {PLV1_CERTIFICATE, 0, MAX_CERT_PAYLOADS, TRUE, FALSE},
447 {PLV1_SIGNATURE, 0, 1, TRUE, FALSE},
448 {PLV1_HASH, 0, 1, TRUE, FALSE},
449 {PLV1_FRAGMENT, 0, 1, FALSE, TRUE},
450 };
451
452 /**
453 * payload order for ID_PROT from initiator.
454 */
455 static payload_order_t id_prot_i_order[] = {
456 /* payload type notify type */
457 {PLV1_SECURITY_ASSOCIATION, 0},
458 {PLV1_KEY_EXCHANGE, 0},
459 {PLV1_NONCE, 0},
460 {PLV1_ID, 0},
461 {PLV1_CERTIFICATE, 0},
462 {PLV1_SIGNATURE, 0},
463 {PLV1_HASH, 0},
464 {PLV1_CERTREQ, 0},
465 {PLV1_NOTIFY, 0},
466 {PLV1_VENDOR_ID, 0},
467 {PLV1_NAT_D, 0},
468 {PLV1_NAT_D_DRAFT_00_03, 0},
469 {PLV1_FRAGMENT, 0},
470 };
471
472 /**
473 * Message rule for ID_PROT from responder.
474 */
475 static payload_rule_t id_prot_r_rules[] = {
476 /* payload type min max encr suff */
477 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
478 {PLV1_SECURITY_ASSOCIATION, 0, 1, FALSE, FALSE},
479 {PLV1_KEY_EXCHANGE, 0, 1, FALSE, FALSE},
480 {PLV1_NONCE, 0, 1, FALSE, FALSE},
481 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
482 {PLV1_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
483 {PLV1_NAT_D, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
484 {PLV1_NAT_D_DRAFT_00_03, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
485 {PLV1_ID, 0, 1, TRUE, FALSE},
486 {PLV1_CERTIFICATE, 0, MAX_CERT_PAYLOADS, TRUE, FALSE},
487 {PLV1_SIGNATURE, 0, 1, TRUE, FALSE},
488 {PLV1_HASH, 0, 1, TRUE, FALSE},
489 {PLV1_FRAGMENT, 0, 1, FALSE, TRUE},
490 };
491
492 /**
493 * payload order for ID_PROT from responder.
494 */
495 static payload_order_t id_prot_r_order[] = {
496 /* payload type notify type */
497 {PLV1_SECURITY_ASSOCIATION, 0},
498 {PLV1_KEY_EXCHANGE, 0},
499 {PLV1_NONCE, 0},
500 {PLV1_ID, 0},
501 {PLV1_CERTIFICATE, 0},
502 {PLV1_SIGNATURE, 0},
503 {PLV1_HASH, 0},
504 {PLV1_CERTREQ, 0},
505 {PLV1_NOTIFY, 0},
506 {PLV1_VENDOR_ID, 0},
507 {PLV1_NAT_D, 0},
508 {PLV1_NAT_D_DRAFT_00_03, 0},
509 {PLV1_FRAGMENT, 0},
510 };
511
512 /**
513 * Message rule for AGGRESSIVE from initiator.
514 */
515 static payload_rule_t aggressive_i_rules[] = {
516 /* payload type min max encr suff */
517 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
518 {PLV1_SECURITY_ASSOCIATION, 0, 1, FALSE, FALSE},
519 {PLV1_KEY_EXCHANGE, 0, 1, FALSE, FALSE},
520 {PLV1_NONCE, 0, 1, FALSE, FALSE},
521 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
522 {PLV1_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
523 {PLV1_NAT_D, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
524 {PLV1_NAT_D_DRAFT_00_03, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
525 {PLV1_ID, 0, 1, FALSE, FALSE},
526 {PLV1_CERTIFICATE, 0, 1, TRUE, FALSE},
527 {PLV1_SIGNATURE, 0, 1, TRUE, FALSE},
528 {PLV1_HASH, 0, 1, TRUE, FALSE},
529 {PLV1_FRAGMENT, 0, 1, FALSE, TRUE},
530 };
531
532 /**
533 * payload order for AGGRESSIVE from initiator.
534 */
535 static payload_order_t aggressive_i_order[] = {
536 /* payload type notify type */
537 {PLV1_SECURITY_ASSOCIATION, 0},
538 {PLV1_KEY_EXCHANGE, 0},
539 {PLV1_NONCE, 0},
540 {PLV1_ID, 0},
541 {PLV1_CERTIFICATE, 0},
542 {PLV1_NAT_D, 0},
543 {PLV1_NAT_D_DRAFT_00_03, 0},
544 {PLV1_SIGNATURE, 0},
545 {PLV1_HASH, 0},
546 {PLV1_CERTREQ, 0},
547 {PLV1_NOTIFY, 0},
548 {PLV1_VENDOR_ID, 0},
549 {PLV1_FRAGMENT, 0},
550 };
551
552 /**
553 * Message rule for AGGRESSIVE from responder.
554 */
555 static payload_rule_t aggressive_r_rules[] = {
556 /* payload type min max encr suff */
557 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
558 {PLV1_SECURITY_ASSOCIATION, 0, 1, FALSE, FALSE},
559 {PLV1_KEY_EXCHANGE, 0, 1, FALSE, FALSE},
560 {PLV1_NONCE, 0, 1, FALSE, FALSE},
561 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
562 {PLV1_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
563 {PLV1_NAT_D, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
564 {PLV1_NAT_D_DRAFT_00_03, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
565 {PLV1_ID, 0, 1, FALSE, FALSE},
566 {PLV1_CERTIFICATE, 0, 1, FALSE, FALSE},
567 {PLV1_SIGNATURE, 0, 1, FALSE, FALSE},
568 {PLV1_HASH, 0, 1, FALSE, FALSE},
569 {PLV1_FRAGMENT, 0, 1, FALSE, TRUE},
570 };
571
572 /**
573 * payload order for AGGRESSIVE from responder.
574 */
575 static payload_order_t aggressive_r_order[] = {
576 /* payload type notify type */
577 {PLV1_SECURITY_ASSOCIATION, 0},
578 {PLV1_KEY_EXCHANGE, 0},
579 {PLV1_NONCE, 0},
580 {PLV1_ID, 0},
581 {PLV1_CERTIFICATE, 0},
582 {PLV1_NAT_D, 0},
583 {PLV1_NAT_D_DRAFT_00_03, 0},
584 {PLV1_SIGNATURE, 0},
585 {PLV1_HASH, 0},
586 {PLV1_CERTREQ, 0},
587 {PLV1_NOTIFY, 0},
588 {PLV1_VENDOR_ID, 0},
589 {PLV1_FRAGMENT, 0},
590 };
591
592 /**
593 * Message rule for INFORMATIONAL_V1 from initiator.
594 */
595 static payload_rule_t informational_i_rules_v1[] = {
596 /* payload type min max encr suff */
597 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
598 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
599 {PLV1_DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
600 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
601 };
602
603 /**
604 * payload order for INFORMATIONAL_V1 from initiator.
605 */
606 static payload_order_t informational_i_order_v1[] = {
607 /* payload type notify type */
608 {PLV1_NOTIFY, 0},
609 {PLV1_DELETE, 0},
610 {PLV1_VENDOR_ID, 0},
611 };
612
613 /**
614 * Message rule for INFORMATIONAL_V1 from responder.
615 */
616 static payload_rule_t informational_r_rules_v1[] = {
617 /* payload type min max encr suff */
618 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
619 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
620 {PLV1_DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
621 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
622 };
623
624 /**
625 * payload order for INFORMATIONAL_V1 from responder.
626 */
627 static payload_order_t informational_r_order_v1[] = {
628 /* payload type notify type */
629 {PLV1_NOTIFY, 0},
630 {PLV1_DELETE, 0},
631 {PLV1_VENDOR_ID, 0},
632 };
633
634 /**
635 * Message rule for QUICK_MODE from initiator.
636 */
637 static payload_rule_t quick_mode_i_rules[] = {
638 /* payload type min max encr suff */
639 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
640 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
641 {PLV1_HASH, 0, 1, TRUE, FALSE},
642 {PLV1_SECURITY_ASSOCIATION, 0, 2, TRUE, FALSE},
643 {PLV1_NONCE, 0, 1, TRUE, FALSE},
644 {PLV1_KEY_EXCHANGE, 0, 1, TRUE, FALSE},
645 {PLV1_ID, 0, 2, TRUE, FALSE},
646 {PLV1_NAT_OA, 0, 2, TRUE, FALSE},
647 {PLV1_NAT_OA_DRAFT_00_03, 0, 2, TRUE, FALSE},
648 };
649
650 /**
651 * payload order for QUICK_MODE from initiator.
652 */
653 static payload_order_t quick_mode_i_order[] = {
654 /* payload type notify type */
655 {PLV1_NOTIFY, 0},
656 {PLV1_VENDOR_ID, 0},
657 {PLV1_HASH, 0},
658 {PLV1_SECURITY_ASSOCIATION, 0},
659 {PLV1_NONCE, 0},
660 {PLV1_KEY_EXCHANGE, 0},
661 {PLV1_ID, 0},
662 {PLV1_NAT_OA, 0},
663 {PLV1_NAT_OA_DRAFT_00_03, 0},
664 };
665
666 /**
667 * Message rule for QUICK_MODE from responder.
668 */
669 static payload_rule_t quick_mode_r_rules[] = {
670 /* payload type min max encr suff */
671 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
672 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
673 {PLV1_HASH, 0, 1, TRUE, FALSE},
674 {PLV1_SECURITY_ASSOCIATION, 0, 2, TRUE, FALSE},
675 {PLV1_NONCE, 0, 1, TRUE, FALSE},
676 {PLV1_KEY_EXCHANGE, 0, 1, TRUE, FALSE},
677 {PLV1_ID, 0, 2, TRUE, FALSE},
678 {PLV1_NAT_OA, 0, 2, TRUE, FALSE},
679 {PLV1_NAT_OA_DRAFT_00_03, 0, 2, TRUE, FALSE},
680 };
681
682 /**
683 * payload order for QUICK_MODE from responder.
684 */
685 static payload_order_t quick_mode_r_order[] = {
686 /* payload type notify type */
687 {PLV1_NOTIFY, 0},
688 {PLV1_VENDOR_ID, 0},
689 {PLV1_HASH, 0},
690 {PLV1_SECURITY_ASSOCIATION, 0},
691 {PLV1_NONCE, 0},
692 {PLV1_KEY_EXCHANGE, 0},
693 {PLV1_ID, 0},
694 {PLV1_NAT_OA, 0},
695 {PLV1_NAT_OA_DRAFT_00_03, 0},
696 };
697
698 /**
699 * Message rule for TRANSACTION.
700 */
701 static payload_rule_t transaction_payload_rules_v1[] = {
702 /* payload type min max encr suff */
703 {PLV1_HASH, 0, 1, TRUE, FALSE},
704 {PLV1_CONFIGURATION, 1, 1, FALSE, FALSE},
705 };
706
707 /**
708 * Payload order for TRANSACTION.
709 */
710 static payload_order_t transaction_payload_order_v1[] = {
711 /* payload type notify type */
712 {PLV1_HASH, 0},
713 {PLV1_CONFIGURATION, 0},
714 };
715
716 #endif /* USE_IKEV1 */
717
718 /**
719 * Message rules, defines allowed payloads.
720 */
721 static message_rule_t message_rules[] = {
722 {IKE_SA_INIT, TRUE, FALSE,
723 countof(ike_sa_init_i_rules), ike_sa_init_i_rules,
724 countof(ike_sa_init_i_order), ike_sa_init_i_order,
725 },
726 {IKE_SA_INIT, FALSE, FALSE,
727 countof(ike_sa_init_r_rules), ike_sa_init_r_rules,
728 countof(ike_sa_init_r_order), ike_sa_init_r_order,
729 },
730 {IKE_AUTH, TRUE, TRUE,
731 countof(ike_auth_i_rules), ike_auth_i_rules,
732 countof(ike_auth_i_order), ike_auth_i_order,
733 },
734 {IKE_AUTH, FALSE, TRUE,
735 countof(ike_auth_r_rules), ike_auth_r_rules,
736 countof(ike_auth_r_order), ike_auth_r_order,
737 },
738 {INFORMATIONAL, TRUE, TRUE,
739 countof(informational_i_rules), informational_i_rules,
740 countof(informational_i_order), informational_i_order,
741 },
742 {INFORMATIONAL, FALSE, TRUE,
743 countof(informational_r_rules), informational_r_rules,
744 countof(informational_r_order), informational_r_order,
745 },
746 {CREATE_CHILD_SA, TRUE, TRUE,
747 countof(create_child_sa_i_rules), create_child_sa_i_rules,
748 countof(create_child_sa_i_order), create_child_sa_i_order,
749 },
750 {CREATE_CHILD_SA, FALSE, TRUE,
751 countof(create_child_sa_r_rules), create_child_sa_r_rules,
752 countof(create_child_sa_r_order), create_child_sa_r_order,
753 },
754 #ifdef ME
755 {ME_CONNECT, TRUE, TRUE,
756 countof(me_connect_i_rules), me_connect_i_rules,
757 countof(me_connect_i_order), me_connect_i_order,
758 },
759 {ME_CONNECT, FALSE, TRUE,
760 countof(me_connect_r_rules), me_connect_r_rules,
761 countof(me_connect_r_order), me_connect_r_order,
762 },
763 #endif /* ME */
764 #ifdef USE_IKEV1
765 {ID_PROT, TRUE, FALSE,
766 countof(id_prot_i_rules), id_prot_i_rules,
767 countof(id_prot_i_order), id_prot_i_order,
768 },
769 {ID_PROT, FALSE, FALSE,
770 countof(id_prot_r_rules), id_prot_r_rules,
771 countof(id_prot_r_order), id_prot_r_order,
772 },
773 {AGGRESSIVE, TRUE, FALSE,
774 countof(aggressive_i_rules), aggressive_i_rules,
775 countof(aggressive_i_order), aggressive_i_order,
776 },
777 {AGGRESSIVE, FALSE, FALSE,
778 countof(aggressive_r_rules), aggressive_r_rules,
779 countof(aggressive_r_order), aggressive_r_order,
780 },
781 {INFORMATIONAL_V1, TRUE, TRUE,
782 countof(informational_i_rules_v1), informational_i_rules_v1,
783 countof(informational_i_order_v1), informational_i_order_v1,
784 },
785 {INFORMATIONAL_V1, FALSE, TRUE,
786 countof(informational_r_rules_v1), informational_r_rules_v1,
787 countof(informational_r_order_v1), informational_r_order_v1,
788 },
789 {QUICK_MODE, TRUE, TRUE,
790 countof(quick_mode_i_rules), quick_mode_i_rules,
791 countof(quick_mode_i_order), quick_mode_i_order,
792 },
793 {QUICK_MODE, FALSE, TRUE,
794 countof(quick_mode_r_rules), quick_mode_r_rules,
795 countof(quick_mode_r_order), quick_mode_r_order,
796 },
797 {TRANSACTION, TRUE, TRUE,
798 countof(transaction_payload_rules_v1), transaction_payload_rules_v1,
799 countof(transaction_payload_order_v1), transaction_payload_order_v1,
800 },
801 {TRANSACTION, FALSE, TRUE,
802 countof(transaction_payload_rules_v1), transaction_payload_rules_v1,
803 countof(transaction_payload_order_v1), transaction_payload_order_v1,
804 },
805 /* TODO-IKEv1: define rules for other exchanges */
806 #endif /* USE_IKEV1 */
807 };
808
809 /**
810 * Data for fragment reassembly.
811 */
812 typedef struct {
813
814 /**
815 * For IKEv1 the number of the last fragment (in case we receive them out
816 * of order), since the first one starts with 1 this defines the number of
817 * fragments we expect.
818 * For IKEv2 we store the total number of fragment we received last.
819 */
820 u_int16_t last;
821
822 /**
823 * Length of all currently received fragments.
824 */
825 size_t len;
826
827 /**
828 * Maximum length of a fragmented packet.
829 */
830 size_t max_packet;
831
832 } fragment_data_t;
833
834 typedef struct private_message_t private_message_t;
835
836 /**
837 * Private data of an message_t object.
838 */
839 struct private_message_t {
840
841 /**
842 * Public part of a message_t object.
843 */
844 message_t public;
845
846 /**
847 * Minor version of message.
848 */
849 u_int8_t major_version;
850
851 /**
852 * Major version of message.
853 */
854 u_int8_t minor_version;
855
856 /**
857 * First Payload in message.
858 */
859 payload_type_t first_payload;
860
861 /**
862 * Assigned exchange type.
863 */
864 exchange_type_t exchange_type;
865
866 /**
867 * TRUE if message is a request, FALSE if a reply.
868 */
869 bool is_request;
870
871 /**
872 * The message is encrypted (IKEv1)
873 */
874 bool is_encrypted;
875
876 /**
877 * Higher version supported?
878 */
879 bool version_flag;
880
881 /**
882 * Reserved bits in IKE header
883 */
884 bool reserved[2];
885
886 /**
887 * Sorting of message disabled?
888 */
889 bool sort_disabled;
890
891 /**
892 * Message ID of this message.
893 */
894 u_int32_t message_id;
895
896 /**
897 * ID of assigned IKE_SA.
898 */
899 ike_sa_id_t *ike_sa_id;
900
901 /**
902 * Assigned UDP packet, stores incoming packet or last generated one.
903 */
904 packet_t *packet;
905
906 /**
907 * Array of generated fragments (if any), as packet_t*.
908 * If defragmenting (i.e. frag != NULL) this contains fragment_t*
909 */
910 array_t *fragments;
911
912 /**
913 * Linked List where payload data are stored in.
914 */
915 linked_list_t *payloads;
916
917 /**
918 * Assigned parser to parse Header and Body of this message.
919 */
920 parser_t *parser;
921
922 /**
923 * The message rule for this message instance
924 */
925 message_rule_t *rule;
926
927 /**
928 * Data used to reassemble a fragmented message
929 */
930 fragment_data_t *frag;
931 };
932
933 /**
934 * Maximum number of fragments we will handle
935 */
936 #define MAX_FRAGMENTS 255
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 /**
1899 * Reduce the fragment length but ensure it stays > 0
1900 */
1901 #define REDUCE_FRAG_LEN(fl, amount) ({ \
1902 fl = max(1, (ssize_t)fl - (amount)); \
1903 })
1904
1905 METHOD(message_t, fragment, status_t,
1906 private_message_t *this, keymat_t *keymat, size_t frag_len,
1907 enumerator_t **fragments)
1908 {
1909 encrypted_payload_t *encrypted = NULL;
1910 generator_t *generator = NULL;
1911 message_t *fragment;
1912 packet_t *packet;
1913 payload_type_t next = PL_NONE;
1914 u_int16_t num, count;
1915 host_t *src, *dst;
1916 chunk_t data;
1917 status_t status;
1918 u_int32_t *lenpos;
1919 size_t len;
1920
1921 src = this->packet->get_source(this->packet);
1922 dst = this->packet->get_destination(this->packet);
1923 if (!frag_len)
1924 {
1925 frag_len = (src->get_family(src) == AF_INET) ? 576 : 1280;
1926 }
1927 /* frag_len is the complete IP datagram length, account for overhead (we
1928 * assume no IP options/extension headers are used) */
1929 REDUCE_FRAG_LEN(frag_len, (src->get_family(src) == AF_INET) ? 20 : 40);
1930 /* 8 (UDP header) */
1931 REDUCE_FRAG_LEN(frag_len, 8);
1932 if (dst->get_port(dst) != IKEV2_UDP_PORT &&
1933 src->get_port(src) != IKEV2_UDP_PORT)
1934 { /* reduce length due to non-ESP marker */
1935 REDUCE_FRAG_LEN(frag_len, 4);
1936 }
1937
1938 if (is_encoded(this))
1939 {
1940 if (this->major_version == IKEV2_MAJOR_VERSION)
1941 {
1942 encrypted = (encrypted_payload_t*)get_payload(this, PLV2_ENCRYPTED);
1943 }
1944 data = this->packet->get_data(this->packet);
1945 len = data.len;
1946 }
1947 else
1948 {
1949 status = generate_message(this, keymat, &generator, &encrypted);
1950 if (status != SUCCESS)
1951 {
1952 DESTROY_IF(generator);
1953 return status;
1954 }
1955 data = generator->get_chunk(generator, &lenpos);
1956 len = data.len + (encrypted ? encrypted->get_length(encrypted) : 0);
1957 }
1958
1959 /* check if we actually need to fragment the message and if we have an
1960 * encrypted payload for IKEv2 */
1961 if (len <= frag_len ||
1962 (this->major_version == IKEV2_MAJOR_VERSION && !encrypted))
1963 {
1964 if (generator)
1965 {
1966 status = finalize_message(this, keymat, generator, encrypted);
1967 if (status != SUCCESS)
1968 {
1969 return status;
1970 }
1971 }
1972 *fragments = enumerator_create_single(this->packet, NULL);
1973 return SUCCESS;
1974 }
1975
1976 /* frag_len denoted the maximum IKE message size so far, later on it will
1977 * denote the maximum content size of a fragment payload, therefore,
1978 * account for IKE header */
1979 REDUCE_FRAG_LEN(frag_len, 28);
1980
1981 if (this->major_version == IKEV1_MAJOR_VERSION)
1982 {
1983 if (generator)
1984 {
1985 status = finalize_message(this, keymat, generator, encrypted);
1986 if (status != SUCCESS)
1987 {
1988 return status;
1989 }
1990 data = this->packet->get_data(this->packet);
1991 generator = NULL;
1992 }
1993 /* overhead for the fragmentation payload header */
1994 REDUCE_FRAG_LEN(frag_len, 8);
1995 }
1996 else
1997 {
1998 aead_t *aead;
1999
2000 if (generator)
2001 {
2002 generator->destroy(generator);
2003 generator = generator_create();
2004 }
2005 else
2006 { /* do not log again if it was generated previously */
2007 generator = generator_create_no_dbg();
2008 }
2009 next = encrypted->payload_interface.get_next_type((payload_t*)encrypted);
2010 encrypted->generate_payloads(encrypted, generator);
2011 data = generator->get_chunk(generator, &lenpos);
2012 if (!is_encoded(this))
2013 {
2014 encrypted->destroy(encrypted);
2015 }
2016 aead = keymat->get_aead(keymat, FALSE);
2017 /* overhead for the encrypted fragment payload */
2018 REDUCE_FRAG_LEN(frag_len, aead->get_iv_size(aead));
2019 REDUCE_FRAG_LEN(frag_len, aead->get_icv_size(aead));
2020 /* header */
2021 REDUCE_FRAG_LEN(frag_len, 8);
2022 /* padding and padding length */
2023 frag_len = round_down(frag_len, aead->get_block_size(aead));
2024 REDUCE_FRAG_LEN(frag_len, 1);
2025 /* TODO-FRAG: if there are unencrypted payloads, should we account for
2026 * their length in the first fragment? we still would have to add
2027 * an encrypted fragment payload (albeit empty), even so we couldn't
2028 * prevent IP fragmentation in every case */
2029 }
2030
2031 count = data.len / frag_len + (data.len % frag_len ? 1 : 0);
2032 this->fragments = array_create(0, count);
2033 DBG1(DBG_ENC, "splitting IKE message with length of %zu bytes into "
2034 "%hu fragments", len, count);
2035 for (num = 1; num <= count; num++)
2036 {
2037 len = min(data.len, frag_len);
2038 fragment = create_fragment(this, next, num, count,
2039 chunk_create(data.ptr, len));
2040 status = fragment->generate(fragment, keymat, &packet);
2041 fragment->destroy(fragment);
2042 if (status != SUCCESS)
2043 {
2044 DBG1(DBG_ENC, "failed to generate IKE fragment");
2045 clear_fragments(this);
2046 DESTROY_IF(generator);
2047 return FAILED;
2048 }
2049 array_insert(this->fragments, ARRAY_TAIL, packet);
2050 data = chunk_skip(data, len);
2051 }
2052 *fragments = array_create_enumerator(this->fragments);
2053 DESTROY_IF(generator);
2054 return SUCCESS;
2055 }
2056
2057 METHOD(message_t, get_packet, packet_t*,
2058 private_message_t *this)
2059 {
2060 return this->packet->clone(this->packet);
2061 }
2062
2063 METHOD(message_t, get_packet_data, chunk_t,
2064 private_message_t *this)
2065 {
2066 return this->packet->get_data(this->packet);
2067 }
2068
2069 METHOD(message_t, get_fragments, enumerator_t*,
2070 private_message_t *this)
2071 {
2072 return array_create_enumerator(this->fragments);
2073 }
2074
2075 METHOD(message_t, parse_header, status_t,
2076 private_message_t *this)
2077 {
2078 ike_header_t *ike_header;
2079 status_t status;
2080 bool *reserved;
2081 int i;
2082
2083 DBG2(DBG_ENC, "parsing header of message");
2084
2085 if (!this->parser)
2086 { /* reassembled IKEv2 message, header is inherited from fragments */
2087 return SUCCESS;
2088 }
2089 this->parser->reset_context(this->parser);
2090 status = this->parser->parse_payload(this->parser, PL_HEADER,
2091 (payload_t**)&ike_header);
2092 if (status != SUCCESS)
2093 {
2094 DBG1(DBG_ENC, "header could not be parsed");
2095 return status;
2096
2097 }
2098
2099 status = ike_header->payload_interface.verify(
2100 &ike_header->payload_interface);
2101 if (status != SUCCESS)
2102 {
2103 DBG1(DBG_ENC, "header verification failed");
2104 ike_header->destroy(ike_header);
2105 return status;
2106 }
2107
2108 DESTROY_IF(this->ike_sa_id);
2109 this->ike_sa_id = ike_sa_id_create(
2110 ike_header->get_maj_version(ike_header),
2111 ike_header->get_initiator_spi(ike_header),
2112 ike_header->get_responder_spi(ike_header),
2113 ike_header->get_initiator_flag(ike_header));
2114
2115 this->exchange_type = ike_header->get_exchange_type(ike_header);
2116 this->message_id = ike_header->get_message_id(ike_header);
2117 this->major_version = ike_header->get_maj_version(ike_header);
2118 this->minor_version = ike_header->get_min_version(ike_header);
2119 if (this->major_version == IKEV2_MAJOR_VERSION)
2120 {
2121 this->is_request = !ike_header->get_response_flag(ike_header);
2122 }
2123 else
2124 {
2125 this->is_encrypted = ike_header->get_encryption_flag(ike_header);
2126 }
2127 this->first_payload = ike_header->payload_interface.get_next_type(
2128 &ike_header->payload_interface);
2129 if (this->first_payload == PLV1_FRAGMENT && this->is_encrypted)
2130 { /* racoon sets the encrypted bit when sending a fragment, but these
2131 * messages are really not encrypted */
2132 this->is_encrypted = FALSE;
2133 }
2134
2135 for (i = 0; i < countof(this->reserved); i++)
2136 {
2137 reserved = payload_get_field(&ike_header->payload_interface,
2138 RESERVED_BIT, i);
2139 if (reserved)
2140 {
2141 this->reserved[i] = *reserved;
2142 }
2143 }
2144 ike_header->destroy(ike_header);
2145
2146 DBG2(DBG_ENC, "parsed a %N %s header", exchange_type_names,
2147 this->exchange_type, this->major_version == IKEV1_MAJOR_VERSION ?
2148 "message" : (this->is_request ? "request" : "response"));
2149 return SUCCESS;
2150 }
2151
2152 /**
2153 * Check if a payload is for a mediation extension connectivity check
2154 */
2155 static bool is_connectivity_check(private_message_t *this, payload_t *payload)
2156 {
2157 #ifdef ME
2158 if (this->exchange_type == INFORMATIONAL &&
2159 payload->get_type(payload) == PLV2_NOTIFY)
2160 {
2161 notify_payload_t *notify = (notify_payload_t*)payload;
2162
2163 switch (notify->get_notify_type(notify))
2164 {
2165 case ME_CONNECTID:
2166 case ME_ENDPOINT:
2167 case ME_CONNECTAUTH:
2168 return TRUE;
2169 default:
2170 break;
2171 }
2172 }
2173 #endif /* !ME */
2174 return FALSE;
2175 }
2176
2177 /**
2178 * Parses and verifies the unencrypted payloads contained in the message
2179 */
2180 static status_t parse_payloads(private_message_t *this)
2181 {
2182 payload_type_t type = this->first_payload;
2183 payload_t *payload;
2184 status_t status;
2185
2186 if (this->is_encrypted)
2187 { /* wrap the whole encrypted IKEv1 message in a special encrypted
2188 * payload which is then handled just like a regular payload */
2189 encrypted_payload_t *encryption;
2190
2191 status = this->parser->parse_payload(this->parser, PLV1_ENCRYPTED,
2192 (payload_t**)&encryption);
2193 if (status != SUCCESS)
2194 {
2195 DBG1(DBG_ENC, "failed to wrap encrypted IKEv1 message");
2196 return PARSE_ERROR;
2197 }
2198 encryption->payload_interface.set_next_type((payload_t*)encryption,
2199 this->first_payload);
2200 this->payloads->insert_last(this->payloads, encryption);
2201 return SUCCESS;
2202 }
2203
2204 while (type != PL_NONE)
2205 {
2206 DBG2(DBG_ENC, "starting parsing a %N payload",
2207 payload_type_names, type);
2208
2209 status = this->parser->parse_payload(this->parser, type, &payload);
2210 if (status != SUCCESS)
2211 {
2212 DBG1(DBG_ENC, "payload type %N could not be parsed",
2213 payload_type_names, type);
2214 return PARSE_ERROR;
2215 }
2216
2217 DBG2(DBG_ENC, "verifying payload of type %N", payload_type_names, type);
2218 status = payload->verify(payload);
2219 if (status != SUCCESS)
2220 {
2221 DBG1(DBG_ENC, "%N payload verification failed",
2222 payload_type_names, type);
2223 payload->destroy(payload);
2224 return VERIFY_ERROR;
2225 }
2226
2227 DBG2(DBG_ENC, "%N payload verified, adding to payload list",
2228 payload_type_names, type);
2229 this->payloads->insert_last(this->payloads, payload);
2230
2231 /* an encrypted (fragment) payload MUST be the last one, so STOP here.
2232 * decryption is done later */
2233 if (type == PLV2_ENCRYPTED || type == PLV2_FRAGMENT)
2234 {
2235 DBG2(DBG_ENC, "%N payload found, stop parsing",
2236 payload_type_names, type);
2237 break;
2238 }
2239 type = payload->get_next_type(payload);
2240 }
2241 return SUCCESS;
2242 }
2243
2244 /**
2245 * Decrypt an encrypted payload and extract all contained payloads.
2246 */
2247 static status_t decrypt_and_extract(private_message_t *this, keymat_t *keymat,
2248 payload_t *previous, encrypted_payload_t *encryption)
2249 {
2250 payload_t *encrypted;
2251 payload_type_t type;
2252 chunk_t chunk;
2253 aead_t *aead;
2254 size_t bs;
2255 status_t status = SUCCESS;
2256
2257 if (!keymat)
2258 {
2259 DBG1(DBG_ENC, "found encrypted payload, but no keymat");
2260 return INVALID_ARG;
2261 }
2262 aead = keymat->get_aead(keymat, TRUE);
2263 if (!aead)
2264 {
2265 DBG1(DBG_ENC, "found encrypted payload, but no transform set");
2266 return INVALID_ARG;
2267 }
2268 if (!this->parser)
2269 {
2270 /* reassembled IKEv2 messages are already decrypted, we still call
2271 * decrypt() to parse the contained payloads */
2272 status = encryption->decrypt(encryption, chunk_empty);
2273 }
2274 else
2275 {
2276 bs = aead->get_block_size(aead);
2277 encryption->set_transform(encryption, aead);
2278 chunk = this->packet->get_data(this->packet);
2279 if (chunk.len < encryption->get_length(encryption) ||
2280 chunk.len < bs)
2281 {
2282 DBG1(DBG_ENC, "invalid payload length");
2283 return VERIFY_ERROR;
2284 }
2285 if (keymat->get_version(keymat) == IKEV1)
2286 { /* instead of associated data we provide the IV, we also update
2287 * the IV with the last encrypted block */
2288 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
2289 chunk_t iv;
2290
2291 if (keymat_v1->get_iv(keymat_v1, this->message_id, &iv))
2292 {
2293 status = encryption->decrypt(encryption, iv);
2294 if (status == SUCCESS)
2295 {
2296 if (!keymat_v1->update_iv(keymat_v1, this->message_id,
2297 chunk_create(chunk.ptr + chunk.len - bs, bs)))
2298 {
2299 status = FAILED;
2300 }
2301 }
2302 }
2303 else
2304 {
2305 status = FAILED;
2306 }
2307 }
2308 else
2309 {
2310 chunk.len -= encryption->get_length(encryption);
2311 status = encryption->decrypt(encryption, chunk);
2312 }
2313 }
2314 if (status != SUCCESS)
2315 {
2316 return status;
2317 }
2318
2319 while ((encrypted = encryption->remove_payload(encryption)))
2320 {
2321 type = encrypted->get_type(encrypted);
2322 if (previous)
2323 {
2324 previous->set_next_type(previous, type);
2325 }
2326 else
2327 {
2328 this->first_payload = type;
2329 }
2330 DBG2(DBG_ENC, "insert decrypted payload of type %N at end of list",
2331 payload_type_names, type);
2332 this->payloads->insert_last(this->payloads, encrypted);
2333 previous = encrypted;
2334 }
2335 return SUCCESS;
2336 }
2337
2338 /**
2339 * Decrypt an encrypted fragment payload.
2340 */
2341 static status_t decrypt_fragment(private_message_t *this, keymat_t *keymat,
2342 encrypted_fragment_payload_t *fragment)
2343 {
2344 encrypted_payload_t *encrypted = (encrypted_payload_t*)fragment;
2345 chunk_t chunk;
2346 aead_t *aead;
2347 size_t bs;
2348
2349 if (!keymat)
2350 {
2351 DBG1(DBG_ENC, "found encrypted fragment payload, but no keymat");
2352 return INVALID_ARG;
2353 }
2354 aead = keymat->get_aead(keymat, TRUE);
2355 if (!aead)
2356 {
2357 DBG1(DBG_ENC, "found encrypted fragment payload, but no transform set");
2358 return INVALID_ARG;
2359 }
2360 bs = aead->get_block_size(aead);
2361 encrypted->set_transform(encrypted, aead);
2362 chunk = this->packet->get_data(this->packet);
2363 if (chunk.len < encrypted->get_length(encrypted) ||
2364 chunk.len < bs)
2365 {
2366 DBG1(DBG_ENC, "invalid payload length");
2367 return VERIFY_ERROR;
2368 }
2369 chunk.len -= encrypted->get_length(encrypted);
2370 return encrypted->decrypt(encrypted, chunk);
2371 }
2372
2373 /**
2374 * Do we accept unencrypted ID/HASH payloads in Main Mode, as seen from
2375 * some SonicWall boxes?
2376 */
2377 static bool accept_unencrypted_mm(private_message_t *this, payload_type_t type)
2378 {
2379 if (this->exchange_type == ID_PROT)
2380 {
2381 if (type == PLV1_ID || type == PLV1_HASH)
2382 {
2383 return lib->settings->get_bool(lib->settings,
2384 "%s.accept_unencrypted_mainmode_messages",
2385 FALSE, lib->ns);
2386 }
2387 }
2388 return FALSE;
2389 }
2390
2391 /**
2392 * Decrypt payload from the encrypted payload
2393 */
2394 static status_t decrypt_payloads(private_message_t *this, keymat_t *keymat)
2395 {
2396 payload_t *payload, *previous = NULL;
2397 enumerator_t *enumerator;
2398 payload_rule_t *rule;
2399 payload_type_t type;
2400 status_t status = SUCCESS;
2401 char *was_encrypted = NULL;
2402
2403 enumerator = this->payloads->create_enumerator(this->payloads);
2404 while (enumerator->enumerate(enumerator, &payload))
2405 {
2406 type = payload->get_type(payload);
2407
2408 DBG2(DBG_ENC, "process payload of type %N", payload_type_names, type);
2409
2410 if (type == PLV2_ENCRYPTED || type == PLV1_ENCRYPTED ||
2411 type == PLV2_FRAGMENT)
2412 {
2413 if (was_encrypted)
2414 {
2415 DBG1(DBG_ENC, "%s can't contain other payloads of type %N",
2416 was_encrypted, payload_type_names, type);
2417 status = VERIFY_ERROR;
2418 break;
2419 }
2420 }
2421
2422 if (type == PLV2_ENCRYPTED || type == PLV1_ENCRYPTED)
2423 {
2424 encrypted_payload_t *encryption;
2425
2426 DBG2(DBG_ENC, "found an encrypted payload");
2427 encryption = (encrypted_payload_t*)payload;
2428 this->payloads->remove_at(this->payloads, enumerator);
2429
2430 if (enumerator->enumerate(enumerator, NULL))
2431 {
2432 DBG1(DBG_ENC, "encrypted payload is not last payload");
2433 encryption->destroy(encryption);
2434 status = VERIFY_ERROR;
2435 break;
2436 }
2437 status = decrypt_and_extract(this, keymat, previous, encryption);
2438 encryption->destroy(encryption);
2439 if (status != SUCCESS)
2440 {
2441 break;
2442 }
2443 was_encrypted = "encrypted payload";
2444 }
2445 else if (type == PLV2_FRAGMENT)
2446 {
2447 encrypted_fragment_payload_t *fragment;
2448
2449 DBG2(DBG_ENC, "found an encrypted fragment payload");
2450 fragment = (encrypted_fragment_payload_t*)payload;
2451
2452 if (enumerator->enumerate(enumerator, NULL))
2453 {
2454 DBG1(DBG_ENC, "encrypted fragment payload is not last payload");
2455 status = VERIFY_ERROR;
2456 break;
2457 }
2458 status = decrypt_fragment(this, keymat, fragment);
2459 if (status != SUCCESS)
2460 {
2461 break;
2462 }
2463 was_encrypted = "encrypted fragment payload";
2464 }
2465
2466 if (payload_is_known(type) && !was_encrypted &&
2467 !is_connectivity_check(this, payload) &&
2468 this->exchange_type != AGGRESSIVE)
2469 {
2470 rule = get_payload_rule(this, type);
2471 if ((!rule || rule->encrypted) &&
2472 !accept_unencrypted_mm(this, type))
2473 {
2474 DBG1(DBG_ENC, "payload type %N was not encrypted",
2475 payload_type_names, type);
2476 status = FAILED;
2477 break;
2478 }
2479 }
2480 previous = payload;
2481 }
2482 enumerator->destroy(enumerator);
2483 return status;
2484 }
2485
2486 /**
2487 * Verify a message and all payload according to message/payload rules
2488 */
2489 static status_t verify(private_message_t *this)
2490 {
2491 bool complete = FALSE;
2492 int i;
2493
2494 DBG2(DBG_ENC, "verifying message structure");
2495
2496 /* check for payloads with wrong count */
2497 for (i = 0; i < this->rule->rule_count; i++)
2498 {
2499 enumerator_t *enumerator;
2500 payload_t *payload;
2501 payload_rule_t *rule;
2502 int found = 0;
2503
2504 rule = &this->rule->rules[i];
2505 enumerator = create_payload_enumerator(this);
2506 while (enumerator->enumerate(enumerator, &payload))
2507 {
2508 payload_type_t type;
2509
2510 type = payload->get_type(payload);
2511 if (type == rule->type)
2512 {
2513 found++;
2514 DBG2(DBG_ENC, "found payload of type %N",
2515 payload_type_names, type);
2516 if (found > rule->max_occurence)
2517 {
2518 DBG1(DBG_ENC, "payload of type %N more than %d times (%d) "
2519 "occurred in current message", payload_type_names,
2520 type, rule->max_occurence, found);
2521 enumerator->destroy(enumerator);
2522 return VERIFY_ERROR;
2523 }
2524 }
2525 }
2526 enumerator->destroy(enumerator);
2527
2528 if (!complete && found < rule->min_occurence)
2529 {
2530 DBG1(DBG_ENC, "payload of type %N not occurred %d times (%d)",
2531 payload_type_names, rule->type, rule->min_occurence, found);
2532 return VERIFY_ERROR;
2533 }
2534 if (found && rule->sufficient)
2535 {
2536 complete = TRUE;
2537 }
2538 }
2539 return SUCCESS;
2540 }
2541
2542 METHOD(message_t, parse_body, status_t,
2543 private_message_t *this, keymat_t *keymat)
2544 {
2545 status_t status = SUCCESS;
2546 char str[BUF_LEN];
2547
2548 DBG2(DBG_ENC, "parsing body of message, first payload is %N",
2549 payload_type_names, this->first_payload);
2550
2551 this->rule = get_message_rule(this);
2552 if (!this->rule)
2553 {
2554 DBG1(DBG_ENC, "no message rules specified for a %N %s",
2555 exchange_type_names, this->exchange_type,
2556 this->is_request ? "request" : "response");
2557 return NOT_SUPPORTED;
2558 }
2559
2560 /* reassembled IKEv2 messages are already parsed (except for the payloads
2561 * contained in the encrypted payload, which are handled below) */
2562 if (this->parser)
2563 {
2564 status = parse_payloads(this);
2565 if (status != SUCCESS)
2566 { /* error is already logged */
2567 return status;
2568 }
2569 }
2570
2571 status = decrypt_payloads(this, keymat);
2572 if (status != SUCCESS)
2573 {
2574 DBG1(DBG_ENC, "could not decrypt payloads");
2575 return status;
2576 }
2577
2578 status = verify(this);
2579 if (status != SUCCESS)
2580 {
2581 return status;
2582 }
2583
2584 DBG1(DBG_ENC, "parsed %s", get_string(this, str, sizeof(str)));
2585
2586 if (keymat && keymat->get_version(keymat) == IKEV1)
2587 {
2588 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
2589 chunk_t hash;
2590
2591 if (keymat_v1->get_hash_phase2(keymat_v1, &this->public, &hash))
2592 {
2593 hash_payload_t *hash_payload;
2594 chunk_t other_hash;
2595
2596 if (this->first_payload != PLV1_HASH)
2597 {
2598 if (this->exchange_type == INFORMATIONAL_V1)
2599 {
2600 DBG1(DBG_ENC, "ignoring unprotected INFORMATIONAL from %H",
2601 this->packet->get_source(this->packet));
2602 }
2603 else
2604 {
2605 DBG1(DBG_ENC, "expected HASH payload as first payload");
2606 }
2607 chunk_free(&hash);
2608 return VERIFY_ERROR;
2609 }
2610 hash_payload = (hash_payload_t*)get_payload(this, PLV1_HASH);
2611 other_hash = hash_payload->get_hash(hash_payload);
2612 DBG3(DBG_ENC, "HASH received %B\nHASH expected %B",
2613 &other_hash, &hash);
2614 if (!chunk_equals(hash, other_hash))
2615 {
2616 DBG1(DBG_ENC, "received HASH payload does not match");
2617 chunk_free(&hash);
2618 return FAILED;
2619 }
2620 chunk_free(&hash);
2621 }
2622 if (this->is_encrypted && this->exchange_type != INFORMATIONAL_V1)
2623 { /* message verified, confirm IV */
2624 if (!keymat_v1->confirm_iv(keymat_v1, this->message_id))
2625 {
2626 return FAILED;
2627 }
2628 }
2629 }
2630 return SUCCESS;
2631 }
2632
2633 /**
2634 * Store the fragment data for the fragment with the given fragment number.
2635 */
2636 static status_t add_fragment(private_message_t *this, u_int16_t num,
2637 chunk_t data)
2638 {
2639 fragment_t *fragment;
2640 int i, insert_at = -1;
2641
2642 for (i = 0; i < array_count(this->fragments); i++)
2643 {
2644 array_get(this->fragments, i, &fragment);
2645 if (fragment->num == num)
2646 {
2647 /* ignore a duplicate fragment */
2648 DBG1(DBG_ENC, "received duplicate fragment #%hu", num);
2649 return NEED_MORE;
2650 }
2651 if (fragment->num > num)
2652 {
2653 insert_at = i;
2654 break;
2655 }
2656 }
2657 this->frag->len += data.len;
2658 if (this->frag->len > this->frag->max_packet)
2659 {
2660 DBG1(DBG_ENC, "fragmented IKE message is too large");
2661 reset_defrag(this);
2662 return FAILED;
2663 }
2664 INIT(fragment,
2665 .num = num,
2666 .data = chunk_clone(data),
2667 );
2668 array_insert(this->fragments, insert_at, fragment);
2669 return SUCCESS;
2670 }
2671
2672 /**
2673 * Merge the cached fragment data and resets the defragmentation state.
2674 * Also updates the IP addresses to those of the last received fragment.
2675 */
2676 static chunk_t merge_fragments(private_message_t *this, message_t *last)
2677 {
2678 fragment_t *fragment;
2679 bio_writer_t *writer;
2680 host_t *src, *dst;
2681 chunk_t data;
2682 int i;
2683
2684 writer = bio_writer_create(this->frag->len);
2685 for (i = 0; i < array_count(this->fragments); i++)
2686 {
2687 array_get(this->fragments, i, &fragment);
2688 writer->write_data(writer, fragment->data);
2689 }
2690 data = writer->extract_buf(writer);
2691 writer->destroy(writer);
2692
2693 /* set addresses to those of the last fragment we received */
2694 src = last->get_source(last);
2695 dst = last->get_destination(last);
2696 this->packet->set_source(this->packet, src->clone(src));
2697 this->packet->set_destination(this->packet, dst->clone(dst));
2698
2699 reset_defrag(this);
2700 free(this->frag);
2701 this->frag = NULL;
2702 return data;
2703 }
2704
2705 METHOD(message_t, add_fragment_v1, status_t,
2706 private_message_t *this, message_t *message)
2707 {
2708 fragment_payload_t *payload;
2709 chunk_t data;
2710 u_int8_t num;
2711 status_t status;
2712
2713 if (!this->frag)
2714 {
2715 return INVALID_STATE;
2716 }
2717 payload = (fragment_payload_t*)message->get_payload(message, PLV1_FRAGMENT);
2718 if (!payload)
2719 {
2720 return INVALID_ARG;
2721 }
2722 if (!this->fragments || this->message_id != payload->get_id(payload))
2723 {
2724 reset_defrag(this);
2725 this->message_id = payload->get_id(payload);
2726 /* we don't know the total number of fragments, assume something */
2727 this->fragments = array_create(0, 4);
2728 }
2729
2730 num = payload->get_number(payload);
2731 data = payload->get_data(payload);
2732 if (!this->frag->last && payload->is_last(payload))
2733 {
2734 this->frag->last = num;
2735 }
2736 status = add_fragment(this, num, data);
2737 if (status != SUCCESS)
2738 {
2739 return status;
2740 }
2741
2742 if (array_count(this->fragments) != this->frag->last)
2743 {
2744 /* there are some fragments missing */
2745 DBG1(DBG_ENC, "received fragment #%hhu, waiting for complete IKE "
2746 "message", num);
2747 return NEED_MORE;
2748 }
2749
2750 DBG1(DBG_ENC, "received fragment #%hhu, reassembling fragmented IKE "
2751 "message", num);
2752
2753 data = merge_fragments(this, message);
2754 this->packet->set_data(this->packet, data);
2755 this->parser = parser_create(data);
2756
2757 if (parse_header(this) != SUCCESS)
2758 {
2759 DBG1(DBG_IKE, "failed to parse header of reassembled IKE message");
2760 return FAILED;
2761 }
2762 return SUCCESS;
2763 }
2764
2765 METHOD(message_t, add_fragment_v2, status_t,
2766 private_message_t *this, message_t *message)
2767 {
2768 encrypted_fragment_payload_t *encrypted_fragment;
2769 encrypted_payload_t *encrypted;
2770 payload_t *payload;
2771 enumerator_t *enumerator;
2772 chunk_t data;
2773 u_int16_t total, num;
2774 status_t status;
2775
2776 if (!this->frag)
2777 {
2778 return INVALID_STATE;
2779 }
2780 payload = message->get_payload(message, PLV2_FRAGMENT);
2781 if (!payload || this->message_id != message->get_message_id(message))
2782 {
2783 return INVALID_ARG;
2784 }
2785 encrypted_fragment = (encrypted_fragment_payload_t*)payload;
2786 total = encrypted_fragment->get_total_fragments(encrypted_fragment);
2787 if (total > MAX_FRAGMENTS)
2788 {
2789 DBG1(DBG_IKE, "maximum fragment count exceeded");
2790 reset_defrag(this);
2791 return FAILED;
2792 }
2793 if (!this->fragments || total > this->frag->last)
2794 {
2795 reset_defrag(this);
2796 this->frag->last = total;
2797 this->fragments = array_create(0, total);
2798 }
2799 num = encrypted_fragment->get_fragment_number(encrypted_fragment);
2800 data = encrypted_fragment->get_content(encrypted_fragment);
2801 status = add_fragment(this, num, data);
2802 if (status != SUCCESS)
2803 {
2804 return status;
2805 }
2806
2807 if (num == 1)
2808 {
2809 /* the first fragment denotes the payload type of the first payload in
2810 * the original encrypted payload, cache that */
2811 this->first_payload = payload->get_next_type(payload);
2812 /* move all unencrypted payloads contained in the first fragment */
2813 enumerator = message->create_payload_enumerator(message);
2814 while (enumerator->enumerate(enumerator, &payload))
2815 {
2816 if (payload->get_type(payload) != PLV2_FRAGMENT)
2817 {
2818 message->remove_payload_at(message, enumerator);
2819 this->payloads->insert_last(this->payloads, payload);
2820 }
2821 }
2822 enumerator->destroy(enumerator);
2823 }
2824
2825 if (array_count(this->fragments) != total)
2826 {
2827 /* there are some fragments missing */
2828 DBG1(DBG_ENC, "received fragment #%hu of %hu, waiting for complete IKE "
2829 "message", num, total);
2830 return NEED_MORE;
2831 }
2832
2833 DBG1(DBG_ENC, "received fragment #%hu of %hu, reassembling fragmented IKE "
2834 "message", num, total);
2835
2836 data = merge_fragments(this, message);
2837 encrypted = encrypted_payload_create_from_plain(this->first_payload, data);
2838 this->payloads->insert_last(this->payloads, encrypted);
2839 /* update next payload type (could be an unencrypted payload) */
2840 this->payloads->get_first(this->payloads, (void**)&payload);
2841 this->first_payload = payload->get_type(payload);
2842 return SUCCESS;
2843 }
2844
2845 METHOD(message_t, destroy, void,
2846 private_message_t *this)
2847 {
2848 DESTROY_IF(this->ike_sa_id);
2849 DESTROY_IF(this->parser);
2850 this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
2851 this->packet->destroy(this->packet);
2852 if (this->frag)
2853 {
2854 reset_defrag(this);
2855 free(this->frag);
2856 }
2857 else
2858 {
2859 array_destroy_offset(this->fragments, offsetof(packet_t, destroy));
2860 }
2861 free(this);
2862 }
2863
2864 /*
2865 * Described in header.
2866 */
2867 message_t *message_create_from_packet(packet_t *packet)
2868 {
2869 private_message_t *this;
2870
2871 INIT(this,
2872 .public = {
2873 .set_major_version = _set_major_version,
2874 .get_major_version = _get_major_version,
2875 .set_minor_version = _set_minor_version,
2876 .get_minor_version = _get_minor_version,
2877 .set_message_id = _set_message_id,
2878 .get_message_id = _get_message_id,
2879 .get_initiator_spi = _get_initiator_spi,
2880 .get_responder_spi = _get_responder_spi,
2881 .set_ike_sa_id = _set_ike_sa_id,
2882 .get_ike_sa_id = _get_ike_sa_id,
2883 .set_exchange_type = _set_exchange_type,
2884 .get_exchange_type = _get_exchange_type,
2885 .get_first_payload_type = _get_first_payload_type,
2886 .set_request = _set_request,
2887 .get_request = _get_request,
2888 .set_version_flag = _set_version_flag,
2889 .get_reserved_header_bit = _get_reserved_header_bit,
2890 .set_reserved_header_bit = _set_reserved_header_bit,
2891 .add_payload = _add_payload,
2892 .add_notify = _add_notify,
2893 .disable_sort = _disable_sort,
2894 .generate = _generate,
2895 .is_encoded = _is_encoded,
2896 .is_fragmented = _is_fragmented,
2897 .fragment = _fragment,
2898 .add_fragment = _add_fragment_v2,
2899 .set_source = _set_source,
2900 .get_source = _get_source,
2901 .set_destination = _set_destination,
2902 .get_destination = _get_destination,
2903 .create_payload_enumerator = _create_payload_enumerator,
2904 .remove_payload_at = _remove_payload_at,
2905 .get_payload = _get_payload,
2906 .get_notify = _get_notify,
2907 .parse_header = _parse_header,
2908 .parse_body = _parse_body,
2909 .get_packet = _get_packet,
2910 .get_packet_data = _get_packet_data,
2911 .get_fragments = _get_fragments,
2912 .destroy = _destroy,
2913 },
2914 .exchange_type = EXCHANGE_TYPE_UNDEFINED,
2915 .is_request = TRUE,
2916 .first_payload = PL_NONE,
2917 .packet = packet,
2918 .payloads = linked_list_create(),
2919 .parser = parser_create(packet->get_data(packet)),
2920 );
2921
2922 return &this->public;
2923 }
2924
2925 /*
2926 * Described in header.
2927 */
2928 message_t *message_create(int major, int minor)
2929 {
2930 message_t *this = message_create_from_packet(packet_create());
2931
2932 this->set_major_version(this, major);
2933 this->set_minor_version(this, minor);
2934
2935 return this;
2936 }
2937
2938 /*
2939 * Described in header.
2940 */
2941 message_t *message_create_defrag(message_t *fragment)
2942 {
2943 private_message_t *this;
2944
2945 if (!fragment->get_payload(fragment, PLV1_FRAGMENT) &&
2946 !fragment->get_payload(fragment, PLV2_FRAGMENT))
2947 {
2948 return NULL;
2949 }
2950 this = (private_message_t*)clone_message((private_message_t*)fragment);
2951 /* we don't need a parser for IKEv2, the one for IKEv1 is created after
2952 * reassembling the original message */
2953 this->parser->destroy(this->parser);
2954 this->parser = NULL;
2955 if (fragment->get_major_version(fragment) == IKEV1_MAJOR_VERSION)
2956 {
2957 /* we store the fragment ID in the message ID field, which should be
2958 * zero for fragments, but make sure */
2959 this->message_id = 0;
2960 this->public.add_fragment = _add_fragment_v1;
2961 }
2962 INIT(this->frag,
2963 .max_packet = lib->settings->get_int(lib->settings,
2964 "%s.max_packet", PACKET_MAX_DEFAULT, lib->ns),
2965 );
2966 return &this->public;
2967 }