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
;
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
;
159 algos
= hash_algorithm_set_create();
160 peer
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
163 rounds
= peer
->create_auth_cfg_enumerator(peer
, FALSE
);
164 while (rounds
->enumerate(rounds
, &auth
))
166 enumerator
= auth
->create_enumerator(auth
);
167 while (enumerator
->enumerate(enumerator
, &rule
, &config
))
169 if (rule
== AUTH_RULE_SIGNATURE_SCHEME
)
171 hash
= hasher_from_signature_scheme(config
);
172 if (hasher_algorithm_for_ikev2(hash
))
174 algos
->add(algos
, hash
);
178 enumerator
->destroy(enumerator
);
180 rounds
->destroy(rounds
);
183 if (!algos
->count(algos
))
185 enumerator
= lib
->crypto
->create_hasher_enumerator(lib
->crypto
);
186 while (enumerator
->enumerate(enumerator
, &hash
, &plugin_name
))
188 if (hasher_algorithm_for_ikev2(hash
))
190 algos
->add(algos
, hash
);
193 enumerator
->destroy(enumerator
);
196 if (algos
->count(algos
))
198 writer
= bio_writer_create(0);
199 enumerator
= algos
->create_enumerator(algos
);
200 while (enumerator
->enumerate(enumerator
, &hash
))
202 writer
->write_uint16(writer
, hash
);
204 enumerator
->destroy(enumerator
);
205 message
->add_notify(message
, FALSE
, SIGNATURE_HASH_ALGORITHMS
,
206 writer
->get_buf(writer
));
207 writer
->destroy(writer
);
209 algos
->destroy(algos
);
213 * Store algorithms supported by other peer
215 static void handle_supported_hash_algorithms(private_ike_init_t
*this,
216 notify_payload_t
*notify
)
218 bio_reader_t
*reader
;
222 reader
= bio_reader_create(notify
->get_notification_data(notify
));
223 while (reader
->remaining(reader
) >= 2 && reader
->read_uint16(reader
, &algo
))
225 if (hasher_algorithm_for_ikev2(algo
))
227 this->keymat
->add_hash_algorithm(this->keymat
, algo
);
231 reader
->destroy(reader
);
235 this->ike_sa
->enable_extension(this->ike_sa
, EXT_SIGNATURE_AUTH
);
240 * build the payloads for the message
242 static bool build_payloads(private_ike_init_t
*this, message_t
*message
)
244 sa_payload_t
*sa_payload
;
245 ke_payload_t
*ke_payload
;
246 nonce_payload_t
*nonce_payload
;
247 linked_list_t
*proposal_list
;
249 proposal_t
*proposal
;
250 enumerator_t
*enumerator
;
252 id
= this->ike_sa
->get_id(this->ike_sa
);
254 this->config
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
258 proposal_list
= this->config
->get_proposals(this->config
);
261 /* include SPI of new IKE_SA when we are rekeying */
262 enumerator
= proposal_list
->create_enumerator(proposal_list
);
263 while (enumerator
->enumerate(enumerator
, (void**)&proposal
))
265 proposal
->set_spi(proposal
, id
->get_initiator_spi(id
));
267 enumerator
->destroy(enumerator
);
270 sa_payload
= sa_payload_create_from_proposals_v2(proposal_list
);
271 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
277 /* include SPI of new IKE_SA when we are rekeying */
278 this->proposal
->set_spi(this->proposal
, id
->get_responder_spi(id
));
280 sa_payload
= sa_payload_create_from_proposal_v2(this->proposal
);
282 message
->add_payload(message
, (payload_t
*)sa_payload
);
284 nonce_payload
= nonce_payload_create(PLV2_NONCE
);
285 nonce_payload
->set_nonce(nonce_payload
, this->my_nonce
);
286 ke_payload
= ke_payload_create_from_diffie_hellman(PLV2_KEY_EXCHANGE
,
290 DBG1(DBG_IKE
, "creating KE payload failed");
295 { /* payload order differs if we are rekeying */
296 message
->add_payload(message
, (payload_t
*)nonce_payload
);
297 message
->add_payload(message
, (payload_t
*)ke_payload
);
301 message
->add_payload(message
, (payload_t
*)ke_payload
);
302 message
->add_payload(message
, (payload_t
*)nonce_payload
);
305 /* negotiate fragmentation if we are not rekeying */
307 this->config
->fragmentation(this->config
) != FRAGMENTATION_NO
)
309 if (this->initiator
||
310 this->ike_sa
->supports_extension(this->ike_sa
,
311 EXT_IKE_FRAGMENTATION
))
313 message
->add_notify(message
, FALSE
, FRAGMENTATION_SUPPORTED
,
317 /* submit supported hash algorithms for signature authentication */
318 if (!this->old_sa
&& this->signature_authentication
)
320 if (this->initiator
||
321 this->ike_sa
->supports_extension(this->ike_sa
,
324 send_supported_hash_algorithms(this, message
);
327 /* notify other peer if we support redirection */
328 if (!this->old_sa
&& this->initiator
)
330 message
->add_notify(message
, FALSE
, REDIRECT_SUPPORTED
, chunk_empty
);
336 * Read payloads from message
338 static void process_payloads(private_ike_init_t
*this, message_t
*message
)
340 enumerator_t
*enumerator
;
342 ke_payload_t
*ke_payload
= NULL
;
344 enumerator
= message
->create_payload_enumerator(message
);
345 while (enumerator
->enumerate(enumerator
, &payload
))
347 switch (payload
->get_type(payload
))
349 case PLV2_SECURITY_ASSOCIATION
:
351 sa_payload_t
*sa_payload
= (sa_payload_t
*)payload
;
352 linked_list_t
*proposal_list
;
355 proposal_list
= sa_payload
->get_proposals(sa_payload
);
356 private = this->ike_sa
->supports_extension(this->ike_sa
,
358 this->proposal
= this->config
->select_proposal(this->config
,
359 proposal_list
, private);
362 charon
->bus
->alert(charon
->bus
, ALERT_PROPOSAL_MISMATCH_IKE
,
365 proposal_list
->destroy_offset(proposal_list
,
366 offsetof(proposal_t
, destroy
));
369 case PLV2_KEY_EXCHANGE
:
371 ke_payload
= (ke_payload_t
*)payload
;
373 this->dh_group
= ke_payload
->get_dh_group_number(ke_payload
);
378 nonce_payload_t
*nonce_payload
= (nonce_payload_t
*)payload
;
380 this->other_nonce
= nonce_payload
->get_nonce(nonce_payload
);
385 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
387 switch (notify
->get_notify_type(notify
))
389 case FRAGMENTATION_SUPPORTED
:
390 this->ike_sa
->enable_extension(this->ike_sa
,
391 EXT_IKE_FRAGMENTATION
);
393 case SIGNATURE_HASH_ALGORITHMS
:
394 if (this->signature_authentication
)
396 handle_supported_hash_algorithms(this, notify
);
399 case REDIRECT_SUPPORTED
:
400 this->ike_sa
->enable_extension(this->ike_sa
,
401 EXT_IKE_REDIRECTION
);
404 /* other notifies are handled elsewhere */
413 enumerator
->destroy(enumerator
);
415 if (ke_payload
&& this->proposal
&&
416 this->proposal
->has_dh_group(this->proposal
, this->dh_group
))
418 if (!this->initiator
)
420 this->dh
= this->keymat
->keymat
.create_dh(
421 &this->keymat
->keymat
, this->dh_group
);
425 this->dh_failed
= !this->dh
->set_other_public_value(this->dh
,
426 ke_payload
->get_key_exchange_data(ke_payload
));
431 METHOD(task_t
, build_i
, status_t
,
432 private_ike_init_t
*this, message_t
*message
)
434 this->config
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
435 DBG0(DBG_IKE
, "initiating IKE_SA %s[%d] to %H",
436 this->ike_sa
->get_name(this->ike_sa
),
437 this->ike_sa
->get_unique_id(this->ike_sa
),
438 this->ike_sa
->get_other_host(this->ike_sa
));
439 this->ike_sa
->set_state(this->ike_sa
, IKE_CONNECTING
);
441 if (this->retry
>= MAX_RETRIES
)
443 DBG1(DBG_IKE
, "giving up after %d retries", MAX_RETRIES
);
447 /* if the DH group is set via use_dh_group(), we already have a DH object */
450 this->dh_group
= this->config
->get_dh_group(this->config
);
451 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
455 DBG1(DBG_IKE
, "configured DH group %N not supported",
456 diffie_hellman_group_names
, this->dh_group
);
461 /* generate nonce only when we are trying the first time */
462 if (this->my_nonce
.ptr
== NULL
)
464 if (!generate_nonce(this))
470 if (this->cookie
.ptr
)
472 message
->add_notify(message
, FALSE
, COOKIE
, this->cookie
);
475 if (!build_payloads(this, message
))
482 chunk_t connect_id
= this->ike_sa
->get_connect_id(this->ike_sa
);
485 message
->add_notify(message
, FALSE
, ME_CONNECTID
, connect_id
);
493 METHOD(task_t
, process_r
, status_t
,
494 private_ike_init_t
*this, message_t
*message
)
496 this->config
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
497 DBG0(DBG_IKE
, "%H is initiating an IKE_SA", message
->get_source(message
));
498 this->ike_sa
->set_state(this->ike_sa
, IKE_CONNECTING
);
500 if (!generate_nonce(this))
507 notify_payload_t
*notify
= message
->get_notify(message
, ME_CONNECTID
);
510 chunk_t connect_id
= notify
->get_notification_data(notify
);
511 DBG2(DBG_IKE
, "received ME_CONNECTID %#B", &connect_id
);
512 charon
->connect_manager
->stop_checks(charon
->connect_manager
,
518 process_payloads(this, message
);
524 * Derive the keymat for the IKE_SA
526 static bool derive_keys(private_ike_init_t
*this,
527 chunk_t nonce_i
, chunk_t nonce_r
)
529 keymat_v2_t
*old_keymat
;
530 pseudo_random_function_t prf_alg
= PRF_UNDEFINED
;
531 chunk_t skd
= chunk_empty
;
534 id
= this->ike_sa
->get_id(this->ike_sa
);
537 /* rekeying: Include old SKd, use old PRF, apply SPI */
538 old_keymat
= (keymat_v2_t
*)this->old_sa
->get_keymat(this->old_sa
);
539 prf_alg
= old_keymat
->get_skd(old_keymat
, &skd
);
542 id
->set_responder_spi(id
, this->proposal
->get_spi(this->proposal
));
546 id
->set_initiator_spi(id
, this->proposal
->get_spi(this->proposal
));
549 if (!this->keymat
->derive_ike_keys(this->keymat
, this->proposal
, this->dh
,
550 nonce_i
, nonce_r
, id
, prf_alg
, skd
))
554 charon
->bus
->ike_keys(charon
->bus
, this->ike_sa
, this->dh
, chunk_empty
,
555 nonce_i
, nonce_r
, this->old_sa
, NULL
);
559 METHOD(task_t
, build_r
, status_t
,
560 private_ike_init_t
*this, message_t
*message
)
562 identification_t
*gateway
;
564 /* check if we have everything we need */
565 if (this->proposal
== NULL
||
566 this->other_nonce
.len
== 0 || this->my_nonce
.len
== 0)
568 DBG1(DBG_IKE
, "received proposals inacceptable");
569 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
572 this->ike_sa
->set_proposal(this->ike_sa
, this->proposal
);
574 /* check if we'd have to redirect the client */
575 if (this->ike_sa
->supports_extension(this->ike_sa
, EXT_IKE_REDIRECTION
) &&
576 charon
->redirect
->redirect_on_init(charon
->redirect
, this->ike_sa
,
581 DBG1(DBG_IKE
, "redirecting peer to %Y", gateway
);
582 data
= redirect_data_create(gateway
, this->other_nonce
);
583 message
->add_notify(message
, TRUE
, REDIRECT
, data
);
584 gateway
->destroy(gateway
);
589 if (this->dh
== NULL
||
590 !this->proposal
->has_dh_group(this->proposal
, this->dh_group
))
594 if (this->proposal
->get_algorithm(this->proposal
, DIFFIE_HELLMAN_GROUP
,
597 DBG1(DBG_IKE
, "DH group %N inacceptable, requesting %N",
598 diffie_hellman_group_names
, this->dh_group
,
599 diffie_hellman_group_names
, group
);
600 this->dh_group
= group
;
601 group
= htons(group
);
602 message
->add_notify(message
, FALSE
, INVALID_KE_PAYLOAD
,
603 chunk_from_thing(group
));
607 DBG1(DBG_IKE
, "no acceptable proposal found");
614 DBG1(DBG_IKE
, "applying DH public value failed");
615 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
619 if (!derive_keys(this, this->other_nonce
, this->my_nonce
))
621 DBG1(DBG_IKE
, "key derivation failed");
622 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
625 if (!build_payloads(this, message
))
627 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
634 * Raise alerts for received notify errors
636 static void raise_alerts(private_ike_init_t
*this, notify_type_t type
)
642 case NO_PROPOSAL_CHOSEN
:
643 list
= this->config
->get_proposals(this->config
);
644 charon
->bus
->alert(charon
->bus
, ALERT_PROPOSAL_MISMATCH_IKE
, list
);
645 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
652 METHOD(task_t
, process_i
, status_t
,
653 private_ike_init_t
*this, message_t
*message
)
655 enumerator_t
*enumerator
;
658 /* check for erroneous notifies */
659 enumerator
= message
->create_payload_enumerator(message
);
660 while (enumerator
->enumerate(enumerator
, &payload
))
662 if (payload
->get_type(payload
) == PLV2_NOTIFY
)
664 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
665 notify_type_t type
= notify
->get_notify_type(notify
);
669 case INVALID_KE_PAYLOAD
:
672 diffie_hellman_group_t bad_group
;
674 bad_group
= this->dh_group
;
675 data
= notify
->get_notification_data(notify
);
676 this->dh_group
= ntohs(*((u_int16_t
*)data
.ptr
));
677 DBG1(DBG_IKE
, "peer didn't accept DH group %N, "
678 "it requested %N", diffie_hellman_group_names
,
679 bad_group
, diffie_hellman_group_names
, this->dh_group
);
681 if (this->old_sa
== NULL
)
682 { /* reset the IKE_SA if we are not rekeying */
683 this->ike_sa
->reset(this->ike_sa
);
686 enumerator
->destroy(enumerator
);
690 case NAT_DETECTION_SOURCE_IP
:
691 case NAT_DETECTION_DESTINATION_IP
:
692 /* skip, handled in ike_natd_t */
694 case MULTIPLE_AUTH_SUPPORTED
:
695 /* handled in ike_auth_t */
699 chunk_free(&this->cookie
);
700 this->cookie
= chunk_clone(notify
->get_notification_data(notify
));
701 this->ike_sa
->reset(this->ike_sa
);
702 enumerator
->destroy(enumerator
);
703 DBG2(DBG_IKE
, "received %N notify", notify_type_names
, type
);
709 identification_t
*gateway
;
710 chunk_t data
, nonce
= chunk_empty
;
711 status_t status
= FAILED
;
713 data
= notify
->get_notification_data(notify
);
714 gateway
= redirect_data_parse(data
, &nonce
);
715 enumerator
->destroy(enumerator
);
716 if (!gateway
|| !chunk_equals(nonce
, this->my_nonce
))
718 DBG1(DBG_IKE
, "received invalid REDIRECT notify");
720 else if (this->ike_sa
->handle_redirect(this->ike_sa
,
733 DBG1(DBG_IKE
, "received %N notify error",
734 notify_type_names
, type
);
735 enumerator
->destroy(enumerator
);
736 raise_alerts(this, type
);
739 DBG2(DBG_IKE
, "received %N notify",
740 notify_type_names
, type
);
746 enumerator
->destroy(enumerator
);
748 process_payloads(this, message
);
750 /* check if we have everything */
751 if (this->proposal
== NULL
||
752 this->other_nonce
.len
== 0 || this->my_nonce
.len
== 0)
754 DBG1(DBG_IKE
, "peers proposal selection invalid");
757 this->ike_sa
->set_proposal(this->ike_sa
, this->proposal
);
759 if (this->dh
== NULL
||
760 !this->proposal
->has_dh_group(this->proposal
, this->dh_group
))
762 DBG1(DBG_IKE
, "peer DH group selection invalid");
768 DBG1(DBG_IKE
, "applying DH public value failed");
772 if (!derive_keys(this, this->my_nonce
, this->other_nonce
))
774 DBG1(DBG_IKE
, "key derivation failed");
780 METHOD(task_t
, get_type
, task_type_t
,
781 private_ike_init_t
*this)
783 return TASK_IKE_INIT
;
786 METHOD(task_t
, migrate
, void,
787 private_ike_init_t
*this, ike_sa_t
*ike_sa
)
789 DESTROY_IF(this->proposal
);
790 chunk_free(&this->other_nonce
);
792 this->ike_sa
= ike_sa
;
793 this->keymat
= (keymat_v2_t
*)ike_sa
->get_keymat(ike_sa
);
794 this->proposal
= NULL
;
795 this->dh_failed
= FALSE
;
796 if (this->dh
&& this->dh
->get_dh_group(this->dh
) != this->dh_group
)
797 { /* reset DH value only if group changed (INVALID_KE_PAYLOAD) */
798 this->dh
->destroy(this->dh
);
799 this->dh
= this->keymat
->keymat
.create_dh(&this->keymat
->keymat
,
804 METHOD(task_t
, destroy
, void,
805 private_ike_init_t
*this)
807 DESTROY_IF(this->dh
);
808 DESTROY_IF(this->proposal
);
809 DESTROY_IF(this->nonceg
);
810 chunk_free(&this->my_nonce
);
811 chunk_free(&this->other_nonce
);
812 chunk_free(&this->cookie
);
816 METHOD(ike_init_t
, get_lower_nonce
, chunk_t
,
817 private_ike_init_t
*this)
819 if (memcmp(this->my_nonce
.ptr
, this->other_nonce
.ptr
,
820 min(this->my_nonce
.len
, this->other_nonce
.len
)) < 0)
822 return this->my_nonce
;
826 return this->other_nonce
;
831 * Described in header.
833 ike_init_t
*ike_init_create(ike_sa_t
*ike_sa
, bool initiator
, ike_sa_t
*old_sa
)
835 private_ike_init_t
*this;
840 .get_type
= _get_type
,
844 .get_lower_nonce
= _get_lower_nonce
,
847 .initiator
= initiator
,
848 .dh_group
= MODP_NONE
,
849 .keymat
= (keymat_v2_t
*)ike_sa
->get_keymat(ike_sa
),
851 .signature_authentication
= lib
->settings
->get_bool(lib
->settings
,
852 "%s.signature_authentication", TRUE
, lib
->ns
),
854 this->nonceg
= this->keymat
->keymat
.create_nonce_gen(&this->keymat
->keymat
);
858 this->public.task
.build
= _build_i
;
859 this->public.task
.process
= _process_i
;
863 this->public.task
.build
= _build_r
;
864 this->public.task
.process
= _process_r
;
866 return &this->public;