ikev1: Move fragment generation to message_t
[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 <collections/array.h>
27 #include <daemon.h>
28 #include <sa/ikev1/keymat_v1.h>
29 #include <encoding/generator.h>
30 #include <encoding/parser.h>
31 #include <encoding/payloads/encodings.h>
32 #include <encoding/payloads/payload.h>
33 #include <encoding/payloads/hash_payload.h>
34 #include <encoding/payloads/encrypted_payload.h>
35 #include <encoding/payloads/unknown_payload.h>
36 #include <encoding/payloads/cp_payload.h>
37 #include <encoding/payloads/fragment_payload.h>
38
39 /**
40 * Max number of notify payloads per IKEv2 message
41 */
42 #define MAX_NOTIFY_PAYLOADS 20
43
44 /**
45 * Max number of delete payloads per IKEv2 message
46 */
47 #define MAX_DELETE_PAYLOADS 20
48
49 /**
50 * Max number of certificate payloads per IKEv2 message
51 */
52 #define MAX_CERT_PAYLOADS 8
53
54 /**
55 * Max number of vendor ID payloads per IKEv2 message
56 */
57 #define MAX_VID_PAYLOADS 20
58
59 /**
60 * Max number of certificate request payloads per IKEv1 message
61 */
62 #define MAX_CERTREQ_PAYLOADS 20
63
64 /**
65 * Max number of NAT-D payloads per IKEv1 message
66 */
67 #define MAX_NAT_D_PAYLOADS 10
68
69 /**
70 * A payload rule defines the rules for a payload
71 * in a specific message rule. It defines if and how
72 * many times a payload must/can occur in a message
73 * and if it must be encrypted.
74 */
75 typedef struct {
76 /* Payload type */
77 payload_type_t type;
78 /* Minimal occurrence of this payload. */
79 size_t min_occurence;
80 /* Max occurrence of this payload. */
81 size_t max_occurence;
82 /* TRUE if payload must be encrypted */
83 bool encrypted;
84 /* If payload occurs, the message rule is fulfilled */
85 bool sufficient;
86 } payload_rule_t;
87
88 /**
89 * payload ordering structure allows us to reorder payloads according to RFC.
90 */
91 typedef struct {
92 /** payload type */
93 payload_type_t type;
94 /** notify type, if payload == PLV2_NOTIFY */
95 notify_type_t notify;
96 } payload_order_t;
97
98 /**
99 * A message rule defines the kind of a message,
100 * if it has encrypted contents and a list
101 * of payload ordering rules and payload parsing rules.
102 */
103 typedef struct {
104 /** Type of message. */
105 exchange_type_t exchange_type;
106 /** Is message a request or response. */
107 bool is_request;
108 /** Message contains encrypted payloads. */
109 bool encrypted;
110 /** Number of payload rules which will follow */
111 int rule_count;
112 /** Pointer to first payload rule */
113 payload_rule_t *rules;
114 /** Number of payload order rules */
115 int order_count;
116 /** payload ordering rules */
117 payload_order_t *order;
118 } message_rule_t;
119
120 /**
121 * Message rule for IKE_SA_INIT from initiator.
122 */
123 static payload_rule_t ike_sa_init_i_rules[] = {
124 /* payload type min max encr suff */
125 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
126 {PLV2_SECURITY_ASSOCIATION, 1, 1, FALSE, FALSE},
127 {PLV2_KEY_EXCHANGE, 1, 1, FALSE, FALSE},
128 {PLV2_NONCE, 1, 1, FALSE, FALSE},
129 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
130 };
131
132 /**
133 * payload order for IKE_SA_INIT initiator
134 */
135 static payload_order_t ike_sa_init_i_order[] = {
136 /* payload type notify type */
137 {PLV2_NOTIFY, COOKIE},
138 {PLV2_SECURITY_ASSOCIATION, 0},
139 {PLV2_KEY_EXCHANGE, 0},
140 {PLV2_NONCE, 0},
141 {PLV2_NOTIFY, NAT_DETECTION_SOURCE_IP},
142 {PLV2_NOTIFY, NAT_DETECTION_DESTINATION_IP},
143 {PLV2_NOTIFY, 0},
144 {PLV2_VENDOR_ID, 0},
145 };
146
147 /**
148 * Message rule for IKE_SA_INIT from responder.
149 */
150 static payload_rule_t ike_sa_init_r_rules[] = {
151 /* payload type min max encr suff */
152 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, TRUE},
153 {PLV2_SECURITY_ASSOCIATION, 1, 1, FALSE, FALSE},
154 {PLV2_KEY_EXCHANGE, 1, 1, FALSE, FALSE},
155 {PLV2_NONCE, 1, 1, FALSE, FALSE},
156 {PLV2_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
157 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
158 };
159
160 /**
161 * payload order for IKE_SA_INIT responder
162 */
163 static payload_order_t ike_sa_init_r_order[] = {
164 /* payload type notify type */
165 {PLV2_SECURITY_ASSOCIATION, 0},
166 {PLV2_KEY_EXCHANGE, 0},
167 {PLV2_NONCE, 0},
168 {PLV2_NOTIFY, NAT_DETECTION_SOURCE_IP},
169 {PLV2_NOTIFY, NAT_DETECTION_DESTINATION_IP},
170 {PLV2_NOTIFY, HTTP_CERT_LOOKUP_SUPPORTED},
171 {PLV2_CERTREQ, 0},
172 {PLV2_NOTIFY, 0},
173 {PLV2_VENDOR_ID, 0},
174 };
175
176 /**
177 * Message rule for IKE_AUTH from initiator.
178 */
179 static payload_rule_t ike_auth_i_rules[] = {
180 /* payload type min max encr suff */
181 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
182 {PLV2_EAP, 0, 1, TRUE, TRUE},
183 {PLV2_AUTH, 0, 1, TRUE, TRUE},
184 {PLV2_ID_INITIATOR, 0, 1, TRUE, FALSE},
185 {PLV2_CERTIFICATE, 0, MAX_CERT_PAYLOADS, TRUE, FALSE},
186 {PLV2_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, TRUE, FALSE},
187 {PLV2_ID_RESPONDER, 0, 1, TRUE, FALSE},
188 #ifdef ME
189 {PLV2_SECURITY_ASSOCIATION, 0, 1, TRUE, FALSE},
190 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
191 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
192 #else
193 {PLV2_SECURITY_ASSOCIATION, 0, 1, TRUE, FALSE},
194 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
195 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
196 #endif /* ME */
197 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
198 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
199 };
200
201 /**
202 * payload order for IKE_AUTH initiator
203 */
204 static payload_order_t ike_auth_i_order[] = {
205 /* payload type notify type */
206 {PLV2_ID_INITIATOR, 0},
207 {PLV2_CERTIFICATE, 0},
208 {PLV2_NOTIFY, INITIAL_CONTACT},
209 {PLV2_NOTIFY, HTTP_CERT_LOOKUP_SUPPORTED},
210 {PLV2_CERTREQ, 0},
211 {PLV2_ID_RESPONDER, 0},
212 {PLV2_AUTH, 0},
213 {PLV2_EAP, 0},
214 {PLV2_CONFIGURATION, 0},
215 {PLV2_NOTIFY, IPCOMP_SUPPORTED},
216 {PLV2_NOTIFY, USE_TRANSPORT_MODE},
217 {PLV2_NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
218 {PLV2_NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
219 {PLV2_SECURITY_ASSOCIATION, 0},
220 {PLV2_TS_INITIATOR, 0},
221 {PLV2_TS_RESPONDER, 0},
222 {PLV2_NOTIFY, MOBIKE_SUPPORTED},
223 {PLV2_NOTIFY, ADDITIONAL_IP4_ADDRESS},
224 {PLV2_NOTIFY, ADDITIONAL_IP6_ADDRESS},
225 {PLV2_NOTIFY, NO_ADDITIONAL_ADDRESSES},
226 {PLV2_NOTIFY, 0},
227 {PLV2_VENDOR_ID, 0},
228 };
229
230 /**
231 * Message rule for IKE_AUTH from responder.
232 */
233 static payload_rule_t ike_auth_r_rules[] = {
234 /* payload type min max encr suff */
235 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
236 {PLV2_EAP, 0, 1, TRUE, TRUE},
237 {PLV2_AUTH, 0, 1, TRUE, TRUE},
238 {PLV2_CERTIFICATE, 0, MAX_CERT_PAYLOADS, TRUE, FALSE},
239 {PLV2_ID_RESPONDER, 0, 1, TRUE, FALSE},
240 {PLV2_SECURITY_ASSOCIATION, 0, 1, TRUE, FALSE},
241 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
242 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
243 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
244 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
245 };
246
247 /**
248 * payload order for IKE_AUTH responder
249 */
250 static payload_order_t ike_auth_r_order[] = {
251 /* payload type notify type */
252 {PLV2_ID_RESPONDER, 0},
253 {PLV2_CERTIFICATE, 0},
254 {PLV2_AUTH, 0},
255 {PLV2_EAP, 0},
256 {PLV2_CONFIGURATION, 0},
257 {PLV2_NOTIFY, IPCOMP_SUPPORTED},
258 {PLV2_NOTIFY, USE_TRANSPORT_MODE},
259 {PLV2_NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
260 {PLV2_NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
261 {PLV2_SECURITY_ASSOCIATION, 0},
262 {PLV2_TS_INITIATOR, 0},
263 {PLV2_TS_RESPONDER, 0},
264 {PLV2_NOTIFY, AUTH_LIFETIME},
265 {PLV2_NOTIFY, MOBIKE_SUPPORTED},
266 {PLV2_NOTIFY, ADDITIONAL_IP4_ADDRESS},
267 {PLV2_NOTIFY, ADDITIONAL_IP6_ADDRESS},
268 {PLV2_NOTIFY, NO_ADDITIONAL_ADDRESSES},
269 {PLV2_NOTIFY, 0},
270 {PLV2_VENDOR_ID, 0},
271 };
272
273 /**
274 * Message rule for INFORMATIONAL from initiator.
275 */
276 static payload_rule_t informational_i_rules[] = {
277 /* payload type min max encr suff */
278 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
279 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
280 {PLV2_DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
281 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
282 };
283
284 /**
285 * payload order for INFORMATIONAL initiator
286 */
287 static payload_order_t informational_i_order[] = {
288 /* payload type notify type */
289 {PLV2_NOTIFY, UPDATE_SA_ADDRESSES},
290 {PLV2_NOTIFY, NAT_DETECTION_SOURCE_IP},
291 {PLV2_NOTIFY, NAT_DETECTION_DESTINATION_IP},
292 {PLV2_NOTIFY, COOKIE2},
293 {PLV2_NOTIFY, 0},
294 {PLV2_DELETE, 0},
295 {PLV2_CONFIGURATION, 0},
296 };
297
298 /**
299 * Message rule for INFORMATIONAL from responder.
300 */
301 static payload_rule_t informational_r_rules[] = {
302 /* payload type min max encr suff */
303 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
304 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
305 {PLV2_DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
306 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
307 };
308
309 /**
310 * payload order for INFORMATIONAL responder
311 */
312 static payload_order_t informational_r_order[] = {
313 /* payload type notify type */
314 {PLV2_NOTIFY, UPDATE_SA_ADDRESSES},
315 {PLV2_NOTIFY, NAT_DETECTION_SOURCE_IP},
316 {PLV2_NOTIFY, NAT_DETECTION_DESTINATION_IP},
317 {PLV2_NOTIFY, COOKIE2},
318 {PLV2_NOTIFY, 0},
319 {PLV2_DELETE, 0},
320 {PLV2_CONFIGURATION, 0},
321 };
322
323 /**
324 * Message rule for CREATE_CHILD_SA from initiator.
325 */
326 static payload_rule_t create_child_sa_i_rules[] = {
327 /* payload type min max encr suff */
328 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
329 {PLV2_SECURITY_ASSOCIATION, 1, 1, TRUE, FALSE},
330 {PLV2_NONCE, 1, 1, TRUE, FALSE},
331 {PLV2_KEY_EXCHANGE, 0, 1, TRUE, FALSE},
332 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
333 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
334 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
335 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
336 };
337
338 /**
339 * payload order for CREATE_CHILD_SA from initiator.
340 */
341 static payload_order_t create_child_sa_i_order[] = {
342 /* payload type notify type */
343 {PLV2_NOTIFY, REKEY_SA},
344 {PLV2_NOTIFY, IPCOMP_SUPPORTED},
345 {PLV2_NOTIFY, USE_TRANSPORT_MODE},
346 {PLV2_NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
347 {PLV2_NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
348 {PLV2_SECURITY_ASSOCIATION, 0},
349 {PLV2_NONCE, 0},
350 {PLV2_KEY_EXCHANGE, 0},
351 {PLV2_TS_INITIATOR, 0},
352 {PLV2_TS_RESPONDER, 0},
353 {PLV2_NOTIFY, 0},
354 };
355
356 /**
357 * Message rule for CREATE_CHILD_SA from responder.
358 */
359 static payload_rule_t create_child_sa_r_rules[] = {
360 /* payload type min max encr suff */
361 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
362 {PLV2_SECURITY_ASSOCIATION, 1, 1, TRUE, FALSE},
363 {PLV2_NONCE, 1, 1, TRUE, FALSE},
364 {PLV2_KEY_EXCHANGE, 0, 1, TRUE, FALSE},
365 {PLV2_TS_INITIATOR, 0, 1, TRUE, FALSE},
366 {PLV2_TS_RESPONDER, 0, 1, TRUE, FALSE},
367 {PLV2_CONFIGURATION, 0, 1, TRUE, FALSE},
368 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
369 };
370
371 /**
372 * payload order for CREATE_CHILD_SA from responder.
373 */
374 static payload_order_t create_child_sa_r_order[] = {
375 /* payload type notify type */
376 {PLV2_NOTIFY, IPCOMP_SUPPORTED},
377 {PLV2_NOTIFY, USE_TRANSPORT_MODE},
378 {PLV2_NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
379 {PLV2_NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
380 {PLV2_SECURITY_ASSOCIATION, 0},
381 {PLV2_NONCE, 0},
382 {PLV2_KEY_EXCHANGE, 0},
383 {PLV2_TS_INITIATOR, 0},
384 {PLV2_TS_RESPONDER, 0},
385 {PLV2_NOTIFY, ADDITIONAL_TS_POSSIBLE},
386 {PLV2_NOTIFY, 0},
387 };
388
389 #ifdef ME
390 /**
391 * Message rule for ME_CONNECT from initiator.
392 */
393 static payload_rule_t me_connect_i_rules[] = {
394 /* payload type min max encr suff */
395 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
396 {PLV2_ID_PEER, 1, 1, TRUE, FALSE},
397 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE}
398 };
399
400 /**
401 * payload order for ME_CONNECT from initiator.
402 */
403 static payload_order_t me_connect_i_order[] = {
404 /* payload type notify type */
405 {PLV2_NOTIFY, 0},
406 {PLV2_ID_PEER, 0},
407 {PLV2_VENDOR_ID, 0},
408 };
409
410 /**
411 * Message rule for ME_CONNECT from responder.
412 */
413 static payload_rule_t me_connect_r_rules[] = {
414 /* payload type min max encr suff */
415 {PLV2_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
416 {PLV2_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE}
417 };
418
419 /**
420 * payload order for ME_CONNECT from responder.
421 */
422 static payload_order_t me_connect_r_order[] = {
423 /* payload type notify type */
424 {PLV2_NOTIFY, 0},
425 {PLV2_VENDOR_ID, 0},
426 };
427 #endif /* ME */
428
429 #ifdef USE_IKEV1
430 /**
431 * Message rule for ID_PROT from initiator.
432 */
433 static payload_rule_t id_prot_i_rules[] = {
434 /* payload type min max encr suff */
435 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
436 {PLV1_SECURITY_ASSOCIATION, 0, 1, FALSE, FALSE},
437 {PLV1_KEY_EXCHANGE, 0, 1, FALSE, FALSE},
438 {PLV1_NONCE, 0, 1, FALSE, FALSE},
439 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
440 {PLV1_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
441 {PLV1_NAT_D, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
442 {PLV1_NAT_D_DRAFT_00_03, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
443 {PLV1_ID, 0, 1, TRUE, FALSE},
444 {PLV1_CERTIFICATE, 0, MAX_CERT_PAYLOADS, TRUE, FALSE},
445 {PLV1_SIGNATURE, 0, 1, TRUE, FALSE},
446 {PLV1_HASH, 0, 1, TRUE, FALSE},
447 {PLV1_FRAGMENT, 0, 1, FALSE, TRUE},
448 };
449
450 /**
451 * payload order for ID_PROT from initiator.
452 */
453 static payload_order_t id_prot_i_order[] = {
454 /* payload type notify type */
455 {PLV1_SECURITY_ASSOCIATION, 0},
456 {PLV1_KEY_EXCHANGE, 0},
457 {PLV1_NONCE, 0},
458 {PLV1_ID, 0},
459 {PLV1_CERTIFICATE, 0},
460 {PLV1_SIGNATURE, 0},
461 {PLV1_HASH, 0},
462 {PLV1_CERTREQ, 0},
463 {PLV1_NOTIFY, 0},
464 {PLV1_VENDOR_ID, 0},
465 {PLV1_NAT_D, 0},
466 {PLV1_NAT_D_DRAFT_00_03, 0},
467 {PLV1_FRAGMENT, 0},
468 };
469
470 /**
471 * Message rule for ID_PROT from responder.
472 */
473 static payload_rule_t id_prot_r_rules[] = {
474 /* payload type min max encr suff */
475 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
476 {PLV1_SECURITY_ASSOCIATION, 0, 1, FALSE, FALSE},
477 {PLV1_KEY_EXCHANGE, 0, 1, FALSE, FALSE},
478 {PLV1_NONCE, 0, 1, FALSE, FALSE},
479 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
480 {PLV1_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
481 {PLV1_NAT_D, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
482 {PLV1_NAT_D_DRAFT_00_03, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
483 {PLV1_ID, 0, 1, TRUE, FALSE},
484 {PLV1_CERTIFICATE, 0, MAX_CERT_PAYLOADS, TRUE, FALSE},
485 {PLV1_SIGNATURE, 0, 1, TRUE, FALSE},
486 {PLV1_HASH, 0, 1, TRUE, FALSE},
487 {PLV1_FRAGMENT, 0, 1, FALSE, TRUE},
488 };
489
490 /**
491 * payload order for ID_PROT from responder.
492 */
493 static payload_order_t id_prot_r_order[] = {
494 /* payload type notify type */
495 {PLV1_SECURITY_ASSOCIATION, 0},
496 {PLV1_KEY_EXCHANGE, 0},
497 {PLV1_NONCE, 0},
498 {PLV1_ID, 0},
499 {PLV1_CERTIFICATE, 0},
500 {PLV1_SIGNATURE, 0},
501 {PLV1_HASH, 0},
502 {PLV1_CERTREQ, 0},
503 {PLV1_NOTIFY, 0},
504 {PLV1_VENDOR_ID, 0},
505 {PLV1_NAT_D, 0},
506 {PLV1_NAT_D_DRAFT_00_03, 0},
507 {PLV1_FRAGMENT, 0},
508 };
509
510 /**
511 * Message rule for AGGRESSIVE from initiator.
512 */
513 static payload_rule_t aggressive_i_rules[] = {
514 /* payload type min max encr suff */
515 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
516 {PLV1_SECURITY_ASSOCIATION, 0, 1, FALSE, FALSE},
517 {PLV1_KEY_EXCHANGE, 0, 1, FALSE, FALSE},
518 {PLV1_NONCE, 0, 1, FALSE, FALSE},
519 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
520 {PLV1_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
521 {PLV1_NAT_D, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
522 {PLV1_NAT_D_DRAFT_00_03, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
523 {PLV1_ID, 0, 1, FALSE, FALSE},
524 {PLV1_CERTIFICATE, 0, 1, TRUE, FALSE},
525 {PLV1_SIGNATURE, 0, 1, TRUE, FALSE},
526 {PLV1_HASH, 0, 1, TRUE, FALSE},
527 {PLV1_FRAGMENT, 0, 1, FALSE, TRUE},
528 };
529
530 /**
531 * payload order for AGGRESSIVE from initiator.
532 */
533 static payload_order_t aggressive_i_order[] = {
534 /* payload type notify type */
535 {PLV1_SECURITY_ASSOCIATION, 0},
536 {PLV1_KEY_EXCHANGE, 0},
537 {PLV1_NONCE, 0},
538 {PLV1_ID, 0},
539 {PLV1_CERTIFICATE, 0},
540 {PLV1_NAT_D, 0},
541 {PLV1_NAT_D_DRAFT_00_03, 0},
542 {PLV1_SIGNATURE, 0},
543 {PLV1_HASH, 0},
544 {PLV1_CERTREQ, 0},
545 {PLV1_NOTIFY, 0},
546 {PLV1_VENDOR_ID, 0},
547 {PLV1_FRAGMENT, 0},
548 };
549
550 /**
551 * Message rule for AGGRESSIVE from responder.
552 */
553 static payload_rule_t aggressive_r_rules[] = {
554 /* payload type min max encr suff */
555 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
556 {PLV1_SECURITY_ASSOCIATION, 0, 1, FALSE, FALSE},
557 {PLV1_KEY_EXCHANGE, 0, 1, FALSE, FALSE},
558 {PLV1_NONCE, 0, 1, FALSE, FALSE},
559 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, FALSE, FALSE},
560 {PLV1_CERTREQ, 0, MAX_CERTREQ_PAYLOADS, FALSE, FALSE},
561 {PLV1_NAT_D, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
562 {PLV1_NAT_D_DRAFT_00_03, 0, MAX_NAT_D_PAYLOADS, FALSE, FALSE},
563 {PLV1_ID, 0, 1, FALSE, FALSE},
564 {PLV1_CERTIFICATE, 0, 1, FALSE, FALSE},
565 {PLV1_SIGNATURE, 0, 1, FALSE, FALSE},
566 {PLV1_HASH, 0, 1, FALSE, FALSE},
567 {PLV1_FRAGMENT, 0, 1, FALSE, TRUE},
568 };
569
570 /**
571 * payload order for AGGRESSIVE from responder.
572 */
573 static payload_order_t aggressive_r_order[] = {
574 /* payload type notify type */
575 {PLV1_SECURITY_ASSOCIATION, 0},
576 {PLV1_KEY_EXCHANGE, 0},
577 {PLV1_NONCE, 0},
578 {PLV1_ID, 0},
579 {PLV1_CERTIFICATE, 0},
580 {PLV1_NAT_D, 0},
581 {PLV1_NAT_D_DRAFT_00_03, 0},
582 {PLV1_SIGNATURE, 0},
583 {PLV1_HASH, 0},
584 {PLV1_CERTREQ, 0},
585 {PLV1_NOTIFY, 0},
586 {PLV1_VENDOR_ID, 0},
587 {PLV1_FRAGMENT, 0},
588 };
589
590 /**
591 * Message rule for INFORMATIONAL_V1 from initiator.
592 */
593 static payload_rule_t informational_i_rules_v1[] = {
594 /* payload type min max encr suff */
595 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
596 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
597 {PLV1_DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
598 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
599 };
600
601 /**
602 * payload order for INFORMATIONAL_V1 from initiator.
603 */
604 static payload_order_t informational_i_order_v1[] = {
605 /* payload type notify type */
606 {PLV1_NOTIFY, 0},
607 {PLV1_DELETE, 0},
608 {PLV1_VENDOR_ID, 0},
609 };
610
611 /**
612 * Message rule for INFORMATIONAL_V1 from responder.
613 */
614 static payload_rule_t informational_r_rules_v1[] = {
615 /* payload type min max encr suff */
616 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
617 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
618 {PLV1_DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
619 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
620 };
621
622 /**
623 * payload order for INFORMATIONAL_V1 from responder.
624 */
625 static payload_order_t informational_r_order_v1[] = {
626 /* payload type notify type */
627 {PLV1_NOTIFY, 0},
628 {PLV1_DELETE, 0},
629 {PLV1_VENDOR_ID, 0},
630 };
631
632 /**
633 * Message rule for QUICK_MODE from initiator.
634 */
635 static payload_rule_t quick_mode_i_rules[] = {
636 /* payload type min max encr suff */
637 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
638 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
639 {PLV1_HASH, 0, 1, TRUE, FALSE},
640 {PLV1_SECURITY_ASSOCIATION, 0, 2, TRUE, FALSE},
641 {PLV1_NONCE, 0, 1, TRUE, FALSE},
642 {PLV1_KEY_EXCHANGE, 0, 1, TRUE, FALSE},
643 {PLV1_ID, 0, 2, TRUE, FALSE},
644 {PLV1_NAT_OA, 0, 2, TRUE, FALSE},
645 {PLV1_NAT_OA_DRAFT_00_03, 0, 2, TRUE, FALSE},
646 };
647
648 /**
649 * payload order for QUICK_MODE from initiator.
650 */
651 static payload_order_t quick_mode_i_order[] = {
652 /* payload type notify type */
653 {PLV1_NOTIFY, 0},
654 {PLV1_VENDOR_ID, 0},
655 {PLV1_HASH, 0},
656 {PLV1_SECURITY_ASSOCIATION, 0},
657 {PLV1_NONCE, 0},
658 {PLV1_KEY_EXCHANGE, 0},
659 {PLV1_ID, 0},
660 {PLV1_NAT_OA, 0},
661 {PLV1_NAT_OA_DRAFT_00_03, 0},
662 };
663
664 /**
665 * Message rule for QUICK_MODE from responder.
666 */
667 static payload_rule_t quick_mode_r_rules[] = {
668 /* payload type min max encr suff */
669 {PLV1_NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
670 {PLV1_VENDOR_ID, 0, MAX_VID_PAYLOADS, TRUE, FALSE},
671 {PLV1_HASH, 0, 1, TRUE, FALSE},
672 {PLV1_SECURITY_ASSOCIATION, 0, 2, TRUE, FALSE},
673 {PLV1_NONCE, 0, 1, TRUE, FALSE},
674 {PLV1_KEY_EXCHANGE, 0, 1, TRUE, FALSE},
675 {PLV1_ID, 0, 2, TRUE, FALSE},
676 {PLV1_NAT_OA, 0, 2, TRUE, FALSE},
677 {PLV1_NAT_OA_DRAFT_00_03, 0, 2, TRUE, FALSE},
678 };
679
680 /**
681 * payload order for QUICK_MODE from responder.
682 */
683 static payload_order_t quick_mode_r_order[] = {
684 /* payload type notify type */
685 {PLV1_NOTIFY, 0},
686 {PLV1_VENDOR_ID, 0},
687 {PLV1_HASH, 0},
688 {PLV1_SECURITY_ASSOCIATION, 0},
689 {PLV1_NONCE, 0},
690 {PLV1_KEY_EXCHANGE, 0},
691 {PLV1_ID, 0},
692 {PLV1_NAT_OA, 0},
693 {PLV1_NAT_OA_DRAFT_00_03, 0},
694 };
695
696 /**
697 * Message rule for TRANSACTION.
698 */
699 static payload_rule_t transaction_payload_rules_v1[] = {
700 /* payload type min max encr suff */
701 {PLV1_HASH, 0, 1, TRUE, FALSE},
702 {PLV1_CONFIGURATION, 1, 1, FALSE, FALSE},
703 };
704
705 /**
706 * Payload order for TRANSACTION.
707 */
708 static payload_order_t transaction_payload_order_v1[] = {
709 /* payload type notify type */
710 {PLV1_HASH, 0},
711 {PLV1_CONFIGURATION, 0},
712 };
713
714 #endif /* USE_IKEV1 */
715
716 /**
717 * Message rules, defines allowed payloads.
718 */
719 static message_rule_t message_rules[] = {
720 {IKE_SA_INIT, TRUE, FALSE,
721 countof(ike_sa_init_i_rules), ike_sa_init_i_rules,
722 countof(ike_sa_init_i_order), ike_sa_init_i_order,
723 },
724 {IKE_SA_INIT, FALSE, FALSE,
725 countof(ike_sa_init_r_rules), ike_sa_init_r_rules,
726 countof(ike_sa_init_r_order), ike_sa_init_r_order,
727 },
728 {IKE_AUTH, TRUE, TRUE,
729 countof(ike_auth_i_rules), ike_auth_i_rules,
730 countof(ike_auth_i_order), ike_auth_i_order,
731 },
732 {IKE_AUTH, FALSE, TRUE,
733 countof(ike_auth_r_rules), ike_auth_r_rules,
734 countof(ike_auth_r_order), ike_auth_r_order,
735 },
736 {INFORMATIONAL, TRUE, TRUE,
737 countof(informational_i_rules), informational_i_rules,
738 countof(informational_i_order), informational_i_order,
739 },
740 {INFORMATIONAL, FALSE, TRUE,
741 countof(informational_r_rules), informational_r_rules,
742 countof(informational_r_order), informational_r_order,
743 },
744 {CREATE_CHILD_SA, TRUE, TRUE,
745 countof(create_child_sa_i_rules), create_child_sa_i_rules,
746 countof(create_child_sa_i_order), create_child_sa_i_order,
747 },
748 {CREATE_CHILD_SA, FALSE, TRUE,
749 countof(create_child_sa_r_rules), create_child_sa_r_rules,
750 countof(create_child_sa_r_order), create_child_sa_r_order,
751 },
752 #ifdef ME
753 {ME_CONNECT, TRUE, TRUE,
754 countof(me_connect_i_rules), me_connect_i_rules,
755 countof(me_connect_i_order), me_connect_i_order,
756 },
757 {ME_CONNECT, FALSE, TRUE,
758 countof(me_connect_r_rules), me_connect_r_rules,
759 countof(me_connect_r_order), me_connect_r_order,
760 },
761 #endif /* ME */
762 #ifdef USE_IKEV1
763 {ID_PROT, TRUE, FALSE,
764 countof(id_prot_i_rules), id_prot_i_rules,
765 countof(id_prot_i_order), id_prot_i_order,
766 },
767 {ID_PROT, FALSE, FALSE,
768 countof(id_prot_r_rules), id_prot_r_rules,
769 countof(id_prot_r_order), id_prot_r_order,
770 },
771 {AGGRESSIVE, TRUE, FALSE,
772 countof(aggressive_i_rules), aggressive_i_rules,
773 countof(aggressive_i_order), aggressive_i_order,
774 },
775 {AGGRESSIVE, FALSE, FALSE,
776 countof(aggressive_r_rules), aggressive_r_rules,
777 countof(aggressive_r_order), aggressive_r_order,
778 },
779 {INFORMATIONAL_V1, TRUE, TRUE,
780 countof(informational_i_rules_v1), informational_i_rules_v1,
781 countof(informational_i_order_v1), informational_i_order_v1,
782 },
783 {INFORMATIONAL_V1, FALSE, TRUE,
784 countof(informational_r_rules_v1), informational_r_rules_v1,
785 countof(informational_r_order_v1), informational_r_order_v1,
786 },
787 {QUICK_MODE, TRUE, TRUE,
788 countof(quick_mode_i_rules), quick_mode_i_rules,
789 countof(quick_mode_i_order), quick_mode_i_order,
790 },
791 {QUICK_MODE, FALSE, TRUE,
792 countof(quick_mode_r_rules), quick_mode_r_rules,
793 countof(quick_mode_r_order), quick_mode_r_order,
794 },
795 {TRANSACTION, TRUE, TRUE,
796 countof(transaction_payload_rules_v1), transaction_payload_rules_v1,
797 countof(transaction_payload_order_v1), transaction_payload_order_v1,
798 },
799 {TRANSACTION, FALSE, TRUE,
800 countof(transaction_payload_rules_v1), transaction_payload_rules_v1,
801 countof(transaction_payload_order_v1), transaction_payload_order_v1,
802 },
803 /* TODO-IKEv1: define rules for other exchanges */
804 #endif /* USE_IKEV1 */
805 };
806
807
808 typedef struct private_message_t private_message_t;
809
810 /**
811 * Private data of an message_t object.
812 */
813 struct private_message_t {
814
815 /**
816 * Public part of a message_t object.
817 */
818 message_t public;
819
820 /**
821 * Minor version of message.
822 */
823 u_int8_t major_version;
824
825 /**
826 * Major version of message.
827 */
828 u_int8_t minor_version;
829
830 /**
831 * First Payload in message.
832 */
833 payload_type_t first_payload;
834
835 /**
836 * Assigned exchange type.
837 */
838 exchange_type_t exchange_type;
839
840 /**
841 * TRUE if message is a request, FALSE if a reply.
842 */
843 bool is_request;
844
845 /**
846 * The message is encrypted (IKEv1)
847 */
848 bool is_encrypted;
849
850 /**
851 * Higher version supported?
852 */
853 bool version_flag;
854
855 /**
856 * Reserved bits in IKE header
857 */
858 bool reserved[2];
859
860 /**
861 * Sorting of message disabled?
862 */
863 bool sort_disabled;
864
865 /**
866 * Message ID of this message.
867 */
868 u_int32_t message_id;
869
870 /**
871 * ID of assigned IKE_SA.
872 */
873 ike_sa_id_t *ike_sa_id;
874
875 /**
876 * Assigned UDP packet, stores incoming packet or last generated one.
877 */
878 packet_t *packet;
879
880 /**
881 * Linked List where payload data are stored in.
882 */
883 linked_list_t *payloads;
884
885 /**
886 * Assigned parser to parse Header and Body of this message.
887 */
888 parser_t *parser;
889
890 /**
891 * The message rule for this message instance
892 */
893 message_rule_t *rule;
894 };
895
896 /**
897 * Get the message rule that applies to this message
898 */
899 static message_rule_t* get_message_rule(private_message_t *this)
900 {
901 int i;
902
903 for (i = 0; i < countof(message_rules); i++)
904 {
905 if ((this->exchange_type == message_rules[i].exchange_type) &&
906 (this->is_request == message_rules[i].is_request))
907 {
908 return &message_rules[i];
909 }
910 }
911 return NULL;
912 }
913
914 /**
915 * Look up a payload rule
916 */
917 static payload_rule_t* get_payload_rule(private_message_t *this,
918 payload_type_t type)
919 {
920 int i;
921
922 for (i = 0; i < this->rule->rule_count;i++)
923 {
924 if (this->rule->rules[i].type == type)
925 {
926 return &this->rule->rules[i];
927 }
928 }
929 return NULL;
930 }
931
932 METHOD(message_t, set_ike_sa_id, void,
933 private_message_t *this,ike_sa_id_t *ike_sa_id)
934 {
935 DESTROY_IF(this->ike_sa_id);
936 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
937 }
938
939 METHOD(message_t, get_ike_sa_id, ike_sa_id_t*,
940 private_message_t *this)
941 {
942 return this->ike_sa_id;
943 }
944
945 METHOD(message_t, set_message_id, void,
946 private_message_t *this,u_int32_t message_id)
947 {
948 this->message_id = message_id;
949 }
950
951 METHOD(message_t, get_message_id, u_int32_t,
952 private_message_t *this)
953 {
954 return this->message_id;
955 }
956
957 METHOD(message_t, get_initiator_spi, u_int64_t,
958 private_message_t *this)
959 {
960 return (this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
961 }
962
963 METHOD(message_t, get_responder_spi, u_int64_t,
964 private_message_t *this)
965 {
966 return (this->ike_sa_id->get_responder_spi(this->ike_sa_id));
967 }
968
969 METHOD(message_t, set_major_version, void,
970 private_message_t *this, u_int8_t major_version)
971 {
972 this->major_version = major_version;
973 }
974
975 METHOD(message_t, get_major_version, u_int8_t,
976 private_message_t *this)
977 {
978 return this->major_version;
979 }
980
981 METHOD(message_t, set_minor_version, void,
982 private_message_t *this,u_int8_t minor_version)
983 {
984 this->minor_version = minor_version;
985 }
986
987 METHOD(message_t, get_minor_version, u_int8_t,
988 private_message_t *this)
989 {
990 return this->minor_version;
991 }
992
993 METHOD(message_t, set_exchange_type, void,
994 private_message_t *this, exchange_type_t exchange_type)
995 {
996 this->exchange_type = exchange_type;
997 }
998
999 METHOD(message_t, get_exchange_type, exchange_type_t,
1000 private_message_t *this)
1001 {
1002 return this->exchange_type;
1003 }
1004
1005 METHOD(message_t, get_first_payload_type, payload_type_t,
1006 private_message_t *this)
1007 {
1008 return this->first_payload;
1009 }
1010
1011 METHOD(message_t, set_request, void,
1012 private_message_t *this, bool request)
1013 {
1014 this->is_request = request;
1015 }
1016
1017 METHOD(message_t, get_request, bool,
1018 private_message_t *this)
1019 {
1020 return this->is_request;
1021 }
1022
1023 METHOD(message_t, set_version_flag, void,
1024 private_message_t *this)
1025 {
1026 this->version_flag = TRUE;
1027 }
1028
1029 METHOD(message_t, get_reserved_header_bit, bool,
1030 private_message_t *this, u_int nr)
1031 {
1032 if (nr < countof(this->reserved))
1033 {
1034 return this->reserved[nr];
1035 }
1036 return FALSE;
1037 }
1038
1039 METHOD(message_t, set_reserved_header_bit, void,
1040 private_message_t *this, u_int nr)
1041 {
1042 if (nr < countof(this->reserved))
1043 {
1044 this->reserved[nr] = TRUE;
1045 }
1046 }
1047
1048 METHOD(message_t, is_encoded, bool,
1049 private_message_t *this)
1050 {
1051 return this->packet->get_data(this->packet).ptr != NULL;
1052 }
1053
1054 METHOD(message_t, add_payload, void,
1055 private_message_t *this, payload_t *payload)
1056 {
1057 payload_t *last_payload;
1058
1059 if (this->payloads->get_count(this->payloads) > 0)
1060 {
1061 this->payloads->get_last(this->payloads, (void **)&last_payload);
1062 last_payload->set_next_type(last_payload, payload->get_type(payload));
1063 }
1064 else
1065 {
1066 this->first_payload = payload->get_type(payload);
1067 }
1068 payload->set_next_type(payload, PL_NONE);
1069 this->payloads->insert_last(this->payloads, payload);
1070
1071 DBG2(DBG_ENC ,"added payload of type %N to message",
1072 payload_type_names, payload->get_type(payload));
1073 }
1074
1075 METHOD(message_t, add_notify, void,
1076 private_message_t *this, bool flush, notify_type_t type, chunk_t data)
1077 {
1078 notify_payload_t *notify;
1079 payload_t *payload;
1080
1081 if (flush)
1082 {
1083 while (this->payloads->remove_last(this->payloads,
1084 (void**)&payload) == SUCCESS)
1085 {
1086 payload->destroy(payload);
1087 }
1088 }
1089 if (this->major_version == IKEV2_MAJOR_VERSION)
1090 {
1091 notify = notify_payload_create(PLV2_NOTIFY);
1092 }
1093 else
1094 {
1095 notify = notify_payload_create(PLV1_NOTIFY);
1096 }
1097 notify->set_notify_type(notify, type);
1098 notify->set_notification_data(notify, data);
1099 add_payload(this, (payload_t*)notify);
1100 }
1101
1102 METHOD(message_t, set_source, void,
1103 private_message_t *this, host_t *host)
1104 {
1105 this->packet->set_source(this->packet, host);
1106 }
1107
1108 METHOD(message_t, set_destination, void,
1109 private_message_t *this, host_t *host)
1110 {
1111 this->packet->set_destination(this->packet, host);
1112 }
1113
1114 METHOD(message_t, get_source, host_t*,
1115 private_message_t *this)
1116 {
1117 return this->packet->get_source(this->packet);
1118 }
1119
1120 METHOD(message_t, get_destination, host_t*,
1121 private_message_t *this)
1122 {
1123 return this->packet->get_destination(this->packet);
1124 }
1125
1126 METHOD(message_t, create_payload_enumerator, enumerator_t*,
1127 private_message_t *this)
1128 {
1129 return this->payloads->create_enumerator(this->payloads);
1130 }
1131
1132 METHOD(message_t, remove_payload_at, void,
1133 private_message_t *this, enumerator_t *enumerator)
1134 {
1135 this->payloads->remove_at(this->payloads, enumerator);
1136 }
1137
1138 METHOD(message_t, get_payload, payload_t*,
1139 private_message_t *this, payload_type_t type)
1140 {
1141 payload_t *current, *found = NULL;
1142 enumerator_t *enumerator;
1143
1144 enumerator = create_payload_enumerator(this);
1145 while (enumerator->enumerate(enumerator, &current))
1146 {
1147 if (current->get_type(current) == type)
1148 {
1149 found = current;
1150 break;
1151 }
1152 }
1153 enumerator->destroy(enumerator);
1154 return found;
1155 }
1156
1157 METHOD(message_t, get_notify, notify_payload_t*,
1158 private_message_t *this, notify_type_t type)
1159 {
1160 enumerator_t *enumerator;
1161 notify_payload_t *notify = NULL;
1162 payload_t *payload;
1163
1164 enumerator = create_payload_enumerator(this);
1165 while (enumerator->enumerate(enumerator, &payload))
1166 {
1167 if (payload->get_type(payload) == PLV2_NOTIFY ||
1168 payload->get_type(payload) == PLV1_NOTIFY)
1169 {
1170 notify = (notify_payload_t*)payload;
1171 if (notify->get_notify_type(notify) == type)
1172 {
1173 break;
1174 }
1175 notify = NULL;
1176 }
1177 }
1178 enumerator->destroy(enumerator);
1179 return notify;
1180 }
1181
1182 /**
1183 * get a string representation of the message
1184 */
1185 static char* get_string(private_message_t *this, char *buf, int len)
1186 {
1187 enumerator_t *enumerator;
1188 payload_t *payload;
1189 int written;
1190 char *pos = buf;
1191
1192 memset(buf, 0, len);
1193 len--;
1194
1195 written = snprintf(pos, len, "%N %s %u [",
1196 exchange_type_names, this->exchange_type,
1197 this->is_request ? "request" : "response",
1198 this->message_id);
1199 if (written >= len || written < 0)
1200 {
1201 return "";
1202 }
1203 pos += written;
1204 len -= written;
1205
1206 enumerator = create_payload_enumerator(this);
1207 while (enumerator->enumerate(enumerator, &payload))
1208 {
1209 written = snprintf(pos, len, " %N", payload_type_short_names,
1210 payload->get_type(payload));
1211 if (written >= len || written < 0)
1212 {
1213 return buf;
1214 }
1215 pos += written;
1216 len -= written;
1217 if (payload->get_type(payload) == PLV2_NOTIFY ||
1218 payload->get_type(payload) == PLV1_NOTIFY)
1219 {
1220 notify_payload_t *notify;
1221 notify_type_t type;
1222 chunk_t data;
1223
1224 notify = (notify_payload_t*)payload;
1225 type = notify->get_notify_type(notify);
1226 data = notify->get_notification_data(notify);
1227 if (type == MS_NOTIFY_STATUS && data.len == 4)
1228 {
1229 written = snprintf(pos, len, "(%N(%d))", notify_type_short_names,
1230 type, untoh32(data.ptr));
1231 }
1232 else
1233 {
1234 written = snprintf(pos, len, "(%N)", notify_type_short_names,
1235 type);
1236 }
1237 if (written >= len || written < 0)
1238 {
1239 return buf;
1240 }
1241 pos += written;
1242 len -= written;
1243 }
1244 if (payload->get_type(payload) == PLV2_EAP)
1245 {
1246 eap_payload_t *eap = (eap_payload_t*)payload;
1247 u_int32_t vendor;
1248 eap_type_t type;
1249 char method[64] = "";
1250
1251 type = eap->get_type(eap, &vendor);
1252 if (type)
1253 {
1254 if (vendor)
1255 {
1256 snprintf(method, sizeof(method), "/%d-%d", type, vendor);
1257 }
1258 else
1259 {
1260 snprintf(method, sizeof(method), "/%N",
1261 eap_type_short_names, type);
1262 }
1263 }
1264 written = snprintf(pos, len, "/%N%s", eap_code_short_names,
1265 eap->get_code(eap), method);
1266 if (written >= len || written < 0)
1267 {
1268 return buf;
1269 }
1270 pos += written;
1271 len -= written;
1272 }
1273 if (payload->get_type(payload) == PLV2_CONFIGURATION ||
1274 payload->get_type(payload) == PLV1_CONFIGURATION)
1275 {
1276 cp_payload_t *cp = (cp_payload_t*)payload;
1277 enumerator_t *attributes;
1278 configuration_attribute_t *attribute;
1279 bool first = TRUE;
1280 char *pfx;
1281
1282 switch (cp->get_type(cp))
1283 {
1284 case CFG_REQUEST:
1285 pfx = "RQ(";
1286 break;
1287 case CFG_REPLY:
1288 pfx = "RP(";
1289 break;
1290 case CFG_SET:
1291 pfx = "S(";
1292 break;
1293 case CFG_ACK:
1294 pfx = "A(";
1295 break;
1296 default:
1297 pfx = "(";
1298 break;
1299 }
1300
1301 attributes = cp->create_attribute_enumerator(cp);
1302 while (attributes->enumerate(attributes, &attribute))
1303 {
1304 written = snprintf(pos, len, "%s%N", first ? pfx : " ",
1305 configuration_attribute_type_short_names,
1306 attribute->get_type(attribute));
1307 if (written >= len || written < 0)
1308 {
1309 return buf;
1310 }
1311 pos += written;
1312 len -= written;
1313 first = FALSE;
1314 }
1315 attributes->destroy(attributes);
1316 if (!first)
1317 {
1318 written = snprintf(pos, len, ")");
1319 if (written >= len || written < 0)
1320 {
1321 return buf;
1322 }
1323 pos += written;
1324 len -= written;
1325 }
1326 }
1327 }
1328 enumerator->destroy(enumerator);
1329
1330 /* remove last space */
1331 snprintf(pos, len, " ]");
1332 return buf;
1333 }
1334
1335 /**
1336 * reorder payloads depending on reordering rules
1337 */
1338 static void order_payloads(private_message_t *this)
1339 {
1340 linked_list_t *list;
1341 payload_t *payload;
1342 int i;
1343
1344 /* move to temp list */
1345 list = linked_list_create();
1346 while (this->payloads->remove_last(this->payloads,
1347 (void**)&payload) == SUCCESS)
1348 {
1349 list->insert_first(list, payload);
1350 }
1351 /* for each rule, ... */
1352 for (i = 0; i < this->rule->order_count; i++)
1353 {
1354 enumerator_t *enumerator;
1355 notify_payload_t *notify;
1356 payload_order_t order;
1357
1358 order = this->rule->order[i];
1359
1360 /* ... find all payload ... */
1361 enumerator = list->create_enumerator(list);
1362 while (enumerator->enumerate(enumerator, &payload))
1363 {
1364 /* ... with that type ... */
1365 if (payload->get_type(payload) == order.type)
1366 {
1367 notify = (notify_payload_t*)payload;
1368
1369 /**... and check notify for type. */
1370 if (order.type != PLV2_NOTIFY || order.notify == 0 ||
1371 order.notify == notify->get_notify_type(notify))
1372 {
1373 list->remove_at(list, enumerator);
1374 add_payload(this, payload);
1375 }
1376 }
1377 }
1378 enumerator->destroy(enumerator);
1379 }
1380 /* append all payloads without a rule to the end */
1381 while (list->remove_last(list, (void**)&payload) == SUCCESS)
1382 {
1383 /* do not complain about payloads in private use space */
1384 if (payload->get_type(payload) < 128)
1385 {
1386 DBG1(DBG_ENC, "payload %N has no ordering rule in %N %s",
1387 payload_type_names, payload->get_type(payload),
1388 exchange_type_names, this->rule->exchange_type,
1389 this->rule->is_request ? "request" : "response");
1390 }
1391 add_payload(this, payload);
1392 }
1393 list->destroy(list);
1394 }
1395
1396 /**
1397 * Wrap payloads in an encrypted payload
1398 */
1399 static encrypted_payload_t* wrap_payloads(private_message_t *this)
1400 {
1401 encrypted_payload_t *encryption;
1402 linked_list_t *payloads;
1403 payload_t *current;
1404
1405 /* copy all payloads in a temporary list */
1406 payloads = linked_list_create();
1407 while (this->payloads->remove_first(this->payloads,
1408 (void**)&current) == SUCCESS)
1409 {
1410 payloads->insert_last(payloads, current);
1411 }
1412
1413 if (this->is_encrypted)
1414 {
1415 encryption = encrypted_payload_create(PLV1_ENCRYPTED);
1416 }
1417 else
1418 {
1419 encryption = encrypted_payload_create(PLV2_ENCRYPTED);
1420 }
1421 while (payloads->remove_first(payloads, (void**)&current) == SUCCESS)
1422 {
1423 payload_rule_t *rule;
1424 payload_type_t type;
1425 bool encrypt = TRUE;
1426
1427 type = current->get_type(current);
1428 rule = get_payload_rule(this, type);
1429 if (rule)
1430 {
1431 encrypt = rule->encrypted;
1432 }
1433 if (encrypt || this->is_encrypted)
1434 { /* encryption is forced for IKEv1 */
1435 DBG2(DBG_ENC, "insert payload %N into encrypted payload",
1436 payload_type_names, type);
1437 encryption->add_payload(encryption, current);
1438 }
1439 else
1440 {
1441 DBG2(DBG_ENC, "insert payload %N unencrypted",
1442 payload_type_names, type);
1443 add_payload(this, current);
1444 }
1445 }
1446 payloads->destroy(payloads);
1447
1448 return encryption;
1449 }
1450
1451 METHOD(message_t, disable_sort, void,
1452 private_message_t *this)
1453 {
1454 this->sort_disabled = TRUE;
1455 }
1456
1457 METHOD(message_t, generate, status_t,
1458 private_message_t *this, keymat_t *keymat, packet_t **packet)
1459 {
1460 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
1461 generator_t *generator;
1462 ike_header_t *ike_header;
1463 payload_t *payload, *next;
1464 encrypted_payload_t *encryption = NULL;
1465 payload_type_t next_type;
1466 enumerator_t *enumerator;
1467 aead_t *aead = NULL;
1468 chunk_t chunk, hash = chunk_empty;
1469 char str[BUF_LEN];
1470 u_int32_t *lenpos;
1471 bool encrypted = FALSE, *reserved;
1472 int i;
1473
1474 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
1475 {
1476 DBG1(DBG_ENC, "exchange type is not defined");
1477 return INVALID_STATE;
1478 }
1479
1480 if (this->packet->get_source(this->packet) == NULL ||
1481 this->packet->get_destination(this->packet) == NULL)
1482 {
1483 DBG1(DBG_ENC, "source/destination not defined");
1484 return INVALID_STATE;
1485 }
1486
1487 this->rule = get_message_rule(this);
1488 if (!this->rule)
1489 {
1490 DBG1(DBG_ENC, "no message rules specified for this message type");
1491 return NOT_SUPPORTED;
1492 }
1493
1494 if (!this->sort_disabled)
1495 {
1496 order_payloads(this);
1497 }
1498 if (keymat && keymat->get_version(keymat) == IKEV1)
1499 {
1500 /* get a hash for this message, if any is required */
1501 if (keymat_v1->get_hash_phase2(keymat_v1, &this->public, &hash))
1502 { /* insert a HASH payload as first payload */
1503 hash_payload_t *hash_payload;
1504
1505 hash_payload = hash_payload_create(PLV1_HASH);
1506 hash_payload->set_hash(hash_payload, hash);
1507 this->payloads->insert_first(this->payloads, hash_payload);
1508 if (this->exchange_type == INFORMATIONAL_V1)
1509 {
1510 this->is_encrypted = encrypted = TRUE;
1511 }
1512 chunk_free(&hash);
1513 }
1514 }
1515 if (this->major_version == IKEV2_MAJOR_VERSION)
1516 {
1517 encrypted = this->rule->encrypted;
1518 }
1519 else if (!encrypted)
1520 {
1521 /* If at least one payload requires encryption, encrypt the message.
1522 * If no key material is available, the flag will be reset below. */
1523 enumerator = this->payloads->create_enumerator(this->payloads);
1524 while (enumerator->enumerate(enumerator, (void**)&payload))
1525 {
1526 payload_rule_t *rule;
1527
1528 rule = get_payload_rule(this, payload->get_type(payload));
1529 if (rule && rule->encrypted)
1530 {
1531 this->is_encrypted = encrypted = TRUE;
1532 break;
1533 }
1534 }
1535 enumerator->destroy(enumerator);
1536 }
1537
1538 DBG1(DBG_ENC, "generating %s", get_string(this, str, sizeof(str)));
1539
1540 if (keymat)
1541 {
1542 aead = keymat->get_aead(keymat, FALSE);
1543 }
1544 if (aead && encrypted)
1545 {
1546 encryption = wrap_payloads(this);
1547 }
1548 else
1549 {
1550 DBG2(DBG_ENC, "not encrypting payloads");
1551 this->is_encrypted = FALSE;
1552 }
1553
1554 ike_header = ike_header_create_version(this->major_version,
1555 this->minor_version);
1556 ike_header->set_exchange_type(ike_header, this->exchange_type);
1557 ike_header->set_message_id(ike_header, this->message_id);
1558 if (this->major_version == IKEV2_MAJOR_VERSION)
1559 {
1560 ike_header->set_response_flag(ike_header, !this->is_request);
1561 ike_header->set_version_flag(ike_header, this->version_flag);
1562 ike_header->set_initiator_flag(ike_header,
1563 this->ike_sa_id->is_initiator(this->ike_sa_id));
1564 }
1565 else
1566 {
1567 ike_header->set_encryption_flag(ike_header, this->is_encrypted);
1568 }
1569 ike_header->set_initiator_spi(ike_header,
1570 this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
1571 ike_header->set_responder_spi(ike_header,
1572 this->ike_sa_id->get_responder_spi(this->ike_sa_id));
1573
1574 for (i = 0; i < countof(this->reserved); i++)
1575 {
1576 reserved = payload_get_field(&ike_header->payload_interface,
1577 RESERVED_BIT, i);
1578 if (reserved)
1579 {
1580 *reserved = this->reserved[i];
1581 }
1582 }
1583
1584 generator = generator_create();
1585
1586 /* generate all payloads with proper next type */
1587 payload = (payload_t*)ike_header;
1588 enumerator = create_payload_enumerator(this);
1589 while (enumerator->enumerate(enumerator, &next))
1590 {
1591 payload->set_next_type(payload, next->get_type(next));
1592 generator->generate_payload(generator, payload);
1593 payload = next;
1594 }
1595 enumerator->destroy(enumerator);
1596 if (this->is_encrypted)
1597 { /* for encrypted IKEv1 messages */
1598 next_type = encryption->payload_interface.get_next_type(
1599 (payload_t*)encryption);
1600 }
1601 else
1602 {
1603 next_type = encryption ? PLV2_ENCRYPTED : PL_NONE;
1604 }
1605 payload->set_next_type(payload, next_type);
1606 generator->generate_payload(generator, payload);
1607 ike_header->destroy(ike_header);
1608
1609 if (encryption)
1610 { /* set_transform() has to be called before get_length() */
1611 encryption->set_transform(encryption, aead);
1612 if (this->is_encrypted)
1613 { /* for IKEv1 instead of associated data we provide the IV */
1614 if (!keymat_v1->get_iv(keymat_v1, this->message_id, &chunk))
1615 {
1616 generator->destroy(generator);
1617 return FAILED;
1618 }
1619 }
1620 else
1621 { /* build associated data (without header of encryption payload) */
1622 chunk = generator->get_chunk(generator, &lenpos);
1623 /* fill in length, including encryption payload */
1624 htoun32(lenpos, chunk.len + encryption->get_length(encryption));
1625 }
1626 this->payloads->insert_last(this->payloads, encryption);
1627 if (encryption->encrypt(encryption, this->message_id, chunk) != SUCCESS)
1628 {
1629 generator->destroy(generator);
1630 return INVALID_STATE;
1631 }
1632 generator->generate_payload(generator, &encryption->payload_interface);
1633 }
1634 chunk = generator->get_chunk(generator, &lenpos);
1635 htoun32(lenpos, chunk.len);
1636 this->packet->set_data(this->packet, chunk_clone(chunk));
1637 if (this->is_encrypted && this->exchange_type != INFORMATIONAL_V1)
1638 {
1639 /* update the IV for the next IKEv1 message */
1640 chunk_t last_block;
1641 size_t bs;
1642
1643 bs = aead->get_block_size(aead);
1644 last_block = chunk_create(chunk.ptr + chunk.len - bs, bs);
1645 if (!keymat_v1->update_iv(keymat_v1, this->message_id, last_block) ||
1646 !keymat_v1->confirm_iv(keymat_v1, this->message_id))
1647 {
1648 generator->destroy(generator);
1649 return FAILED;
1650 }
1651 }
1652 generator->destroy(generator);
1653 *packet = this->packet->clone(this->packet);
1654 return SUCCESS;
1655 }
1656
1657 /**
1658 * Creates a (basic) clone of the given message
1659 */
1660 static message_t *clone_message(private_message_t *this)
1661 {
1662 message_t *message;
1663 host_t *src, *dst;
1664
1665 src = this->packet->get_source(this->packet);
1666 dst = this->packet->get_destination(this->packet);
1667
1668 message = message_create(this->major_version, this->minor_version);
1669 message->set_message_id(message, this->message_id);
1670 message->set_request(message, this->is_request);
1671 message->set_source(message, src->clone(src));
1672 message->set_destination(message, dst->clone(dst));
1673 message->set_exchange_type(message, this->exchange_type);
1674 return message;
1675 }
1676
1677 /**
1678 * Create a single fragment with the given data
1679 */
1680 static message_t *create_fragment(private_message_t *this, u_int8_t num,
1681 bool last, chunk_t data)
1682 {
1683 fragment_payload_t *fragment;
1684 message_t *message;
1685
1686 fragment = fragment_payload_create_from_data(num, last, data);
1687 message = clone_message(this);
1688 message->add_payload(message, (payload_t*)fragment);
1689 return message;
1690 }
1691
1692 /**
1693 * Destroy all messages in the given array
1694 */
1695 CALLBACK(destroy_fragments, void,
1696 array_t *fragments)
1697 {
1698 array_destroy_offset(fragments, offsetof(message_t, destroy));
1699 }
1700
1701 METHOD(message_t, fragment, status_t,
1702 private_message_t *this, size_t frag_len, enumerator_t **fragments)
1703 {
1704 array_t *messages;
1705 message_t *fragment;
1706 u_int8_t num, count;
1707 host_t *src, *dst;
1708 chunk_t data;
1709 size_t len;
1710
1711 if (!is_encoded(this) || this->major_version == IKEV2_MAJOR_VERSION)
1712 {
1713 return INVALID_STATE;
1714 }
1715
1716 src = this->packet->get_source(this->packet);
1717 dst = this->packet->get_destination(this->packet);
1718 if (!frag_len)
1719 {
1720 frag_len = (src->get_family(src) == AF_INET) ? 576 : 1280;
1721 }
1722 /* frag_len is the complete IP datagram length, account for overhead (we
1723 * assume no IP options/extension headers are used) */
1724 frag_len -= (src->get_family(src) == AF_INET) ? 20 : 40;
1725 /* 8 (UDP header) + 28 (IKE header) */
1726 frag_len -= 36;
1727 if (dst->get_port(dst) != IKEV2_UDP_PORT &&
1728 src->get_port(src) != IKEV2_UDP_PORT)
1729 { /* reduce length due to non-ESP marker */
1730 frag_len -= 4;
1731 }
1732
1733 data = this->packet->get_data(this->packet);
1734 if (data.len <= frag_len)
1735 {
1736 return ALREADY_DONE;
1737 }
1738 /* overhead for the fragmentation payload header */
1739 frag_len -= 8;
1740
1741 count = data.len / frag_len + (data.len % frag_len ? 1 : 0);
1742 messages = array_create(0, count);
1743 DBG2(DBG_ENC, "splitting IKE message with length of %zu bytes into "
1744 "%hhu fragments", data.len, count);
1745 for (num = 1; num <= count; num++)
1746 {
1747 len = min(data.len, frag_len);
1748 fragment = create_fragment(this, num, num == count,
1749 chunk_create(data.ptr, len));
1750 array_insert(messages, ARRAY_TAIL, fragment);
1751 data = chunk_skip(data, len);
1752 }
1753 *fragments = enumerator_create_cleaner(array_create_enumerator(messages),
1754 destroy_fragments, messages);
1755 return SUCCESS;
1756 }
1757
1758 METHOD(message_t, get_packet, packet_t*,
1759 private_message_t *this)
1760 {
1761 if (this->packet == NULL)
1762 {
1763 return NULL;
1764 }
1765 return this->packet->clone(this->packet);
1766 }
1767
1768 METHOD(message_t, get_packet_data, chunk_t,
1769 private_message_t *this)
1770 {
1771 if (this->packet == NULL)
1772 {
1773 return chunk_empty;
1774 }
1775 return this->packet->get_data(this->packet);
1776 }
1777
1778 METHOD(message_t, parse_header, status_t,
1779 private_message_t *this)
1780 {
1781 ike_header_t *ike_header;
1782 status_t status;
1783 bool *reserved;
1784 int i;
1785
1786 DBG2(DBG_ENC, "parsing header of message");
1787
1788 this->parser->reset_context(this->parser);
1789 status = this->parser->parse_payload(this->parser, PL_HEADER,
1790 (payload_t**)&ike_header);
1791 if (status != SUCCESS)
1792 {
1793 DBG1(DBG_ENC, "header could not be parsed");
1794 return status;
1795
1796 }
1797
1798 status = ike_header->payload_interface.verify(
1799 &ike_header->payload_interface);
1800 if (status != SUCCESS)
1801 {
1802 DBG1(DBG_ENC, "header verification failed");
1803 ike_header->destroy(ike_header);
1804 return status;
1805 }
1806
1807 DESTROY_IF(this->ike_sa_id);
1808 this->ike_sa_id = ike_sa_id_create(
1809 ike_header->get_maj_version(ike_header),
1810 ike_header->get_initiator_spi(ike_header),
1811 ike_header->get_responder_spi(ike_header),
1812 ike_header->get_initiator_flag(ike_header));
1813
1814 this->exchange_type = ike_header->get_exchange_type(ike_header);
1815 this->message_id = ike_header->get_message_id(ike_header);
1816 this->major_version = ike_header->get_maj_version(ike_header);
1817 this->minor_version = ike_header->get_min_version(ike_header);
1818 if (this->major_version == IKEV2_MAJOR_VERSION)
1819 {
1820 this->is_request = !ike_header->get_response_flag(ike_header);
1821 }
1822 else
1823 {
1824 this->is_encrypted = ike_header->get_encryption_flag(ike_header);
1825 }
1826 this->first_payload = ike_header->payload_interface.get_next_type(
1827 &ike_header->payload_interface);
1828 if (this->first_payload == PLV1_FRAGMENT && this->is_encrypted)
1829 { /* racoon sets the encryted bit when sending a fragment, but these
1830 * messages are really not encrypted */
1831 this->is_encrypted = FALSE;
1832 }
1833
1834 for (i = 0; i < countof(this->reserved); i++)
1835 {
1836 reserved = payload_get_field(&ike_header->payload_interface,
1837 RESERVED_BIT, i);
1838 if (reserved)
1839 {
1840 this->reserved[i] = *reserved;
1841 }
1842 }
1843 ike_header->destroy(ike_header);
1844
1845 DBG2(DBG_ENC, "parsed a %N %s header", exchange_type_names,
1846 this->exchange_type, this->major_version == IKEV1_MAJOR_VERSION ?
1847 "message" : (this->is_request ? "request" : "response"));
1848 return SUCCESS;
1849 }
1850
1851 /**
1852 * Check if a payload is for a mediation extension connectivity check
1853 */
1854 static bool is_connectivity_check(private_message_t *this, payload_t *payload)
1855 {
1856 #ifdef ME
1857 if (this->exchange_type == INFORMATIONAL &&
1858 payload->get_type(payload) == PLV2_NOTIFY)
1859 {
1860 notify_payload_t *notify = (notify_payload_t*)payload;
1861
1862 switch (notify->get_notify_type(notify))
1863 {
1864 case ME_CONNECTID:
1865 case ME_ENDPOINT:
1866 case ME_CONNECTAUTH:
1867 return TRUE;
1868 default:
1869 break;
1870 }
1871 }
1872 #endif /* !ME */
1873 return FALSE;
1874 }
1875
1876 /**
1877 * Parses and verifies the unencrypted payloads contained in the message
1878 */
1879 static status_t parse_payloads(private_message_t *this)
1880 {
1881 payload_type_t type = this->first_payload;
1882 payload_t *payload;
1883 status_t status;
1884
1885 if (this->is_encrypted)
1886 { /* wrap the whole encrypted IKEv1 message in a special encrypted
1887 * payload which is then handled just like a regular payload */
1888 encrypted_payload_t *encryption;
1889
1890 status = this->parser->parse_payload(this->parser, PLV1_ENCRYPTED,
1891 (payload_t**)&encryption);
1892 if (status != SUCCESS)
1893 {
1894 DBG1(DBG_ENC, "failed to wrap encrypted IKEv1 message");
1895 return PARSE_ERROR;
1896 }
1897 encryption->payload_interface.set_next_type((payload_t*)encryption,
1898 this->first_payload);
1899 this->payloads->insert_last(this->payloads, encryption);
1900 return SUCCESS;
1901 }
1902
1903 while (type != PL_NONE)
1904 {
1905 DBG2(DBG_ENC, "starting parsing a %N payload",
1906 payload_type_names, type);
1907
1908 status = this->parser->parse_payload(this->parser, type, &payload);
1909 if (status != SUCCESS)
1910 {
1911 DBG1(DBG_ENC, "payload type %N could not be parsed",
1912 payload_type_names, type);
1913 return PARSE_ERROR;
1914 }
1915
1916 DBG2(DBG_ENC, "verifying payload of type %N", payload_type_names, type);
1917 status = payload->verify(payload);
1918 if (status != SUCCESS)
1919 {
1920 DBG1(DBG_ENC, "%N payload verification failed",
1921 payload_type_names, type);
1922 payload->destroy(payload);
1923 return VERIFY_ERROR;
1924 }
1925
1926 DBG2(DBG_ENC, "%N payload verified, adding to payload list",
1927 payload_type_names, type);
1928 this->payloads->insert_last(this->payloads, payload);
1929
1930 /* an encrypted payload is the last one, so STOP here. decryption is
1931 * done later */
1932 if (type == PLV2_ENCRYPTED)
1933 {
1934 DBG2(DBG_ENC, "%N payload found, stop parsing",
1935 payload_type_names, type);
1936 break;
1937 }
1938 type = payload->get_next_type(payload);
1939 }
1940 return SUCCESS;
1941 }
1942
1943 /**
1944 * Decrypt an encrypted payload and extract all contained payloads.
1945 */
1946 static status_t decrypt_and_extract(private_message_t *this, keymat_t *keymat,
1947 payload_t *previous, encrypted_payload_t *encryption)
1948 {
1949 payload_t *encrypted;
1950 payload_type_t type;
1951 chunk_t chunk;
1952 aead_t *aead;
1953 size_t bs;
1954 status_t status = SUCCESS;
1955
1956 if (!keymat)
1957 {
1958 DBG1(DBG_ENC, "found encrypted payload, but no keymat");
1959 return INVALID_ARG;
1960 }
1961 aead = keymat->get_aead(keymat, TRUE);
1962 if (!aead)
1963 {
1964 DBG1(DBG_ENC, "found encrypted payload, but no transform set");
1965 return INVALID_ARG;
1966 }
1967 bs = aead->get_block_size(aead);
1968 encryption->set_transform(encryption, aead);
1969 chunk = this->packet->get_data(this->packet);
1970 if (chunk.len < encryption->get_length(encryption) ||
1971 chunk.len < bs)
1972 {
1973 DBG1(DBG_ENC, "invalid payload length");
1974 return VERIFY_ERROR;
1975 }
1976 if (keymat->get_version(keymat) == IKEV1)
1977 { /* instead of associated data we provide the IV, we also update
1978 * the IV with the last encrypted block */
1979 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
1980 chunk_t iv;
1981
1982 if (keymat_v1->get_iv(keymat_v1, this->message_id, &iv))
1983 {
1984 status = encryption->decrypt(encryption, iv);
1985 if (status == SUCCESS)
1986 {
1987 if (!keymat_v1->update_iv(keymat_v1, this->message_id,
1988 chunk_create(chunk.ptr + chunk.len - bs, bs)))
1989 {
1990 status = FAILED;
1991 }
1992 }
1993 }
1994 else
1995 {
1996 status = FAILED;
1997 }
1998 }
1999 else
2000 {
2001 chunk.len -= encryption->get_length(encryption);
2002 status = encryption->decrypt(encryption, chunk);
2003 }
2004 if (status != SUCCESS)
2005 {
2006 return status;
2007 }
2008
2009 while ((encrypted = encryption->remove_payload(encryption)))
2010 {
2011 type = encrypted->get_type(encrypted);
2012 if (previous)
2013 {
2014 previous->set_next_type(previous, type);
2015 }
2016 else
2017 {
2018 this->first_payload = type;
2019 }
2020 DBG2(DBG_ENC, "insert decrypted payload of type %N at end of list",
2021 payload_type_names, type);
2022 this->payloads->insert_last(this->payloads, encrypted);
2023 previous = encrypted;
2024 }
2025 return SUCCESS;
2026 }
2027
2028 /**
2029 * Do we accept unencrypted ID/HASH payloads in Main Mode, as seen from
2030 * some SonicWall boxes?
2031 */
2032 static bool accept_unencrypted_mm(private_message_t *this, payload_type_t type)
2033 {
2034 if (this->exchange_type == ID_PROT)
2035 {
2036 if (type == PLV1_ID || type == PLV1_HASH)
2037 {
2038 return lib->settings->get_bool(lib->settings,
2039 "%s.accept_unencrypted_mainmode_messages",
2040 FALSE, lib->ns);
2041 }
2042 }
2043 return FALSE;
2044 }
2045
2046 /**
2047 * Decrypt payload from the encrypted payload
2048 */
2049 static status_t decrypt_payloads(private_message_t *this, keymat_t *keymat)
2050 {
2051 payload_t *payload, *previous = NULL;
2052 enumerator_t *enumerator;
2053 payload_rule_t *rule;
2054 payload_type_t type;
2055 status_t status = SUCCESS;
2056 bool was_encrypted = FALSE;
2057
2058 enumerator = this->payloads->create_enumerator(this->payloads);
2059 while (enumerator->enumerate(enumerator, &payload))
2060 {
2061 type = payload->get_type(payload);
2062
2063 DBG2(DBG_ENC, "process payload of type %N", payload_type_names, type);
2064
2065 if (type == PLV2_ENCRYPTED || type == PLV1_ENCRYPTED)
2066 {
2067 encrypted_payload_t *encryption;
2068
2069 if (was_encrypted)
2070 {
2071 DBG1(DBG_ENC, "encrypted payload can't contain other payloads "
2072 "of type %N", payload_type_names, type);
2073 status = VERIFY_ERROR;
2074 break;
2075 }
2076
2077 DBG2(DBG_ENC, "found an encrypted payload");
2078 encryption = (encrypted_payload_t*)payload;
2079 this->payloads->remove_at(this->payloads, enumerator);
2080
2081 if (enumerator->enumerate(enumerator, NULL))
2082 {
2083 DBG1(DBG_ENC, "encrypted payload is not last payload");
2084 encryption->destroy(encryption);
2085 status = VERIFY_ERROR;
2086 break;
2087 }
2088 status = decrypt_and_extract(this, keymat, previous, encryption);
2089 encryption->destroy(encryption);
2090 if (status != SUCCESS)
2091 {
2092 break;
2093 }
2094 was_encrypted = TRUE;
2095 }
2096
2097 if (payload_is_known(type) && !was_encrypted &&
2098 !is_connectivity_check(this, payload) &&
2099 this->exchange_type != AGGRESSIVE)
2100 {
2101 rule = get_payload_rule(this, type);
2102 if ((!rule || rule->encrypted) &&
2103 !accept_unencrypted_mm(this, type))
2104 {
2105 DBG1(DBG_ENC, "payload type %N was not encrypted",
2106 payload_type_names, type);
2107 status = FAILED;
2108 break;
2109 }
2110 }
2111 previous = payload;
2112 }
2113 enumerator->destroy(enumerator);
2114 return status;
2115 }
2116
2117 /**
2118 * Verify a message and all payload according to message/payload rules
2119 */
2120 static status_t verify(private_message_t *this)
2121 {
2122 bool complete = FALSE;
2123 int i;
2124
2125 DBG2(DBG_ENC, "verifying message structure");
2126
2127 /* check for payloads with wrong count */
2128 for (i = 0; i < this->rule->rule_count; i++)
2129 {
2130 enumerator_t *enumerator;
2131 payload_t *payload;
2132 payload_rule_t *rule;
2133 int found = 0;
2134
2135 rule = &this->rule->rules[i];
2136 enumerator = create_payload_enumerator(this);
2137 while (enumerator->enumerate(enumerator, &payload))
2138 {
2139 payload_type_t type;
2140
2141 type = payload->get_type(payload);
2142 if (type == rule->type)
2143 {
2144 found++;
2145 DBG2(DBG_ENC, "found payload of type %N",
2146 payload_type_names, type);
2147 if (found > rule->max_occurence)
2148 {
2149 DBG1(DBG_ENC, "payload of type %N more than %d times (%d) "
2150 "occurred in current message", payload_type_names,
2151 type, rule->max_occurence, found);
2152 enumerator->destroy(enumerator);
2153 return VERIFY_ERROR;
2154 }
2155 }
2156 }
2157 enumerator->destroy(enumerator);
2158
2159 if (!complete && found < rule->min_occurence)
2160 {
2161 DBG1(DBG_ENC, "payload of type %N not occurred %d times (%d)",
2162 payload_type_names, rule->type, rule->min_occurence, found);
2163 return VERIFY_ERROR;
2164 }
2165 if (found && rule->sufficient)
2166 {
2167 complete = TRUE;
2168 }
2169 }
2170 return SUCCESS;
2171 }
2172
2173 METHOD(message_t, parse_body, status_t,
2174 private_message_t *this, keymat_t *keymat)
2175 {
2176 status_t status = SUCCESS;
2177 char str[BUF_LEN];
2178
2179 DBG2(DBG_ENC, "parsing body of message, first payload is %N",
2180 payload_type_names, this->first_payload);
2181
2182 this->rule = get_message_rule(this);
2183 if (!this->rule)
2184 {
2185 DBG1(DBG_ENC, "no message rules specified for a %N %s",
2186 exchange_type_names, this->exchange_type,
2187 this->is_request ? "request" : "response");
2188 return NOT_SUPPORTED;
2189 }
2190
2191 status = parse_payloads(this);
2192 if (status != SUCCESS)
2193 { /* error is already logged */
2194 return status;
2195 }
2196
2197 status = decrypt_payloads(this, keymat);
2198 if (status != SUCCESS)
2199 {
2200 DBG1(DBG_ENC, "could not decrypt payloads");
2201 return status;
2202 }
2203
2204 status = verify(this);
2205 if (status != SUCCESS)
2206 {
2207 return status;
2208 }
2209
2210 DBG1(DBG_ENC, "parsed %s", get_string(this, str, sizeof(str)));
2211
2212 if (keymat && keymat->get_version(keymat) == IKEV1)
2213 {
2214 keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
2215 chunk_t hash;
2216
2217 if (keymat_v1->get_hash_phase2(keymat_v1, &this->public, &hash))
2218 {
2219 hash_payload_t *hash_payload;
2220 chunk_t other_hash;
2221
2222 if (this->first_payload != PLV1_HASH)
2223 {
2224 if (this->exchange_type == INFORMATIONAL_V1)
2225 {
2226 DBG1(DBG_ENC, "ignoring unprotected INFORMATIONAL from %H",
2227 this->packet->get_source(this->packet));
2228 }
2229 else
2230 {
2231 DBG1(DBG_ENC, "expected HASH payload as first payload");
2232 }
2233 chunk_free(&hash);
2234 return VERIFY_ERROR;
2235 }
2236 hash_payload = (hash_payload_t*)get_payload(this, PLV1_HASH);
2237 other_hash = hash_payload->get_hash(hash_payload);
2238 DBG3(DBG_ENC, "HASH received %B\nHASH expected %B",
2239 &other_hash, &hash);
2240 if (!chunk_equals(hash, other_hash))
2241 {
2242 DBG1(DBG_ENC, "received HASH payload does not match");
2243 chunk_free(&hash);
2244 return FAILED;
2245 }
2246 chunk_free(&hash);
2247 }
2248 if (this->is_encrypted && this->exchange_type != INFORMATIONAL_V1)
2249 { /* message verified, confirm IV */
2250 if (!keymat_v1->confirm_iv(keymat_v1, this->message_id))
2251 {
2252 return FAILED;
2253 }
2254 }
2255 }
2256 return SUCCESS;
2257 }
2258
2259 METHOD(message_t, destroy, void,
2260 private_message_t *this)
2261 {
2262 DESTROY_IF(this->ike_sa_id);
2263 this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
2264 this->packet->destroy(this->packet);
2265 this->parser->destroy(this->parser);
2266 free(this);
2267 }
2268
2269 /*
2270 * Described in header.
2271 */
2272 message_t *message_create_from_packet(packet_t *packet)
2273 {
2274 private_message_t *this;
2275
2276 INIT(this,
2277 .public = {
2278 .set_major_version = _set_major_version,
2279 .get_major_version = _get_major_version,
2280 .set_minor_version = _set_minor_version,
2281 .get_minor_version = _get_minor_version,
2282 .set_message_id = _set_message_id,
2283 .get_message_id = _get_message_id,
2284 .get_initiator_spi = _get_initiator_spi,
2285 .get_responder_spi = _get_responder_spi,
2286 .set_ike_sa_id = _set_ike_sa_id,
2287 .get_ike_sa_id = _get_ike_sa_id,
2288 .set_exchange_type = _set_exchange_type,
2289 .get_exchange_type = _get_exchange_type,
2290 .get_first_payload_type = _get_first_payload_type,
2291 .set_request = _set_request,
2292 .get_request = _get_request,
2293 .set_version_flag = _set_version_flag,
2294 .get_reserved_header_bit = _get_reserved_header_bit,
2295 .set_reserved_header_bit = _set_reserved_header_bit,
2296 .add_payload = _add_payload,
2297 .add_notify = _add_notify,
2298 .disable_sort = _disable_sort,
2299 .generate = _generate,
2300 .is_encoded = _is_encoded,
2301 .fragment = _fragment,
2302 .set_source = _set_source,
2303 .get_source = _get_source,
2304 .set_destination = _set_destination,
2305 .get_destination = _get_destination,
2306 .create_payload_enumerator = _create_payload_enumerator,
2307 .remove_payload_at = _remove_payload_at,
2308 .get_payload = _get_payload,
2309 .get_notify = _get_notify,
2310 .parse_header = _parse_header,
2311 .parse_body = _parse_body,
2312 .get_packet = _get_packet,
2313 .get_packet_data = _get_packet_data,
2314 .destroy = _destroy,
2315 },
2316 .exchange_type = EXCHANGE_TYPE_UNDEFINED,
2317 .is_request = TRUE,
2318 .first_payload = PL_NONE,
2319 .packet = packet,
2320 .payloads = linked_list_create(),
2321 .parser = parser_create(packet->get_data(packet)),
2322 );
2323
2324 return &this->public;
2325 }
2326
2327 /*
2328 * Described in header.
2329 */
2330 message_t *message_create(int major, int minor)
2331 {
2332 message_t *this = message_create_from_packet(packet_create());
2333
2334 this->set_major_version(this, major);
2335 this->set_minor_version(this, minor);
2336
2337 return this;
2338 }