ike-init: Make DH group reuse optional to test INVALID_KE_PAYLOAD handling
[strongswan.git] / src / libcharon / sa / ikev2 / tasks / ike_init.c
1 /*
2 * Copyright (C) 2008-2018 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * HSR 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 we are retrying after an INVALID_KE_PAYLOAD we already have one */
535 if (!this->dh)
536 {
537 if (this->old_sa && lib->settings->get_bool(lib->settings,
538 "%s.prefer_previous_dh_group", TRUE, lib->ns))
539 { /* reuse the DH group we used for the old IKE_SA when rekeying */
540 proposal_t *proposal;
541 uint16_t dh_group;
542
543 proposal = this->old_sa->get_proposal(this->old_sa);
544 if (proposal->get_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
545 &dh_group, NULL))
546 {
547 this->dh_group = dh_group;
548 }
549 else
550 { /* this shouldn't happen, but let's be safe */
551 this->dh_group = this->config->get_dh_group(this->config);
552 }
553 }
554 else
555 {
556 this->dh_group = this->config->get_dh_group(this->config);
557 }
558 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
559 this->dh_group);
560 if (!this->dh)
561 {
562 DBG1(DBG_IKE, "configured DH group %N not supported",
563 diffie_hellman_group_names, this->dh_group);
564 return FAILED;
565 }
566 }
567 else if (this->dh->get_dh_group(this->dh) != this->dh_group)
568 { /* reset DH instance if group changed (INVALID_KE_PAYLOAD) */
569 this->dh->destroy(this->dh);
570 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
571 this->dh_group);
572 if (!this->dh)
573 {
574 DBG1(DBG_IKE, "requested DH group %N not supported",
575 diffie_hellman_group_names, this->dh_group);
576 return FAILED;
577 }
578 }
579
580 /* generate nonce only when we are trying the first time */
581 if (this->my_nonce.ptr == NULL)
582 {
583 if (!generate_nonce(this))
584 {
585 return FAILED;
586 }
587 }
588
589 if (this->cookie.ptr)
590 {
591 message->add_notify(message, FALSE, COOKIE, this->cookie);
592 }
593
594 if (!build_payloads(this, message))
595 {
596 return FAILED;
597 }
598
599 #ifdef ME
600 {
601 chunk_t connect_id = this->ike_sa->get_connect_id(this->ike_sa);
602 if (connect_id.ptr)
603 {
604 message->add_notify(message, FALSE, ME_CONNECTID, connect_id);
605 }
606 }
607 #endif /* ME */
608
609 return NEED_MORE;
610 }
611
612 METHOD(task_t, process_r, status_t,
613 private_ike_init_t *this, message_t *message)
614 {
615 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
616 DBG0(DBG_IKE, "%H is initiating an IKE_SA", message->get_source(message));
617 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
618
619 if (!generate_nonce(this))
620 {
621 return FAILED;
622 }
623
624 #ifdef ME
625 {
626 notify_payload_t *notify = message->get_notify(message, ME_CONNECTID);
627 if (notify)
628 {
629 chunk_t connect_id = notify->get_notification_data(notify);
630 DBG2(DBG_IKE, "received ME_CONNECTID %#B", &connect_id);
631 charon->connect_manager->stop_checks(charon->connect_manager,
632 connect_id);
633 }
634 }
635 #endif /* ME */
636
637 process_payloads(this, message);
638
639 return NEED_MORE;
640 }
641
642 /**
643 * Derive the keymat for the IKE_SA
644 */
645 static bool derive_keys(private_ike_init_t *this,
646 chunk_t nonce_i, chunk_t nonce_r)
647 {
648 keymat_v2_t *old_keymat;
649 pseudo_random_function_t prf_alg = PRF_UNDEFINED;
650 chunk_t skd = chunk_empty;
651 ike_sa_id_t *id;
652
653 id = this->ike_sa->get_id(this->ike_sa);
654 if (this->old_sa)
655 {
656 /* rekeying: Include old SKd, use old PRF, apply SPI */
657 old_keymat = (keymat_v2_t*)this->old_sa->get_keymat(this->old_sa);
658 prf_alg = old_keymat->get_skd(old_keymat, &skd);
659 if (this->initiator)
660 {
661 id->set_responder_spi(id, this->proposal->get_spi(this->proposal));
662 }
663 else
664 {
665 id->set_initiator_spi(id, this->proposal->get_spi(this->proposal));
666 }
667 }
668 if (!this->keymat->derive_ike_keys(this->keymat, this->proposal, this->dh,
669 nonce_i, nonce_r, id, prf_alg, skd))
670 {
671 return FALSE;
672 }
673 charon->bus->ike_keys(charon->bus, this->ike_sa, this->dh, chunk_empty,
674 nonce_i, nonce_r, this->old_sa, NULL);
675 return TRUE;
676 }
677
678 METHOD(task_t, build_r, status_t,
679 private_ike_init_t *this, message_t *message)
680 {
681 identification_t *gateway;
682
683 /* check if we have everything we need */
684 if (this->proposal == NULL ||
685 this->other_nonce.len == 0 || this->my_nonce.len == 0)
686 {
687 DBG1(DBG_IKE, "received proposals inacceptable");
688 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
689 return FAILED;
690 }
691
692 /* check if we'd have to redirect the client */
693 if (!this->old_sa &&
694 this->ike_sa->supports_extension(this->ike_sa, EXT_IKE_REDIRECTION) &&
695 charon->redirect->redirect_on_init(charon->redirect, this->ike_sa,
696 &gateway))
697 {
698 chunk_t data;
699
700 DBG1(DBG_IKE, "redirecting peer to %Y", gateway);
701 data = redirect_data_create(gateway, this->other_nonce);
702 message->add_notify(message, TRUE, REDIRECT, data);
703 gateway->destroy(gateway);
704 chunk_free(&data);
705 return FAILED;
706 }
707
708 if (this->dh == NULL ||
709 !this->proposal->has_dh_group(this->proposal, this->dh_group))
710 {
711 uint16_t group;
712
713 if (this->proposal->get_algorithm(this->proposal, DIFFIE_HELLMAN_GROUP,
714 &group, NULL))
715 {
716 DBG1(DBG_IKE, "DH group %N inacceptable, requesting %N",
717 diffie_hellman_group_names, this->dh_group,
718 diffie_hellman_group_names, group);
719 this->dh_group = group;
720 group = htons(group);
721 message->add_notify(message, FALSE, INVALID_KE_PAYLOAD,
722 chunk_from_thing(group));
723 }
724 else
725 {
726 DBG1(DBG_IKE, "no acceptable proposal found");
727 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
728 }
729 return FAILED;
730 }
731
732 if (this->dh_failed)
733 {
734 DBG1(DBG_IKE, "applying DH public value failed");
735 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
736 return FAILED;
737 }
738
739 if (!derive_keys(this, this->other_nonce, this->my_nonce))
740 {
741 DBG1(DBG_IKE, "key derivation failed");
742 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
743 return FAILED;
744 }
745 if (!build_payloads(this, message))
746 {
747 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
748 return FAILED;
749 }
750 return SUCCESS;
751 }
752
753 /**
754 * Raise alerts for received notify errors
755 */
756 static void raise_alerts(private_ike_init_t *this, notify_type_t type)
757 {
758 linked_list_t *list;
759
760 switch (type)
761 {
762 case NO_PROPOSAL_CHOSEN:
763 list = this->config->get_proposals(this->config);
764 charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE, list);
765 list->destroy_offset(list, offsetof(proposal_t, destroy));
766 break;
767 default:
768 break;
769 }
770 }
771
772 METHOD(task_t, pre_process_i, status_t,
773 private_ike_init_t *this, message_t *message)
774 {
775 enumerator_t *enumerator;
776 payload_t *payload;
777
778 /* check for erroneous notifies */
779 enumerator = message->create_payload_enumerator(message);
780 while (enumerator->enumerate(enumerator, &payload))
781 {
782 if (payload->get_type(payload) == PLV2_NOTIFY)
783 {
784 notify_payload_t *notify = (notify_payload_t*)payload;
785 notify_type_t type = notify->get_notify_type(notify);
786
787 switch (type)
788 {
789 case REDIRECT:
790 {
791 identification_t *gateway;
792 chunk_t data, nonce = chunk_empty;
793 status_t status = SUCCESS;
794
795 if (this->old_sa)
796 {
797 break;
798 }
799 data = notify->get_notification_data(notify);
800 gateway = redirect_data_parse(data, &nonce);
801 if (!gateway || !chunk_equals(nonce, this->my_nonce))
802 {
803 DBG1(DBG_IKE, "received invalid REDIRECT notify");
804 status = FAILED;
805 }
806 DESTROY_IF(gateway);
807 chunk_free(&nonce);
808 enumerator->destroy(enumerator);
809 return status;
810 }
811 default:
812 break;
813 }
814 }
815 }
816 enumerator->destroy(enumerator);
817 return SUCCESS;
818 }
819
820 METHOD(task_t, process_i, status_t,
821 private_ike_init_t *this, message_t *message)
822 {
823 enumerator_t *enumerator;
824 payload_t *payload;
825
826 /* check for erroneous notifies */
827 enumerator = message->create_payload_enumerator(message);
828 while (enumerator->enumerate(enumerator, &payload))
829 {
830 if (payload->get_type(payload) == PLV2_NOTIFY)
831 {
832 notify_payload_t *notify = (notify_payload_t*)payload;
833 notify_type_t type = notify->get_notify_type(notify);
834
835 switch (type)
836 {
837 case INVALID_KE_PAYLOAD:
838 {
839 chunk_t data;
840 diffie_hellman_group_t bad_group;
841
842 bad_group = this->dh_group;
843 data = notify->get_notification_data(notify);
844 this->dh_group = ntohs(*((uint16_t*)data.ptr));
845 DBG1(DBG_IKE, "peer didn't accept DH group %N, "
846 "it requested %N", diffie_hellman_group_names,
847 bad_group, diffie_hellman_group_names, this->dh_group);
848
849 if (this->old_sa == NULL)
850 { /* reset the IKE_SA if we are not rekeying */
851 this->ike_sa->reset(this->ike_sa, FALSE);
852 }
853
854 enumerator->destroy(enumerator);
855 this->retry++;
856 return NEED_MORE;
857 }
858 case NAT_DETECTION_SOURCE_IP:
859 case NAT_DETECTION_DESTINATION_IP:
860 /* skip, handled in ike_natd_t */
861 break;
862 case MULTIPLE_AUTH_SUPPORTED:
863 /* handled in ike_auth_t */
864 break;
865 case COOKIE:
866 {
867 chunk_free(&this->cookie);
868 this->cookie = chunk_clone(notify->get_notification_data(notify));
869 this->ike_sa->reset(this->ike_sa, FALSE);
870 enumerator->destroy(enumerator);
871 DBG2(DBG_IKE, "received %N notify", notify_type_names, type);
872 this->retry++;
873 return NEED_MORE;
874 }
875 case REDIRECT:
876 {
877 identification_t *gateway;
878 chunk_t data, nonce = chunk_empty;
879 status_t status = FAILED;
880
881 if (this->old_sa)
882 {
883 DBG1(DBG_IKE, "received REDIRECT notify during rekeying"
884 ", ignored");
885 break;
886 }
887 data = notify->get_notification_data(notify);
888 gateway = redirect_data_parse(data, &nonce);
889 if (this->ike_sa->handle_redirect(this->ike_sa, gateway))
890 {
891 status = NEED_MORE;
892 }
893 DESTROY_IF(gateway);
894 chunk_free(&nonce);
895 enumerator->destroy(enumerator);
896 return status;
897 }
898 default:
899 {
900 if (type <= 16383)
901 {
902 DBG1(DBG_IKE, "received %N notify error",
903 notify_type_names, type);
904 enumerator->destroy(enumerator);
905 raise_alerts(this, type);
906 return FAILED;
907 }
908 DBG2(DBG_IKE, "received %N notify",
909 notify_type_names, type);
910 break;
911 }
912 }
913 }
914 }
915 enumerator->destroy(enumerator);
916
917 process_payloads(this, message);
918
919 /* check if we have everything */
920 if (this->proposal == NULL ||
921 this->other_nonce.len == 0 || this->my_nonce.len == 0)
922 {
923 DBG1(DBG_IKE, "peers proposal selection invalid");
924 return FAILED;
925 }
926
927 if (this->dh == NULL ||
928 !this->proposal->has_dh_group(this->proposal, this->dh_group))
929 {
930 DBG1(DBG_IKE, "peer DH group selection invalid");
931 return FAILED;
932 }
933
934 if (this->dh_failed)
935 {
936 DBG1(DBG_IKE, "applying DH public value failed");
937 return FAILED;
938 }
939
940 if (!derive_keys(this, this->my_nonce, this->other_nonce))
941 {
942 DBG1(DBG_IKE, "key derivation failed");
943 return FAILED;
944 }
945 return SUCCESS;
946 }
947
948 METHOD(task_t, get_type, task_type_t,
949 private_ike_init_t *this)
950 {
951 return TASK_IKE_INIT;
952 }
953
954 METHOD(task_t, migrate, void,
955 private_ike_init_t *this, ike_sa_t *ike_sa)
956 {
957 DESTROY_IF(this->proposal);
958 chunk_free(&this->other_nonce);
959
960 this->ike_sa = ike_sa;
961 this->keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa);
962 this->proposal = NULL;
963 this->dh_failed = FALSE;
964 }
965
966 METHOD(task_t, destroy, void,
967 private_ike_init_t *this)
968 {
969 DESTROY_IF(this->dh);
970 DESTROY_IF(this->proposal);
971 DESTROY_IF(this->nonceg);
972 chunk_free(&this->my_nonce);
973 chunk_free(&this->other_nonce);
974 chunk_free(&this->cookie);
975 free(this);
976 }
977
978 METHOD(ike_init_t, get_lower_nonce, chunk_t,
979 private_ike_init_t *this)
980 {
981 if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr,
982 min(this->my_nonce.len, this->other_nonce.len)) < 0)
983 {
984 return this->my_nonce;
985 }
986 else
987 {
988 return this->other_nonce;
989 }
990 }
991
992 /*
993 * Described in header.
994 */
995 ike_init_t *ike_init_create(ike_sa_t *ike_sa, bool initiator, ike_sa_t *old_sa)
996 {
997 private_ike_init_t *this;
998
999 INIT(this,
1000 .public = {
1001 .task = {
1002 .get_type = _get_type,
1003 .migrate = _migrate,
1004 .destroy = _destroy,
1005 },
1006 .get_lower_nonce = _get_lower_nonce,
1007 },
1008 .ike_sa = ike_sa,
1009 .initiator = initiator,
1010 .dh_group = MODP_NONE,
1011 .keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa),
1012 .old_sa = old_sa,
1013 .signature_authentication = lib->settings->get_bool(lib->settings,
1014 "%s.signature_authentication", TRUE, lib->ns),
1015 .follow_redirects = lib->settings->get_bool(lib->settings,
1016 "%s.follow_redirects", TRUE, lib->ns),
1017 );
1018 this->nonceg = this->keymat->keymat.create_nonce_gen(&this->keymat->keymat);
1019
1020 if (initiator)
1021 {
1022 this->public.task.build = _build_i;
1023 this->public.task.process = _process_i;
1024 this->public.task.pre_process = _pre_process_i;
1025 }
1026 else
1027 {
1028 this->public.task.build = _build_r;
1029 this->public.task.process = _process_r;
1030 }
1031 return &this->public;
1032 }