Merge branch 'childless'
[strongswan.git] / src / libcharon / sa / ikev2 / tasks / ike_init.c
1 /*
2 * Copyright (C) 2008-2019 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 * diffie hellman group to use
59 */
60 diffie_hellman_group_t dh_group;
61
62 /**
63 * diffie hellman key exchange
64 */
65 diffie_hellman_t *dh;
66
67 /**
68 * Applying DH public value failed?
69 */
70 bool dh_failed;
71
72 /**
73 * Keymat derivation (from IKE_SA)
74 */
75 keymat_v2_t *keymat;
76
77 /**
78 * nonce chosen by us
79 */
80 chunk_t my_nonce;
81
82 /**
83 * nonce chosen by peer
84 */
85 chunk_t other_nonce;
86
87 /**
88 * nonce generator
89 */
90 nonce_gen_t *nonceg;
91
92 /**
93 * Negotiated proposal used for IKE_SA
94 */
95 proposal_t *proposal;
96
97 /**
98 * Old IKE_SA which gets rekeyed
99 */
100 ike_sa_t *old_sa;
101
102 /**
103 * cookie received from responder
104 */
105 chunk_t cookie;
106
107 /**
108 * retries done so far after failure (cookie or bad dh group)
109 */
110 u_int retry;
111
112 /**
113 * Whether to use Signature Authentication as per RFC 7427
114 */
115 bool signature_authentication;
116
117 /**
118 * Whether to follow IKEv2 redirects as per RFC 5685
119 */
120 bool follow_redirects;
121 };
122
123 /**
124 * Allocate our own nonce value
125 */
126 static bool generate_nonce(private_ike_init_t *this)
127 {
128 if (!this->nonceg)
129 {
130 DBG1(DBG_IKE, "no nonce generator found to create nonce");
131 return FALSE;
132 }
133 if (!this->nonceg->allocate_nonce(this->nonceg, NONCE_SIZE,
134 &this->my_nonce))
135 {
136 DBG1(DBG_IKE, "nonce allocation failed");
137 return FALSE;
138 }
139 return TRUE;
140 }
141
142 /**
143 * Notify the peer about the hash algorithms we support or expect,
144 * as per RFC 7427
145 */
146 static void send_supported_hash_algorithms(private_ike_init_t *this,
147 message_t *message)
148 {
149 hash_algorithm_set_t *algos;
150 enumerator_t *enumerator, *rounds;
151 bio_writer_t *writer;
152 hash_algorithm_t hash;
153 peer_cfg_t *peer;
154 auth_cfg_t *auth;
155 auth_rule_t rule;
156 signature_params_t *config;
157 int written;
158 size_t len = BUF_LEN;
159 char buf[len];
160 char *pos = buf;
161 char *plugin_name;
162
163 algos = hash_algorithm_set_create();
164 peer = this->ike_sa->get_peer_cfg(this->ike_sa);
165 if (peer)
166 {
167 rounds = peer->create_auth_cfg_enumerator(peer, FALSE);
168 while (rounds->enumerate(rounds, &auth))
169 {
170 enumerator = auth->create_enumerator(auth);
171 while (enumerator->enumerate(enumerator, &rule, &config))
172 {
173 if (rule == AUTH_RULE_IKE_SIGNATURE_SCHEME)
174 {
175 hash = hasher_from_signature_scheme(config->scheme,
176 config->params);
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 /* generate debug output */
210 written = snprintf(pos, len, " %N", hash_algorithm_short_names,
211 hash);
212 if (written > 0 && written < len)
213 {
214 pos += written;
215 len -= written;
216 }
217 }
218 enumerator->destroy(enumerator);
219 message->add_notify(message, FALSE, SIGNATURE_HASH_ALGORITHMS,
220 writer->get_buf(writer));
221 writer->destroy(writer);
222
223 *pos = '\0';
224 DBG2(DBG_CFG, "sending supported signature hash algorithms:%s", buf);
225 }
226 algos->destroy(algos);
227 }
228
229 /**
230 * Store algorithms supported by other peer
231 */
232 static void handle_supported_hash_algorithms(private_ike_init_t *this,
233 notify_payload_t *notify)
234 {
235 bio_reader_t *reader;
236 uint16_t algo;
237 int written;
238 size_t len = BUF_LEN;
239 char buf[len];
240 char *pos = buf;
241 bool added = FALSE;
242
243 reader = bio_reader_create(notify->get_notification_data(notify));
244 while (reader->remaining(reader) >= 2 && reader->read_uint16(reader, &algo))
245 {
246 if (hasher_algorithm_for_ikev2(algo))
247 {
248 this->keymat->add_hash_algorithm(this->keymat, algo);
249 added = TRUE;
250
251 /* generate debug output */
252 written = snprintf(pos, len, " %N", hash_algorithm_short_names,
253 algo);
254 if (written > 0 && written < len)
255 {
256 pos += written;
257 len -= written;
258 }
259 }
260 }
261 reader->destroy(reader);
262
263 *pos = '\0';
264 DBG2(DBG_CFG, "received supported signature hash algorithms:%s", buf);
265
266 if (added)
267 {
268 this->ike_sa->enable_extension(this->ike_sa, EXT_SIGNATURE_AUTH);
269 }
270 }
271
272 /**
273 * Check whether to send a USE_PPK notify
274 */
275 static bool send_use_ppk(private_ike_init_t *this)
276 {
277 peer_cfg_t *peer;
278 enumerator_t *keys;
279 shared_key_t *key;
280 bool use_ppk = FALSE;
281
282 if (this->initiator)
283 {
284 peer = this->ike_sa->get_peer_cfg(this->ike_sa);
285 if (peer->get_ppk_id(peer))
286 {
287 use_ppk = TRUE;
288 }
289 }
290 else if (this->ike_sa->supports_extension(this->ike_sa, EXT_PPK))
291 {
292 /* check if we have at least one PPK available */
293 keys = lib->credmgr->create_shared_enumerator(lib->credmgr, SHARED_PPK,
294 NULL, NULL);
295 if (keys->enumerate(keys, &key, NULL, NULL))
296 {
297 use_ppk = TRUE;
298 }
299 keys->destroy(keys);
300 }
301 return use_ppk;
302 }
303
304 /**
305 * build the payloads for the message
306 */
307 static bool build_payloads(private_ike_init_t *this, message_t *message)
308 {
309 sa_payload_t *sa_payload;
310 ke_payload_t *ke_payload;
311 nonce_payload_t *nonce_payload;
312 linked_list_t *proposal_list, *other_dh_groups;
313 ike_sa_id_t *id;
314 proposal_t *proposal;
315 enumerator_t *enumerator;
316 ike_cfg_t *ike_cfg;
317
318 id = this->ike_sa->get_id(this->ike_sa);
319
320 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
321
322 if (this->initiator)
323 {
324 proposal_list = ike_cfg->get_proposals(ike_cfg);
325 other_dh_groups = linked_list_create();
326 enumerator = proposal_list->create_enumerator(proposal_list);
327 while (enumerator->enumerate(enumerator, (void**)&proposal))
328 {
329 /* include SPI of new IKE_SA when we are rekeying */
330 if (this->old_sa)
331 {
332 proposal->set_spi(proposal, id->get_initiator_spi(id));
333 }
334 /* move the selected DH group to the front of the proposal */
335 if (!proposal->promote_dh_group(proposal, this->dh_group))
336 { /* the proposal does not include the group, move to the back */
337 proposal_list->remove_at(proposal_list, enumerator);
338 other_dh_groups->insert_last(other_dh_groups, proposal);
339 }
340 }
341 enumerator->destroy(enumerator);
342 /* add proposals that don't contain the selected group */
343 enumerator = other_dh_groups->create_enumerator(other_dh_groups);
344 while (enumerator->enumerate(enumerator, (void**)&proposal))
345 { /* no need to remove from the list as we destroy it anyway*/
346 proposal_list->insert_last(proposal_list, proposal);
347 }
348 enumerator->destroy(enumerator);
349 other_dh_groups->destroy(other_dh_groups);
350
351 sa_payload = sa_payload_create_from_proposals_v2(proposal_list);
352 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
353 }
354 else
355 {
356 if (this->old_sa)
357 {
358 /* include SPI of new IKE_SA when we are rekeying */
359 this->proposal->set_spi(this->proposal, id->get_responder_spi(id));
360 }
361 sa_payload = sa_payload_create_from_proposal_v2(this->proposal);
362 }
363 message->add_payload(message, (payload_t*)sa_payload);
364
365 ke_payload = ke_payload_create_from_diffie_hellman(PLV2_KEY_EXCHANGE,
366 this->dh);
367 if (!ke_payload)
368 {
369 DBG1(DBG_IKE, "creating KE payload failed");
370 return FALSE;
371 }
372 nonce_payload = nonce_payload_create(PLV2_NONCE);
373 nonce_payload->set_nonce(nonce_payload, this->my_nonce);
374
375 if (this->old_sa)
376 { /* payload order differs if we are rekeying */
377 message->add_payload(message, (payload_t*)nonce_payload);
378 message->add_payload(message, (payload_t*)ke_payload);
379 }
380 else
381 {
382 message->add_payload(message, (payload_t*)ke_payload);
383 message->add_payload(message, (payload_t*)nonce_payload);
384 }
385
386 /* negotiate fragmentation if we are not rekeying */
387 if (!this->old_sa &&
388 ike_cfg->fragmentation(ike_cfg) != FRAGMENTATION_NO)
389 {
390 if (this->initiator ||
391 this->ike_sa->supports_extension(this->ike_sa,
392 EXT_IKE_FRAGMENTATION))
393 {
394 message->add_notify(message, FALSE, FRAGMENTATION_SUPPORTED,
395 chunk_empty);
396 }
397 }
398 /* submit supported hash algorithms for signature authentication */
399 if (!this->old_sa && this->signature_authentication)
400 {
401 if (this->initiator ||
402 this->ike_sa->supports_extension(this->ike_sa,
403 EXT_SIGNATURE_AUTH))
404 {
405 send_supported_hash_algorithms(this, message);
406 }
407 }
408 /* notify other peer if we support redirection */
409 if (!this->old_sa && this->initiator && this->follow_redirects)
410 {
411 identification_t *gateway;
412 host_t *from;
413 chunk_t data;
414
415 from = this->ike_sa->get_redirected_from(this->ike_sa);
416 if (from)
417 {
418 gateway = identification_create_from_sockaddr(
419 from->get_sockaddr(from));
420 data = redirect_data_create(gateway, chunk_empty);
421 message->add_notify(message, FALSE, REDIRECTED_FROM, data);
422 chunk_free(&data);
423 gateway->destroy(gateway);
424 }
425 else
426 {
427 message->add_notify(message, FALSE, REDIRECT_SUPPORTED,
428 chunk_empty);
429 }
430 }
431 /* notify the peer if we want to use/support PPK */
432 if (!this->old_sa && send_use_ppk(this))
433 {
434 message->add_notify(message, FALSE, USE_PPK, chunk_empty);
435 }
436 /* notify the peer if we accept childless IKE_SAs */
437 if (!this->old_sa && !this->initiator &&
438 ike_cfg->childless(ike_cfg) != CHILDLESS_NEVER)
439 {
440 message->add_notify(message, FALSE, CHILDLESS_IKEV2_SUPPORTED,
441 chunk_empty);
442 }
443 return TRUE;
444 }
445
446 /**
447 * Process the SA payload and select a proposal
448 */
449 static void process_sa_payload(private_ike_init_t *this, message_t *message,
450 sa_payload_t *sa_payload)
451 {
452 ike_cfg_t *ike_cfg, *cfg, *alt_cfg = NULL;
453 enumerator_t *enumerator;
454 linked_list_t *proposal_list;
455 host_t *me, *other;
456 bool private, prefer_configured;
457
458 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
459
460 proposal_list = sa_payload->get_proposals(sa_payload);
461 private = this->ike_sa->supports_extension(this->ike_sa, EXT_STRONGSWAN);
462 prefer_configured = lib->settings->get_bool(lib->settings,
463 "%s.prefer_configured_proposals", TRUE, lib->ns);
464
465 this->proposal = ike_cfg->select_proposal(ike_cfg, proposal_list, private,
466 prefer_configured);
467 if (!this->proposal)
468 {
469 if (!this->initiator && !this->old_sa)
470 {
471 me = message->get_destination(message);
472 other = message->get_source(message);
473 enumerator = charon->backends->create_ike_cfg_enumerator(
474 charon->backends, me, other, IKEV2);
475 while (enumerator->enumerate(enumerator, &cfg))
476 {
477 if (ike_cfg == cfg)
478 { /* already tried and failed */
479 continue;
480 }
481 DBG1(DBG_IKE, "no matching proposal found, trying alternative "
482 "config");
483 this->proposal = cfg->select_proposal(cfg, proposal_list,
484 private, prefer_configured);
485 if (this->proposal)
486 {
487 alt_cfg = cfg->get_ref(cfg);
488 break;
489 }
490 }
491 enumerator->destroy(enumerator);
492 }
493 if (alt_cfg)
494 {
495 this->ike_sa->set_ike_cfg(this->ike_sa, alt_cfg);
496 alt_cfg->destroy(alt_cfg);
497 }
498 else
499 {
500 charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE,
501 proposal_list);
502 }
503 }
504 proposal_list->destroy_offset(proposal_list,
505 offsetof(proposal_t, destroy));
506 }
507
508 /**
509 * Read payloads from message
510 */
511 static void process_payloads(private_ike_init_t *this, message_t *message)
512 {
513 enumerator_t *enumerator;
514 payload_t *payload;
515 ke_payload_t *ke_payload = NULL;
516
517 enumerator = message->create_payload_enumerator(message);
518 while (enumerator->enumerate(enumerator, &payload))
519 {
520 switch (payload->get_type(payload))
521 {
522 case PLV2_SECURITY_ASSOCIATION:
523 {
524 process_sa_payload(this, message, (sa_payload_t*)payload);
525 break;
526 }
527 case PLV2_KEY_EXCHANGE:
528 {
529 ke_payload = (ke_payload_t*)payload;
530
531 this->dh_group = ke_payload->get_dh_group_number(ke_payload);
532 break;
533 }
534 case PLV2_NONCE:
535 {
536 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
537
538 this->other_nonce = nonce_payload->get_nonce(nonce_payload);
539 break;
540 }
541 case PLV2_NOTIFY:
542 {
543 notify_payload_t *notify = (notify_payload_t*)payload;
544
545 switch (notify->get_notify_type(notify))
546 {
547 case FRAGMENTATION_SUPPORTED:
548 this->ike_sa->enable_extension(this->ike_sa,
549 EXT_IKE_FRAGMENTATION);
550 break;
551 case SIGNATURE_HASH_ALGORITHMS:
552 if (this->signature_authentication)
553 {
554 handle_supported_hash_algorithms(this, notify);
555 }
556 break;
557 case USE_PPK:
558 if (!this->old_sa)
559 {
560 this->ike_sa->enable_extension(this->ike_sa,
561 EXT_PPK);
562 }
563 break;
564 case REDIRECTED_FROM:
565 {
566 identification_t *gateway;
567 chunk_t data;
568
569 data = notify->get_notification_data(notify);
570 gateway = redirect_data_parse(data, NULL);
571 if (!gateway)
572 {
573 DBG1(DBG_IKE, "received invalid REDIRECTED_FROM "
574 "notify, ignored");
575 break;
576 }
577 DBG1(DBG_IKE, "client got redirected from %Y", gateway);
578 gateway->destroy(gateway);
579 /* fall-through */
580 }
581 case REDIRECT_SUPPORTED:
582 if (!this->old_sa)
583 {
584 this->ike_sa->enable_extension(this->ike_sa,
585 EXT_IKE_REDIRECTION);
586 }
587 break;
588 case CHILDLESS_IKEV2_SUPPORTED:
589 if (this->initiator && !this->old_sa)
590 {
591 this->ike_sa->enable_extension(this->ike_sa,
592 EXT_IKE_CHILDLESS);
593 }
594 break;
595 default:
596 /* other notifies are handled elsewhere */
597 break;
598 }
599
600 }
601 default:
602 break;
603 }
604 }
605 enumerator->destroy(enumerator);
606
607 if (this->proposal)
608 {
609 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
610 }
611
612 if (ke_payload && this->proposal &&
613 this->proposal->has_dh_group(this->proposal, this->dh_group))
614 {
615 if (!this->initiator)
616 {
617 this->dh = this->keymat->keymat.create_dh(
618 &this->keymat->keymat, this->dh_group);
619 }
620 else if (this->dh)
621 {
622 this->dh_failed = this->dh->get_dh_group(this->dh) != this->dh_group;
623 }
624 if (this->dh && !this->dh_failed)
625 {
626 this->dh_failed = !this->dh->set_other_public_value(this->dh,
627 ke_payload->get_key_exchange_data(ke_payload));
628 }
629 }
630 }
631
632 METHOD(task_t, build_i, status_t,
633 private_ike_init_t *this, message_t *message)
634 {
635 ike_cfg_t *ike_cfg;
636
637 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
638
639 DBG0(DBG_IKE, "initiating IKE_SA %s[%d] to %H",
640 this->ike_sa->get_name(this->ike_sa),
641 this->ike_sa->get_unique_id(this->ike_sa),
642 this->ike_sa->get_other_host(this->ike_sa));
643 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
644
645 if (this->retry >= MAX_RETRIES)
646 {
647 DBG1(DBG_IKE, "giving up after %d retries", MAX_RETRIES);
648 return FAILED;
649 }
650
651 /* if we are retrying after an INVALID_KE_PAYLOAD we already have one */
652 if (!this->dh)
653 {
654 if (this->old_sa && lib->settings->get_bool(lib->settings,
655 "%s.prefer_previous_dh_group", TRUE, lib->ns))
656 { /* reuse the DH group we used for the old IKE_SA when rekeying */
657 proposal_t *proposal;
658 uint16_t dh_group;
659
660 proposal = this->old_sa->get_proposal(this->old_sa);
661 if (proposal->get_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
662 &dh_group, NULL))
663 {
664 this->dh_group = dh_group;
665 }
666 else
667 { /* this shouldn't happen, but let's be safe */
668 this->dh_group = ike_cfg->get_dh_group(ike_cfg);
669 }
670 }
671 else
672 {
673 this->dh_group = ike_cfg->get_dh_group(ike_cfg);
674 }
675 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
676 this->dh_group);
677 if (!this->dh)
678 {
679 DBG1(DBG_IKE, "configured DH group %N not supported",
680 diffie_hellman_group_names, this->dh_group);
681 return FAILED;
682 }
683 }
684 else if (this->dh->get_dh_group(this->dh) != this->dh_group)
685 { /* reset DH instance if group changed (INVALID_KE_PAYLOAD) */
686 this->dh->destroy(this->dh);
687 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
688 this->dh_group);
689 if (!this->dh)
690 {
691 DBG1(DBG_IKE, "requested DH group %N not supported",
692 diffie_hellman_group_names, this->dh_group);
693 return FAILED;
694 }
695 }
696
697 /* generate nonce only when we are trying the first time */
698 if (this->my_nonce.ptr == NULL)
699 {
700 if (!generate_nonce(this))
701 {
702 return FAILED;
703 }
704 }
705
706 if (this->cookie.ptr)
707 {
708 message->add_notify(message, FALSE, COOKIE, this->cookie);
709 }
710
711 if (!build_payloads(this, message))
712 {
713 return FAILED;
714 }
715
716 #ifdef ME
717 {
718 chunk_t connect_id = this->ike_sa->get_connect_id(this->ike_sa);
719 if (connect_id.ptr)
720 {
721 message->add_notify(message, FALSE, ME_CONNECTID, connect_id);
722 }
723 }
724 #endif /* ME */
725
726 return NEED_MORE;
727 }
728
729 METHOD(task_t, process_r, status_t,
730 private_ike_init_t *this, message_t *message)
731 {
732 DBG0(DBG_IKE, "%H is initiating an IKE_SA", message->get_source(message));
733 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
734
735 if (!generate_nonce(this))
736 {
737 return FAILED;
738 }
739
740 #ifdef ME
741 {
742 notify_payload_t *notify = message->get_notify(message, ME_CONNECTID);
743 if (notify)
744 {
745 chunk_t connect_id = notify->get_notification_data(notify);
746 DBG2(DBG_IKE, "received ME_CONNECTID %#B", &connect_id);
747 charon->connect_manager->stop_checks(charon->connect_manager,
748 connect_id);
749 }
750 }
751 #endif /* ME */
752
753 process_payloads(this, message);
754
755 return NEED_MORE;
756 }
757
758 /**
759 * Derive the keymat for the IKE_SA
760 */
761 static bool derive_keys(private_ike_init_t *this,
762 chunk_t nonce_i, chunk_t nonce_r)
763 {
764 keymat_v2_t *old_keymat;
765 pseudo_random_function_t prf_alg = PRF_UNDEFINED;
766 chunk_t skd = chunk_empty;
767 ike_sa_id_t *id;
768
769 id = this->ike_sa->get_id(this->ike_sa);
770 if (this->old_sa)
771 {
772 /* rekeying: Include old SKd, use old PRF, apply SPI */
773 old_keymat = (keymat_v2_t*)this->old_sa->get_keymat(this->old_sa);
774 prf_alg = old_keymat->get_skd(old_keymat, &skd);
775 if (this->initiator)
776 {
777 id->set_responder_spi(id, this->proposal->get_spi(this->proposal));
778 }
779 else
780 {
781 id->set_initiator_spi(id, this->proposal->get_spi(this->proposal));
782 }
783 }
784 if (!this->keymat->derive_ike_keys(this->keymat, this->proposal, this->dh,
785 nonce_i, nonce_r, id, prf_alg, skd))
786 {
787 return FALSE;
788 }
789 charon->bus->ike_keys(charon->bus, this->ike_sa, this->dh, chunk_empty,
790 nonce_i, nonce_r, this->old_sa, NULL, AUTH_NONE);
791 return TRUE;
792 }
793
794 METHOD(task_t, build_r, status_t,
795 private_ike_init_t *this, message_t *message)
796 {
797 identification_t *gateway;
798
799 /* check if we have everything we need */
800 if (this->proposal == NULL ||
801 this->other_nonce.len == 0 || this->my_nonce.len == 0)
802 {
803 DBG1(DBG_IKE, "received proposals unacceptable");
804 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
805 return FAILED;
806 }
807
808 /* check if we'd have to redirect the client */
809 if (!this->old_sa &&
810 this->ike_sa->supports_extension(this->ike_sa, EXT_IKE_REDIRECTION) &&
811 charon->redirect->redirect_on_init(charon->redirect, this->ike_sa,
812 &gateway))
813 {
814 chunk_t data;
815
816 DBG1(DBG_IKE, "redirecting peer to %Y", gateway);
817 data = redirect_data_create(gateway, this->other_nonce);
818 message->add_notify(message, TRUE, REDIRECT, data);
819 gateway->destroy(gateway);
820 chunk_free(&data);
821 return FAILED;
822 }
823
824 if (this->dh == NULL ||
825 !this->proposal->has_dh_group(this->proposal, this->dh_group))
826 {
827 uint16_t group;
828
829 if (this->proposal->get_algorithm(this->proposal, DIFFIE_HELLMAN_GROUP,
830 &group, NULL))
831 {
832 DBG1(DBG_IKE, "DH group %N unacceptable, requesting %N",
833 diffie_hellman_group_names, this->dh_group,
834 diffie_hellman_group_names, group);
835 this->dh_group = group;
836 group = htons(group);
837 message->add_notify(message, FALSE, INVALID_KE_PAYLOAD,
838 chunk_from_thing(group));
839 }
840 else
841 {
842 DBG1(DBG_IKE, "no acceptable proposal found");
843 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
844 }
845 return FAILED;
846 }
847
848 if (this->dh_failed)
849 {
850 DBG1(DBG_IKE, "applying DH public value failed");
851 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
852 return FAILED;
853 }
854
855 if (!derive_keys(this, this->other_nonce, this->my_nonce))
856 {
857 DBG1(DBG_IKE, "key derivation failed");
858 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
859 return FAILED;
860 }
861 if (!build_payloads(this, message))
862 {
863 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
864 return FAILED;
865 }
866 return SUCCESS;
867 }
868
869 /**
870 * Raise alerts for received notify errors
871 */
872 static void raise_alerts(private_ike_init_t *this, notify_type_t type)
873 {
874 ike_cfg_t *ike_cfg;
875 linked_list_t *list;
876
877 switch (type)
878 {
879 case NO_PROPOSAL_CHOSEN:
880 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
881 list = ike_cfg->get_proposals(ike_cfg);
882 charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE, list);
883 list->destroy_offset(list, offsetof(proposal_t, destroy));
884 break;
885 default:
886 break;
887 }
888 }
889
890 METHOD(task_t, pre_process_i, status_t,
891 private_ike_init_t *this, message_t *message)
892 {
893 enumerator_t *enumerator;
894 payload_t *payload;
895
896 /* check for erroneous notifies */
897 enumerator = message->create_payload_enumerator(message);
898 while (enumerator->enumerate(enumerator, &payload))
899 {
900 if (payload->get_type(payload) == PLV2_NOTIFY)
901 {
902 notify_payload_t *notify = (notify_payload_t*)payload;
903 notify_type_t type = notify->get_notify_type(notify);
904
905 switch (type)
906 {
907 case COOKIE:
908 {
909 chunk_t cookie;
910
911 cookie = notify->get_notification_data(notify);
912 if (chunk_equals(cookie, this->cookie))
913 {
914 DBG1(DBG_IKE, "ignore response with duplicate COOKIE "
915 "notify");
916 enumerator->destroy(enumerator);
917 return FAILED;
918 }
919 break;
920 }
921 case REDIRECT:
922 {
923 identification_t *gateway;
924 chunk_t data, nonce = chunk_empty;
925 status_t status = SUCCESS;
926
927 if (this->old_sa)
928 {
929 break;
930 }
931 data = notify->get_notification_data(notify);
932 gateway = redirect_data_parse(data, &nonce);
933 if (!gateway || !chunk_equals(nonce, this->my_nonce))
934 {
935 DBG1(DBG_IKE, "received invalid REDIRECT notify");
936 status = FAILED;
937 }
938 DESTROY_IF(gateway);
939 chunk_free(&nonce);
940 enumerator->destroy(enumerator);
941 return status;
942 }
943 default:
944 break;
945 }
946 }
947 }
948 enumerator->destroy(enumerator);
949 return SUCCESS;
950 }
951
952 METHOD(task_t, process_i, status_t,
953 private_ike_init_t *this, message_t *message)
954 {
955 enumerator_t *enumerator;
956 payload_t *payload;
957
958 /* check for erroneous notifies */
959 enumerator = message->create_payload_enumerator(message);
960 while (enumerator->enumerate(enumerator, &payload))
961 {
962 if (payload->get_type(payload) == PLV2_NOTIFY)
963 {
964 notify_payload_t *notify = (notify_payload_t*)payload;
965 notify_type_t type = notify->get_notify_type(notify);
966
967 switch (type)
968 {
969 case INVALID_KE_PAYLOAD:
970 {
971 chunk_t data;
972 diffie_hellman_group_t bad_group;
973
974 bad_group = this->dh_group;
975 data = notify->get_notification_data(notify);
976 this->dh_group = ntohs(*((uint16_t*)data.ptr));
977 DBG1(DBG_IKE, "peer didn't accept DH group %N, "
978 "it requested %N", diffie_hellman_group_names,
979 bad_group, diffie_hellman_group_names, this->dh_group);
980
981 if (this->old_sa == NULL)
982 { /* reset the IKE_SA if we are not rekeying */
983 this->ike_sa->reset(this->ike_sa, FALSE);
984 }
985
986 enumerator->destroy(enumerator);
987 this->retry++;
988 return NEED_MORE;
989 }
990 case NAT_DETECTION_SOURCE_IP:
991 case NAT_DETECTION_DESTINATION_IP:
992 /* skip, handled in ike_natd_t */
993 break;
994 case MULTIPLE_AUTH_SUPPORTED:
995 /* handled in ike_auth_t */
996 break;
997 case COOKIE:
998 {
999 chunk_free(&this->cookie);
1000 this->cookie = chunk_clone(notify->get_notification_data(notify));
1001 this->ike_sa->reset(this->ike_sa, FALSE);
1002 enumerator->destroy(enumerator);
1003 DBG2(DBG_IKE, "received %N notify", notify_type_names, type);
1004 this->retry++;
1005 return NEED_MORE;
1006 }
1007 case REDIRECT:
1008 {
1009 identification_t *gateway;
1010 chunk_t data, nonce = chunk_empty;
1011 status_t status = FAILED;
1012
1013 if (this->old_sa)
1014 {
1015 DBG1(DBG_IKE, "received REDIRECT notify during rekeying"
1016 ", ignored");
1017 break;
1018 }
1019 data = notify->get_notification_data(notify);
1020 gateway = redirect_data_parse(data, &nonce);
1021 if (this->ike_sa->handle_redirect(this->ike_sa, gateway))
1022 {
1023 status = NEED_MORE;
1024 }
1025 DESTROY_IF(gateway);
1026 chunk_free(&nonce);
1027 enumerator->destroy(enumerator);
1028 return status;
1029 }
1030 default:
1031 {
1032 if (type <= 16383)
1033 {
1034 DBG1(DBG_IKE, "received %N notify error",
1035 notify_type_names, type);
1036 enumerator->destroy(enumerator);
1037 raise_alerts(this, type);
1038 return FAILED;
1039 }
1040 DBG2(DBG_IKE, "received %N notify",
1041 notify_type_names, type);
1042 break;
1043 }
1044 }
1045 }
1046 }
1047 enumerator->destroy(enumerator);
1048
1049 process_payloads(this, message);
1050
1051 /* check if we have everything */
1052 if (this->proposal == NULL ||
1053 this->other_nonce.len == 0 || this->my_nonce.len == 0)
1054 {
1055 DBG1(DBG_IKE, "peers proposal selection invalid");
1056 return FAILED;
1057 }
1058
1059 if (this->dh == NULL ||
1060 !this->proposal->has_dh_group(this->proposal, this->dh_group))
1061 {
1062 DBG1(DBG_IKE, "peer DH group selection invalid");
1063 return FAILED;
1064 }
1065
1066 if (this->dh_failed)
1067 {
1068 DBG1(DBG_IKE, "applying DH public value failed");
1069 return FAILED;
1070 }
1071
1072 if (!derive_keys(this, this->my_nonce, this->other_nonce))
1073 {
1074 DBG1(DBG_IKE, "key derivation failed");
1075 return FAILED;
1076 }
1077 return SUCCESS;
1078 }
1079
1080 METHOD(task_t, get_type, task_type_t,
1081 private_ike_init_t *this)
1082 {
1083 return TASK_IKE_INIT;
1084 }
1085
1086 METHOD(task_t, migrate, void,
1087 private_ike_init_t *this, ike_sa_t *ike_sa)
1088 {
1089 DESTROY_IF(this->proposal);
1090 chunk_free(&this->other_nonce);
1091
1092 this->ike_sa = ike_sa;
1093 this->keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa);
1094 this->proposal = NULL;
1095 this->dh_failed = FALSE;
1096 }
1097
1098 METHOD(task_t, destroy, void,
1099 private_ike_init_t *this)
1100 {
1101 DESTROY_IF(this->dh);
1102 DESTROY_IF(this->proposal);
1103 DESTROY_IF(this->nonceg);
1104 chunk_free(&this->my_nonce);
1105 chunk_free(&this->other_nonce);
1106 chunk_free(&this->cookie);
1107 free(this);
1108 }
1109
1110 METHOD(ike_init_t, get_lower_nonce, chunk_t,
1111 private_ike_init_t *this)
1112 {
1113 if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr,
1114 min(this->my_nonce.len, this->other_nonce.len)) < 0)
1115 {
1116 return this->my_nonce;
1117 }
1118 else
1119 {
1120 return this->other_nonce;
1121 }
1122 }
1123
1124 /*
1125 * Described in header.
1126 */
1127 ike_init_t *ike_init_create(ike_sa_t *ike_sa, bool initiator, ike_sa_t *old_sa)
1128 {
1129 private_ike_init_t *this;
1130
1131 INIT(this,
1132 .public = {
1133 .task = {
1134 .get_type = _get_type,
1135 .migrate = _migrate,
1136 .destroy = _destroy,
1137 },
1138 .get_lower_nonce = _get_lower_nonce,
1139 },
1140 .ike_sa = ike_sa,
1141 .initiator = initiator,
1142 .dh_group = MODP_NONE,
1143 .keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa),
1144 .old_sa = old_sa,
1145 .signature_authentication = lib->settings->get_bool(lib->settings,
1146 "%s.signature_authentication", TRUE, lib->ns),
1147 .follow_redirects = lib->settings->get_bool(lib->settings,
1148 "%s.follow_redirects", TRUE, lib->ns),
1149 );
1150 this->nonceg = this->keymat->keymat.create_nonce_gen(&this->keymat->keymat);
1151
1152 if (initiator)
1153 {
1154 this->public.task.build = _build_i;
1155 this->public.task.process = _process_i;
1156 this->public.task.pre_process = _pre_process_i;
1157 }
1158 else
1159 {
1160 this->public.task.build = _build_r;
1161 this->public.task.process = _process_r;
1162 }
1163 return &this->public;
1164 }