5f3f91f8b1261147e7d23cfad80e1ce51f33479e
[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, *first_payload;
485
486 if ((this->is_request && payload->get_type(payload) == ID_INITIATOR) ||
487 (!this->is_request && payload->get_type(payload) == ID_RESPONDER))
488 {
489 /* HOTD: insert ID payload in the beginning to respect RFC */
490 if (this->payloads->get_first(this->payloads,
491 (void **)&first_payload) == SUCCESS)
492 {
493 payload->set_next_type(payload, first_payload->get_type(first_payload));
494 }
495 else
496 {
497 payload->set_next_type(payload, NO_PAYLOAD);
498 }
499 this->first_payload = payload->get_type(payload);
500 this->payloads->insert_first(this->payloads, payload);
501 }
502 else
503 {
504 if (this->payloads->get_count(this->payloads) > 0)
505 {
506 this->payloads->get_last(this->payloads,(void **) &last_payload);
507 last_payload->set_next_type(last_payload, payload->get_type(payload));
508 }
509 else
510 {
511 this->first_payload = payload->get_type(payload);
512 }
513 payload->set_next_type(payload, NO_PAYLOAD);
514 this->payloads->insert_last(this->payloads, payload);
515 }
516
517 DBG2(DBG_ENC ,"added payload of type %N to message",
518 payload_type_names, payload->get_type(payload));
519 }
520
521 /**
522 * Implementation of message_t.add_notify.
523 */
524 static void add_notify(private_message_t *this, bool flush, notify_type_t type,
525 chunk_t data)
526 {
527 notify_payload_t *notify;
528 payload_t *payload;
529
530 if (flush)
531 {
532 while (this->payloads->remove_last(this->payloads,
533 (void**)&payload) == SUCCESS)
534 {
535 payload->destroy(payload);
536 }
537 }
538 notify = notify_payload_create();
539 notify->set_notify_type(notify, type);
540 notify->set_notification_data(notify, data);
541 add_payload(this, (payload_t*)notify);
542 }
543
544 /**
545 * Implementation of message_t.set_source.
546 */
547 static void set_source(private_message_t *this, host_t *host)
548 {
549 this->packet->set_source(this->packet, host);
550 }
551
552 /**
553 * Implementation of message_t.set_destination.
554 */
555 static void set_destination(private_message_t *this, host_t *host)
556 {
557 this->packet->set_destination(this->packet, host);
558 }
559
560 /**
561 * Implementation of message_t.get_source.
562 */
563 static host_t* get_source(private_message_t *this)
564 {
565 return this->packet->get_source(this->packet);
566 }
567
568 /**
569 * Implementation of message_t.get_destination.
570 */
571 static host_t * get_destination(private_message_t *this)
572 {
573 return this->packet->get_destination(this->packet);
574 }
575
576 /**
577 * Implementation of message_t.get_payload_iterator.
578 */
579 static iterator_t *get_payload_iterator(private_message_t *this)
580 {
581 return this->payloads->create_iterator(this->payloads, TRUE);
582 }
583
584 /**
585 * Implementation of message_t.get_payload.
586 */
587 static payload_t *get_payload(private_message_t *this, payload_type_t type)
588 {
589 payload_t *current, *found = NULL;
590 iterator_t *iterator;
591
592 iterator = this->payloads->create_iterator(this->payloads, TRUE);
593 while (iterator->iterate(iterator, (void**)&current))
594 {
595 if (current->get_type(current) == type)
596 {
597 found = current;
598 break;
599 }
600 }
601 iterator->destroy(iterator);
602 return found;
603 }
604
605 /**
606 * output handler in printf()
607 */
608 static int print(FILE *stream, const struct printf_info *info,
609 const void *const *args)
610 {
611 private_message_t *this = *((private_message_t**)(args[0]));
612 iterator_t *iterator;
613 payload_t *payload;
614 bool first = TRUE;
615 size_t total_written = 0;
616 size_t written;
617
618 if (this == NULL)
619 {
620 return fprintf(stream, "(null)");
621 }
622
623 written = fprintf(stream, "%N %s [",
624 exchange_type_names, this->exchange_type,
625 this->is_request ? "request" : "response");
626 if (written < 0)
627 {
628 return written;
629 }
630 total_written += written;
631
632 iterator = this->payloads->create_iterator(this->payloads, TRUE);
633 while (iterator->iterate(iterator, (void**)&payload))
634 {
635 if (!first)
636 {
637 written = fprintf(stream, " ");
638 if (written < 0)
639 {
640 return written;
641 }
642 total_written += written;
643 }
644 else
645 {
646 first = FALSE;
647 }
648 written = fprintf(stream, "%N", payload_type_short_names,
649 payload->get_type(payload));
650 if (written < 0)
651 {
652 return written;
653 }
654 total_written += written;
655 }
656 iterator->destroy(iterator);
657 written = fprintf(stream, "]");
658 if (written < 0)
659 {
660 return written;
661 }
662 total_written += written;
663 return total_written;
664 }
665
666 /**
667 * register printf() handlers
668 */
669 static void __attribute__ ((constructor))print_register()
670 {
671 register_printf_function(PRINTF_MESSAGE, print, arginfo_ptr);
672 }
673
674 /**
675 * Implementation of private_message_t.encrypt_payloads.
676 */
677 static status_t encrypt_payloads (private_message_t *this,crypter_t *crypter, signer_t* signer)
678 {
679 encryption_payload_t *encryption_payload = NULL;
680 status_t status;
681 linked_list_t *all_payloads;
682
683 if (!this->message_rule->encrypted_content)
684 {
685 DBG2(DBG_ENC, "message doesn't have to be encrypted");
686 /* message contains no content to encrypt */
687 return SUCCESS;
688 }
689
690 DBG2(DBG_ENC, "copy all payloads to a temporary list");
691 all_payloads = linked_list_create();
692
693 /* first copy all payloads in a temporary list */
694 while (this->payloads->get_count(this->payloads) > 0)
695 {
696 void *current_payload;
697 this->payloads->remove_first(this->payloads,&current_payload);
698 all_payloads->insert_last(all_payloads,current_payload);
699 }
700
701 encryption_payload = encryption_payload_create();
702
703 DBG2(DBG_ENC, "check each payloads if they have to get encrypted");
704 while (all_payloads->get_count(all_payloads) > 0)
705 {
706 payload_rule_t *payload_rule;
707 payload_t *current_payload;
708 bool to_encrypt = FALSE;
709
710 all_payloads->remove_first(all_payloads,(void **)&current_payload);
711
712 status = get_payload_rule(this,
713 current_payload->get_type(current_payload),&payload_rule);
714 /* for payload types which are not found in supported payload list,
715 * it is presumed that they don't have to be encrypted */
716 if ((status == SUCCESS) && (payload_rule->encrypted))
717 {
718 DBG2(DBG_ENC, "payload %N gets encrypted",
719 payload_type_names, current_payload->get_type(current_payload));
720 to_encrypt = TRUE;
721 }
722
723 if (to_encrypt)
724 {
725 DBG2(DBG_ENC, "insert payload %N to encryption payload",
726 payload_type_names, current_payload->get_type(current_payload));
727 encryption_payload->add_payload(encryption_payload,current_payload);
728 }
729 else
730 {
731 DBG2(DBG_ENC, "insert payload %N unencrypted",
732 payload_type_names ,current_payload->get_type(current_payload));
733 add_payload(this, (payload_t*)encryption_payload);
734 }
735 }
736
737 status = SUCCESS;
738 DBG2(DBG_ENC, "encrypting encryption payload");
739 encryption_payload->set_transforms(encryption_payload, crypter,signer);
740 status = encryption_payload->encrypt(encryption_payload);
741 DBG2(DBG_ENC, "add encrypted payload to payload list");
742 add_payload(this, (payload_t*)encryption_payload);
743
744 all_payloads->destroy(all_payloads);
745
746 return status;
747 }
748
749 /**
750 * Implementation of message_t.generate.
751 */
752 static status_t generate(private_message_t *this, crypter_t *crypter, signer_t* signer, packet_t **packet)
753 {
754 generator_t *generator;
755 ike_header_t *ike_header;
756 payload_t *payload, *next_payload;
757 iterator_t *iterator;
758 status_t status;
759 chunk_t packet_data;
760
761 if (is_encoded(this))
762 {
763 /* already generated, return a new packet clone */
764 *packet = this->packet->clone(this->packet);
765 return SUCCESS;
766 }
767
768 DBG1(DBG_ENC, "generating %M", this);
769
770 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
771 {
772 DBG1(DBG_ENC, "exchange type is not defined");
773 return INVALID_STATE;
774 }
775
776 if (this->packet->get_source(this->packet) == NULL ||
777 this->packet->get_destination(this->packet) == NULL)
778 {
779 DBG1(DBG_ENC, "%s not defined",
780 !this->packet->get_source(this->packet) ? "source" : "destination");
781 return INVALID_STATE;
782 }
783
784 /* set the rules for this messge */
785 status = set_message_rule(this);
786 if (status != SUCCESS)
787 {
788 DBG1(DBG_ENC, "no message rules specified for this message type");
789 return NOT_SUPPORTED;
790 }
791
792 /* going to encrypt all content which have to be encrypted */
793 status = encrypt_payloads(this, crypter, signer);
794 if (status != SUCCESS)
795 {
796 DBG1(DBG_ENC, "payload encryption failed");
797 return status;
798 }
799
800 /* build ike header */
801 ike_header = ike_header_create();
802
803 ike_header->set_exchange_type(ike_header, this->exchange_type);
804 ike_header->set_message_id(ike_header, this->message_id);
805 ike_header->set_response_flag(ike_header, !this->is_request);
806 ike_header->set_initiator_flag(ike_header, this->ike_sa_id->is_initiator(this->ike_sa_id));
807 ike_header->set_initiator_spi(ike_header, this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
808 ike_header->set_responder_spi(ike_header, this->ike_sa_id->get_responder_spi(this->ike_sa_id));
809
810 generator = generator_create();
811
812 payload = (payload_t*)ike_header;
813
814
815 /* generate every payload expect last one, this is doen later*/
816 iterator = this->payloads->create_iterator(this->payloads, TRUE);
817 while(iterator->iterate(iterator, (void**)&next_payload))
818 {
819 payload->set_next_type(payload, next_payload->get_type(next_payload));
820 generator->generate_payload(generator, payload);
821 payload = next_payload;
822 }
823 iterator->destroy(iterator);
824
825 /* last payload has no next payload*/
826 payload->set_next_type(payload, NO_PAYLOAD);
827
828 generator->generate_payload(generator, payload);
829
830 ike_header->destroy(ike_header);
831
832 /* build packet */
833 generator->write_to_chunk(generator, &packet_data);
834 generator->destroy(generator);
835
836 /* if last payload is of type encrypted, integrity checksum if necessary */
837 if (payload->get_type(payload) == ENCRYPTED)
838 {
839 DBG2(DBG_ENC, "build signature on whole message");
840 encryption_payload_t *encryption_payload = (encryption_payload_t*)payload;
841 status = encryption_payload->build_signature(encryption_payload, packet_data);
842 if (status != SUCCESS)
843 {
844 return status;
845 }
846 }
847
848 this->packet->set_data(this->packet, packet_data);
849
850 /* clone packet for caller */
851 *packet = this->packet->clone(this->packet);
852
853 DBG2(DBG_ENC, "message generated successfully");
854 return SUCCESS;
855 }
856
857 /**
858 * Implementation of message_t.get_packet.
859 */
860 static packet_t *get_packet (private_message_t *this)
861 {
862 if (this->packet == NULL)
863 {
864 return NULL;
865 }
866 return this->packet->clone(this->packet);
867 }
868
869 /**
870 * Implementation of message_t.get_packet_data.
871 */
872 static chunk_t get_packet_data (private_message_t *this)
873 {
874 if (this->packet == NULL)
875 {
876 return chunk_empty;
877 }
878 return chunk_clone(this->packet->get_data(this->packet));
879 }
880
881 /**
882 * Implementation of message_t.parse_header.
883 */
884 static status_t parse_header(private_message_t *this)
885 {
886 ike_header_t *ike_header;
887 status_t status;
888
889 DBG2(DBG_ENC, "parsing header of message");
890
891 this->parser->reset_context(this->parser);
892 status = this->parser->parse_payload(this->parser,HEADER,(payload_t **) &ike_header);
893 if (status != SUCCESS)
894 {
895 DBG1(DBG_ENC, "header could not be parsed");
896 return status;
897
898 }
899
900 /* verify payload */
901 status = ike_header->payload_interface.verify(&(ike_header->payload_interface));
902 if (status != SUCCESS)
903 {
904 DBG1(DBG_ENC, "header verification failed");
905 ike_header->destroy(ike_header);
906 return status;
907 }
908
909 if (this->ike_sa_id != NULL)
910 {
911 this->ike_sa_id->destroy(this->ike_sa_id);
912 }
913
914 this->ike_sa_id = ike_sa_id_create(ike_header->get_initiator_spi(ike_header),
915 ike_header->get_responder_spi(ike_header),
916 ike_header->get_initiator_flag(ike_header));
917
918 this->exchange_type = ike_header->get_exchange_type(ike_header);
919 this->message_id = ike_header->get_message_id(ike_header);
920 this->is_request = (!(ike_header->get_response_flag(ike_header)));
921 this->major_version = ike_header->get_maj_version(ike_header);
922 this->minor_version = ike_header->get_min_version(ike_header);
923 this->first_payload = ike_header->payload_interface.get_next_type(&(ike_header->payload_interface));
924
925 DBG2(DBG_ENC, "parsed a %N %s", exchange_type_names, this->exchange_type,
926 this->is_request ? "request" : "response");
927
928 ike_header->destroy(ike_header);
929
930 /* get the rules for this messge */
931 status = set_message_rule(this);
932 if (status != SUCCESS)
933 {
934 DBG1(DBG_ENC, "no message rules specified for a %N %s",
935 exchange_type_names, this->exchange_type,
936 this->is_request ? "request" : "response");
937 }
938
939 return status;
940 }
941
942 /**
943 * Implementation of private_message_t.decrypt_and_verify_payloads.
944 */
945 static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, signer_t* signer)
946 {
947 bool current_payload_was_encrypted = FALSE;
948 payload_t *previous_payload = NULL;
949 int payload_number = 1;
950 iterator_t *iterator;
951 payload_t *current_payload;
952 status_t status;
953
954 iterator = this->payloads->create_iterator(this->payloads,TRUE);
955
956 /* process each payload and decrypt a encryption payload */
957 while(iterator->iterate(iterator, (void**)&current_payload))
958 {
959 payload_rule_t *payload_rule;
960 payload_type_t current_payload_type;
961
962 /* needed to check */
963 current_payload_type = current_payload->get_type(current_payload);
964
965 DBG2(DBG_ENC, "process payload of type %N",
966 payload_type_names, current_payload_type);
967
968 if (current_payload_type == ENCRYPTED)
969 {
970 encryption_payload_t *encryption_payload;
971 payload_t *current_encrypted_payload;
972
973 encryption_payload = (encryption_payload_t*)current_payload;
974
975 DBG2(DBG_ENC, "found an encryption payload");
976
977 if (payload_number != this->payloads->get_count(this->payloads))
978 {
979 /* encrypted payload is not last one */
980 DBG1(DBG_ENC, "encrypted payload is not last payload");
981 iterator->destroy(iterator);
982 return VERIFY_ERROR;
983 }
984 /* decrypt */
985 encryption_payload->set_transforms(encryption_payload, crypter, signer);
986 DBG2(DBG_ENC, "verify signature of encryption payload");
987 status = encryption_payload->verify_signature(encryption_payload,
988 this->packet->get_data(this->packet));
989 if (status != SUCCESS)
990 {
991 DBG1(DBG_ENC, "encryption payload signature invalid");
992 iterator->destroy(iterator);
993 return FAILED;
994 }
995 DBG2(DBG_ENC, "decrypting content of encryption payload");
996 status = encryption_payload->decrypt(encryption_payload);
997 if (status != SUCCESS)
998 {
999 DBG1(DBG_ENC, "encrypted payload could not be decrypted and parsed");
1000 iterator->destroy(iterator);
1001 return PARSE_ERROR;
1002 }
1003
1004 /* needed later to find out if a payload was encrypted */
1005 current_payload_was_encrypted = TRUE;
1006
1007 /* check if there are payloads contained in the encryption payload */
1008 if (encryption_payload->get_payload_count(encryption_payload) == 0)
1009 {
1010 DBG2(DBG_ENC, "encrypted payload is empty");
1011 /* remove the encryption payload, is not needed anymore */
1012 iterator->remove(iterator);
1013 /* encrypted payload contains no other payload */
1014 current_payload_type = NO_PAYLOAD;
1015 }
1016 else
1017 {
1018 /* encryption_payload is replaced with first payload contained in encryption_payload */
1019 encryption_payload->remove_first_payload(encryption_payload, &current_encrypted_payload);
1020 iterator->replace(iterator,NULL,(void *) current_encrypted_payload);
1021 current_payload_type = current_encrypted_payload->get_type(current_encrypted_payload);
1022 }
1023
1024 /* is the current paylad the first in the message? */
1025 if (previous_payload == NULL)
1026 {
1027 /* yes, set the first payload type of the message to the current type */
1028 this->first_payload = current_payload_type;
1029 }
1030 else
1031 {
1032 /* no, set the next_type of the previous payload to the current type */
1033 previous_payload->set_next_type(previous_payload, current_payload_type);
1034 }
1035
1036 /* all encrypted payloads are added to the payload list */
1037 while (encryption_payload->get_payload_count(encryption_payload) > 0)
1038 {
1039 encryption_payload->remove_first_payload(encryption_payload, &current_encrypted_payload);
1040 DBG2(DBG_ENC, "insert unencrypted payload of type %N at end of list",
1041 payload_type_names, current_encrypted_payload->get_type(current_encrypted_payload));
1042 this->payloads->insert_last(this->payloads,current_encrypted_payload);
1043 }
1044
1045 /* encryption payload is processed, payloads are moved. Destroy it. */
1046 encryption_payload->destroy(encryption_payload);
1047 }
1048
1049 /* we allow unknown payloads of any type and don't bother if it was encrypted. Not our problem. */
1050 if (current_payload_type != UNKNOWN_PAYLOAD && current_payload_type != NO_PAYLOAD)
1051 {
1052 /* get the ruleset for found payload */
1053 status = get_payload_rule(this, current_payload_type, &payload_rule);
1054 if (status != SUCCESS)
1055 {
1056 /* payload is not allowed */
1057 DBG1(DBG_ENC, "payload type %N not allowed",
1058 payload_type_names, current_payload_type);
1059 iterator->destroy(iterator);
1060 return VERIFY_ERROR;
1061 }
1062
1063 /* check if the payload was encrypted, and if it should been have encrypted */
1064 if (payload_rule->encrypted != current_payload_was_encrypted)
1065 {
1066 /* payload was not encrypted, but should have been. or vice-versa */
1067 DBG1(DBG_ENC, "payload type %N should be %s!",
1068 payload_type_names, current_payload_type,
1069 (payload_rule->encrypted) ? "encrypted" : "not encrypted");
1070 iterator->destroy(iterator);
1071 return VERIFY_ERROR;
1072 }
1073 }
1074 /* advance to the next payload */
1075 payload_number++;
1076 /* is stored to set next payload in case of found encryption payload */
1077 previous_payload = current_payload;
1078 }
1079 iterator->destroy(iterator);
1080 return SUCCESS;
1081 }
1082
1083 /**
1084 * Implementation of private_message_t.verify.
1085 */
1086 static status_t verify(private_message_t *this)
1087 {
1088 int i;
1089 iterator_t *iterator;
1090 payload_t *current_payload;
1091 size_t total_found_payloads = 0;
1092
1093 DBG2(DBG_ENC, "verifying message structure");
1094
1095 iterator = this->payloads->create_iterator(this->payloads,TRUE);
1096 /* check for payloads with wrong count*/
1097 for (i = 0; i < this->message_rule->payload_rule_count;i++)
1098 {
1099 size_t found_payloads = 0;
1100
1101 /* check all payloads for specific rule */
1102 iterator->reset(iterator);
1103
1104 while(iterator->iterate(iterator,(void **)&current_payload))
1105 {
1106 payload_type_t current_payload_type;
1107
1108 current_payload_type = current_payload->get_type(current_payload);
1109 if (current_payload_type == UNKNOWN_PAYLOAD)
1110 {
1111 /* unknown payloads are ignored, IF they are not critical */
1112 unknown_payload_t *unknown_payload = (unknown_payload_t*)current_payload;
1113 if (unknown_payload->is_critical(unknown_payload))
1114 {
1115 DBG1(DBG_ENC, "%N is not supported, but its critical!",
1116 payload_type_names, current_payload_type);
1117 iterator->destroy(iterator);
1118 return NOT_SUPPORTED;
1119 }
1120 }
1121 else if (current_payload_type == this->message_rule->payload_rules[i].payload_type)
1122 {
1123 found_payloads++;
1124 total_found_payloads++;
1125 DBG2(DBG_ENC, "found payload of type %N",
1126 payload_type_names, this->message_rule->payload_rules[i].payload_type);
1127
1128 /* as soon as ohe payload occures more then specified, the verification fails */
1129 if (found_payloads > this->message_rule->payload_rules[i].max_occurence)
1130 {
1131 DBG1(DBG_ENC, "payload of type %N more than %d times (%d) occured in current message",
1132 payload_type_names, current_payload_type,
1133 this->message_rule->payload_rules[i].max_occurence, found_payloads);
1134 iterator->destroy(iterator);
1135 return VERIFY_ERROR;
1136 }
1137 }
1138 }
1139
1140 if (found_payloads < this->message_rule->payload_rules[i].min_occurence)
1141 {
1142 DBG1(DBG_ENC, "payload of type %N not occured %d times (%d)",
1143 payload_type_names, this->message_rule->payload_rules[i].payload_type,
1144 this->message_rule->payload_rules[i].min_occurence, found_payloads);
1145 iterator->destroy(iterator);
1146 return VERIFY_ERROR;
1147 }
1148 if ((this->message_rule->payload_rules[i].sufficient) && (this->payloads->get_count(this->payloads) == total_found_payloads))
1149 {
1150 iterator->destroy(iterator);
1151 return SUCCESS;
1152 }
1153 }
1154 iterator->destroy(iterator);
1155 return SUCCESS;
1156 }
1157
1158 /**
1159 * Implementation of message_t.parse_body.
1160 */
1161 static status_t parse_body(private_message_t *this, crypter_t *crypter, signer_t *signer)
1162 {
1163 status_t status = SUCCESS;
1164 payload_type_t current_payload_type;
1165
1166 current_payload_type = this->first_payload;
1167
1168 DBG2(DBG_ENC, "parsing body of message, first payload is %N",
1169 payload_type_names, current_payload_type);
1170
1171 /* parse payload for payload, while there are more available */
1172 while ((current_payload_type != NO_PAYLOAD))
1173 {
1174 payload_t *current_payload;
1175
1176 DBG2(DBG_ENC, "starting parsing a %N payload",
1177 payload_type_names, current_payload_type);
1178
1179 /* parse current payload */
1180 status = this->parser->parse_payload(this->parser,current_payload_type,(payload_t **) &current_payload);
1181
1182 if (status != SUCCESS)
1183 {
1184 DBG1(DBG_ENC, "payload type %N could not be parsed",
1185 payload_type_names, current_payload_type);
1186 return PARSE_ERROR;
1187 }
1188
1189 DBG2(DBG_ENC, "verifying payload of type %N",
1190 payload_type_names, current_payload_type);
1191
1192 /* verify it, stop parsig if its invalid */
1193 status = current_payload->verify(current_payload);
1194 if (status != SUCCESS)
1195 {
1196 DBG1(DBG_ENC, "%N payload verification failed",
1197 payload_type_names, current_payload_type);
1198 current_payload->destroy(current_payload);
1199 return VERIFY_ERROR;
1200 }
1201
1202 DBG2(DBG_ENC, "%N payload verified. Adding to payload list",
1203 payload_type_names, current_payload_type);
1204 this->payloads->insert_last(this->payloads,current_payload);
1205
1206 /* an encryption payload is the last one, so STOP here. decryption is done later */
1207 if (current_payload_type == ENCRYPTED)
1208 {
1209 DBG2(DBG_ENC, "%N payload found. Stop parsing",
1210 payload_type_names, current_payload_type);
1211 break;
1212 }
1213
1214 /* get next payload type */
1215 current_payload_type = current_payload->get_next_type(current_payload);
1216 }
1217
1218 if (current_payload_type == ENCRYPTED)
1219 {
1220 status = decrypt_payloads(this,crypter,signer);
1221 if (status != SUCCESS)
1222 {
1223 DBG1(DBG_ENC, "could not decrypt payloads");
1224 return status;
1225 }
1226 }
1227
1228 status = verify(this);
1229 if (status != SUCCESS)
1230 {
1231 return status;
1232 }
1233
1234 DBG1(DBG_ENC, "parsed %M", this);
1235
1236 return SUCCESS;
1237 }
1238
1239 /**
1240 * Implementation of message_t.destroy.
1241 */
1242 static void destroy (private_message_t *this)
1243 {
1244 DESTROY_IF(this->ike_sa_id);
1245 this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
1246 this->packet->destroy(this->packet);
1247 this->parser->destroy(this->parser);
1248 free(this);
1249 }
1250
1251 /*
1252 * Described in Header-File
1253 */
1254 message_t *message_create_from_packet(packet_t *packet)
1255 {
1256 private_message_t *this = malloc_thing(private_message_t);
1257
1258 /* public functions */
1259 this->public.set_major_version = (void(*)(message_t*, u_int8_t))set_major_version;
1260 this->public.get_major_version = (u_int8_t(*)(message_t*))get_major_version;
1261 this->public.set_minor_version = (void(*)(message_t*, u_int8_t))set_minor_version;
1262 this->public.get_minor_version = (u_int8_t(*)(message_t*))get_minor_version;
1263 this->public.set_message_id = (void(*)(message_t*, u_int32_t))set_message_id;
1264 this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
1265 this->public.get_initiator_spi = (u_int64_t(*)(message_t*))get_initiator_spi;
1266 this->public.get_responder_spi = (u_int64_t(*)(message_t*))get_responder_spi;
1267 this->public.set_ike_sa_id = (void(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
1268 this->public.get_ike_sa_id = (ike_sa_id_t*(*)(message_t*))get_ike_sa_id;
1269 this->public.set_exchange_type = (void(*)(message_t*, exchange_type_t))set_exchange_type;
1270 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
1271 this->public.set_request = (void(*)(message_t*, bool))set_request;
1272 this->public.get_request = (bool(*)(message_t*))get_request;
1273 this->public.add_payload = (void(*)(message_t*,payload_t*))add_payload;
1274 this->public.add_notify = (void(*)(message_t*,bool,notify_type_t,chunk_t))add_notify;
1275 this->public.generate = (status_t (*) (message_t *,crypter_t*,signer_t*,packet_t**)) generate;
1276 this->public.set_source = (void (*) (message_t*,host_t*)) set_source;
1277 this->public.get_source = (host_t * (*) (message_t*)) get_source;
1278 this->public.set_destination = (void (*) (message_t*,host_t*)) set_destination;
1279 this->public.get_destination = (host_t * (*) (message_t*)) get_destination;
1280 this->public.get_payload_iterator = (iterator_t * (*) (message_t *)) get_payload_iterator;
1281 this->public.get_payload = (payload_t * (*) (message_t *, payload_type_t)) get_payload;
1282 this->public.parse_header = (status_t (*) (message_t *)) parse_header;
1283 this->public.parse_body = (status_t (*) (message_t *,crypter_t*,signer_t*)) parse_body;
1284 this->public.get_packet = (packet_t * (*) (message_t*)) get_packet;
1285 this->public.get_packet_data = (chunk_t (*) (message_t *this)) get_packet_data;
1286 this->public.destroy = (void(*)(message_t*))destroy;
1287
1288 /* private values */
1289 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
1290 this->is_request = TRUE;
1291 this->ike_sa_id = NULL;
1292 this->first_payload = NO_PAYLOAD;
1293 this->message_id = 0;
1294
1295 /* private values */
1296 if (packet == NULL)
1297 {
1298 packet = packet_create();
1299 }
1300 this->message_rule = NULL;
1301 this->packet = packet;
1302 this->payloads = linked_list_create();
1303
1304 /* parser is created from data of packet */
1305 this->parser = parser_create(this->packet->get_data(this->packet));
1306
1307 return (&this->public);
1308 }
1309
1310 /*
1311 * Described in Header.
1312 */
1313 message_t *message_create()
1314 {
1315 return message_create_from_packet(NULL);
1316 }