Remove keymat proxy from TKM keymat
[strongswan.git] / src / charon-tkm / src / tkm / tkm_keymat.c
1 /*
2 * Copyrigth (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
5 *
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>.
10 *
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
14 * for more details.
15 */
16
17 #include <daemon.h>
18 #include <tkm/constants.h>
19 #include <tkm/client.h>
20
21 #include "tkm.h"
22 #include "tkm_types.h"
23 #include "tkm_utils.h"
24 #include "tkm_diffie_hellman.h"
25 #include "tkm_keymat.h"
26
27 typedef struct private_tkm_keymat_t private_tkm_keymat_t;
28
29 /**
30 * Private data of a keymat_t object.
31 */
32 struct private_tkm_keymat_t {
33
34 /**
35 * Public tkm_keymat_t interface.
36 */
37 tkm_keymat_t public;
38
39 /**
40 * IKE_SA Role, initiator or responder.
41 */
42 bool initiator;
43
44 /**
45 * Inbound AEAD.
46 */
47 aead_t *aead_in;
48
49 /**
50 * Outbound AEAD.
51 */
52 aead_t *aead_out;
53
54 /**
55 * ISA context id.
56 */
57 isa_id_type isa_ctx_id;
58
59 /**
60 * AE context id.
61 */
62 ae_id_type ae_ctx_id;
63
64 /**
65 * AUTH payload chunk.
66 */
67 chunk_t auth_payload;
68
69 };
70
71 /**
72 * Create AEAD transforms from given key chunks.
73 *
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
84 */
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)
90 {
91 *in = *out = NULL;
92
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)
96 {
97 DBG1(DBG_IKE, "%N %N not supported!",
98 transform_type_names, INTEGRITY_ALGORITHM,
99 integrity_algorithm_names, int_alg);
100 return;
101 }
102 crypter_t * const crypter_i = lib->crypto->create_crypter(lib->crypto,
103 enc_alg, key_size);
104 crypter_t * const crypter_r = lib->crypto->create_crypter(lib->crypto,
105 enc_alg, key_size);
106 if (crypter_i == NULL || crypter_r == NULL)
107 {
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);
113 return;
114 }
115
116 DBG4(DBG_IKE, "Sk_ai %B", sk_ai);
117 if (!signer_i->set_key(signer_i, *sk_ai))
118 {
119 return;
120 }
121 DBG4(DBG_IKE, "Sk_ar %B", sk_ar);
122 if (!signer_r->set_key(signer_r, *sk_ar))
123 {
124 return;
125 }
126 DBG4(DBG_IKE, "Sk_ei %B", sk_ei);
127 if (!crypter_i->set_key(crypter_i, *sk_ei))
128 {
129 return;
130 }
131 DBG4(DBG_IKE, "Sk_er %B", sk_er);
132 if (!crypter_r->set_key(crypter_r, *sk_er))
133 {
134 return;
135 }
136
137 if (initiator)
138 {
139 *in = aead_create(crypter_r, signer_r);
140 *out = aead_create(crypter_i, signer_i);
141 }
142 else
143 {
144 *in = aead_create(crypter_i, signer_i);
145 *out = aead_create(crypter_r, signer_r);
146 }
147 }
148
149 METHOD(keymat_t, get_version, ike_version_t,
150 private_tkm_keymat_t *this)
151 {
152 return IKEV2;
153 }
154
155 METHOD(keymat_t, create_dh, diffie_hellman_t*,
156 private_tkm_keymat_t *this, diffie_hellman_group_t group)
157 {
158 return lib->crypto->create_dh(lib->crypto, group);
159 }
160
161 METHOD(keymat_t, create_nonce_gen, nonce_gen_t*,
162 private_tkm_keymat_t *this)
163 {
164 return lib->crypto->create_nonce_gen(lib->crypto);
165 }
166
167 METHOD(tkm_keymat_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)
171 {
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))
175 {
176 DBG1(DBG_IKE, "no %N selected", transform_type_names,
177 ENCRYPTION_ALGORITHM);
178 return FALSE;
179 }
180 if (encryption_algorithm_is_aead(enc_alg))
181 {
182 DBG1(DBG_IKE, "AEAD algorithm %N not supported",
183 encryption_algorithm_names, enc_alg);
184 return FALSE;
185 }
186 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &int_alg, NULL))
187 {
188 DBG1(DBG_IKE, "no %N selected", transform_type_names,
189 INTEGRITY_ALGORITHM);
190 return FALSE;
191 }
192 if (!(enc_alg == ENCR_AES_CBC && key_size == 256 &&
193 int_alg == AUTH_HMAC_SHA2_512_256))
194 {
195 DBG1(DBG_IKE, "the TKM only supports aes256-sha512 at the moment, please"
196 " update your configuration");
197 return FALSE;
198 }
199
200 DBG2(DBG_IKE, "using %N for encryption, %N for integrity",
201 encryption_algorithm_names, enc_alg,
202 integrity_algorithm_names, int_alg);
203
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);
207 if (!nc_id)
208 {
209 DBG1(DBG_IKE, "unable to acquire context id for nonce");
210 return FALSE;
211 }
212
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);
216
217 nonce_type nonce_rem;
218 u_int64_t spi_loc, spi_rem;
219
220 if (this->initiator)
221 {
222 chunk_to_sequence(&nonce_r, &nonce_rem);
223 spi_loc = id->get_initiator_spi(id);
224 spi_rem = id->get_responder_spi(id);
225 }
226 else
227 {
228 chunk_to_sequence(&nonce_i, &nonce_rem);
229 spi_loc = id->get_responder_spi(id);
230 spi_rem = id->get_initiator_spi(id);
231 }
232
233 key_type sk_ai, sk_ar, sk_ei, sk_er;
234 DBG1(DBG_IKE, "deriving IKE keys (nc: %llu, dh: %llu, spi_loc: %llx, "
235 "spi_rem: %llx)", nc_id, dh_id, spi_loc, spi_rem);
236 /* Fake some data for now */
237 if (ike_isa_create(this->isa_ctx_id, this->ae_ctx_id, 1, dh_id, nc_id,
238 nonce_rem, 1, spi_loc, spi_rem,
239 &sk_ai, &sk_ar, &sk_ei, &sk_er) != TKM_OK)
240 {
241 DBG1(DBG_IKE, "key derivation failed");
242 return FALSE;
243 }
244
245 chunk_t c_ai, c_ar, c_ei, c_er;
246 sequence_to_chunk(sk_ai.data, sk_ai.size, &c_ai);
247 sequence_to_chunk(sk_ar.data, sk_ar.size, &c_ar);
248 sequence_to_chunk(sk_ei.data, sk_ei.size, &c_ei);
249 sequence_to_chunk(sk_er.data, sk_er.size, &c_er);
250
251 aead_create_from_keys(&this->aead_in, &this->aead_out,
252 &c_ai, &c_ar, &c_ei, &c_er,
253 enc_alg, int_alg, key_size / 8, this->initiator);
254
255 chunk_clear(&c_ai);
256 chunk_clear(&c_ar);
257 chunk_clear(&c_ei);
258 chunk_clear(&c_er);
259
260 if (!this->aead_in || !this->aead_out)
261 {
262 DBG1(DBG_IKE, "could not initialize AEAD transforms");
263 return FALSE;
264 }
265
266 /* TODO: Add failure handler (see keymat_v2.c) */
267
268 tkm->chunk_map->remove(tkm->chunk_map, nonce);
269 if (ike_nc_reset(nc_id) != TKM_OK)
270 {
271 DBG1(DBG_IKE, "failed to reset nonce context %llu", nc_id);
272 }
273 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, nc_id);
274
275 return TRUE;
276 }
277
278 METHOD(tkm_keymat_t, derive_child_keys, bool,
279 private_tkm_keymat_t *this, proposal_t *proposal, diffie_hellman_t *dh,
280 chunk_t nonce_i, chunk_t nonce_r, chunk_t *encr_i, chunk_t *integ_i,
281 chunk_t *encr_r, chunk_t *integ_r)
282 {
283 DBG1(DBG_CHD, "deriving child keys");
284 *encr_i = chunk_alloc(sizeof(esa_info_t));
285 (*(esa_info_t*)(encr_i->ptr)).isa_id = this->isa_ctx_id;
286 return TRUE;
287 }
288
289 METHOD(keymat_t, get_aead, aead_t*,
290 private_tkm_keymat_t *this, bool in)
291 {
292 return in ? this->aead_in : this->aead_out;
293 }
294
295 METHOD(tkm_keymat_t, get_auth_octets, bool,
296 private_tkm_keymat_t *this, bool verify, chunk_t ike_sa_init,
297 chunk_t nonce, identification_t *id, char reserved[3], chunk_t *octets)
298 {
299 DBG1(DBG_IKE, "returning auth octets");
300 *octets = chunk_empty;
301 return TRUE;
302 }
303
304 METHOD(tkm_keymat_t, get_skd, pseudo_random_function_t,
305 private_tkm_keymat_t *this, chunk_t *skd)
306 {
307 DBG1(DBG_IKE, "returning skd");
308 *skd = chunk_empty;
309 return PRF_HMAC_SHA2_512;
310 }
311
312 METHOD(tkm_keymat_t, get_psk_sig, bool,
313 private_tkm_keymat_t *this, bool verify, chunk_t ike_sa_init, chunk_t nonce,
314 chunk_t secret, identification_t *id, char reserved[3], chunk_t *sig)
315 {
316 DBG1(DBG_IKE, "returning %s PSK signature", verify ? "remote" : "local");
317
318 signature_type signature;
319 init_message_type msg;
320 chunk_to_sequence(&ike_sa_init, &msg);
321
322 chunk_t idx_chunk, chunk = chunk_alloca(4);
323 chunk.ptr[0] = id->get_type(id);
324 memcpy(chunk.ptr + 1, reserved, 3);
325 idx_chunk = chunk_cata("cc", chunk, id->get_encoding(id));
326 idx_type idx;
327 chunk_to_sequence(&idx_chunk, &idx);
328
329 if (ike_isa_sign_psk(this->isa_ctx_id, msg, idx, verify == TRUE, &signature)
330 != TKM_OK)
331 {
332 DBG1(DBG_IKE, "get %s PSK signature failed", verify ?
333 "remote" : "local");
334 return FALSE;
335 }
336
337 sequence_to_chunk(&signature.data[0], signature.size, sig);
338 return TRUE;
339 }
340
341 METHOD(keymat_t, destroy, void,
342 private_tkm_keymat_t *this)
343 {
344 if (ike_isa_reset(this->isa_ctx_id) != TKM_OK)
345 {
346 DBG1(DBG_IKE, "failed to reset ISA context %d", this->isa_ctx_id);
347 }
348 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_ISA, this->isa_ctx_id);
349
350 DESTROY_IF(this->aead_in);
351 DESTROY_IF(this->aead_out);
352 chunk_free(&this->auth_payload);
353 free(this);
354 }
355
356 METHOD(tkm_keymat_t, get_isa_id, isa_id_type,
357 private_tkm_keymat_t *this)
358 {
359 return this->isa_ctx_id;
360 }
361
362 METHOD(tkm_keymat_t, set_auth_payload, void,
363 private_tkm_keymat_t *this, const chunk_t * const payload)
364 {
365 this->auth_payload = chunk_clone(*payload);
366 }
367
368 METHOD(tkm_keymat_t, get_auth_payload, chunk_t*,
369 private_tkm_keymat_t *this)
370 {
371 return &this->auth_payload;
372 }
373
374 /**
375 * See header.
376 */
377 tkm_keymat_t *tkm_keymat_create(bool initiator)
378 {
379 private_tkm_keymat_t *this;
380
381 INIT(this,
382 .public = {
383 .keymat = {
384 .get_version = _get_version,
385 .create_dh = _create_dh,
386 .create_nonce_gen = _create_nonce_gen,
387 .get_aead = _get_aead,
388 .destroy = _destroy,
389 },
390 .derive_ike_keys = _derive_ike_keys,
391 .derive_child_keys = _derive_child_keys,
392 .get_skd = _get_skd,
393 .get_auth_octets = _get_auth_octets,
394 .get_psk_sig = _get_psk_sig,
395 .get_isa_id = _get_isa_id,
396 .set_auth_payload = _set_auth_payload,
397 .get_auth_payload = _get_auth_payload,
398 },
399 .initiator = initiator,
400 .isa_ctx_id = tkm->idmgr->acquire_id(tkm->idmgr, TKM_CTX_ISA),
401 .ae_ctx_id = tkm->idmgr->acquire_id(tkm->idmgr, TKM_CTX_AE),
402 .auth_payload = chunk_empty,
403 );
404
405 if (!this->isa_ctx_id || !this->ae_ctx_id)
406 {
407 free(this);
408 return NULL;
409 }
410
411 return &this->public;
412 }