635c32c0d2ee799d039f9565891291ddab452a16
[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 signature_params_t *config;
162 int written;
163 size_t len = BUF_LEN;
164 char buf[len];
165 char *pos = buf;
166 char *plugin_name;
167
168 algos = hash_algorithm_set_create();
169 peer = this->ike_sa->get_peer_cfg(this->ike_sa);
170 if (peer)
171 {
172 rounds = peer->create_auth_cfg_enumerator(peer, FALSE);
173 while (rounds->enumerate(rounds, &auth))
174 {
175 enumerator = auth->create_enumerator(auth);
176 while (enumerator->enumerate(enumerator, &rule, &config))
177 {
178 if (rule == AUTH_RULE_IKE_SIGNATURE_SCHEME)
179 {
180 hash = hasher_from_signature_scheme(config->scheme,
181 config->params);
182 if (hasher_algorithm_for_ikev2(hash))
183 {
184 algos->add(algos, hash);
185 }
186 }
187 }
188 enumerator->destroy(enumerator);
189 }
190 rounds->destroy(rounds);
191 }
192
193 if (!algos->count(algos))
194 {
195 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
196 while (enumerator->enumerate(enumerator, &hash, &plugin_name))
197 {
198 if (hasher_algorithm_for_ikev2(hash))
199 {
200 algos->add(algos, hash);
201 }
202 }
203 enumerator->destroy(enumerator);
204 }
205
206 if (algos->count(algos))
207 {
208 writer = bio_writer_create(0);
209 enumerator = algos->create_enumerator(algos);
210 while (enumerator->enumerate(enumerator, &hash))
211 {
212 writer->write_uint16(writer, hash);
213
214 /* generate debug output */
215 written = snprintf(pos, len, " %N", hash_algorithm_short_names,
216 hash);
217 if (written > 0 && written < len)
218 {
219 pos += written;
220 len -= written;
221 }
222 }
223 enumerator->destroy(enumerator);
224 message->add_notify(message, FALSE, SIGNATURE_HASH_ALGORITHMS,
225 writer->get_buf(writer));
226 writer->destroy(writer);
227
228 *pos = '\0';
229 DBG2(DBG_CFG, "sending supported signature hash algorithms:%s", buf);
230 }
231 algos->destroy(algos);
232 }
233
234 /**
235 * Store algorithms supported by other peer
236 */
237 static void handle_supported_hash_algorithms(private_ike_init_t *this,
238 notify_payload_t *notify)
239 {
240 bio_reader_t *reader;
241 uint16_t algo;
242 int written;
243 size_t len = BUF_LEN;
244 char buf[len];
245 char *pos = buf;
246 bool added = FALSE;
247
248 reader = bio_reader_create(notify->get_notification_data(notify));
249 while (reader->remaining(reader) >= 2 && reader->read_uint16(reader, &algo))
250 {
251 if (hasher_algorithm_for_ikev2(algo))
252 {
253 this->keymat->add_hash_algorithm(this->keymat, algo);
254 added = TRUE;
255
256 /* generate debug output */
257 written = snprintf(pos, len, " %N", hash_algorithm_short_names,
258 algo);
259 if (written > 0 && written < len)
260 {
261 pos += written;
262 len -= written;
263 }
264 }
265 }
266 reader->destroy(reader);
267
268 *pos = '\0';
269 DBG2(DBG_CFG, "received supported signature hash algorithms:%s", buf);
270
271 if (added)
272 {
273 this->ike_sa->enable_extension(this->ike_sa, EXT_SIGNATURE_AUTH);
274 }
275 }
276
277 /**
278 * build the payloads for the message
279 */
280 static bool build_payloads(private_ike_init_t *this, message_t *message)
281 {
282 sa_payload_t *sa_payload;
283 ke_payload_t *ke_payload;
284 nonce_payload_t *nonce_payload;
285 linked_list_t *proposal_list;
286 ike_sa_id_t *id;
287 proposal_t *proposal;
288 enumerator_t *enumerator;
289
290 id = this->ike_sa->get_id(this->ike_sa);
291
292 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
293
294 if (this->initiator)
295 {
296 proposal_list = this->config->get_proposals(this->config);
297 if (this->old_sa)
298 {
299 /* include SPI of new IKE_SA when we are rekeying */
300 enumerator = proposal_list->create_enumerator(proposal_list);
301 while (enumerator->enumerate(enumerator, (void**)&proposal))
302 {
303 proposal->set_spi(proposal, id->get_initiator_spi(id));
304 }
305 enumerator->destroy(enumerator);
306 }
307
308 sa_payload = sa_payload_create_from_proposals_v2(proposal_list);
309 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
310 }
311 else
312 {
313 if (this->old_sa)
314 {
315 /* include SPI of new IKE_SA when we are rekeying */
316 this->proposal->set_spi(this->proposal, id->get_responder_spi(id));
317 }
318 sa_payload = sa_payload_create_from_proposal_v2(this->proposal);
319 }
320 message->add_payload(message, (payload_t*)sa_payload);
321
322 nonce_payload = nonce_payload_create(PLV2_NONCE);
323 nonce_payload->set_nonce(nonce_payload, this->my_nonce);
324 ke_payload = ke_payload_create_from_diffie_hellman(PLV2_KEY_EXCHANGE,
325 this->dh);
326 if (!ke_payload)
327 {
328 DBG1(DBG_IKE, "creating KE payload failed");
329 return FALSE;
330 }
331
332 if (this->old_sa)
333 { /* payload order differs if we are rekeying */
334 message->add_payload(message, (payload_t*)nonce_payload);
335 message->add_payload(message, (payload_t*)ke_payload);
336 }
337 else
338 {
339 message->add_payload(message, (payload_t*)ke_payload);
340 message->add_payload(message, (payload_t*)nonce_payload);
341 }
342
343 /* negotiate fragmentation if we are not rekeying */
344 if (!this->old_sa &&
345 this->config->fragmentation(this->config) != FRAGMENTATION_NO)
346 {
347 if (this->initiator ||
348 this->ike_sa->supports_extension(this->ike_sa,
349 EXT_IKE_FRAGMENTATION))
350 {
351 message->add_notify(message, FALSE, FRAGMENTATION_SUPPORTED,
352 chunk_empty);
353 }
354 }
355 /* submit supported hash algorithms for signature authentication */
356 if (!this->old_sa && this->signature_authentication)
357 {
358 if (this->initiator ||
359 this->ike_sa->supports_extension(this->ike_sa,
360 EXT_SIGNATURE_AUTH))
361 {
362 send_supported_hash_algorithms(this, message);
363 }
364 }
365 /* notify other peer if we support redirection */
366 if (!this->old_sa && this->initiator && this->follow_redirects)
367 {
368 identification_t *gateway;
369 host_t *from;
370 chunk_t data;
371
372 from = this->ike_sa->get_redirected_from(this->ike_sa);
373 if (from)
374 {
375 gateway = identification_create_from_sockaddr(
376 from->get_sockaddr(from));
377 data = redirect_data_create(gateway, chunk_empty);
378 message->add_notify(message, FALSE, REDIRECTED_FROM, data);
379 chunk_free(&data);
380 gateway->destroy(gateway);
381 }
382 else
383 {
384 message->add_notify(message, FALSE, REDIRECT_SUPPORTED,
385 chunk_empty);
386 }
387 }
388 return TRUE;
389 }
390
391 /**
392 * Read payloads from message
393 */
394 static void process_payloads(private_ike_init_t *this, message_t *message)
395 {
396 enumerator_t *enumerator;
397 payload_t *payload;
398 ke_payload_t *ke_payload = NULL;
399
400 enumerator = message->create_payload_enumerator(message);
401 while (enumerator->enumerate(enumerator, &payload))
402 {
403 switch (payload->get_type(payload))
404 {
405 case PLV2_SECURITY_ASSOCIATION:
406 {
407 sa_payload_t *sa_payload = (sa_payload_t*)payload;
408 linked_list_t *proposal_list;
409 bool private, prefer_configured;
410
411 proposal_list = sa_payload->get_proposals(sa_payload);
412 private = this->ike_sa->supports_extension(this->ike_sa,
413 EXT_STRONGSWAN);
414 prefer_configured = lib->settings->get_bool(lib->settings,
415 "%s.prefer_configured_proposals", TRUE, lib->ns);
416 this->proposal = this->config->select_proposal(this->config,
417 proposal_list, private, prefer_configured);
418 if (!this->proposal)
419 {
420 charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE,
421 proposal_list);
422 }
423 proposal_list->destroy_offset(proposal_list,
424 offsetof(proposal_t, destroy));
425 break;
426 }
427 case PLV2_KEY_EXCHANGE:
428 {
429 ke_payload = (ke_payload_t*)payload;
430
431 this->dh_group = ke_payload->get_dh_group_number(ke_payload);
432 break;
433 }
434 case PLV2_NONCE:
435 {
436 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
437
438 this->other_nonce = nonce_payload->get_nonce(nonce_payload);
439 break;
440 }
441 case PLV2_NOTIFY:
442 {
443 notify_payload_t *notify = (notify_payload_t*)payload;
444
445 switch (notify->get_notify_type(notify))
446 {
447 case FRAGMENTATION_SUPPORTED:
448 this->ike_sa->enable_extension(this->ike_sa,
449 EXT_IKE_FRAGMENTATION);
450 break;
451 case SIGNATURE_HASH_ALGORITHMS:
452 if (this->signature_authentication)
453 {
454 handle_supported_hash_algorithms(this, notify);
455 }
456 break;
457 case REDIRECTED_FROM:
458 {
459 identification_t *gateway;
460 chunk_t data;
461
462 data = notify->get_notification_data(notify);
463 gateway = redirect_data_parse(data, NULL);
464 if (!gateway)
465 {
466 DBG1(DBG_IKE, "received invalid REDIRECTED_FROM "
467 "notify, ignored");
468 break;
469 }
470 DBG1(DBG_IKE, "client got redirected from %Y", gateway);
471 gateway->destroy(gateway);
472 /* fall-through */
473 }
474 case REDIRECT_SUPPORTED:
475 if (!this->old_sa)
476 {
477 this->ike_sa->enable_extension(this->ike_sa,
478 EXT_IKE_REDIRECTION);
479 }
480 break;
481 default:
482 /* other notifies are handled elsewhere */
483 break;
484 }
485
486 }
487 default:
488 break;
489 }
490 }
491 enumerator->destroy(enumerator);
492
493 if (this->proposal)
494 {
495 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
496 }
497
498 if (ke_payload && this->proposal &&
499 this->proposal->has_dh_group(this->proposal, this->dh_group))
500 {
501 if (!this->initiator)
502 {
503 this->dh = this->keymat->keymat.create_dh(
504 &this->keymat->keymat, this->dh_group);
505 }
506 else if (this->dh)
507 {
508 this->dh_failed = this->dh->get_dh_group(this->dh) != this->dh_group;
509 }
510 if (this->dh && !this->dh_failed)
511 {
512 this->dh_failed = !this->dh->set_other_public_value(this->dh,
513 ke_payload->get_key_exchange_data(ke_payload));
514 }
515 }
516 }
517
518 METHOD(task_t, build_i, status_t,
519 private_ike_init_t *this, message_t *message)
520 {
521 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
522 DBG0(DBG_IKE, "initiating IKE_SA %s[%d] to %H",
523 this->ike_sa->get_name(this->ike_sa),
524 this->ike_sa->get_unique_id(this->ike_sa),
525 this->ike_sa->get_other_host(this->ike_sa));
526 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
527
528 if (this->retry >= MAX_RETRIES)
529 {
530 DBG1(DBG_IKE, "giving up after %d retries", MAX_RETRIES);
531 return FAILED;
532 }
533
534 /* if the DH group is set via use_dh_group(), we already have a DH object */
535 if (!this->dh)
536 {
537 this->dh_group = this->config->get_dh_group(this->config);
538 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
539 this->dh_group);
540 if (!this->dh)
541 {
542 DBG1(DBG_IKE, "configured DH group %N not supported",
543 diffie_hellman_group_names, this->dh_group);
544 return FAILED;
545 }
546 }
547 else if (this->dh->get_dh_group(this->dh) != this->dh_group)
548 { /* reset DH instance if group changed (INVALID_KE_PAYLOAD) */
549 this->dh->destroy(this->dh);
550 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
551 this->dh_group);
552 if (!this->dh)
553 {
554 DBG1(DBG_IKE, "requested DH group %N not supported",
555 diffie_hellman_group_names, this->dh_group);
556 return FAILED;
557 }
558 }
559
560 /* generate nonce only when we are trying the first time */
561 if (this->my_nonce.ptr == NULL)
562 {
563 if (!generate_nonce(this))
564 {
565 return FAILED;
566 }
567 }
568
569 if (this->cookie.ptr)
570 {
571 message->add_notify(message, FALSE, COOKIE, this->cookie);
572 }
573
574 if (!build_payloads(this, message))
575 {
576 return FAILED;
577 }
578
579 #ifdef ME
580 {
581 chunk_t connect_id = this->ike_sa->get_connect_id(this->ike_sa);
582 if (connect_id.ptr)
583 {
584 message->add_notify(message, FALSE, ME_CONNECTID, connect_id);
585 }
586 }
587 #endif /* ME */
588
589 return NEED_MORE;
590 }
591
592 METHOD(task_t, process_r, status_t,
593 private_ike_init_t *this, message_t *message)
594 {
595 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
596 DBG0(DBG_IKE, "%H is initiating an IKE_SA", message->get_source(message));
597 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
598
599 if (!generate_nonce(this))
600 {
601 return FAILED;
602 }
603
604 #ifdef ME
605 {
606 notify_payload_t *notify = message->get_notify(message, ME_CONNECTID);
607 if (notify)
608 {
609 chunk_t connect_id = notify->get_notification_data(notify);
610 DBG2(DBG_IKE, "received ME_CONNECTID %#B", &connect_id);
611 charon->connect_manager->stop_checks(charon->connect_manager,
612 connect_id);
613 }
614 }
615 #endif /* ME */
616
617 process_payloads(this, message);
618
619 return NEED_MORE;
620 }
621
622 /**
623 * Derive the keymat for the IKE_SA
624 */
625 static bool derive_keys(private_ike_init_t *this,
626 chunk_t nonce_i, chunk_t nonce_r)
627 {
628 keymat_v2_t *old_keymat;
629 pseudo_random_function_t prf_alg = PRF_UNDEFINED;
630 chunk_t skd = chunk_empty;
631 ike_sa_id_t *id;
632
633 id = this->ike_sa->get_id(this->ike_sa);
634 if (this->old_sa)
635 {
636 /* rekeying: Include old SKd, use old PRF, apply SPI */
637 old_keymat = (keymat_v2_t*)this->old_sa->get_keymat(this->old_sa);
638 prf_alg = old_keymat->get_skd(old_keymat, &skd);
639 if (this->initiator)
640 {
641 id->set_responder_spi(id, this->proposal->get_spi(this->proposal));
642 }
643 else
644 {
645 id->set_initiator_spi(id, this->proposal->get_spi(this->proposal));
646 }
647 }
648 if (!this->keymat->derive_ike_keys(this->keymat, this->proposal, this->dh,
649 nonce_i, nonce_r, id, prf_alg, skd))
650 {
651 return FALSE;
652 }
653 charon->bus->ike_keys(charon->bus, this->ike_sa, this->dh, chunk_empty,
654 nonce_i, nonce_r, this->old_sa, NULL);
655 return TRUE;
656 }
657
658 METHOD(task_t, build_r, status_t,
659 private_ike_init_t *this, message_t *message)
660 {
661 identification_t *gateway;
662
663 /* check if we have everything we need */
664 if (this->proposal == NULL ||
665 this->other_nonce.len == 0 || this->my_nonce.len == 0)
666 {
667 DBG1(DBG_IKE, "received proposals inacceptable");
668 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
669 return FAILED;
670 }
671
672 /* check if we'd have to redirect the client */
673 if (!this->old_sa &&
674 this->ike_sa->supports_extension(this->ike_sa, EXT_IKE_REDIRECTION) &&
675 charon->redirect->redirect_on_init(charon->redirect, this->ike_sa,
676 &gateway))
677 {
678 chunk_t data;
679
680 DBG1(DBG_IKE, "redirecting peer to %Y", gateway);
681 data = redirect_data_create(gateway, this->other_nonce);
682 message->add_notify(message, TRUE, REDIRECT, data);
683 gateway->destroy(gateway);
684 chunk_free(&data);
685 return FAILED;
686 }
687
688 if (this->dh == NULL ||
689 !this->proposal->has_dh_group(this->proposal, this->dh_group))
690 {
691 uint16_t group;
692
693 if (this->proposal->get_algorithm(this->proposal, DIFFIE_HELLMAN_GROUP,
694 &group, NULL))
695 {
696 DBG1(DBG_IKE, "DH group %N inacceptable, requesting %N",
697 diffie_hellman_group_names, this->dh_group,
698 diffie_hellman_group_names, group);
699 this->dh_group = group;
700 group = htons(group);
701 message->add_notify(message, FALSE, INVALID_KE_PAYLOAD,
702 chunk_from_thing(group));
703 }
704 else
705 {
706 DBG1(DBG_IKE, "no acceptable proposal found");
707 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
708 }
709 return FAILED;
710 }
711
712 if (this->dh_failed)
713 {
714 DBG1(DBG_IKE, "applying DH public value failed");
715 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
716 return FAILED;
717 }
718
719 if (!derive_keys(this, this->other_nonce, this->my_nonce))
720 {
721 DBG1(DBG_IKE, "key derivation failed");
722 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
723 return FAILED;
724 }
725 if (!build_payloads(this, message))
726 {
727 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
728 return FAILED;
729 }
730 return SUCCESS;
731 }
732
733 /**
734 * Raise alerts for received notify errors
735 */
736 static void raise_alerts(private_ike_init_t *this, notify_type_t type)
737 {
738 linked_list_t *list;
739
740 switch (type)
741 {
742 case NO_PROPOSAL_CHOSEN:
743 list = this->config->get_proposals(this->config);
744 charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE, list);
745 list->destroy_offset(list, offsetof(proposal_t, destroy));
746 break;
747 default:
748 break;
749 }
750 }
751
752 METHOD(task_t, pre_process_i, status_t,
753 private_ike_init_t *this, message_t *message)
754 {
755 enumerator_t *enumerator;
756 payload_t *payload;
757
758 /* check for erroneous notifies */
759 enumerator = message->create_payload_enumerator(message);
760 while (enumerator->enumerate(enumerator, &payload))
761 {
762 if (payload->get_type(payload) == PLV2_NOTIFY)
763 {
764 notify_payload_t *notify = (notify_payload_t*)payload;
765 notify_type_t type = notify->get_notify_type(notify);
766
767 switch (type)
768 {
769 case REDIRECT:
770 {
771 identification_t *gateway;
772 chunk_t data, nonce = chunk_empty;
773 status_t status = SUCCESS;
774
775 if (this->old_sa)
776 {
777 break;
778 }
779 data = notify->get_notification_data(notify);
780 gateway = redirect_data_parse(data, &nonce);
781 if (!gateway || !chunk_equals(nonce, this->my_nonce))
782 {
783 DBG1(DBG_IKE, "received invalid REDIRECT notify");
784 status = FAILED;
785 }
786 DESTROY_IF(gateway);
787 chunk_free(&nonce);
788 enumerator->destroy(enumerator);
789 return status;
790 }
791 default:
792 break;
793 }
794 }
795 }
796 enumerator->destroy(enumerator);
797 return SUCCESS;
798 }
799
800 METHOD(task_t, process_i, status_t,
801 private_ike_init_t *this, message_t *message)
802 {
803 enumerator_t *enumerator;
804 payload_t *payload;
805
806 /* check for erroneous notifies */
807 enumerator = message->create_payload_enumerator(message);
808 while (enumerator->enumerate(enumerator, &payload))
809 {
810 if (payload->get_type(payload) == PLV2_NOTIFY)
811 {
812 notify_payload_t *notify = (notify_payload_t*)payload;
813 notify_type_t type = notify->get_notify_type(notify);
814
815 switch (type)
816 {
817 case INVALID_KE_PAYLOAD:
818 {
819 chunk_t data;
820 diffie_hellman_group_t bad_group;
821
822 bad_group = this->dh_group;
823 data = notify->get_notification_data(notify);
824 this->dh_group = ntohs(*((uint16_t*)data.ptr));
825 DBG1(DBG_IKE, "peer didn't accept DH group %N, "
826 "it requested %N", diffie_hellman_group_names,
827 bad_group, diffie_hellman_group_names, this->dh_group);
828
829 if (this->old_sa == NULL)
830 { /* reset the IKE_SA if we are not rekeying */
831 this->ike_sa->reset(this->ike_sa, FALSE);
832 }
833
834 enumerator->destroy(enumerator);
835 this->retry++;
836 return NEED_MORE;
837 }
838 case NAT_DETECTION_SOURCE_IP:
839 case NAT_DETECTION_DESTINATION_IP:
840 /* skip, handled in ike_natd_t */
841 break;
842 case MULTIPLE_AUTH_SUPPORTED:
843 /* handled in ike_auth_t */
844 break;
845 case COOKIE:
846 {
847 chunk_free(&this->cookie);
848 this->cookie = chunk_clone(notify->get_notification_data(notify));
849 this->ike_sa->reset(this->ike_sa, FALSE);
850 enumerator->destroy(enumerator);
851 DBG2(DBG_IKE, "received %N notify", notify_type_names, type);
852 this->retry++;
853 return NEED_MORE;
854 }
855 case REDIRECT:
856 {
857 identification_t *gateway;
858 chunk_t data, nonce = chunk_empty;
859 status_t status = FAILED;
860
861 if (this->old_sa)
862 {
863 DBG1(DBG_IKE, "received REDIRECT notify during rekeying"
864 ", ignored");
865 break;
866 }
867 data = notify->get_notification_data(notify);
868 gateway = redirect_data_parse(data, &nonce);
869 if (this->ike_sa->handle_redirect(this->ike_sa, gateway))
870 {
871 status = NEED_MORE;
872 }
873 DESTROY_IF(gateway);
874 chunk_free(&nonce);
875 enumerator->destroy(enumerator);
876 return status;
877 }
878 default:
879 {
880 if (type <= 16383)
881 {
882 DBG1(DBG_IKE, "received %N notify error",
883 notify_type_names, type);
884 enumerator->destroy(enumerator);
885 raise_alerts(this, type);
886 return FAILED;
887 }
888 DBG2(DBG_IKE, "received %N notify",
889 notify_type_names, type);
890 break;
891 }
892 }
893 }
894 }
895 enumerator->destroy(enumerator);
896
897 process_payloads(this, message);
898
899 /* check if we have everything */
900 if (this->proposal == NULL ||
901 this->other_nonce.len == 0 || this->my_nonce.len == 0)
902 {
903 DBG1(DBG_IKE, "peers proposal selection invalid");
904 return FAILED;
905 }
906
907 if (this->dh == NULL ||
908 !this->proposal->has_dh_group(this->proposal, this->dh_group))
909 {
910 DBG1(DBG_IKE, "peer DH group selection invalid");
911 return FAILED;
912 }
913
914 if (this->dh_failed)
915 {
916 DBG1(DBG_IKE, "applying DH public value failed");
917 return FAILED;
918 }
919
920 if (!derive_keys(this, this->my_nonce, this->other_nonce))
921 {
922 DBG1(DBG_IKE, "key derivation failed");
923 return FAILED;
924 }
925 return SUCCESS;
926 }
927
928 METHOD(task_t, get_type, task_type_t,
929 private_ike_init_t *this)
930 {
931 return TASK_IKE_INIT;
932 }
933
934 METHOD(task_t, migrate, void,
935 private_ike_init_t *this, ike_sa_t *ike_sa)
936 {
937 DESTROY_IF(this->proposal);
938 chunk_free(&this->other_nonce);
939
940 this->ike_sa = ike_sa;
941 this->keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa);
942 this->proposal = NULL;
943 this->dh_failed = FALSE;
944 }
945
946 METHOD(task_t, destroy, void,
947 private_ike_init_t *this)
948 {
949 DESTROY_IF(this->dh);
950 DESTROY_IF(this->proposal);
951 DESTROY_IF(this->nonceg);
952 chunk_free(&this->my_nonce);
953 chunk_free(&this->other_nonce);
954 chunk_free(&this->cookie);
955 free(this);
956 }
957
958 METHOD(ike_init_t, get_lower_nonce, chunk_t,
959 private_ike_init_t *this)
960 {
961 if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr,
962 min(this->my_nonce.len, this->other_nonce.len)) < 0)
963 {
964 return this->my_nonce;
965 }
966 else
967 {
968 return this->other_nonce;
969 }
970 }
971
972 /*
973 * Described in header.
974 */
975 ike_init_t *ike_init_create(ike_sa_t *ike_sa, bool initiator, ike_sa_t *old_sa)
976 {
977 private_ike_init_t *this;
978
979 INIT(this,
980 .public = {
981 .task = {
982 .get_type = _get_type,
983 .migrate = _migrate,
984 .destroy = _destroy,
985 },
986 .get_lower_nonce = _get_lower_nonce,
987 },
988 .ike_sa = ike_sa,
989 .initiator = initiator,
990 .dh_group = MODP_NONE,
991 .keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa),
992 .old_sa = old_sa,
993 .signature_authentication = lib->settings->get_bool(lib->settings,
994 "%s.signature_authentication", TRUE, lib->ns),
995 .follow_redirects = lib->settings->get_bool(lib->settings,
996 "%s.follow_redirects", TRUE, lib->ns),
997 );
998 this->nonceg = this->keymat->keymat.create_nonce_gen(&this->keymat->keymat);
999
1000 if (initiator)
1001 {
1002 this->public.task.build = _build_i;
1003 this->public.task.process = _process_i;
1004 this->public.task.pre_process = _pre_process_i;
1005 }
1006 else
1007 {
1008 this->public.task.build = _build_r;
1009 this->public.task.process = _process_r;
1010 }
1011 return &this->public;
1012 }