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