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