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