removed %M printf handler, five more to go
[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 void get_string(private_message_t *this, char *buf, int len)
608 {
609 iterator_t *iterator;
610 payload_t *payload;
611 int written;
612
613 written = snprintf(buf, len, "%N %s [",
614 exchange_type_names, this->exchange_type,
615 this->is_request ? "request" : "response");
616 if (written >= len || written < 0)
617 {
618 return;
619 }
620 buf += written;
621 len -= written;
622
623 iterator = this->payloads->create_iterator(this->payloads, TRUE);
624 while (iterator->iterate(iterator, (void**)&payload))
625 {
626 written = snprintf(buf, len, "%N ", payload_type_short_names,
627 payload->get_type(payload));
628 if (written >= len || written < 0)
629 {
630 return;
631 }
632 buf += written;
633 len -= written;
634 }
635 iterator->destroy(iterator);
636
637 /* remove last space */
638 len++;
639 buf--;
640 snprintf(buf, len, "]");
641 }
642
643 /**
644 * Implementation of private_message_t.encrypt_payloads.
645 */
646 static status_t encrypt_payloads (private_message_t *this,crypter_t *crypter, signer_t* signer)
647 {
648 encryption_payload_t *encryption_payload = NULL;
649 status_t status;
650 linked_list_t *all_payloads;
651
652 if (!this->message_rule->encrypted_content)
653 {
654 DBG2(DBG_ENC, "message doesn't have to be encrypted");
655 /* message contains no content to encrypt */
656 return SUCCESS;
657 }
658
659 DBG2(DBG_ENC, "copy all payloads to a temporary list");
660 all_payloads = linked_list_create();
661
662 /* first copy all payloads in a temporary list */
663 while (this->payloads->get_count(this->payloads) > 0)
664 {
665 void *current_payload;
666 this->payloads->remove_first(this->payloads,&current_payload);
667 all_payloads->insert_last(all_payloads,current_payload);
668 }
669
670 encryption_payload = encryption_payload_create();
671
672 DBG2(DBG_ENC, "check each payloads if they have to get encrypted");
673 while (all_payloads->get_count(all_payloads) > 0)
674 {
675 payload_rule_t *payload_rule;
676 payload_t *current_payload;
677 bool to_encrypt = FALSE;
678
679 all_payloads->remove_first(all_payloads,(void **)&current_payload);
680
681 status = get_payload_rule(this,
682 current_payload->get_type(current_payload),&payload_rule);
683 /* for payload types which are not found in supported payload list,
684 * it is presumed that they don't have to be encrypted */
685 if ((status == SUCCESS) && (payload_rule->encrypted))
686 {
687 DBG2(DBG_ENC, "payload %N gets encrypted",
688 payload_type_names, current_payload->get_type(current_payload));
689 to_encrypt = TRUE;
690 }
691
692 if (to_encrypt)
693 {
694 DBG2(DBG_ENC, "insert payload %N to encryption payload",
695 payload_type_names, current_payload->get_type(current_payload));
696 encryption_payload->add_payload(encryption_payload,current_payload);
697 }
698 else
699 {
700 DBG2(DBG_ENC, "insert payload %N unencrypted",
701 payload_type_names ,current_payload->get_type(current_payload));
702 add_payload(this, (payload_t*)encryption_payload);
703 }
704 }
705
706 status = SUCCESS;
707 DBG2(DBG_ENC, "encrypting encryption payload");
708 encryption_payload->set_transforms(encryption_payload, crypter,signer);
709 status = encryption_payload->encrypt(encryption_payload);
710 DBG2(DBG_ENC, "add encrypted payload to payload list");
711 add_payload(this, (payload_t*)encryption_payload);
712
713 all_payloads->destroy(all_payloads);
714
715 return status;
716 }
717
718 /**
719 * Implementation of message_t.generate.
720 */
721 static status_t generate(private_message_t *this, crypter_t *crypter, signer_t* signer, packet_t **packet)
722 {
723 generator_t *generator;
724 ike_header_t *ike_header;
725 payload_t *payload, *next_payload;
726 iterator_t *iterator;
727 status_t status;
728 chunk_t packet_data;
729 char str[128];
730
731 if (is_encoded(this))
732 {
733 /* already generated, return a new packet clone */
734 *packet = this->packet->clone(this->packet);
735 return SUCCESS;
736 }
737
738 get_string(this, str, sizeof(str));
739 DBG1(DBG_ENC, "generating %s", str);
740
741 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
742 {
743 DBG1(DBG_ENC, "exchange type is not defined");
744 return INVALID_STATE;
745 }
746
747 if (this->packet->get_source(this->packet) == NULL ||
748 this->packet->get_destination(this->packet) == NULL)
749 {
750 DBG1(DBG_ENC, "%s not defined",
751 !this->packet->get_source(this->packet) ? "source" : "destination");
752 return INVALID_STATE;
753 }
754
755 /* set the rules for this messge */
756 status = set_message_rule(this);
757 if (status != SUCCESS)
758 {
759 DBG1(DBG_ENC, "no message rules specified for this message type");
760 return NOT_SUPPORTED;
761 }
762
763 /* going to encrypt all content which have to be encrypted */
764 status = encrypt_payloads(this, crypter, signer);
765 if (status != SUCCESS)
766 {
767 DBG1(DBG_ENC, "payload encryption failed");
768 return status;
769 }
770
771 /* build ike header */
772 ike_header = ike_header_create();
773
774 ike_header->set_exchange_type(ike_header, this->exchange_type);
775 ike_header->set_message_id(ike_header, this->message_id);
776 ike_header->set_response_flag(ike_header, !this->is_request);
777 ike_header->set_initiator_flag(ike_header, this->ike_sa_id->is_initiator(this->ike_sa_id));
778 ike_header->set_initiator_spi(ike_header, this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
779 ike_header->set_responder_spi(ike_header, this->ike_sa_id->get_responder_spi(this->ike_sa_id));
780
781 generator = generator_create();
782
783 payload = (payload_t*)ike_header;
784
785
786 /* generate every payload expect last one, this is doen later*/
787 iterator = this->payloads->create_iterator(this->payloads, TRUE);
788 while(iterator->iterate(iterator, (void**)&next_payload))
789 {
790 payload->set_next_type(payload, next_payload->get_type(next_payload));
791 generator->generate_payload(generator, payload);
792 payload = next_payload;
793 }
794 iterator->destroy(iterator);
795
796 /* last payload has no next payload*/
797 payload->set_next_type(payload, NO_PAYLOAD);
798
799 generator->generate_payload(generator, payload);
800
801 ike_header->destroy(ike_header);
802
803 /* build packet */
804 generator->write_to_chunk(generator, &packet_data);
805 generator->destroy(generator);
806
807 /* if last payload is of type encrypted, integrity checksum if necessary */
808 if (payload->get_type(payload) == ENCRYPTED)
809 {
810 DBG2(DBG_ENC, "build signature on whole message");
811 encryption_payload_t *encryption_payload = (encryption_payload_t*)payload;
812 status = encryption_payload->build_signature(encryption_payload, packet_data);
813 if (status != SUCCESS)
814 {
815 return status;
816 }
817 }
818
819 this->packet->set_data(this->packet, packet_data);
820
821 /* clone packet for caller */
822 *packet = this->packet->clone(this->packet);
823
824 DBG2(DBG_ENC, "message generated successfully");
825 return SUCCESS;
826 }
827
828 /**
829 * Implementation of message_t.get_packet.
830 */
831 static packet_t *get_packet (private_message_t *this)
832 {
833 if (this->packet == NULL)
834 {
835 return NULL;
836 }
837 return this->packet->clone(this->packet);
838 }
839
840 /**
841 * Implementation of message_t.get_packet_data.
842 */
843 static chunk_t get_packet_data (private_message_t *this)
844 {
845 if (this->packet == NULL)
846 {
847 return chunk_empty;
848 }
849 return chunk_clone(this->packet->get_data(this->packet));
850 }
851
852 /**
853 * Implementation of message_t.parse_header.
854 */
855 static status_t parse_header(private_message_t *this)
856 {
857 ike_header_t *ike_header;
858 status_t status;
859
860 DBG2(DBG_ENC, "parsing header of message");
861
862 this->parser->reset_context(this->parser);
863 status = this->parser->parse_payload(this->parser,HEADER,(payload_t **) &ike_header);
864 if (status != SUCCESS)
865 {
866 DBG1(DBG_ENC, "header could not be parsed");
867 return status;
868
869 }
870
871 /* verify payload */
872 status = ike_header->payload_interface.verify(&(ike_header->payload_interface));
873 if (status != SUCCESS)
874 {
875 DBG1(DBG_ENC, "header verification failed");
876 ike_header->destroy(ike_header);
877 return status;
878 }
879
880 if (this->ike_sa_id != NULL)
881 {
882 this->ike_sa_id->destroy(this->ike_sa_id);
883 }
884
885 this->ike_sa_id = ike_sa_id_create(ike_header->get_initiator_spi(ike_header),
886 ike_header->get_responder_spi(ike_header),
887 ike_header->get_initiator_flag(ike_header));
888
889 this->exchange_type = ike_header->get_exchange_type(ike_header);
890 this->message_id = ike_header->get_message_id(ike_header);
891 this->is_request = (!(ike_header->get_response_flag(ike_header)));
892 this->major_version = ike_header->get_maj_version(ike_header);
893 this->minor_version = ike_header->get_min_version(ike_header);
894 this->first_payload = ike_header->payload_interface.get_next_type(&(ike_header->payload_interface));
895
896 DBG2(DBG_ENC, "parsed a %N %s", exchange_type_names, this->exchange_type,
897 this->is_request ? "request" : "response");
898
899 ike_header->destroy(ike_header);
900
901 /* get the rules for this messge */
902 status = set_message_rule(this);
903 if (status != SUCCESS)
904 {
905 DBG1(DBG_ENC, "no message rules specified for a %N %s",
906 exchange_type_names, this->exchange_type,
907 this->is_request ? "request" : "response");
908 }
909
910 return status;
911 }
912
913 /**
914 * Implementation of private_message_t.decrypt_and_verify_payloads.
915 */
916 static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, signer_t* signer)
917 {
918 bool current_payload_was_encrypted = FALSE;
919 payload_t *previous_payload = NULL;
920 int payload_number = 1;
921 iterator_t *iterator;
922 payload_t *current_payload;
923 status_t status;
924
925 iterator = this->payloads->create_iterator(this->payloads,TRUE);
926
927 /* process each payload and decrypt a encryption payload */
928 while(iterator->iterate(iterator, (void**)&current_payload))
929 {
930 payload_rule_t *payload_rule;
931 payload_type_t current_payload_type;
932
933 /* needed to check */
934 current_payload_type = current_payload->get_type(current_payload);
935
936 DBG2(DBG_ENC, "process payload of type %N",
937 payload_type_names, current_payload_type);
938
939 if (current_payload_type == ENCRYPTED)
940 {
941 encryption_payload_t *encryption_payload;
942 payload_t *current_encrypted_payload;
943
944 encryption_payload = (encryption_payload_t*)current_payload;
945
946 DBG2(DBG_ENC, "found an encryption payload");
947
948 if (payload_number != this->payloads->get_count(this->payloads))
949 {
950 /* encrypted payload is not last one */
951 DBG1(DBG_ENC, "encrypted payload is not last payload");
952 iterator->destroy(iterator);
953 return VERIFY_ERROR;
954 }
955 /* decrypt */
956 encryption_payload->set_transforms(encryption_payload, crypter, signer);
957 DBG2(DBG_ENC, "verify signature of encryption payload");
958 status = encryption_payload->verify_signature(encryption_payload,
959 this->packet->get_data(this->packet));
960 if (status != SUCCESS)
961 {
962 DBG1(DBG_ENC, "encryption payload signature invalid");
963 iterator->destroy(iterator);
964 return FAILED;
965 }
966 DBG2(DBG_ENC, "decrypting content of encryption payload");
967 status = encryption_payload->decrypt(encryption_payload);
968 if (status != SUCCESS)
969 {
970 DBG1(DBG_ENC, "encrypted payload could not be decrypted and parsed");
971 iterator->destroy(iterator);
972 return PARSE_ERROR;
973 }
974
975 /* needed later to find out if a payload was encrypted */
976 current_payload_was_encrypted = TRUE;
977
978 /* check if there are payloads contained in the encryption payload */
979 if (encryption_payload->get_payload_count(encryption_payload) == 0)
980 {
981 DBG2(DBG_ENC, "encrypted payload is empty");
982 /* remove the encryption payload, is not needed anymore */
983 iterator->remove(iterator);
984 /* encrypted payload contains no other payload */
985 current_payload_type = NO_PAYLOAD;
986 }
987 else
988 {
989 /* encryption_payload is replaced with first payload contained in encryption_payload */
990 encryption_payload->remove_first_payload(encryption_payload, &current_encrypted_payload);
991 iterator->replace(iterator,NULL,(void *) current_encrypted_payload);
992 current_payload_type = current_encrypted_payload->get_type(current_encrypted_payload);
993 }
994
995 /* is the current paylad the first in the message? */
996 if (previous_payload == NULL)
997 {
998 /* yes, set the first payload type of the message to the current type */
999 this->first_payload = current_payload_type;
1000 }
1001 else
1002 {
1003 /* no, set the next_type of the previous payload to the current type */
1004 previous_payload->set_next_type(previous_payload, current_payload_type);
1005 }
1006
1007 /* all encrypted payloads are added to the payload list */
1008 while (encryption_payload->get_payload_count(encryption_payload) > 0)
1009 {
1010 encryption_payload->remove_first_payload(encryption_payload, &current_encrypted_payload);
1011 DBG2(DBG_ENC, "insert unencrypted payload of type %N at end of list",
1012 payload_type_names, current_encrypted_payload->get_type(current_encrypted_payload));
1013 this->payloads->insert_last(this->payloads,current_encrypted_payload);
1014 }
1015
1016 /* encryption payload is processed, payloads are moved. Destroy it. */
1017 encryption_payload->destroy(encryption_payload);
1018 }
1019
1020 /* we allow unknown payloads of any type and don't bother if it was encrypted. Not our problem. */
1021 if (current_payload_type != UNKNOWN_PAYLOAD && current_payload_type != NO_PAYLOAD)
1022 {
1023 /* get the ruleset for found payload */
1024 status = get_payload_rule(this, current_payload_type, &payload_rule);
1025 if (status != SUCCESS)
1026 {
1027 /* payload is not allowed */
1028 DBG1(DBG_ENC, "payload type %N not allowed",
1029 payload_type_names, current_payload_type);
1030 iterator->destroy(iterator);
1031 return VERIFY_ERROR;
1032 }
1033
1034 /* check if the payload was encrypted, and if it should been have encrypted */
1035 if (payload_rule->encrypted != current_payload_was_encrypted)
1036 {
1037 /* payload was not encrypted, but should have been. or vice-versa */
1038 DBG1(DBG_ENC, "payload type %N should be %s!",
1039 payload_type_names, current_payload_type,
1040 (payload_rule->encrypted) ? "encrypted" : "not encrypted");
1041 iterator->destroy(iterator);
1042 return VERIFY_ERROR;
1043 }
1044 }
1045 /* advance to the next payload */
1046 payload_number++;
1047 /* is stored to set next payload in case of found encryption payload */
1048 previous_payload = current_payload;
1049 }
1050 iterator->destroy(iterator);
1051 return SUCCESS;
1052 }
1053
1054 /**
1055 * Implementation of private_message_t.verify.
1056 */
1057 static status_t verify(private_message_t *this)
1058 {
1059 int i;
1060 iterator_t *iterator;
1061 payload_t *current_payload;
1062 size_t total_found_payloads = 0;
1063
1064 DBG2(DBG_ENC, "verifying message structure");
1065
1066 iterator = this->payloads->create_iterator(this->payloads,TRUE);
1067 /* check for payloads with wrong count*/
1068 for (i = 0; i < this->message_rule->payload_rule_count;i++)
1069 {
1070 size_t found_payloads = 0;
1071
1072 /* check all payloads for specific rule */
1073 iterator->reset(iterator);
1074
1075 while(iterator->iterate(iterator,(void **)&current_payload))
1076 {
1077 payload_type_t current_payload_type;
1078
1079 current_payload_type = current_payload->get_type(current_payload);
1080 if (current_payload_type == UNKNOWN_PAYLOAD)
1081 {
1082 /* unknown payloads are ignored, IF they are not critical */
1083 unknown_payload_t *unknown_payload = (unknown_payload_t*)current_payload;
1084 if (unknown_payload->is_critical(unknown_payload))
1085 {
1086 DBG1(DBG_ENC, "%N is not supported, but its critical!",
1087 payload_type_names, current_payload_type);
1088 iterator->destroy(iterator);
1089 return NOT_SUPPORTED;
1090 }
1091 }
1092 else if (current_payload_type == this->message_rule->payload_rules[i].payload_type)
1093 {
1094 found_payloads++;
1095 total_found_payloads++;
1096 DBG2(DBG_ENC, "found payload of type %N",
1097 payload_type_names, this->message_rule->payload_rules[i].payload_type);
1098
1099 /* as soon as ohe payload occures more then specified, the verification fails */
1100 if (found_payloads > this->message_rule->payload_rules[i].max_occurence)
1101 {
1102 DBG1(DBG_ENC, "payload of type %N more than %d times (%d) occured in current message",
1103 payload_type_names, current_payload_type,
1104 this->message_rule->payload_rules[i].max_occurence, found_payloads);
1105 iterator->destroy(iterator);
1106 return VERIFY_ERROR;
1107 }
1108 }
1109 }
1110
1111 if (found_payloads < this->message_rule->payload_rules[i].min_occurence)
1112 {
1113 DBG1(DBG_ENC, "payload of type %N not occured %d times (%d)",
1114 payload_type_names, this->message_rule->payload_rules[i].payload_type,
1115 this->message_rule->payload_rules[i].min_occurence, found_payloads);
1116 iterator->destroy(iterator);
1117 return VERIFY_ERROR;
1118 }
1119 if ((this->message_rule->payload_rules[i].sufficient) && (this->payloads->get_count(this->payloads) == total_found_payloads))
1120 {
1121 iterator->destroy(iterator);
1122 return SUCCESS;
1123 }
1124 }
1125 iterator->destroy(iterator);
1126 return SUCCESS;
1127 }
1128
1129 /**
1130 * Implementation of message_t.parse_body.
1131 */
1132 static status_t parse_body(private_message_t *this, crypter_t *crypter, signer_t *signer)
1133 {
1134 status_t status = SUCCESS;
1135 payload_type_t current_payload_type;
1136 char str[128];
1137
1138 current_payload_type = this->first_payload;
1139
1140 DBG2(DBG_ENC, "parsing body of message, first payload is %N",
1141 payload_type_names, current_payload_type);
1142
1143 /* parse payload for payload, while there are more available */
1144 while ((current_payload_type != NO_PAYLOAD))
1145 {
1146 payload_t *current_payload;
1147
1148 DBG2(DBG_ENC, "starting parsing a %N payload",
1149 payload_type_names, current_payload_type);
1150
1151 /* parse current payload */
1152 status = this->parser->parse_payload(this->parser,current_payload_type,(payload_t **) &current_payload);
1153
1154 if (status != SUCCESS)
1155 {
1156 DBG1(DBG_ENC, "payload type %N could not be parsed",
1157 payload_type_names, current_payload_type);
1158 return PARSE_ERROR;
1159 }
1160
1161 DBG2(DBG_ENC, "verifying payload of type %N",
1162 payload_type_names, current_payload_type);
1163
1164 /* verify it, stop parsig if its invalid */
1165 status = current_payload->verify(current_payload);
1166 if (status != SUCCESS)
1167 {
1168 DBG1(DBG_ENC, "%N payload verification failed",
1169 payload_type_names, current_payload_type);
1170 current_payload->destroy(current_payload);
1171 return VERIFY_ERROR;
1172 }
1173
1174 DBG2(DBG_ENC, "%N payload verified. Adding to payload list",
1175 payload_type_names, current_payload_type);
1176 this->payloads->insert_last(this->payloads,current_payload);
1177
1178 /* an encryption payload is the last one, so STOP here. decryption is done later */
1179 if (current_payload_type == ENCRYPTED)
1180 {
1181 DBG2(DBG_ENC, "%N payload found. Stop parsing",
1182 payload_type_names, current_payload_type);
1183 break;
1184 }
1185
1186 /* get next payload type */
1187 current_payload_type = current_payload->get_next_type(current_payload);
1188 }
1189
1190 if (current_payload_type == ENCRYPTED)
1191 {
1192 status = decrypt_payloads(this,crypter,signer);
1193 if (status != SUCCESS)
1194 {
1195 DBG1(DBG_ENC, "could not decrypt payloads");
1196 return status;
1197 }
1198 }
1199
1200 status = verify(this);
1201 if (status != SUCCESS)
1202 {
1203 return status;
1204 }
1205
1206 get_string(this, str, sizeof(str));
1207 DBG1(DBG_ENC, "parsed %s", str);
1208
1209 return SUCCESS;
1210 }
1211
1212 /**
1213 * Implementation of message_t.destroy.
1214 */
1215 static void destroy (private_message_t *this)
1216 {
1217 DESTROY_IF(this->ike_sa_id);
1218 this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
1219 this->packet->destroy(this->packet);
1220 this->parser->destroy(this->parser);
1221 free(this);
1222 }
1223
1224 /*
1225 * Described in Header-File
1226 */
1227 message_t *message_create_from_packet(packet_t *packet)
1228 {
1229 private_message_t *this = malloc_thing(private_message_t);
1230
1231 /* public functions */
1232 this->public.set_major_version = (void(*)(message_t*, u_int8_t))set_major_version;
1233 this->public.get_major_version = (u_int8_t(*)(message_t*))get_major_version;
1234 this->public.set_minor_version = (void(*)(message_t*, u_int8_t))set_minor_version;
1235 this->public.get_minor_version = (u_int8_t(*)(message_t*))get_minor_version;
1236 this->public.set_message_id = (void(*)(message_t*, u_int32_t))set_message_id;
1237 this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
1238 this->public.get_initiator_spi = (u_int64_t(*)(message_t*))get_initiator_spi;
1239 this->public.get_responder_spi = (u_int64_t(*)(message_t*))get_responder_spi;
1240 this->public.set_ike_sa_id = (void(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
1241 this->public.get_ike_sa_id = (ike_sa_id_t*(*)(message_t*))get_ike_sa_id;
1242 this->public.set_exchange_type = (void(*)(message_t*, exchange_type_t))set_exchange_type;
1243 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
1244 this->public.set_request = (void(*)(message_t*, bool))set_request;
1245 this->public.get_request = (bool(*)(message_t*))get_request;
1246 this->public.add_payload = (void(*)(message_t*,payload_t*))add_payload;
1247 this->public.add_notify = (void(*)(message_t*,bool,notify_type_t,chunk_t))add_notify;
1248 this->public.generate = (status_t (*) (message_t *,crypter_t*,signer_t*,packet_t**)) generate;
1249 this->public.set_source = (void (*) (message_t*,host_t*)) set_source;
1250 this->public.get_source = (host_t * (*) (message_t*)) get_source;
1251 this->public.set_destination = (void (*) (message_t*,host_t*)) set_destination;
1252 this->public.get_destination = (host_t * (*) (message_t*)) get_destination;
1253 this->public.get_payload_iterator = (iterator_t * (*) (message_t *)) get_payload_iterator;
1254 this->public.get_payload = (payload_t * (*) (message_t *, payload_type_t)) get_payload;
1255 this->public.parse_header = (status_t (*) (message_t *)) parse_header;
1256 this->public.parse_body = (status_t (*) (message_t *,crypter_t*,signer_t*)) parse_body;
1257 this->public.get_packet = (packet_t * (*) (message_t*)) get_packet;
1258 this->public.get_packet_data = (chunk_t (*) (message_t *this)) get_packet_data;
1259 this->public.destroy = (void(*)(message_t*))destroy;
1260
1261 /* private values */
1262 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
1263 this->is_request = TRUE;
1264 this->ike_sa_id = NULL;
1265 this->first_payload = NO_PAYLOAD;
1266 this->message_id = 0;
1267
1268 /* private values */
1269 if (packet == NULL)
1270 {
1271 packet = packet_create();
1272 }
1273 this->message_rule = NULL;
1274 this->packet = packet;
1275 this->payloads = linked_list_create();
1276
1277 /* parser is created from data of packet */
1278 this->parser = parser_create(this->packet->get_data(this->packet));
1279
1280 return (&this->public);
1281 }
1282
1283 /*
1284 * Described in Header.
1285 */
1286 message_t *message_create()
1287 {
1288 return message_create_from_packet(NULL);
1289 }