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