merged multi-auth branch back into trunk
[strongswan.git] / src / charon / encoding / message.c
1 /*
2 * Copyright (C) 2006-2007 Tobias Brunner
3 * Copyright (C) 2005-2009 Martin Willi
4 * Copyright (C) 2006 Daniel Roethlisberger
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, 0, 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, 0, 1, TRUE, FALSE},
221 {TRAFFIC_SELECTOR_INITIATOR, 0, 1, TRUE, FALSE},
222 {TRAFFIC_SELECTOR_RESPONDER, 0, 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 {AUTHENTICATION, 0, 1, TRUE, TRUE},
265 {CERTIFICATE, 0, 4, TRUE, FALSE},
266 {ID_RESPONDER, 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.create_payload_enumerator.
850 */
851 static enumerator_t *create_payload_enumerator(private_message_t *this)
852 {
853 return this->payloads->create_enumerator(this->payloads);
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 enumerator_t *enumerator;
863
864 enumerator = create_payload_enumerator(this);
865 while (enumerator->enumerate(enumerator, &current))
866 {
867 if (current->get_type(current) == type)
868 {
869 found = current;
870 break;
871 }
872 }
873 enumerator->destroy(enumerator);
874 return found;
875 }
876
877 /**
878 * Implementation of message_t.get_notify
879 */
880 static notify_payload_t* get_notify(private_message_t *this, notify_type_t type)
881 {
882 enumerator_t *enumerator;
883 notify_payload_t *notify = NULL;
884 payload_t *payload;
885
886 enumerator = create_payload_enumerator(this);
887 while (enumerator->enumerate(enumerator, &payload))
888 {
889 if (payload->get_type(payload) == NOTIFY)
890 {
891 notify = (notify_payload_t*)payload;
892 if (notify->get_notify_type(notify) == type)
893 {
894 break;
895 }
896 notify = NULL;
897 }
898 }
899 enumerator->destroy(enumerator);
900 return notify;
901 }
902
903 /**
904 * get a string representation of the message
905 */
906 static char* get_string(private_message_t *this, char *buf, int len)
907 {
908 enumerator_t *enumerator;
909 payload_t *payload;
910 int written;
911 char *pos = buf;
912
913 memset(buf, 0, len);
914 len--;
915
916 written = snprintf(pos, len, "%N %s %d [",
917 exchange_type_names, this->exchange_type,
918 this->is_request ? "request" : "response",
919 this->message_id);
920 if (written >= len || written < 0)
921 {
922 return "";
923 }
924 pos += written;
925 len -= written;
926
927 enumerator = create_payload_enumerator(this);
928 while (enumerator->enumerate(enumerator, &payload))
929 {
930 written = snprintf(pos, len, " %N", payload_type_short_names,
931 payload->get_type(payload));
932 if (written >= len || written < 0)
933 {
934 return buf;
935 }
936 pos += written;
937 len -= written;
938 if (payload->get_type(payload) == NOTIFY)
939 {
940 notify_payload_t *notify = (notify_payload_t*)payload;
941 written = snprintf(pos, len, "(%N)", notify_type_short_names,
942 notify->get_notify_type(notify));
943 if (written >= len || written < 0)
944 {
945 return buf;
946 }
947 pos += written;
948 len -= written;
949 }
950 }
951 enumerator->destroy(enumerator);
952
953 /* remove last space */
954 snprintf(pos, len, " ]");
955 return buf;
956 }
957
958 /**
959 * reorder payloads depending on reordering rules
960 */
961 static void order_payloads(private_message_t *this)
962 {
963 linked_list_t *list;
964 payload_t *payload;
965 int i;
966
967 /* move to temp list */
968 list = linked_list_create();
969 while (this->payloads->remove_last(this->payloads,
970 (void**)&payload) == SUCCESS)
971 {
972 list->insert_first(list, payload);
973 }
974 /* for each rule, ... */
975 for (i = 0; i < this->message_rule->payload_order_count; i++)
976 {
977 enumerator_t *enumerator;
978 notify_payload_t *notify;
979 payload_order_t order = this->message_rule->payload_order[i];
980
981 /* ... find all payload ... */
982 enumerator = list->create_enumerator(list);
983 while (enumerator->enumerate(enumerator, &payload))
984 {
985 /* ... with that type ... */
986 if (payload->get_type(payload) == order.type)
987 {
988 notify = (notify_payload_t*)payload;
989
990 /**... and check notify for type. */
991 if (order.type != NOTIFY || order.notify == 0 ||
992 order.notify == notify->get_notify_type(notify))
993 {
994 list->remove_at(list, enumerator);
995 add_payload(this, payload);
996 }
997 }
998 }
999 enumerator->destroy(enumerator);
1000 }
1001 /* append all payloads without a rule to the end */
1002 while (list->remove_last(list, (void**)&payload) == SUCCESS)
1003 {
1004 DBG1(DBG_ENC, "payload %N has no ordering rule in %N %s",
1005 payload_type_names, payload->get_type(payload),
1006 exchange_type_names, this->message_rule->exchange_type,
1007 this->message_rule->is_request ? "request" : "response");
1008 add_payload(this, payload);
1009 }
1010 list->destroy(list);
1011 }
1012
1013 /**
1014 * Implementation of private_message_t.encrypt_payloads.
1015 */
1016 static status_t encrypt_payloads(private_message_t *this,
1017 crypter_t *crypter, signer_t* signer)
1018 {
1019 encryption_payload_t *encryption_payload = NULL;
1020 status_t status;
1021 linked_list_t *all_payloads;
1022
1023 if (!this->message_rule->encrypted_content)
1024 {
1025 DBG2(DBG_ENC, "message doesn't have to be encrypted");
1026 /* message contains no content to encrypt */
1027 return SUCCESS;
1028 }
1029
1030 if (!crypter || !signer)
1031 {
1032 DBG2(DBG_ENC, "no crypter or signer specified, do not encrypt message");
1033 /* message contains no content to encrypt */
1034 return SUCCESS;
1035 }
1036
1037 DBG2(DBG_ENC, "copy all payloads to a temporary list");
1038 all_payloads = linked_list_create();
1039
1040 /* first copy all payloads in a temporary list */
1041 while (this->payloads->get_count(this->payloads) > 0)
1042 {
1043 void *current_payload;
1044 this->payloads->remove_first(this->payloads,&current_payload);
1045 all_payloads->insert_last(all_payloads,current_payload);
1046 }
1047
1048 encryption_payload = encryption_payload_create();
1049
1050 DBG2(DBG_ENC, "check each payloads if they have to get encrypted");
1051 while (all_payloads->get_count(all_payloads) > 0)
1052 {
1053 payload_rule_t *payload_rule;
1054 payload_t *current_payload;
1055 bool to_encrypt = FALSE;
1056
1057 all_payloads->remove_first(all_payloads,(void **)&current_payload);
1058
1059 status = get_payload_rule(this,
1060 current_payload->get_type(current_payload),&payload_rule);
1061 /* for payload types which are not found in supported payload list,
1062 * it is presumed that they don't have to be encrypted */
1063 if ((status == SUCCESS) && (payload_rule->encrypted))
1064 {
1065 DBG2(DBG_ENC, "payload %N gets encrypted",
1066 payload_type_names, current_payload->get_type(current_payload));
1067 to_encrypt = TRUE;
1068 }
1069
1070 if (to_encrypt)
1071 {
1072 DBG2(DBG_ENC, "insert payload %N to encryption payload",
1073 payload_type_names, current_payload->get_type(current_payload));
1074 encryption_payload->add_payload(encryption_payload,current_payload);
1075 }
1076 else
1077 {
1078 DBG2(DBG_ENC, "insert payload %N unencrypted",
1079 payload_type_names ,current_payload->get_type(current_payload));
1080 add_payload(this, (payload_t*)encryption_payload);
1081 }
1082 }
1083
1084 status = SUCCESS;
1085 DBG2(DBG_ENC, "encrypting encryption payload");
1086 encryption_payload->set_transforms(encryption_payload, crypter,signer);
1087 status = encryption_payload->encrypt(encryption_payload);
1088 DBG2(DBG_ENC, "add encrypted payload to payload list");
1089 add_payload(this, (payload_t*)encryption_payload);
1090
1091 all_payloads->destroy(all_payloads);
1092
1093 return status;
1094 }
1095
1096 /**
1097 * Implementation of message_t.generate.
1098 */
1099 static status_t generate(private_message_t *this, crypter_t *crypter,
1100 signer_t* signer, packet_t **packet)
1101 {
1102 generator_t *generator;
1103 ike_header_t *ike_header;
1104 payload_t *payload, *next_payload;
1105 enumerator_t *enumerator;
1106 status_t status;
1107 chunk_t packet_data;
1108 char str[256];
1109
1110 if (is_encoded(this))
1111 {
1112 /* already generated, return a new packet clone */
1113 *packet = this->packet->clone(this->packet);
1114 return SUCCESS;
1115 }
1116
1117 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
1118 {
1119 DBG1(DBG_ENC, "exchange type is not defined");
1120 return INVALID_STATE;
1121 }
1122
1123 if (this->packet->get_source(this->packet) == NULL ||
1124 this->packet->get_destination(this->packet) == NULL)
1125 {
1126 DBG1(DBG_ENC, "%s not defined",
1127 !this->packet->get_source(this->packet) ? "source" : "destination");
1128 return INVALID_STATE;
1129 }
1130
1131 /* set the rules for this messge */
1132 status = set_message_rule(this);
1133 if (status != SUCCESS)
1134 {
1135 DBG1(DBG_ENC, "no message rules specified for this message type");
1136 return NOT_SUPPORTED;
1137 }
1138
1139 order_payloads(this);
1140
1141 DBG1(DBG_ENC, "generating %s", get_string(this, str, sizeof(str)));
1142
1143 /* going to encrypt all content which have to be encrypted */
1144 status = encrypt_payloads(this, crypter, signer);
1145 if (status != SUCCESS)
1146 {
1147 DBG1(DBG_ENC, "payload encryption failed");
1148 return status;
1149 }
1150
1151 /* build ike header */
1152 ike_header = ike_header_create();
1153
1154 ike_header->set_exchange_type(ike_header, this->exchange_type);
1155 ike_header->set_message_id(ike_header, this->message_id);
1156 ike_header->set_response_flag(ike_header, !this->is_request);
1157 ike_header->set_initiator_flag(ike_header, this->ike_sa_id->is_initiator(this->ike_sa_id));
1158 ike_header->set_initiator_spi(ike_header, this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
1159 ike_header->set_responder_spi(ike_header, this->ike_sa_id->get_responder_spi(this->ike_sa_id));
1160
1161 generator = generator_create();
1162
1163 payload = (payload_t*)ike_header;
1164
1165 /* generate every payload expect last one, this is done later*/
1166 enumerator = create_payload_enumerator(this);
1167 while (enumerator->enumerate(enumerator, &next_payload))
1168 {
1169 payload->set_next_type(payload, next_payload->get_type(next_payload));
1170 generator->generate_payload(generator, payload);
1171 payload = next_payload;
1172 }
1173 enumerator->destroy(enumerator);
1174
1175 /* last payload has no next payload*/
1176 payload->set_next_type(payload, NO_PAYLOAD);
1177
1178 generator->generate_payload(generator, payload);
1179
1180 ike_header->destroy(ike_header);
1181
1182 /* build packet */
1183 generator->write_to_chunk(generator, &packet_data);
1184 generator->destroy(generator);
1185
1186 /* if last payload is of type encrypted, integrity checksum if necessary */
1187 if (payload->get_type(payload) == ENCRYPTED)
1188 {
1189 DBG2(DBG_ENC, "build signature on whole message");
1190 encryption_payload_t *encryption_payload = (encryption_payload_t*)payload;
1191 status = encryption_payload->build_signature(encryption_payload, packet_data);
1192 if (status != SUCCESS)
1193 {
1194 return status;
1195 }
1196 }
1197
1198 this->packet->set_data(this->packet, packet_data);
1199
1200 /* clone packet for caller */
1201 *packet = this->packet->clone(this->packet);
1202
1203 DBG2(DBG_ENC, "message generated successfully");
1204 return SUCCESS;
1205 }
1206
1207 /**
1208 * Implementation of message_t.get_packet.
1209 */
1210 static packet_t *get_packet (private_message_t *this)
1211 {
1212 if (this->packet == NULL)
1213 {
1214 return NULL;
1215 }
1216 return this->packet->clone(this->packet);
1217 }
1218
1219 /**
1220 * Implementation of message_t.get_packet_data.
1221 */
1222 static chunk_t get_packet_data (private_message_t *this)
1223 {
1224 if (this->packet == NULL)
1225 {
1226 return chunk_empty;
1227 }
1228 return chunk_clone(this->packet->get_data(this->packet));
1229 }
1230
1231 /**
1232 * Implementation of message_t.parse_header.
1233 */
1234 static status_t parse_header(private_message_t *this)
1235 {
1236 ike_header_t *ike_header;
1237 status_t status;
1238
1239 DBG2(DBG_ENC, "parsing header of message");
1240
1241 this->parser->reset_context(this->parser);
1242 status = this->parser->parse_payload(this->parser,HEADER,(payload_t **) &ike_header);
1243 if (status != SUCCESS)
1244 {
1245 DBG1(DBG_ENC, "header could not be parsed");
1246 return status;
1247
1248 }
1249
1250 /* verify payload */
1251 status = ike_header->payload_interface.verify(&(ike_header->payload_interface));
1252 if (status != SUCCESS)
1253 {
1254 DBG1(DBG_ENC, "header verification failed");
1255 ike_header->destroy(ike_header);
1256 return status;
1257 }
1258
1259 if (this->ike_sa_id != NULL)
1260 {
1261 this->ike_sa_id->destroy(this->ike_sa_id);
1262 }
1263
1264 this->ike_sa_id = ike_sa_id_create(ike_header->get_initiator_spi(ike_header),
1265 ike_header->get_responder_spi(ike_header),
1266 ike_header->get_initiator_flag(ike_header));
1267
1268 this->exchange_type = ike_header->get_exchange_type(ike_header);
1269 this->message_id = ike_header->get_message_id(ike_header);
1270 this->is_request = (!(ike_header->get_response_flag(ike_header)));
1271 this->major_version = ike_header->get_maj_version(ike_header);
1272 this->minor_version = ike_header->get_min_version(ike_header);
1273 this->first_payload = ike_header->payload_interface.get_next_type(&(ike_header->payload_interface));
1274
1275 DBG2(DBG_ENC, "parsed a %N %s", exchange_type_names, this->exchange_type,
1276 this->is_request ? "request" : "response");
1277
1278 ike_header->destroy(ike_header);
1279
1280 /* get the rules for this messge */
1281 status = set_message_rule(this);
1282 if (status != SUCCESS)
1283 {
1284 DBG1(DBG_ENC, "no message rules specified for a %N %s",
1285 exchange_type_names, this->exchange_type,
1286 this->is_request ? "request" : "response");
1287 }
1288
1289 return status;
1290 }
1291
1292 /**
1293 * Implementation of private_message_t.decrypt_and_verify_payloads.
1294 */
1295 static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, signer_t* signer)
1296 {
1297 bool current_payload_was_encrypted = FALSE;
1298 payload_t *previous_payload = NULL;
1299 int payload_number = 1;
1300 iterator_t *iterator;
1301 payload_t *current_payload;
1302 status_t status;
1303
1304 iterator = this->payloads->create_iterator(this->payloads,TRUE);
1305
1306 /* process each payload and decrypt a encryption payload */
1307 while(iterator->iterate(iterator, (void**)&current_payload))
1308 {
1309 payload_rule_t *payload_rule;
1310 payload_type_t current_payload_type;
1311
1312 /* needed to check */
1313 current_payload_type = current_payload->get_type(current_payload);
1314
1315 DBG2(DBG_ENC, "process payload of type %N",
1316 payload_type_names, current_payload_type);
1317
1318 if (current_payload_type == ENCRYPTED)
1319 {
1320 encryption_payload_t *encryption_payload;
1321 payload_t *current_encrypted_payload;
1322
1323 encryption_payload = (encryption_payload_t*)current_payload;
1324
1325 DBG2(DBG_ENC, "found an encryption payload");
1326
1327 if (payload_number != this->payloads->get_count(this->payloads))
1328 {
1329 /* encrypted payload is not last one */
1330 DBG1(DBG_ENC, "encrypted payload is not last payload");
1331 iterator->destroy(iterator);
1332 return VERIFY_ERROR;
1333 }
1334 /* decrypt */
1335 encryption_payload->set_transforms(encryption_payload, crypter, signer);
1336 DBG2(DBG_ENC, "verify signature of encryption payload");
1337 status = encryption_payload->verify_signature(encryption_payload,
1338 this->packet->get_data(this->packet));
1339 if (status != SUCCESS)
1340 {
1341 DBG1(DBG_ENC, "encryption payload signature invalid");
1342 iterator->destroy(iterator);
1343 return FAILED;
1344 }
1345 DBG2(DBG_ENC, "decrypting content of encryption payload");
1346 status = encryption_payload->decrypt(encryption_payload);
1347 if (status != SUCCESS)
1348 {
1349 DBG1(DBG_ENC, "encrypted payload could not be decrypted and parsed");
1350 iterator->destroy(iterator);
1351 return PARSE_ERROR;
1352 }
1353
1354 /* needed later to find out if a payload was encrypted */
1355 current_payload_was_encrypted = TRUE;
1356
1357 /* check if there are payloads contained in the encryption payload */
1358 if (encryption_payload->get_payload_count(encryption_payload) == 0)
1359 {
1360 DBG2(DBG_ENC, "encrypted payload is empty");
1361 /* remove the encryption payload, is not needed anymore */
1362 iterator->remove(iterator);
1363 /* encrypted payload contains no other payload */
1364 current_payload_type = NO_PAYLOAD;
1365 }
1366 else
1367 {
1368 /* encryption_payload is replaced with first payload contained in encryption_payload */
1369 encryption_payload->remove_first_payload(encryption_payload, &current_encrypted_payload);
1370 iterator->replace(iterator,NULL,(void *) current_encrypted_payload);
1371 current_payload_type = current_encrypted_payload->get_type(current_encrypted_payload);
1372 }
1373
1374 /* is the current paylad the first in the message? */
1375 if (previous_payload == NULL)
1376 {
1377 /* yes, set the first payload type of the message to the current type */
1378 this->first_payload = current_payload_type;
1379 }
1380 else
1381 {
1382 /* no, set the next_type of the previous payload to the current type */
1383 previous_payload->set_next_type(previous_payload, current_payload_type);
1384 }
1385
1386 /* all encrypted payloads are added to the payload list */
1387 while (encryption_payload->get_payload_count(encryption_payload) > 0)
1388 {
1389 encryption_payload->remove_first_payload(encryption_payload, &current_encrypted_payload);
1390 DBG2(DBG_ENC, "insert unencrypted payload of type %N at end of list",
1391 payload_type_names, current_encrypted_payload->get_type(current_encrypted_payload));
1392 this->payloads->insert_last(this->payloads,current_encrypted_payload);
1393 }
1394
1395 /* encryption payload is processed, payloads are moved. Destroy it. */
1396 encryption_payload->destroy(encryption_payload);
1397 }
1398
1399 /* we allow unknown payloads of any type and don't bother if it was encrypted. Not our problem. */
1400 if (current_payload_type != UNKNOWN_PAYLOAD && current_payload_type != NO_PAYLOAD)
1401 {
1402 /* get the ruleset for found payload */
1403 status = get_payload_rule(this, current_payload_type, &payload_rule);
1404 if (status != SUCCESS)
1405 {
1406 /* payload is not allowed */
1407 DBG1(DBG_ENC, "payload type %N not allowed",
1408 payload_type_names, current_payload_type);
1409 iterator->destroy(iterator);
1410 return VERIFY_ERROR;
1411 }
1412
1413 /* check if the payload was encrypted, and if it should been have encrypted */
1414 if (payload_rule->encrypted != current_payload_was_encrypted)
1415 {
1416 /* payload was not encrypted, but should have been. or vice-versa */
1417 DBG1(DBG_ENC, "payload type %N should be %s!",
1418 payload_type_names, current_payload_type,
1419 (payload_rule->encrypted) ? "encrypted" : "not encrypted");
1420 iterator->destroy(iterator);
1421 return VERIFY_ERROR;
1422 }
1423 }
1424 /* advance to the next payload */
1425 payload_number++;
1426 /* is stored to set next payload in case of found encryption payload */
1427 previous_payload = current_payload;
1428 }
1429 iterator->destroy(iterator);
1430 return SUCCESS;
1431 }
1432
1433 /**
1434 * Implementation of private_message_t.verify.
1435 */
1436 static status_t verify(private_message_t *this)
1437 {
1438 int i;
1439 enumerator_t *enumerator;
1440 payload_t *current_payload;
1441 size_t total_found_payloads = 0;
1442
1443 DBG2(DBG_ENC, "verifying message structure");
1444
1445 /* check for payloads with wrong count*/
1446 for (i = 0; i < this->message_rule->payload_rule_count; i++)
1447 {
1448 size_t found_payloads = 0;
1449 payload_rule_t *rule;
1450
1451 rule = &this->message_rule->payload_rules[i];
1452 enumerator = create_payload_enumerator(this);
1453
1454 /* check all payloads for specific rule */
1455 while (enumerator->enumerate(enumerator, &current_payload))
1456 {
1457 payload_type_t current_payload_type;
1458 unknown_payload_t *unknown_payload;
1459
1460 current_payload_type = current_payload->get_type(current_payload);
1461 if (current_payload_type == UNKNOWN_PAYLOAD)
1462 {
1463 /* unknown payloads are ignored, IF they are not critical */
1464 unknown_payload = (unknown_payload_t*)current_payload;
1465 if (unknown_payload->is_critical(unknown_payload))
1466 {
1467 DBG1(DBG_ENC, "%N is not supported, but its critical!",
1468 payload_type_names, current_payload_type);
1469 enumerator->destroy(enumerator);
1470 return NOT_SUPPORTED;
1471 }
1472 }
1473 else if (current_payload_type == rule->payload_type)
1474 {
1475 found_payloads++;
1476 total_found_payloads++;
1477 DBG2(DBG_ENC, "found payload of type %N", payload_type_names,
1478 rule->payload_type);
1479
1480 /* as soon as ohe payload occures more then specified,
1481 * the verification fails */
1482 if (found_payloads >
1483 rule->max_occurence)
1484 {
1485 DBG1(DBG_ENC, "payload of type %N more than %d times (%d) "
1486 "occured in current message", payload_type_names,
1487 current_payload_type, rule->max_occurence,
1488 found_payloads);
1489 enumerator->destroy(enumerator);
1490 return VERIFY_ERROR;
1491 }
1492 }
1493 }
1494
1495 if (found_payloads < rule->min_occurence)
1496 {
1497 DBG1(DBG_ENC, "payload of type %N not occured %d times (%d)",
1498 payload_type_names, rule->payload_type, rule->min_occurence,
1499 found_payloads);
1500 enumerator->destroy(enumerator);
1501 return VERIFY_ERROR;
1502 }
1503 if (rule->sufficient &&
1504 this->payloads->get_count(this->payloads) == total_found_payloads)
1505 {
1506 enumerator->destroy(enumerator);
1507 return SUCCESS;
1508 }
1509 enumerator->destroy(enumerator);
1510 }
1511 return SUCCESS;
1512 }
1513
1514 /**
1515 * Implementation of message_t.parse_body.
1516 */
1517 static status_t parse_body(private_message_t *this, crypter_t *crypter, signer_t *signer)
1518 {
1519 status_t status = SUCCESS;
1520 payload_type_t current_payload_type;
1521 char str[256];
1522
1523 current_payload_type = this->first_payload;
1524
1525 DBG2(DBG_ENC, "parsing body of message, first payload is %N",
1526 payload_type_names, current_payload_type);
1527
1528 /* parse payload for payload, while there are more available */
1529 while ((current_payload_type != NO_PAYLOAD))
1530 {
1531 payload_t *current_payload;
1532
1533 DBG2(DBG_ENC, "starting parsing a %N payload",
1534 payload_type_names, current_payload_type);
1535
1536 /* parse current payload */
1537 status = this->parser->parse_payload(this->parser,current_payload_type,(payload_t **) &current_payload);
1538
1539 if (status != SUCCESS)
1540 {
1541 DBG1(DBG_ENC, "payload type %N could not be parsed",
1542 payload_type_names, current_payload_type);
1543 return PARSE_ERROR;
1544 }
1545
1546 DBG2(DBG_ENC, "verifying payload of type %N",
1547 payload_type_names, current_payload_type);
1548
1549 /* verify it, stop parsig if its invalid */
1550 status = current_payload->verify(current_payload);
1551 if (status != SUCCESS)
1552 {
1553 DBG1(DBG_ENC, "%N payload verification failed",
1554 payload_type_names, current_payload_type);
1555 current_payload->destroy(current_payload);
1556 return VERIFY_ERROR;
1557 }
1558
1559 DBG2(DBG_ENC, "%N payload verified. Adding to payload list",
1560 payload_type_names, current_payload_type);
1561 this->payloads->insert_last(this->payloads,current_payload);
1562
1563 /* an encryption payload is the last one, so STOP here. decryption is done later */
1564 if (current_payload_type == ENCRYPTED)
1565 {
1566 DBG2(DBG_ENC, "%N payload found. Stop parsing",
1567 payload_type_names, current_payload_type);
1568 break;
1569 }
1570
1571 /* get next payload type */
1572 current_payload_type = current_payload->get_next_type(current_payload);
1573 }
1574
1575 if (current_payload_type == ENCRYPTED)
1576 {
1577 status = decrypt_payloads(this,crypter,signer);
1578 if (status != SUCCESS)
1579 {
1580 DBG1(DBG_ENC, "could not decrypt payloads");
1581 return status;
1582 }
1583 }
1584
1585 status = verify(this);
1586 if (status != SUCCESS)
1587 {
1588 return status;
1589 }
1590
1591 DBG1(DBG_ENC, "parsed %s", get_string(this, str, sizeof(str)));
1592
1593 return SUCCESS;
1594 }
1595
1596 /**
1597 * Implementation of message_t.destroy.
1598 */
1599 static void destroy (private_message_t *this)
1600 {
1601 DESTROY_IF(this->ike_sa_id);
1602 this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
1603 this->packet->destroy(this->packet);
1604 this->parser->destroy(this->parser);
1605 free(this);
1606 }
1607
1608 /*
1609 * Described in Header-File
1610 */
1611 message_t *message_create_from_packet(packet_t *packet)
1612 {
1613 private_message_t *this = malloc_thing(private_message_t);
1614
1615 /* public functions */
1616 this->public.set_major_version = (void(*)(message_t*, u_int8_t))set_major_version;
1617 this->public.get_major_version = (u_int8_t(*)(message_t*))get_major_version;
1618 this->public.set_minor_version = (void(*)(message_t*, u_int8_t))set_minor_version;
1619 this->public.get_minor_version = (u_int8_t(*)(message_t*))get_minor_version;
1620 this->public.set_message_id = (void(*)(message_t*, u_int32_t))set_message_id;
1621 this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
1622 this->public.get_initiator_spi = (u_int64_t(*)(message_t*))get_initiator_spi;
1623 this->public.get_responder_spi = (u_int64_t(*)(message_t*))get_responder_spi;
1624 this->public.set_ike_sa_id = (void(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
1625 this->public.get_ike_sa_id = (ike_sa_id_t*(*)(message_t*))get_ike_sa_id;
1626 this->public.set_exchange_type = (void(*)(message_t*, exchange_type_t))set_exchange_type;
1627 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
1628 this->public.get_first_payload_type = (payload_type_t(*)(message_t*))get_first_payload_type;
1629 this->public.set_request = (void(*)(message_t*, bool))set_request;
1630 this->public.get_request = (bool(*)(message_t*))get_request;
1631 this->public.add_payload = (void(*)(message_t*,payload_t*))add_payload;
1632 this->public.add_notify = (void(*)(message_t*,bool,notify_type_t,chunk_t))add_notify;
1633 this->public.generate = (status_t (*) (message_t *,crypter_t*,signer_t*,packet_t**)) generate;
1634 this->public.set_source = (void (*) (message_t*,host_t*)) set_source;
1635 this->public.get_source = (host_t * (*) (message_t*)) get_source;
1636 this->public.set_destination = (void (*) (message_t*,host_t*)) set_destination;
1637 this->public.get_destination = (host_t * (*) (message_t*)) get_destination;
1638 this->public.create_payload_enumerator = (enumerator_t * (*) (message_t *)) create_payload_enumerator;
1639 this->public.get_payload = (payload_t * (*) (message_t *, payload_type_t)) get_payload;
1640 this->public.get_notify = (notify_payload_t*(*)(message_t*, notify_type_t type))get_notify;
1641 this->public.parse_header = (status_t (*) (message_t *)) parse_header;
1642 this->public.parse_body = (status_t (*) (message_t *,crypter_t*,signer_t*)) parse_body;
1643 this->public.get_packet = (packet_t * (*) (message_t*)) get_packet;
1644 this->public.get_packet_data = (chunk_t (*) (message_t *this)) get_packet_data;
1645 this->public.destroy = (void(*)(message_t*))destroy;
1646
1647 /* private values */
1648 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
1649 this->is_request = TRUE;
1650 this->ike_sa_id = NULL;
1651 this->first_payload = NO_PAYLOAD;
1652 this->message_id = 0;
1653
1654 /* private values */
1655 if (packet == NULL)
1656 {
1657 packet = packet_create();
1658 }
1659 this->message_rule = NULL;
1660 this->packet = packet;
1661 this->payloads = linked_list_create();
1662
1663 /* parser is created from data of packet */
1664 this->parser = parser_create(this->packet->get_data(this->packet));
1665
1666 return (&this->public);
1667 }
1668
1669 /*
1670 * Described in Header.
1671 */
1672 message_t *message_create()
1673 {
1674 return message_create_from_packet(NULL);
1675 }
1676