- reworked configuration framework completly
[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/delete_payload.h>
39 #include <encoding/payloads/transform_substructure.h>
40 #include <encoding/payloads/transform_attribute.h>
41 #include <sa/states/initiator_init.h>
42 #include <sa/states/responder_init.h>
43 #include <queues/jobs/retransmit_request_job.h>
44 #include <queues/jobs/delete_established_ike_sa_job.h>
45
46
47
48
49 typedef struct private_ike_sa_t private_ike_sa_t;
50
51 /**
52 * Private data of an ike_sa_t object.
53 */
54 struct private_ike_sa_t {
55
56 /**
57 * Protected part of a ike_sa_t object.
58 */
59 protected_ike_sa_t protected;
60
61 /**
62 * Identifier for the current IKE_SA.
63 */
64 ike_sa_id_t *ike_sa_id;
65
66 /**
67 * Linked List containing the child sa's of the current IKE_SA.
68 */
69 linked_list_t *child_sas;
70
71 /**
72 * Current state of the IKE_SA represented as state_t object.
73 *
74 * A state object representates one of the following states and is processing
75 * messages in the specific state:
76 * - INITIATOR_INIT
77 * - RESPONDER_INIT
78 * - IKE_SA_INIT_REQUESTED
79 * - IKE_SA_INIT_RESPONDED
80 * - IKE_AUTH_REQUESTED
81 * -IKE_SA_ESTABLISHED
82 */
83 state_t *current_state;
84
85 /**
86 * INIT configuration, needed for the IKE_SA_INIT exchange.
87 *
88 * Gets set in states:
89 * - INITATOR_INIT
90 * - RESPONDER_INIT
91 *
92 * Available in states:
93 * - IKE_SA_INIT_REQUESTED
94 * - IKE_SA_INIT_RESPONDED
95 * - IKE_AUTH_REQUESTED
96 * -IKE_SA_ESTABLISHED
97 */
98 connection_t *connection;
99
100 /**
101 * SA configuration, needed for all other exchanges after IKE_SA_INIT exchange.
102 *
103 * Gets set in states:
104 * - IKE_SA_INIT_REQUESTED
105 * - IKE_SA_INIT_RESPONDED
106 *
107 * Available in states:
108 * - IKE_AUTH_REQUESTED
109 * -IKE_SA_ESTABLISHED
110 */
111 policy_t *policy;
112
113 /**
114 * This SA's source for random data.
115 *
116 * Is available in every state.
117 */
118 randomizer_t *randomizer;
119
120 /**
121 * The last responded message.
122 */
123 message_t *last_responded_message;
124
125 /**
126 * The ast requested message.
127 */
128 message_t *last_requested_message;
129
130 /**
131 * Crypter object for initiator.
132 */
133 crypter_t *crypter_initiator;
134
135 /**
136 * Crypter object for responder.
137 */
138 crypter_t *crypter_responder;
139
140 /**
141 * Signer object for initiator.
142 */
143 signer_t *signer_initiator;
144
145 /**
146 * Signer object for responder.
147 */
148 signer_t *signer_responder;
149
150 /**
151 * Multi purpose prf, set key, use it, forget it
152 */
153 prf_t *prf;
154
155 /**
156 * Prf function for derivating keymat child SAs
157 */
158 prf_t *child_prf;
159
160 /**
161 * PRF, with key set to pi_key, used for authentication
162 */
163 prf_t *prf_auth_i;
164
165 /**
166 * PRF, with key set to pr_key, used for authentication
167 */
168 prf_t *prf_auth_r;
169
170 /**
171 * Next message id to receive.
172 */
173 u_int32_t message_id_in;
174
175 /**
176 * Next message id to send.
177 */
178 u_int32_t message_id_out;
179
180 /**
181 * Last reply id which was successfully received.
182 */
183 int32_t last_replied_message_id;
184
185 /**
186 * A logger for this IKE_SA.
187 */
188 logger_t *logger;
189
190 /**
191 * Resends the last sent reply.
192 *
193 * @param this calling object
194 */
195 status_t (*resend_last_reply) (private_ike_sa_t *this);
196 };
197
198 /**
199 * Implementation of ike_sa_t.process_message.
200 */
201 static status_t process_message (private_ike_sa_t *this, message_t *message)
202 {
203 u_int32_t message_id;
204 exchange_type_t exchange_type;
205 bool is_request;
206
207 /* We must process each request or response from remote host */
208
209 /* Find out type of message (request or response) */
210 is_request = message->get_request(message);
211 exchange_type = message->get_exchange_type(message);
212
213 this->logger->log(this->logger, CONTROL, "Process %s message of exchange type %s",
214 (is_request) ? "REQUEST" : "RESPONSE",mapping_find(exchange_type_m,exchange_type));
215
216 message_id = message->get_message_id(message);
217
218 /*
219 * It has to be checked, if the message has to be resent cause of lost packets!
220 */
221 if (is_request && (message_id == (this->message_id_in - 1)))
222 {
223 /* Message can be resent ! */
224 this->logger->log(this->logger, CONTROL|LEVEL1, "Resent request detected. Send stored reply.");
225 return (this->resend_last_reply(this));
226 }
227
228 /* Now, the message id is checked for request AND reply */
229 if (is_request)
230 {
231 /* In a request, the message has to be this->message_id_in (other case is already handled) */
232 if (message_id != this->message_id_in)
233 {
234 this->logger->log(this->logger, ERROR | LEVEL1,
235 "Message request with message id %d received, but %d expected",
236 message_id,this->message_id_in);
237 return FAILED;
238 }
239 }
240 else
241 {
242 /* In a reply, the message has to be this->message_id_out -1 cause it is the reply to the last sent message*/
243 if (message_id != (this->message_id_out - 1))
244 {
245 this->logger->log(this->logger, ERROR | LEVEL1,
246 "Message reply with message id %d received, but %d expected",
247 message_id,this->message_id_in);
248 return FAILED;
249 }
250 }
251
252 /* now the message is processed by the current state object.
253 * The specific state object is responsible to check if a message can be received in
254 * the state it represents.
255 * The current state is also responsible to change the state object to the next state
256 * by calling protected_ike_sa_t.set_new_state*/
257 return this->current_state->process_message(this->current_state,message);
258 }
259
260 /**
261 * Implementation of 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 *me, *other;
267
268 me = this->connection->get_my_host(this->connection);
269 other = this->connection->get_other_host(this->connection);
270
271 this->logger->log(this->logger, CONTROL|LEVEL2, "Build empty message");
272 new_message = message_create();
273 new_message->set_source(new_message, me->clone(me));
274 new_message->set_destination(new_message, other->clone(other));
275 new_message->set_exchange_type(new_message, type);
276 new_message->set_request(new_message, request);
277 new_message->set_message_id(new_message, (request) ? this->message_id_out : this->message_id_in);
278 new_message->set_ike_sa_id(new_message, this->ike_sa_id);
279
280 *message = new_message;
281 }
282
283 /**
284 * Implementation of protected_ike_sa_t.initiate_connection.
285 */
286 static status_t initiate_connection(private_ike_sa_t *this, connection_t *connection)
287 {
288 initiator_init_t *current_state;
289
290 /* Work is done in state object of type INITIATOR_INIT. All other states are not
291 * initial states and so don't have a initialize_connection function */
292
293 if (this->current_state->get_state(this->current_state) != INITIATOR_INIT)
294 {
295 return FAILED;
296 }
297
298 current_state = (initiator_init_t *) this->current_state;
299
300 return current_state->initiate_connection(current_state, connection);
301 }
302
303 /**
304 * Implementation of ike_sa_t.send_delete_ike_sa_request.
305 */
306 static void send_delete_ike_sa_request (private_ike_sa_t *this)
307 {
308 message_t *informational_request;
309 delete_payload_t *delete_payload;
310 crypter_t *crypter;
311 signer_t *signer;
312 packet_t *packet;
313 status_t status;
314
315 if (this->current_state->get_state(this->current_state) != IKE_SA_ESTABLISHED)
316 {
317 return;
318 }
319
320 /* build empty INFORMATIONAL message */
321 this->protected.build_message(&(this->protected), INFORMATIONAL, TRUE, &informational_request);
322
323 delete_payload = delete_payload_create();
324 delete_payload->set_protocol_id(delete_payload,IKE);
325
326 informational_request->add_payload(informational_request,(payload_t *)delete_payload);
327
328 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
329 {
330 crypter = this->crypter_initiator;
331 signer = this->signer_initiator;
332 }
333 else
334 {
335 crypter = this->crypter_responder;
336 signer = this->signer_responder;
337 }
338
339 status = informational_request->generate(informational_request,
340 crypter,
341 signer, &packet);
342 informational_request->destroy(informational_request);
343 if (status != SUCCESS)
344 {
345 this->logger->log(this->logger, ERROR, "Could not generate packet from message");
346 return ;
347 }
348
349 charon->send_queue->add(charon->send_queue,packet);
350 }
351
352 /**
353 * Implementation of protected_ike_sa_t.get_id.
354 */
355 static ike_sa_id_t* get_id(private_ike_sa_t *this)
356 {
357 return this->ike_sa_id;
358 }
359
360 /**
361 * Implementation of private_ike_sa_t.resend_last_reply.
362 */
363 static status_t resend_last_reply(private_ike_sa_t *this)
364 {
365 packet_t *packet;
366
367 this->logger->log(this->logger, CONTROL | LEVEL1, "Going to retransmit last reply");
368 packet = this->last_responded_message->get_packet(this->last_responded_message);
369 charon->send_queue->add(charon->send_queue, packet);
370
371 return SUCCESS;
372 }
373
374 /**
375 * Implementation of ike_sa_t.retransmit_request.
376 */
377 status_t retransmit_request (private_ike_sa_t *this, u_int32_t message_id)
378 {
379 packet_t *packet;
380
381 if (this->last_requested_message == NULL)
382 {
383 return NOT_FOUND;
384 }
385
386 if (message_id == this->last_replied_message_id)
387 {
388 return NOT_FOUND;
389 }
390
391 if ((this->last_requested_message->get_message_id(this->last_requested_message)) != message_id)
392 {
393 return NOT_FOUND;
394 }
395
396 this->logger->log(this->logger, CONTROL | LEVEL1, "Going to retransmit message with id %d",message_id);
397 packet = this->last_requested_message->get_packet(this->last_requested_message);
398 charon->send_queue->add(charon->send_queue, packet);
399
400 return SUCCESS;
401 }
402
403 /**
404 * Implementation of protected_ike_sa_t.set_new_state.
405 */
406 static void set_new_state (private_ike_sa_t *this, state_t *state)
407 {
408 this->logger->log(this->logger, CONTROL, "statechange: %s => %s",
409 mapping_find(ike_sa_state_m,this->current_state->get_state(this->current_state)),
410 mapping_find(ike_sa_state_m,state->get_state(state)));
411 this->current_state = state;
412 }
413
414 /**
415 * Implementation of protected_ike_sa_t.get_logger.
416 */
417 static logger_t *get_logger (private_ike_sa_t *this)
418 {
419 return this->logger;
420 }
421
422 /**
423 * Implementation of protected_ike_sa_t.get_connection.
424 */
425 static connection_t *get_connection (private_ike_sa_t *this)
426 {
427 return this->connection;
428 }
429
430 /**
431 * Implementation of protected_ike_sa_t.set_connection.
432 */
433 static void set_connection (private_ike_sa_t *this,connection_t * connection)
434 {
435 this->connection = connection;
436 }
437
438 /**
439 * Implementation of protected_ike_sa_t.get_policy.
440 */
441 static policy_t *get_policy (private_ike_sa_t *this)
442 {
443 return this->policy;
444 }
445
446 /**
447 * Implementation of protected_ike_sa_t.set_policy.
448 */
449 static void set_policy (private_ike_sa_t *this,policy_t * policy)
450 {
451 this->policy = policy;
452 }
453
454 /**
455 * Implementation of protected_ike_sa_t.get_prf.
456 */
457 static prf_t *get_prf (private_ike_sa_t *this)
458 {
459 return this->prf;
460 }
461
462 /**
463 * Implementation of protected_ike_sa_t.get_prf.
464 */
465 static prf_t *get_child_prf (private_ike_sa_t *this)
466 {
467 return this->child_prf;
468 }
469
470 /**
471 * Implementation of protected_ike_sa_t.get_prf_auth_i.
472 */
473 static prf_t *get_prf_auth_i (private_ike_sa_t *this)
474 {
475 return this->prf_auth_i;
476 }
477
478 /**
479 * Implementation of protected_ike_sa_t.get_prf_auth_r.
480 */
481 static prf_t *get_prf_auth_r (private_ike_sa_t *this)
482 {
483 return this->prf_auth_r;
484 }
485
486
487 /**
488 * Implementation of protected_ike_sa_t.build_transforms.
489 */
490 static status_t build_transforms(private_ike_sa_t *this, proposal_t *proposal, diffie_hellman_t *dh, chunk_t nonce_i, chunk_t nonce_r)
491 {
492 chunk_t nonces, nonces_spis, skeyseed, key, secret;
493 u_int64_t spi_i, spi_r;
494 prf_plus_t *prf_plus;
495 algorithm_t *algo;
496 size_t key_size;
497
498 /*
499 * Build the PRF+ instance for deriving keys
500 */
501 if (this->prf != NULL)
502 {
503 this->prf->destroy(this->prf);
504 }
505 proposal->get_algorithm(proposal, IKE, PSEUDO_RANDOM_FUNCTION, &algo);
506 if (algo == NULL)
507 {
508 this->logger->log(this->logger, ERROR|LEVEL2, "No PRF algoithm selected!?");
509 return FAILED;
510 }
511 this->prf = prf_create(algo->algorithm);
512 if (this->prf == NULL)
513 {
514 this->logger->log(this->logger, ERROR|LEVEL1,
515 "PSEUDO_RANDOM_FUNCTION %s not supported!",
516 mapping_find(pseudo_random_function_m, algo->algorithm));
517 return FAILED;
518 }
519
520 /* concatenate nonces = nonce_i | nonce_r */
521 nonces = allocator_alloc_as_chunk(nonce_i.len + nonce_r.len);
522 memcpy(nonces.ptr, nonce_i.ptr, nonce_i.len);
523 memcpy(nonces.ptr + nonce_i.len, nonce_r.ptr, nonce_r.len);
524
525 /* concatenate prf_seed = nonce_i | nonce_r | spi_i | spi_r */
526 nonces_spis = allocator_alloc_as_chunk(nonces.len + 16);
527 memcpy(nonces_spis.ptr, nonces.ptr, nonces.len);
528 spi_i = this->ike_sa_id->get_initiator_spi(this->ike_sa_id);
529 spi_r = this->ike_sa_id->get_responder_spi(this->ike_sa_id);
530 memcpy(nonces_spis.ptr + nonces.len, &spi_i, 8);
531 memcpy(nonces_spis.ptr + nonces.len + 8, &spi_r, 8);
532
533 /* SKEYSEED = prf(Ni | Nr, g^ir) */
534 dh->get_shared_secret(dh, &secret);
535 this->logger->log_chunk(this->logger, PRIVATE, "Shared Diffie Hellman secret", secret);
536 this->prf->set_key(this->prf, nonces);
537 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
538 this->logger->log_chunk(this->logger, PRIVATE | LEVEL1, "SKEYSEED", skeyseed);
539 allocator_free_chunk(&secret);
540
541 /* prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr )
542 * = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr
543 *
544 * we use the prf directly for prf+
545 */
546 this->prf->set_key(this->prf, skeyseed);
547 prf_plus = prf_plus_create(this->prf, nonces_spis);
548
549 /* clean up unused stuff */
550 allocator_free_chunk(&nonces);
551 allocator_free_chunk(&nonces_spis);
552 allocator_free_chunk(&skeyseed);
553
554
555 /*
556 * We now can derive all of our key. We build the transforms
557 * directly.
558 */
559
560
561 /* SK_d used for prf+ to derive keys for child SAs */
562 this->child_prf = prf_create(algo->algorithm);
563 key_size = this->child_prf->get_key_size(this->child_prf);
564 prf_plus->allocate_bytes(prf_plus, key_size, &key);
565 this->logger->log_chunk(this->logger, PRIVATE, "Sk_d secret", key);
566 this->child_prf->set_key(this->child_prf, key);
567 allocator_free_chunk(&key);
568
569
570 /* SK_ai/SK_ar used for integrity protection */
571 proposal->get_algorithm(proposal, IKE, INTEGRITY_ALGORITHM, &algo);
572 if (algo == NULL)
573 {
574 this->logger->log(this->logger, ERROR|LEVEL2, "No integrity algoithm selected?!");
575 return FAILED;
576 }
577 if (this->signer_initiator != NULL)
578 {
579 this->signer_initiator->destroy(this->signer_initiator);
580 }
581 if (this->signer_responder != NULL)
582 {
583 this->signer_responder->destroy(this->signer_responder);
584 }
585
586 this->signer_initiator = signer_create(algo->algorithm);
587 this->signer_responder = signer_create(algo->algorithm);
588 if (this->signer_initiator == NULL || this->signer_responder == NULL)
589 {
590 this->logger->log(this->logger, ERROR|LEVEL1,
591 "INTEGRITY_ALGORITHM %s not supported!",
592 mapping_find(integrity_algorithm_m,algo->algorithm));
593 return FAILED;
594 }
595 key_size = this->signer_initiator->get_key_size(this->signer_initiator);
596
597 prf_plus->allocate_bytes(prf_plus, key_size, &key);
598 this->logger->log_chunk(this->logger, PRIVATE, "Sk_ai secret", key);
599 this->signer_initiator->set_key(this->signer_initiator, key);
600 allocator_free_chunk(&key);
601
602 prf_plus->allocate_bytes(prf_plus, key_size, &key);
603 this->logger->log_chunk(this->logger, PRIVATE, "Sk_ar secret", key);
604 this->signer_responder->set_key(this->signer_responder, key);
605 allocator_free_chunk(&key);
606
607
608 /* SK_ei/SK_er used for encryption */
609 proposal->get_algorithm(proposal, IKE, ENCRYPTION_ALGORITHM, &algo);
610 if (algo == NULL)
611 {
612 this->logger->log(this->logger, ERROR|LEVEL2, "No encryption algoithm selected!?");
613 return FAILED;
614 }
615 if (this->crypter_initiator != NULL)
616 {
617 this->crypter_initiator->destroy(this->crypter_initiator);
618 }
619 if (this->crypter_responder != NULL)
620 {
621 this->crypter_responder->destroy(this->crypter_responder);
622 }
623
624 this->crypter_initiator = crypter_create(algo->algorithm, algo->key_size);
625 this->crypter_responder = crypter_create(algo->algorithm, algo->key_size);
626 if (this->crypter_initiator == NULL || this->crypter_responder == NULL)
627 {
628 this->logger->log(this->logger, ERROR|LEVEL1,
629 "ENCRYPTION_ALGORITHM %s (key size %d) not supported!",
630 mapping_find(encryption_algorithm_m, algo->algorithm),
631 algo->key_size);
632 return FAILED;
633 }
634 key_size = this->crypter_initiator->get_key_size(this->crypter_initiator);
635
636 prf_plus->allocate_bytes(prf_plus, key_size, &key);
637 this->logger->log_chunk(this->logger, PRIVATE, "Sk_ei secret", key);
638 this->crypter_initiator->set_key(this->crypter_initiator, key);
639 allocator_free_chunk(&key);
640
641 prf_plus->allocate_bytes(prf_plus, key_size, &key);
642 this->logger->log_chunk(this->logger, PRIVATE, "Sk_er secret", key);
643 this->crypter_responder->set_key(this->crypter_responder, key);
644 allocator_free_chunk(&key);
645
646 /* SK_pi/SK_pr used for authentication */
647 proposal->get_algorithm(proposal, IKE, PSEUDO_RANDOM_FUNCTION, &algo);
648 if (this->prf_auth_i != NULL)
649 {
650 this->prf_auth_i->destroy(this->prf_auth_i);
651 }
652 if (this->prf_auth_r != NULL)
653 {
654 this->prf_auth_r->destroy(this->prf_auth_r);
655 }
656
657 this->prf_auth_i = prf_create(algo->algorithm);
658 this->prf_auth_r = prf_create(algo->algorithm);
659
660 key_size = this->prf_auth_i->get_key_size(this->prf_auth_i);
661 prf_plus->allocate_bytes(prf_plus, key_size, &key);
662 this->logger->log_chunk(this->logger, PRIVATE, "Sk_pi secret", key);
663 this->prf_auth_i->set_key(this->prf_auth_i, key);
664 allocator_free_chunk(&key);
665
666 prf_plus->allocate_bytes(prf_plus, key_size, &key);
667 this->logger->log_chunk(this->logger, PRIVATE, "Sk_pr secret", key);
668 this->prf_auth_r->set_key(this->prf_auth_r, key);
669 allocator_free_chunk(&key);
670
671 /* all done, prf_plus not needed anymore */
672 prf_plus->destroy(prf_plus);
673
674 return SUCCESS;
675 }
676
677 /**
678 * Implementation of protected_ike_sa_t.get_randomizer.
679 */
680 static randomizer_t *get_randomizer (private_ike_sa_t *this)
681 {
682 return this->randomizer;
683 }
684
685 /**
686 * Implementation of protected_ike_sa_t.get_crypter_initiator.
687 */
688 static crypter_t *get_crypter_initiator (private_ike_sa_t *this)
689 {
690 return this->crypter_initiator;
691 }
692
693 /**
694 * Implementation of protected_ike_sa_t.get_signer_initiator.
695 */
696 static signer_t *get_signer_initiator (private_ike_sa_t *this)
697 {
698 return this->signer_initiator;
699 }
700
701 /**
702 * Implementation of protected_ike_sa_t.get_crypter_responder.
703 */
704 static crypter_t *get_crypter_responder(private_ike_sa_t *this)
705 {
706 return this->crypter_responder;
707 }
708
709 /**
710 * Implementation of protected_ike_sa_t.get_signer_responder.
711 */
712 static signer_t *get_signer_responder (private_ike_sa_t *this)
713 {
714 return this->signer_responder;
715 }
716
717 /**
718 * Implementation of protected_ike_sa_t.send_request.
719 */
720 static status_t send_request (private_ike_sa_t *this,message_t * message)
721 {
722 retransmit_request_job_t *retransmit_job;
723 u_int32_t timeout;
724 crypter_t *crypter;
725 signer_t *signer;
726 packet_t *packet;
727 status_t status;
728
729 if (message->get_message_id(message) != this->message_id_out)
730 {
731 this->logger->log(this->logger, ERROR, "Message could not be sent cause id (%d) was not as expected (%d)",
732 message->get_message_id(message),this->message_id_out);
733 return FAILED;
734 }
735
736 /* generate packet */
737 this->logger->log(this->logger, CONTROL|LEVEL2, "Generate packet from message");
738
739 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
740 {
741 crypter = this->crypter_initiator;
742 signer = this->signer_initiator;
743 }
744 else
745 {
746 crypter = this->crypter_responder;
747 signer =this->signer_responder;
748 }
749
750 status = message->generate(message, crypter,signer, &packet);
751 if (status != SUCCESS)
752 {
753 this->logger->log(this->logger, ERROR, "Could not generate packet from message");
754 return FAILED;
755 }
756
757 this->logger->log(this->logger, CONTROL|LEVEL3,
758 "Add request packet with message id %d to global send queue",
759 this->message_id_out);
760 charon->send_queue->add(charon->send_queue, packet);
761
762 if (this->last_requested_message != NULL)
763 {
764 /* destroy message */
765 this->last_requested_message->destroy(this->last_requested_message);
766 }
767
768 this->logger->log(this->logger, CONTROL|LEVEL3, "Replace last requested message with new one");
769 this->last_requested_message = message;
770
771 retransmit_job = retransmit_request_job_create(this->message_id_out,this->ike_sa_id);
772
773 status = charon->configuration->get_retransmit_timeout (charon->configuration,
774 retransmit_job->get_retransmit_count(retransmit_job),&timeout);
775
776 if (status != SUCCESS)
777 {
778 this->logger->log(this->logger, CONTROL|LEVEL2, "No retransmit job for message created!");
779 retransmit_job->destroy(retransmit_job);
780 }
781 else
782 {
783 this->logger->log(this->logger, CONTROL|LEVEL2, "Request will be retransmitted in %d ms.",timeout);
784 charon->event_queue->add_relative(charon->event_queue,(job_t *) retransmit_job,timeout);
785 }
786
787 /* message counter can now be increased */
788 this->logger->log(this->logger, CONTROL|LEVEL3,
789 "Increase message counter for outgoing messages from %d",
790 this->message_id_out);
791 this->message_id_out++;
792 return SUCCESS;
793 }
794
795 /**
796 * Implementation of protected_ike_sa_t.send_response.
797 */
798 static status_t send_response (private_ike_sa_t *this,message_t * message)
799 {
800 crypter_t *crypter;
801 signer_t *signer;
802 packet_t *packet;
803 status_t status;
804
805 if (message->get_message_id(message) != this->message_id_in)
806 {
807 this->logger->log(this->logger, ERROR, "Message could not be sent cause id was not as expected");
808 return FAILED;
809 }
810
811
812 if (this->ike_sa_id->is_initiator(this->ike_sa_id))
813 {
814 crypter = this->crypter_initiator;
815 signer = this->signer_initiator;
816 }
817 else
818 {
819 crypter = this->crypter_responder;
820 signer =this->signer_responder;
821 }
822
823 status = message->generate(message, crypter,signer, &packet);
824 if (status != SUCCESS)
825 {
826 this->logger->log(this->logger, ERROR, "Could not generate packet from message");
827 return FAILED;
828 }
829
830 this->logger->log(this->logger, CONTROL|LEVEL3,
831 "Add response packet with message id %d to global send queue",
832 this->message_id_in);
833 charon->send_queue->add(charon->send_queue, packet);
834
835 if (this->last_responded_message != NULL)
836 {
837 /* destroy message */
838 this->last_responded_message->destroy(this->last_responded_message);
839 }
840
841 this->logger->log(this->logger, CONTROL|LEVEL3, "Replace last responded message with new one");
842 this->last_responded_message = message;
843
844 /* message counter can now be increased */
845 this->logger->log(this->logger, CONTROL|LEVEL3, "Increase message counter for incoming messages");
846 this->message_id_in++;
847
848 return SUCCESS;
849 }
850
851 /**
852 * Implementation of of private_responder_init_t.send_notify_reply.
853 */
854 static void send_notify(private_ike_sa_t *this, exchange_type_t exchange_type, notify_message_type_t type, chunk_t data)
855 {
856 notify_payload_t *payload;
857 message_t *response;
858 packet_t *packet;
859 status_t status;
860
861 this->logger->log(this->logger, CONTROL|LEVEL2, "Going to build message with notify payload");
862 /* set up the reply */
863 this->protected.build_message(&(this->protected), exchange_type, FALSE, &response);
864 payload = notify_payload_create_from_protocol_and_type(IKE,type);
865 if ((data.ptr != NULL) && (data.len > 0))
866 {
867 this->logger->log(this->logger, CONTROL|LEVEL2, "Add Data to notify payload");
868 payload->set_notification_data(payload,data);
869 }
870
871 this->logger->log(this->logger, CONTROL|LEVEL2, "Add Notify payload to message");
872 response->add_payload(response,(payload_t *) payload);
873
874 /* generate packet */
875 this->logger->log(this->logger, CONTROL|LEVEL2, "Generate packet from message");
876 status = response->generate(response, this->crypter_responder, this->signer_responder, &packet);
877 if (status != SUCCESS)
878 {
879 this->logger->log(this->logger, ERROR|LEVEL1, "Could not generate notify message");
880 response->destroy(response);
881 return;
882 }
883
884 this->logger->log(this->logger, CONTROL|LEVEL2, "Add packet to global send queue");
885 charon->send_queue->add(charon->send_queue, packet);
886 this->logger->log(this->logger, CONTROL|LEVEL2, "Destroy message");
887 response->destroy(response);
888 }
889
890 /**
891 * Implementation of protected_ike_sa_t.set_last_replied_message_id.
892 */
893 static void set_last_replied_message_id (private_ike_sa_t *this,u_int32_t message_id)
894 {
895 this->last_replied_message_id = message_id;
896 }
897
898 /**
899 * Implementation of protected_ike_sa_t.get_last_responded_message.
900 */
901 static message_t * get_last_responded_message (private_ike_sa_t *this)
902 {
903 return this->last_responded_message;
904 }
905
906 /**
907 * Implementation of protected_ike_sa_t.get_last_requested_message.
908 */
909 static message_t * get_last_requested_message (private_ike_sa_t *this)
910 {
911 return this->last_requested_message;
912 }
913
914 /**
915 * Implementation of protected_ike_sa_t.get_state.
916 */
917 static ike_sa_state_t get_state (private_ike_sa_t *this)
918 {
919 return this->current_state->get_state(this->current_state);
920 }
921
922 /**
923 * Implementation of protected_ike_sa_t.get_state.
924 */
925 static void add_child_sa (private_ike_sa_t *this, child_sa_t *child_sa)
926 {
927 this->child_sas->insert_last(this->child_sas, child_sa);
928 }
929
930 /**
931 * Implementation of protected_ike_sa_t.reset_message_buffers.
932 */
933 static void reset_message_buffers (private_ike_sa_t *this)
934 {
935 this->logger->log(this->logger, CONTROL|LEVEL2, "Reset message counters and destroy stored messages");
936 /* destroy stored requested message */
937 if (this->last_requested_message != NULL)
938 {
939 this->last_requested_message->destroy(this->last_requested_message);
940 this->last_requested_message = NULL;
941 }
942
943 /* destroy stored responded messages */
944 if (this->last_responded_message != NULL)
945 {
946 this->last_responded_message->destroy(this->last_responded_message);
947 this->last_responded_message = NULL;
948 }
949
950 this->message_id_out = 0;
951 this->message_id_in = 0;
952 this->last_replied_message_id = -1;
953 }
954
955 /**
956 * Implementation of protected_ike_sa_t.destroy.
957 */
958 static void destroy (private_ike_sa_t *this)
959 {
960 child_sa_t *child_sa;
961
962 this->logger->log(this->logger, CONTROL|LEVEL2, "Going to destroy IKE SA %llu:%llu, role %s",
963 this->ike_sa_id->get_initiator_spi(this->ike_sa_id),
964 this->ike_sa_id->get_responder_spi(this->ike_sa_id),
965 this->ike_sa_id->is_initiator(this->ike_sa_id) ? "initiator" : "responder");
966
967 /* inform other peer of delete */
968 send_delete_ike_sa_request(this);
969
970 while (this->child_sas->remove_last(this->child_sas, (void**)&child_sa) == SUCCESS)
971 {
972 child_sa->destroy(child_sa);
973 }
974 this->child_sas->destroy(this->child_sas);
975 if (this->crypter_initiator != NULL)
976 {
977 this->crypter_initiator->destroy(this->crypter_initiator);
978 }
979 if (this->crypter_responder != NULL)
980 {
981 this->crypter_responder->destroy(this->crypter_responder);
982 }
983 if (this->signer_initiator != NULL)
984 {
985 this->signer_initiator->destroy(this->signer_initiator);
986 }
987 if (this->signer_responder != NULL)
988 {
989 this->signer_responder->destroy(this->signer_responder);
990 }
991 if (this->prf != NULL)
992 {
993 this->prf->destroy(this->prf);
994 }
995 if (this->child_prf != NULL)
996 {
997 this->child_prf->destroy(this->child_prf);
998 }
999 if (this->prf_auth_i != NULL)
1000 {
1001 this->prf_auth_i->destroy(this->prf_auth_i);
1002 }
1003 if (this->prf_auth_r != NULL)
1004 {
1005 this->prf_auth_r->destroy(this->prf_auth_r);
1006 }
1007 this->ike_sa_id->destroy(this->ike_sa_id);
1008 if (this->last_requested_message != NULL)
1009 {
1010 this->last_requested_message->destroy(this->last_requested_message);
1011 }
1012 if (this->last_responded_message != NULL)
1013 {
1014 this->last_responded_message->destroy(this->last_responded_message);
1015 }
1016 this->randomizer->destroy(this->randomizer);
1017 this->current_state->destroy(this->current_state);
1018 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
1019
1020 allocator_free(this);
1021 }
1022
1023 /*
1024 * Described in header.
1025 */
1026 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
1027 {
1028 private_ike_sa_t *this = allocator_alloc_thing(private_ike_sa_t);
1029
1030 /* Public functions */
1031 this->protected.public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
1032 this->protected.public.initiate_connection = (status_t(*)(ike_sa_t*,connection_t*)) initiate_connection;
1033 this->protected.public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
1034 this->protected.public.retransmit_request = (status_t (*) (ike_sa_t *, u_int32_t)) retransmit_request;
1035 this->protected.public.get_state = (ike_sa_state_t (*) (ike_sa_t *this)) get_state;
1036 this->protected.public.send_delete_ike_sa_request = (void (*)(ike_sa_t*)) send_delete_ike_sa_request;
1037 this->protected.public.destroy = (void(*)(ike_sa_t*))destroy;
1038
1039 /* protected functions */
1040 this->protected.build_message = (void (*) (protected_ike_sa_t *, exchange_type_t , bool , message_t **)) build_message;
1041 this->protected.get_prf = (prf_t *(*) (protected_ike_sa_t *)) get_prf;
1042 this->protected.get_child_prf = (prf_t *(*) (protected_ike_sa_t *)) get_child_prf;
1043 this->protected.get_prf_auth_i = (prf_t *(*) (protected_ike_sa_t *)) get_prf_auth_i;
1044 this->protected.get_prf_auth_r = (prf_t *(*) (protected_ike_sa_t *)) get_prf_auth_r;
1045 this->protected.add_child_sa = (void (*) (protected_ike_sa_t*,child_sa_t*)) add_child_sa;
1046 this->protected.get_logger = (logger_t *(*) (protected_ike_sa_t *)) get_logger;
1047 this->protected.set_connection = (void (*) (protected_ike_sa_t *,connection_t *)) set_connection;
1048 this->protected.get_connection = (connection_t *(*) (protected_ike_sa_t *)) get_connection;
1049 this->protected.set_policy = (void (*) (protected_ike_sa_t *,policy_t *)) set_policy;
1050 this->protected.get_policy = (policy_t *(*) (protected_ike_sa_t *)) get_policy;
1051 this->protected.get_randomizer = (randomizer_t *(*) (protected_ike_sa_t *)) get_randomizer;
1052 this->protected.send_request = (status_t (*) (protected_ike_sa_t *,message_t *)) send_request;
1053 this->protected.send_response = (status_t (*) (protected_ike_sa_t *,message_t *)) send_response;
1054 this->protected.send_notify = (void (*)(protected_ike_sa_t*,exchange_type_t,notify_message_type_t,chunk_t)) send_notify;
1055 this->protected.build_transforms = (status_t (*) (protected_ike_sa_t *,proposal_t*,diffie_hellman_t*,chunk_t,chunk_t)) build_transforms;
1056 this->protected.set_new_state = (void (*) (protected_ike_sa_t *,state_t *)) set_new_state;
1057 this->protected.get_crypter_initiator = (crypter_t *(*) (protected_ike_sa_t *)) get_crypter_initiator;
1058 this->protected.get_signer_initiator = (signer_t *(*) (protected_ike_sa_t *)) get_signer_initiator;
1059 this->protected.get_crypter_responder = (crypter_t *(*) (protected_ike_sa_t *)) get_crypter_responder;
1060 this->protected.get_signer_responder = (signer_t *(*) (protected_ike_sa_t *)) get_signer_responder;
1061 this->protected.reset_message_buffers = (void (*) (protected_ike_sa_t *)) reset_message_buffers;
1062 this->protected.get_last_responded_message = (message_t * (*) (protected_ike_sa_t *this)) get_last_responded_message;
1063 this->protected.get_last_requested_message = (message_t * (*) (protected_ike_sa_t *this)) get_last_requested_message;
1064
1065 this->protected.set_last_replied_message_id = (void (*) (protected_ike_sa_t *,u_int32_t)) set_last_replied_message_id;
1066
1067 /* private functions */
1068 this->resend_last_reply = resend_last_reply;
1069
1070 /* initialize private fields */
1071 this->logger = charon->logger_manager->create_logger(charon->logger_manager, IKE_SA, NULL);
1072
1073 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
1074 this->child_sas = linked_list_create();
1075 this->randomizer = randomizer_create();
1076
1077 this->last_requested_message = NULL;
1078 this->last_responded_message = NULL;
1079 this->message_id_out = 0;
1080 this->message_id_in = 0;
1081 this->last_replied_message_id = -1;
1082 this->crypter_initiator = NULL;
1083 this->crypter_responder = NULL;
1084 this->signer_initiator = NULL;
1085 this->signer_responder = NULL;
1086 this->prf = NULL;
1087 this->prf_auth_i = NULL;
1088 this->prf_auth_r = NULL;
1089 this->child_prf = NULL;
1090 this->connection = NULL;
1091 this->policy = NULL;
1092
1093 /* at creation time, IKE_SA is in a initiator state */
1094 if (ike_sa_id->is_initiator(ike_sa_id))
1095 {
1096 this->logger->log(this->logger, CONTROL | LEVEL2, "Create first state_t object of type INITIATOR_INIT");
1097 this->current_state = (state_t *) initiator_init_create(&(this->protected));
1098 }
1099 else
1100 {
1101 this->logger->log(this->logger, CONTROL | LEVEL2, "Create first state_t object of type RESPONDER_INIT");
1102 this->current_state = (state_t *) responder_init_create(&(this->protected));
1103 }
1104 return &(this->protected.public);
1105 }