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
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>.
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
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>
32 /** maximum retries to do with cookies/other dh groups */
35 typedef struct private_ike_init_t private_ike_init_t
;
38 * Private members of a ike_init_t task.
40 struct private_ike_init_t
{
43 * Public methods and task_t interface.
53 * Are we the initiator?
58 * diffie hellman group to use
60 diffie_hellman_group_t dh_group
;
63 * diffie hellman key exchange
68 * Applying DH public value failed?
73 * Keymat derivation (from IKE_SA)
83 * nonce chosen by peer
93 * Negotiated proposal used for IKE_SA
98 * Old IKE_SA which gets rekeyed
103 * cookie received from responder
108 * retries done so far after failure (cookie or bad dh group)
113 * Whether to use Signature Authentication as per RFC 7427
115 bool signature_authentication
;
118 * Whether to follow IKEv2 redirects as per RFC 5685
120 bool follow_redirects
;
124 * Allocate our own nonce value
126 static bool generate_nonce(private_ike_init_t
*this)
130 DBG1(DBG_IKE
, "no nonce generator found to create nonce");
133 if (!this->nonceg
->allocate_nonce(this->nonceg
, NONCE_SIZE
,
136 DBG1(DBG_IKE
, "nonce allocation failed");
143 * Notify the peer about the hash algorithms we support or expect,
146 static void send_supported_hash_algorithms(private_ike_init_t
*this,
149 hash_algorithm_set_t
*algos
;
150 enumerator_t
*enumerator
, *rounds
;
151 bio_writer_t
*writer
;
152 hash_algorithm_t hash
;
156 signature_params_t
*config
;
158 size_t len
= BUF_LEN
;
163 algos
= hash_algorithm_set_create();
164 peer
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
167 rounds
= peer
->create_auth_cfg_enumerator(peer
, FALSE
);
168 while (rounds
->enumerate(rounds
, &auth
))
170 enumerator
= auth
->create_enumerator(auth
);
171 while (enumerator
->enumerate(enumerator
, &rule
, &config
))
173 if (rule
== AUTH_RULE_IKE_SIGNATURE_SCHEME
)
175 hash
= hasher_from_signature_scheme(config
->scheme
,
177 if (hasher_algorithm_for_ikev2(hash
))
179 algos
->add(algos
, hash
);
183 enumerator
->destroy(enumerator
);
185 rounds
->destroy(rounds
);
188 if (!algos
->count(algos
))
190 enumerator
= lib
->crypto
->create_hasher_enumerator(lib
->crypto
);
191 while (enumerator
->enumerate(enumerator
, &hash
, &plugin_name
))
193 if (hasher_algorithm_for_ikev2(hash
))
195 algos
->add(algos
, hash
);
198 enumerator
->destroy(enumerator
);
201 if (algos
->count(algos
))
203 writer
= bio_writer_create(0);
204 enumerator
= algos
->create_enumerator(algos
);
205 while (enumerator
->enumerate(enumerator
, &hash
))
207 writer
->write_uint16(writer
, hash
);
209 /* generate debug output */
210 written
= snprintf(pos
, len
, " %N", hash_algorithm_short_names
,
212 if (written
> 0 && written
< len
)
218 enumerator
->destroy(enumerator
);
219 message
->add_notify(message
, FALSE
, SIGNATURE_HASH_ALGORITHMS
,
220 writer
->get_buf(writer
));
221 writer
->destroy(writer
);
224 DBG2(DBG_CFG
, "sending supported signature hash algorithms:%s", buf
);
226 algos
->destroy(algos
);
230 * Store algorithms supported by other peer
232 static void handle_supported_hash_algorithms(private_ike_init_t
*this,
233 notify_payload_t
*notify
)
235 bio_reader_t
*reader
;
238 size_t len
= BUF_LEN
;
243 reader
= bio_reader_create(notify
->get_notification_data(notify
));
244 while (reader
->remaining(reader
) >= 2 && reader
->read_uint16(reader
, &algo
))
246 if (hasher_algorithm_for_ikev2(algo
))
248 this->keymat
->add_hash_algorithm(this->keymat
, algo
);
251 /* generate debug output */
252 written
= snprintf(pos
, len
, " %N", hash_algorithm_short_names
,
254 if (written
> 0 && written
< len
)
261 reader
->destroy(reader
);
264 DBG2(DBG_CFG
, "received supported signature hash algorithms:%s", buf
);
268 this->ike_sa
->enable_extension(this->ike_sa
, EXT_SIGNATURE_AUTH
);
273 * Check whether to send a USE_PPK notify
275 static bool send_use_ppk(private_ike_init_t
*this)
280 bool use_ppk
= FALSE
;
284 peer
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
285 if (peer
->get_ppk_id(peer
))
290 else if (this->ike_sa
->supports_extension(this->ike_sa
, EXT_PPK
))
292 /* check if we have at least one PPK available */
293 keys
= lib
->credmgr
->create_shared_enumerator(lib
->credmgr
, SHARED_PPK
,
295 if (keys
->enumerate(keys
, &key
, NULL
, NULL
))
305 * build the payloads for the message
307 static bool build_payloads(private_ike_init_t
*this, message_t
*message
)
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
;
314 proposal_t
*proposal
;
315 enumerator_t
*enumerator
;
318 id
= this->ike_sa
->get_id(this->ike_sa
);
320 ike_cfg
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
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
))
329 /* include SPI of new IKE_SA when we are rekeying */
332 proposal
->set_spi(proposal
, id
->get_initiator_spi(id
));
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
);
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
);
348 enumerator
->destroy(enumerator
);
349 other_dh_groups
->destroy(other_dh_groups
);
351 sa_payload
= sa_payload_create_from_proposals_v2(proposal_list
);
352 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
358 /* include SPI of new IKE_SA when we are rekeying */
359 this->proposal
->set_spi(this->proposal
, id
->get_responder_spi(id
));
361 sa_payload
= sa_payload_create_from_proposal_v2(this->proposal
);
363 message
->add_payload(message
, (payload_t
*)sa_payload
);
365 ke_payload
= ke_payload_create_from_diffie_hellman(PLV2_KEY_EXCHANGE
,
369 DBG1(DBG_IKE
, "creating KE payload failed");
372 nonce_payload
= nonce_payload_create(PLV2_NONCE
);
373 nonce_payload
->set_nonce(nonce_payload
, this->my_nonce
);
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
);
382 message
->add_payload(message
, (payload_t
*)ke_payload
);
383 message
->add_payload(message
, (payload_t
*)nonce_payload
);
386 /* negotiate fragmentation if we are not rekeying */
388 ike_cfg
->fragmentation(ike_cfg
) != FRAGMENTATION_NO
)
390 if (this->initiator
||
391 this->ike_sa
->supports_extension(this->ike_sa
,
392 EXT_IKE_FRAGMENTATION
))
394 message
->add_notify(message
, FALSE
, FRAGMENTATION_SUPPORTED
,
398 /* submit supported hash algorithms for signature authentication */
399 if (!this->old_sa
&& this->signature_authentication
)
401 if (this->initiator
||
402 this->ike_sa
->supports_extension(this->ike_sa
,
405 send_supported_hash_algorithms(this, message
);
408 /* notify other peer if we support redirection */
409 if (!this->old_sa
&& this->initiator
&& this->follow_redirects
)
411 identification_t
*gateway
;
415 from
= this->ike_sa
->get_redirected_from(this->ike_sa
);
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
);
423 gateway
->destroy(gateway
);
427 message
->add_notify(message
, FALSE
, REDIRECT_SUPPORTED
,
431 /* notify the peer if we want to use/support PPK */
432 if (!this->old_sa
&& send_use_ppk(this))
434 message
->add_notify(message
, FALSE
, USE_PPK
, chunk_empty
);
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
)
440 message
->add_notify(message
, FALSE
, CHILDLESS_IKEV2_SUPPORTED
,
447 * Process the SA payload and select a proposal
449 static void process_sa_payload(private_ike_init_t
*this, message_t
*message
,
450 sa_payload_t
*sa_payload
)
452 ike_cfg_t
*ike_cfg
, *cfg
, *alt_cfg
= NULL
;
453 enumerator_t
*enumerator
;
454 linked_list_t
*proposal_list
;
456 proposal_selection_flag_t flags
= 0;
458 ike_cfg
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
460 proposal_list
= sa_payload
->get_proposals(sa_payload
);
461 if (this->ike_sa
->supports_extension(this->ike_sa
, EXT_STRONGSWAN
))
463 flags
|= PROPOSAL_ALLOW_PRIVATE
;
465 if (!lib
->settings
->get_bool(lib
->settings
,
466 "%s.prefer_configured_proposals", TRUE
, lib
->ns
))
468 flags
|= PROPOSAL_PREFER_SUPPLIED
;
470 this->proposal
= ike_cfg
->select_proposal(ike_cfg
, proposal_list
, flags
);
473 if (!this->initiator
&& !this->old_sa
)
475 me
= message
->get_destination(message
);
476 other
= message
->get_source(message
);
477 enumerator
= charon
->backends
->create_ike_cfg_enumerator(
478 charon
->backends
, me
, other
, IKEV2
);
479 while (enumerator
->enumerate(enumerator
, &cfg
))
482 { /* already tried and failed */
485 DBG1(DBG_IKE
, "no matching proposal found, trying alternative "
487 this->proposal
= cfg
->select_proposal(cfg
, proposal_list
,
491 alt_cfg
= cfg
->get_ref(cfg
);
495 enumerator
->destroy(enumerator
);
499 this->ike_sa
->set_ike_cfg(this->ike_sa
, alt_cfg
);
500 alt_cfg
->destroy(alt_cfg
);
504 charon
->bus
->alert(charon
->bus
, ALERT_PROPOSAL_MISMATCH_IKE
,
508 proposal_list
->destroy_offset(proposal_list
,
509 offsetof(proposal_t
, destroy
));
513 * Read payloads from message
515 static void process_payloads(private_ike_init_t
*this, message_t
*message
)
517 enumerator_t
*enumerator
;
519 ke_payload_t
*ke_payload
= NULL
;
521 enumerator
= message
->create_payload_enumerator(message
);
522 while (enumerator
->enumerate(enumerator
, &payload
))
524 switch (payload
->get_type(payload
))
526 case PLV2_SECURITY_ASSOCIATION
:
528 process_sa_payload(this, message
, (sa_payload_t
*)payload
);
531 case PLV2_KEY_EXCHANGE
:
533 ke_payload
= (ke_payload_t
*)payload
;
535 this->dh_group
= ke_payload
->get_dh_group_number(ke_payload
);
540 nonce_payload_t
*nonce_payload
= (nonce_payload_t
*)payload
;
542 this->other_nonce
= nonce_payload
->get_nonce(nonce_payload
);
547 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
549 switch (notify
->get_notify_type(notify
))
551 case FRAGMENTATION_SUPPORTED
:
552 this->ike_sa
->enable_extension(this->ike_sa
,
553 EXT_IKE_FRAGMENTATION
);
555 case SIGNATURE_HASH_ALGORITHMS
:
556 if (this->signature_authentication
)
558 handle_supported_hash_algorithms(this, notify
);
564 this->ike_sa
->enable_extension(this->ike_sa
,
568 case REDIRECTED_FROM
:
570 identification_t
*gateway
;
573 data
= notify
->get_notification_data(notify
);
574 gateway
= redirect_data_parse(data
, NULL
);
577 DBG1(DBG_IKE
, "received invalid REDIRECTED_FROM "
581 DBG1(DBG_IKE
, "client got redirected from %Y", gateway
);
582 gateway
->destroy(gateway
);
585 case REDIRECT_SUPPORTED
:
588 this->ike_sa
->enable_extension(this->ike_sa
,
589 EXT_IKE_REDIRECTION
);
592 case CHILDLESS_IKEV2_SUPPORTED
:
593 if (this->initiator
&& !this->old_sa
)
595 this->ike_sa
->enable_extension(this->ike_sa
,
600 /* other notifies are handled elsewhere */
609 enumerator
->destroy(enumerator
);
613 this->ike_sa
->set_proposal(this->ike_sa
, this->proposal
);
616 if (ke_payload
&& this->proposal
&&
617 this->proposal
->has_dh_group(this->proposal
, this->dh_group
))
619 if (!this->initiator
)
621 this->dh
= this->keymat
->keymat
.create_dh(
622 &this->keymat
->keymat
, this->dh_group
);
626 this->dh_failed
= this->dh
->get_dh_group(this->dh
) != this->dh_group
;
628 if (this->dh
&& !this->dh_failed
)
630 this->dh_failed
= !this->dh
->set_other_public_value(this->dh
,
631 ke_payload
->get_key_exchange_data(ke_payload
));
636 METHOD(task_t
, build_i
, status_t
,
637 private_ike_init_t
*this, message_t
*message
)
641 ike_cfg
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
643 DBG0(DBG_IKE
, "initiating IKE_SA %s[%d] to %H",
644 this->ike_sa
->get_name(this->ike_sa
),
645 this->ike_sa
->get_unique_id(this->ike_sa
),
646 this->ike_sa
->get_other_host(this->ike_sa
));
647 this->ike_sa
->set_state(this->ike_sa
, IKE_CONNECTING
);
649 if (this->retry
>= MAX_RETRIES
)
651 DBG1(DBG_IKE
, "giving up after %d retries", MAX_RETRIES
);
655 /* if we are retrying after an INVALID_KE_PAYLOAD we already have one */
658 if (this->old_sa
&& lib
->settings
->get_bool(lib
->settings
,
659 "%s.prefer_previous_dh_group", TRUE
, lib
->ns
))
660 { /* reuse the DH group we used for the old IKE_SA when rekeying */
661 proposal_t
*proposal
;
664 proposal
= this->old_sa
->get_proposal(this->old_sa
);
665 if (proposal
->get_algorithm(proposal
, DIFFIE_HELLMAN_GROUP
,
668 this->dh_group
= dh_group
;
671 { /* this shouldn't happen, but let's be safe */
672 this->dh_group
= ike_cfg
->get_dh_group(ike_cfg
);
677 this->dh_group
= ike_cfg
->get_dh_group(ike_cfg
);
679 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
683 DBG1(DBG_IKE
, "configured DH group %N not supported",
684 diffie_hellman_group_names
, this->dh_group
);
688 else if (this->dh
->get_dh_group(this->dh
) != this->dh_group
)
689 { /* reset DH instance if group changed (INVALID_KE_PAYLOAD) */
690 this->dh
->destroy(this->dh
);
691 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
695 DBG1(DBG_IKE
, "requested DH group %N not supported",
696 diffie_hellman_group_names
, this->dh_group
);
701 /* generate nonce only when we are trying the first time */
702 if (this->my_nonce
.ptr
== NULL
)
704 if (!generate_nonce(this))
710 if (this->cookie
.ptr
)
712 message
->add_notify(message
, FALSE
, COOKIE
, this->cookie
);
715 if (!build_payloads(this, message
))
722 chunk_t connect_id
= this->ike_sa
->get_connect_id(this->ike_sa
);
725 message
->add_notify(message
, FALSE
, ME_CONNECTID
, connect_id
);
733 METHOD(task_t
, process_r
, status_t
,
734 private_ike_init_t
*this, message_t
*message
)
736 DBG0(DBG_IKE
, "%H is initiating an IKE_SA", message
->get_source(message
));
737 this->ike_sa
->set_state(this->ike_sa
, IKE_CONNECTING
);
739 if (!generate_nonce(this))
746 notify_payload_t
*notify
= message
->get_notify(message
, ME_CONNECTID
);
749 chunk_t connect_id
= notify
->get_notification_data(notify
);
750 DBG2(DBG_IKE
, "received ME_CONNECTID %#B", &connect_id
);
751 charon
->connect_manager
->stop_checks(charon
->connect_manager
,
757 process_payloads(this, message
);
763 * Derive the keymat for the IKE_SA
765 static bool derive_keys(private_ike_init_t
*this,
766 chunk_t nonce_i
, chunk_t nonce_r
)
768 keymat_v2_t
*old_keymat
;
769 pseudo_random_function_t prf_alg
= PRF_UNDEFINED
;
770 chunk_t skd
= chunk_empty
;
773 id
= this->ike_sa
->get_id(this->ike_sa
);
776 /* rekeying: Include old SKd, use old PRF, apply SPI */
777 old_keymat
= (keymat_v2_t
*)this->old_sa
->get_keymat(this->old_sa
);
778 prf_alg
= old_keymat
->get_skd(old_keymat
, &skd
);
781 id
->set_responder_spi(id
, this->proposal
->get_spi(this->proposal
));
785 id
->set_initiator_spi(id
, this->proposal
->get_spi(this->proposal
));
788 if (!this->keymat
->derive_ike_keys(this->keymat
, this->proposal
, this->dh
,
789 nonce_i
, nonce_r
, id
, prf_alg
, skd
))
793 charon
->bus
->ike_keys(charon
->bus
, this->ike_sa
, this->dh
, chunk_empty
,
794 nonce_i
, nonce_r
, this->old_sa
, NULL
, AUTH_NONE
);
798 METHOD(task_t
, build_r
, status_t
,
799 private_ike_init_t
*this, message_t
*message
)
801 identification_t
*gateway
;
803 /* check if we have everything we need */
804 if (this->proposal
== NULL
||
805 this->other_nonce
.len
== 0 || this->my_nonce
.len
== 0)
807 DBG1(DBG_IKE
, "received proposals unacceptable");
808 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
812 /* check if we'd have to redirect the client */
814 this->ike_sa
->supports_extension(this->ike_sa
, EXT_IKE_REDIRECTION
) &&
815 charon
->redirect
->redirect_on_init(charon
->redirect
, this->ike_sa
,
820 DBG1(DBG_IKE
, "redirecting peer to %Y", gateway
);
821 data
= redirect_data_create(gateway
, this->other_nonce
);
822 message
->add_notify(message
, TRUE
, REDIRECT
, data
);
823 gateway
->destroy(gateway
);
828 if (this->dh
== NULL
||
829 !this->proposal
->has_dh_group(this->proposal
, this->dh_group
))
833 if (this->proposal
->get_algorithm(this->proposal
, DIFFIE_HELLMAN_GROUP
,
836 DBG1(DBG_IKE
, "DH group %N unacceptable, requesting %N",
837 diffie_hellman_group_names
, this->dh_group
,
838 diffie_hellman_group_names
, group
);
839 this->dh_group
= group
;
840 group
= htons(group
);
841 message
->add_notify(message
, FALSE
, INVALID_KE_PAYLOAD
,
842 chunk_from_thing(group
));
846 DBG1(DBG_IKE
, "no acceptable proposal found");
847 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
854 DBG1(DBG_IKE
, "applying DH public value failed");
855 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
859 if (!derive_keys(this, this->other_nonce
, this->my_nonce
))
861 DBG1(DBG_IKE
, "key derivation failed");
862 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
865 if (!build_payloads(this, message
))
867 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
874 * Raise alerts for received notify errors
876 static void raise_alerts(private_ike_init_t
*this, notify_type_t type
)
883 case NO_PROPOSAL_CHOSEN
:
884 ike_cfg
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
885 list
= ike_cfg
->get_proposals(ike_cfg
);
886 charon
->bus
->alert(charon
->bus
, ALERT_PROPOSAL_MISMATCH_IKE
, list
);
887 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
894 METHOD(task_t
, pre_process_i
, status_t
,
895 private_ike_init_t
*this, message_t
*message
)
897 enumerator_t
*enumerator
;
900 /* check for erroneous notifies */
901 enumerator
= message
->create_payload_enumerator(message
);
902 while (enumerator
->enumerate(enumerator
, &payload
))
904 if (payload
->get_type(payload
) == PLV2_NOTIFY
)
906 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
907 notify_type_t type
= notify
->get_notify_type(notify
);
915 cookie
= notify
->get_notification_data(notify
);
916 if (chunk_equals(cookie
, this->cookie
))
918 DBG1(DBG_IKE
, "ignore response with duplicate COOKIE "
920 enumerator
->destroy(enumerator
);
927 identification_t
*gateway
;
928 chunk_t data
, nonce
= chunk_empty
;
929 status_t status
= SUCCESS
;
935 data
= notify
->get_notification_data(notify
);
936 gateway
= redirect_data_parse(data
, &nonce
);
937 if (!gateway
|| !chunk_equals(nonce
, this->my_nonce
))
939 DBG1(DBG_IKE
, "received invalid REDIRECT notify");
944 enumerator
->destroy(enumerator
);
952 enumerator
->destroy(enumerator
);
956 METHOD(task_t
, process_i
, status_t
,
957 private_ike_init_t
*this, message_t
*message
)
959 enumerator_t
*enumerator
;
962 /* check for erroneous notifies */
963 enumerator
= message
->create_payload_enumerator(message
);
964 while (enumerator
->enumerate(enumerator
, &payload
))
966 if (payload
->get_type(payload
) == PLV2_NOTIFY
)
968 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
969 notify_type_t type
= notify
->get_notify_type(notify
);
973 case INVALID_KE_PAYLOAD
:
976 diffie_hellman_group_t bad_group
;
978 bad_group
= this->dh_group
;
979 data
= notify
->get_notification_data(notify
);
980 this->dh_group
= ntohs(*((uint16_t*)data
.ptr
));
981 DBG1(DBG_IKE
, "peer didn't accept DH group %N, "
982 "it requested %N", diffie_hellman_group_names
,
983 bad_group
, diffie_hellman_group_names
, this->dh_group
);
985 if (this->old_sa
== NULL
)
986 { /* reset the IKE_SA if we are not rekeying */
987 this->ike_sa
->reset(this->ike_sa
, FALSE
);
990 enumerator
->destroy(enumerator
);
994 case NAT_DETECTION_SOURCE_IP
:
995 case NAT_DETECTION_DESTINATION_IP
:
996 /* skip, handled in ike_natd_t */
998 case MULTIPLE_AUTH_SUPPORTED
:
999 /* handled in ike_auth_t */
1003 chunk_free(&this->cookie
);
1004 this->cookie
= chunk_clone(notify
->get_notification_data(notify
));
1005 this->ike_sa
->reset(this->ike_sa
, FALSE
);
1006 enumerator
->destroy(enumerator
);
1007 DBG2(DBG_IKE
, "received %N notify", notify_type_names
, type
);
1013 identification_t
*gateway
;
1014 chunk_t data
, nonce
= chunk_empty
;
1015 status_t status
= FAILED
;
1019 DBG1(DBG_IKE
, "received REDIRECT notify during rekeying"
1023 data
= notify
->get_notification_data(notify
);
1024 gateway
= redirect_data_parse(data
, &nonce
);
1025 if (this->ike_sa
->handle_redirect(this->ike_sa
, gateway
))
1029 DESTROY_IF(gateway
);
1031 enumerator
->destroy(enumerator
);
1038 DBG1(DBG_IKE
, "received %N notify error",
1039 notify_type_names
, type
);
1040 enumerator
->destroy(enumerator
);
1041 raise_alerts(this, type
);
1044 DBG2(DBG_IKE
, "received %N notify",
1045 notify_type_names
, type
);
1051 enumerator
->destroy(enumerator
);
1053 process_payloads(this, message
);
1055 /* check if we have everything */
1056 if (this->proposal
== NULL
||
1057 this->other_nonce
.len
== 0 || this->my_nonce
.len
== 0)
1059 DBG1(DBG_IKE
, "peers proposal selection invalid");
1063 if (this->dh
== NULL
||
1064 !this->proposal
->has_dh_group(this->proposal
, this->dh_group
))
1066 DBG1(DBG_IKE
, "peer DH group selection invalid");
1070 if (this->dh_failed
)
1072 DBG1(DBG_IKE
, "applying DH public value failed");
1076 if (!derive_keys(this, this->my_nonce
, this->other_nonce
))
1078 DBG1(DBG_IKE
, "key derivation failed");
1084 METHOD(task_t
, get_type
, task_type_t
,
1085 private_ike_init_t
*this)
1087 return TASK_IKE_INIT
;
1090 METHOD(task_t
, migrate
, void,
1091 private_ike_init_t
*this, ike_sa_t
*ike_sa
)
1093 DESTROY_IF(this->proposal
);
1094 chunk_free(&this->other_nonce
);
1096 this->ike_sa
= ike_sa
;
1097 this->keymat
= (keymat_v2_t
*)ike_sa
->get_keymat(ike_sa
);
1098 this->proposal
= NULL
;
1099 this->dh_failed
= FALSE
;
1102 METHOD(task_t
, destroy
, void,
1103 private_ike_init_t
*this)
1105 DESTROY_IF(this->dh
);
1106 DESTROY_IF(this->proposal
);
1107 DESTROY_IF(this->nonceg
);
1108 chunk_free(&this->my_nonce
);
1109 chunk_free(&this->other_nonce
);
1110 chunk_free(&this->cookie
);
1114 METHOD(ike_init_t
, get_lower_nonce
, chunk_t
,
1115 private_ike_init_t
*this)
1117 if (memcmp(this->my_nonce
.ptr
, this->other_nonce
.ptr
,
1118 min(this->my_nonce
.len
, this->other_nonce
.len
)) < 0)
1120 return this->my_nonce
;
1124 return this->other_nonce
;
1129 * Described in header.
1131 ike_init_t
*ike_init_create(ike_sa_t
*ike_sa
, bool initiator
, ike_sa_t
*old_sa
)
1133 private_ike_init_t
*this;
1138 .get_type
= _get_type
,
1139 .migrate
= _migrate
,
1140 .destroy
= _destroy
,
1142 .get_lower_nonce
= _get_lower_nonce
,
1145 .initiator
= initiator
,
1146 .dh_group
= MODP_NONE
,
1147 .keymat
= (keymat_v2_t
*)ike_sa
->get_keymat(ike_sa
),
1149 .signature_authentication
= lib
->settings
->get_bool(lib
->settings
,
1150 "%s.signature_authentication", TRUE
, lib
->ns
),
1151 .follow_redirects
= lib
->settings
->get_bool(lib
->settings
,
1152 "%s.follow_redirects", TRUE
, lib
->ns
),
1154 this->nonceg
= this->keymat
->keymat
.create_nonce_gen(&this->keymat
->keymat
);
1158 this->public.task
.build
= _build_i
;
1159 this->public.task
.process
= _process_i
;
1160 this->public.task
.pre_process
= _pre_process_i
;
1164 this->public.task
.build
= _build_r
;
1165 this->public.task
.process
= _process_r
;
1167 return &this->public;