Merge branch 'proposal-checks'
[strongswan.git] / src / libcharon / sa / ikev2 / tasks / ike_init.c
1 /*
2 * Copyright (C) 2008-2015 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "ike_init.h"
19
20 #include <string.h>
21
22 #include <daemon.h>
23 #include <bio/bio_reader.h>
24 #include <bio/bio_writer.h>
25 #include <sa/ikev2/keymat_v2.h>
26 #include <crypto/diffie_hellman.h>
27 #include <crypto/hashers/hash_algorithm_set.h>
28 #include <encoding/payloads/sa_payload.h>
29 #include <encoding/payloads/ke_payload.h>
30 #include <encoding/payloads/nonce_payload.h>
31
32 /** maximum retries to do with cookies/other dh groups */
33 #define MAX_RETRIES 5
34
35 typedef struct private_ike_init_t private_ike_init_t;
36
37 /**
38 * Private members of a ike_init_t task.
39 */
40 struct private_ike_init_t {
41
42 /**
43 * Public methods and task_t interface.
44 */
45 ike_init_t public;
46
47 /**
48 * Assigned IKE_SA.
49 */
50 ike_sa_t *ike_sa;
51
52 /**
53 * Are we the initiator?
54 */
55 bool initiator;
56
57 /**
58 * IKE config to establish
59 */
60 ike_cfg_t *config;
61
62 /**
63 * diffie hellman group to use
64 */
65 diffie_hellman_group_t dh_group;
66
67 /**
68 * diffie hellman key exchange
69 */
70 diffie_hellman_t *dh;
71
72 /**
73 * Applying DH public value failed?
74 */
75 bool dh_failed;
76
77 /**
78 * Keymat derivation (from IKE_SA)
79 */
80 keymat_v2_t *keymat;
81
82 /**
83 * nonce chosen by us
84 */
85 chunk_t my_nonce;
86
87 /**
88 * nonce chosen by peer
89 */
90 chunk_t other_nonce;
91
92 /**
93 * nonce generator
94 */
95 nonce_gen_t *nonceg;
96
97 /**
98 * Negotiated proposal used for IKE_SA
99 */
100 proposal_t *proposal;
101
102 /**
103 * Old IKE_SA which gets rekeyed
104 */
105 ike_sa_t *old_sa;
106
107 /**
108 * cookie received from responder
109 */
110 chunk_t cookie;
111
112 /**
113 * retries done so far after failure (cookie or bad dh group)
114 */
115 u_int retry;
116
117 /**
118 * Whether to use Signature Authentication as per RFC 7427
119 */
120 bool signature_authentication;
121
122 /**
123 * Whether to follow IKEv2 redirects as per RFC 5685
124 */
125 bool follow_redirects;
126 };
127
128 /**
129 * Allocate our own nonce value
130 */
131 static bool generate_nonce(private_ike_init_t *this)
132 {
133 if (!this->nonceg)
134 {
135 DBG1(DBG_IKE, "no nonce generator found to create nonce");
136 return FALSE;
137 }
138 if (!this->nonceg->allocate_nonce(this->nonceg, NONCE_SIZE,
139 &this->my_nonce))
140 {
141 DBG1(DBG_IKE, "nonce allocation failed");
142 return FALSE;
143 }
144 return TRUE;
145 }
146
147 /**
148 * Notify the peer about the hash algorithms we support or expect,
149 * as per RFC 7427
150 */
151 static void send_supported_hash_algorithms(private_ike_init_t *this,
152 message_t *message)
153 {
154 hash_algorithm_set_t *algos;
155 enumerator_t *enumerator, *rounds;
156 bio_writer_t *writer;
157 hash_algorithm_t hash;
158 peer_cfg_t *peer;
159 auth_cfg_t *auth;
160 auth_rule_t rule;
161 uintptr_t config;
162 char *plugin_name;
163
164 algos = hash_algorithm_set_create();
165 peer = this->ike_sa->get_peer_cfg(this->ike_sa);
166 if (peer)
167 {
168 rounds = peer->create_auth_cfg_enumerator(peer, FALSE);
169 while (rounds->enumerate(rounds, &auth))
170 {
171 enumerator = auth->create_enumerator(auth);
172 while (enumerator->enumerate(enumerator, &rule, &config))
173 {
174 if (rule == AUTH_RULE_IKE_SIGNATURE_SCHEME)
175 {
176 hash = hasher_from_signature_scheme(config);
177 if (hasher_algorithm_for_ikev2(hash))
178 {
179 algos->add(algos, hash);
180 }
181 }
182 }
183 enumerator->destroy(enumerator);
184 }
185 rounds->destroy(rounds);
186 }
187
188 if (!algos->count(algos))
189 {
190 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
191 while (enumerator->enumerate(enumerator, &hash, &plugin_name))
192 {
193 if (hasher_algorithm_for_ikev2(hash))
194 {
195 algos->add(algos, hash);
196 }
197 }
198 enumerator->destroy(enumerator);
199 }
200
201 if (algos->count(algos))
202 {
203 writer = bio_writer_create(0);
204 enumerator = algos->create_enumerator(algos);
205 while (enumerator->enumerate(enumerator, &hash))
206 {
207 writer->write_uint16(writer, hash);
208 }
209 enumerator->destroy(enumerator);
210 message->add_notify(message, FALSE, SIGNATURE_HASH_ALGORITHMS,
211 writer->get_buf(writer));
212 writer->destroy(writer);
213 }
214 algos->destroy(algos);
215 }
216
217 /**
218 * Store algorithms supported by other peer
219 */
220 static void handle_supported_hash_algorithms(private_ike_init_t *this,
221 notify_payload_t *notify)
222 {
223 bio_reader_t *reader;
224 uint16_t algo;
225 bool added = FALSE;
226
227 reader = bio_reader_create(notify->get_notification_data(notify));
228 while (reader->remaining(reader) >= 2 && reader->read_uint16(reader, &algo))
229 {
230 if (hasher_algorithm_for_ikev2(algo))
231 {
232 this->keymat->add_hash_algorithm(this->keymat, algo);
233 added = TRUE;
234 }
235 }
236 reader->destroy(reader);
237
238 if (added)
239 {
240 this->ike_sa->enable_extension(this->ike_sa, EXT_SIGNATURE_AUTH);
241 }
242 }
243
244 /**
245 * build the payloads for the message
246 */
247 static bool build_payloads(private_ike_init_t *this, message_t *message)
248 {
249 sa_payload_t *sa_payload;
250 ke_payload_t *ke_payload;
251 nonce_payload_t *nonce_payload;
252 linked_list_t *proposal_list;
253 ike_sa_id_t *id;
254 proposal_t *proposal;
255 enumerator_t *enumerator;
256
257 id = this->ike_sa->get_id(this->ike_sa);
258
259 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
260
261 if (this->initiator)
262 {
263 proposal_list = this->config->get_proposals(this->config);
264 if (this->old_sa)
265 {
266 /* include SPI of new IKE_SA when we are rekeying */
267 enumerator = proposal_list->create_enumerator(proposal_list);
268 while (enumerator->enumerate(enumerator, (void**)&proposal))
269 {
270 proposal->set_spi(proposal, id->get_initiator_spi(id));
271 }
272 enumerator->destroy(enumerator);
273 }
274
275 sa_payload = sa_payload_create_from_proposals_v2(proposal_list);
276 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
277 }
278 else
279 {
280 if (this->old_sa)
281 {
282 /* include SPI of new IKE_SA when we are rekeying */
283 this->proposal->set_spi(this->proposal, id->get_responder_spi(id));
284 }
285 sa_payload = sa_payload_create_from_proposal_v2(this->proposal);
286 }
287 message->add_payload(message, (payload_t*)sa_payload);
288
289 nonce_payload = nonce_payload_create(PLV2_NONCE);
290 nonce_payload->set_nonce(nonce_payload, this->my_nonce);
291 ke_payload = ke_payload_create_from_diffie_hellman(PLV2_KEY_EXCHANGE,
292 this->dh);
293 if (!ke_payload)
294 {
295 DBG1(DBG_IKE, "creating KE payload failed");
296 return FALSE;
297 }
298
299 if (this->old_sa)
300 { /* payload order differs if we are rekeying */
301 message->add_payload(message, (payload_t*)nonce_payload);
302 message->add_payload(message, (payload_t*)ke_payload);
303 }
304 else
305 {
306 message->add_payload(message, (payload_t*)ke_payload);
307 message->add_payload(message, (payload_t*)nonce_payload);
308 }
309
310 /* negotiate fragmentation if we are not rekeying */
311 if (!this->old_sa &&
312 this->config->fragmentation(this->config) != FRAGMENTATION_NO)
313 {
314 if (this->initiator ||
315 this->ike_sa->supports_extension(this->ike_sa,
316 EXT_IKE_FRAGMENTATION))
317 {
318 message->add_notify(message, FALSE, FRAGMENTATION_SUPPORTED,
319 chunk_empty);
320 }
321 }
322 /* submit supported hash algorithms for signature authentication */
323 if (!this->old_sa && this->signature_authentication)
324 {
325 if (this->initiator ||
326 this->ike_sa->supports_extension(this->ike_sa,
327 EXT_SIGNATURE_AUTH))
328 {
329 send_supported_hash_algorithms(this, message);
330 }
331 }
332 /* notify other peer if we support redirection */
333 if (!this->old_sa && this->initiator && this->follow_redirects)
334 {
335 identification_t *gateway;
336 host_t *from;
337 chunk_t data;
338
339 from = this->ike_sa->get_redirected_from(this->ike_sa);
340 if (from)
341 {
342 gateway = identification_create_from_sockaddr(
343 from->get_sockaddr(from));
344 data = redirect_data_create(gateway, chunk_empty);
345 message->add_notify(message, FALSE, REDIRECTED_FROM, data);
346 chunk_free(&data);
347 gateway->destroy(gateway);
348 }
349 else
350 {
351 message->add_notify(message, FALSE, REDIRECT_SUPPORTED,
352 chunk_empty);
353 }
354 }
355 return TRUE;
356 }
357
358 /**
359 * Read payloads from message
360 */
361 static void process_payloads(private_ike_init_t *this, message_t *message)
362 {
363 enumerator_t *enumerator;
364 payload_t *payload;
365 ke_payload_t *ke_payload = NULL;
366
367 enumerator = message->create_payload_enumerator(message);
368 while (enumerator->enumerate(enumerator, &payload))
369 {
370 switch (payload->get_type(payload))
371 {
372 case PLV2_SECURITY_ASSOCIATION:
373 {
374 sa_payload_t *sa_payload = (sa_payload_t*)payload;
375 linked_list_t *proposal_list;
376 bool private, prefer_configured;
377
378 proposal_list = sa_payload->get_proposals(sa_payload);
379 private = this->ike_sa->supports_extension(this->ike_sa,
380 EXT_STRONGSWAN);
381 prefer_configured = lib->settings->get_bool(lib->settings,
382 "%s.prefer_configured_proposals", TRUE, lib->ns);
383 this->proposal = this->config->select_proposal(this->config,
384 proposal_list, private, prefer_configured);
385 if (!this->proposal)
386 {
387 charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE,
388 proposal_list);
389 }
390 proposal_list->destroy_offset(proposal_list,
391 offsetof(proposal_t, destroy));
392 break;
393 }
394 case PLV2_KEY_EXCHANGE:
395 {
396 ke_payload = (ke_payload_t*)payload;
397
398 this->dh_group = ke_payload->get_dh_group_number(ke_payload);
399 break;
400 }
401 case PLV2_NONCE:
402 {
403 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
404
405 this->other_nonce = nonce_payload->get_nonce(nonce_payload);
406 break;
407 }
408 case PLV2_NOTIFY:
409 {
410 notify_payload_t *notify = (notify_payload_t*)payload;
411
412 switch (notify->get_notify_type(notify))
413 {
414 case FRAGMENTATION_SUPPORTED:
415 this->ike_sa->enable_extension(this->ike_sa,
416 EXT_IKE_FRAGMENTATION);
417 break;
418 case SIGNATURE_HASH_ALGORITHMS:
419 if (this->signature_authentication)
420 {
421 handle_supported_hash_algorithms(this, notify);
422 }
423 break;
424 case REDIRECTED_FROM:
425 {
426 identification_t *gateway;
427 chunk_t data;
428
429 data = notify->get_notification_data(notify);
430 gateway = redirect_data_parse(data, NULL);
431 if (!gateway)
432 {
433 DBG1(DBG_IKE, "received invalid REDIRECTED_FROM "
434 "notify, ignored");
435 break;
436 }
437 DBG1(DBG_IKE, "client got redirected from %Y", gateway);
438 gateway->destroy(gateway);
439 /* fall-through */
440 }
441 case REDIRECT_SUPPORTED:
442 if (!this->old_sa)
443 {
444 this->ike_sa->enable_extension(this->ike_sa,
445 EXT_IKE_REDIRECTION);
446 }
447 break;
448 default:
449 /* other notifies are handled elsewhere */
450 break;
451 }
452
453 }
454 default:
455 break;
456 }
457 }
458 enumerator->destroy(enumerator);
459
460 if (this->proposal)
461 {
462 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
463 }
464
465 if (ke_payload && this->proposal &&
466 this->proposal->has_dh_group(this->proposal, this->dh_group))
467 {
468 if (!this->initiator)
469 {
470 this->dh = this->keymat->keymat.create_dh(
471 &this->keymat->keymat, this->dh_group);
472 }
473 if (this->dh)
474 {
475 this->dh_failed = !this->dh->set_other_public_value(this->dh,
476 ke_payload->get_key_exchange_data(ke_payload));
477 }
478 }
479 }
480
481 METHOD(task_t, build_i, status_t,
482 private_ike_init_t *this, message_t *message)
483 {
484 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
485 DBG0(DBG_IKE, "initiating IKE_SA %s[%d] to %H",
486 this->ike_sa->get_name(this->ike_sa),
487 this->ike_sa->get_unique_id(this->ike_sa),
488 this->ike_sa->get_other_host(this->ike_sa));
489 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
490
491 if (this->retry >= MAX_RETRIES)
492 {
493 DBG1(DBG_IKE, "giving up after %d retries", MAX_RETRIES);
494 return FAILED;
495 }
496
497 /* if the DH group is set via use_dh_group(), we already have a DH object */
498 if (!this->dh)
499 {
500 this->dh_group = this->config->get_dh_group(this->config);
501 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
502 this->dh_group);
503 if (!this->dh)
504 {
505 DBG1(DBG_IKE, "configured DH group %N not supported",
506 diffie_hellman_group_names, this->dh_group);
507 return FAILED;
508 }
509 }
510
511 /* generate nonce only when we are trying the first time */
512 if (this->my_nonce.ptr == NULL)
513 {
514 if (!generate_nonce(this))
515 {
516 return FAILED;
517 }
518 }
519
520 if (this->cookie.ptr)
521 {
522 message->add_notify(message, FALSE, COOKIE, this->cookie);
523 }
524
525 if (!build_payloads(this, message))
526 {
527 return FAILED;
528 }
529
530 #ifdef ME
531 {
532 chunk_t connect_id = this->ike_sa->get_connect_id(this->ike_sa);
533 if (connect_id.ptr)
534 {
535 message->add_notify(message, FALSE, ME_CONNECTID, connect_id);
536 }
537 }
538 #endif /* ME */
539
540 return NEED_MORE;
541 }
542
543 METHOD(task_t, process_r, status_t,
544 private_ike_init_t *this, message_t *message)
545 {
546 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
547 DBG0(DBG_IKE, "%H is initiating an IKE_SA", message->get_source(message));
548 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
549
550 if (!generate_nonce(this))
551 {
552 return FAILED;
553 }
554
555 #ifdef ME
556 {
557 notify_payload_t *notify = message->get_notify(message, ME_CONNECTID);
558 if (notify)
559 {
560 chunk_t connect_id = notify->get_notification_data(notify);
561 DBG2(DBG_IKE, "received ME_CONNECTID %#B", &connect_id);
562 charon->connect_manager->stop_checks(charon->connect_manager,
563 connect_id);
564 }
565 }
566 #endif /* ME */
567
568 process_payloads(this, message);
569
570 return NEED_MORE;
571 }
572
573 /**
574 * Derive the keymat for the IKE_SA
575 */
576 static bool derive_keys(private_ike_init_t *this,
577 chunk_t nonce_i, chunk_t nonce_r)
578 {
579 keymat_v2_t *old_keymat;
580 pseudo_random_function_t prf_alg = PRF_UNDEFINED;
581 chunk_t skd = chunk_empty;
582 ike_sa_id_t *id;
583
584 id = this->ike_sa->get_id(this->ike_sa);
585 if (this->old_sa)
586 {
587 /* rekeying: Include old SKd, use old PRF, apply SPI */
588 old_keymat = (keymat_v2_t*)this->old_sa->get_keymat(this->old_sa);
589 prf_alg = old_keymat->get_skd(old_keymat, &skd);
590 if (this->initiator)
591 {
592 id->set_responder_spi(id, this->proposal->get_spi(this->proposal));
593 }
594 else
595 {
596 id->set_initiator_spi(id, this->proposal->get_spi(this->proposal));
597 }
598 }
599 if (!this->keymat->derive_ike_keys(this->keymat, this->proposal, this->dh,
600 nonce_i, nonce_r, id, prf_alg, skd))
601 {
602 return FALSE;
603 }
604 charon->bus->ike_keys(charon->bus, this->ike_sa, this->dh, chunk_empty,
605 nonce_i, nonce_r, this->old_sa, NULL);
606 return TRUE;
607 }
608
609 METHOD(task_t, build_r, status_t,
610 private_ike_init_t *this, message_t *message)
611 {
612 identification_t *gateway;
613
614 /* check if we have everything we need */
615 if (this->proposal == NULL ||
616 this->other_nonce.len == 0 || this->my_nonce.len == 0)
617 {
618 DBG1(DBG_IKE, "received proposals inacceptable");
619 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
620 return FAILED;
621 }
622
623 /* check if we'd have to redirect the client */
624 if (!this->old_sa &&
625 this->ike_sa->supports_extension(this->ike_sa, EXT_IKE_REDIRECTION) &&
626 charon->redirect->redirect_on_init(charon->redirect, this->ike_sa,
627 &gateway))
628 {
629 chunk_t data;
630
631 DBG1(DBG_IKE, "redirecting peer to %Y", gateway);
632 data = redirect_data_create(gateway, this->other_nonce);
633 message->add_notify(message, TRUE, REDIRECT, data);
634 gateway->destroy(gateway);
635 chunk_free(&data);
636 return FAILED;
637 }
638
639 if (this->dh == NULL ||
640 !this->proposal->has_dh_group(this->proposal, this->dh_group))
641 {
642 uint16_t group;
643
644 if (this->proposal->get_algorithm(this->proposal, DIFFIE_HELLMAN_GROUP,
645 &group, NULL))
646 {
647 DBG1(DBG_IKE, "DH group %N inacceptable, requesting %N",
648 diffie_hellman_group_names, this->dh_group,
649 diffie_hellman_group_names, group);
650 this->dh_group = group;
651 group = htons(group);
652 message->add_notify(message, FALSE, INVALID_KE_PAYLOAD,
653 chunk_from_thing(group));
654 }
655 else
656 {
657 DBG1(DBG_IKE, "no acceptable proposal found");
658 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
659 }
660 return FAILED;
661 }
662
663 if (this->dh_failed)
664 {
665 DBG1(DBG_IKE, "applying DH public value failed");
666 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
667 return FAILED;
668 }
669
670 if (!derive_keys(this, this->other_nonce, this->my_nonce))
671 {
672 DBG1(DBG_IKE, "key derivation failed");
673 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
674 return FAILED;
675 }
676 if (!build_payloads(this, message))
677 {
678 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
679 return FAILED;
680 }
681 return SUCCESS;
682 }
683
684 /**
685 * Raise alerts for received notify errors
686 */
687 static void raise_alerts(private_ike_init_t *this, notify_type_t type)
688 {
689 linked_list_t *list;
690
691 switch (type)
692 {
693 case NO_PROPOSAL_CHOSEN:
694 list = this->config->get_proposals(this->config);
695 charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE, list);
696 list->destroy_offset(list, offsetof(proposal_t, destroy));
697 break;
698 default:
699 break;
700 }
701 }
702
703 METHOD(task_t, pre_process_i, status_t,
704 private_ike_init_t *this, message_t *message)
705 {
706 enumerator_t *enumerator;
707 payload_t *payload;
708
709 /* check for erroneous notifies */
710 enumerator = message->create_payload_enumerator(message);
711 while (enumerator->enumerate(enumerator, &payload))
712 {
713 if (payload->get_type(payload) == PLV2_NOTIFY)
714 {
715 notify_payload_t *notify = (notify_payload_t*)payload;
716 notify_type_t type = notify->get_notify_type(notify);
717
718 switch (type)
719 {
720 case REDIRECT:
721 {
722 identification_t *gateway;
723 chunk_t data, nonce = chunk_empty;
724 status_t status = SUCCESS;
725
726 if (this->old_sa)
727 {
728 break;
729 }
730 data = notify->get_notification_data(notify);
731 gateway = redirect_data_parse(data, &nonce);
732 if (!gateway || !chunk_equals(nonce, this->my_nonce))
733 {
734 DBG1(DBG_IKE, "received invalid REDIRECT notify");
735 status = FAILED;
736 }
737 DESTROY_IF(gateway);
738 chunk_free(&nonce);
739 enumerator->destroy(enumerator);
740 return status;
741 }
742 default:
743 break;
744 }
745 }
746 }
747 enumerator->destroy(enumerator);
748 return SUCCESS;
749 }
750
751 METHOD(task_t, process_i, status_t,
752 private_ike_init_t *this, message_t *message)
753 {
754 enumerator_t *enumerator;
755 payload_t *payload;
756
757 /* check for erroneous notifies */
758 enumerator = message->create_payload_enumerator(message);
759 while (enumerator->enumerate(enumerator, &payload))
760 {
761 if (payload->get_type(payload) == PLV2_NOTIFY)
762 {
763 notify_payload_t *notify = (notify_payload_t*)payload;
764 notify_type_t type = notify->get_notify_type(notify);
765
766 switch (type)
767 {
768 case INVALID_KE_PAYLOAD:
769 {
770 chunk_t data;
771 diffie_hellman_group_t bad_group;
772
773 bad_group = this->dh_group;
774 data = notify->get_notification_data(notify);
775 this->dh_group = ntohs(*((uint16_t*)data.ptr));
776 DBG1(DBG_IKE, "peer didn't accept DH group %N, "
777 "it requested %N", diffie_hellman_group_names,
778 bad_group, diffie_hellman_group_names, this->dh_group);
779
780 if (this->old_sa == NULL)
781 { /* reset the IKE_SA if we are not rekeying */
782 this->ike_sa->reset(this->ike_sa);
783 }
784
785 enumerator->destroy(enumerator);
786 this->retry++;
787 return NEED_MORE;
788 }
789 case NAT_DETECTION_SOURCE_IP:
790 case NAT_DETECTION_DESTINATION_IP:
791 /* skip, handled in ike_natd_t */
792 break;
793 case MULTIPLE_AUTH_SUPPORTED:
794 /* handled in ike_auth_t */
795 break;
796 case COOKIE:
797 {
798 chunk_free(&this->cookie);
799 this->cookie = chunk_clone(notify->get_notification_data(notify));
800 this->ike_sa->reset(this->ike_sa);
801 enumerator->destroy(enumerator);
802 DBG2(DBG_IKE, "received %N notify", notify_type_names, type);
803 this->retry++;
804 return NEED_MORE;
805 }
806 case REDIRECT:
807 {
808 identification_t *gateway;
809 chunk_t data, nonce = chunk_empty;
810 status_t status = FAILED;
811
812 if (this->old_sa)
813 {
814 DBG1(DBG_IKE, "received REDIRECT notify during rekeying"
815 ", ignored");
816 break;
817 }
818 data = notify->get_notification_data(notify);
819 gateway = redirect_data_parse(data, &nonce);
820 if (this->ike_sa->handle_redirect(this->ike_sa, gateway))
821 {
822 status = NEED_MORE;
823 }
824 DESTROY_IF(gateway);
825 chunk_free(&nonce);
826 enumerator->destroy(enumerator);
827 return status;
828 }
829 default:
830 {
831 if (type <= 16383)
832 {
833 DBG1(DBG_IKE, "received %N notify error",
834 notify_type_names, type);
835 enumerator->destroy(enumerator);
836 raise_alerts(this, type);
837 return FAILED;
838 }
839 DBG2(DBG_IKE, "received %N notify",
840 notify_type_names, type);
841 break;
842 }
843 }
844 }
845 }
846 enumerator->destroy(enumerator);
847
848 process_payloads(this, message);
849
850 /* check if we have everything */
851 if (this->proposal == NULL ||
852 this->other_nonce.len == 0 || this->my_nonce.len == 0)
853 {
854 DBG1(DBG_IKE, "peers proposal selection invalid");
855 return FAILED;
856 }
857
858 if (this->dh == NULL ||
859 !this->proposal->has_dh_group(this->proposal, this->dh_group))
860 {
861 DBG1(DBG_IKE, "peer DH group selection invalid");
862 return FAILED;
863 }
864
865 if (this->dh_failed)
866 {
867 DBG1(DBG_IKE, "applying DH public value failed");
868 return FAILED;
869 }
870
871 if (!derive_keys(this, this->my_nonce, this->other_nonce))
872 {
873 DBG1(DBG_IKE, "key derivation failed");
874 return FAILED;
875 }
876 return SUCCESS;
877 }
878
879 METHOD(task_t, get_type, task_type_t,
880 private_ike_init_t *this)
881 {
882 return TASK_IKE_INIT;
883 }
884
885 METHOD(task_t, migrate, void,
886 private_ike_init_t *this, ike_sa_t *ike_sa)
887 {
888 DESTROY_IF(this->proposal);
889 chunk_free(&this->other_nonce);
890
891 this->ike_sa = ike_sa;
892 this->keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa);
893 this->proposal = NULL;
894 this->dh_failed = FALSE;
895 if (this->dh && this->dh->get_dh_group(this->dh) != this->dh_group)
896 { /* reset DH value only if group changed (INVALID_KE_PAYLOAD) */
897 this->dh->destroy(this->dh);
898 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
899 this->dh_group);
900 }
901 }
902
903 METHOD(task_t, destroy, void,
904 private_ike_init_t *this)
905 {
906 DESTROY_IF(this->dh);
907 DESTROY_IF(this->proposal);
908 DESTROY_IF(this->nonceg);
909 chunk_free(&this->my_nonce);
910 chunk_free(&this->other_nonce);
911 chunk_free(&this->cookie);
912 free(this);
913 }
914
915 METHOD(ike_init_t, get_lower_nonce, chunk_t,
916 private_ike_init_t *this)
917 {
918 if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr,
919 min(this->my_nonce.len, this->other_nonce.len)) < 0)
920 {
921 return this->my_nonce;
922 }
923 else
924 {
925 return this->other_nonce;
926 }
927 }
928
929 /*
930 * Described in header.
931 */
932 ike_init_t *ike_init_create(ike_sa_t *ike_sa, bool initiator, ike_sa_t *old_sa)
933 {
934 private_ike_init_t *this;
935
936 INIT(this,
937 .public = {
938 .task = {
939 .get_type = _get_type,
940 .migrate = _migrate,
941 .destroy = _destroy,
942 },
943 .get_lower_nonce = _get_lower_nonce,
944 },
945 .ike_sa = ike_sa,
946 .initiator = initiator,
947 .dh_group = MODP_NONE,
948 .keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa),
949 .old_sa = old_sa,
950 .signature_authentication = lib->settings->get_bool(lib->settings,
951 "%s.signature_authentication", TRUE, lib->ns),
952 .follow_redirects = lib->settings->get_bool(lib->settings,
953 "%s.follow_redirects", TRUE, lib->ns),
954 );
955 this->nonceg = this->keymat->keymat.create_nonce_gen(&this->keymat->keymat);
956
957 if (initiator)
958 {
959 this->public.task.build = _build_i;
960 this->public.task.process = _process_i;
961 this->public.task.pre_process = _pre_process_i;
962 }
963 else
964 {
965 this->public.task.build = _build_r;
966 this->public.task.process = _process_r;
967 }
968 return &this->public;
969 }