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