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