(no commit message)
[strongswan.git] / Source / charon / ike_sa.c
1 /**
2 * @file ike_sa.c
3 *
4 * @brief Class ike_sa_t. An object of this type is managed by an
5 * ike_sa_manager_t object and represents an IKE_SA
6 *
7 */
8
9 /*
10 * Copyright (C) 2005 Jan Hutter, Martin Willi
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include "ike_sa.h"
25
26 #include "types.h"
27 #include "globals.h"
28 #include "definitions.h"
29 #include "utils/allocator.h"
30 #include "utils/linked_list.h"
31 #include "utils/logger_manager.h"
32 #include "utils/randomizer.h"
33 #include "transforms/diffie_hellman.h"
34 #include "payloads/sa_payload.h"
35 #include "payloads/nonce_payload.h"
36 #include "payloads/ke_payload.h"
37 #include "payloads/transform_substructure.h"
38 #include "payloads/transform_attribute.h"
39
40
41 /**
42 * States in which a IKE_SA can actually be
43 */
44 typedef enum ike_sa_state_e ike_sa_state_t;
45
46 enum ike_sa_state_e {
47
48 /**
49 * IKE_SA is is not in a state
50 */
51 NO_STATE = 1,
52
53 /**
54 * A IKE_SA_INIT-message was sent: role initiator
55 */
56 IKE_SA_INIT_REQUESTED = 2,
57
58 /**
59 * A IKE_SA_INIT-message was replied: role responder
60 */
61 IKE_SA_INIT_RESPONDED = 3,
62
63 /**
64 * An IKE_AUTH-message was sent after a successful
65 * IKE_SA_INIT-exchange: role initiator
66 */
67 IKE_AUTH_REQUESTED = 4,
68
69 /**
70 * An IKE_AUTH-message was replied: role responder.
71 * In this state, all the informations for an IKE_SA
72 * and one CHILD_SA are known.
73 */
74 IKE_SA_INITIALIZED = 5
75 };
76
77 /**
78 * string mappings for ike_sa_state
79 */
80 mapping_t ike_sa_state_m[] = {
81 {NO_STATE, "NO_STATE"},
82 {IKE_SA_INIT_REQUESTED, "IKE_SA_INIT_REQUESTED"},
83 {IKE_SA_INIT_RESPONDED, "IKE_SA_INIT_RESPONDED"},
84 {IKE_AUTH_REQUESTED, "IKE_AUTH_REQUESTED"},
85 {IKE_SA_INITIALIZED, "IKE_SA_INITIALIZED"},
86 {MAPPING_END, NULL}
87 };
88
89
90 /**
91 * Private data of an message_t object
92 */
93 typedef struct private_ike_sa_s private_ike_sa_t;
94
95 struct private_ike_sa_s {
96
97 /**
98 * Public part of a ike_sa_t object
99 */
100 ike_sa_t public;
101
102 status_t (*build_sa_payload) (private_ike_sa_t *this, sa_payload_t **payload);
103 status_t (*build_nonce_payload) (private_ike_sa_t *this, nonce_payload_t **payload);
104
105 status_t (*build_message) (private_ike_sa_t *this, exchange_type_t type, bool request);
106
107 status_t (*transto_ike_sa_init_responded) (private_ike_sa_t *this, message_t *message);
108 status_t (*transto_ike_auth_requested) (private_ike_sa_t *this, message_t *message);
109
110 /* Private values */
111 /**
112 * Identifier for the current IKE_SA
113 */
114 ike_sa_id_t *ike_sa_id;
115
116 /**
117 * Linked List containing the child sa's of the current IKE_SA
118 */
119 linked_list_t *child_sas;
120
121 /**
122 * Current state of the IKE_SA
123 */
124 ike_sa_state_t state;
125
126 /**
127 * is this IKE_SA the original initiator of this IKE_SA
128 */
129 bool original_initiator;
130
131 /**
132 * this SA's source for random data
133 */
134 randomizer_t *randomizer;
135
136 linked_list_t *sent_messages;
137
138 struct {
139 host_t *host;
140 } me;
141
142 struct {
143 host_t *host;
144 } other;
145
146
147 struct {
148 /**
149 * Diffie Hellman object used to compute shared secret
150 */
151 diffie_hellman_t *diffie_hellman;
152 /**
153 * Diffie Hellman group number
154 */
155 u_int16_t dh_group_number;
156
157 /**
158 * Priority used get matching dh_group number
159 */
160 u_int16_t dh_group_priority;
161 } ike_sa_init_data;
162
163
164 /**
165 * next message id to receive
166 */
167 u_int32_t message_id_in;
168
169 /**
170 * next message id to send
171 */
172 u_int32_t message_id_out;
173
174 /**
175 * a logger for this IKE_SA
176 */
177 logger_t *logger;
178 };
179
180 /**
181 * @brief implements function process_message of private_ike_sa_t
182 */
183 static status_t process_message (private_ike_sa_t *this, message_t *message)
184 {
185 this->logger->log(this->logger, CONTROL|MORE, "Process message of exchange type %s",
186 mapping_find(exchange_type_m,message->get_exchange_type(message)));
187
188 switch (message->get_exchange_type(message))
189 {
190 case IKE_SA_INIT:
191 {
192 if (message->get_request(message)) {
193 if (this->state == NO_STATE)
194 {
195 /* state transission NO_STATE => IKE_SA_INIT_RESPONDED */
196 return this->transto_ike_sa_init_responded(this, message);
197 }
198 }
199 else
200 {
201 if (this->state == IKE_SA_INIT_REQUESTED)
202 {
203 /* state transission IKE_SA_INIT_REQUESTED => IKE_AUTH_REQUESTED*/
204 return this->transto_ike_auth_requested(this, message);
205 }
206 }
207 break;
208 }
209 case IKE_AUTH:
210 {
211 /* break; */
212 }
213 case CREATE_CHILD_SA:
214 {
215 /* break; */
216 }
217 case INFORMATIONAL:
218 {
219 /* break; */
220 }
221 default:
222 {
223 this->logger->log(this->logger, ERROR, "processing %s-message not supported.",
224 mapping_find(exchange_type_m,message->get_exchange_type(message)));
225 return NOT_SUPPORTED;
226 }
227 }
228 this->logger->log(this->logger, ERROR, "received %s-message in state %s, rejected.",
229 mapping_find(exchange_type_m, message->get_exchange_type(message)),
230 mapping_find(ike_sa_state_m, this->state));
231 return INVALID_STATE;
232 }
233
234
235 static status_t build_message(private_ike_sa_t *this, exchange_type_t type, bool request)
236 {
237 status_t status;
238 message_t *message;
239 host_t *source, *destination;
240
241 message = message_create();
242 if (message == NULL)
243 {
244 return OUT_OF_RES;
245 }
246
247 status = this->me.host->clone(this->me.host, &source);
248 status |= this->other.host->clone(this->other.host, &destination);
249 if (status != SUCCESS)
250 {
251 message->destroy(message);
252 return status;
253 }
254 message->set_source(message, source);
255 message->set_destination(message, destination);
256
257 message->set_exchange_type(message, type);
258 message->set_request(message, request);
259
260 message->set_ike_sa_id(message, this->ike_sa_id);
261
262 return SUCCESS;
263 }
264
265 static status_t transto_ike_sa_init_responded(private_ike_sa_t *this, message_t *message)
266 {
267 status_t status;
268 linked_list_iterator_t *payloads;
269 message_t *respond;
270
271 status = message->parse_body(message);
272 if (status != SUCCESS)
273 {
274 return status;
275 }
276
277
278
279
280
281 status = message->get_payload_iterator(message, &payloads);
282 if (status != SUCCESS)
283 {
284 respond->destroy(respond);
285 return status;
286 }
287 while (payloads->has_next(payloads))
288 {
289 payload_t *payload;
290 payloads->current(payloads, (void**)payload);
291 switch (payload->get_type(payload))
292 {
293 case SECURITY_ASSOCIATION:
294 {
295 sa_payload_t *sa_payload;
296 linked_list_iterator_t *proposals;
297
298 sa_payload = (sa_payload_t*)payload;
299 status = sa_payload->create_proposal_substructure_iterator(sa_payload, &proposals, TRUE);
300 if (status != SUCCESS)
301 {
302 payloads->destroy(payloads);
303 return status;
304 }
305 //global_configuration_manager->select_proposals_for_host
306
307 break;
308 }
309 case KEY_EXCHANGE:
310 {
311 break;
312 }
313 case NONCE:
314 {
315 break;
316 }
317 default:
318 {
319
320 }
321
322 }
323
324 }
325
326
327
328
329
330 /*
331 job_t *delete_job;
332 delete_job = (job_t *) delete_ike_sa_job_create(this->ike_sa_id);
333 if (delete_job == NULL)
334 {
335 this->logger->log(this->logger, ERROR, "Job to delete IKE SA could not be created");
336 }
337
338 status = global_job_queue->add(global_job_queue,delete_job);
339 if (status != SUCCESS)
340 {
341 this->logger->log(this->logger, ERROR, "%s Job to delete IKE SA could not be added to job queue",mapping_find(status_m,status));
342 delete_job->destroy_all(delete_job);
343 }*/
344 return SUCCESS;
345 }
346
347 static status_t transto_ike_auth_requested(private_ike_sa_t *this, message_t *message)
348 {
349 return SUCCESS;
350 }
351
352 /**
353 * @brief implements function process_configuration of private_ike_sa_t
354 */
355 static status_t initialize_connection(private_ike_sa_t *this, char *name)
356 {
357 message_t *message;
358 payload_t *payload;
359 packet_t *packet;
360 status_t status;
361
362 this->logger->log(this->logger, CONTROL, "initializing connection");
363
364 this->original_initiator = TRUE;
365
366 status = global_configuration_manager->get_local_host(global_configuration_manager, name, &(this->me.host));
367 if (status != SUCCESS)
368 {
369 return INVALID_ARG;
370 }
371
372 status = global_configuration_manager->get_remote_host(global_configuration_manager, name, &(this->other.host));
373 if (status != SUCCESS)
374 {
375 return INVALID_ARG;
376 }
377
378 status = global_configuration_manager->get_dh_group_number(global_configuration_manager, name, &(this->ike_sa_init_data.dh_group_number), this->ike_sa_init_data.dh_group_priority);
379 if (status != SUCCESS)
380 {
381 return INVALID_ARG;
382 }
383
384 message = message_create();
385
386 if (message == NULL)
387 {
388 return OUT_OF_RES;
389 }
390
391
392 message->set_source(message, this->me.host);
393 message->set_destination(message, this->other.host);
394
395 message->set_exchange_type(message, IKE_SA_INIT);
396 message->set_original_initiator(message, this->original_initiator);
397 message->set_message_id(message, this->message_id_out++);
398 message->set_ike_sa_id(message, this->ike_sa_id);
399 message->set_request(message, TRUE);
400
401 status = this->build_sa_payload(this, (sa_payload_t**)&payload);
402 if (status != SUCCESS)
403 {
404 this->logger->log(this->logger, ERROR, "Could not build SA payload");
405 message->destroy(message);
406 return status;
407 }
408 payload->set_next_type(payload, KEY_EXCHANGE);
409 message->add_payload(message, payload);
410
411 {
412 ke_payload_t *ke_payload;
413 chunk_t key_data;
414 this ->logger->log(this->logger, CONTROL|MORE, "create diffie hellman object");
415 if (this->ike_sa_init_data.diffie_hellman != NULL)
416 {
417 this->logger->log(this->logger, ERROR, "Object of type diffie_hellman_t already existing!");
418 message->destroy(message);
419 return FAILED;
420 }
421 this->ike_sa_init_data.diffie_hellman = diffie_hellman_create(this->ike_sa_init_data.dh_group_number);
422
423 if (this->ike_sa_init_data.diffie_hellman == NULL)
424 {
425 this->logger->log(this->logger, ERROR, "Object of type diffie_hellman_t could not be created!");
426 message->destroy(message);
427 return FAILED;
428 }
429
430 this ->logger->log(this->logger, CONTROL|MORE, "get public dh value to send in ke payload");
431
432 status = this->ike_sa_init_data.diffie_hellman->get_my_public_value(this->ike_sa_init_data.diffie_hellman,&key_data);
433 if (status != SUCCESS)
434 {
435 this->logger->log(this->logger, ERROR, "Could not get my DH public value");
436 message->destroy(message);
437 return status;
438 }
439
440 ke_payload = ke_payload_create();
441 if (ke_payload == NULL)
442 {
443 this->logger->log(this->logger, ERROR, "Could not build KE payload");
444 message->destroy(message);
445 allocator_free_chunk(key_data);
446 return OUT_OF_RES;
447 }
448 ke_payload->set_dh_group_number(ke_payload, MODP_1024_BIT);
449 if (ke_payload->set_key_exchange_data(ke_payload, key_data) != SUCCESS)
450 {
451 this->logger->log(this->logger, ERROR, "Could not build KE payload");
452 ke_payload->destroy(ke_payload);
453 message->destroy(message);
454 allocator_free_chunk(key_data);
455 return OUT_OF_RES;
456 }
457 allocator_free_chunk(key_data);
458
459 payload = (payload_t *) ke_payload;
460 }
461
462 payload->set_next_type(payload, NONCE);
463 message->add_payload(message, payload);
464
465 status = this->build_nonce_payload(this, (nonce_payload_t**)&payload);
466 if (status != SUCCESS)
467 {
468 this->logger->log(this->logger, ERROR, "Could not build NONCE payload");
469 message->destroy(message);
470 return status;
471 }
472 payload->set_next_type(payload, NO_PAYLOAD);
473 message->add_payload(message, payload);
474
475 status = message->generate(message, &packet);
476 if (status != SUCCESS)
477 {
478 this->logger->log(this->logger, ERROR, "Could not generate message");
479 message->destroy(message);
480 return status;
481 }
482
483
484 global_send_queue->add(global_send_queue, packet);
485
486 message->destroy(message);
487
488 this->state = IKE_SA_INIT_REQUESTED;
489
490 return SUCCESS;
491 }
492
493 /**
494 * @brief implements function private_ike_sa_t.get_id
495 */
496 static ike_sa_id_t* get_id(private_ike_sa_t *this)
497 {
498 return this->ike_sa_id;
499 }
500
501 /**
502 * implements private_ike_sa_t.build_sa_payload
503 */
504 static status_t build_sa_payload(private_ike_sa_t *this, sa_payload_t **payload)
505 {
506 sa_payload_t* sa_payload;
507 linked_list_iterator_t *iterator;
508 status_t status;
509
510 this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
511
512 sa_payload = sa_payload_create();
513 if (sa_payload == NULL)
514 {
515 return OUT_OF_RES;
516 }
517 status = sa_payload->create_proposal_substructure_iterator(sa_payload, &iterator, FALSE);
518 if (status != SUCCESS)
519 {
520 sa_payload->destroy(sa_payload);
521 return status;
522 }
523 status = global_configuration_manager->get_proposals_for_host(global_configuration_manager, this->other.host, iterator);
524 if (status != SUCCESS)
525 {
526 sa_payload->destroy(sa_payload);
527 return status;
528 }
529
530 *payload = sa_payload;
531
532 return SUCCESS;
533 }
534
535
536 /**
537 * implements private_ike_sa_t.build_nonce_payload
538 */
539 static status_t build_nonce_payload(private_ike_sa_t *this, nonce_payload_t **payload)
540 {
541 nonce_payload_t *nonce_payload;
542 chunk_t nonce;
543
544 this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
545
546 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer, 16, &nonce) != SUCCESS)
547 {
548 return OUT_OF_RES;
549 }
550
551 nonce_payload = nonce_payload_create();
552 if (nonce_payload == NULL)
553 {
554 return OUT_OF_RES;
555 }
556
557 nonce_payload->set_nonce(nonce_payload, nonce);
558
559 *payload = nonce_payload;
560
561 return SUCCESS;
562 }
563
564 /**
565 * @brief implements function destroy of private_ike_sa_t
566 */
567 static status_t destroy (private_ike_sa_t *this)
568 {
569 linked_list_iterator_t *iterator;
570
571 this->child_sas->create_iterator(this->child_sas, &iterator, TRUE);
572 while (iterator->has_next(iterator))
573 {
574 payload_t *payload;
575 iterator->current(iterator, (void**)&payload);
576 payload->destroy(payload);
577 }
578 iterator->destroy(iterator);
579 this->child_sas->destroy(this->child_sas);
580
581 this->ike_sa_id->destroy(this->ike_sa_id);
582 this->sent_messages->destroy(this->sent_messages);
583 this->randomizer->destroy(this->randomizer);
584 if (this->ike_sa_init_data.diffie_hellman != NULL)
585 {
586 this->ike_sa_init_data.diffie_hellman->destroy(this->ike_sa_init_data.diffie_hellman);
587 }
588
589 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
590
591 allocator_free(this);
592
593 return SUCCESS;
594 }
595
596 /*
597 * Described in Header
598 */
599 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
600 {
601 private_ike_sa_t *this = allocator_alloc_thing(private_ike_sa_t);
602 if (this == NULL)
603 {
604 return NULL;
605 }
606
607
608 /* Public functions */
609 this->public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
610 this->public.initialize_connection = (status_t(*)(ike_sa_t*, char*)) initialize_connection;
611 this->public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
612 this->public.destroy = (status_t(*)(ike_sa_t*))destroy;
613
614 this->build_sa_payload = build_sa_payload;
615 this->build_nonce_payload = build_nonce_payload;
616
617 this->build_message = build_message;
618 this->transto_ike_sa_init_responded = transto_ike_sa_init_responded;
619 this->transto_ike_auth_requested = transto_ike_auth_requested;
620
621
622
623 /* initialize private fields */
624 if (ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id)) != SUCCESS)
625 {
626 allocator_free(this);
627 return NULL;
628 }
629 this->child_sas = linked_list_create();
630 if (this->child_sas == NULL)
631 {
632 this->ike_sa_id->destroy(this->ike_sa_id);
633 allocator_free(this);
634 return NULL;
635 }
636 this->randomizer = randomizer_create();
637 if (this->randomizer == NULL)
638 {
639 this->child_sas->destroy(this->child_sas);
640 this->ike_sa_id->destroy(this->ike_sa_id);
641 allocator_free(this);
642 }
643 this->sent_messages = linked_list_create();
644 if (this->sent_messages == NULL)
645 {
646 this->randomizer->destroy(this->randomizer);
647 this->child_sas->destroy(this->child_sas);
648 this->ike_sa_id->destroy(this->ike_sa_id);
649 allocator_free(this);
650 }
651 this->logger = global_logger_manager->create_logger(global_logger_manager, IKE_SA, NULL);
652 if (this->logger == NULL)
653 {
654 this->randomizer->destroy(this->randomizer);
655 this->child_sas->destroy(this->child_sas);
656 this->ike_sa_id->destroy(this->ike_sa_id);
657 this->sent_messages->destroy(this->sent_messages);
658 allocator_free(this);
659 }
660
661 this->me.host = NULL;
662 this->other.host = NULL;
663 this->ike_sa_init_data.diffie_hellman = NULL;
664 this->ike_sa_init_data.dh_group_number = 0;
665 /* 1 means highest priority */
666 this->ike_sa_init_data.dh_group_priority = 1;
667 this->message_id_out = 0;
668 this->message_id_in = 0;
669
670
671 /* at creation time, IKE_SA isn't in a specific state */
672 this->state = NO_STATE;
673
674 return (&this->public);
675 }