11a814ba1b9e65d2b8bee9cdc073c27e98c3d79b
[strongswan.git] / Source / charon / encoding / message.c
1 /**
2 * @file message.c
3 *
4 * @brief Implementation of message_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <stdlib.h>
24
25 #include "message.h"
26
27 #include <types.h>
28 #include <daemon.h>
29 #include <sa/ike_sa_id.h>
30 #include <encoding/generator.h>
31 #include <encoding/parser.h>
32 #include <utils/linked_list.h>
33 #include <utils/allocator.h>
34 #include <utils/logger_manager.h>
35 #include <encoding/payloads/encodings.h>
36 #include <encoding/payloads/payload.h>
37 #include <encoding/payloads/encryption_payload.h>
38
39
40 typedef struct supported_payload_entry_t supported_payload_entry_t;
41
42 /**
43 * Supported payload entry used in message_rule_t.
44 *
45 */
46 struct supported_payload_entry_t {
47 /**
48 * Payload type.
49 */
50 payload_type_t payload_type;
51
52 /**
53 * Minimal occurence of this payload.
54 */
55 size_t min_occurence;
56
57 /**
58 * Max occurence of this payload.
59 */
60 size_t max_occurence;
61
62 /**
63 * TRUE if payload has to get encrypted
64 */
65 bool encrypted;
66 };
67
68 typedef struct message_rule_t message_rule_t;
69
70 /**
71 * Message Rule used to find out which payloads
72 * are supported by each message type.
73 *
74 */
75 struct message_rule_t {
76 /**
77 * Type of message.
78 */
79 exchange_type_t exchange_type;
80
81 /**
82 * Is message a request or response.
83 */
84 bool is_request;
85 /**
86 * Number of supported payloads.
87 */
88 size_t supported_payloads_count;
89 /**
90 * Pointer to first supported payload entry.
91 */
92 supported_payload_entry_t *supported_payloads;
93 };
94
95 /**
96 * Message rule for IKE_SA_INIT from initiator.
97 */
98 static supported_payload_entry_t supported_ike_sa_init_i_payloads[] =
99 {
100 {SECURITY_ASSOCIATION,1,1,FALSE},
101 {KEY_EXCHANGE,1,1,FALSE},
102 {NONCE,1,1,FALSE},
103 };
104
105 /**
106 * Message rule for IKE_SA_INIT from responder.
107 */
108 static supported_payload_entry_t supported_ike_sa_init_r_payloads[] =
109 {
110 {SECURITY_ASSOCIATION,1,1,FALSE},
111 {KEY_EXCHANGE,1,1,FALSE},
112 {NONCE,1,1,FALSE},
113 };
114
115 /**
116 * Message rule for IKE_AUTH from initiator.
117 */
118 static supported_payload_entry_t supported_ike_auth_i_payloads[] =
119 {
120 {ID_INITIATOR,1,1,TRUE},
121 {CERTIFICATE,0,1,TRUE},
122 {CERTIFICATE_REQUEST,0,1,TRUE},
123 {ID_RESPONDER,0,1,TRUE},
124 {AUTHENTICATION,1,1,TRUE},
125 {SECURITY_ASSOCIATION,1,1,TRUE},
126 {TRAFFIC_SELECTOR_INITIATOR,1,1,TRUE},
127 {TRAFFIC_SELECTOR_RESPONDER,1,1,TRUE},
128 };
129
130 /**
131 * Message rule for IKE_AUTH from responder.
132 */
133 static supported_payload_entry_t supported_ike_auth_r_payloads[] =
134 {
135 {CERTIFICATE,0,1,TRUE},
136 {ID_RESPONDER,0,1,TRUE},
137 {AUTHENTICATION,1,1,TRUE},
138 {SECURITY_ASSOCIATION,1,1,TRUE},
139 {TRAFFIC_SELECTOR_INITIATOR,1,1,TRUE},
140 {TRAFFIC_SELECTOR_RESPONDER,1,1,TRUE},
141 };
142
143 /**
144 * Message rules, defines allowed payloads.
145 */
146 static message_rule_t message_rules[] = {
147 {IKE_SA_INIT,TRUE,(sizeof(supported_ike_sa_init_i_payloads)/sizeof(supported_payload_entry_t)),supported_ike_sa_init_i_payloads},
148 {IKE_SA_INIT,FALSE,(sizeof(supported_ike_sa_init_r_payloads)/sizeof(supported_payload_entry_t)),supported_ike_sa_init_r_payloads},
149 {IKE_AUTH,TRUE,(sizeof(supported_ike_auth_i_payloads)/sizeof(supported_payload_entry_t)),supported_ike_auth_i_payloads},
150 {IKE_AUTH,FALSE,(sizeof(supported_ike_auth_r_payloads)/sizeof(supported_payload_entry_t)),supported_ike_auth_r_payloads}
151 };
152
153 typedef struct payload_entry_t payload_entry_t;
154
155 /**
156 * Entry for a payload in the internal used linked list.
157 *
158 */
159 struct payload_entry_t {
160 /**
161 * Type of payload.
162 */
163 payload_type_t payload_type;
164 /**
165 * Data struct holding the data of given payload.
166 */
167 void *data_struct;
168 };
169
170
171 typedef struct private_message_t private_message_t;
172
173 /**
174 * Private data of an message_t object.
175 */
176 struct private_message_t {
177
178 /**
179 * Public part of a message_t object.
180 */
181 message_t public;
182
183 /**
184 * Minor version of message.
185 */
186 u_int8_t major_version;
187
188 /**
189 * Major version of message.
190 */
191 u_int8_t minor_version;
192
193 /**
194 * First Payload in message.
195 */
196 payload_type_t first_payload;
197
198 /**
199 * Assigned exchange type.
200 */
201 exchange_type_t exchange_type;
202
203
204 /**
205 * TRUE if message is request.
206 * FALSE if message is reply.
207 */
208 bool is_request;
209
210 /**
211 * Message ID of this message.
212 */
213 u_int32_t message_id;
214
215 /**
216 * ID of assigned IKE_SA.
217 */
218 ike_sa_id_t *ike_sa_id;
219
220 /**
221 * Assigned UDP packet.
222 *
223 * Stores incoming packet or last generated one.
224 */
225 packet_t *packet;
226
227 /**
228 * Linked List where payload data are stored in.
229 */
230 linked_list_t *payloads;
231
232 /**
233 * Assigned parser to parse Header and Body of this message.
234 */
235 parser_t *parser;
236
237 /**
238 * Assigned logger.
239 */
240 logger_t *logger;
241
242 /**
243 * Gets a list of supported payloads of this message type
244 *
245 * @param this calling object
246 * @param[out] supported_payloads first entry of supported payloads
247 * @param[out] supported_payloads_count number of supported payload entries
248 *
249 * @return
250 * - SUCCESS
251 * - NOT_FOUND if no supported payload definition could be found
252 */
253 status_t (*get_supported_payloads) (private_message_t *this, supported_payload_entry_t **supported_payloads,size_t *supported_payloads_count);
254
255 /**
256 * Encrypts all payloads which has to get encrypted.
257 *
258 * @param this calling object
259 */
260 status_t (*encrypt_payloads) (private_message_t *this,crypter_t *crypter, signer_t* signer);
261
262 };
263
264 /**
265 * Implementation of private_message_t.get_supported_payloads.
266 */
267 status_t get_supported_payloads (private_message_t *this, supported_payload_entry_t **supported_payloads,size_t *supported_payloads_count)
268 {
269 int i;
270 exchange_type_t exchange_type = this->public.get_exchange_type(&(this->public));
271 bool is_request = this->public.get_request(&(this->public));
272
273
274 for (i = 0; i < (sizeof(message_rules) / sizeof(message_rule_t)); i++)
275 {
276 if ((exchange_type == message_rules[i].exchange_type) &&
277 (is_request == message_rules[i].is_request))
278 {
279 /* found rule for given exchange_type*/
280 *supported_payloads = message_rules[i].supported_payloads;
281 *supported_payloads_count = message_rules[i].supported_payloads_count;
282
283 return SUCCESS;
284 }
285
286
287 }
288 *supported_payloads = NULL;
289 *supported_payloads_count = 0;
290 return NOT_FOUND;
291 }
292
293 /**
294 * Implementation of message_t.set_ike_sa_id.
295 */
296 static void set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id)
297 {
298 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
299 }
300
301 /**
302 * Implementation of message_t.get_ike_sa_id.
303 */
304 static status_t get_ike_sa_id (private_message_t *this,ike_sa_id_t **ike_sa_id)
305 {
306 if (this->ike_sa_id == NULL)
307 {
308 return FAILED;
309 }
310 *ike_sa_id = this->ike_sa_id->clone(this->ike_sa_id);
311 return SUCCESS;
312 }
313
314 /**
315 * Implementation of message_t.set_message_id.
316 */
317 static void set_message_id (private_message_t *this,u_int32_t message_id)
318 {
319 this->message_id = message_id;
320 }
321
322 /**
323 * Implementation of message_t.get_message_id.
324 */
325 static u_int32_t get_message_id (private_message_t *this)
326 {
327 return this->message_id;
328 }
329
330 /**
331 * Implementation of message_t.get_responder_spi.
332 */
333 static u_int64_t get_responder_spi (private_message_t *this)
334 {
335 return (this->ike_sa_id->get_responder_spi(this->ike_sa_id));
336 }
337
338 /**
339 * Implementation of message_t.set_major_version.
340 */
341 static void set_major_version (private_message_t *this,u_int8_t major_version)
342 {
343 this->major_version = major_version;
344 }
345
346
347 /**
348 * Implementation of message_t.set_major_version.
349 */
350 static u_int8_t get_major_version (private_message_t *this)
351 {
352 return this->major_version;
353 }
354
355 /**
356 * Implementation of message_t.set_minor_version.
357 */
358 static void set_minor_version (private_message_t *this,u_int8_t minor_version)
359 {
360 this->minor_version = minor_version;
361 }
362
363 /**
364 * Implementation of message_t.get_minor_version.
365 */
366 static u_int8_t get_minor_version (private_message_t *this)
367 {
368 return this->minor_version;
369 }
370
371 /**
372 * Implementation of message_t.set_exchange_type.
373 */
374 static void set_exchange_type (private_message_t *this,exchange_type_t exchange_type)
375 {
376 this->exchange_type = exchange_type;
377 }
378
379 /**
380 * Implementation of message_t.get_exchange_type.
381 */
382 static exchange_type_t get_exchange_type (private_message_t *this)
383 {
384 return this->exchange_type;
385 }
386
387 /**
388 * Implementation of message_t.set_request.
389 */
390 static void set_request (private_message_t *this,bool request)
391 {
392 this->is_request = request;
393 }
394
395 /**
396 * Implementation of message_t.get_request.
397 */
398 static exchange_type_t get_request (private_message_t *this)
399 {
400 return this->is_request;
401 }
402
403 /**
404 * Implementation of message_t.add_payload.
405 */
406 static void add_payload(private_message_t *this, payload_t *payload)
407 {
408 payload_t *last_payload;
409 if (this->payloads->get_count(this->payloads) > 0)
410 {
411 this->payloads->get_last(this->payloads,(void **) &last_payload);
412 }
413
414 this->payloads->insert_last(this->payloads, payload);
415
416 if (this->payloads->get_count(this->payloads) == 1)
417 {
418 this->first_payload = payload->get_type(payload);
419 }
420 else
421 {
422 last_payload->set_next_type(last_payload,payload->get_type(payload));
423 }
424
425 this->logger->log(this->logger, CONTROL|MORE, "added payload of type %s to message",
426 mapping_find(payload_type_m, payload->get_type(payload)));
427
428 }
429
430 /**
431 * Implementation of message_t.set_source.
432 */
433 static void set_source(private_message_t *this, host_t *host)
434 {
435 if (this->packet->source != NULL)
436 {
437 this->packet->source->destroy(this->packet->source);
438 }
439 this->packet->source = host;
440 }
441
442 /**
443 * Implementation of message_t.set_destination.
444 */
445 static void set_destination(private_message_t *this, host_t *host)
446 {
447 if (this->packet->destination != NULL)
448 {
449 this->packet->destination->destroy(this->packet->destination);
450 }
451 this->packet->destination = host;
452 }
453
454 /**
455 * Implementation of message_t.get_source.
456 */
457 static void get_source(private_message_t *this, host_t **host)
458 {
459 *host = this->packet->source;
460 }
461
462 /**
463 * Implementation of message_t.get_destination.
464 */
465 static void get_destination(private_message_t *this, host_t **host)
466 {
467 *host = this->packet->destination;
468 }
469
470 /**
471 * Implementation of message_t.get_destination.
472 */
473 static iterator_t *get_payload_iterator(private_message_t *this)
474 {
475 return this->payloads->create_iterator(this->payloads, TRUE);
476 }
477
478
479 /**
480 * Implementation of message_t.generate.
481 */
482 static status_t generate(private_message_t *this, crypter_t *crypter, signer_t* signer, packet_t **packet)
483 {
484 generator_t *generator;
485 ike_header_t *ike_header;
486 payload_t *payload, *next_payload;
487 iterator_t *iterator;
488 status_t status;
489
490
491 this->logger->log(this->logger, CONTROL, "generating message, contains %d payloads",
492 this->payloads->get_count(this->payloads));
493
494 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
495 {
496 this->logger->log(this->logger, ERROR, "exchange type is not defined");
497 return INVALID_STATE;
498 }
499
500 if (this->packet->source == NULL ||
501 this->packet->destination == NULL)
502 {
503 this->logger->log(this->logger, ERROR, "source/destination not defined");
504 return INVALID_STATE;
505 }
506
507 status = this->encrypt_payloads(this,crypter,signer);
508 if (status != SUCCESS)
509 {
510 this->logger->log(this->logger, ERROR, "Could not encrypt payloads");
511 return status;
512 }
513
514 /* build ike header */
515 ike_header = ike_header_create();
516
517 ike_header->set_exchange_type(ike_header, this->exchange_type);
518 ike_header->set_message_id(ike_header, this->message_id);
519 ike_header->set_response_flag(ike_header, !this->is_request);
520 ike_header->set_initiator_flag(ike_header, this->ike_sa_id->is_initiator(this->ike_sa_id));
521 ike_header->set_initiator_spi(ike_header, this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
522 ike_header->set_responder_spi(ike_header, this->ike_sa_id->get_responder_spi(this->ike_sa_id));
523
524
525 generator = generator_create();
526
527 payload = (payload_t*)ike_header;
528
529 iterator = this->payloads->create_iterator(this->payloads, TRUE);
530
531 /* generate every payload, except last one */
532 while(iterator->has_next(iterator))
533 {
534 iterator->current(iterator, (void**)&next_payload);
535 payload->set_next_type(payload, next_payload->get_type(next_payload));
536 generator->generate_payload(generator, payload);
537
538 payload = next_payload;
539 }
540 iterator->destroy(iterator);
541
542 /* build last payload */
543 payload->set_next_type(payload, NO_PAYLOAD);
544 /* if it's an encryption payload, build it first */
545
546 generator->generate_payload(generator, payload);
547 ike_header->destroy(ike_header);
548
549 /* build packet */
550 if (this->packet->data.ptr != NULL)
551 {
552 allocator_free(this->packet->data.ptr);
553 }
554 generator->write_to_chunk(generator, &(this->packet->data));
555 generator->destroy(generator);
556
557 /* append integrity checksum if necessary */
558 if (payload->get_type(payload) == ENCRYPTED)
559 {
560 encryption_payload_t *encryption_payload = (encryption_payload_t*)payload;
561 status = encryption_payload->build_signature(encryption_payload, this->packet->data);
562 if (status != SUCCESS)
563 {
564 return status;
565 }
566 }
567
568 /* clone packet for caller */
569 *packet = this->packet->clone(this->packet);
570
571 this->logger->log(this->logger, CONTROL, "message generated successfully");
572 return SUCCESS;
573 }
574
575 /**
576 * Implements message_t.parse_header.
577 */
578 static status_t parse_header(private_message_t *this)
579 {
580 ike_header_t *ike_header;
581 status_t status;
582
583
584 this->logger->log(this->logger, CONTROL, "parsing header of message");
585
586 this->parser->reset_context(this->parser);
587 status = this->parser->parse_payload(this->parser,HEADER,(payload_t **) &ike_header);
588 if (status != SUCCESS)
589 {
590 this->logger->log(this->logger, ERROR, "Header could not be parsed");
591 return status;
592
593 }
594
595 /* verify payload */
596 status = ike_header->payload_interface.verify(&(ike_header->payload_interface));
597 if (status != SUCCESS)
598 {
599 this->logger->log(this->logger, ERROR, "Header verification failed");
600 ike_header->destroy(ike_header);
601 return status;
602 }
603
604 if (this->ike_sa_id != NULL)
605 {
606 this->ike_sa_id->destroy(this->ike_sa_id);
607 }
608
609 this->ike_sa_id = ike_sa_id_create(ike_header->get_initiator_spi(ike_header),
610 ike_header->get_responder_spi(ike_header),
611 ike_header->get_initiator_flag(ike_header));
612
613 this->exchange_type = ike_header->get_exchange_type(ike_header);
614 this->message_id = ike_header->get_message_id(ike_header);
615 this->is_request = (!(ike_header->get_response_flag(ike_header)));
616 this->major_version = ike_header->get_maj_version(ike_header);
617 this->minor_version = ike_header->get_min_version(ike_header);
618 this->first_payload = ike_header->payload_interface.get_next_type(&(ike_header->payload_interface));
619
620
621 this->logger->log(this->logger, CONTROL, "parsing header successfully");
622
623 ike_header->destroy(ike_header);
624 return SUCCESS;
625 }
626
627 /**
628 * Implements message_t.parse_body.
629 */
630 static status_t parse_body(private_message_t *this, crypter_t *crypter, signer_t *signer)
631 {
632 status_t status = SUCCESS;
633 payload_type_t current_payload_type = this->first_payload;
634
635 this->logger->log(this->logger, CONTROL, "parsing body of message");
636
637 while (current_payload_type != NO_PAYLOAD)
638 {
639 payload_t *current_payload;
640
641 this->logger->log(this->logger, CONTROL|MORE, "start parsing payload of type %s",
642 mapping_find(payload_type_m, current_payload_type));
643
644 status = this->parser->parse_payload(this->parser,current_payload_type,(payload_t **) &current_payload);
645 if (status != SUCCESS)
646 {
647 this->logger->log(this->logger, ERROR, "payload type %s could not be parsed",mapping_find(payload_type_m,current_payload_type));
648 return status;
649 }
650
651 status = current_payload->verify(current_payload);
652 if (status != SUCCESS)
653 {
654 this->logger->log(this->logger, ERROR, "payload type %s could not be verified",mapping_find(payload_type_m,current_payload_type));
655 current_payload->destroy(current_payload);
656 status = VERIFY_ERROR;
657 return status;
658 }
659
660 /* encrypted payload must be decrypted */
661 if (current_payload->get_type(current_payload) == ENCRYPTED)
662 {
663 encryption_payload_t *encryption_payload = (encryption_payload_t*)current_payload;
664 encryption_payload->set_transforms(encryption_payload, crypter, signer);
665 status = encryption_payload->verify_signature(encryption_payload, this->packet->data);
666 if (status != SUCCESS)
667 {
668 this->logger->log(this->logger, ERROR, "encryption payload signature invaild");
669 current_payload->destroy(current_payload);
670 return status;
671 }
672 status = encryption_payload->decrypt(encryption_payload);
673 if (status != SUCCESS)
674 {
675 this->logger->log(this->logger, ERROR, "parsing decrypted encryption payload failed");
676 current_payload->destroy(current_payload);
677 return status;
678 }
679 }
680
681 /* get next payload type */
682 current_payload_type = current_payload->get_next_type(current_payload);
683
684 this->payloads->insert_last(this->payloads,current_payload);
685
686 }
687 return this->public.verify(&(this->public));
688
689 }
690
691 /**
692 * implements message_t.verify
693 */
694 static status_t verify(private_message_t *this)
695 {
696 iterator_t *iterator;
697 status_t status;
698 int i;
699 supported_payload_entry_t *supported_payloads;
700 size_t supported_payloads_count;
701
702 this->logger->log(this->logger, CONTROL|MORE, "verifying message");
703
704 status = this->get_supported_payloads(this, &supported_payloads, &supported_payloads_count);
705 if (status != SUCCESS)
706 {
707 this->logger->log(this->logger, ERROR, "could not get supported payloads: %s");
708 return status;
709 }
710 iterator = this->payloads->create_iterator(this->payloads,TRUE);
711 /* check for payloads with wrong count*/
712 for (i = 0; i < supported_payloads_count;i++)
713 {
714 size_t min_occurence = supported_payloads[i].min_occurence;
715 size_t max_occurence = supported_payloads[i].max_occurence;
716 payload_type_t payload_type = supported_payloads[i].payload_type;
717 size_t found_payloads = 0;
718
719 iterator->reset(iterator);
720
721 while(iterator->has_next(iterator))
722 {
723 payload_t *current_payload;
724 iterator->current(iterator,(void **)&current_payload);
725
726
727 if (current_payload->get_type(current_payload) == payload_type)
728 {
729 found_payloads++;
730 if (found_payloads > max_occurence)
731 {
732 this->logger->log(this->logger, ERROR, "Payload of type %s more than %d times (%d) occured in current message",
733 mapping_find(payload_type_m,current_payload->get_type(current_payload)),max_occurence,found_payloads);
734 iterator->destroy(iterator);
735 return NOT_SUPPORTED;
736 }
737 }
738 }
739 if (found_payloads < min_occurence)
740 {
741 this->logger->log(this->logger, ERROR, "Payload of type %s not occured %d times",
742 mapping_find(payload_type_m,payload_type),min_occurence);
743 iterator->destroy(iterator);
744 return NOT_SUPPORTED;
745 }
746 }
747 iterator->destroy(iterator);
748
749 return SUCCESS;
750 }
751
752
753 static status_t encrypt_payloads (private_message_t *this,crypter_t *crypter, signer_t* signer)
754 {
755 status_t status;
756 supported_payload_entry_t *supported_payloads;
757 size_t supported_payloads_count;
758 encryption_payload_t *encryption_payload = NULL;
759 linked_list_t *all_payloads = linked_list_create();
760 int i;
761
762 status = this->get_supported_payloads(this, &supported_payloads, &supported_payloads_count);
763 if (status != SUCCESS)
764 {
765 return status;
766 }
767
768 /* first copy all payloads in a temporary list */
769 while (this->payloads->get_count(this->payloads) > 0)
770 {
771 void *current_payload;
772 this->payloads->remove_first(this->payloads,&current_payload);
773 all_payloads->insert_last(all_payloads,current_payload);
774 }
775
776 while (all_payloads->get_count(all_payloads) > 0)
777 {
778 payload_t *current_payload;
779 bool to_encrypt = FALSE;
780
781 all_payloads->remove_first(all_payloads,(void **)&current_payload);
782
783 for (i = 0; i < supported_payloads_count;i++)
784 {
785 if ((supported_payloads[i].payload_type == current_payload->get_type(current_payload)) &&
786 (supported_payloads[i].encrypted))
787 {
788 to_encrypt = TRUE;
789 break;
790 }
791 }
792
793 if (to_encrypt)
794 {
795 if (encryption_payload == NULL)
796 {
797 encryption_payload = encryption_payload_create();
798 }
799 encryption_payload->add_payload(encryption_payload,current_payload);
800 }
801 else
802 {
803 this->payloads->insert_last(this->payloads,current_payload);
804 }
805 }
806
807 status = SUCCESS;
808 if (encryption_payload != NULL)
809 {
810 encryption_payload->set_transforms(encryption_payload,crypter,signer);
811 status = encryption_payload->encrypt(encryption_payload);
812 this->payloads->insert_last(this->payloads,encryption_payload);
813 }
814
815 all_payloads->destroy(all_payloads);
816
817 return status;
818 }
819
820 /**
821 * Implements message_t's destroy function.
822 * See #message_s.destroy.
823 */
824 static void destroy (private_message_t *this)
825 {
826 iterator_t *iterator;
827
828 this->packet->destroy(this->packet);
829
830 if (this->ike_sa_id != NULL)
831 {
832 this->ike_sa_id->destroy(this->ike_sa_id);
833 }
834
835 iterator = this->payloads->create_iterator(this->payloads, TRUE);
836 while (iterator->has_next(iterator))
837 {
838 payload_t *payload;
839 iterator->current(iterator, (void**)&payload);
840 this->logger->log(this->logger, CONTROL|MOST, "Destroying payload of type %s",
841 mapping_find(payload_type_m, payload->get_type(payload)));
842 payload->destroy(payload);
843 }
844 iterator->destroy(iterator);
845 this->payloads->destroy(this->payloads);
846 this->parser->destroy(this->parser);
847 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
848
849 allocator_free(this);
850 }
851
852 /*
853 * Described in Header-File
854 */
855 message_t *message_create_from_packet(packet_t *packet)
856 {
857 private_message_t *this = allocator_alloc_thing(private_message_t);
858
859 /* public functions */
860 this->public.set_major_version = (void(*)(message_t*, u_int8_t))set_major_version;
861 this->public.get_major_version = (u_int8_t(*)(message_t*))get_major_version;
862 this->public.set_minor_version = (void(*)(message_t*, u_int8_t))set_minor_version;
863 this->public.get_minor_version = (u_int8_t(*)(message_t*))get_minor_version;
864 this->public.set_message_id = (void(*)(message_t*, u_int32_t))set_message_id;
865 this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
866 this->public.get_responder_spi = (u_int64_t(*)(message_t*))get_responder_spi;
867 this->public.set_ike_sa_id = (void(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
868 this->public.get_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t **))get_ike_sa_id;
869 this->public.set_exchange_type = (void(*)(message_t*, exchange_type_t))set_exchange_type;
870 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
871 this->public.set_request = (void(*)(message_t*, bool))set_request;
872 this->public.get_request = (bool(*)(message_t*))get_request;
873 this->public.add_payload = (void(*)(message_t*,payload_t*))add_payload;
874 this->public.generate = (status_t (*) (message_t *,crypter_t*,signer_t*,packet_t**)) generate;
875 this->public.set_source = (void (*) (message_t*,host_t*)) set_source;
876 this->public.get_source = (void (*) (message_t*,host_t**)) get_source;
877 this->public.set_destination = (void (*) (message_t*,host_t*)) set_destination;
878 this->public.get_destination = (void (*) (message_t*,host_t**)) get_destination;
879 this->public.get_payload_iterator = (iterator_t * (*) (message_t *)) get_payload_iterator;
880 this->public.parse_header = (status_t (*) (message_t *)) parse_header;
881 this->public.parse_body = (status_t (*) (message_t *,crypter_t*,signer_t*)) parse_body;
882 this->public.verify = (status_t (*) (message_t*)) verify;
883 this->public.destroy = (void(*)(message_t*))destroy;
884
885 /* private values */
886 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
887 this->is_request = TRUE;
888 this->ike_sa_id = NULL;
889 this->first_payload = NO_PAYLOAD;
890 this->message_id = 0;
891
892 /* private functions */
893 this->get_supported_payloads = get_supported_payloads;
894 this->encrypt_payloads = encrypt_payloads;
895
896 /* private values */
897 if (packet == NULL)
898 {
899 packet = packet_create();
900 }
901 this->packet = packet;
902 this->payloads = linked_list_create();
903
904 /* parser is created from data of packet */
905 this->parser = parser_create(this->packet->data);
906
907 this->logger = charon->logger_manager->create_logger(charon->logger_manager, MESSAGE, NULL);
908
909 return (&this->public);
910 }
911
912 /*
913 * Described in Header-File
914 */
915 message_t *message_create()
916 {
917 return message_create_from_packet(NULL);
918 }