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