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