allow multiple DELETE payloads in an informational message
[strongswan.git] / src / charon / encoding / message.c
1 /*
2 * Copyright (C) 2006-2007 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2006 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * $Id$
19 */
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "message.h"
25
26 #include <library.h>
27 #include <daemon.h>
28 #include <sa/ike_sa_id.h>
29 #include <encoding/generator.h>
30 #include <encoding/parser.h>
31 #include <utils/linked_list.h>
32 #include <encoding/payloads/encodings.h>
33 #include <encoding/payloads/payload.h>
34 #include <encoding/payloads/encryption_payload.h>
35 #include <encoding/payloads/unknown_payload.h>
36
37 /**
38 * Max number of notify payloads per IKEv2 Message
39 */
40 #define MAX_NOTIFY_PAYLOADS 20
41
42 /**
43 * Max number of delete payloads per IKEv2 Message
44 */
45 #define MAX_DELETE_PAYLOADS 20
46
47
48 typedef struct payload_rule_t payload_rule_t;
49
50 /**
51 * A payload rule defines the rules for a payload
52 * in a specific message rule. It defines if and how
53 * many times a payload must/can occur in a message
54 * and if it must be encrypted.
55 */
56 struct payload_rule_t {
57 /**
58 * Payload type.
59 */
60 payload_type_t payload_type;
61
62 /**
63 * Minimal occurence of this payload.
64 */
65 size_t min_occurence;
66
67 /**
68 * Max occurence of this payload.
69 */
70 size_t max_occurence;
71
72 /**
73 * TRUE if payload must be encrypted
74 */
75 bool encrypted;
76
77 /**
78 * If this payload occurs, the message rule is
79 * fullfilled in any case. This applies e.g. to
80 * notify_payloads.
81 */
82 bool sufficient;
83 };
84
85 typedef struct payload_order_t payload_order_t;
86
87 /**
88 * payload ordering structure allows us to reorder payloads according to RFC.
89 */
90 struct payload_order_t {
91
92 /**
93 * payload type
94 */
95 payload_type_t type;
96
97 /**
98 * notify type, if payload == NOTIFY
99 */
100 notify_type_t notify;
101 };
102
103
104 typedef struct message_rule_t message_rule_t;
105
106 /**
107 * A message rule defines the kind of a message,
108 * if it has encrypted contents and a list
109 * of payload ordering rules and payload parsing rules.
110 */
111 struct message_rule_t {
112 /**
113 * Type of message.
114 */
115 exchange_type_t exchange_type;
116
117 /**
118 * Is message a request or response.
119 */
120 bool is_request;
121
122 /**
123 * Message contains encrypted content.
124 */
125 bool encrypted_content;
126
127 /**
128 * Number of payload rules which will follow
129 */
130 int payload_rule_count;
131
132 /**
133 * Pointer to first payload rule
134 */
135 payload_rule_t *payload_rules;
136
137 /**
138 * Number of payload order rules
139 */
140 int payload_order_count;
141
142 /**
143 * payload ordering rules
144 */
145 payload_order_t *payload_order;
146 };
147
148 /**
149 * Message rule for IKE_SA_INIT from initiator.
150 */
151 static payload_rule_t ike_sa_init_i_payload_rules[] = {
152 /* payload type min max encr suff */
153 {NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, FALSE},
154 {SECURITY_ASSOCIATION, 1, 1, FALSE, FALSE},
155 {KEY_EXCHANGE, 1, 1, FALSE, FALSE},
156 {NONCE, 1, 1, FALSE, FALSE},
157 {VENDOR_ID, 0, 10, FALSE, FALSE},
158 };
159
160 /**
161 * payload order for IKE_SA_INIT initiator
162 */
163 static payload_order_t ike_sa_init_i_payload_order[] = {
164 /* payload type notify type */
165 {NOTIFY, COOKIE},
166 {SECURITY_ASSOCIATION, 0},
167 {KEY_EXCHANGE, 0},
168 {NONCE, 0},
169 {NOTIFY, NAT_DETECTION_SOURCE_IP},
170 {NOTIFY, NAT_DETECTION_DESTINATION_IP},
171 {NOTIFY, 0},
172 {VENDOR_ID, 0},
173 };
174
175 /**
176 * Message rule for IKE_SA_INIT from responder.
177 */
178 static payload_rule_t ike_sa_init_r_payload_rules[] = {
179 /* payload type min max encr suff */
180 {NOTIFY, 0, MAX_NOTIFY_PAYLOADS, FALSE, TRUE},
181 {SECURITY_ASSOCIATION, 1, 1, FALSE, FALSE},
182 {KEY_EXCHANGE, 1, 1, FALSE, FALSE},
183 {NONCE, 1, 1, FALSE, FALSE},
184 {VENDOR_ID, 0, 10, FALSE, FALSE},
185 };
186
187 /**
188 * payload order for IKE_SA_INIT responder
189 */
190 static payload_order_t ike_sa_init_r_payload_order[] = {
191 /* payload type notify type */
192 {SECURITY_ASSOCIATION, 0},
193 {KEY_EXCHANGE, 0},
194 {NONCE, 0},
195 {NOTIFY, NAT_DETECTION_SOURCE_IP},
196 {NOTIFY, NAT_DETECTION_DESTINATION_IP},
197 {NOTIFY, HTTP_CERT_LOOKUP_SUPPORTED},
198 {CERTIFICATE_REQUEST, 0},
199 {NOTIFY, 0},
200 {VENDOR_ID, 0},
201 };
202
203 /**
204 * Message rule for IKE_AUTH from initiator.
205 */
206 static payload_rule_t ike_auth_i_payload_rules[] = {
207 /* payload type min max encr suff */
208 {NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
209 {EXTENSIBLE_AUTHENTICATION, 0, 1, TRUE, TRUE},
210 {AUTHENTICATION, 0, 1, TRUE, TRUE},
211 {ID_INITIATOR, 1, 1, TRUE, FALSE},
212 {CERTIFICATE, 0, 4, TRUE, FALSE},
213 {CERTIFICATE_REQUEST, 0, 1, TRUE, FALSE},
214 {ID_RESPONDER, 0, 1, TRUE, FALSE},
215 #ifdef ME
216 {SECURITY_ASSOCIATION, 0, 1, TRUE, FALSE},
217 {TRAFFIC_SELECTOR_INITIATOR, 0, 1, TRUE, FALSE},
218 {TRAFFIC_SELECTOR_RESPONDER, 0, 1, TRUE, FALSE},
219 #else
220 {SECURITY_ASSOCIATION, 1, 1, TRUE, FALSE},
221 {TRAFFIC_SELECTOR_INITIATOR, 1, 1, TRUE, FALSE},
222 {TRAFFIC_SELECTOR_RESPONDER, 1, 1, TRUE, FALSE},
223 #endif /* ME */
224 {CONFIGURATION, 0, 1, TRUE, FALSE},
225 {VENDOR_ID, 0, 10, TRUE, FALSE},
226 };
227
228 /**
229 * payload order for IKE_AUTH initiator
230 */
231 static payload_order_t ike_auth_i_payload_order[] = {
232 /* payload type notify type */
233 {ID_INITIATOR, 0},
234 {CERTIFICATE, 0},
235 {NOTIFY, INITIAL_CONTACT},
236 {NOTIFY, HTTP_CERT_LOOKUP_SUPPORTED},
237 {CERTIFICATE_REQUEST, 0},
238 {ID_RESPONDER, 0},
239 {AUTHENTICATION, 0},
240 {EXTENSIBLE_AUTHENTICATION, 0},
241 {CONFIGURATION, 0},
242 {NOTIFY, IPCOMP_SUPPORTED},
243 {NOTIFY, USE_TRANSPORT_MODE},
244 {NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
245 {NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
246 {SECURITY_ASSOCIATION, 0},
247 {TRAFFIC_SELECTOR_INITIATOR, 0},
248 {TRAFFIC_SELECTOR_RESPONDER, 0},
249 {NOTIFY, MOBIKE_SUPPORTED},
250 {NOTIFY, ADDITIONAL_IP4_ADDRESS},
251 {NOTIFY, ADDITIONAL_IP6_ADDRESS},
252 {NOTIFY, NO_ADDITIONAL_ADDRESSES},
253 {NOTIFY, 0},
254 {VENDOR_ID, 0},
255 };
256
257 /**
258 * Message rule for IKE_AUTH from responder.
259 */
260 static payload_rule_t ike_auth_r_payload_rules[] = {
261 /* payload type min max encr suff */
262 {NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
263 {EXTENSIBLE_AUTHENTICATION, 0, 1, TRUE, TRUE},
264 {CERTIFICATE, 0, 4, TRUE, FALSE},
265 {ID_RESPONDER, 0, 1, TRUE, FALSE},
266 {AUTHENTICATION, 0, 1, TRUE, FALSE},
267 {SECURITY_ASSOCIATION, 0, 1, TRUE, FALSE},
268 {TRAFFIC_SELECTOR_INITIATOR, 0, 1, TRUE, FALSE},
269 {TRAFFIC_SELECTOR_RESPONDER, 0, 1, TRUE, FALSE},
270 {CONFIGURATION, 0, 1, TRUE, FALSE},
271 {VENDOR_ID, 0, 10, TRUE, FALSE},
272 };
273
274 /**
275 * payload order for IKE_AUTH responder
276 */
277 static payload_order_t ike_auth_r_payload_order[] = {
278 /* payload type notify type */
279 {ID_RESPONDER, 0},
280 {CERTIFICATE, 0},
281 {AUTHENTICATION, 0},
282 {EXTENSIBLE_AUTHENTICATION, 0},
283 {CONFIGURATION, 0},
284 {NOTIFY, IPCOMP_SUPPORTED},
285 {NOTIFY, USE_TRANSPORT_MODE},
286 {NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
287 {NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
288 {SECURITY_ASSOCIATION, 0},
289 {TRAFFIC_SELECTOR_INITIATOR, 0},
290 {TRAFFIC_SELECTOR_RESPONDER, 0},
291 {NOTIFY, AUTH_LIFETIME},
292 {NOTIFY, MOBIKE_SUPPORTED},
293 {NOTIFY, ADDITIONAL_IP4_ADDRESS},
294 {NOTIFY, ADDITIONAL_IP6_ADDRESS},
295 {NOTIFY, NO_ADDITIONAL_ADDRESSES},
296 {NOTIFY, 0},
297 {VENDOR_ID, 0},
298 };
299
300 /**
301 * Message rule for INFORMATIONAL from initiator.
302 */
303 static payload_rule_t informational_i_payload_rules[] = {
304 /* payload type min max encr suff */
305 {NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
306 {CONFIGURATION, 0, 1, TRUE, FALSE},
307 {DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
308 {VENDOR_ID, 0, 10, TRUE, FALSE},
309 };
310
311 /**
312 * payload order for INFORMATIONAL initiator
313 */
314 static payload_order_t informational_i_payload_order[] = {
315 /* payload type notify type */
316 {NOTIFY, UPDATE_SA_ADDRESSES},
317 {NOTIFY, NAT_DETECTION_SOURCE_IP},
318 {NOTIFY, NAT_DETECTION_DESTINATION_IP},
319 {NOTIFY, COOKIE2},
320 {NOTIFY, 0},
321 {DELETE, 0},
322 {CONFIGURATION, 0},
323 };
324
325 /**
326 * Message rule for INFORMATIONAL from responder.
327 */
328 static payload_rule_t informational_r_payload_rules[] = {
329 /* payload type min max encr suff */
330 {NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
331 {CONFIGURATION, 0, 1, TRUE, FALSE},
332 {DELETE, 0, MAX_DELETE_PAYLOADS, TRUE, FALSE},
333 {VENDOR_ID, 0, 10, TRUE, FALSE},
334 };
335
336 /**
337 * payload order for INFORMATIONAL responder
338 */
339 static payload_order_t informational_r_payload_order[] = {
340 /* payload type notify type */
341 {NOTIFY, UPDATE_SA_ADDRESSES},
342 {NOTIFY, NAT_DETECTION_SOURCE_IP},
343 {NOTIFY, NAT_DETECTION_DESTINATION_IP},
344 {NOTIFY, COOKIE2},
345 {NOTIFY, 0},
346 {DELETE, 0},
347 {CONFIGURATION, 0},
348 };
349
350 /**
351 * Message rule for CREATE_CHILD_SA from initiator.
352 */
353 static payload_rule_t create_child_sa_i_payload_rules[] = {
354 /* payload type min max encr suff */
355 {NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, FALSE},
356 {SECURITY_ASSOCIATION, 1, 1, TRUE, FALSE},
357 {NONCE, 1, 1, TRUE, FALSE},
358 {KEY_EXCHANGE, 0, 1, TRUE, FALSE},
359 {TRAFFIC_SELECTOR_INITIATOR, 0, 1, TRUE, FALSE},
360 {TRAFFIC_SELECTOR_RESPONDER, 0, 1, TRUE, FALSE},
361 {CONFIGURATION, 0, 1, TRUE, FALSE},
362 {VENDOR_ID, 0, 10, TRUE, FALSE},
363 };
364
365 /**
366 * payload order for CREATE_CHILD_SA from initiator.
367 */
368 static payload_order_t create_child_sa_i_payload_order[] = {
369 /* payload type notify type */
370 {NOTIFY, REKEY_SA},
371 {NOTIFY, IPCOMP_SUPPORTED},
372 {NOTIFY, USE_TRANSPORT_MODE},
373 {NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
374 {NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
375 {SECURITY_ASSOCIATION, 0},
376 {NONCE, 0},
377 {KEY_EXCHANGE, 0},
378 {TRAFFIC_SELECTOR_INITIATOR, 0},
379 {TRAFFIC_SELECTOR_RESPONDER, 0},
380 {NOTIFY, 0},
381 };
382
383 /**
384 * Message rule for CREATE_CHILD_SA from responder.
385 */
386 static payload_rule_t create_child_sa_r_payload_rules[] = {
387 /* payload type min max encr suff */
388 {NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
389 {SECURITY_ASSOCIATION, 1, 1, TRUE, FALSE},
390 {NONCE, 1, 1, TRUE, FALSE},
391 {KEY_EXCHANGE, 0, 1, TRUE, FALSE},
392 {TRAFFIC_SELECTOR_INITIATOR, 0, 1, TRUE, FALSE},
393 {TRAFFIC_SELECTOR_RESPONDER, 0, 1, TRUE, FALSE},
394 {CONFIGURATION, 0, 1, TRUE, FALSE},
395 {VENDOR_ID, 0, 10, TRUE, FALSE},
396 };
397
398 /**
399 * payload order for CREATE_CHILD_SA from responder.
400 */
401 static payload_order_t create_child_sa_r_payload_order[] = {
402 /* payload type notify type */
403 {NOTIFY, IPCOMP_SUPPORTED},
404 {NOTIFY, USE_TRANSPORT_MODE},
405 {NOTIFY, ESP_TFC_PADDING_NOT_SUPPORTED},
406 {NOTIFY, NON_FIRST_FRAGMENTS_ALSO},
407 {SECURITY_ASSOCIATION, 0},
408 {NONCE, 0},
409 {KEY_EXCHANGE, 0},
410 {TRAFFIC_SELECTOR_INITIATOR, 0},
411 {TRAFFIC_SELECTOR_RESPONDER, 0},
412 {NOTIFY, ADDITIONAL_TS_POSSIBLE},
413 {NOTIFY, 0},
414 };
415
416 #ifdef ME
417 /**
418 * Message rule for ME_CONNECT from initiator.
419 */
420 static payload_rule_t me_connect_i_payload_rules[] = {
421 /* payload type min max encr suff */
422 {NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
423 {ID_PEER, 1, 1, TRUE, FALSE},
424 {VENDOR_ID, 0, 10, TRUE, FALSE}
425 };
426
427 /**
428 * payload order for ME_CONNECT from initiator.
429 */
430 static payload_order_t me_connect_i_payload_order[] = {
431 /* payload type notify type */
432 {NOTIFY, 0},
433 {ID_PEER, 0},
434 {VENDOR_ID, 0},
435 };
436
437 /**
438 * Message rule for ME_CONNECT from responder.
439 */
440 static payload_rule_t me_connect_r_payload_rules[] = {
441 /* payload type min max encr suff */
442 {NOTIFY, 0, MAX_NOTIFY_PAYLOADS, TRUE, TRUE},
443 {VENDOR_ID, 0, 10, TRUE, FALSE}
444 };
445
446 /**
447 * payload order for ME_CONNECT from responder.
448 */
449 static payload_order_t me_connect_r_payload_order[] = {
450 /* payload type notify type */
451 {NOTIFY, 0},
452 {VENDOR_ID, 0},
453 };
454 #endif /* ME */
455
456 /**
457 * Message rules, defines allowed payloads.
458 */
459 static message_rule_t message_rules[] = {
460 {IKE_SA_INIT, TRUE, FALSE,
461 (sizeof(ike_sa_init_i_payload_rules)/sizeof(payload_rule_t)),
462 ike_sa_init_i_payload_rules,
463 (sizeof(ike_sa_init_i_payload_order)/sizeof(payload_order_t)),
464 ike_sa_init_i_payload_order,
465 },
466 {IKE_SA_INIT, FALSE, FALSE,
467 (sizeof(ike_sa_init_r_payload_rules)/sizeof(payload_rule_t)),
468 ike_sa_init_r_payload_rules,
469 (sizeof(ike_sa_init_r_payload_order)/sizeof(payload_order_t)),
470 ike_sa_init_r_payload_order,
471 },
472 {IKE_AUTH, TRUE, TRUE,
473 (sizeof(ike_auth_i_payload_rules)/sizeof(payload_rule_t)),
474 ike_auth_i_payload_rules,
475 (sizeof(ike_auth_i_payload_order)/sizeof(payload_order_t)),
476 ike_auth_i_payload_order,
477 },
478 {IKE_AUTH, FALSE, TRUE,
479 (sizeof(ike_auth_r_payload_rules)/sizeof(payload_rule_t)),
480 ike_auth_r_payload_rules,
481 (sizeof(ike_auth_r_payload_order)/sizeof(payload_order_t)),
482 ike_auth_r_payload_order,
483 },
484 {INFORMATIONAL, TRUE, TRUE,
485 (sizeof(informational_i_payload_rules)/sizeof(payload_rule_t)),
486 informational_i_payload_rules,
487 (sizeof(informational_i_payload_order)/sizeof(payload_order_t)),
488 informational_i_payload_order,
489 },
490 {INFORMATIONAL, FALSE, TRUE,
491 (sizeof(informational_r_payload_rules)/sizeof(payload_rule_t)),
492 informational_r_payload_rules,
493 (sizeof(informational_r_payload_order)/sizeof(payload_order_t)),
494 informational_r_payload_order,
495 },
496 {CREATE_CHILD_SA, TRUE, TRUE,
497 (sizeof(create_child_sa_i_payload_rules)/sizeof(payload_rule_t)),
498 create_child_sa_i_payload_rules,
499 (sizeof(create_child_sa_i_payload_order)/sizeof(payload_order_t)),
500 create_child_sa_i_payload_order,
501 },
502 {CREATE_CHILD_SA, FALSE, TRUE,
503 (sizeof(create_child_sa_r_payload_rules)/sizeof(payload_rule_t)),
504 create_child_sa_r_payload_rules,
505 (sizeof(create_child_sa_r_payload_order)/sizeof(payload_order_t)),
506 create_child_sa_r_payload_order,
507 },
508 #ifdef ME
509 {ME_CONNECT, TRUE, TRUE,
510 (sizeof(me_connect_i_payload_rules)/sizeof(payload_rule_t)),
511 me_connect_i_payload_rules,
512 (sizeof(me_connect_i_payload_order)/sizeof(payload_order_t)),
513 me_connect_i_payload_order,
514 },
515 {ME_CONNECT, FALSE, TRUE,
516 (sizeof(me_connect_r_payload_rules)/sizeof(payload_rule_t)),
517 me_connect_r_payload_rules,
518 (sizeof(me_connect_r_payload_order)/sizeof(payload_order_t)),
519 me_connect_r_payload_order,
520 },
521 #endif /* ME */
522 };
523
524
525 typedef struct private_message_t private_message_t;
526
527 /**
528 * Private data of an message_t object.
529 */
530 struct private_message_t {
531
532 /**
533 * Public part of a message_t object.
534 */
535 message_t public;
536
537 /**
538 * Minor version of message.
539 */
540 u_int8_t major_version;
541
542 /**
543 * Major version of message.
544 */
545 u_int8_t minor_version;
546
547 /**
548 * First Payload in message.
549 */
550 payload_type_t first_payload;
551
552 /**
553 * Assigned exchange type.
554 */
555 exchange_type_t exchange_type;
556
557 /**
558 * TRUE if message is a request, FALSE if a reply.
559 */
560 bool is_request;
561
562 /**
563 * Message ID of this message.
564 */
565 u_int32_t message_id;
566
567 /**
568 * ID of assigned IKE_SA.
569 */
570 ike_sa_id_t *ike_sa_id;
571
572 /**
573 * Assigned UDP packet, stores incoming packet or last generated one.
574 */
575 packet_t *packet;
576
577 /**
578 * Linked List where payload data are stored in.
579 */
580 linked_list_t *payloads;
581
582 /**
583 * Assigned parser to parse Header and Body of this message.
584 */
585 parser_t *parser;
586
587 /**
588 * The message rule for this message instance
589 */
590 message_rule_t *message_rule;
591 };
592
593 /**
594 * Implementation of private_message_t.set_message_rule.
595 */
596 static status_t set_message_rule(private_message_t *this)
597 {
598 int i;
599
600 for (i = 0; i < (sizeof(message_rules) / sizeof(message_rule_t)); i++)
601 {
602 if ((this->exchange_type == message_rules[i].exchange_type) &&
603 (this->is_request == message_rules[i].is_request))
604 {
605 /* found rule for given exchange_type*/
606 this->message_rule = &(message_rules[i]);
607 return SUCCESS;
608 }
609 }
610 this->message_rule = NULL;
611 return NOT_FOUND;
612 }
613
614 /**
615 * Implementation of private_message_t.get_payload_rule.
616 */
617 static status_t get_payload_rule(private_message_t *this, payload_type_t payload_type, payload_rule_t **payload_rule)
618 {
619 int i;
620
621 for (i = 0; i < this->message_rule->payload_rule_count;i++)
622 {
623 if (this->message_rule->payload_rules[i].payload_type == payload_type)
624 {
625 *payload_rule = &(this->message_rule->payload_rules[i]);
626 return SUCCESS;
627 }
628 }
629
630 *payload_rule = NULL;
631 return NOT_FOUND;
632 }
633
634 /**
635 * Implementation of message_t.set_ike_sa_id.
636 */
637 static void set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id)
638 {
639 DESTROY_IF(this->ike_sa_id);
640 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
641 }
642
643 /**
644 * Implementation of message_t.get_ike_sa_id.
645 */
646 static ike_sa_id_t* get_ike_sa_id (private_message_t *this)
647 {
648 return this->ike_sa_id;
649 }
650
651 /**
652 * Implementation of message_t.set_message_id.
653 */
654 static void set_message_id (private_message_t *this,u_int32_t message_id)
655 {
656 this->message_id = message_id;
657 }
658
659 /**
660 * Implementation of message_t.get_message_id.
661 */
662 static u_int32_t get_message_id (private_message_t *this)
663 {
664 return this->message_id;
665 }
666
667 /**
668 * Implementation of message_t.get_initiator_spi.
669 */
670 static u_int64_t get_initiator_spi (private_message_t *this)
671 {
672 return (this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
673 }
674
675 /**
676 * Implementation of message_t.get_responder_spi.
677 */
678 static u_int64_t get_responder_spi (private_message_t *this)
679 {
680 return (this->ike_sa_id->get_responder_spi(this->ike_sa_id));
681 }
682
683 /**
684 * Implementation of message_t.set_major_version.
685 */
686 static void set_major_version (private_message_t *this,u_int8_t major_version)
687 {
688 this->major_version = major_version;
689 }
690
691
692 /**
693 * Implementation of message_t.set_major_version.
694 */
695 static u_int8_t get_major_version (private_message_t *this)
696 {
697 return this->major_version;
698 }
699
700 /**
701 * Implementation of message_t.set_minor_version.
702 */
703 static void set_minor_version (private_message_t *this,u_int8_t minor_version)
704 {
705 this->minor_version = minor_version;
706 }
707
708 /**
709 * Implementation of message_t.get_minor_version.
710 */
711 static u_int8_t get_minor_version (private_message_t *this)
712 {
713 return this->minor_version;
714 }
715
716 /**
717 * Implementation of message_t.set_exchange_type.
718 */
719 static void set_exchange_type (private_message_t *this,exchange_type_t exchange_type)
720 {
721 this->exchange_type = exchange_type;
722 }
723
724 /**
725 * Implementation of message_t.get_exchange_type.
726 */
727 static exchange_type_t get_exchange_type (private_message_t *this)
728 {
729 return this->exchange_type;
730 }
731
732 /**
733 * Implementation of message_t.get_first_payload_type.
734 */
735 static payload_type_t get_first_payload_type (private_message_t *this)
736 {
737 return this->first_payload;
738 }
739
740 /**
741 * Implementation of message_t.set_request.
742 */
743 static void set_request (private_message_t *this,bool request)
744 {
745 this->is_request = request;
746 }
747
748 /**
749 * Implementation of message_t.get_request.
750 */
751 static exchange_type_t get_request (private_message_t *this)
752 {
753 return this->is_request;
754 }
755
756 /**
757 * Is this message in an encoded form?
758 */
759 static bool is_encoded(private_message_t *this)
760 {
761 chunk_t data = this->packet->get_data(this->packet);
762
763 if (data.ptr == NULL)
764 {
765 return FALSE;
766 }
767 return TRUE;
768 }
769
770 /**
771 * Implementation of message_t.add_payload.
772 */
773 static void add_payload(private_message_t *this, payload_t *payload)
774 {
775 payload_t *last_payload;
776
777 if (this->payloads->get_count(this->payloads) > 0)
778 {
779 this->payloads->get_last(this->payloads, (void **)&last_payload);
780 last_payload->set_next_type(last_payload, payload->get_type(payload));
781 }
782 else
783 {
784 this->first_payload = payload->get_type(payload);
785 }
786 payload->set_next_type(payload, NO_PAYLOAD);
787 this->payloads->insert_last(this->payloads, payload);
788
789 DBG2(DBG_ENC ,"added payload of type %N to message",
790 payload_type_names, payload->get_type(payload));
791 }
792
793 /**
794 * Implementation of message_t.add_notify.
795 */
796 static void add_notify(private_message_t *this, bool flush, notify_type_t type,
797 chunk_t data)
798 {
799 notify_payload_t *notify;
800 payload_t *payload;
801
802 if (flush)
803 {
804 while (this->payloads->remove_last(this->payloads,
805 (void**)&payload) == SUCCESS)
806 {
807 payload->destroy(payload);
808 }
809 }
810 notify = notify_payload_create();
811 notify->set_notify_type(notify, type);
812 notify->set_notification_data(notify, data);
813 add_payload(this, (payload_t*)notify);
814 }
815
816 /**
817 * Implementation of message_t.set_source.
818 */
819 static void set_source(private_message_t *this, host_t *host)
820 {
821 this->packet->set_source(this->packet, host);
822 }
823
824 /**
825 * Implementation of message_t.set_destination.
826 */
827 static void set_destination(private_message_t *this, host_t *host)
828 {
829 this->packet->set_destination(this->packet, host);
830 }
831
832 /**
833 * Implementation of message_t.get_source.
834 */
835 static host_t* get_source(private_message_t *this)
836 {
837 return this->packet->get_source(this->packet);
838 }
839
840 /**
841 * Implementation of message_t.get_destination.
842 */
843 static host_t * get_destination(private_message_t *this)
844 {
845 return this->packet->get_destination(this->packet);
846 }
847
848 /**
849 * Implementation of message_t.get_payload_iterator.
850 */
851 static iterator_t *get_payload_iterator(private_message_t *this)
852 {
853 return this->payloads->create_iterator(this->payloads, TRUE);
854 }
855
856 /**
857 * Implementation of message_t.get_payload.
858 */
859 static payload_t *get_payload(private_message_t *this, payload_type_t type)
860 {
861 payload_t *current, *found = NULL;
862 iterator_t *iterator;
863
864 iterator = this->payloads->create_iterator(this->payloads, TRUE);
865 while (iterator->iterate(iterator, (void**)&current))
866 {
867 if (current->get_type(current) == type)
868 {
869 found = current;
870 break;
871 }
872 }
873 iterator->destroy(iterator);
874 return found;
875 }
876
877 /**
878 * get a string representation of the message
879 */
880 static char* get_string(private_message_t *this, char *buf, int len)
881 {
882 iterator_t *iterator;
883 payload_t *payload;
884 int written;
885 char *pos = buf;
886
887 memset(buf, 0, len);
888 len--;
889
890 written = snprintf(pos, len, "%N %s %d [",
891 exchange_type_names, this->exchange_type,
892 this->is_request ? "request" : "response",
893 this->message_id);
894 if (written >= len || written < 0)
895 {
896 return "";
897 }
898 pos += written;
899 len -= written;
900
901 iterator = this->payloads->create_iterator(this->payloads, TRUE);
902 while (iterator->iterate(iterator, (void**)&payload))
903 {
904 written = snprintf(pos, len, " %N", payload_type_short_names,
905 payload->get_type(payload));
906 if (written >= len || written < 0)
907 {
908 return buf;
909 }
910 pos += written;
911 len -= written;
912 if (payload->get_type(payload) == NOTIFY)
913 {
914 notify_payload_t *notify = (notify_payload_t*)payload;
915 written = snprintf(pos, len, "(%N)", notify_type_short_names,
916 notify->get_notify_type(notify));
917 if (written >= len || written < 0)
918 {
919 return buf;
920 }
921 pos += written;
922 len -= written;
923 }
924 }
925 iterator->destroy(iterator);
926
927 /* remove last space */
928 snprintf(pos, len, " ]");
929 return buf;
930 }
931
932 /**
933 * reorder payloads depending on reordering rules
934 */
935 static void order_payloads(private_message_t *this)
936 {
937 linked_list_t *list;
938 payload_t *payload;
939 int i;
940
941 /* move to temp list */
942 list = linked_list_create();
943 while (this->payloads->remove_last(this->payloads,
944 (void**)&payload) == SUCCESS)
945 {
946 list->insert_first(list, payload);
947 }
948 /* for each rule, ... */
949 for (i = 0; i < this->message_rule->payload_order_count; i++)
950 {
951 enumerator_t *enumerator;
952 notify_payload_t *notify;
953 payload_order_t order = this->message_rule->payload_order[i];
954
955 /* ... find all payload ... */
956 enumerator = list->create_enumerator(list);
957 while (enumerator->enumerate(enumerator, &payload))
958 {
959 /* ... with that type ... */
960 if (payload->get_type(payload) == order.type)
961 {
962 notify = (notify_payload_t*)payload;
963
964 /**... and check notify for type. */
965 if (order.type != NOTIFY || order.notify == 0 ||
966 order.notify == notify->get_notify_type(notify))
967 {
968 list->remove_at(list, enumerator);
969 add_payload(this, payload);
970 }
971 }
972 }
973 enumerator->destroy(enumerator);
974 }
975 /* append all payloads without a rule to the end */
976 while (list->remove_last(list, (void**)&payload) == SUCCESS)
977 {
978 DBG1(DBG_ENC, "payload %N has no ordering rule in %N %s",
979 payload_type_names, payload->get_type(payload),
980 exchange_type_names, this->message_rule->exchange_type,
981 this->message_rule->is_request ? "request" : "response");
982 add_payload(this, payload);
983 }
984 list->destroy(list);
985 }
986
987 /**
988 * Implementation of private_message_t.encrypt_payloads.
989 */
990 static status_t encrypt_payloads(private_message_t *this,
991 crypter_t *crypter, signer_t* signer)
992 {
993 encryption_payload_t *encryption_payload = NULL;
994 status_t status;
995 linked_list_t *all_payloads;
996
997 if (!this->message_rule->encrypted_content)
998 {
999 DBG2(DBG_ENC, "message doesn't have to be encrypted");
1000 /* message contains no content to encrypt */
1001 return SUCCESS;
1002 }
1003
1004 if (!crypter || !signer)
1005 {
1006 DBG2(DBG_ENC, "no crypter or signer specified, do not encrypt message");
1007 /* message contains no content to encrypt */
1008 return SUCCESS;
1009 }
1010
1011 DBG2(DBG_ENC, "copy all payloads to a temporary list");
1012 all_payloads = linked_list_create();
1013
1014 /* first copy all payloads in a temporary list */
1015 while (this->payloads->get_count(this->payloads) > 0)
1016 {
1017 void *current_payload;
1018 this->payloads->remove_first(this->payloads,&current_payload);
1019 all_payloads->insert_last(all_payloads,current_payload);
1020 }
1021
1022 encryption_payload = encryption_payload_create();
1023
1024 DBG2(DBG_ENC, "check each payloads if they have to get encrypted");
1025 while (all_payloads->get_count(all_payloads) > 0)
1026 {
1027 payload_rule_t *payload_rule;
1028 payload_t *current_payload;
1029 bool to_encrypt = FALSE;
1030
1031 all_payloads->remove_first(all_payloads,(void **)&current_payload);
1032
1033 status = get_payload_rule(this,
1034 current_payload->get_type(current_payload),&payload_rule);
1035 /* for payload types which are not found in supported payload list,
1036 * it is presumed that they don't have to be encrypted */
1037 if ((status == SUCCESS) && (payload_rule->encrypted))
1038 {
1039 DBG2(DBG_ENC, "payload %N gets encrypted",
1040 payload_type_names, current_payload->get_type(current_payload));
1041 to_encrypt = TRUE;
1042 }
1043
1044 if (to_encrypt)
1045 {
1046 DBG2(DBG_ENC, "insert payload %N to encryption payload",
1047 payload_type_names, current_payload->get_type(current_payload));
1048 encryption_payload->add_payload(encryption_payload,current_payload);
1049 }
1050 else
1051 {
1052 DBG2(DBG_ENC, "insert payload %N unencrypted",
1053 payload_type_names ,current_payload->get_type(current_payload));
1054 add_payload(this, (payload_t*)encryption_payload);
1055 }
1056 }
1057
1058 status = SUCCESS;
1059 DBG2(DBG_ENC, "encrypting encryption payload");
1060 encryption_payload->set_transforms(encryption_payload, crypter,signer);
1061 status = encryption_payload->encrypt(encryption_payload);
1062 DBG2(DBG_ENC, "add encrypted payload to payload list");
1063 add_payload(this, (payload_t*)encryption_payload);
1064
1065 all_payloads->destroy(all_payloads);
1066
1067 return status;
1068 }
1069
1070 /**
1071 * Implementation of message_t.generate.
1072 */
1073 static status_t generate(private_message_t *this, crypter_t *crypter,
1074 signer_t* signer, packet_t **packet)
1075 {
1076 generator_t *generator;
1077 ike_header_t *ike_header;
1078 payload_t *payload, *next_payload;
1079 iterator_t *iterator;
1080 status_t status;
1081 chunk_t packet_data;
1082 char str[256];
1083
1084 if (is_encoded(this))
1085 {
1086 /* already generated, return a new packet clone */
1087 *packet = this->packet->clone(this->packet);
1088 return SUCCESS;
1089 }
1090
1091 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
1092 {
1093 DBG1(DBG_ENC, "exchange type is not defined");
1094 return INVALID_STATE;
1095 }
1096
1097 if (this->packet->get_source(this->packet) == NULL ||
1098 this->packet->get_destination(this->packet) == NULL)
1099 {
1100 DBG1(DBG_ENC, "%s not defined",
1101 !this->packet->get_source(this->packet) ? "source" : "destination");
1102 return INVALID_STATE;
1103 }
1104
1105 /* set the rules for this messge */
1106 status = set_message_rule(this);
1107 if (status != SUCCESS)
1108 {
1109 DBG1(DBG_ENC, "no message rules specified for this message type");
1110 return NOT_SUPPORTED;
1111 }
1112
1113 order_payloads(this);
1114
1115 DBG1(DBG_ENC, "generating %s", get_string(this, str, sizeof(str)));
1116
1117 /* going to encrypt all content which have to be encrypted */
1118 status = encrypt_payloads(this, crypter, signer);
1119 if (status != SUCCESS)
1120 {
1121 DBG1(DBG_ENC, "payload encryption failed");
1122 return status;
1123 }
1124
1125 /* build ike header */
1126 ike_header = ike_header_create();
1127
1128 ike_header->set_exchange_type(ike_header, this->exchange_type);
1129 ike_header->set_message_id(ike_header, this->message_id);
1130 ike_header->set_response_flag(ike_header, !this->is_request);
1131 ike_header->set_initiator_flag(ike_header, this->ike_sa_id->is_initiator(this->ike_sa_id));
1132 ike_header->set_initiator_spi(ike_header, this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
1133 ike_header->set_responder_spi(ike_header, this->ike_sa_id->get_responder_spi(this->ike_sa_id));
1134
1135 generator = generator_create();
1136
1137 payload = (payload_t*)ike_header;
1138
1139
1140 /* generate every payload expect last one, this is done later*/
1141 iterator = this->payloads->create_iterator(this->payloads, TRUE);
1142 while(iterator->iterate(iterator, (void**)&next_payload))
1143 {
1144 payload->set_next_type(payload, next_payload->get_type(next_payload));
1145 generator->generate_payload(generator, payload);
1146 payload = next_payload;
1147 }
1148 iterator->destroy(iterator);
1149
1150 /* last payload has no next payload*/
1151 payload->set_next_type(payload, NO_PAYLOAD);
1152
1153 generator->generate_payload(generator, payload);
1154
1155 ike_header->destroy(ike_header);
1156
1157 /* build packet */
1158 generator->write_to_chunk(generator, &packet_data);
1159 generator->destroy(generator);
1160
1161 /* if last payload is of type encrypted, integrity checksum if necessary */
1162 if (payload->get_type(payload) == ENCRYPTED)
1163 {
1164 DBG2(DBG_ENC, "build signature on whole message");
1165 encryption_payload_t *encryption_payload = (encryption_payload_t*)payload;
1166 status = encryption_payload->build_signature(encryption_payload, packet_data);
1167 if (status != SUCCESS)
1168 {
1169 return status;
1170 }
1171 }
1172
1173 this->packet->set_data(this->packet, packet_data);
1174
1175 /* clone packet for caller */
1176 *packet = this->packet->clone(this->packet);
1177
1178 DBG2(DBG_ENC, "message generated successfully");
1179 return SUCCESS;
1180 }
1181
1182 /**
1183 * Implementation of message_t.get_packet.
1184 */
1185 static packet_t *get_packet (private_message_t *this)
1186 {
1187 if (this->packet == NULL)
1188 {
1189 return NULL;
1190 }
1191 return this->packet->clone(this->packet);
1192 }
1193
1194 /**
1195 * Implementation of message_t.get_packet_data.
1196 */
1197 static chunk_t get_packet_data (private_message_t *this)
1198 {
1199 if (this->packet == NULL)
1200 {
1201 return chunk_empty;
1202 }
1203 return chunk_clone(this->packet->get_data(this->packet));
1204 }
1205
1206 /**
1207 * Implementation of message_t.parse_header.
1208 */
1209 static status_t parse_header(private_message_t *this)
1210 {
1211 ike_header_t *ike_header;
1212 status_t status;
1213
1214 DBG2(DBG_ENC, "parsing header of message");
1215
1216 this->parser->reset_context(this->parser);
1217 status = this->parser->parse_payload(this->parser,HEADER,(payload_t **) &ike_header);
1218 if (status != SUCCESS)
1219 {
1220 DBG1(DBG_ENC, "header could not be parsed");
1221 return status;
1222
1223 }
1224
1225 /* verify payload */
1226 status = ike_header->payload_interface.verify(&(ike_header->payload_interface));
1227 if (status != SUCCESS)
1228 {
1229 DBG1(DBG_ENC, "header verification failed");
1230 ike_header->destroy(ike_header);
1231 return status;
1232 }
1233
1234 if (this->ike_sa_id != NULL)
1235 {
1236 this->ike_sa_id->destroy(this->ike_sa_id);
1237 }
1238
1239 this->ike_sa_id = ike_sa_id_create(ike_header->get_initiator_spi(ike_header),
1240 ike_header->get_responder_spi(ike_header),
1241 ike_header->get_initiator_flag(ike_header));
1242
1243 this->exchange_type = ike_header->get_exchange_type(ike_header);
1244 this->message_id = ike_header->get_message_id(ike_header);
1245 this->is_request = (!(ike_header->get_response_flag(ike_header)));
1246 this->major_version = ike_header->get_maj_version(ike_header);
1247 this->minor_version = ike_header->get_min_version(ike_header);
1248 this->first_payload = ike_header->payload_interface.get_next_type(&(ike_header->payload_interface));
1249
1250 DBG2(DBG_ENC, "parsed a %N %s", exchange_type_names, this->exchange_type,
1251 this->is_request ? "request" : "response");
1252
1253 ike_header->destroy(ike_header);
1254
1255 /* get the rules for this messge */
1256 status = set_message_rule(this);
1257 if (status != SUCCESS)
1258 {
1259 DBG1(DBG_ENC, "no message rules specified for a %N %s",
1260 exchange_type_names, this->exchange_type,
1261 this->is_request ? "request" : "response");
1262 }
1263
1264 return status;
1265 }
1266
1267 /**
1268 * Implementation of private_message_t.decrypt_and_verify_payloads.
1269 */
1270 static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, signer_t* signer)
1271 {
1272 bool current_payload_was_encrypted = FALSE;
1273 payload_t *previous_payload = NULL;
1274 int payload_number = 1;
1275 iterator_t *iterator;
1276 payload_t *current_payload;
1277 status_t status;
1278
1279 iterator = this->payloads->create_iterator(this->payloads,TRUE);
1280
1281 /* process each payload and decrypt a encryption payload */
1282 while(iterator->iterate(iterator, (void**)&current_payload))
1283 {
1284 payload_rule_t *payload_rule;
1285 payload_type_t current_payload_type;
1286
1287 /* needed to check */
1288 current_payload_type = current_payload->get_type(current_payload);
1289
1290 DBG2(DBG_ENC, "process payload of type %N",
1291 payload_type_names, current_payload_type);
1292
1293 if (current_payload_type == ENCRYPTED)
1294 {
1295 encryption_payload_t *encryption_payload;
1296 payload_t *current_encrypted_payload;
1297
1298 encryption_payload = (encryption_payload_t*)current_payload;
1299
1300 DBG2(DBG_ENC, "found an encryption payload");
1301
1302 if (payload_number != this->payloads->get_count(this->payloads))
1303 {
1304 /* encrypted payload is not last one */
1305 DBG1(DBG_ENC, "encrypted payload is not last payload");
1306 iterator->destroy(iterator);
1307 return VERIFY_ERROR;
1308 }
1309 /* decrypt */
1310 encryption_payload->set_transforms(encryption_payload, crypter, signer);
1311 DBG2(DBG_ENC, "verify signature of encryption payload");
1312 status = encryption_payload->verify_signature(encryption_payload,
1313 this->packet->get_data(this->packet));
1314 if (status != SUCCESS)
1315 {
1316 DBG1(DBG_ENC, "encryption payload signature invalid");
1317 iterator->destroy(iterator);
1318 return FAILED;
1319 }
1320 DBG2(DBG_ENC, "decrypting content of encryption payload");
1321 status = encryption_payload->decrypt(encryption_payload);
1322 if (status != SUCCESS)
1323 {
1324 DBG1(DBG_ENC, "encrypted payload could not be decrypted and parsed");
1325 iterator->destroy(iterator);
1326 return PARSE_ERROR;
1327 }
1328
1329 /* needed later to find out if a payload was encrypted */
1330 current_payload_was_encrypted = TRUE;
1331
1332 /* check if there are payloads contained in the encryption payload */
1333 if (encryption_payload->get_payload_count(encryption_payload) == 0)
1334 {
1335 DBG2(DBG_ENC, "encrypted payload is empty");
1336 /* remove the encryption payload, is not needed anymore */
1337 iterator->remove(iterator);
1338 /* encrypted payload contains no other payload */
1339 current_payload_type = NO_PAYLOAD;
1340 }
1341 else
1342 {
1343 /* encryption_payload is replaced with first payload contained in encryption_payload */
1344 encryption_payload->remove_first_payload(encryption_payload, &current_encrypted_payload);
1345 iterator->replace(iterator,NULL,(void *) current_encrypted_payload);
1346 current_payload_type = current_encrypted_payload->get_type(current_encrypted_payload);
1347 }
1348
1349 /* is the current paylad the first in the message? */
1350 if (previous_payload == NULL)
1351 {
1352 /* yes, set the first payload type of the message to the current type */
1353 this->first_payload = current_payload_type;
1354 }
1355 else
1356 {
1357 /* no, set the next_type of the previous payload to the current type */
1358 previous_payload->set_next_type(previous_payload, current_payload_type);
1359 }
1360
1361 /* all encrypted payloads are added to the payload list */
1362 while (encryption_payload->get_payload_count(encryption_payload) > 0)
1363 {
1364 encryption_payload->remove_first_payload(encryption_payload, &current_encrypted_payload);
1365 DBG2(DBG_ENC, "insert unencrypted payload of type %N at end of list",
1366 payload_type_names, current_encrypted_payload->get_type(current_encrypted_payload));
1367 this->payloads->insert_last(this->payloads,current_encrypted_payload);
1368 }
1369
1370 /* encryption payload is processed, payloads are moved. Destroy it. */
1371 encryption_payload->destroy(encryption_payload);
1372 }
1373
1374 /* we allow unknown payloads of any type and don't bother if it was encrypted. Not our problem. */
1375 if (current_payload_type != UNKNOWN_PAYLOAD && current_payload_type != NO_PAYLOAD)
1376 {
1377 /* get the ruleset for found payload */
1378 status = get_payload_rule(this, current_payload_type, &payload_rule);
1379 if (status != SUCCESS)
1380 {
1381 /* payload is not allowed */
1382 DBG1(DBG_ENC, "payload type %N not allowed",
1383 payload_type_names, current_payload_type);
1384 iterator->destroy(iterator);
1385 return VERIFY_ERROR;
1386 }
1387
1388 /* check if the payload was encrypted, and if it should been have encrypted */
1389 if (payload_rule->encrypted != current_payload_was_encrypted)
1390 {
1391 /* payload was not encrypted, but should have been. or vice-versa */
1392 DBG1(DBG_ENC, "payload type %N should be %s!",
1393 payload_type_names, current_payload_type,
1394 (payload_rule->encrypted) ? "encrypted" : "not encrypted");
1395 iterator->destroy(iterator);
1396 return VERIFY_ERROR;
1397 }
1398 }
1399 /* advance to the next payload */
1400 payload_number++;
1401 /* is stored to set next payload in case of found encryption payload */
1402 previous_payload = current_payload;
1403 }
1404 iterator->destroy(iterator);
1405 return SUCCESS;
1406 }
1407
1408 /**
1409 * Implementation of private_message_t.verify.
1410 */
1411 static status_t verify(private_message_t *this)
1412 {
1413 int i;
1414 iterator_t *iterator;
1415 payload_t *current_payload;
1416 size_t total_found_payloads = 0;
1417
1418 DBG2(DBG_ENC, "verifying message structure");
1419
1420 iterator = this->payloads->create_iterator(this->payloads,TRUE);
1421 /* check for payloads with wrong count*/
1422 for (i = 0; i < this->message_rule->payload_rule_count;i++)
1423 {
1424 size_t found_payloads = 0;
1425
1426 /* check all payloads for specific rule */
1427 iterator->reset(iterator);
1428
1429 while(iterator->iterate(iterator,(void **)&current_payload))
1430 {
1431 payload_type_t current_payload_type;
1432
1433 current_payload_type = current_payload->get_type(current_payload);
1434 if (current_payload_type == UNKNOWN_PAYLOAD)
1435 {
1436 /* unknown payloads are ignored, IF they are not critical */
1437 unknown_payload_t *unknown_payload = (unknown_payload_t*)current_payload;
1438 if (unknown_payload->is_critical(unknown_payload))
1439 {
1440 DBG1(DBG_ENC, "%N is not supported, but its critical!",
1441 payload_type_names, current_payload_type);
1442 iterator->destroy(iterator);
1443 return NOT_SUPPORTED;
1444 }
1445 }
1446 else if (current_payload_type == this->message_rule->payload_rules[i].payload_type)
1447 {
1448 found_payloads++;
1449 total_found_payloads++;
1450 DBG2(DBG_ENC, "found payload of type %N",
1451 payload_type_names, this->message_rule->payload_rules[i].payload_type);
1452
1453 /* as soon as ohe payload occures more then specified, the verification fails */
1454 if (found_payloads > this->message_rule->payload_rules[i].max_occurence)
1455 {
1456 DBG1(DBG_ENC, "payload of type %N more than %d times (%d) occured in current message",
1457 payload_type_names, current_payload_type,
1458 this->message_rule->payload_rules[i].max_occurence, found_payloads);
1459 iterator->destroy(iterator);
1460 return VERIFY_ERROR;
1461 }
1462 }
1463 }
1464
1465 if (found_payloads < this->message_rule->payload_rules[i].min_occurence)
1466 {
1467 DBG1(DBG_ENC, "payload of type %N not occured %d times (%d)",
1468 payload_type_names, this->message_rule->payload_rules[i].payload_type,
1469 this->message_rule->payload_rules[i].min_occurence, found_payloads);
1470 iterator->destroy(iterator);
1471 return VERIFY_ERROR;
1472 }
1473 if ((this->message_rule->payload_rules[i].sufficient) && (this->payloads->get_count(this->payloads) == total_found_payloads))
1474 {
1475 iterator->destroy(iterator);
1476 return SUCCESS;
1477 }
1478 }
1479 iterator->destroy(iterator);
1480 return SUCCESS;
1481 }
1482
1483 /**
1484 * Implementation of message_t.parse_body.
1485 */
1486 static status_t parse_body(private_message_t *this, crypter_t *crypter, signer_t *signer)
1487 {
1488 status_t status = SUCCESS;
1489 payload_type_t current_payload_type;
1490 char str[256];
1491
1492 current_payload_type = this->first_payload;
1493
1494 DBG2(DBG_ENC, "parsing body of message, first payload is %N",
1495 payload_type_names, current_payload_type);
1496
1497 /* parse payload for payload, while there are more available */
1498 while ((current_payload_type != NO_PAYLOAD))
1499 {
1500 payload_t *current_payload;
1501
1502 DBG2(DBG_ENC, "starting parsing a %N payload",
1503 payload_type_names, current_payload_type);
1504
1505 /* parse current payload */
1506 status = this->parser->parse_payload(this->parser,current_payload_type,(payload_t **) &current_payload);
1507
1508 if (status != SUCCESS)
1509 {
1510 DBG1(DBG_ENC, "payload type %N could not be parsed",
1511 payload_type_names, current_payload_type);
1512 return PARSE_ERROR;
1513 }
1514
1515 DBG2(DBG_ENC, "verifying payload of type %N",
1516 payload_type_names, current_payload_type);
1517
1518 /* verify it, stop parsig if its invalid */
1519 status = current_payload->verify(current_payload);
1520 if (status != SUCCESS)
1521 {
1522 DBG1(DBG_ENC, "%N payload verification failed",
1523 payload_type_names, current_payload_type);
1524 current_payload->destroy(current_payload);
1525 return VERIFY_ERROR;
1526 }
1527
1528 DBG2(DBG_ENC, "%N payload verified. Adding to payload list",
1529 payload_type_names, current_payload_type);
1530 this->payloads->insert_last(this->payloads,current_payload);
1531
1532 /* an encryption payload is the last one, so STOP here. decryption is done later */
1533 if (current_payload_type == ENCRYPTED)
1534 {
1535 DBG2(DBG_ENC, "%N payload found. Stop parsing",
1536 payload_type_names, current_payload_type);
1537 break;
1538 }
1539
1540 /* get next payload type */
1541 current_payload_type = current_payload->get_next_type(current_payload);
1542 }
1543
1544 if (current_payload_type == ENCRYPTED)
1545 {
1546 status = decrypt_payloads(this,crypter,signer);
1547 if (status != SUCCESS)
1548 {
1549 DBG1(DBG_ENC, "could not decrypt payloads");
1550 return status;
1551 }
1552 }
1553
1554 status = verify(this);
1555 if (status != SUCCESS)
1556 {
1557 return status;
1558 }
1559
1560 DBG1(DBG_ENC, "parsed %s", get_string(this, str, sizeof(str)));
1561
1562 return SUCCESS;
1563 }
1564
1565 /**
1566 * Implementation of message_t.destroy.
1567 */
1568 static void destroy (private_message_t *this)
1569 {
1570 DESTROY_IF(this->ike_sa_id);
1571 this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
1572 this->packet->destroy(this->packet);
1573 this->parser->destroy(this->parser);
1574 free(this);
1575 }
1576
1577 /*
1578 * Described in Header-File
1579 */
1580 message_t *message_create_from_packet(packet_t *packet)
1581 {
1582 private_message_t *this = malloc_thing(private_message_t);
1583
1584 /* public functions */
1585 this->public.set_major_version = (void(*)(message_t*, u_int8_t))set_major_version;
1586 this->public.get_major_version = (u_int8_t(*)(message_t*))get_major_version;
1587 this->public.set_minor_version = (void(*)(message_t*, u_int8_t))set_minor_version;
1588 this->public.get_minor_version = (u_int8_t(*)(message_t*))get_minor_version;
1589 this->public.set_message_id = (void(*)(message_t*, u_int32_t))set_message_id;
1590 this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
1591 this->public.get_initiator_spi = (u_int64_t(*)(message_t*))get_initiator_spi;
1592 this->public.get_responder_spi = (u_int64_t(*)(message_t*))get_responder_spi;
1593 this->public.set_ike_sa_id = (void(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
1594 this->public.get_ike_sa_id = (ike_sa_id_t*(*)(message_t*))get_ike_sa_id;
1595 this->public.set_exchange_type = (void(*)(message_t*, exchange_type_t))set_exchange_type;
1596 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
1597 this->public.get_first_payload_type = (payload_type_t(*)(message_t*))get_first_payload_type;
1598 this->public.set_request = (void(*)(message_t*, bool))set_request;
1599 this->public.get_request = (bool(*)(message_t*))get_request;
1600 this->public.add_payload = (void(*)(message_t*,payload_t*))add_payload;
1601 this->public.add_notify = (void(*)(message_t*,bool,notify_type_t,chunk_t))add_notify;
1602 this->public.generate = (status_t (*) (message_t *,crypter_t*,signer_t*,packet_t**)) generate;
1603 this->public.set_source = (void (*) (message_t*,host_t*)) set_source;
1604 this->public.get_source = (host_t * (*) (message_t*)) get_source;
1605 this->public.set_destination = (void (*) (message_t*,host_t*)) set_destination;
1606 this->public.get_destination = (host_t * (*) (message_t*)) get_destination;
1607 this->public.get_payload_iterator = (iterator_t * (*) (message_t *)) get_payload_iterator;
1608 this->public.get_payload = (payload_t * (*) (message_t *, payload_type_t)) get_payload;
1609 this->public.parse_header = (status_t (*) (message_t *)) parse_header;
1610 this->public.parse_body = (status_t (*) (message_t *,crypter_t*,signer_t*)) parse_body;
1611 this->public.get_packet = (packet_t * (*) (message_t*)) get_packet;
1612 this->public.get_packet_data = (chunk_t (*) (message_t *this)) get_packet_data;
1613 this->public.destroy = (void(*)(message_t*))destroy;
1614
1615 /* private values */
1616 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
1617 this->is_request = TRUE;
1618 this->ike_sa_id = NULL;
1619 this->first_payload = NO_PAYLOAD;
1620 this->message_id = 0;
1621
1622 /* private values */
1623 if (packet == NULL)
1624 {
1625 packet = packet_create();
1626 }
1627 this->message_rule = NULL;
1628 this->packet = packet;
1629 this->payloads = linked_list_create();
1630
1631 /* parser is created from data of packet */
1632 this->parser = parser_create(this->packet->get_data(this->packet));
1633
1634 return (&this->public);
1635 }
1636
1637 /*
1638 * Described in Header.
1639 */
1640 message_t *message_create()
1641 {
1642 return message_create_from_packet(NULL);
1643 }
1644