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