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