2 * Copyrigth (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include <tkm/constants.h>
19 #include <tkm/client.h>
22 #include "tkm_types.h"
23 #include "tkm_utils.h"
24 #include "tkm_diffie_hellman.h"
25 #include "tkm_keymat.h"
27 typedef struct private_tkm_keymat_t private_tkm_keymat_t
;
30 * Private data of a keymat_t object.
32 struct private_tkm_keymat_t
{
35 * Public tkm_keymat_t interface.
40 * IKE_SA Role, initiator or responder.
57 isa_id_type isa_ctx_id
;
72 * Create AEAD transforms from given key chunks.
74 * @param in inbound AEAD transform to allocate, NULL if failed
75 * @param out outbound AEAD transform to allocate, NULL if failed
76 * @param sk_ai SK_ai key chunk
77 * @param sk_ar SK_ar key chunk
78 * @param sk_ei SK_ei key chunk
79 * @param sk_er SK_er key chunk
80 * @param enc_alg encryption algorithm to use
81 * @param int_alg integrity algorithm to use
82 * @param key_size encryption key size in bytes
83 * @param initiator TRUE if initiator
85 static void aead_create_from_keys(aead_t
**in
, aead_t
**out
,
86 const chunk_t
* const sk_ai
, const chunk_t
* const sk_ar
,
87 const chunk_t
* const sk_ei
, const chunk_t
* const sk_er
,
88 const u_int16_t enc_alg
, const u_int16_t int_alg
,
89 const u_int16_t key_size
, bool initiator
)
93 signer_t
* const signer_i
= lib
->crypto
->create_signer(lib
->crypto
, int_alg
);
94 signer_t
* const signer_r
= lib
->crypto
->create_signer(lib
->crypto
, int_alg
);
95 if (signer_i
== NULL
|| signer_r
== NULL
)
97 DBG1(DBG_IKE
, "%N %N not supported!",
98 transform_type_names
, INTEGRITY_ALGORITHM
,
99 integrity_algorithm_names
, int_alg
);
102 crypter_t
* const crypter_i
= lib
->crypto
->create_crypter(lib
->crypto
,
104 crypter_t
* const crypter_r
= lib
->crypto
->create_crypter(lib
->crypto
,
106 if (crypter_i
== NULL
|| crypter_r
== NULL
)
108 signer_i
->destroy(signer_i
);
109 signer_r
->destroy(signer_r
);
110 DBG1(DBG_IKE
, "%N %N (key size %d) not supported!",
111 transform_type_names
, ENCRYPTION_ALGORITHM
,
112 encryption_algorithm_names
, enc_alg
, key_size
);
116 DBG4(DBG_IKE
, "Sk_ai %B", sk_ai
);
117 if (!signer_i
->set_key(signer_i
, *sk_ai
))
121 DBG4(DBG_IKE
, "Sk_ar %B", sk_ar
);
122 if (!signer_r
->set_key(signer_r
, *sk_ar
))
126 DBG4(DBG_IKE
, "Sk_ei %B", sk_ei
);
127 if (!crypter_i
->set_key(crypter_i
, *sk_ei
))
131 DBG4(DBG_IKE
, "Sk_er %B", sk_er
);
132 if (!crypter_r
->set_key(crypter_r
, *sk_er
))
139 *in
= aead_create(crypter_r
, signer_r
);
140 *out
= aead_create(crypter_i
, signer_i
);
144 *in
= aead_create(crypter_i
, signer_i
);
145 *out
= aead_create(crypter_r
, signer_r
);
149 METHOD(keymat_t
, get_version
, ike_version_t
,
150 private_tkm_keymat_t
*this)
155 METHOD(keymat_t
, create_dh
, diffie_hellman_t
*,
156 private_tkm_keymat_t
*this, diffie_hellman_group_t group
)
158 return lib
->crypto
->create_dh(lib
->crypto
, group
);
161 METHOD(keymat_t
, create_nonce_gen
, nonce_gen_t
*,
162 private_tkm_keymat_t
*this)
164 return lib
->crypto
->create_nonce_gen(lib
->crypto
);
167 METHOD(keymat_v2_t
, derive_ike_keys
, bool,
168 private_tkm_keymat_t
*this, proposal_t
*proposal
, diffie_hellman_t
*dh
,
169 chunk_t nonce_i
, chunk_t nonce_r
, ike_sa_id_t
*id
,
170 pseudo_random_function_t rekey_function
, chunk_t rekey_skd
)
172 /* Check encryption and integrity algorithms */
173 u_int16_t enc_alg
, int_alg
, key_size
;
174 if (!proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &enc_alg
, &key_size
))
176 DBG1(DBG_IKE
, "no %N selected", transform_type_names
,
177 ENCRYPTION_ALGORITHM
);
180 if (encryption_algorithm_is_aead(enc_alg
))
182 DBG1(DBG_IKE
, "AEAD algorithm %N not supported",
183 encryption_algorithm_names
, enc_alg
);
186 if (!proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &int_alg
, NULL
))
188 DBG1(DBG_IKE
, "no %N selected", transform_type_names
,
189 INTEGRITY_ALGORITHM
);
192 if (!(enc_alg
== ENCR_AES_CBC
&& key_size
== 256 &&
193 int_alg
== AUTH_HMAC_SHA2_512_256
))
195 DBG1(DBG_IKE
, "the TKM only supports aes256-sha512 at the moment, please"
196 " update your configuration");
200 DBG2(DBG_IKE
, "using %N for encryption, %N for integrity",
201 encryption_algorithm_names
, enc_alg
,
202 integrity_algorithm_names
, int_alg
);
204 /* Acquire nonce context id */
205 chunk_t
* const nonce
= this->initiator ?
&nonce_i
: &nonce_r
;
206 const uint64_t nc_id
= tkm
->chunk_map
->get_id(tkm
->chunk_map
, nonce
);
209 DBG1(DBG_IKE
, "unable to acquire context id for nonce");
213 /* Get DH context id */
214 tkm_diffie_hellman_t
* const tkm_dh
= (tkm_diffie_hellman_t
*)dh
;
215 const dh_id_type dh_id
= tkm_dh
->get_id(tkm_dh
);
217 nonce_type nonce_rem
;
218 u_int64_t spi_loc
, spi_rem
;
222 chunk_to_sequence(&nonce_r
, &nonce_rem
);
223 spi_loc
= id
->get_initiator_spi(id
);
224 spi_rem
= id
->get_responder_spi(id
);
228 chunk_to_sequence(&nonce_i
, &nonce_rem
);
229 spi_loc
= id
->get_responder_spi(id
);
230 spi_rem
= id
->get_initiator_spi(id
);
234 key_type sk_ai
, sk_ar
, sk_ei
, sk_er
;
235 if (rekey_function
== PRF_UNDEFINED
)
237 this->ae_ctx_id
= tkm
->idmgr
->acquire_id(tkm
->idmgr
, TKM_CTX_AE
);
238 if (!this->ae_ctx_id
)
240 DBG1(DBG_IKE
, "unable to acquire ae context id");
243 DBG1(DBG_IKE
, "deriving IKE keys (nc: %llu, dh: %llu, spi_loc: %llx, "
244 "spi_rem: %llx)", nc_id
, dh_id
, spi_loc
, spi_rem
);
245 res
= ike_isa_create(this->isa_ctx_id
, this->ae_ctx_id
, 1, dh_id
, nc_id
,
246 nonce_rem
, this->initiator
, spi_loc
, spi_rem
,
247 &sk_ai
, &sk_ar
, &sk_ei
, &sk_er
);
251 if (rekey_skd
.ptr
== NULL
|| rekey_skd
.len
!= sizeof(isa_info_t
))
253 DBG1(DBG_IKE
, "unable to retrieve parent isa info");
256 const isa_info_t isa_info
= *((isa_info_t
*)(rekey_skd
.ptr
));
257 DBG1(DBG_IKE
, "deriving IKE keys (parent_isa: %llu, ae: %llu, nc: %llu,"
258 "dh: %llu, spi_loc: %llx, spi_rem: %llx)", isa_info
.parent_isa_id
,
259 isa_info
.ae_id
, nc_id
, dh_id
, spi_loc
, spi_rem
);
260 this->ae_ctx_id
= isa_info
.ae_id
;
261 res
= ike_isa_create_child(this->isa_ctx_id
, isa_info
.parent_isa_id
, 1,
262 dh_id
, nc_id
, nonce_rem
, this->initiator
,
263 spi_loc
, spi_rem
, &sk_ai
, &sk_ar
, &sk_ei
,
265 chunk_free(&rekey_skd
);
270 DBG1(DBG_IKE
, "key derivation failed (isa: %llu)", this->isa_ctx_id
);
274 chunk_t c_ai
, c_ar
, c_ei
, c_er
;
275 sequence_to_chunk(sk_ai
.data
, sk_ai
.size
, &c_ai
);
276 sequence_to_chunk(sk_ar
.data
, sk_ar
.size
, &c_ar
);
277 sequence_to_chunk(sk_ei
.data
, sk_ei
.size
, &c_ei
);
278 sequence_to_chunk(sk_er
.data
, sk_er
.size
, &c_er
);
280 aead_create_from_keys(&this->aead_in
, &this->aead_out
,
281 &c_ai
, &c_ar
, &c_ei
, &c_er
,
282 enc_alg
, int_alg
, key_size
/ 8, this->initiator
);
289 if (!this->aead_in
|| !this->aead_out
)
291 DBG1(DBG_IKE
, "could not initialize AEAD transforms");
295 /* TODO: Add failure handler (see keymat_v2.c) */
297 tkm
->chunk_map
->remove(tkm
->chunk_map
, nonce
);
298 if (ike_nc_reset(nc_id
) != TKM_OK
)
300 DBG1(DBG_IKE
, "failed to reset nonce context %llu", nc_id
);
302 tkm
->idmgr
->release_id(tkm
->idmgr
, TKM_CTX_NONCE
, nc_id
);
307 METHOD(keymat_v2_t
, derive_child_keys
, bool,
308 private_tkm_keymat_t
*this, proposal_t
*proposal
, diffie_hellman_t
*dh
,
309 chunk_t nonce_i
, chunk_t nonce_r
, chunk_t
*encr_i
, chunk_t
*integ_i
,
310 chunk_t
*encr_r
, chunk_t
*integ_r
)
312 esa_info_t
*esa_info_i
, *esa_info_r
;
314 dh_id_type dh_id
= 0;
317 dh_id
= ((tkm_diffie_hellman_t
*)dh
)->get_id((tkm_diffie_hellman_t
*)dh
);
321 .isa_id
= this->isa_ctx_id
,
322 .spi_r
= proposal
->get_spi(proposal
),
323 .nonce_i
= chunk_clone(nonce_i
),
324 .nonce_r
= chunk_clone(nonce_r
),
330 .isa_id
= this->isa_ctx_id
,
331 .spi_r
= proposal
->get_spi(proposal
),
332 .nonce_i
= chunk_clone(nonce_i
),
333 .nonce_r
= chunk_clone(nonce_r
),
338 DBG1(DBG_CHD
, "passing on esa info (isa: %llu, spi_r: %x, dh_id: %llu)",
339 esa_info_i
->isa_id
, ntohl(esa_info_i
->spi_r
), esa_info_i
->dh_id
);
341 /* store ESA info in encr_i/r, which is passed to add_sa */
342 *encr_i
= chunk_create((u_char
*)esa_info_i
, sizeof(esa_info_t
));
343 *encr_r
= chunk_create((u_char
*)esa_info_r
, sizeof(esa_info_t
));
344 *integ_i
= chunk_empty
;
345 *integ_r
= chunk_empty
;
350 METHOD(keymat_t
, get_aead
, aead_t
*,
351 private_tkm_keymat_t
*this, bool in
)
353 return in ?
this->aead_in
: this->aead_out
;
356 METHOD(keymat_v2_t
, get_auth_octets
, bool,
357 private_tkm_keymat_t
*this, bool verify
, chunk_t ike_sa_init
,
358 chunk_t nonce
, identification_t
*id
, char reserved
[3], chunk_t
*octets
)
360 DBG1(DBG_IKE
, "returning auth octets");
361 *octets
= chunk_empty
;
365 METHOD(keymat_v2_t
, get_skd
, pseudo_random_function_t
,
366 private_tkm_keymat_t
*this, chunk_t
*skd
)
368 isa_info_t
*isa_info
;
371 .parent_isa_id
= this->isa_ctx_id
,
372 .ae_id
= this->ae_ctx_id
,
375 *skd
= chunk_create((u_char
*)isa_info
, sizeof(isa_info_t
));
378 * remove ae context id, since control has now been handed over to the new
382 return PRF_HMAC_SHA2_512
;
385 METHOD(keymat_v2_t
, get_psk_sig
, bool,
386 private_tkm_keymat_t
*this, bool verify
, chunk_t ike_sa_init
, chunk_t nonce
,
387 chunk_t secret
, identification_t
*id
, char reserved
[3], chunk_t
*sig
)
389 DBG1(DBG_IKE
, "returning %s PSK signature", verify ?
"remote" : "local");
391 signature_type signature
;
392 init_message_type msg
;
393 chunk_to_sequence(&ike_sa_init
, &msg
);
395 chunk_t idx_chunk
, chunk
= chunk_alloca(4);
396 chunk
.ptr
[0] = id
->get_type(id
);
397 memcpy(chunk
.ptr
+ 1, reserved
, 3);
398 idx_chunk
= chunk_cata("cc", chunk
, id
->get_encoding(id
));
400 chunk_to_sequence(&idx_chunk
, &idx
);
402 if (ike_isa_sign_psk(this->isa_ctx_id
, msg
, idx
, verify
== TRUE
, &signature
)
405 DBG1(DBG_IKE
, "get %s PSK signature failed", verify ?
410 sequence_to_chunk(&signature
.data
[0], signature
.size
, sig
);
414 METHOD(keymat_t
, destroy
, void,
415 private_tkm_keymat_t
*this)
417 if (ike_isa_reset(this->isa_ctx_id
) != TKM_OK
)
419 DBG1(DBG_IKE
, "failed to reset ISA context %d", this->isa_ctx_id
);
421 tkm
->idmgr
->release_id(tkm
->idmgr
, TKM_CTX_ISA
, this->isa_ctx_id
);
422 /* only reset ae context if set */
423 if (this->ae_ctx_id
!= 0)
425 if (ike_ae_reset(this->ae_ctx_id
) != TKM_OK
)
427 DBG1(DBG_IKE
, "failed to reset AE context %d", this->ae_ctx_id
);
429 tkm
->idmgr
->release_id(tkm
->idmgr
, TKM_CTX_AE
, this->ae_ctx_id
);
432 DESTROY_IF(this->aead_in
);
433 DESTROY_IF(this->aead_out
);
434 chunk_free(&this->auth_payload
);
438 METHOD(tkm_keymat_t
, get_isa_id
, isa_id_type
,
439 private_tkm_keymat_t
*this)
441 return this->isa_ctx_id
;
444 METHOD(tkm_keymat_t
, set_auth_payload
, void,
445 private_tkm_keymat_t
*this, const chunk_t
* const payload
)
447 this->auth_payload
= chunk_clone(*payload
);
450 METHOD(tkm_keymat_t
, get_auth_payload
, chunk_t
*,
451 private_tkm_keymat_t
*this)
453 return &this->auth_payload
;
459 tkm_keymat_t
*tkm_keymat_create(bool initiator
)
461 private_tkm_keymat_t
*this;
467 .get_version
= _get_version
,
468 .create_dh
= _create_dh
,
469 .create_nonce_gen
= _create_nonce_gen
,
470 .get_aead
= _get_aead
,
473 .derive_ike_keys
= _derive_ike_keys
,
474 .derive_child_keys
= _derive_child_keys
,
476 .get_auth_octets
= _get_auth_octets
,
477 .get_psk_sig
= _get_psk_sig
,
479 .get_isa_id
= _get_isa_id
,
480 .set_auth_payload
= _set_auth_payload
,
481 .get_auth_payload
= _get_auth_payload
,
483 .initiator
= initiator
,
484 .isa_ctx_id
= tkm
->idmgr
->acquire_id(tkm
->idmgr
, TKM_CTX_ISA
),
486 .auth_payload
= chunk_empty
,
489 if (!this->isa_ctx_id
)
495 return &this->public;