8f62e7b2fe5f45e571ef12dd1faac0fc2811588d
[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 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 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 message_rule_t message_rules[] = {
104 {IKE_SA_INIT,TRUE,sizeof(supported_ike_sa_init_i_payloads),supported_ike_sa_init_i_payloads},
105 {IKE_SA_INIT,FALSE,sizeof(supported_ike_sa_init_r_payloads),supported_ike_sa_init_r_payloads}
106
107 };
108
109 /**
110 * Entry for a payload in the internal used linked list
111 *
112 */
113 typedef struct payload_entry_s payload_entry_t;
114
115 struct payload_entry_s {
116 /**
117 * Type of payload
118 */
119 payload_type_t payload_type;
120 /**
121 * Data struct holding the data of given payload
122 */
123 void *data_struct;
124 };
125
126
127 /**
128 * Private data of an message_t object
129 */
130 typedef struct private_message_s private_message_t;
131
132 struct private_message_s {
133
134 /**
135 * Public part of a message_t object
136 */
137 message_t public;
138
139
140 /**
141 * Minor version of message
142 */
143 u_int8_t major_version;
144
145 /**
146 * Major version of message
147 */
148 u_int8_t minor_version;
149
150 /**
151 * First Payload in message
152 */
153 payload_type_t first_payload;
154
155 /**
156 * Assigned exchange type
157 */
158 exchange_type_t exchange_type;
159
160 /**
161 * TRUE if message is from original initiator, FALSE otherwise.
162 */
163 bool original_initiator;
164
165 /**
166 * TRUE if message is request.
167 * FALSE if message is reply.
168 */
169 bool is_request;
170
171 /**
172 * Message ID of this message
173 */
174 u_int32_t message_id;
175
176 /**
177 * ID of assigned IKE_SA
178 */
179 ike_sa_id_t *ike_sa_id;
180
181 /**
182 * Assigned UDP packet.
183 *
184 * Stores incoming packet or last generated one.
185 */
186 packet_t *packet;
187
188 /**
189 * Linked List where payload data are stored in
190 */
191 linked_list_t *payloads;
192
193 /**
194 * Assigned parser to parse Header and Body of this message
195 */
196 parser_t *parser;
197
198 /**
199 * logger for this message
200 */
201 logger_t *logger;
202
203 };
204
205
206 /**
207 * Implements message_t's set_ike_sa_id function.
208 * See #message_s.set_ike_sa_id.
209 */
210 static status_t set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id)
211 {
212 status_t status;
213 status = ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id));
214 return status;
215 }
216
217 /**
218 * Implements message_t's get_ike_sa_id function.
219 * See #message_s.get_ike_sa_id.
220 */
221 static status_t get_ike_sa_id (private_message_t *this,ike_sa_id_t **ike_sa_id)
222 {
223 status_t status;
224 if (this->ike_sa_id == NULL)
225 {
226 return FAILED;
227 }
228 status = this->ike_sa_id->clone(this->ike_sa_id,ike_sa_id);
229 return status;
230 }
231
232
233 /**
234 * Implements message_t's set_message_id function.
235 * See #message_s.set_message_id.
236 */
237 static status_t set_message_id (private_message_t *this,u_int32_t message_id)
238 {
239 this->message_id = message_id;
240 return SUCCESS;
241 }
242
243
244 /**
245 * Implements message_t's set_message_id function.
246 * See #message_s.set_message_id.
247 */
248 static u_int32_t get_message_id (private_message_t *this)
249 {
250 return this->message_id;
251 }
252
253 /**
254 * Implements message_t's set_major_version function.
255 * See #message_s.set_major_version.
256 */
257 static status_t set_major_version (private_message_t *this,u_int8_t major_version)
258 {
259 this->major_version = major_version;
260 return SUCCESS;
261 }
262
263
264 /**
265 * Implements message_t's get_major_version function.
266 * See #message_s.get_major_version.
267 */
268 static u_int8_t get_major_version (private_message_t *this)
269 {
270 return this->major_version;
271 }
272
273 /**
274 * Implements message_t's set_minor_version function.
275 * See #message_s.set_minor_version.
276 */
277 static status_t set_minor_version (private_message_t *this,u_int8_t minor_version)
278 {
279 this->minor_version = minor_version;
280 return SUCCESS;
281 }
282
283
284 /**
285 * Implements message_t's get_minor_version function.
286 * See #message_s.get_minor_version.
287 */
288 static u_int8_t get_minor_version (private_message_t *this)
289 {
290 return this->minor_version;
291 }
292
293 /**
294 * Implements message_t's set_exchange_type function.
295 * See #message_s.set_exchange_type.
296 */
297 static status_t set_exchange_type (private_message_t *this,exchange_type_t exchange_type)
298 {
299 this->exchange_type = exchange_type;
300 return SUCCESS;
301 }
302
303
304 /**
305 * Implements message_t's get_exchange_type function.
306 * See #message_s.get_exchange_type.
307 */
308 static exchange_type_t get_exchange_type (private_message_t *this)
309 {
310 return this->exchange_type;
311 }
312
313 /**
314 * Implements message_t's set_original_initiator function.
315 * See #message_s.set_original_initiator.
316 */
317 static status_t set_original_initiator (private_message_t *this,bool original_initiator)
318 {
319 this->original_initiator = original_initiator;
320 return SUCCESS;
321 }
322
323 /**
324 * Implements message_t's get_original_initiator function.
325 * See #message_s.get_original_initiator.
326 */
327 static exchange_type_t get_original_initiator (private_message_t *this)
328 {
329 return this->original_initiator;
330 }
331
332 /**
333 * Implements message_t's set_request function.
334 * See #message_s.set_request.
335 */
336 static status_t set_request (private_message_t *this,bool request)
337 {
338 this->is_request = request;
339 return SUCCESS;
340 }
341
342 /**
343 * Implements message_t's get_request function.
344 * See #message_s.get_request.
345 */
346 static exchange_type_t get_request (private_message_t *this)
347 {
348 return this->is_request;
349 }
350
351 static status_t add_payload(private_message_t *this, payload_t *payload)
352 {
353 if (this->payloads->insert_last(this->payloads, payload) != SUCCESS)
354 {
355 return OUT_OF_RES;
356 }
357 return SUCCESS;
358 }
359
360 static status_t set_source(private_message_t *this, host_t *host)
361 {
362 if (this->packet->source != NULL)
363 {
364 this->packet->source->destroy(this->packet->source);
365 }
366 this->packet->source = host;
367 return SUCCESS;
368 }
369
370 static status_t set_destination(private_message_t *this, host_t *host)
371 {
372 if (this->packet->destination != NULL)
373 {
374 this->packet->destination->destroy(this->packet->destination);
375 }
376 this->packet->destination = host;
377 return SUCCESS;
378 }
379
380 static status_t get_source(private_message_t *this, host_t **host)
381 {
382 *host = this->packet->source;
383 return SUCCESS;
384 }
385
386 static status_t get_destination(private_message_t *this, host_t **host)
387 {
388 *host = this->packet->destination;
389 return SUCCESS;
390 }
391
392
393 /**
394 * Implements message_t's generate function.
395 * See #message_s.generate.
396 */
397 static status_t generate(private_message_t *this, packet_t **packet)
398 {
399 generator_t *generator;
400 ike_header_t *ike_header;
401 payload_t *payload, *next_payload;
402 linked_list_iterator_t *iterator;
403 u_int64_t initiator_spi, responder_spi;
404 bool is_initiator;
405 status_t status;
406
407 if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
408 {
409 return INVALID_STATE;
410 }
411
412 if (this->packet->source == NULL ||
413 this->packet->destination == NULL)
414 {
415 return INVALID_STATE;
416 }
417
418 ike_header = ike_header_create();
419 if (ike_header == NULL)
420 {
421 return OUT_OF_RES;
422 }
423
424 this->ike_sa_id->get_values(this->ike_sa_id, &initiator_spi, &responder_spi, &is_initiator);
425
426 ike_header->set_exchange_type(ike_header, this->exchange_type);
427 ike_header->set_initiator_flag(ike_header, this->original_initiator);
428 ike_header->set_message_id(ike_header, this->message_id);
429 ike_header->set_response_flag(ike_header, !this->is_request);
430 ike_header->set_initiator_flag(ike_header, is_initiator);
431 ike_header->set_initiator_spi(ike_header, initiator_spi);
432 ike_header->set_initiator_spi(ike_header, responder_spi);
433
434 generator = generator_create();
435
436 payload = (payload_t*)ike_header;
437
438 if (this->payloads->create_iterator(this->payloads, &iterator, TRUE) != SUCCESS)
439 {
440 generator->destroy(generator);
441 ike_header->destroy(ike_header);
442 return OUT_OF_RES;
443 }
444 while(iterator->has_next(iterator))
445 {
446 iterator->current(iterator, (void**)&next_payload);
447 payload->set_next_type(payload, next_payload->get_type(next_payload));
448 status = generator->generate_payload(generator, payload);
449 if (status != SUCCESS)
450 {
451 generator->destroy(generator);
452 ike_header->destroy(ike_header);
453 return status;
454 }
455 payload = next_payload;
456 }
457 iterator->destroy(iterator);
458
459 payload->set_next_type(payload, NO_PAYLOAD);
460 status = generator->generate_payload(generator, payload);
461 if (status != SUCCESS)
462 {
463 generator->destroy(generator);
464 ike_header->destroy(ike_header);
465 return status;
466 }
467
468 ike_header->destroy(ike_header);
469
470
471
472 if (this->packet->data.ptr != NULL)
473 {
474 allocator_free(this->packet->data.ptr);
475 }
476
477 status = generator->write_to_chunk(generator, &(this->packet->data));
478 if (status != SUCCESS)
479 {
480 generator->destroy(generator);
481 return status;
482 }
483
484 this->packet->clone(this->packet, packet);
485
486 generator->destroy(generator);
487 return SUCCESS;
488 }
489
490 /**
491 * Implements message_t's parse_header function.
492 * See #message_s.parse_header.
493 */
494 static status_t parse_header (private_message_t *this)
495 {
496 ike_header_t *ike_header;
497 status_t status;
498
499 this->parser->reset_context(this->parser);
500 status = this->parser->parse_payload(this->parser,HEADER,(payload_t **) &ike_header);
501 if (status != SUCCESS)
502 {
503 return status;
504
505 }
506 if (this->ike_sa_id != NULL)
507 {
508 this->ike_sa_id->destroy(this->ike_sa_id);
509 }
510 this->original_initiator = (!ike_header->get_initiator_flag(ike_header));
511
512 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);
513 if (this->ike_sa_id == NULL)
514 {
515 ike_header->destroy(ike_header);
516 return OUT_OF_RES;
517 }
518 this->exchange_type = ike_header->get_exchange_type(ike_header);
519 this->message_id = ike_header->get_message_id(ike_header);
520 this->is_request = (!ike_header->get_response_flag(ike_header));
521 if ((ike_header->get_initiator_spi(ike_header) == 0) && (ike_header->get_responder_spi(ike_header) != 0))
522 {
523 /* initiator spi not set */
524 ike_header->destroy(ike_header);
525 return PARSE_ERROR;
526 }
527 this->major_version = ike_header->get_maj_version(ike_header);
528 this->minor_version = ike_header->get_min_version(ike_header);
529 this->first_payload = ike_header->payload_interface.get_next_type(&(ike_header->payload_interface));
530
531 ike_header->destroy(ike_header);
532 return SUCCESS;
533 }
534
535 /**
536 * Implements message_t's parse_body function.
537 * See #message_s.parse_body.
538 */
539 static status_t parse_body (private_message_t *this)
540 {
541 status_t status;
542 payload_type_t current_payload_type = this->first_payload;
543
544
545 while (current_payload_type != NO_PAYLOAD)
546 {
547 payload_t *current_payload;
548
549 status = this->parser->parse_payload(this->parser,current_payload_type,(payload_t **) &current_payload);
550
551 current_payload_type = current_payload->get_next_type(current_payload);
552 current_payload->destroy(current_payload);
553
554 }
555
556 return SUCCESS;
557
558 }
559
560
561
562 /**
563 * Implements message_t's destroy function.
564 * See #message_s.destroy.
565 */
566 static status_t destroy (private_message_t *this)
567 {
568 linked_list_iterator_t *iterator;
569
570 if (this->packet != NULL)
571 {
572 this->packet->destroy(this->packet);
573 }
574 if (this->ike_sa_id != NULL)
575 {
576 this->ike_sa_id->destroy(this->ike_sa_id);
577 }
578
579 this->payloads->create_iterator(this->payloads, &iterator, TRUE);
580 while (iterator->has_next(iterator))
581 {
582 payload_t *payload;
583 iterator->current(iterator, (void**)&payload);
584 this->logger->log(this->logger, CONTROL_MORE, "Destroying payload of type %s",
585 mapping_find(payload_type_m, payload->get_type(payload)));
586 payload->destroy(payload);
587 }
588 iterator->destroy(iterator);
589 this->payloads->destroy(this->payloads);
590 this->parser->destroy(this->parser);
591
592 allocator_free(this);
593 return SUCCESS;
594 }
595
596 /*
597 * Described in Header-File
598 */
599 message_t *message_create_from_packet(packet_t *packet)
600 {
601 private_message_t *this = allocator_alloc_thing(private_message_t);
602 if (this == NULL)
603 {
604 return NULL;
605 }
606
607 /* public functions */
608 this->public.set_major_version = (status_t(*)(message_t*, u_int8_t))set_major_version;
609 this->public.get_major_version = (u_int8_t(*)(message_t*))get_major_version;
610 this->public.set_minor_version = (status_t(*)(message_t*, u_int8_t))set_minor_version;
611 this->public.get_minor_version = (u_int8_t(*)(message_t*))get_minor_version;
612 this->public.set_message_id = (status_t(*)(message_t*, u_int32_t))set_message_id;
613 this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
614 this->public.set_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
615 this->public.get_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t **))get_ike_sa_id;
616 this->public.set_exchange_type = (status_t(*)(message_t*, exchange_type_t))set_exchange_type;
617 this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
618 this->public.set_original_initiator = (status_t(*)(message_t*, bool))set_original_initiator;
619 this->public.get_original_initiator = (bool(*)(message_t*))get_original_initiator;
620 this->public.set_request = (status_t(*)(message_t*, bool))set_request;
621 this->public.get_request = (bool(*)(message_t*))get_request;
622 this->public.add_payload = (status_t(*)(message_t*,payload_t*))add_payload;
623 this->public.generate = (status_t (*) (message_t *, packet_t**)) generate;
624 this->public.set_source = (status_t (*) (message_t*,host_t*)) set_source;
625 this->public.get_source = (status_t (*) (message_t*,host_t**)) get_source;
626 this->public.set_destination = (status_t (*) (message_t*,host_t*)) set_destination;
627 this->public.get_destination = (status_t (*) (message_t*,host_t**)) get_destination;
628 this->public.parse_header = (status_t (*) (message_t *)) parse_header;
629 this->public.parse_body = (status_t (*) (message_t *)) parse_body;
630 this->public.destroy = (status_t(*)(message_t*))destroy;
631
632 /* public values */
633 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
634 this->original_initiator = TRUE;
635 this->is_request = TRUE;
636 this->ike_sa_id = NULL;
637 this->first_payload = NO_PAYLOAD;
638 this->message_id = 0;
639
640 /* private values */
641 if (packet == NULL)
642 {
643 packet = packet_create();
644 }
645 if (packet == NULL)
646 {
647 allocator_free(this);
648 return NULL;
649 }
650 this->packet = packet;
651 this->payloads = linked_list_create();
652 if (this->payloads == NULL)
653 {
654 allocator_free(this);
655 return NULL;
656 }
657
658 /* parser is created from data of packet */
659 this->parser = parser_create(this->packet->data);
660 if (this->parser == NULL)
661 {
662 this->payloads->destroy(this->payloads);
663 allocator_free(this);
664 return NULL;
665 }
666
667 this->logger = global_logger_manager->create_logger(global_logger_manager, MESSAGE, NULL);
668 if (this->logger == NULL)
669 {
670 this->parser->destroy(this->parser);
671 this->payloads->destroy(this->payloads);
672 allocator_free(this);
673 return NULL;
674 }
675
676 return (&this->public);
677 }
678
679 /*
680 * Described in Header-File
681 */
682 message_t *message_create()
683 {
684 return message_create_from_packet(NULL);
685 }