fixed ID selection bug when peer doesn't include IDr payload
[strongswan.git] / src / charon / encoding / message.c
1 /**
2 * @file message.c
3 *
4 * @brief Implementation of message_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <printf.h>
28
29 #include "message.h"
30
31 #include <library.h>
32 #include <daemon.h>
33 #include <sa/ike_sa_id.h>
34 #include <encoding/generator.h>
35 #include <encoding/parser.h>
36 #include <utils/linked_list.h>
37 #include <encoding/payloads/encodings.h>
38 #include <encoding/payloads/payload.h>
39 #include <encoding/payloads/encryption_payload.h>
40 #include <encoding/payloads/unknown_payload.h>
41
42 /**
43 * Max number of notify payloads per IKEv2 Message
44 */
45 #define MAX_NOTIFY_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 message_rule_t message_rule_t;
86
87 /**
88 * A message rule defines the kind of a message,
89 * if it has encrypted contents and a list
90 * of payload rules.
91 *
92 */
93 struct message_rule_t {
94 /**
95 * Type of message.
96 */
97 exchange_type_t exchange_type;
98
99 /**
100 * Is message a request or response.
101 */
102 bool is_request;
103
104 /**
105 * Message contains encrypted content.
106 */
107 bool encrypted_content;
108
109 /**
110 * Number of payload rules which will follow
111 */
112 size_t payload_rule_count;
113
114 /**
115 * Pointer to first payload rule
116 */
117 payload_rule_t *payload_rules;
118 };
119
120 /**
121 * Message rule for IKE_SA_INIT from initiator.
122 */
123 static payload_rule_t ike_sa_init_i_payload_rules[] = {
124 {NOTIFY,0,MAX_NOTIFY_PAYLOADS,FALSE,FALSE},
125 {SECURITY_ASSOCIATION,1,1,FALSE,FALSE},
126 {KEY_EXCHANGE,1,1,FALSE,FALSE},
127 {NONCE,1,1,FALSE,FALSE},
128 {VENDOR_ID,0,10,FALSE,FALSE},
129 };
130
131 /**
132 * Message rule for IKE_SA_INIT from responder.
133 */
134 static payload_rule_t ike_sa_init_r_payload_rules[] = {
135 {NOTIFY,0,MAX_NOTIFY_PAYLOADS,FALSE,TRUE},
136 {SECURITY_ASSOCIATION,1,1,FALSE,FALSE},
137 {KEY_EXCHANGE,1,1,FALSE,FALSE},
138 {NONCE,1,1,FALSE,FALSE},
139 {VENDOR_ID,0,10,FALSE,FALSE},
140 };
141
142 /**
143 * Message rule for IKE_AUTH from initiator.
144 */
145 static payload_rule_t ike_auth_i_payload_rules[] = {
146 {NOTIFY,0,MAX_NOTIFY_PAYLOADS,TRUE,FALSE},
147 {EXTENSIBLE_AUTHENTICATION,0,1,TRUE,TRUE},
148 {AUTHENTICATION,0,1,TRUE,TRUE},
149 {ID_INITIATOR,1,1,TRUE,FALSE},
150 {CERTIFICATE,0,1,TRUE,FALSE},
151 {CERTIFICATE_REQUEST,0,1,TRUE,FALSE},
152 {ID_RESPONDER,0,1,TRUE,FALSE},
153 {SECURITY_ASSOCIATION,1,1,TRUE,FALSE},
154 {TRAFFIC_SELECTOR_INITIATOR,1,1,TRUE,FALSE},
155 {TRAFFIC_SELECTOR_RESPONDER,1,1,TRUE,FALSE},
156 {CONFIGURATION,0,1,TRUE,FALSE},
157 {VENDOR_ID,0,10,TRUE,FALSE},
158 };
159
160 /**
161 * Message rule for IKE_AUTH from responder.
162 */
163 static payload_rule_t ike_auth_r_payload_rules[] = {
164 {NOTIFY,0,MAX_NOTIFY_PAYLOADS,TRUE,TRUE},
165 {EXTENSIBLE_AUTHENTICATION,0,1,TRUE,TRUE},
166 {CERTIFICATE,0,1,TRUE,FALSE},
167 {ID_RESPONDER,0,1,TRUE,FALSE},
168 {AUTHENTICATION,0,1,TRUE,FALSE},
169 {SECURITY_ASSOCIATION,0,1,TRUE,FALSE},
170 {TRAFFIC_SELECTOR_INITIATOR,0,1,TRUE,FALSE},
171 {TRAFFIC_SELECTOR_RESPONDER,0,1,TRUE,FALSE},
172 {CONFIGURATION,0,1,TRUE,FALSE},
173 {VENDOR_ID,0,10,TRUE,FALSE},
174 };
175
176
177 /**
178 * Message rule for INFORMATIONAL from initiator.
179 */
180 static payload_rule_t informational_i_payload_rules[] = {
181 {NOTIFY,0,MAX_NOTIFY_PAYLOADS,TRUE,FALSE},
182 {CONFIGURATION,0,1,TRUE,FALSE},
183 {DELETE,0,1,TRUE,FALSE},
184 {VENDOR_ID,0,10,TRUE,FALSE},
185
186 };
187
188 /**
189 * Message rule for INFORMATIONAL from responder.
190 */
191 static payload_rule_t informational_r_payload_rules[] = {
192 {NOTIFY,0,MAX_NOTIFY_PAYLOADS,TRUE,FALSE},
193 {CONFIGURATION,0,1,TRUE,FALSE},
194 {DELETE,0,1,TRUE,FALSE},
195 {VENDOR_ID,0,10,TRUE,FALSE},
196 };
197
198 /**
199 * Message rule for CREATE_CHILD_SA from initiator.
200 */
201 static payload_rule_t create_child_sa_i_payload_rules[] = {
202 {NOTIFY,0,MAX_NOTIFY_PAYLOADS,TRUE,FALSE},
203 {SECURITY_ASSOCIATION,1,1,TRUE,FALSE},
204 {NONCE,1,1,TRUE,FALSE},
205 {KEY_EXCHANGE,0,1,TRUE,FALSE},
206 {TRAFFIC_SELECTOR_INITIATOR,0,1,TRUE,FALSE},
207 {TRAFFIC_SELECTOR_RESPONDER,0,1,TRUE,FALSE},
208 {CONFIGURATION,0,1,TRUE,FALSE},
209 {VENDOR_ID,0,10,TRUE,FALSE},
210 };
211
212 /**
213 * Message rule for CREATE_CHILD_SA from responder.
214 */
215 static payload_rule_t create_child_sa_r_payload_rules[] = {
216 {NOTIFY,0,MAX_NOTIFY_PAYLOADS,TRUE,TRUE},
217 {SECURITY_ASSOCIATION,1,1,TRUE,FALSE},
218 {NONCE,1,1,TRUE,FALSE},
219 {KEY_EXCHANGE,0,1,TRUE,FALSE},
220 {TRAFFIC_SELECTOR_INITIATOR,0,1,TRUE,FALSE},
221 {TRAFFIC_SELECTOR_RESPONDER,0,1,TRUE,FALSE},
222 {CONFIGURATION,0,1,TRUE,FALSE},
223 {VENDOR_ID,0,10,TRUE,FALSE},
224 };
225
226
227 /**
228 * Message rules, defines allowed payloads.
229 */
230 static message_rule_t message_rules[] = {
231 {IKE_SA_INIT,TRUE,FALSE,(sizeof(ike_sa_init_i_payload_rules)/sizeof(payload_rule_t)),ike_sa_init_i_payload_rules},
232 {IKE_SA_INIT,FALSE,FALSE,(sizeof(ike_sa_init_r_payload_rules)/sizeof(payload_rule_t)),ike_sa_init_r_payload_rules},
233 {IKE_AUTH,TRUE,TRUE,(sizeof(ike_auth_i_payload_rules)/sizeof(payload_rule_t)),ike_auth_i_payload_rules},
234 {IKE_AUTH,FALSE,TRUE,(sizeof(ike_auth_r_payload_rules)/sizeof(payload_rule_t)),ike_auth_r_payload_rules},
235 {INFORMATIONAL,TRUE,TRUE,(sizeof(informational_i_payload_rules)/sizeof(payload_rule_t)),informational_i_payload_rules},
236 {INFORMATIONAL,FALSE,TRUE,(sizeof(informational_r_payload_rules)/sizeof(payload_rule_t)),informational_r_payload_rules},
237 {CREATE_CHILD_SA,TRUE,TRUE,(sizeof(create_child_sa_i_payload_rules)/sizeof(payload_rule_t)),create_child_sa_i_payload_rules},
238 {CREATE_CHILD_SA,FALSE,TRUE,(sizeof(create_child_sa_r_payload_rules)/sizeof(payload_rule_t)),create_child_sa_r_payload_rules},
239 };
240
241
242 typedef struct private_message_t private_message_t;
243
244 /**
245 * Private data of an message_t object.
246 */
247 struct private_message_t {
248
249 /**
250 * Public part of a message_t object.
251 */
252 message_t public;
253
254 /**
255 * Minor version of message.
256 */
257 u_int8_t major_version;
258
259 /**
260 * Major version of message.
261 */
262 u_int8_t minor_version;
263
264 /**
265 * First Payload in message.
266 */
267 payload_type_t first_payload;
268
269 /**
270 * Assigned exchange type.
271 */
272 exchange_type_t exchange_type;
273
274 /**
275 * TRUE if message is a request, FALSE if a reply.
276 */
277 bool is_request;
278
279 /**
280 * Message ID of this message.
281 */
282 u_int32_t message_id;
283
284 /**
285 * ID of assigned IKE_SA.
286 */
287 ike_sa_id_t *ike_sa_id;
288
289 /**
290 * Assigned UDP packet, stores incoming packet or last generated one.
291 */
292 packet_t *packet;
293
294 /**
295 * Linked List where payload data are stored in.
296 */
297 linked_list_t *payloads;
298
299 /**
300 * Assigned parser to parse Header and Body of this message.
301 */
302 parser_t *parser;
303
304 /**
305 * The message rule for this message instance
306 */
307 message_rule_t *message_rule;
308 };
309
310 /**
311 * Implementation of private_message_t.set_message_rule.
312 */
313 static status_t set_message_rule(private_message_t *this)
314 {
315 int i;
316
317 for (i = 0; i < (sizeof(message_rules) / sizeof(message_rule_t)); i++)
318 {
319 if ((this->exchange_type == message_rules[i].exchange_type) &&
320 (this->is_request == message_rules[i].is_request))
321 {
322 /* found rule for given exchange_type*/
323 this->message_rule = &(message_rules[i]);
324 return SUCCESS;
325 }
326 }
327 this->message_rule = NULL;
328 return NOT_FOUND;
329 }
330
331 /**
332 * Implementation of private_message_t.get_payload_rule.
333 */
334 static status_t get_payload_rule(private_message_t *this, payload_type_t payload_type, payload_rule_t **payload_rule)
335 {
336 int i;
337
338 for (i = 0; i < this->message_rule->payload_rule_count;i++)
339 {
340 if (this->message_rule->payload_rules[i].payload_type == payload_type)
341 {
342 *payload_rule = &(this->message_rule->payload_rules[i]);
343 return SUCCESS;
344 }
345 }
346
347 *payload_rule = NULL;
348 return NOT_FOUND;
349 }
350
351 /**
352 * Implementation of message_t.set_ike_sa_id.
353 */
354 static void set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id)
355 {
356 DESTROY_IF(this->ike_sa_id);
357 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
358 }
359
360 /**
361 * Implementation of message_t.get_ike_sa_id.
362 */
363 static ike_sa_id_t* get_ike_sa_id (private_message_t *this)
364 {
365 return this->ike_sa_id;
366 }
367
368 /**
369 * Implementation of message_t.set_message_id.
370 */
371 static void set_message_id (private_message_t *this,u_int32_t message_id)
372 {
373 this->message_id = message_id;
374 }
375
376 /**
377 * Implementation of message_t.get_message_id.
378 */
379 static u_int32_t get_message_id (private_message_t *this)
380 {
381 return this->message_id;
382 }
383
384 /**
385 * Implementation of message_t.get_initiator_spi.
386 */
387 static u_int64_t get_initiator_spi (private_message_t *this)
388 {
389 return (this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
390 }
391
392 /**
393 * Implementation of message_t.get_responder_spi.
394 */
395 static u_int64_t get_responder_spi (private_message_t *this)
396 {
397 return (this->ike_sa_id->get_responder_spi(this->ike_sa_id));
398 }
399
400 /**
401 * Implementation of message_t.set_major_version.
402 */
403 static void set_major_version (private_message_t *this,u_int8_t major_version)
404 {
405 this->major_version = major_version;
406 }
407
408
409 /**
410 * Implementation of message_t.set_major_version.
411 */
412 static u_int8_t get_major_version (private_message_t *this)
413 {
414 return this->major_version;
415 }
416
417 /**
418 * Implementation of message_t.set_minor_version.
419 */
420 static void set_minor_version (private_message_t *this,u_int8_t minor_version)
421 {
422 this->minor_version = minor_version;
423 }
424
425 /**
426 * Implementation of message_t.get_minor_version.
427 */
428 static u_int8_t get_minor_version (private_message_t *this)
429 {
430 return this->minor_version;
431 }
432
433 /**
434 * Implementation of message_t.set_exchange_type.
435 */
436 static void set_exchange_type (private_message_t *this,exchange_type_t exchange_type)
437 {
438 this->exchange_type = exchange_type;
439 }
440
441 /**
442 * Implementation of message_t.get_exchange_type.
443 */
444 static exchange_type_t get_exchange_type (private_message_t *this)
445 {
446 return this->exchange_type;
447 }
448
449 /**
450 * Implementation of message_t.set_request.
451 */
452 static void set_request (private_message_t *this,bool request)
453 {
454 this->is_request = request;
455 }
456
457 /**
458 * Implementation of message_t.get_request.
459 */
460 static exchange_type_t get_request (private_message_t *this)
461 {
462 return this->is_request;
463 }
464
465 /**
466 * Is this message in an encoded form?
467 */
468 static bool is_encoded(private_message_t *this)
469 {
470 chunk_t data = this->packet->get_data(this->packet);
471
472 if (data.ptr == NULL)
473 {
474 return FALSE;
475 }
476 return TRUE;
477 }
478
479 /**
480 * Implementation of message_t.add_payload.
481 */
482 static void add_payload(private_message_t *this, payload_t *payload)
483 {
484 payload_t *last_payload;
485 if (this->payloads->get_count(this->payloads) > 0)
486 {
487 this->payloads->get_last(this->payloads,(void **) &last_payload);
488 last_payload->set_next_type(last_payload, payload->get_type(payload));
489 }
490 else
491 {
492 this->first_payload = payload->get_type(payload);
493 }
494 payload->set_next_type(payload, NO_PAYLOAD);
495 this->payloads->insert_last(this->payloads, (void*)payload);
496
497 DBG2(DBG_ENC ,"added payload of type %N to message",
498 payload_type_names, payload->get_type(payload));
499 }
500
501 /**
502 * Implementation of message_t.add_notify.
503 */
504 static void add_notify(private_message_t *this, bool flush, notify_type_t type,
505 chunk_t data)
506 {
507 notify_payload_t *notify;
508 payload_t *payload;
509
510 if (flush)
511 {
512 while (this->payloads->remove_last(this->payloads,
513 (void**)&payload) == SUCCESS)
514 {
515 payload->destroy(payload);
516 }
517 }
518 notify = notify_payload_create();
519 notify->set_notify_type(notify, type);
520 notify->set_notification_data(notify, data);
521 add_payload(this, (payload_t*)notify);
522 }
523
524 /**
525 * Implementation of message_t.set_source.
526 */
527 static void set_source(private_message_t *this, host_t *host)
528 {
529 this->packet->set_source(this->packet, host);
530 }
531
532 /**
533 * Implementation of message_t.set_destination.
534 */
535 static void set_destination(private_message_t *this, host_t *host)
536 {
537 this->packet->set_destination(this->packet, host);
538 }
539
540 /**
541 * Implementation of message_t.get_source.
542 */
543 static host_t* get_source(private_message_t *this)
544 {
545 return this->packet->get_source(this->packet);
546 }
547
548 /**
549 * Implementation of message_t.get_destination.
550 */
551 static host_t * get_destination(private_message_t *this)
552 {
553 return this->packet->get_destination(this->packet);
554 }
555
556 /**
557 * Implementation of message_t.get_payload_iterator.
558 */
559 static iterator_t *get_payload_iterator(private_message_t *this)
560 {
561 return this->payloads->create_iterator(this->payloads, TRUE);
562 }
563
564 /**
565 * Implementation of message_t.get_payload.
566 */
567 static payload_t *get_payload(private_message_t *this, payload_type_t type)
568 {
569 payload_t *current, *found = NULL;
570 iterator_t *iterator;
571
572 iterator = this->payloads->create_iterator(this->payloads, TRUE);
573 while (iterator->iterate(iterator, (void**)&current))
574 {
575 if (current->get_type(current) == type)
576 {
577 found = current;
578 break;
579 }
580 }
581 iterator->destroy(iterator);
582 return found;
583 }
584
585 /**
586 * output handler in printf()
587 */
588 static int print(FILE *stream, const struct printf_info *info,
589 const void *const *args)
590 {
591 private_message_t *this = *((private_message_t**)(args[0]));
592 iterator_t *iterator;
593 payload_t *payload;
594 bool first = TRUE;
595 size_t total_written = 0;
596 size_t written;
597
598 if (this == NULL)
599 {
600 return fprintf(stream, "(null)");
601 }
602
603 written = fprintf(stream, "%N %s [",
604 exchange_type_names, this->exchange_type,
605 this->is_request ? "request" : "response");
606 if (written < 0)
607 {
608 return written;
609 }
610 total_written += written;
611
612 iterator = this->payloads->create_iterator(this->payloads, TRUE);
613 while (iterator->iterate(iterator, (void**)&payload))
614 {
615 if (!first)
616 {
617 written = fprintf(stream, " ");
618 if (written < 0)
619 {
620 return written;
621 }
622 total_written += written;
623 }
624 else
625 {
626 first = FALSE;
627 }
628 written = fprintf(stream, "%N", payload_type_short_names,
629 payload->get_type(payload));
630 if (written < 0)
631 {
632 return written;
633 }
634 total_written += written;
635 }
636 iterator->destroy(iterator);
637 written = fprintf(stream, "]");
638 if (written < 0)
639 {
640 return written;
641 }
642 total_written += written;
643 return total_written;
644 }
645
646 /**
647 * register printf() handlers
648 */
649 static void __attribute__ ((constructor))print_register()
650 {
651 register_printf_function(PRINTF_MESSAGE, print, arginfo_ptr);
652 }
653
654 /**
655 * Implementation of private_message_t.encrypt_payloads.
656 */
657 static status_t encrypt_payloads (private_message_t *this,crypter_t *crypter, signer_t* signer)
658 {
659 encryption_payload_t *encryption_payload = NULL;
660 status_t status;
661 linked_list_t *all_payloads;
662
663 if (!this->message_rule->encrypted_content)
664 {
665 DBG2(DBG_ENC, "message doesn't have to be encrypted");
666 /* message contains no content to encrypt */
667 return SUCCESS;
668 }
669
670 DBG2(DBG_ENC, "copy all payloads to a temporary list");
671 all_payloads = linked_list_create();
672
673 /* first copy all payloads in a temporary list */
674 while (this->payloads->get_count(this->payloads) > 0)
675 {
676 void *current_payload;
677 this->payloads->remove_first(this->payloads,&current_payload);
678 all_payloads->insert_last(all_payloads,current_payload);
679 }
680
681 encryption_payload = encryption_payload_create();
682
683 DBG2(DBG_ENC, "check each payloads if they have to get encrypted");
684 while (all_payloads->get_count(all_payloads) > 0)
685 {
686 payload_rule_t *payload_rule;
687 payload_t *current_payload;
688 bool to_encrypt = FALSE;
689
690 all_payloads->remove_first(all_payloads,(void **)&current_payload);
691
692 status = get_payload_rule(this,
693 current_payload->get_type(current_payload),&payload_rule);
694 /* for payload types which are not found in supported payload list,
695 * it is presumed that they don't have to be encrypted */
696 if ((status == SUCCESS) && (payload_rule->encrypted))
697 {
698 DBG2(DBG_ENC, "payload %N gets encrypted",
699 payload_type_names, current_payload->get_type(current_payload));
700 to_encrypt = TRUE;
701 }
702
703 if (to_encrypt)
704 {
705 DBG2(DBG_ENC, "insert payload %N to encryption payload",
706 payload_type_names, current_payload->get_type(current_payload));
707 encryption_payload->add_payload(encryption_payload,current_payload);
708 }
709 else
710 {
711 DBG2(DBG_ENC, "insert payload %N unencrypted",
712 payload_type_names ,current_payload->get_type(current_payload));
713 add_payload(this, (payload_t*)encryption_payload);
714 }
715 }
716
717 status = SUCCESS;
718 DBG2(DBG_ENC, "encrypting encryption payload");
719 encryption_payload->set_transforms(encryption_payload, crypter,signer);
720 status = encryption_payload->encrypt(encryption_payload);
721 DBG2(DBG_ENC, "add encrypted payload to payload list");
722 add_payload(this, (payload_t*)encryption_payload);
723
724 all_payloads->destroy(all_payloads);
725
726 return status;
727 }
728
729 /**
730 * Implementation of message_t.generate.
731 */
732 static status_t generate(private_message_t *this, crypter_t *crypter, signer_t* signer, packet_t **packet)
733 {
734 generator_t *generator;
735 ike_header_t *ike_header;
736 payload_t *payload, *next_payload;
737 iterator_t *iterator;
738 status_t status;
739 chunk_t packet_data;
740
741 if (is_encoded(this))
742 {
743 /* already generated, return a new packet clone */
744 *packet = this->packet->clone(this->packet);
745 return SUCCESS;
746 }
747
748 DBG1(DBG_ENC, "generating %M", this);
749
750 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
751 {
752 DBG1(DBG_ENC, "exchange type is not defined");
753 return INVALID_STATE;
754 }
755
756 if (this->packet->get_source(this->packet) == NULL ||
757 this->packet->get_destination(this->packet) == NULL)
758 {
759 DBG1(DBG_ENC, "%s not defined",
760 !this->packet->get_source(this->packet) ? "source" : "destination");
761 return INVALID_STATE;
762 }
763
764 /* set the rules for this messge */
765 status = set_message_rule(this);
766 if (status != SUCCESS)
767 {
768 DBG1(DBG_ENC, "no message rules specified for this message type");
769 return NOT_SUPPORTED;
770 }
771
772 /* going to encrypt all content which have to be encrypted */
773 status = encrypt_payloads(this, crypter, signer);
774 if (status != SUCCESS)
775 {
776 DBG1(DBG_ENC, "payload encryption failed");
777 return status;
778 }
779
780 /* build ike header */
781 ike_header = ike_header_create();
782
783 ike_header->set_exchange_type(ike_header, this->exchange_type);
784 ike_header->set_message_id(ike_header, this->message_id);
785 ike_header->set_response_flag(ike_header, !this->is_request);
786 ike_header->set_initiator_flag(ike_header, this->ike_sa_id->is_initiator(this->ike_sa_id));
787 ike_header->set_initiator_spi(ike_header, this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
788 ike_header->set_responder_spi(ike_header, this->ike_sa_id->get_responder_spi(this->ike_sa_id));
789
790 generator = generator_create();
791
792 payload = (payload_t*)ike_header;
793
794
795 /* generate every payload expect last one, this is doen later*/
796 iterator = this->payloads->create_iterator(this->payloads, TRUE);
797 while(iterator->iterate(iterator, (void**)&next_payload))
798 {
799 payload->set_next_type(payload, next_payload->get_type(next_payload));
800 generator->generate_payload(generator, payload);
801 payload = next_payload;
802 }
803 iterator->destroy(iterator);
804
805 /* last payload has no next payload*/
806 payload->set_next_type(payload, NO_PAYLOAD);
807
808 generator->generate_payload(generator, payload);
809
810 ike_header->destroy(ike_header);
811
812 /* build packet */
813 generator->write_to_chunk(generator, &packet_data);
814 generator->destroy(generator);
815
816 /* if last payload is of type encrypted, integrity checksum if necessary */
817 if (payload->get_type(payload) == ENCRYPTED)
818 {
819 DBG2(DBG_ENC, "build signature on whole message");
820 encryption_payload_t *encryption_payload = (encryption_payload_t*)payload;
821 status = encryption_payload->build_signature(encryption_payload, packet_data);
822 if (status != SUCCESS)
823 {
824 return status;
825 }
826 }
827
828 this->packet->set_data(this->packet, packet_data);
829
830 /* clone packet for caller */
831 *packet = this->packet->clone(this->packet);
832
833 DBG2(DBG_ENC, "message generated successfully");
834 return SUCCESS;
835 }
836
837 /**
838 * Implementation of message_t.get_packet.
839 */
840 static packet_t *get_packet (private_message_t *this)
841 {
842 if (this->packet == NULL)
843 {
844 return NULL;
845 }
846 return this->packet->clone(this->packet);
847 }
848
849 /**
850 * Implementation of message_t.get_packet_data.
851 */
852 static chunk_t get_packet_data (private_message_t *this)
853 {
854 if (this->packet == NULL)
855 {
856 return chunk_empty;
857 }
858 return chunk_clone(this->packet->get_data(this->packet));
859 }
860
861 /**
862 * Implementation of message_t.parse_header.
863 */
864 static status_t parse_header(private_message_t *this)
865 {
866 ike_header_t *ike_header;
867 status_t status;
868
869 DBG2(DBG_ENC, "parsing header of message");
870
871 this->parser->reset_context(this->parser);
872 status = this->parser->parse_payload(this->parser,HEADER,(payload_t **) &ike_header);
873 if (status != SUCCESS)
874 {
875 DBG1(DBG_ENC, "header could not be parsed");
876 return status;
877
878 }
879
880 /* verify payload */
881 status = ike_header->payload_interface.verify(&(ike_header->payload_interface));
882 if (status != SUCCESS)
883 {
884 DBG1(DBG_ENC, "header verification failed");
885 ike_header->destroy(ike_header);
886 return status;
887 }
888
889 if (this->ike_sa_id != NULL)
890 {
891 this->ike_sa_id->destroy(this->ike_sa_id);
892 }
893
894 this->ike_sa_id = ike_sa_id_create(ike_header->get_initiator_spi(ike_header),
895 ike_header->get_responder_spi(ike_header),
896 ike_header->get_initiator_flag(ike_header));
897
898 this->exchange_type = ike_header->get_exchange_type(ike_header);
899 this->message_id = ike_header->get_message_id(ike_header);
900 this->is_request = (!(ike_header->get_response_flag(ike_header)));
901 this->major_version = ike_header->get_maj_version(ike_header);
902 this->minor_version = ike_header->get_min_version(ike_header);
903 this->first_payload = ike_header->payload_interface.get_next_type(&(ike_header->payload_interface));
904
905 DBG2(DBG_ENC, "parsed a %N %s", exchange_type_names, this->exchange_type,
906 this->is_request ? "request" : "response");
907
908 ike_header->destroy(ike_header);
909
910 /* get the rules for this messge */
911 status = set_message_rule(this);
912 if (status != SUCCESS)
913 {
914 DBG1(DBG_ENC, "no message rules specified for a %N %s",
915 exchange_type_names, this->exchange_type,
916 this->is_request ? "request" : "response");
917 }
918
919 return status;
920 }
921
922 /**
923 * Implementation of private_message_t.decrypt_and_verify_payloads.
924 */
925 static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, signer_t* signer)
926 {
927 bool current_payload_was_encrypted = FALSE;
928 payload_t *previous_payload = NULL;
929 int payload_number = 1;
930 iterator_t *iterator;
931 payload_t *current_payload;
932 status_t status;
933
934 iterator = this->payloads->create_iterator(this->payloads,TRUE);
935
936 /* process each payload and decrypt a encryption payload */
937 while(iterator->iterate(iterator, (void**)&current_payload))
938 {
939 payload_rule_t *payload_rule;
940 payload_type_t current_payload_type;
941
942 /* needed to check */
943 current_payload_type = current_payload->get_type(current_payload);
944
945 DBG2(DBG_ENC, "process payload of type %N",
946 payload_type_names, current_payload_type);
947
948 if (current_payload_type == ENCRYPTED)
949 {
950 encryption_payload_t *encryption_payload;
951 payload_t *current_encrypted_payload;
952
953 encryption_payload = (encryption_payload_t*)current_payload;
954
955 DBG2(DBG_ENC, "found an encryption payload");
956
957 if (payload_number != this->payloads->get_count(this->payloads))
958 {
959 /* encrypted payload is not last one */
960 DBG1(DBG_ENC, "encrypted payload is not last payload");
961 iterator->destroy(iterator);
962 return VERIFY_ERROR;
963 }
964 /* decrypt */
965 encryption_payload->set_transforms(encryption_payload, crypter, signer);
966 DBG2(DBG_ENC, "verify signature of encryption payload");
967 status = encryption_payload->verify_signature(encryption_payload,
968 this->packet->get_data(this->packet));
969 if (status != SUCCESS)
970 {
971 DBG1(DBG_ENC, "encryption payload signature invalid");
972 iterator->destroy(iterator);
973 return FAILED;
974 }
975 DBG2(DBG_ENC, "decrypting content of encryption payload");
976 status = encryption_payload->decrypt(encryption_payload);
977 if (status != SUCCESS)
978 {
979 DBG1(DBG_ENC, "encrypted payload could not be decrypted and parsed");
980 iterator->destroy(iterator);
981 return PARSE_ERROR;
982 }
983
984 /* needed later to find out if a payload was encrypted */
985 current_payload_was_encrypted = TRUE;
986
987 /* check if there are payloads contained in the encryption payload */
988 if (encryption_payload->get_payload_count(encryption_payload) == 0)
989 {
990 DBG2(DBG_ENC, "encrypted payload is empty");
991 /* remove the encryption payload, is not needed anymore */
992 iterator->remove(iterator);
993 /* encrypted payload contains no other payload */
994 current_payload_type = NO_PAYLOAD;
995 }
996 else
997 {
998 /* encryption_payload is replaced with first payload contained in encryption_payload */
999 encryption_payload->remove_first_payload(encryption_payload, &current_encrypted_payload);
1000 iterator->replace(iterator,NULL,(void *) current_encrypted_payload);
1001 current_payload_type = current_encrypted_payload->get_type(current_encrypted_payload);
1002 }
1003
1004 /* is the current paylad the first in the message? */
1005 if (previous_payload == NULL)
1006 {
1007 /* yes, set the first payload type of the message to the current type */
1008 this->first_payload = current_payload_type;
1009 }
1010 else
1011 {
1012 /* no, set the next_type of the previous payload to the current type */
1013 previous_payload->set_next_type(previous_payload, current_payload_type);
1014 }
1015
1016 /* all encrypted payloads are added to the payload list */
1017 while (encryption_payload->get_payload_count(encryption_payload) > 0)
1018 {
1019 encryption_payload->remove_first_payload(encryption_payload, &current_encrypted_payload);
1020 DBG2(DBG_ENC, "insert unencrypted payload of type %N at end of list",
1021 payload_type_names, current_encrypted_payload->get_type(current_encrypted_payload));
1022 this->payloads->insert_last(this->payloads,current_encrypted_payload);
1023 }
1024
1025 /* encryption payload is processed, payloads are moved. Destroy it. */
1026 encryption_payload->destroy(encryption_payload);
1027 }
1028
1029 /* we allow unknown payloads of any type and don't bother if it was encrypted. Not our problem. */
1030 if (current_payload_type != UNKNOWN_PAYLOAD && current_payload_type != NO_PAYLOAD)
1031 {
1032 /* get the ruleset for found payload */
1033 status = get_payload_rule(this, current_payload_type, &payload_rule);
1034 if (status != SUCCESS)
1035 {
1036 /* payload is not allowed */
1037 DBG1(DBG_ENC, "payload type %N not allowed",
1038 payload_type_names, current_payload_type);
1039 iterator->destroy(iterator);
1040 return VERIFY_ERROR;
1041 }
1042
1043 /* check if the payload was encrypted, and if it should been have encrypted */
1044 if (payload_rule->encrypted != current_payload_was_encrypted)
1045 {
1046 /* payload was not encrypted, but should have been. or vice-versa */
1047 DBG1(DBG_ENC, "payload type %N should be %s!",
1048 payload_type_names, current_payload_type,
1049 (payload_rule->encrypted) ? "encrypted" : "not encrypted");
1050 iterator->destroy(iterator);
1051 return VERIFY_ERROR;
1052 }
1053 }
1054 /* advance to the next payload */
1055 payload_number++;
1056 /* is stored to set next payload in case of found encryption payload */
1057 previous_payload = current_payload;
1058 }
1059 iterator->destroy(iterator);
1060 return SUCCESS;
1061 }
1062
1063 /**
1064 * Implementation of private_message_t.verify.
1065 */
1066 static status_t verify(private_message_t *this)
1067 {
1068 int i;
1069 iterator_t *iterator;
1070 payload_t *current_payload;
1071 size_t total_found_payloads = 0;
1072
1073 DBG2(DBG_ENC, "verifying message structure");
1074
1075 iterator = this->payloads->create_iterator(this->payloads,TRUE);
1076 /* check for payloads with wrong count*/
1077 for (i = 0; i < this->message_rule->payload_rule_count;i++)
1078 {
1079 size_t found_payloads = 0;
1080
1081 /* check all payloads for specific rule */
1082 iterator->reset(iterator);
1083
1084 while(iterator->iterate(iterator,(void **)&current_payload))
1085 {
1086 payload_type_t current_payload_type;
1087
1088 current_payload_type = current_payload->get_type(current_payload);
1089 if (current_payload_type == UNKNOWN_PAYLOAD)
1090 {
1091 /* unknown payloads are ignored, IF they are not critical */
1092 unknown_payload_t *unknown_payload = (unknown_payload_t*)current_payload;
1093 if (unknown_payload->is_critical(unknown_payload))
1094 {
1095 DBG1(DBG_ENC, "%N is not supported, but its critical!",
1096 payload_type_names, current_payload_type);
1097 iterator->destroy(iterator);
1098 return NOT_SUPPORTED;
1099 }
1100 }
1101 else if (current_payload_type == this->message_rule->payload_rules[i].payload_type)
1102 {
1103 found_payloads++;
1104 total_found_payloads++;
1105 DBG2(DBG_ENC, "found payload of type %N",
1106 payload_type_names, this->message_rule->payload_rules[i].payload_type);
1107
1108 /* as soon as ohe payload occures more then specified, the verification fails */
1109 if (found_payloads > this->message_rule->payload_rules[i].max_occurence)
1110 {
1111 DBG1(DBG_ENC, "payload of type %N more than %d times (%d) occured in current message",
1112 payload_type_names, current_payload_type,
1113 this->message_rule->payload_rules[i].max_occurence, found_payloads);
1114 iterator->destroy(iterator);
1115 return VERIFY_ERROR;
1116 }
1117 }
1118 }
1119
1120 if (found_payloads < this->message_rule->payload_rules[i].min_occurence)
1121 {
1122 DBG1(DBG_ENC, "payload of type %N not occured %d times (%d)",
1123 payload_type_names, this->message_rule->payload_rules[i].payload_type,
1124 this->message_rule->payload_rules[i].min_occurence, found_payloads);
1125 iterator->destroy(iterator);
1126 return VERIFY_ERROR;
1127 }
1128 if ((this->message_rule->payload_rules[i].sufficient) && (this->payloads->get_count(this->payloads) == total_found_payloads))
1129 {
1130 iterator->destroy(iterator);
1131 return SUCCESS;
1132 }
1133 }
1134 iterator->destroy(iterator);
1135 return SUCCESS;
1136 }
1137
1138 /**
1139 * Implementation of message_t.parse_body.
1140 */
1141 static status_t parse_body(private_message_t *this, crypter_t *crypter, signer_t *signer)
1142 {
1143 status_t status = SUCCESS;
1144 payload_type_t current_payload_type;
1145
1146 current_payload_type = this->first_payload;
1147
1148 DBG2(DBG_ENC, "parsing body of message, first payload is %N",
1149 payload_type_names, current_payload_type);
1150
1151 /* parse payload for payload, while there are more available */
1152 while ((current_payload_type != NO_PAYLOAD))
1153 {
1154 payload_t *current_payload;
1155
1156 DBG2(DBG_ENC, "starting parsing a %N payload",
1157 payload_type_names, current_payload_type);
1158
1159 /* parse current payload */
1160 status = this->parser->parse_payload(this->parser,current_payload_type,(payload_t **) &current_payload);
1161
1162 if (status != SUCCESS)
1163 {
1164 DBG1(DBG_ENC, "payload type %N could not be parsed",
1165 payload_type_names, current_payload_type);
1166 return PARSE_ERROR;
1167 }
1168
1169 DBG2(DBG_ENC, "verifying payload of type %N",
1170 payload_type_names, current_payload_type);
1171
1172 /* verify it, stop parsig if its invalid */
1173 status = current_payload->verify(current_payload);
1174 if (status != SUCCESS)
1175 {
1176 DBG1(DBG_ENC, "%N payload verification failed",
1177 payload_type_names, current_payload_type);
1178 current_payload->destroy(current_payload);
1179 return VERIFY_ERROR;
1180 }
1181
1182 DBG2(DBG_ENC, "%N payload verified. Adding to payload list",
1183 payload_type_names, current_payload_type);
1184 this->payloads->insert_last(this->payloads,current_payload);
1185
1186 /* an encryption payload is the last one, so STOP here. decryption is done later */
1187 if (current_payload_type == ENCRYPTED)
1188 {
1189 DBG2(DBG_ENC, "%N payload found. Stop parsing",
1190 payload_type_names, current_payload_type);
1191 break;
1192 }
1193
1194 /* get next payload type */
1195 current_payload_type = current_payload->get_next_type(current_payload);
1196 }
1197
1198 if (current_payload_type == ENCRYPTED)
1199 {
1200 status = decrypt_payloads(this,crypter,signer);
1201 if (status != SUCCESS)
1202 {
1203 DBG1(DBG_ENC, "could not decrypt payloads");
1204 return status;
1205 }
1206 }
1207
1208 status = verify(this);
1209 if (status != SUCCESS)
1210 {
1211 return status;
1212 }
1213
1214 DBG1(DBG_ENC, "parsed %M", this);
1215
1216 return SUCCESS;
1217 }
1218
1219 /**
1220 * Implementation of message_t.destroy.
1221 */
1222 static void destroy (private_message_t *this)
1223 {
1224 DESTROY_IF(this->ike_sa_id);
1225 this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
1226 this->packet->destroy(this->packet);
1227 this->parser->destroy(this->parser);
1228 free(this);
1229 }
1230
1231 /*
1232 * Described in Header-File
1233 */
1234 message_t *message_create_from_packet(packet_t *packet)
1235 {
1236 private_message_t *this = malloc_thing(private_message_t);
1237
1238 /* public functions */
1239 this->public.set_major_version = (void(*)(message_t*, u_int8_t))set_major_version;
1240 this->public.get_major_version = (u_int8_t(*)(message_t*))get_major_version;
1241 this->public.set_minor_version = (void(*)(message_t*, u_int8_t))set_minor_version;
1242 this->public.get_minor_version = (u_int8_t(*)(message_t*))get_minor_version;
1243 this->public.set_message_id = (void(*)(message_t*, u_int32_t))set_message_id;
1244 this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
1245 this->public.get_initiator_spi = (u_int64_t(*)(message_t*))get_initiator_spi;
1246 this->public.get_responder_spi = (u_int64_t(*)(message_t*))get_responder_spi;
1247 this->public.set_ike_sa_id = (void(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
1248 this->public.get_ike_sa_id = (ike_sa_id_t*(*)(message_t*))get_ike_sa_id;
1249 this->public.set_exchange_type = (void(*)(message_t*, exchange_type_t))set_exchange_type;
1250 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
1251 this->public.set_request = (void(*)(message_t*, bool))set_request;
1252 this->public.get_request = (bool(*)(message_t*))get_request;
1253 this->public.add_payload = (void(*)(message_t*,payload_t*))add_payload;
1254 this->public.add_notify = (void(*)(message_t*,bool,notify_type_t,chunk_t))add_notify;
1255 this->public.generate = (status_t (*) (message_t *,crypter_t*,signer_t*,packet_t**)) generate;
1256 this->public.set_source = (void (*) (message_t*,host_t*)) set_source;
1257 this->public.get_source = (host_t * (*) (message_t*)) get_source;
1258 this->public.set_destination = (void (*) (message_t*,host_t*)) set_destination;
1259 this->public.get_destination = (host_t * (*) (message_t*)) get_destination;
1260 this->public.get_payload_iterator = (iterator_t * (*) (message_t *)) get_payload_iterator;
1261 this->public.get_payload = (payload_t * (*) (message_t *, payload_type_t)) get_payload;
1262 this->public.parse_header = (status_t (*) (message_t *)) parse_header;
1263 this->public.parse_body = (status_t (*) (message_t *,crypter_t*,signer_t*)) parse_body;
1264 this->public.get_packet = (packet_t * (*) (message_t*)) get_packet;
1265 this->public.get_packet_data = (chunk_t (*) (message_t *this)) get_packet_data;
1266 this->public.destroy = (void(*)(message_t*))destroy;
1267
1268 /* private values */
1269 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
1270 this->is_request = TRUE;
1271 this->ike_sa_id = NULL;
1272 this->first_payload = NO_PAYLOAD;
1273 this->message_id = 0;
1274
1275 /* private values */
1276 if (packet == NULL)
1277 {
1278 packet = packet_create();
1279 }
1280 this->message_rule = NULL;
1281 this->packet = packet;
1282 this->payloads = linked_list_create();
1283
1284 /* parser is created from data of packet */
1285 this->parser = parser_create(this->packet->get_data(this->packet));
1286
1287 return (&this->public);
1288 }
1289
1290 /*
1291 * Described in Header.
1292 */
1293 message_t *message_create()
1294 {
1295 return message_create_from_packet(NULL);
1296 }