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