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