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