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