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