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