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