message: Show the fragmentation numbers in message stringification
[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 if (payload->get_type(payload) == PLV1_FRAGMENT)
1415 {
1416 fragment_payload_t *frag;
1417
1418 frag = (fragment_payload_t*)payload;
1419 if (frag->is_last(frag))
1420 {
1421 written = snprintf(pos, len, "(%u/%u)",
1422 frag->get_number(frag), frag->get_number(frag));
1423 }
1424 else
1425 {
1426 written = snprintf(pos, len, "(%u)", frag->get_number(frag));
1427 }
1428 if (written >= len || written < 0)
1429 {
1430 return buf;
1431 }
1432 pos += written;
1433 len -= written;
1434 }
1435 if (payload->get_type(payload) == PLV2_FRAGMENT)
1436 {
1437 encrypted_fragment_payload_t *frag;
1438
1439 frag = (encrypted_fragment_payload_t*)payload;
1440 written = snprintf(pos, len, "(%u/%u)",
1441 frag->get_fragment_number(frag),
1442 frag->get_total_fragments(frag));
1443 if (written >= len || written < 0)
1444 {
1445 return buf;
1446 }
1447 pos += written;
1448 len -= written;
1449 }
1450 }
1451 enumerator->destroy(enumerator);
1452
1453 /* remove last space */
1454 snprintf(pos, len, " ]");
1455 return buf;
1456 }
1457
1458 METHOD(message_t, disable_sort, void,
1459 private_message_t *this)
1460 {
1461 this->sort_disabled = TRUE;
1462 }
1463
1464 /**
1465 * reorder payloads depending on reordering rules
1466 */
1467 static void order_payloads(private_message_t *this)
1468 {
1469 linked_list_t *list;
1470 payload_t *payload;
1471 int i;
1472
1473 DBG2(DBG_ENC, "order payloads in message");
1474
1475 /* move to temp list */
1476 list = linked_list_create();
1477 while (this->payloads->remove_last(this->payloads,
1478 (void**)&payload) == SUCCESS)
1479 {
1480 list->insert_first(list, payload);
1481 }
1482 /* for each rule, ... */
1483 for (i = 0; i < this->rule->order_count; i++)
1484 {
1485 enumerator_t *enumerator;
1486 notify_payload_t *notify;
1487 payload_order_t order;
1488
1489 order = this->rule->order[i];
1490
1491 /* ... find all payload ... */
1492 enumerator = list->create_enumerator(list);
1493 while (enumerator->enumerate(enumerator, &payload))
1494 {
1495 /* ... with that type ... */
1496 if (payload->get_type(payload) == order.type)
1497 {
1498 notify = (notify_payload_t*)payload;
1499
1500 /**... and check notify for type. */
1501 if (order.type != PLV2_NOTIFY || order.notify == 0 ||
1502 order.notify == notify->get_notify_type(notify))
1503 {
1504 list->remove_at(list, enumerator);
1505 add_payload(this, payload);
1506 }
1507 }
1508 }
1509 enumerator->destroy(enumerator);
1510 }
1511 /* append all payloads without a rule to the end */
1512 while (list->remove_last(list, (void**)&payload) == SUCCESS)
1513 {
1514 /* do not complain about payloads in private use space */
1515 if (payload->get_type(payload) < 128)
1516 {
1517 DBG1(DBG_ENC, "payload %N has no ordering rule in %N %s",
1518 payload_type_names, payload->get_type(payload),
1519 exchange_type_names, this->rule->exchange_type,
1520 this->rule->is_request ? "request" : "response");
1521 }
1522 add_payload(this, payload);
1523 }
1524 list->destroy(list);
1525 }
1526
1527 /**
1528 * Wrap payloads in an encrypted payload
1529 */
1530 static encrypted_payload_t* wrap_payloads(private_message_t *this)
1531 {
1532 encrypted_payload_t *encrypted = NULL;
1533 linked_list_t *payloads;
1534 payload_t *current;
1535
1536 /* move all payloads to a temporary list */
1537 payloads = linked_list_create();
1538 while (this->payloads->remove_first(this->payloads,
1539 (void**)&current) == SUCCESS)
1540 {
1541 if (current->get_type(current) == PLV2_FRAGMENT)
1542 { /* treat encrypted fragment payload as encrypted payload */
1543 encrypted = (encrypted_payload_t*)current;
1544 }
1545 else
1546 {
1547 payloads->insert_last(payloads, current);
1548 }
1549 }
1550 if (encrypted)
1551 { /* simply adopt all the unencrypted payloads */
1552 this->payloads->destroy(this->payloads);
1553 this->payloads = payloads;
1554 return encrypted;
1555 }
1556
1557 if (this->is_encrypted)
1558 {
1559 encrypted = encrypted_payload_create(PLV1_ENCRYPTED);
1560 }
1561 else
1562 {
1563 encrypted = encrypted_payload_create(PLV2_ENCRYPTED);
1564 }
1565 while (payloads->remove_first(payloads, (void**)&current) == SUCCESS)
1566 {
1567 payload_rule_t *rule;
1568 payload_type_t type;
1569 bool encrypt = TRUE;
1570
1571 type = current->get_type(current);
1572 rule = get_payload_rule(this, type);
1573 if (rule)
1574 {
1575 encrypt = rule->encrypted;
1576 }
1577 if (encrypt || this->is_encrypted)
1578 { /* encryption is forced for IKEv1 */
1579 DBG2(DBG_ENC, "insert payload %N into encrypted payload",
1580 payload_type_names, type);
1581 encrypted->add_payload(encrypted, current);
1582 }
1583 else
1584 {
1585 DBG2(DBG_ENC, "insert payload %N unencrypted",
1586 payload_type_names, type);
1587 add_payload(this, current);
1588 }
1589 }
1590 payloads->destroy(payloads);
1591
1592 return encrypted;
1593 }
1594
1595 /**
1596 * Creates the IKE header for this message
1597 */
1598 static ike_header_t *create_header(private_message_t *this)
1599 {
1600 ike_header_t *ike_header;
1601 bool *reserved;
1602 int i;
1603
1604 ike_header = ike_header_create_version(this->major_version,
1605 this->minor_version);
1606 ike_header->set_exchange_type(ike_header, this->exchange_type);
1607 ike_header->set_message_id(ike_header, this->message_id);
1608 if (this->major_version == IKEV2_MAJOR_VERSION)
1609 {
1610 ike_header->set_response_flag(ike_header, !this->is_request);
1611 ike_header->set_version_flag(ike_header, this->version_flag);
1612 ike_header->set_initiator_flag(ike_header,
1613 this->ike_sa_id->is_initiator(this->ike_sa_id));
1614 }
1615 else
1616 {
1617 ike_header->set_encryption_flag(ike_header, this->is_encrypted);
1618 }
1619 ike_header->set_initiator_spi(ike_header,
1620 this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
1621 ike_header->set_responder_spi(ike_header,
1622 this->ike_sa_id->get_responder_spi(this->ike_sa_id));
1623
1624 for (i = 0; i < countof(this->reserved); i++)
1625 {
1626 reserved = payload_get_field(&ike_header->payload_interface,
1627 RESERVED_BIT, i);
1628 if (reserved)
1629 {
1630 *reserved = this->reserved[i];
1631 }
1632 }
1633 return ike_header;
1634 }
1635
1636 /**
1637 * Generates the message, if needed, wraps the payloads in an encrypted payload.
1638 *
1639 * The generator and the possible enrypted payload are returned. The latter
1640 * is not yet encrypted (but the transform is set). It is also not added to
1641 * the payload list (so unless there are unencrypted payloads that list will
1642 * be empty afterwards).
1643 */
1644 static status_t generate_message(private_message_t *this, keymat_t *keymat,
1645 generator_t **out_generator, encrypted_payload_t **encrypted)
1646 {
1647 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
1648 generator_t *generator;
1649 payload_type_t next_type;
1650 enumerator_t *enumerator;
1651 aead_t *aead = NULL;
1652 chunk_t hash = chunk_empty;
1653 char str[BUF_LEN];
1654 ike_header_t *ike_header;
1655 payload_t *payload, *next;
1656 bool encrypting = FALSE;
1657
1658 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
1659 {
1660 DBG1(DBG_ENC, "exchange type is not defined");
1661 return INVALID_STATE;
1662 }
1663
1664 if (this->packet->get_source(this->packet) == NULL ||
1665 this->packet->get_destination(this->packet) == NULL)
1666 {
1667 DBG1(DBG_ENC, "source/destination not defined");
1668 return INVALID_STATE;
1669 }
1670
1671 this->rule = get_message_rule(this);
1672 if (!this->rule)
1673 {
1674 DBG1(DBG_ENC, "no message rules specified for this message type");
1675 return NOT_SUPPORTED;
1676 }
1677
1678 if (!this->sort_disabled)
1679 {
1680 order_payloads(this);
1681 }
1682
1683 if (keymat && keymat->get_version(keymat) == IKEV1)
1684 {
1685 /* get a hash for this message, if any is required */
1686 if (keymat_v1->get_hash_phase2(keymat_v1, &this->public, &hash))
1687 { /* insert a HASH payload as first payload */
1688 hash_payload_t *hash_payload;
1689
1690 hash_payload = hash_payload_create(PLV1_HASH);
1691 hash_payload->set_hash(hash_payload, hash);
1692 this->payloads->insert_first(this->payloads, hash_payload);
1693 if (this->exchange_type == INFORMATIONAL_V1)
1694 {
1695 this->is_encrypted = encrypting = TRUE;
1696 }
1697 chunk_free(&hash);
1698 }
1699 }
1700
1701 if (this->major_version == IKEV2_MAJOR_VERSION)
1702 {
1703 encrypting = this->rule->encrypted;
1704 }
1705 else if (!encrypting)
1706 {
1707 /* If at least one payload requires encryption, encrypt the message.
1708 * If no key material is available, the flag will be reset below. */
1709 enumerator = this->payloads->create_enumerator(this->payloads);
1710 while (enumerator->enumerate(enumerator, (void**)&payload))
1711 {
1712 payload_rule_t *rule;
1713
1714 rule = get_payload_rule(this, payload->get_type(payload));
1715 if (rule && rule->encrypted)
1716 {
1717 this->is_encrypted = encrypting = TRUE;
1718 break;
1719 }
1720 }
1721 enumerator->destroy(enumerator);
1722 }
1723
1724 DBG1(DBG_ENC, "generating %s", get_string(this, str, sizeof(str)));
1725
1726 if (keymat)
1727 {
1728 aead = keymat->get_aead(keymat, FALSE);
1729 }
1730 if (aead && encrypting)
1731 {
1732 *encrypted = wrap_payloads(this);
1733 (*encrypted)->set_transform(*encrypted, aead);
1734 }
1735 else
1736 {
1737 DBG2(DBG_ENC, "not encrypting payloads");
1738 this->is_encrypted = FALSE;
1739 }
1740
1741 /* generate all payloads with proper next type */
1742 *out_generator = generator = generator_create();
1743 ike_header = create_header(this);
1744 payload = (payload_t*)ike_header;
1745 enumerator = create_payload_enumerator(this);
1746 while (enumerator->enumerate(enumerator, &next))
1747 {
1748 payload->set_next_type(payload, next->get_type(next));
1749 generator->generate_payload(generator, payload);
1750 payload = next;
1751 }
1752 enumerator->destroy(enumerator);
1753
1754 next_type = PL_NONE;
1755 if (this->is_encrypted)
1756 { /* for encrypted IKEv1 messages */
1757 next_type = (*encrypted)->payload_interface.get_next_type(
1758 (payload_t*)*encrypted);
1759 }
1760 else if (*encrypted)
1761 { /* use proper IKEv2 encrypted (fragment) payload type */
1762 next_type = (*encrypted)->payload_interface.get_type(
1763 (payload_t*)*encrypted);
1764 }
1765 payload->set_next_type(payload, next_type);
1766 generator->generate_payload(generator, payload);
1767 ike_header->destroy(ike_header);
1768 return SUCCESS;
1769 }
1770
1771 /**
1772 * Encrypts and adds the encrypted payload (if any) to the payload list and
1773 * finalizes the message generation. Destroys the given generator.
1774 */
1775 static status_t finalize_message(private_message_t *this, keymat_t *keymat,
1776 generator_t *generator, encrypted_payload_t *encrypted)
1777 {
1778 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
1779 chunk_t chunk;
1780 u_int32_t *lenpos;
1781
1782 if (encrypted)
1783 {
1784 if (this->is_encrypted)
1785 { /* for IKEv1 instead of associated data we provide the IV */
1786 if (!keymat_v1->get_iv(keymat_v1, this->message_id, &chunk))
1787 {
1788 generator->destroy(generator);
1789 encrypted->destroy(encrypted);
1790 return FAILED;
1791 }
1792 }
1793 else
1794 { /* build associated data (without header of encrypted payload) */
1795 chunk = generator->get_chunk(generator, &lenpos);
1796 /* fill in length, including encrypted payload */
1797 htoun32(lenpos, chunk.len + encrypted->get_length(encrypted));
1798 }
1799 this->payloads->insert_last(this->payloads, encrypted);
1800 if (encrypted->encrypt(encrypted, this->message_id, chunk) != SUCCESS)
1801 {
1802 generator->destroy(generator);
1803 return INVALID_STATE;
1804 }
1805 generator->generate_payload(generator, &encrypted->payload_interface);
1806 }
1807 chunk = generator->get_chunk(generator, &lenpos);
1808 htoun32(lenpos, chunk.len);
1809 this->packet->set_data(this->packet, chunk_clone(chunk));
1810 if (this->is_encrypted && this->exchange_type != INFORMATIONAL_V1)
1811 {
1812 /* update the IV for the next IKEv1 message */
1813 chunk_t last_block;
1814 aead_t *aead;
1815 size_t bs;
1816
1817 aead = keymat->get_aead(keymat, FALSE);
1818 bs = aead->get_block_size(aead);
1819 last_block = chunk_create(chunk.ptr + chunk.len - bs, bs);
1820 if (!keymat_v1->update_iv(keymat_v1, this->message_id, last_block) ||
1821 !keymat_v1->confirm_iv(keymat_v1, this->message_id))
1822 {
1823 generator->destroy(generator);
1824 return FAILED;
1825 }
1826 }
1827 generator->destroy(generator);
1828 return SUCCESS;
1829 }
1830
1831 METHOD(message_t, generate, status_t,
1832 private_message_t *this, keymat_t *keymat, packet_t **packet)
1833 {
1834 generator_t *generator = NULL;
1835 encrypted_payload_t *encrypted = NULL;
1836 status_t status;
1837
1838 status = generate_message(this, keymat, &generator, &encrypted);
1839 if (status != SUCCESS)
1840 {
1841 DESTROY_IF(generator);
1842 return status;
1843 }
1844 status = finalize_message(this, keymat, generator, encrypted);
1845 if (status != SUCCESS)
1846 {
1847 return status;
1848 }
1849 if (packet)
1850 {
1851 *packet = this->packet->clone(this->packet);
1852 }
1853 return SUCCESS;
1854 }
1855
1856 /**
1857 * Creates a (basic) clone of the given message
1858 */
1859 static message_t *clone_message(private_message_t *this)
1860 {
1861 message_t *message;
1862 host_t *src, *dst;
1863
1864 src = this->packet->get_source(this->packet);
1865 dst = this->packet->get_destination(this->packet);
1866
1867 message = message_create(this->major_version, this->minor_version);
1868 message->set_ike_sa_id(message, this->ike_sa_id);
1869 message->set_message_id(message, this->message_id);
1870 message->set_request(message, this->is_request);
1871 message->set_source(message, src->clone(src));
1872 message->set_destination(message, dst->clone(dst));
1873 message->set_exchange_type(message, this->exchange_type);
1874 memcpy(((private_message_t*)message)->reserved, this->reserved,
1875 sizeof(this->reserved));
1876 return message;
1877 }
1878
1879 /**
1880 * Create a single fragment with the given data
1881 */
1882 static message_t *create_fragment(private_message_t *this, payload_type_t next,
1883 u_int16_t num, u_int16_t count, chunk_t data)
1884 {
1885 enumerator_t *enumerator;
1886 payload_t *fragment, *payload;
1887 message_t *message;
1888 peer_cfg_t *peer_cfg;
1889 ike_sa_t *ike_sa;
1890
1891 message = clone_message(this);
1892 if (this->major_version == IKEV1_MAJOR_VERSION)
1893 {
1894 /* other implementations seem to just use 0 as message ID, so here we go */
1895 message->set_message_id(message, 0);
1896 /* always use the initial message type for fragments, even for quick mode
1897 * or transaction messages. */
1898 ike_sa = charon->bus->get_sa(charon->bus);
1899 if (ike_sa && (peer_cfg = ike_sa->get_peer_cfg(ike_sa)) &&
1900 peer_cfg->use_aggressive(peer_cfg))
1901 {
1902 message->set_exchange_type(message, AGGRESSIVE);
1903 }
1904 else
1905 {
1906 message->set_exchange_type(message, ID_PROT);
1907 }
1908 fragment = (payload_t*)fragment_payload_create_from_data(
1909 num, num == count, data);
1910 }
1911 else
1912 {
1913 fragment = (payload_t*)encrypted_fragment_payload_create_from_data(
1914 num, count, data);
1915 if (num == 1)
1916 {
1917 /* only in the first fragment is this set to the type of the first
1918 * payload in the encrypted payload */
1919 fragment->set_next_type(fragment, next);
1920 /* move unencrypted payloads to the first fragment */
1921 enumerator = this->payloads->create_enumerator(this->payloads);
1922 while (enumerator->enumerate(enumerator, &payload))
1923 {
1924 if (payload->get_type(payload) != PLV2_ENCRYPTED)
1925 {
1926 this->payloads->remove_at(this->payloads, enumerator);
1927 message->add_payload(message, payload);
1928 }
1929 }
1930 enumerator->destroy(enumerator);
1931 }
1932 }
1933 message->add_payload(message, (payload_t*)fragment);
1934 return message;
1935 }
1936
1937 /**
1938 * Destroy all fragments
1939 */
1940 static void clear_fragments(private_message_t *this)
1941 {
1942 array_destroy_offset(this->fragments, offsetof(packet_t, destroy));
1943 this->fragments = NULL;
1944 }
1945
1946 /**
1947 * Reduce the fragment length but ensure it stays > 0
1948 */
1949 #define REDUCE_FRAG_LEN(fl, amount) ({ \
1950 fl = max(1, (ssize_t)fl - (amount)); \
1951 })
1952
1953 METHOD(message_t, fragment, status_t,
1954 private_message_t *this, keymat_t *keymat, size_t frag_len,
1955 enumerator_t **fragments)
1956 {
1957 encrypted_payload_t *encrypted = NULL;
1958 generator_t *generator = NULL;
1959 message_t *fragment;
1960 packet_t *packet;
1961 payload_type_t next = PL_NONE;
1962 u_int16_t num, count;
1963 host_t *src, *dst;
1964 chunk_t data;
1965 status_t status;
1966 u_int32_t *lenpos;
1967 size_t len;
1968
1969 src = this->packet->get_source(this->packet);
1970 dst = this->packet->get_destination(this->packet);
1971 if (!frag_len)
1972 {
1973 frag_len = (src->get_family(src) == AF_INET) ? 576 : 1280;
1974 }
1975 /* frag_len is the complete IP datagram length, account for overhead (we
1976 * assume no IP options/extension headers are used) */
1977 REDUCE_FRAG_LEN(frag_len, (src->get_family(src) == AF_INET) ? 20 : 40);
1978 /* 8 (UDP header) */
1979 REDUCE_FRAG_LEN(frag_len, 8);
1980 if (dst->get_port(dst) != IKEV2_UDP_PORT &&
1981 src->get_port(src) != IKEV2_UDP_PORT)
1982 { /* reduce length due to non-ESP marker */
1983 REDUCE_FRAG_LEN(frag_len, 4);
1984 }
1985
1986 if (is_encoded(this))
1987 {
1988 if (this->major_version == IKEV2_MAJOR_VERSION)
1989 {
1990 encrypted = (encrypted_payload_t*)get_payload(this, PLV2_ENCRYPTED);
1991 }
1992 data = this->packet->get_data(this->packet);
1993 len = data.len;
1994 }
1995 else
1996 {
1997 status = generate_message(this, keymat, &generator, &encrypted);
1998 if (status != SUCCESS)
1999 {
2000 DESTROY_IF(generator);
2001 return status;
2002 }
2003 data = generator->get_chunk(generator, &lenpos);
2004 len = data.len + (encrypted ? encrypted->get_length(encrypted) : 0);
2005 }
2006
2007 /* check if we actually need to fragment the message and if we have an
2008 * encrypted payload for IKEv2 */
2009 if (len <= frag_len ||
2010 (this->major_version == IKEV2_MAJOR_VERSION && !encrypted))
2011 {
2012 if (generator)
2013 {
2014 status = finalize_message(this, keymat, generator, encrypted);
2015 if (status != SUCCESS)
2016 {
2017 return status;
2018 }
2019 }
2020 *fragments = enumerator_create_single(this->packet, NULL);
2021 return SUCCESS;
2022 }
2023
2024 /* frag_len denoted the maximum IKE message size so far, later on it will
2025 * denote the maximum content size of a fragment payload, therefore,
2026 * account for IKE header */
2027 REDUCE_FRAG_LEN(frag_len, 28);
2028
2029 if (this->major_version == IKEV1_MAJOR_VERSION)
2030 {
2031 if (generator)
2032 {
2033 status = finalize_message(this, keymat, generator, encrypted);
2034 if (status != SUCCESS)
2035 {
2036 return status;
2037 }
2038 data = this->packet->get_data(this->packet);
2039 generator = NULL;
2040 }
2041 /* overhead for the fragmentation payload header */
2042 REDUCE_FRAG_LEN(frag_len, 8);
2043 }
2044 else
2045 {
2046 aead_t *aead;
2047
2048 if (generator)
2049 {
2050 generator->destroy(generator);
2051 generator = generator_create();
2052 }
2053 else
2054 { /* do not log again if it was generated previously */
2055 generator = generator_create_no_dbg();
2056 }
2057 next = encrypted->payload_interface.get_next_type((payload_t*)encrypted);
2058 encrypted->generate_payloads(encrypted, generator);
2059 data = generator->get_chunk(generator, &lenpos);
2060 if (!is_encoded(this))
2061 {
2062 encrypted->destroy(encrypted);
2063 }
2064 aead = keymat->get_aead(keymat, FALSE);
2065 /* overhead for the encrypted fragment payload */
2066 REDUCE_FRAG_LEN(frag_len, aead->get_iv_size(aead));
2067 REDUCE_FRAG_LEN(frag_len, aead->get_icv_size(aead));
2068 /* header */
2069 REDUCE_FRAG_LEN(frag_len, 8);
2070 /* padding and padding length */
2071 frag_len = round_down(frag_len, aead->get_block_size(aead));
2072 REDUCE_FRAG_LEN(frag_len, 1);
2073 /* TODO-FRAG: if there are unencrypted payloads, should we account for
2074 * their length in the first fragment? we still would have to add
2075 * an encrypted fragment payload (albeit empty), even so we couldn't
2076 * prevent IP fragmentation in every case */
2077 }
2078
2079 count = data.len / frag_len + (data.len % frag_len ? 1 : 0);
2080 this->fragments = array_create(0, count);
2081 DBG1(DBG_ENC, "splitting IKE message with length of %zu bytes into "
2082 "%hu fragments", len, count);
2083 for (num = 1; num <= count; num++)
2084 {
2085 len = min(data.len, frag_len);
2086 fragment = create_fragment(this, next, num, count,
2087 chunk_create(data.ptr, len));
2088 status = fragment->generate(fragment, keymat, &packet);
2089 fragment->destroy(fragment);
2090 if (status != SUCCESS)
2091 {
2092 DBG1(DBG_ENC, "failed to generate IKE fragment");
2093 clear_fragments(this);
2094 DESTROY_IF(generator);
2095 return FAILED;
2096 }
2097 array_insert(this->fragments, ARRAY_TAIL, packet);
2098 data = chunk_skip(data, len);
2099 }
2100 *fragments = array_create_enumerator(this->fragments);
2101 DESTROY_IF(generator);
2102 return SUCCESS;
2103 }
2104
2105 METHOD(message_t, get_packet, packet_t*,
2106 private_message_t *this)
2107 {
2108 return this->packet->clone(this->packet);
2109 }
2110
2111 METHOD(message_t, get_packet_data, chunk_t,
2112 private_message_t *this)
2113 {
2114 return this->packet->get_data(this->packet);
2115 }
2116
2117 METHOD(message_t, get_fragments, enumerator_t*,
2118 private_message_t *this)
2119 {
2120 return array_create_enumerator(this->fragments);
2121 }
2122
2123 METHOD(message_t, parse_header, status_t,
2124 private_message_t *this)
2125 {
2126 ike_header_t *ike_header;
2127 status_t status;
2128 bool *reserved;
2129 int i;
2130
2131 DBG2(DBG_ENC, "parsing header of message");
2132
2133 if (!this->parser)
2134 { /* reassembled IKEv2 message, header is inherited from fragments */
2135 return SUCCESS;
2136 }
2137 this->parser->reset_context(this->parser);
2138 status = this->parser->parse_payload(this->parser, PL_HEADER,
2139 (payload_t**)&ike_header);
2140 if (status != SUCCESS)
2141 {
2142 DBG1(DBG_ENC, "header could not be parsed");
2143 return status;
2144
2145 }
2146
2147 status = ike_header->payload_interface.verify(
2148 &ike_header->payload_interface);
2149 if (status != SUCCESS)
2150 {
2151 DBG1(DBG_ENC, "header verification failed");
2152 ike_header->destroy(ike_header);
2153 return status;
2154 }
2155
2156 DESTROY_IF(this->ike_sa_id);
2157 this->ike_sa_id = ike_sa_id_create(
2158 ike_header->get_maj_version(ike_header),
2159 ike_header->get_initiator_spi(ike_header),
2160 ike_header->get_responder_spi(ike_header),
2161 ike_header->get_initiator_flag(ike_header));
2162
2163 this->exchange_type = ike_header->get_exchange_type(ike_header);
2164 this->message_id = ike_header->get_message_id(ike_header);
2165 this->major_version = ike_header->get_maj_version(ike_header);
2166 this->minor_version = ike_header->get_min_version(ike_header);
2167 if (this->major_version == IKEV2_MAJOR_VERSION)
2168 {
2169 this->is_request = !ike_header->get_response_flag(ike_header);
2170 }
2171 else
2172 {
2173 this->is_encrypted = ike_header->get_encryption_flag(ike_header);
2174 }
2175 this->first_payload = ike_header->payload_interface.get_next_type(
2176 &ike_header->payload_interface);
2177 if (this->first_payload == PLV1_FRAGMENT && this->is_encrypted)
2178 { /* racoon sets the encrypted bit when sending a fragment, but these
2179 * messages are really not encrypted */
2180 this->is_encrypted = FALSE;
2181 }
2182
2183 for (i = 0; i < countof(this->reserved); i++)
2184 {
2185 reserved = payload_get_field(&ike_header->payload_interface,
2186 RESERVED_BIT, i);
2187 if (reserved)
2188 {
2189 this->reserved[i] = *reserved;
2190 }
2191 }
2192 ike_header->destroy(ike_header);
2193
2194 this->parser->set_major_version(this->parser, this->major_version);
2195
2196 DBG2(DBG_ENC, "parsed a %N %s header", exchange_type_names,
2197 this->exchange_type, this->major_version == IKEV1_MAJOR_VERSION ?
2198 "message" : (this->is_request ? "request" : "response"));
2199 return SUCCESS;
2200 }
2201
2202 /**
2203 * Check if a payload is for a mediation extension connectivity check
2204 */
2205 static bool is_connectivity_check(private_message_t *this, payload_t *payload)
2206 {
2207 #ifdef ME
2208 if (this->exchange_type == INFORMATIONAL &&
2209 payload->get_type(payload) == PLV2_NOTIFY)
2210 {
2211 notify_payload_t *notify = (notify_payload_t*)payload;
2212
2213 switch (notify->get_notify_type(notify))
2214 {
2215 case ME_CONNECTID:
2216 case ME_ENDPOINT:
2217 case ME_CONNECTAUTH:
2218 return TRUE;
2219 default:
2220 break;
2221 }
2222 }
2223 #endif /* !ME */
2224 return FALSE;
2225 }
2226
2227 /**
2228 * Parses and verifies the unencrypted payloads contained in the message
2229 */
2230 static status_t parse_payloads(private_message_t *this)
2231 {
2232 payload_type_t type = this->first_payload;
2233 payload_t *payload;
2234 status_t status;
2235
2236 if (this->is_encrypted)
2237 { /* wrap the whole encrypted IKEv1 message in a special encrypted
2238 * payload which is then handled just like a regular payload */
2239 encrypted_payload_t *encryption;
2240
2241 status = this->parser->parse_payload(this->parser, PLV1_ENCRYPTED,
2242 (payload_t**)&encryption);
2243 if (status != SUCCESS)
2244 {
2245 DBG1(DBG_ENC, "failed to wrap encrypted IKEv1 message");
2246 return PARSE_ERROR;
2247 }
2248 encryption->payload_interface.set_next_type((payload_t*)encryption,
2249 this->first_payload);
2250 this->payloads->insert_last(this->payloads, encryption);
2251 return SUCCESS;
2252 }
2253
2254 while (type != PL_NONE)
2255 {
2256 DBG2(DBG_ENC, "starting parsing a %N payload",
2257 payload_type_names, type);
2258
2259 status = this->parser->parse_payload(this->parser, type, &payload);
2260 if (status != SUCCESS)
2261 {
2262 DBG1(DBG_ENC, "payload type %N could not be parsed",
2263 payload_type_names, type);
2264 return PARSE_ERROR;
2265 }
2266
2267 DBG2(DBG_ENC, "verifying payload of type %N", payload_type_names, type);
2268 status = payload->verify(payload);
2269 if (status != SUCCESS)
2270 {
2271 DBG1(DBG_ENC, "%N payload verification failed",
2272 payload_type_names, type);
2273 payload->destroy(payload);
2274 return VERIFY_ERROR;
2275 }
2276
2277 DBG2(DBG_ENC, "%N payload verified, adding to payload list",
2278 payload_type_names, type);
2279 this->payloads->insert_last(this->payloads, payload);
2280
2281 /* an encrypted (fragment) payload MUST be the last one, so STOP here.
2282 * decryption is done later */
2283 if (type == PLV2_ENCRYPTED || type == PLV2_FRAGMENT)
2284 {
2285 DBG2(DBG_ENC, "%N payload found, stop parsing",
2286 payload_type_names, type);
2287 break;
2288 }
2289 type = payload->get_next_type(payload);
2290 }
2291 return SUCCESS;
2292 }
2293
2294 /**
2295 * Decrypt an encrypted payload and extract all contained payloads.
2296 */
2297 static status_t decrypt_and_extract(private_message_t *this, keymat_t *keymat,
2298 payload_t *previous, encrypted_payload_t *encryption)
2299 {
2300 payload_t *encrypted;
2301 payload_type_t type;
2302 chunk_t chunk;
2303 aead_t *aead;
2304 size_t bs;
2305 status_t status = SUCCESS;
2306
2307 if (!keymat)
2308 {
2309 DBG1(DBG_ENC, "found encrypted payload, but no keymat");
2310 return INVALID_ARG;
2311 }
2312 aead = keymat->get_aead(keymat, TRUE);
2313 if (!aead)
2314 {
2315 DBG1(DBG_ENC, "found encrypted payload, but no transform set");
2316 return INVALID_ARG;
2317 }
2318 if (!this->parser)
2319 {
2320 /* reassembled IKEv2 messages are already decrypted, we still call
2321 * decrypt() to parse the contained payloads */
2322 status = encryption->decrypt(encryption, chunk_empty);
2323 }
2324 else
2325 {
2326 bs = aead->get_block_size(aead);
2327 encryption->set_transform(encryption, aead);
2328 chunk = this->packet->get_data(this->packet);
2329 if (chunk.len < encryption->get_length(encryption) ||
2330 chunk.len < bs)
2331 {
2332 DBG1(DBG_ENC, "invalid payload length");
2333 return VERIFY_ERROR;
2334 }
2335 if (keymat->get_version(keymat) == IKEV1)
2336 { /* instead of associated data we provide the IV, we also update
2337 * the IV with the last encrypted block */
2338 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
2339 chunk_t iv;
2340
2341 if (keymat_v1->get_iv(keymat_v1, this->message_id, &iv))
2342 {
2343 status = encryption->decrypt(encryption, iv);
2344 if (status == SUCCESS)
2345 {
2346 if (!keymat_v1->update_iv(keymat_v1, this->message_id,
2347 chunk_create(chunk.ptr + chunk.len - bs, bs)))
2348 {
2349 status = FAILED;
2350 }
2351 }
2352 }
2353 else
2354 {
2355 status = FAILED;
2356 }
2357 }
2358 else
2359 {
2360 chunk.len -= encryption->get_length(encryption);
2361 status = encryption->decrypt(encryption, chunk);
2362 }
2363 }
2364 if (status != SUCCESS)
2365 {
2366 return status;
2367 }
2368
2369 while ((encrypted = encryption->remove_payload(encryption)))
2370 {
2371 type = encrypted->get_type(encrypted);
2372 if (previous)
2373 {
2374 previous->set_next_type(previous, type);
2375 }
2376 else
2377 {
2378 this->first_payload = type;
2379 }
2380 DBG2(DBG_ENC, "insert decrypted payload of type %N at end of list",
2381 payload_type_names, type);
2382 this->payloads->insert_last(this->payloads, encrypted);
2383 previous = encrypted;
2384 }
2385 return SUCCESS;
2386 }
2387
2388 /**
2389 * Decrypt an encrypted fragment payload.
2390 */
2391 static status_t decrypt_fragment(private_message_t *this, keymat_t *keymat,
2392 encrypted_fragment_payload_t *fragment)
2393 {
2394 encrypted_payload_t *encrypted = (encrypted_payload_t*)fragment;
2395 chunk_t chunk;
2396 aead_t *aead;
2397 size_t bs;
2398
2399 if (!keymat)
2400 {
2401 DBG1(DBG_ENC, "found encrypted fragment payload, but no keymat");
2402 return INVALID_ARG;
2403 }
2404 aead = keymat->get_aead(keymat, TRUE);
2405 if (!aead)
2406 {
2407 DBG1(DBG_ENC, "found encrypted fragment payload, but no transform set");
2408 return INVALID_ARG;
2409 }
2410 bs = aead->get_block_size(aead);
2411 encrypted->set_transform(encrypted, aead);
2412 chunk = this->packet->get_data(this->packet);
2413 if (chunk.len < encrypted->get_length(encrypted) ||
2414 chunk.len < bs)
2415 {
2416 DBG1(DBG_ENC, "invalid payload length");
2417 return VERIFY_ERROR;
2418 }
2419 chunk.len -= encrypted->get_length(encrypted);
2420 return encrypted->decrypt(encrypted, chunk);
2421 }
2422
2423 /**
2424 * Do we accept unencrypted ID/HASH payloads in Main Mode, as seen from
2425 * some SonicWall boxes?
2426 */
2427 static bool accept_unencrypted_mm(private_message_t *this, payload_type_t type)
2428 {
2429 if (this->exchange_type == ID_PROT)
2430 {
2431 if (type == PLV1_ID || type == PLV1_HASH)
2432 {
2433 return lib->settings->get_bool(lib->settings,
2434 "%s.accept_unencrypted_mainmode_messages",
2435 FALSE, lib->ns);
2436 }
2437 }
2438 return FALSE;
2439 }
2440
2441 /**
2442 * Decrypt payload from the encrypted payload
2443 */
2444 static status_t decrypt_payloads(private_message_t *this, keymat_t *keymat)
2445 {
2446 payload_t *payload, *previous = NULL;
2447 enumerator_t *enumerator;
2448 payload_rule_t *rule;
2449 payload_type_t type;
2450 status_t status = SUCCESS;
2451 char *was_encrypted = NULL;
2452
2453 enumerator = this->payloads->create_enumerator(this->payloads);
2454 while (enumerator->enumerate(enumerator, &payload))
2455 {
2456 type = payload->get_type(payload);
2457
2458 DBG2(DBG_ENC, "process payload of type %N", payload_type_names, type);
2459
2460 if (type == PLV2_ENCRYPTED || type == PLV1_ENCRYPTED ||
2461 type == PLV2_FRAGMENT)
2462 {
2463 if (was_encrypted)
2464 {
2465 DBG1(DBG_ENC, "%s can't contain other payloads of type %N",
2466 was_encrypted, payload_type_names, type);
2467 status = VERIFY_ERROR;
2468 break;
2469 }
2470 }
2471
2472 if (type == PLV2_ENCRYPTED || type == PLV1_ENCRYPTED)
2473 {
2474 encrypted_payload_t *encryption;
2475
2476 DBG2(DBG_ENC, "found an encrypted payload");
2477 encryption = (encrypted_payload_t*)payload;
2478 this->payloads->remove_at(this->payloads, enumerator);
2479
2480 if (enumerator->enumerate(enumerator, NULL))
2481 {
2482 DBG1(DBG_ENC, "encrypted payload is not last payload");
2483 encryption->destroy(encryption);
2484 status = VERIFY_ERROR;
2485 break;
2486 }
2487 status = decrypt_and_extract(this, keymat, previous, encryption);
2488 encryption->destroy(encryption);
2489 if (status != SUCCESS)
2490 {
2491 break;
2492 }
2493 was_encrypted = "encrypted payload";
2494 }
2495 else if (type == PLV2_FRAGMENT)
2496 {
2497 encrypted_fragment_payload_t *fragment;
2498
2499 DBG2(DBG_ENC, "found an encrypted fragment payload");
2500 fragment = (encrypted_fragment_payload_t*)payload;
2501
2502 if (enumerator->enumerate(enumerator, NULL))
2503 {
2504 DBG1(DBG_ENC, "encrypted fragment payload is not last payload");
2505 status = VERIFY_ERROR;
2506 break;
2507 }
2508 status = decrypt_fragment(this, keymat, fragment);
2509 if (status != SUCCESS)
2510 {
2511 break;
2512 }
2513 was_encrypted = "encrypted fragment payload";
2514 }
2515
2516 if (payload_is_known(type, this->major_version) && !was_encrypted &&
2517 !is_connectivity_check(this, payload) &&
2518 this->exchange_type != AGGRESSIVE)
2519 {
2520 rule = get_payload_rule(this, type);
2521 if ((!rule || rule->encrypted) &&
2522 !accept_unencrypted_mm(this, type))
2523 {
2524 DBG1(DBG_ENC, "payload type %N was not encrypted",
2525 payload_type_names, type);
2526 status = FAILED;
2527 break;
2528 }
2529 }
2530 previous = payload;
2531 }
2532 enumerator->destroy(enumerator);
2533 return status;
2534 }
2535
2536 /**
2537 * Verify a message and all payload according to message/payload rules
2538 */
2539 static status_t verify(private_message_t *this)
2540 {
2541 bool complete = FALSE;
2542 int i;
2543
2544 DBG2(DBG_ENC, "verifying message structure");
2545
2546 /* check for payloads with wrong count */
2547 for (i = 0; i < this->rule->rule_count; i++)
2548 {
2549 enumerator_t *enumerator;
2550 payload_t *payload;
2551 payload_rule_t *rule;
2552 int found = 0;
2553
2554 rule = &this->rule->rules[i];
2555 enumerator = create_payload_enumerator(this);
2556 while (enumerator->enumerate(enumerator, &payload))
2557 {
2558 payload_type_t type;
2559
2560 type = payload->get_type(payload);
2561 if (type == rule->type)
2562 {
2563 found++;
2564 DBG2(DBG_ENC, "found payload of type %N",
2565 payload_type_names, type);
2566 if (found > rule->max_occurence)
2567 {
2568 DBG1(DBG_ENC, "payload of type %N more than %d times (%d) "
2569 "occurred in current message", payload_type_names,
2570 type, rule->max_occurence, found);
2571 enumerator->destroy(enumerator);
2572 return VERIFY_ERROR;
2573 }
2574 }
2575 }
2576 enumerator->destroy(enumerator);
2577
2578 if (!complete && found < rule->min_occurence)
2579 {
2580 DBG1(DBG_ENC, "payload of type %N not occurred %d times (%d)",
2581 payload_type_names, rule->type, rule->min_occurence, found);
2582 return VERIFY_ERROR;
2583 }
2584 if (found && rule->sufficient)
2585 {
2586 complete = TRUE;
2587 }
2588 }
2589 return SUCCESS;
2590 }
2591
2592 METHOD(message_t, parse_body, status_t,
2593 private_message_t *this, keymat_t *keymat)
2594 {
2595 status_t status = SUCCESS;
2596 char str[BUF_LEN];
2597
2598 DBG2(DBG_ENC, "parsing body of message, first payload is %N",
2599 payload_type_names, this->first_payload);
2600
2601 this->rule = get_message_rule(this);
2602 if (!this->rule)
2603 {
2604 DBG1(DBG_ENC, "no message rules specified for a %N %s",
2605 exchange_type_names, this->exchange_type,
2606 this->is_request ? "request" : "response");
2607 return NOT_SUPPORTED;
2608 }
2609
2610 /* reassembled IKEv2 messages are already parsed (except for the payloads
2611 * contained in the encrypted payload, which are handled below) */
2612 if (this->parser)
2613 {
2614 status = parse_payloads(this);
2615 if (status != SUCCESS)
2616 { /* error is already logged */
2617 return status;
2618 }
2619 }
2620
2621 status = decrypt_payloads(this, keymat);
2622 if (status != SUCCESS)
2623 {
2624 DBG1(DBG_ENC, "could not decrypt payloads");
2625 return status;
2626 }
2627
2628 status = verify(this);
2629 if (status != SUCCESS)
2630 {
2631 return status;
2632 }
2633
2634 DBG1(DBG_ENC, "parsed %s", get_string(this, str, sizeof(str)));
2635
2636 if (keymat && keymat->get_version(keymat) == IKEV1)
2637 {
2638 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
2639 chunk_t hash;
2640
2641 if (keymat_v1->get_hash_phase2(keymat_v1, &this->public, &hash))
2642 {
2643 hash_payload_t *hash_payload;
2644 chunk_t other_hash;
2645
2646 if (this->first_payload != PLV1_HASH)
2647 {
2648 if (this->exchange_type == INFORMATIONAL_V1)
2649 {
2650 DBG1(DBG_ENC, "ignoring unprotected INFORMATIONAL from %H",
2651 this->packet->get_source(this->packet));
2652 }
2653 else
2654 {
2655 DBG1(DBG_ENC, "expected HASH payload as first payload");
2656 }
2657 chunk_free(&hash);
2658 return VERIFY_ERROR;
2659 }
2660 hash_payload = (hash_payload_t*)get_payload(this, PLV1_HASH);
2661 other_hash = hash_payload->get_hash(hash_payload);
2662 DBG3(DBG_ENC, "HASH received %B\nHASH expected %B",
2663 &other_hash, &hash);
2664 if (!chunk_equals_const(hash, other_hash))
2665 {
2666 DBG1(DBG_ENC, "received HASH payload does not match");
2667 chunk_free(&hash);
2668 return FAILED;
2669 }
2670 chunk_free(&hash);
2671 }
2672 if (this->is_encrypted && this->exchange_type != INFORMATIONAL_V1)
2673 { /* message verified, confirm IV */
2674 if (!keymat_v1->confirm_iv(keymat_v1, this->message_id))
2675 {
2676 return FAILED;
2677 }
2678 }
2679 }
2680 return SUCCESS;
2681 }
2682
2683 /**
2684 * Store the fragment data for the fragment with the given fragment number.
2685 */
2686 static status_t add_fragment(private_message_t *this, u_int16_t num,
2687 chunk_t data)
2688 {
2689 fragment_t *fragment;
2690 int i, insert_at = -1;
2691
2692 for (i = 0; i < array_count(this->fragments); i++)
2693 {
2694 array_get(this->fragments, i, &fragment);
2695 if (fragment->num == num)
2696 {
2697 /* ignore a duplicate fragment */
2698 DBG1(DBG_ENC, "received duplicate fragment #%hu", num);
2699 return NEED_MORE;
2700 }
2701 if (fragment->num > num)
2702 {
2703 insert_at = i;
2704 break;
2705 }
2706 }
2707 this->frag->len += data.len;
2708 if (this->frag->len > this->frag->max_packet)
2709 {
2710 DBG1(DBG_ENC, "fragmented IKE message is too large");
2711 reset_defrag(this);
2712 return FAILED;
2713 }
2714 INIT(fragment,
2715 .num = num,
2716 .data = chunk_clone(data),
2717 );
2718 array_insert(this->fragments, insert_at, fragment);
2719 return SUCCESS;
2720 }
2721
2722 /**
2723 * Merge the cached fragment data and resets the defragmentation state.
2724 * Also updates the IP addresses to those of the last received fragment.
2725 */
2726 static chunk_t merge_fragments(private_message_t *this, message_t *last)
2727 {
2728 fragment_t *fragment;
2729 bio_writer_t *writer;
2730 host_t *src, *dst;
2731 chunk_t data;
2732 int i;
2733
2734 writer = bio_writer_create(this->frag->len);
2735 for (i = 0; i < array_count(this->fragments); i++)
2736 {
2737 array_get(this->fragments, i, &fragment);
2738 writer->write_data(writer, fragment->data);
2739 }
2740 data = writer->extract_buf(writer);
2741 writer->destroy(writer);
2742
2743 /* set addresses to those of the last fragment we received */
2744 src = last->get_source(last);
2745 dst = last->get_destination(last);
2746 this->packet->set_source(this->packet, src->clone(src));
2747 this->packet->set_destination(this->packet, dst->clone(dst));
2748
2749 reset_defrag(this);
2750 free(this->frag);
2751 this->frag = NULL;
2752 return data;
2753 }
2754
2755 METHOD(message_t, add_fragment_v1, status_t,
2756 private_message_t *this, message_t *message)
2757 {
2758 fragment_payload_t *payload;
2759 chunk_t data;
2760 u_int8_t num;
2761 status_t status;
2762
2763 if (!this->frag)
2764 {
2765 return INVALID_STATE;
2766 }
2767 payload = (fragment_payload_t*)message->get_payload(message, PLV1_FRAGMENT);
2768 if (!payload)
2769 {
2770 return INVALID_ARG;
2771 }
2772 if (!this->fragments || this->message_id != payload->get_id(payload))
2773 {
2774 reset_defrag(this);
2775 this->message_id = payload->get_id(payload);
2776 /* we don't know the total number of fragments, assume something */
2777 this->fragments = array_create(0, 4);
2778 }
2779
2780 num = payload->get_number(payload);
2781 data = payload->get_data(payload);
2782 if (!this->frag->last && payload->is_last(payload))
2783 {
2784 this->frag->last = num;
2785 }
2786 status = add_fragment(this, num, data);
2787 if (status != SUCCESS)
2788 {
2789 return status;
2790 }
2791
2792 if (array_count(this->fragments) != this->frag->last)
2793 {
2794 /* there are some fragments missing */
2795 DBG1(DBG_ENC, "received fragment #%hhu, waiting for complete IKE "
2796 "message", num);
2797 return NEED_MORE;
2798 }
2799
2800 DBG1(DBG_ENC, "received fragment #%hhu, reassembling fragmented IKE "
2801 "message", num);
2802
2803 data = merge_fragments(this, message);
2804 this->packet->set_data(this->packet, data);
2805 this->parser = parser_create(data);
2806
2807 if (parse_header(this) != SUCCESS)
2808 {
2809 DBG1(DBG_IKE, "failed to parse header of reassembled IKE message");
2810 return FAILED;
2811 }
2812 return SUCCESS;
2813 }
2814
2815 METHOD(message_t, add_fragment_v2, status_t,
2816 private_message_t *this, message_t *message)
2817 {
2818 encrypted_fragment_payload_t *encrypted_fragment;
2819 encrypted_payload_t *encrypted;
2820 payload_t *payload;
2821 enumerator_t *enumerator;
2822 chunk_t data;
2823 u_int16_t total, num;
2824 status_t status;
2825
2826 if (!this->frag)
2827 {
2828 return INVALID_STATE;
2829 }
2830 payload = message->get_payload(message, PLV2_FRAGMENT);
2831 if (!payload || this->message_id != message->get_message_id(message))
2832 {
2833 return INVALID_ARG;
2834 }
2835 encrypted_fragment = (encrypted_fragment_payload_t*)payload;
2836 total = encrypted_fragment->get_total_fragments(encrypted_fragment);
2837 if (total > MAX_FRAGMENTS)
2838 {
2839 DBG1(DBG_IKE, "maximum fragment count exceeded");
2840 reset_defrag(this);
2841 return FAILED;
2842 }
2843 if (!this->fragments || total > this->frag->last)
2844 {
2845 reset_defrag(this);
2846 this->frag->last = total;
2847 this->fragments = array_create(0, total);
2848 }
2849 num = encrypted_fragment->get_fragment_number(encrypted_fragment);
2850 data = encrypted_fragment->get_content(encrypted_fragment);
2851 status = add_fragment(this, num, data);
2852 if (status != SUCCESS)
2853 {
2854 return status;
2855 }
2856
2857 if (num == 1)
2858 {
2859 /* the first fragment denotes the payload type of the first payload in
2860 * the original encrypted payload, cache that */
2861 this->first_payload = payload->get_next_type(payload);
2862 /* move all unencrypted payloads contained in the first fragment */
2863 enumerator = message->create_payload_enumerator(message);
2864 while (enumerator->enumerate(enumerator, &payload))
2865 {
2866 if (payload->get_type(payload) != PLV2_FRAGMENT)
2867 {
2868 message->remove_payload_at(message, enumerator);
2869 this->payloads->insert_last(this->payloads, payload);
2870 }
2871 }
2872 enumerator->destroy(enumerator);
2873 }
2874
2875 if (array_count(this->fragments) != total)
2876 {
2877 /* there are some fragments missing */
2878 DBG1(DBG_ENC, "received fragment #%hu of %hu, waiting for complete IKE "
2879 "message", num, total);
2880 return NEED_MORE;
2881 }
2882
2883 DBG1(DBG_ENC, "received fragment #%hu of %hu, reassembling fragmented IKE "
2884 "message", num, total);
2885
2886 data = merge_fragments(this, message);
2887 encrypted = encrypted_payload_create_from_plain(this->first_payload, data);
2888 this->payloads->insert_last(this->payloads, encrypted);
2889 /* update next payload type (could be an unencrypted payload) */
2890 this->payloads->get_first(this->payloads, (void**)&payload);
2891 this->first_payload = payload->get_type(payload);
2892 return SUCCESS;
2893 }
2894
2895 METHOD(message_t, destroy, void,
2896 private_message_t *this)
2897 {
2898 DESTROY_IF(this->ike_sa_id);
2899 DESTROY_IF(this->parser);
2900 this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
2901 this->packet->destroy(this->packet);
2902 if (this->frag)
2903 {
2904 reset_defrag(this);
2905 free(this->frag);
2906 }
2907 else
2908 {
2909 array_destroy_offset(this->fragments, offsetof(packet_t, destroy));
2910 }
2911 free(this);
2912 }
2913
2914 /*
2915 * Described in header.
2916 */
2917 message_t *message_create_from_packet(packet_t *packet)
2918 {
2919 private_message_t *this;
2920
2921 INIT(this,
2922 .public = {
2923 .set_major_version = _set_major_version,
2924 .get_major_version = _get_major_version,
2925 .set_minor_version = _set_minor_version,
2926 .get_minor_version = _get_minor_version,
2927 .set_message_id = _set_message_id,
2928 .get_message_id = _get_message_id,
2929 .get_initiator_spi = _get_initiator_spi,
2930 .get_responder_spi = _get_responder_spi,
2931 .set_ike_sa_id = _set_ike_sa_id,
2932 .get_ike_sa_id = _get_ike_sa_id,
2933 .set_exchange_type = _set_exchange_type,
2934 .get_exchange_type = _get_exchange_type,
2935 .get_first_payload_type = _get_first_payload_type,
2936 .set_request = _set_request,
2937 .get_request = _get_request,
2938 .set_version_flag = _set_version_flag,
2939 .get_reserved_header_bit = _get_reserved_header_bit,
2940 .set_reserved_header_bit = _set_reserved_header_bit,
2941 .add_payload = _add_payload,
2942 .add_notify = _add_notify,
2943 .disable_sort = _disable_sort,
2944 .generate = _generate,
2945 .is_encoded = _is_encoded,
2946 .is_fragmented = _is_fragmented,
2947 .fragment = _fragment,
2948 .add_fragment = _add_fragment_v2,
2949 .set_source = _set_source,
2950 .get_source = _get_source,
2951 .set_destination = _set_destination,
2952 .get_destination = _get_destination,
2953 .create_payload_enumerator = _create_payload_enumerator,
2954 .remove_payload_at = _remove_payload_at,
2955 .get_payload = _get_payload,
2956 .get_notify = _get_notify,
2957 .parse_header = _parse_header,
2958 .parse_body = _parse_body,
2959 .get_packet = _get_packet,
2960 .get_packet_data = _get_packet_data,
2961 .get_fragments = _get_fragments,
2962 .destroy = _destroy,
2963 },
2964 .exchange_type = EXCHANGE_TYPE_UNDEFINED,
2965 .is_request = TRUE,
2966 .first_payload = PL_NONE,
2967 .packet = packet,
2968 .payloads = linked_list_create(),
2969 .parser = parser_create(packet->get_data(packet)),
2970 );
2971
2972 return &this->public;
2973 }
2974
2975 /*
2976 * Described in header.
2977 */
2978 message_t *message_create(int major, int minor)
2979 {
2980 message_t *this = message_create_from_packet(packet_create());
2981
2982 this->set_major_version(this, major);
2983 this->set_minor_version(this, minor);
2984
2985 return this;
2986 }
2987
2988 /*
2989 * Described in header.
2990 */
2991 message_t *message_create_defrag(message_t *fragment)
2992 {
2993 private_message_t *this;
2994
2995 if (!fragment->get_payload(fragment, PLV1_FRAGMENT) &&
2996 !fragment->get_payload(fragment, PLV2_FRAGMENT))
2997 {
2998 return NULL;
2999 }
3000 this = (private_message_t*)clone_message((private_message_t*)fragment);
3001 /* we don't need a parser for IKEv2, the one for IKEv1 is created after
3002 * reassembling the original message */
3003 this->parser->destroy(this->parser);
3004 this->parser = NULL;
3005 if (fragment->get_major_version(fragment) == IKEV1_MAJOR_VERSION)
3006 {
3007 /* we store the fragment ID in the message ID field, which should be
3008 * zero for fragments, but make sure */
3009 this->message_id = 0;
3010 this->public.add_fragment = _add_fragment_v1;
3011 }
3012 INIT(this->frag,
3013 .max_packet = lib->settings->get_int(lib->settings,
3014 "%s.max_packet", PACKET_MAX_DEFAULT, lib->ns),
3015 );
3016 return &this->public;
3017 }