2 * Copyright (C) 2008-2015 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * 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 * IKE config to establish
63 * diffie hellman group to use
65 diffie_hellman_group_t dh_group
;
68 * diffie hellman key exchange
73 * Applying DH public value failed?
78 * Keymat derivation (from IKE_SA)
88 * nonce chosen by peer
98 * Negotiated proposal used for IKE_SA
100 proposal_t
*proposal
;
103 * Old IKE_SA which gets rekeyed
108 * cookie received from responder
113 * retries done so far after failure (cookie or bad dh group)
118 * Whether to use Signature Authentication as per RFC 7427
120 bool signature_authentication
;
123 * Whether to follow IKEv2 redirects as per RFC 5685
125 bool follow_redirects
;
129 * Allocate our own nonce value
131 static bool generate_nonce(private_ike_init_t
*this)
135 DBG1(DBG_IKE
, "no nonce generator found to create nonce");
138 if (!this->nonceg
->allocate_nonce(this->nonceg
, NONCE_SIZE
,
141 DBG1(DBG_IKE
, "nonce allocation failed");
148 * Notify the peer about the hash algorithms we support or expect,
151 static void send_supported_hash_algorithms(private_ike_init_t
*this,
154 hash_algorithm_set_t
*algos
;
155 enumerator_t
*enumerator
, *rounds
;
156 bio_writer_t
*writer
;
157 hash_algorithm_t hash
;
164 algos
= hash_algorithm_set_create();
165 peer
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
168 rounds
= peer
->create_auth_cfg_enumerator(peer
, FALSE
);
169 while (rounds
->enumerate(rounds
, &auth
))
171 enumerator
= auth
->create_enumerator(auth
);
172 while (enumerator
->enumerate(enumerator
, &rule
, &config
))
174 if (rule
== AUTH_RULE_SIGNATURE_SCHEME
)
176 hash
= hasher_from_signature_scheme(config
);
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 enumerator
->destroy(enumerator
);
210 message
->add_notify(message
, FALSE
, SIGNATURE_HASH_ALGORITHMS
,
211 writer
->get_buf(writer
));
212 writer
->destroy(writer
);
214 algos
->destroy(algos
);
218 * Store algorithms supported by other peer
220 static void handle_supported_hash_algorithms(private_ike_init_t
*this,
221 notify_payload_t
*notify
)
223 bio_reader_t
*reader
;
227 reader
= bio_reader_create(notify
->get_notification_data(notify
));
228 while (reader
->remaining(reader
) >= 2 && reader
->read_uint16(reader
, &algo
))
230 if (hasher_algorithm_for_ikev2(algo
))
232 this->keymat
->add_hash_algorithm(this->keymat
, algo
);
236 reader
->destroy(reader
);
240 this->ike_sa
->enable_extension(this->ike_sa
, EXT_SIGNATURE_AUTH
);
245 * build the payloads for the message
247 static bool build_payloads(private_ike_init_t
*this, message_t
*message
)
249 sa_payload_t
*sa_payload
;
250 ke_payload_t
*ke_payload
;
251 nonce_payload_t
*nonce_payload
;
252 linked_list_t
*proposal_list
;
254 proposal_t
*proposal
;
255 enumerator_t
*enumerator
;
257 id
= this->ike_sa
->get_id(this->ike_sa
);
259 this->config
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
263 proposal_list
= this->config
->get_proposals(this->config
);
266 /* include SPI of new IKE_SA when we are rekeying */
267 enumerator
= proposal_list
->create_enumerator(proposal_list
);
268 while (enumerator
->enumerate(enumerator
, (void**)&proposal
))
270 proposal
->set_spi(proposal
, id
->get_initiator_spi(id
));
272 enumerator
->destroy(enumerator
);
275 sa_payload
= sa_payload_create_from_proposals_v2(proposal_list
);
276 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
282 /* include SPI of new IKE_SA when we are rekeying */
283 this->proposal
->set_spi(this->proposal
, id
->get_responder_spi(id
));
285 sa_payload
= sa_payload_create_from_proposal_v2(this->proposal
);
287 message
->add_payload(message
, (payload_t
*)sa_payload
);
289 nonce_payload
= nonce_payload_create(PLV2_NONCE
);
290 nonce_payload
->set_nonce(nonce_payload
, this->my_nonce
);
291 ke_payload
= ke_payload_create_from_diffie_hellman(PLV2_KEY_EXCHANGE
,
295 DBG1(DBG_IKE
, "creating KE payload failed");
300 { /* payload order differs if we are rekeying */
301 message
->add_payload(message
, (payload_t
*)nonce_payload
);
302 message
->add_payload(message
, (payload_t
*)ke_payload
);
306 message
->add_payload(message
, (payload_t
*)ke_payload
);
307 message
->add_payload(message
, (payload_t
*)nonce_payload
);
310 /* negotiate fragmentation if we are not rekeying */
312 this->config
->fragmentation(this->config
) != FRAGMENTATION_NO
)
314 if (this->initiator
||
315 this->ike_sa
->supports_extension(this->ike_sa
,
316 EXT_IKE_FRAGMENTATION
))
318 message
->add_notify(message
, FALSE
, FRAGMENTATION_SUPPORTED
,
322 /* submit supported hash algorithms for signature authentication */
323 if (!this->old_sa
&& this->signature_authentication
)
325 if (this->initiator
||
326 this->ike_sa
->supports_extension(this->ike_sa
,
329 send_supported_hash_algorithms(this, message
);
332 /* notify other peer if we support redirection */
333 if (!this->old_sa
&& this->initiator
&& this->follow_redirects
)
335 message
->add_notify(message
, FALSE
, REDIRECT_SUPPORTED
, chunk_empty
);
341 * Read payloads from message
343 static void process_payloads(private_ike_init_t
*this, message_t
*message
)
345 enumerator_t
*enumerator
;
347 ke_payload_t
*ke_payload
= NULL
;
349 enumerator
= message
->create_payload_enumerator(message
);
350 while (enumerator
->enumerate(enumerator
, &payload
))
352 switch (payload
->get_type(payload
))
354 case PLV2_SECURITY_ASSOCIATION
:
356 sa_payload_t
*sa_payload
= (sa_payload_t
*)payload
;
357 linked_list_t
*proposal_list
;
360 proposal_list
= sa_payload
->get_proposals(sa_payload
);
361 private = this->ike_sa
->supports_extension(this->ike_sa
,
363 this->proposal
= this->config
->select_proposal(this->config
,
364 proposal_list
, private);
367 charon
->bus
->alert(charon
->bus
, ALERT_PROPOSAL_MISMATCH_IKE
,
370 proposal_list
->destroy_offset(proposal_list
,
371 offsetof(proposal_t
, destroy
));
374 case PLV2_KEY_EXCHANGE
:
376 ke_payload
= (ke_payload_t
*)payload
;
378 this->dh_group
= ke_payload
->get_dh_group_number(ke_payload
);
383 nonce_payload_t
*nonce_payload
= (nonce_payload_t
*)payload
;
385 this->other_nonce
= nonce_payload
->get_nonce(nonce_payload
);
390 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
392 switch (notify
->get_notify_type(notify
))
394 case FRAGMENTATION_SUPPORTED
:
395 this->ike_sa
->enable_extension(this->ike_sa
,
396 EXT_IKE_FRAGMENTATION
);
398 case SIGNATURE_HASH_ALGORITHMS
:
399 if (this->signature_authentication
)
401 handle_supported_hash_algorithms(this, notify
);
404 case REDIRECT_SUPPORTED
:
405 this->ike_sa
->enable_extension(this->ike_sa
,
406 EXT_IKE_REDIRECTION
);
409 /* other notifies are handled elsewhere */
418 enumerator
->destroy(enumerator
);
420 if (ke_payload
&& this->proposal
&&
421 this->proposal
->has_dh_group(this->proposal
, this->dh_group
))
423 if (!this->initiator
)
425 this->dh
= this->keymat
->keymat
.create_dh(
426 &this->keymat
->keymat
, this->dh_group
);
430 this->dh_failed
= !this->dh
->set_other_public_value(this->dh
,
431 ke_payload
->get_key_exchange_data(ke_payload
));
436 METHOD(task_t
, build_i
, status_t
,
437 private_ike_init_t
*this, message_t
*message
)
439 this->config
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
440 DBG0(DBG_IKE
, "initiating IKE_SA %s[%d] to %H",
441 this->ike_sa
->get_name(this->ike_sa
),
442 this->ike_sa
->get_unique_id(this->ike_sa
),
443 this->ike_sa
->get_other_host(this->ike_sa
));
444 this->ike_sa
->set_state(this->ike_sa
, IKE_CONNECTING
);
446 if (this->retry
>= MAX_RETRIES
)
448 DBG1(DBG_IKE
, "giving up after %d retries", MAX_RETRIES
);
452 /* if the DH group is set via use_dh_group(), we already have a DH object */
455 this->dh_group
= this->config
->get_dh_group(this->config
);
456 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
460 DBG1(DBG_IKE
, "configured DH group %N not supported",
461 diffie_hellman_group_names
, this->dh_group
);
466 /* generate nonce only when we are trying the first time */
467 if (this->my_nonce
.ptr
== NULL
)
469 if (!generate_nonce(this))
475 if (this->cookie
.ptr
)
477 message
->add_notify(message
, FALSE
, COOKIE
, this->cookie
);
480 if (!build_payloads(this, message
))
487 chunk_t connect_id
= this->ike_sa
->get_connect_id(this->ike_sa
);
490 message
->add_notify(message
, FALSE
, ME_CONNECTID
, connect_id
);
498 METHOD(task_t
, process_r
, status_t
,
499 private_ike_init_t
*this, message_t
*message
)
501 this->config
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
502 DBG0(DBG_IKE
, "%H is initiating an IKE_SA", message
->get_source(message
));
503 this->ike_sa
->set_state(this->ike_sa
, IKE_CONNECTING
);
505 if (!generate_nonce(this))
512 notify_payload_t
*notify
= message
->get_notify(message
, ME_CONNECTID
);
515 chunk_t connect_id
= notify
->get_notification_data(notify
);
516 DBG2(DBG_IKE
, "received ME_CONNECTID %#B", &connect_id
);
517 charon
->connect_manager
->stop_checks(charon
->connect_manager
,
523 process_payloads(this, message
);
529 * Derive the keymat for the IKE_SA
531 static bool derive_keys(private_ike_init_t
*this,
532 chunk_t nonce_i
, chunk_t nonce_r
)
534 keymat_v2_t
*old_keymat
;
535 pseudo_random_function_t prf_alg
= PRF_UNDEFINED
;
536 chunk_t skd
= chunk_empty
;
539 id
= this->ike_sa
->get_id(this->ike_sa
);
542 /* rekeying: Include old SKd, use old PRF, apply SPI */
543 old_keymat
= (keymat_v2_t
*)this->old_sa
->get_keymat(this->old_sa
);
544 prf_alg
= old_keymat
->get_skd(old_keymat
, &skd
);
547 id
->set_responder_spi(id
, this->proposal
->get_spi(this->proposal
));
551 id
->set_initiator_spi(id
, this->proposal
->get_spi(this->proposal
));
554 if (!this->keymat
->derive_ike_keys(this->keymat
, this->proposal
, this->dh
,
555 nonce_i
, nonce_r
, id
, prf_alg
, skd
))
559 charon
->bus
->ike_keys(charon
->bus
, this->ike_sa
, this->dh
, chunk_empty
,
560 nonce_i
, nonce_r
, this->old_sa
, NULL
);
564 METHOD(task_t
, build_r
, status_t
,
565 private_ike_init_t
*this, message_t
*message
)
567 identification_t
*gateway
;
569 /* check if we have everything we need */
570 if (this->proposal
== NULL
||
571 this->other_nonce
.len
== 0 || this->my_nonce
.len
== 0)
573 DBG1(DBG_IKE
, "received proposals inacceptable");
574 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
577 this->ike_sa
->set_proposal(this->ike_sa
, this->proposal
);
579 /* check if we'd have to redirect the client */
580 if (this->ike_sa
->supports_extension(this->ike_sa
, EXT_IKE_REDIRECTION
) &&
581 charon
->redirect
->redirect_on_init(charon
->redirect
, this->ike_sa
,
586 DBG1(DBG_IKE
, "redirecting peer to %Y", gateway
);
587 data
= redirect_data_create(gateway
, this->other_nonce
);
588 message
->add_notify(message
, TRUE
, REDIRECT
, data
);
589 gateway
->destroy(gateway
);
594 if (this->dh
== NULL
||
595 !this->proposal
->has_dh_group(this->proposal
, this->dh_group
))
599 if (this->proposal
->get_algorithm(this->proposal
, DIFFIE_HELLMAN_GROUP
,
602 DBG1(DBG_IKE
, "DH group %N inacceptable, requesting %N",
603 diffie_hellman_group_names
, this->dh_group
,
604 diffie_hellman_group_names
, group
);
605 this->dh_group
= group
;
606 group
= htons(group
);
607 message
->add_notify(message
, FALSE
, INVALID_KE_PAYLOAD
,
608 chunk_from_thing(group
));
612 DBG1(DBG_IKE
, "no acceptable proposal found");
619 DBG1(DBG_IKE
, "applying DH public value failed");
620 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
624 if (!derive_keys(this, this->other_nonce
, this->my_nonce
))
626 DBG1(DBG_IKE
, "key derivation failed");
627 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
630 if (!build_payloads(this, message
))
632 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
639 * Raise alerts for received notify errors
641 static void raise_alerts(private_ike_init_t
*this, notify_type_t type
)
647 case NO_PROPOSAL_CHOSEN
:
648 list
= this->config
->get_proposals(this->config
);
649 charon
->bus
->alert(charon
->bus
, ALERT_PROPOSAL_MISMATCH_IKE
, list
);
650 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
657 METHOD(task_t
, process_i
, status_t
,
658 private_ike_init_t
*this, message_t
*message
)
660 enumerator_t
*enumerator
;
663 /* check for erroneous notifies */
664 enumerator
= message
->create_payload_enumerator(message
);
665 while (enumerator
->enumerate(enumerator
, &payload
))
667 if (payload
->get_type(payload
) == PLV2_NOTIFY
)
669 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
670 notify_type_t type
= notify
->get_notify_type(notify
);
674 case INVALID_KE_PAYLOAD
:
677 diffie_hellman_group_t bad_group
;
679 bad_group
= this->dh_group
;
680 data
= notify
->get_notification_data(notify
);
681 this->dh_group
= ntohs(*((u_int16_t
*)data
.ptr
));
682 DBG1(DBG_IKE
, "peer didn't accept DH group %N, "
683 "it requested %N", diffie_hellman_group_names
,
684 bad_group
, diffie_hellman_group_names
, this->dh_group
);
686 if (this->old_sa
== NULL
)
687 { /* reset the IKE_SA if we are not rekeying */
688 this->ike_sa
->reset(this->ike_sa
);
691 enumerator
->destroy(enumerator
);
695 case NAT_DETECTION_SOURCE_IP
:
696 case NAT_DETECTION_DESTINATION_IP
:
697 /* skip, handled in ike_natd_t */
699 case MULTIPLE_AUTH_SUPPORTED
:
700 /* handled in ike_auth_t */
704 chunk_free(&this->cookie
);
705 this->cookie
= chunk_clone(notify
->get_notification_data(notify
));
706 this->ike_sa
->reset(this->ike_sa
);
707 enumerator
->destroy(enumerator
);
708 DBG2(DBG_IKE
, "received %N notify", notify_type_names
, type
);
714 identification_t
*gateway
;
715 chunk_t data
, nonce
= chunk_empty
;
716 status_t status
= FAILED
;
718 data
= notify
->get_notification_data(notify
);
719 gateway
= redirect_data_parse(data
, &nonce
);
720 enumerator
->destroy(enumerator
);
721 if (!gateway
|| !chunk_equals(nonce
, this->my_nonce
))
723 DBG1(DBG_IKE
, "received invalid REDIRECT notify");
725 else if (this->ike_sa
->handle_redirect(this->ike_sa
,
738 DBG1(DBG_IKE
, "received %N notify error",
739 notify_type_names
, type
);
740 enumerator
->destroy(enumerator
);
741 raise_alerts(this, type
);
744 DBG2(DBG_IKE
, "received %N notify",
745 notify_type_names
, type
);
751 enumerator
->destroy(enumerator
);
753 process_payloads(this, message
);
755 /* check if we have everything */
756 if (this->proposal
== NULL
||
757 this->other_nonce
.len
== 0 || this->my_nonce
.len
== 0)
759 DBG1(DBG_IKE
, "peers proposal selection invalid");
762 this->ike_sa
->set_proposal(this->ike_sa
, this->proposal
);
764 if (this->dh
== NULL
||
765 !this->proposal
->has_dh_group(this->proposal
, this->dh_group
))
767 DBG1(DBG_IKE
, "peer DH group selection invalid");
773 DBG1(DBG_IKE
, "applying DH public value failed");
777 if (!derive_keys(this, this->my_nonce
, this->other_nonce
))
779 DBG1(DBG_IKE
, "key derivation failed");
785 METHOD(task_t
, get_type
, task_type_t
,
786 private_ike_init_t
*this)
788 return TASK_IKE_INIT
;
791 METHOD(task_t
, migrate
, void,
792 private_ike_init_t
*this, ike_sa_t
*ike_sa
)
794 DESTROY_IF(this->proposal
);
795 chunk_free(&this->other_nonce
);
797 this->ike_sa
= ike_sa
;
798 this->keymat
= (keymat_v2_t
*)ike_sa
->get_keymat(ike_sa
);
799 this->proposal
= NULL
;
800 this->dh_failed
= FALSE
;
801 if (this->dh
&& this->dh
->get_dh_group(this->dh
) != this->dh_group
)
802 { /* reset DH value only if group changed (INVALID_KE_PAYLOAD) */
803 this->dh
->destroy(this->dh
);
804 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
809 METHOD(task_t
, destroy
, void,
810 private_ike_init_t
*this)
812 DESTROY_IF(this->dh
);
813 DESTROY_IF(this->proposal
);
814 DESTROY_IF(this->nonceg
);
815 chunk_free(&this->my_nonce
);
816 chunk_free(&this->other_nonce
);
817 chunk_free(&this->cookie
);
821 METHOD(ike_init_t
, get_lower_nonce
, chunk_t
,
822 private_ike_init_t
*this)
824 if (memcmp(this->my_nonce
.ptr
, this->other_nonce
.ptr
,
825 min(this->my_nonce
.len
, this->other_nonce
.len
)) < 0)
827 return this->my_nonce
;
831 return this->other_nonce
;
836 * Described in header.
838 ike_init_t
*ike_init_create(ike_sa_t
*ike_sa
, bool initiator
, ike_sa_t
*old_sa
)
840 private_ike_init_t
*this;
845 .get_type
= _get_type
,
849 .get_lower_nonce
= _get_lower_nonce
,
852 .initiator
= initiator
,
853 .dh_group
= MODP_NONE
,
854 .keymat
= (keymat_v2_t
*)ike_sa
->get_keymat(ike_sa
),
856 .signature_authentication
= lib
->settings
->get_bool(lib
->settings
,
857 "%s.signature_authentication", TRUE
, lib
->ns
),
858 .follow_redirects
= lib
->settings
->get_bool(lib
->settings
,
859 "%s.follow_redirects", TRUE
, lib
->ns
),
861 this->nonceg
= this->keymat
->keymat
.create_nonce_gen(&this->keymat
->keymat
);
865 this->public.task
.build
= _build_i
;
866 this->public.task
.process
= _process_i
;
870 this->public.task
.build
= _build_r
;
871 this->public.task
.process
= _process_r
;
873 return &this->public;