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