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