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