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