- globals packed in a daemon
[strongswan.git] / Source / charon / sa / ike_sa.c
1 /**
2 * @file ike_sa.c
3 *
4 * @brief Implementation of ike_sa_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 "ike_sa.h"
24
25 #include <types.h>
26 #include <daemon.h>
27 #include <definitions.h>
28 #include <utils/allocator.h>
29 #include <utils/linked_list.h>
30 #include <utils/logger_manager.h>
31 #include <utils/randomizer.h>
32 #include <transforms/diffie_hellman.h>
33 #include <transforms/prf_plus.h>
34 #include <transforms/crypters/crypter.h>
35 #include <encoding/payloads/sa_payload.h>
36 #include <encoding/payloads/nonce_payload.h>
37 #include <encoding/payloads/ke_payload.h>
38 #include <encoding/payloads/transform_substructure.h>
39 #include <encoding/payloads/transform_attribute.h>
40 #include <sa/states/initiator_init.h>
41 #include <sa/states/responder_init.h>
42 #include <queues/jobs/delete_ike_sa_job.h>
43
44
45
46
47 typedef struct private_ike_sa_t private_ike_sa_t;
48
49 /**
50 * Private data of an ike_sa_t object.
51 */
52 struct private_ike_sa_t {
53
54 /**
55 * Protected part of a ike_sa_t object.
56 */
57 protected_ike_sa_t protected;
58
59 /**
60 * Creates a job to delete the given IKE_SA.
61 *
62 * @param this calling object
63 */
64 status_t (*create_delete_job) (private_ike_sa_t *this);
65
66 /**
67 * Resends the last sent reply.
68 *
69 * @param this calling object
70 */
71 status_t (*resend_last_reply) (private_ike_sa_t *this);
72
73 /* private values */
74
75 /**
76 * Identifier for the current IKE_SA
77 */
78 ike_sa_id_t *ike_sa_id;
79
80 /**
81 * Linked List containing the child sa's of the current IKE_SA
82 */
83 linked_list_t *child_sas;
84
85 /**
86 * Current state of the IKE_SA
87 */
88 state_t *current_state;
89
90 /**
91 * this SA's source for random data
92 */
93 randomizer_t *randomizer;
94
95 /**
96 * contains the last responded message
97 *
98 */
99 message_t *last_responded_message;
100
101 /**
102 * contains the last requested message
103 *
104 */
105 message_t *last_requested_message;
106
107 /**
108 * Informations of this host
109 */
110 struct {
111 host_t *host;
112 } me;
113
114 /**
115 * Informations of the other host
116 */
117 struct {
118 host_t *host;
119 } other;
120
121 /**
122 * Crypter object for initiator
123 */
124 crypter_t *crypter_initiator;
125
126 /**
127 * Crypter object for responder
128 */
129 crypter_t *crypter_responder;
130
131 /**
132 * Signer object for initiator
133 */
134 signer_t *signer_initiator;
135
136 /**
137 * Signer object for responder
138 */
139 signer_t *signer_responder;
140
141 /**
142 * prf function
143 */
144 prf_t *prf;
145
146
147
148 /**
149 * Shared secrets
150 */
151 struct {
152 /**
153 * Key used for deriving other keys
154 */
155 chunk_t d_key;
156
157 /**
158 * Key for authenticate (initiator)
159 */
160 chunk_t ai_key;
161
162 /**
163 * Key for authenticate (responder)
164 */
165 chunk_t ar_key;
166
167 /**
168 * Key for encryption (initiator)
169 */
170 chunk_t ei_key;
171
172 /**
173 * Key for encryption (responder)
174 */
175 chunk_t er_key;
176
177 /**
178 * Key for generating auth payload (initiator)
179 */
180 chunk_t pi_key;
181
182 /**
183 * Key for generating auth payload (responder)
184 */
185 chunk_t pr_key;
186
187 } secrets;
188
189 /**
190 * next message id to receive
191 */
192 u_int32_t message_id_in;
193
194 /**
195 * next message id to send
196 */
197 u_int32_t message_id_out;
198
199 /**
200 * a logger for this IKE_SA
201 */
202 logger_t *logger;
203 };
204
205
206 /**
207 * Implements protected_ike_sa_t.process_message.
208 */
209 static status_t process_message (private_ike_sa_t *this, message_t *message)
210 {
211 u_int32_t message_id;
212 exchange_type_t exchange_type;
213 bool is_request;
214
215 /* we must process each request or response from remote host */
216
217 /* find out type of message (request or response) */
218 is_request = message->get_request(message);
219 exchange_type = message->get_exchange_type(message);
220
221 this->logger->log(this->logger, CONTROL, "Process %s message of exchange type %s",(is_request) ? "REQUEST" : "RESPONSE",mapping_find(exchange_type_m,exchange_type));
222
223 message_id = message->get_message_id(message);
224
225 /*
226 * It has to be checked, if the message has to be resent cause of lost packets!
227 */
228 if (is_request && (message_id == (this->message_id_in - 1)))
229 {
230 /* message can be resent ! */
231 this->logger->log(this->logger, CONTROL|MORE, "Resent message detected. Send stored reply");
232 return (this->resend_last_reply(this));
233 }
234
235 /* Now, the message id is checked for request AND reply */
236 if (is_request)
237 {
238 /* In a request, the message has to be this->message_id_in (other case is already handled) */
239 if (message_id != this->message_id_in)
240 {
241 this->logger->log(this->logger, ERROR | MORE, "Message request with message id %d received, but %d expected",message_id,this->message_id_in);
242 return FAILED;
243 }
244 }
245 else
246 {
247 /* In a reply, the message has to be this->message_id_out -1 cause it is the reply to the last sent message*/
248 if (message_id != (this->message_id_out - 1))
249 {
250 this->logger->log(this->logger, ERROR | MORE, "Message reply with message id %d received, but %d expected",message_id,this->message_id_in);
251 return FAILED;
252 }
253 }
254
255 /* now the message is processed by the current state object */
256 /* the current state does change the current change to the next one*/
257 return this->current_state->process_message(this->current_state,message);
258 }
259
260 /**
261 * Implements protected_ike_sa_t.build_message.
262 */
263 static void build_message(private_ike_sa_t *this, exchange_type_t type, bool request, message_t **message)
264 {
265 message_t *new_message;
266 host_t *source, *destination;
267
268 this->logger->log(this->logger, CONTROL|MORE, "build empty message");
269 new_message = message_create();
270
271 source = this->me.host->clone(this->me.host);
272 destination = this->other.host->clone(this->other.host);
273
274 new_message->set_source(new_message, source);
275 new_message->set_destination(new_message, destination);
276 new_message->set_exchange_type(new_message, type);
277 new_message->set_request(new_message, request);
278 new_message->set_message_id(new_message, (request) ? this->message_id_out : this->message_id_in);
279 new_message->set_ike_sa_id(new_message, this->ike_sa_id);
280 *message = new_message;
281 }
282
283 /**
284 * Implements protected_ike_sa_t.process_configuration.
285 */
286 static status_t initialize_connection(private_ike_sa_t *this, char *name)
287 {
288 /* work is done in state object of type INITIATOR_INIT */
289 initiator_init_t *current_state;
290 status_t status;
291
292 if (this->current_state->get_state(this->current_state) != INITIATOR_INIT)
293 {
294 return FAILED;
295 }
296
297 current_state = (initiator_init_t *) this->current_state;
298
299 status = current_state->initiate_connection(current_state,name);
300
301 if (status != SUCCESS)
302 {
303 this->create_delete_job(this);
304 }
305 return status;
306 }
307
308 /**
309 * Implements protected_ike_sa_t.get_id.
310 */
311 static ike_sa_id_t* get_id(private_ike_sa_t *this)
312 {
313 return this->ike_sa_id;
314 }
315
316 /**
317 * Implements protected_ike_sa_t.compute_secrets.
318 */
319 static void compute_secrets(private_ike_sa_t *this,chunk_t dh_shared_secret,chunk_t initiator_nonce, chunk_t responder_nonce)
320 {
321 chunk_t concatenated_nonces;
322 chunk_t skeyseed;
323 chunk_t prf_plus_seed;
324 u_int64_t initiator_spi;
325 u_int64_t responder_spi;
326 prf_plus_t *prf_plus;
327
328
329 /*
330 * TODO check length for specific prf's
331 */
332 concatenated_nonces.len = (initiator_nonce.len + responder_nonce.len);
333 concatenated_nonces.ptr = allocator_alloc(concatenated_nonces.len);
334
335 /* first is initiator */
336 memcpy(concatenated_nonces.ptr,initiator_nonce.ptr,initiator_nonce.len);
337 /* second is responder */
338 memcpy(concatenated_nonces.ptr + initiator_nonce.len,responder_nonce.ptr,responder_nonce.len);
339
340 this->logger->log_chunk(this->logger, RAW, "Nonce data", &concatenated_nonces);
341
342 /* status of set_key is not checked */
343 this->prf->set_key(this->prf,concatenated_nonces);
344
345 this->prf->allocate_bytes(this->prf,dh_shared_secret,&skeyseed);
346
347 allocator_free_chunk(&concatenated_nonces);
348
349 prf_plus_seed.len = (initiator_nonce.len + responder_nonce.len + 16);
350 prf_plus_seed.ptr = allocator_alloc(prf_plus_seed.len);
351
352 /* first is initiator */
353 memcpy(prf_plus_seed.ptr,initiator_nonce.ptr,initiator_nonce.len);
354 /* second is responder */
355 memcpy(prf_plus_seed.ptr + initiator_nonce.len,responder_nonce.ptr,responder_nonce.len);
356 /* third is initiator spi */
357 initiator_spi = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
358 memcpy(prf_plus_seed.ptr + initiator_nonce.len + responder_nonce.len,&initiator_spi,8);
359 /* fourth is responder spi */
360 responder_spi = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
361 memcpy(prf_plus_seed.ptr + initiator_nonce.len + responder_nonce.len + 8,&responder_spi,8);
362
363 this->logger->log_chunk(this->logger, PRIVATE | MORE, "Keyseed", &skeyseed);
364 this->logger->log_chunk(this->logger, PRIVATE | MORE, "PRF+ Seed", &prf_plus_seed);
365
366 this->logger->log(this->logger, CONTROL | MOST, "Set new key of prf object");
367 this->prf->set_key(this->prf,skeyseed);
368 allocator_free_chunk(&skeyseed);
369
370 this->logger->log(this->logger, CONTROL | MOST, "Create new prf+ object");
371 prf_plus = prf_plus_create(this->prf, prf_plus_seed);
372 allocator_free_chunk(&prf_plus_seed);
373
374 prf_plus->allocate_bytes(prf_plus,this->prf->get_block_size(this->prf),&(this->secrets.d_key));
375 this->logger->log_chunk(this->logger, PRIVATE, "Sk_d secret", &(this->secrets.d_key));
376
377 prf_plus->allocate_bytes(prf_plus,this->crypter_initiator->get_block_size(this->crypter_initiator),&(this->secrets.ei_key));
378 this->logger->log_chunk(this->logger, PRIVATE, "Sk_ei secret", &(this->secrets.ei_key));
379 this->crypter_initiator->set_key(this->crypter_initiator,this->secrets.ei_key);
380
381 prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.er_key));
382 this->logger->log_chunk(this->logger, PRIVATE, "Sk_er secret", &(this->secrets.er_key));
383 this->crypter_responder->set_key(this->crypter_responder,this->secrets.er_key);
384
385 prf_plus->allocate_bytes(prf_plus,this->signer_initiator->get_block_size(this->signer_initiator),&(this->secrets.ai_key));
386 this->logger->log_chunk(this->logger, PRIVATE, "Sk_ai secret", &(this->secrets.ai_key));
387 this->signer_initiator->set_key(this->signer_initiator,this->secrets.ai_key);
388
389 prf_plus->allocate_bytes(prf_plus,this->signer_responder->get_block_size(this->signer_responder),&(this->secrets.ar_key));
390 this->logger->log_chunk(this->logger, PRIVATE, "Sk_ar secret", &(this->secrets.ar_key));
391 this->signer_responder->set_key(this->signer_responder,this->secrets.ar_key);
392
393 prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.pi_key));
394 this->logger->log_chunk(this->logger, PRIVATE, "Sk_pi secret", &(this->secrets.pi_key));
395
396 prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.pr_key));
397 this->logger->log_chunk(this->logger, PRIVATE, "Sk_pr secret", &(this->secrets.pr_key));
398
399 prf_plus->destroy(prf_plus);
400 }
401
402 /**
403 * Implements protected_ike_sa_t.resend_last_reply.
404 */
405 static status_t resend_last_reply(private_ike_sa_t *this)
406 {
407 packet_t *packet;
408 status_t status;
409
410 status = this->last_responded_message->generate(this->last_responded_message, NULL, NULL, &packet);
411 if (status != SUCCESS)
412 {
413 this->logger->log(this->logger, ERROR, "Could not generate message to resent");
414 return status;
415 }
416
417 charon->send_queue->add(charon->send_queue, packet);
418 return SUCCESS;
419 }
420
421 /**
422 * Implements protected_ike_sa_t.resend_last_reply.
423 */
424 static status_t create_delete_job(private_ike_sa_t *this)
425 {
426 job_t *delete_job;
427
428 this->logger->log(this->logger, CONTROL | MORE, "Going to create job to delete this IKE_SA");
429
430 delete_job = (job_t *) delete_ike_sa_job_create(this->ike_sa_id);
431 charon->job_queue->add(charon->job_queue,delete_job);
432
433 return SUCCESS;
434 }
435
436 /**
437 * Implementation of protected_ike_sa_t.set_new_state.
438 */
439 static void set_new_state (private_ike_sa_t *this, state_t *state)
440 {
441 this->logger->log(this->logger, ERROR, "Change current state %s to %s",mapping_find(ike_sa_state_m,this->current_state->get_state(this->current_state)),mapping_find(ike_sa_state_m,state->get_state(state)));
442 this->current_state = state;
443 }
444
445 /**
446 * Implementation of protected_ike_sa_t.get_logger.
447 */
448 static logger_t *get_logger (private_ike_sa_t *this)
449 {
450 return this->logger;
451 }
452
453 /**
454 * Implementation of protected_ike_sa_t.get_my_host.
455 */
456 static host_t *get_my_host (private_ike_sa_t *this)
457 {
458 return this->me.host;
459 }
460
461 /**
462 * Implementation of protected_ike_sa_t.get_other_host.
463 */
464 static host_t *get_other_host (private_ike_sa_t *this)
465 {
466 return this->other.host;
467 }
468
469 /**
470 * Implementation of protected_ike_sa_t.set_my_host.
471 */
472 static void set_my_host (private_ike_sa_t *this, host_t *my_host)
473 {
474 if (this->me.host != NULL)
475 {
476 this ->logger->log(this->logger, CONTROL|MOST, "Destroy existing my host object");
477 this->me.host->destroy(this->me.host);
478 }
479 this->me.host = my_host;
480 }
481
482 /**
483 * Implementation of protected_ike_sa_t.set_other_host.
484 */
485 static void set_other_host (private_ike_sa_t *this, host_t *other_host)
486 {
487 if (this->other.host != NULL)
488 {
489 this ->logger->log(this->logger, CONTROL|MOST, "Destroy existing other host object");
490 this->other.host->destroy(this->other.host);
491 }
492 this->other.host = other_host;
493 }
494
495 /**
496 * Implementation of protected_ike_sa_t.set_prf.
497 */
498 static status_t create_transforms_from_proposal (private_ike_sa_t *this,proposal_substructure_t *proposal)
499 {
500 status_t status;
501 u_int16_t encryption_algorithm;
502 u_int16_t encryption_algorithm_key_length;
503 u_int16_t integrity_algorithm;
504 u_int16_t integrity_algorithm_key_length;
505 u_int16_t pseudo_random_function;
506 u_int16_t pseudo_random_function_key_length;
507
508 this->logger->log(this->logger, CONTROL|MORE, "Going to create transform objects for proposal");
509
510 this->logger->log(this->logger, CONTROL|MOST, "Get encryption transform type");
511 status = proposal->get_info_for_transform_type(proposal,ENCRYPTION_ALGORITHM,&(encryption_algorithm),&(encryption_algorithm_key_length));
512 if (status != SUCCESS)
513 {
514 this->logger->log(this->logger, ERROR|MORE, "Could not get encryption transform type");
515 return status;
516 }
517 this->logger->log(this->logger, CONTROL|MORE, "Encryption algorithm: %s with keylength %d",mapping_find(encryption_algorithm_m,encryption_algorithm),encryption_algorithm_key_length);
518
519 this->logger->log(this->logger, CONTROL|MOST, "Get integrity transform type");
520 status = proposal->get_info_for_transform_type(proposal,INTEGRITY_ALGORITHM,&(integrity_algorithm),&(integrity_algorithm_key_length));
521 if (status != SUCCESS)
522 {
523 this->logger->log(this->logger, ERROR|MORE, "Could not get integrity transform type");
524 return status;
525 }
526 this->logger->log(this->logger, CONTROL|MORE, "integrity algorithm: %s with keylength %d",mapping_find(integrity_algorithm_m,integrity_algorithm),integrity_algorithm_key_length);
527
528 this->logger->log(this->logger, CONTROL|MOST, "Get prf transform type");
529 status = proposal->get_info_for_transform_type(proposal,PSEUDO_RANDOM_FUNCTION,&(pseudo_random_function),&(pseudo_random_function_key_length));
530 if (status != SUCCESS)
531 {
532 this->logger->log(this->logger, ERROR|MORE, "Could not prf transform type");
533 return status;
534 }
535 this->logger->log(this->logger, CONTROL|MORE, "prf: %s with keylength %d",mapping_find(pseudo_random_function_m,pseudo_random_function),pseudo_random_function_key_length);
536
537 if (this->prf != NULL)
538 {
539 this->prf->destroy(this->prf);
540 }
541 this->prf = prf_create(pseudo_random_function);
542 if (this->prf == NULL)
543 {
544 this->logger->log(this->logger, ERROR|MORE, "prf not supported!");
545 return FAILED;
546 }
547
548 if (this->crypter_initiator != NULL)
549 {
550 this->crypter_initiator->destroy(this->crypter_initiator);
551 }
552 this->crypter_initiator = crypter_create(encryption_algorithm,encryption_algorithm_key_length);
553 if (this->crypter_initiator == NULL)
554 {
555 this->logger->log(this->logger, ERROR|MORE, "encryption algorithm not supported!");
556 return FAILED;
557 }
558
559 if (this->crypter_responder != NULL)
560 {
561 this->crypter_responder->destroy(this->crypter_responder);
562 }
563 this->crypter_responder = crypter_create(encryption_algorithm,encryption_algorithm_key_length);
564 if (this->crypter_responder == NULL)
565 {
566 this->logger->log(this->logger, ERROR|MORE, "encryption algorithm not supported!");
567 return FAILED;
568 }
569
570 if (this->signer_initiator != NULL)
571 {
572 this->signer_initiator->destroy(this->signer_initiator);
573 }
574 this->signer_initiator = signer_create(integrity_algorithm);
575 if (this->signer_initiator == NULL)
576 {
577 this->logger->log(this->logger, ERROR|MORE, "integrity algorithm not supported!");
578 return FAILED;
579 }
580
581 if (this->signer_responder != NULL)
582 {
583 this->signer_responder->destroy(this->signer_responder);
584 }
585 this->signer_responder = signer_create(integrity_algorithm);
586 if (this->signer_responder == NULL)
587 {
588 this->logger->log(this->logger, ERROR|MORE, "integrity algorithm not supported!");
589 return FAILED;
590 }
591
592 return SUCCESS;
593 }
594
595 /**
596 * Implementation of protected_ike_sa_t.get_randomizer.
597 */
598 static randomizer_t *get_randomizer (private_ike_sa_t *this)
599 {
600 return this->randomizer;
601 }
602
603 /**
604 * Implementation of protected_ike_sa_t.set_last_requested_message.
605 */
606 static status_t set_last_requested_message (private_ike_sa_t *this,message_t * message)
607 {
608 if (this->last_requested_message != NULL)
609 {
610 /* destroy message */
611 this->last_requested_message->destroy(this->last_requested_message);
612 }
613
614 if (message->get_message_id(message) != this->message_id_out)
615 {
616 this->logger->log(this->logger, CONTROL|MOST, "last requested message could not be set cause id was not as expected");
617 return FAILED;
618 }
619 this->logger->log(this->logger, CONTROL|MOST, "replace last requested message with new one");
620 this->last_requested_message = message;
621
622 /* message counter can now be increased */
623 this->logger->log(this->logger, CONTROL|MOST, "Increate message counter for outgoing messages");
624 this->message_id_out++;
625 return SUCCESS;
626 }
627
628 /**
629 * Implementation of protected_ike_sa_t.set_last_responded_message.
630 */
631 static status_t set_last_responded_message (private_ike_sa_t *this,message_t * message)
632 {
633 if (this->last_responded_message != NULL)
634 {
635 /* destroy message */
636 this->last_responded_message->destroy(this->last_responded_message);
637 }
638 if (message->get_message_id(message) != this->message_id_in)
639 {
640 this->logger->log(this->logger, CONTROL|MOST, "last responded message could not be set cause id was not as expected");
641 return FAILED;
642
643 }
644 this->logger->log(this->logger, CONTROL|MOST, "replace last responded message with new one");
645 this->last_responded_message = message;
646
647 /* message counter can now be increased */
648 this->logger->log(this->logger, CONTROL|MOST, "Increate message counter for incoming messages");
649 this->message_id_in++;
650
651 return SUCCESS;
652 }
653
654
655 /**
656 * Implements protected_ike_sa_t.destroy.
657 */
658 static void destroy (private_ike_sa_t *this)
659 {
660 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy IKE_SA");
661
662 /* destroy child sa's */
663 this->logger->log(this->logger, CONTROL | MOST, "Destroy all child_sa's");
664 while (this->child_sas->get_count(this->child_sas) > 0)
665 {
666 void *child_sa;
667 if (this->child_sas->remove_first(this->child_sas, &child_sa) != SUCCESS)
668 {
669 break;
670 }
671 /* destroy child sa */
672 }
673 this->child_sas->destroy(this->child_sas);
674
675 this->logger->log(this->logger, CONTROL | MOST, "Destroy secrets");
676
677 allocator_free(this->secrets.d_key.ptr);
678 allocator_free(this->secrets.ai_key.ptr);
679 allocator_free(this->secrets.ar_key.ptr);
680 allocator_free(this->secrets.ei_key.ptr);
681 allocator_free(this->secrets.er_key.ptr);
682 allocator_free(this->secrets.pi_key.ptr);
683 allocator_free(this->secrets.pr_key.ptr);
684
685 if (this->crypter_initiator != NULL)
686 {
687 this->crypter_initiator->destroy(this->crypter_initiator);
688 }
689
690 if (this->crypter_responder != NULL)
691 {
692 this->crypter_responder->destroy(this->crypter_responder);
693 }
694
695 if (this->signer_initiator != NULL)
696 {
697 this->signer_initiator->destroy(this->signer_initiator);
698 }
699
700 if (this->signer_responder != NULL)
701 {
702 this->signer_responder->destroy(this->signer_responder);
703 }
704
705 if (this->prf != NULL)
706 {
707 this->prf->destroy(this->prf);
708 }
709
710 /* destroy ike_sa_id */
711 this->ike_sa_id->destroy(this->ike_sa_id);
712
713 /* destroy stored requested message */
714 if (this->last_requested_message != NULL)
715 {
716 this->last_requested_message->destroy(this->last_requested_message);
717 }
718
719 /* destroy stored responded messages */
720 if (this->last_responded_message != NULL)
721 {
722 this->last_responded_message->destroy(this->last_responded_message);
723 }
724
725 this->randomizer->destroy(this->randomizer);
726
727 if (this->me.host != NULL)
728 {
729 this->me.host->destroy(this->me.host);
730 }
731
732 if (this->other.host != NULL)
733 {
734 this->other.host->destroy(this->other.host);
735 }
736
737 this->logger->log(this->logger, CONTROL | MOST, "Destroy current state object");
738 this->current_state->destroy(this->current_state);
739
740 this->logger->log(this->logger, CONTROL | MOST, "Destroy logger of IKE_SA");
741 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
742
743 allocator_free(this);
744 }
745
746 /*
747 * Described in Header
748 */
749 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
750 {
751 private_ike_sa_t *this = allocator_alloc_thing(private_ike_sa_t);
752
753 /* Public functions */
754 this->protected.public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
755 this->protected.public.initialize_connection = (status_t(*)(ike_sa_t*, char*)) initialize_connection;
756 this->protected.public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
757 this->protected.public.destroy = (void(*)(ike_sa_t*))destroy;
758
759 /* protected functions */
760 this->protected.build_message = (void (*) (protected_ike_sa_t *, exchange_type_t , bool , message_t **)) build_message;
761 this->protected.compute_secrets = (void (*) (protected_ike_sa_t *,chunk_t ,chunk_t , chunk_t )) compute_secrets;
762 this->protected.get_logger = (logger_t *(*) (protected_ike_sa_t *)) get_logger;
763 this->protected.get_my_host = (host_t *(*) (protected_ike_sa_t *)) get_my_host;
764 this->protected.get_other_host = (host_t *(*) (protected_ike_sa_t *)) get_other_host;
765 this->protected.set_my_host = (void(*) (protected_ike_sa_t *,host_t *)) set_my_host;
766 this->protected.set_other_host = (void(*) (protected_ike_sa_t *, host_t *)) set_other_host;
767 this->protected.get_randomizer = (randomizer_t *(*) (protected_ike_sa_t *)) get_randomizer;
768 this->protected.set_last_requested_message = (status_t (*) (protected_ike_sa_t *,message_t *)) set_last_requested_message;
769 this->protected.set_last_responded_message = (status_t (*) (protected_ike_sa_t *,message_t *)) set_last_responded_message;
770 this->protected.create_transforms_from_proposal = (status_t (*) (protected_ike_sa_t *,proposal_substructure_t *)) create_transforms_from_proposal;
771 this->protected.set_new_state = (void (*) (protected_ike_sa_t *,state_t *)) set_new_state;
772
773 /* private functions */
774 this->resend_last_reply = resend_last_reply;
775 this->create_delete_job = create_delete_job;
776
777
778 /* initialize private fields */
779 this->logger = charon->logger_manager->create_logger(charon->logger_manager, IKE_SA, NULL);
780
781 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
782 this->child_sas = linked_list_create();
783 this->randomizer = randomizer_create();
784
785 this->me.host = NULL;
786 this->other.host = NULL;
787 this->last_requested_message = NULL;
788 this->last_responded_message = NULL;
789 this->message_id_out = 0;
790 this->message_id_in = 0;
791 this->secrets.d_key = CHUNK_INITIALIZER;
792 this->secrets.ai_key = CHUNK_INITIALIZER;
793 this->secrets.ar_key = CHUNK_INITIALIZER;
794 this->secrets.ei_key = CHUNK_INITIALIZER;
795 this->secrets.er_key = CHUNK_INITIALIZER;
796 this->secrets.pi_key = CHUNK_INITIALIZER;
797 this->secrets.pr_key = CHUNK_INITIALIZER;
798 this->crypter_initiator = NULL;
799 this->crypter_responder = NULL;
800 this->signer_initiator = NULL;
801 this->signer_responder = NULL;
802 this->prf = NULL;
803
804 /* at creation time, IKE_SA is in a initiator state */
805 if (ike_sa_id->is_initiator(ike_sa_id))
806 {
807 this->current_state = (state_t *) initiator_init_create(&(this->protected));
808 }
809 else
810 {
811 this->current_state = (state_t *) responder_init_create(&(this->protected));
812 }
813 return &(this->protected.public);
814 }