- parse_body improved
[strongswan.git] / Source / charon / message.c
1 /**
2 * @file message.c
3 *
4 * @brief Class message_t. Object of this type represents an IKEv2-Message.
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 "globals.h"
29 #include "ike_sa_id.h"
30 #include "generator.h"
31 #include "utils/linked_list.h"
32 #include "utils/allocator.h"
33 #include "utils/logger_manager.h"
34 #include "payloads/encodings.h"
35 #include "payloads/payload.h"
36 #include "parser.h"
37
38
39 /**
40 * Supported payload entry used in message_rule_t
41 *
42 */
43 typedef struct supported_payload_entry_s supported_payload_entry_t;
44
45 struct supported_payload_entry_s {
46 /**
47 * Payload type
48 */
49 payload_type_t payload_type;
50
51 /**
52 * Minimal occurence of this payload
53 */
54 size_t min_occurence;
55
56 /**
57 * Max occurence of this payload
58 */
59 size_t max_occurence;
60 };
61
62 /**
63 * Message Rule used to find out which payloads are supported by each message type
64 *
65 */
66 typedef struct message_rule_s message_rule_t;
67
68 struct message_rule_s {
69 /**
70 * Type of message
71 */
72 exchange_type_t exchange_type;
73
74 /**
75 * Is message a request or response
76 */
77 bool is_request;
78 /**
79 * Number of supported payloads
80 */
81 size_t supported_payloads_count;
82 /**
83 * Pointer to first supported payload entry
84 */
85 supported_payload_entry_t *supported_payloads;
86 };
87
88
89 static supported_payload_entry_t supported_ike_sa_init_i_payloads[] =
90 {
91 {SECURITY_ASSOCIATION,1,1},
92 {KEY_EXCHANGE,1,1},
93 {NONCE,1,1},
94 };
95
96 static supported_payload_entry_t supported_ike_sa_init_r_payloads[] =
97 {
98 {SECURITY_ASSOCIATION,1,1},
99 {KEY_EXCHANGE,1,1},
100 {NONCE,1,1},
101 };
102
103 static message_rule_t message_rules[] = {
104 {IKE_SA_INIT,TRUE,(sizeof(supported_ike_sa_init_i_payloads)/sizeof(supported_payload_entry_t)),supported_ike_sa_init_i_payloads},
105 {IKE_SA_INIT,FALSE,(sizeof(supported_ike_sa_init_r_payloads)/sizeof(supported_payload_entry_t)),supported_ike_sa_init_r_payloads}
106 };
107
108 /**
109 * Entry for a payload in the internal used linked list
110 *
111 */
112 typedef struct payload_entry_s payload_entry_t;
113
114 struct payload_entry_s {
115 /**
116 * Type of payload
117 */
118 payload_type_t payload_type;
119 /**
120 * Data struct holding the data of given payload
121 */
122 void *data_struct;
123 };
124
125
126 /**
127 * Private data of an message_t object
128 */
129 typedef struct private_message_s private_message_t;
130
131 struct private_message_s {
132
133 /**
134 * Public part of a message_t object
135 */
136 message_t public;
137
138
139 /**
140 * Minor version of message
141 */
142 u_int8_t major_version;
143
144 /**
145 * Major version of message
146 */
147 u_int8_t minor_version;
148
149 /**
150 * First Payload in message
151 */
152 payload_type_t first_payload;
153
154 /**
155 * Assigned exchange type
156 */
157 exchange_type_t exchange_type;
158
159 /**
160 * TRUE if message is from original initiator, FALSE otherwise.
161 */
162 bool original_initiator;
163
164 /**
165 * TRUE if message is request.
166 * FALSE if message is reply.
167 */
168 bool is_request;
169
170 /**
171 * Message ID of this message
172 */
173 u_int32_t message_id;
174
175 /**
176 * ID of assigned IKE_SA
177 */
178 ike_sa_id_t *ike_sa_id;
179
180 /**
181 * Assigned UDP packet.
182 *
183 * Stores incoming packet or last generated one.
184 */
185 packet_t *packet;
186
187 /**
188 * Linked List where payload data are stored in
189 */
190 linked_list_t *payloads;
191
192 /**
193 * Assigned parser to parse Header and Body of this message
194 */
195 parser_t *parser;
196
197 /**
198 * logger for this message
199 */
200 logger_t *logger;
201
202 /**
203 * Gets a list of supported payloads of this message type
204 *
205 * @param this calling object
206 * @param[out] supported_payloads first entry of supported payloads
207 * @param[out] supported_payloads_count number of supported payload entries
208 *
209 * @return SUCCESS
210 * NOT_FOUND if no supported payload definition could be found
211 */
212 status_t (*get_supported_payloads) (private_message_t *this, supported_payload_entry_t **supported_payloads,size_t *supported_payloads_count);
213
214 };
215
216 /**
217 * Implements private_message_t's get_supported_payloads function.
218 * See #private_message_t.get_supported_payloads.
219 */
220 status_t get_supported_payloads (private_message_t *this, supported_payload_entry_t **supported_payloads,size_t *supported_payloads_count)
221 {
222 int i;
223 exchange_type_t exchange_type = this->public.get_exchange_type(&(this->public));
224 bool is_request = this->public.get_request(&(this->public));
225
226
227 for (i = 0; i < (sizeof(message_rules) / sizeof(message_rule_t)); i++)
228 {
229 if ((exchange_type == message_rules[i].exchange_type) &&
230 (is_request == message_rules[i].is_request))
231 {
232 /* found rule for given exchange_type*/
233 *supported_payloads = message_rules[i].supported_payloads;
234 *supported_payloads_count = message_rules[i].supported_payloads_count;
235
236 return SUCCESS;
237 }
238
239
240 }
241 *supported_payloads = NULL;
242 *supported_payloads_count = 0;
243 return NOT_FOUND;
244 }
245
246 /**
247 * Implements message_t's set_ike_sa_id function.
248 * See #message_s.set_ike_sa_id.
249 */
250 static status_t set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id)
251 {
252 status_t status;
253 status = ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id));
254 return status;
255 }
256
257 /**
258 * Implements message_t's get_ike_sa_id function.
259 * See #message_s.get_ike_sa_id.
260 */
261 static status_t get_ike_sa_id (private_message_t *this,ike_sa_id_t **ike_sa_id)
262 {
263 status_t status;
264 if (this->ike_sa_id == NULL)
265 {
266 return FAILED;
267 }
268 status = this->ike_sa_id->clone(this->ike_sa_id,ike_sa_id);
269 return status;
270 }
271
272
273 /**
274 * Implements message_t's set_message_id function.
275 * See #message_s.set_message_id.
276 */
277 static status_t set_message_id (private_message_t *this,u_int32_t message_id)
278 {
279 this->message_id = message_id;
280 return SUCCESS;
281 }
282
283
284 /**
285 * Implements message_t's set_message_id function.
286 * See #message_s.set_message_id.
287 */
288 static u_int32_t get_message_id (private_message_t *this)
289 {
290 return this->message_id;
291 }
292
293 /**
294 * Implements message_t's set_major_version function.
295 * See #message_s.set_major_version.
296 */
297 static status_t set_major_version (private_message_t *this,u_int8_t major_version)
298 {
299 this->major_version = major_version;
300 return SUCCESS;
301 }
302
303
304 /**
305 * Implements message_t's get_major_version function.
306 * See #message_s.get_major_version.
307 */
308 static u_int8_t get_major_version (private_message_t *this)
309 {
310 return this->major_version;
311 }
312
313 /**
314 * Implements message_t's set_minor_version function.
315 * See #message_s.set_minor_version.
316 */
317 static status_t set_minor_version (private_message_t *this,u_int8_t minor_version)
318 {
319 this->minor_version = minor_version;
320 return SUCCESS;
321 }
322
323
324 /**
325 * Implements message_t's get_minor_version function.
326 * See #message_s.get_minor_version.
327 */
328 static u_int8_t get_minor_version (private_message_t *this)
329 {
330 return this->minor_version;
331 }
332
333 /**
334 * Implements message_t's set_exchange_type function.
335 * See #message_s.set_exchange_type.
336 */
337 static status_t set_exchange_type (private_message_t *this,exchange_type_t exchange_type)
338 {
339 this->exchange_type = exchange_type;
340 return SUCCESS;
341 }
342
343
344 /**
345 * Implements message_t's get_exchange_type function.
346 * See #message_s.get_exchange_type.
347 */
348 static exchange_type_t get_exchange_type (private_message_t *this)
349 {
350 return this->exchange_type;
351 }
352
353 /**
354 * Implements message_t's set_original_initiator function.
355 * See #message_s.set_original_initiator.
356 */
357 static status_t set_original_initiator (private_message_t *this,bool original_initiator)
358 {
359 this->original_initiator = original_initiator;
360 return SUCCESS;
361 }
362
363 /**
364 * Implements message_t's get_original_initiator function.
365 * See #message_s.get_original_initiator.
366 */
367 static exchange_type_t get_original_initiator (private_message_t *this)
368 {
369 return this->original_initiator;
370 }
371
372 /**
373 * Implements message_t's set_request function.
374 * See #message_s.set_request.
375 */
376 static status_t set_request (private_message_t *this,bool request)
377 {
378 this->is_request = request;
379 return SUCCESS;
380 }
381
382 /**
383 * Implements message_t's get_request function.
384 * See #message_s.get_request.
385 */
386 static exchange_type_t get_request (private_message_t *this)
387 {
388 return this->is_request;
389 }
390
391 static status_t add_payload(private_message_t *this, payload_t *payload)
392 {
393 if (this->payloads->insert_last(this->payloads, payload) != SUCCESS)
394 {
395 return OUT_OF_RES;
396 }
397 return SUCCESS;
398 }
399
400 static status_t set_source(private_message_t *this, host_t *host)
401 {
402 if (this->packet->source != NULL)
403 {
404 this->packet->source->destroy(this->packet->source);
405 }
406 this->packet->source = host;
407 return SUCCESS;
408 }
409
410 static status_t set_destination(private_message_t *this, host_t *host)
411 {
412 if (this->packet->destination != NULL)
413 {
414 this->packet->destination->destroy(this->packet->destination);
415 }
416 this->packet->destination = host;
417 return SUCCESS;
418 }
419
420 static status_t get_source(private_message_t *this, host_t **host)
421 {
422 *host = this->packet->source;
423 return SUCCESS;
424 }
425
426 static status_t get_destination(private_message_t *this, host_t **host)
427 {
428 *host = this->packet->destination;
429 return SUCCESS;
430 }
431
432
433 /**
434 * Implements message_t's generate function.
435 * See #message_s.generate.
436 */
437 static status_t generate(private_message_t *this, packet_t **packet)
438 {
439 generator_t *generator;
440 ike_header_t *ike_header;
441 payload_t *payload, *next_payload;
442 linked_list_iterator_t *iterator;
443 u_int64_t initiator_spi, responder_spi;
444 bool is_initiator;
445 status_t status;
446
447 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
448 {
449 return INVALID_STATE;
450 }
451
452 if (this->packet->source == NULL ||
453 this->packet->destination == NULL)
454 {
455 return INVALID_STATE;
456 }
457
458 ike_header = ike_header_create();
459 if (ike_header == NULL)
460 {
461 return OUT_OF_RES;
462 }
463
464 this->ike_sa_id->get_values(this->ike_sa_id, &initiator_spi, &responder_spi, &is_initiator);
465
466 ike_header->set_exchange_type(ike_header, this->exchange_type);
467 ike_header->set_initiator_flag(ike_header, this->original_initiator);
468 ike_header->set_message_id(ike_header, this->message_id);
469 ike_header->set_response_flag(ike_header, !this->is_request);
470 ike_header->set_initiator_flag(ike_header, is_initiator);
471 ike_header->set_initiator_spi(ike_header, initiator_spi);
472 ike_header->set_initiator_spi(ike_header, responder_spi);
473
474 generator = generator_create();
475
476 payload = (payload_t*)ike_header;
477
478 if (this->payloads->create_iterator(this->payloads, &iterator, TRUE) != SUCCESS)
479 {
480 generator->destroy(generator);
481 ike_header->destroy(ike_header);
482 return OUT_OF_RES;
483 }
484 while(iterator->has_next(iterator))
485 {
486 iterator->current(iterator, (void**)&next_payload);
487 payload->set_next_type(payload, next_payload->get_type(next_payload));
488 status = generator->generate_payload(generator, payload);
489 if (status != SUCCESS)
490 {
491 generator->destroy(generator);
492 ike_header->destroy(ike_header);
493 return status;
494 }
495 payload = next_payload;
496 }
497 iterator->destroy(iterator);
498
499 payload->set_next_type(payload, NO_PAYLOAD);
500 status = generator->generate_payload(generator, payload);
501 if (status != SUCCESS)
502 {
503 generator->destroy(generator);
504 ike_header->destroy(ike_header);
505 return status;
506 }
507
508 ike_header->destroy(ike_header);
509
510
511
512 if (this->packet->data.ptr != NULL)
513 {
514 allocator_free(this->packet->data.ptr);
515 }
516
517 status = generator->write_to_chunk(generator, &(this->packet->data));
518 if (status != SUCCESS)
519 {
520 generator->destroy(generator);
521 return status;
522 }
523
524 this->packet->clone(this->packet, packet);
525
526 generator->destroy(generator);
527 return SUCCESS;
528 }
529
530 /**
531 * Implements message_t's parse_header function.
532 * See #message_s.parse_header.
533 */
534 static status_t parse_header (private_message_t *this)
535 {
536 ike_header_t *ike_header;
537 status_t status;
538
539 this->parser->reset_context(this->parser);
540 status = this->parser->parse_payload(this->parser,HEADER,(payload_t **) &ike_header);
541 if (status != SUCCESS)
542 {
543 return status;
544
545 }
546 if (this->ike_sa_id != NULL)
547 {
548 this->ike_sa_id->destroy(this->ike_sa_id);
549 }
550 this->original_initiator = (!ike_header->get_initiator_flag(ike_header));
551
552 this->ike_sa_id = ike_sa_id_create(ike_header->get_initiator_spi(ike_header),ike_header->get_responder_spi(ike_header),this->original_initiator);
553 if (this->ike_sa_id == NULL)
554 {
555 ike_header->destroy(ike_header);
556 return OUT_OF_RES;
557 }
558 this->exchange_type = ike_header->get_exchange_type(ike_header);
559 this->message_id = ike_header->get_message_id(ike_header);
560 this->is_request = (!(ike_header->get_response_flag(ike_header)));
561 if ((ike_header->get_initiator_spi(ike_header) == 0) && (ike_header->get_responder_spi(ike_header) != 0))
562 {
563 /* initiator spi not set */
564 ike_header->destroy(ike_header);
565 return PARSE_ERROR;
566 }
567 this->major_version = ike_header->get_maj_version(ike_header);
568 this->minor_version = ike_header->get_min_version(ike_header);
569 this->first_payload = ike_header->payload_interface.get_next_type(&(ike_header->payload_interface));
570
571 ike_header->destroy(ike_header);
572 return SUCCESS;
573 }
574
575 /**
576 * Implements message_t's parse_body function.
577 * See #message_s.parse_body.
578 */
579 static status_t parse_body (private_message_t *this)
580 {
581 status_t status;
582 int i;
583 payload_type_t current_payload_type = this->first_payload;
584 supported_payload_entry_t *supported_payloads;
585 size_t supported_payloads_count;
586
587 if (this->get_supported_payloads (this,&supported_payloads,&supported_payloads_count) != SUCCESS)
588 {
589 /* message type is not supported */
590 return FAILED;
591 }
592
593 while (current_payload_type != NO_PAYLOAD)
594 {
595 payload_t *current_payload;
596
597 bool supported = FALSE;
598 for (i = 0; i < supported_payloads_count;i++)
599 {
600 if (supported_payloads[i].payload_type == current_payload_type)
601 {
602 supported = TRUE;
603 break;
604 }
605 }
606 if (!supported && (current_payload_type != NO_PAYLOAD))
607 {
608 /* type not supported */
609 status = NOT_SUPPORTED;
610 this->logger->log(this->logger, ERROR, "Payload type %s not supported",mapping_find(payload_type_m,current_payload_type));
611 break;
612 }
613
614 status = this->parser->parse_payload(this->parser,current_payload_type,(payload_t **) &current_payload);
615 if (status != SUCCESS)
616 {
617 this->logger->log(this->logger, ERROR, "Payload type %s could not be parsed",mapping_find(payload_type_m,current_payload_type));
618 break;
619 }
620
621 current_payload_type = current_payload->get_next_type(current_payload);
622
623 // status = current_payload->verify(current_payload);
624 if (status != SUCCESS)
625 {
626 this->logger->log(this->logger, ERROR, "Payload type %s could not be verified",mapping_find(payload_type_m,current_payload_type));
627 status = PARSE_ERROR;
628 break;
629 }
630
631 status = this->payloads->insert_last(this->payloads,current_payload);
632 if (status != SUCCESS)
633 {
634 this->logger->log(this->logger, ERROR, "Could not insert current payload to internal list cause of ressource exhausting");
635 break;
636 }
637
638 }
639 if (status != SUCCESS)
640 {
641 /* already parsed payload is destroyed later in destroy call from outside this object */
642 }
643 else
644 {
645 linked_list_iterator_t *iterator;
646
647 status = this->payloads->create_iterator(this->payloads,&iterator,TRUE);
648 if (status != SUCCESS)
649 {
650 this->logger->log(this->logger, ERROR, "Could not create iterator to check supported payloads");
651 return status;
652 }
653
654
655 /* check for payloads with wrong count*/
656 for (i = 0; i < supported_payloads_count;i++)
657 {
658 size_t min_occurence = supported_payloads[i].min_occurence;
659 size_t max_occurence = supported_payloads[i].max_occurence;
660 payload_type_t payload_type = supported_payloads[i].payload_type;
661 size_t found_payloads = 0;
662
663 iterator->reset(iterator);
664
665 while(iterator->has_next(iterator))
666 {
667 payload_t *current_payload;
668 status = iterator->current(iterator,(void **)&current_payload);
669 if (status != SUCCESS)
670 {
671 this->logger->log(this->logger, CONTROL_MORE, "Could not get payload from internal list");
672 iterator->destroy(iterator);
673 return status;
674 }
675 if (current_payload->get_type(current_payload) == payload_type)
676 {
677 found_payloads++;
678 if (found_payloads > max_occurence)
679 {
680 this->logger->log(this->logger, CONTROL_MORE, "Payload of type %s more than %d times (%d) occured in current message",
681 mapping_find(payload_type_m,current_payload->get_type(current_payload)),max_occurence,found_payloads);
682 iterator->destroy(iterator);
683 return NOT_SUPPORTED;
684 }
685 }
686
687 }
688 if (found_payloads < min_occurence)
689 {
690 this->logger->log(this->logger, CONTROL_MORE, "Payload of type %s not occured %d times",
691 mapping_find(payload_type_m,payload_type),min_occurence);
692 iterator->destroy(iterator);
693 return NOT_SUPPORTED;
694 }
695
696 }
697 iterator->destroy(iterator);
698 }
699 return status;
700
701 }
702
703
704
705 /**
706 * Implements message_t's destroy function.
707 * See #message_s.destroy.
708 */
709 static status_t destroy (private_message_t *this)
710 {
711 linked_list_iterator_t *iterator;
712
713 if (this->packet != NULL)
714 {
715 this->packet->destroy(this->packet);
716 }
717 if (this->ike_sa_id != NULL)
718 {
719 this->ike_sa_id->destroy(this->ike_sa_id);
720 }
721
722 this->payloads->create_iterator(this->payloads, &iterator, TRUE);
723 while (iterator->has_next(iterator))
724 {
725 payload_t *payload;
726 iterator->current(iterator, (void**)&payload);
727 this->logger->log(this->logger, CONTROL_MORE, "Destroying payload of type %s",
728 mapping_find(payload_type_m, payload->get_type(payload)));
729 payload->destroy(payload);
730 }
731 iterator->destroy(iterator);
732 this->payloads->destroy(this->payloads);
733 this->parser->destroy(this->parser);
734
735 allocator_free(this);
736 return SUCCESS;
737 }
738
739 /*
740 * Described in Header-File
741 */
742 message_t *message_create_from_packet(packet_t *packet)
743 {
744 private_message_t *this = allocator_alloc_thing(private_message_t);
745 if (this == NULL)
746 {
747 return NULL;
748 }
749
750 /* public functions */
751 this->public.set_major_version = (status_t(*)(message_t*, u_int8_t))set_major_version;
752 this->public.get_major_version = (u_int8_t(*)(message_t*))get_major_version;
753 this->public.set_minor_version = (status_t(*)(message_t*, u_int8_t))set_minor_version;
754 this->public.get_minor_version = (u_int8_t(*)(message_t*))get_minor_version;
755 this->public.set_message_id = (status_t(*)(message_t*, u_int32_t))set_message_id;
756 this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
757 this->public.set_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
758 this->public.get_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t **))get_ike_sa_id;
759 this->public.set_exchange_type = (status_t(*)(message_t*, exchange_type_t))set_exchange_type;
760 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
761 this->public.set_original_initiator = (status_t(*)(message_t*, bool))set_original_initiator;
762 this->public.get_original_initiator = (bool(*)(message_t*))get_original_initiator;
763 this->public.set_request = (status_t(*)(message_t*, bool))set_request;
764 this->public.get_request = (bool(*)(message_t*))get_request;
765 this->public.add_payload = (status_t(*)(message_t*,payload_t*))add_payload;
766 this->public.generate = (status_t (*) (message_t *, packet_t**)) generate;
767 this->public.set_source = (status_t (*) (message_t*,host_t*)) set_source;
768 this->public.get_source = (status_t (*) (message_t*,host_t**)) get_source;
769 this->public.set_destination = (status_t (*) (message_t*,host_t*)) set_destination;
770 this->public.get_destination = (status_t (*) (message_t*,host_t**)) get_destination;
771 this->public.parse_header = (status_t (*) (message_t *)) parse_header;
772 this->public.parse_body = (status_t (*) (message_t *)) parse_body;
773 this->public.destroy = (status_t(*)(message_t*))destroy;
774
775 /* public values */
776 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
777 this->original_initiator = TRUE;
778 this->is_request = TRUE;
779 this->ike_sa_id = NULL;
780 this->first_payload = NO_PAYLOAD;
781 this->message_id = 0;
782
783 /* private functions */
784 this->get_supported_payloads = get_supported_payloads;
785
786 /* private values */
787 if (packet == NULL)
788 {
789 packet = packet_create();
790 }
791 if (packet == NULL)
792 {
793 allocator_free(this);
794 return NULL;
795 }
796 this->packet = packet;
797 this->payloads = linked_list_create();
798 if (this->payloads == NULL)
799 {
800 allocator_free(this);
801 return NULL;
802 }
803
804 /* parser is created from data of packet */
805 this->parser = parser_create(this->packet->data);
806 if (this->parser == NULL)
807 {
808 this->payloads->destroy(this->payloads);
809 allocator_free(this);
810 return NULL;
811 }
812
813 this->logger = global_logger_manager->create_logger(global_logger_manager, MESSAGE, NULL);
814 if (this->logger == NULL)
815 {
816 this->parser->destroy(this->parser);
817 this->payloads->destroy(this->payloads);
818 allocator_free(this);
819 return NULL;
820 }
821
822 return (&this->public);
823 }
824
825 /*
826 * Described in Header-File
827 */
828 message_t *message_create()
829 {
830 return message_create_from_packet(NULL);
831 }