Factor out AEAD transform creation
[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 <sa/ikev2/keymat_v2.h>
19 #include <tkm/constants.h>
20 #include <tkm/client.h>
21
22 #include "tkm.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 * IKEv2 keymat proxy (will be removed).
41 */
42 keymat_v2_t *proxy;
43
44 /**
45 * IKE_SA Role, initiator or responder.
46 */
47 bool initiator;
48
49 /**
50 * Inbound AEAD.
51 */
52 aead_t *aead_in;
53
54 /**
55 * Outbound AEAD.
56 */
57 aead_t *aead_out;
58
59 };
60
61 /**
62 * Create AEAD transforms from given key chunks.
63 *
64 * @param in inbound AEAD transform to allocate, NULL if failed
65 * @param out outbound AEAD transform to allocate, NULL if failed
66 * @param sk_ai SK_ai key chunk
67 * @param sk_ar SK_ar key chunk
68 * @param sk_ei SK_ei key chunk
69 * @param sk_er SK_er key chunk
70 * @param enc_alg encryption algorithm to use
71 * @param int_alg integrity algorithm to use
72 * @param key_size encryption key size in bytes
73 * @param initiator TRUE if initiator
74 */
75 static void aead_create_from_keys(aead_t **in, aead_t **out,
76 const chunk_t * const sk_ai, const chunk_t * const sk_ar,
77 const chunk_t * const sk_ei, const chunk_t * const sk_er,
78 const u_int16_t enc_alg, const u_int16_t int_alg,
79 const u_int16_t key_size, bool initiator)
80 {
81 *in = *out = NULL;
82
83 signer_t * const signer_i = lib->crypto->create_signer(lib->crypto, int_alg);
84 signer_t * const signer_r = lib->crypto->create_signer(lib->crypto, int_alg);
85 if (signer_i == NULL || signer_r == NULL)
86 {
87 DBG1(DBG_IKE, "%N %N not supported!",
88 transform_type_names, INTEGRITY_ALGORITHM,
89 integrity_algorithm_names, int_alg);
90 return;
91 }
92 crypter_t * const crypter_i = lib->crypto->create_crypter(lib->crypto,
93 enc_alg, key_size);
94 crypter_t * const crypter_r = lib->crypto->create_crypter(lib->crypto,
95 enc_alg, key_size);
96 if (crypter_i == NULL || crypter_r == NULL)
97 {
98 signer_i->destroy(signer_i);
99 signer_r->destroy(signer_r);
100 DBG1(DBG_IKE, "%N %N (key size %d) not supported!",
101 transform_type_names, ENCRYPTION_ALGORITHM,
102 encryption_algorithm_names, enc_alg, key_size);
103 return;
104 }
105
106 DBG4(DBG_IKE, "Sk_ai %B", sk_ai);
107 if (!signer_i->set_key(signer_i, *sk_ai))
108 {
109 return;
110 }
111 DBG4(DBG_IKE, "Sk_ar %B", sk_ar);
112 if (!signer_r->set_key(signer_r, *sk_ar))
113 {
114 return;
115 }
116 DBG4(DBG_IKE, "Sk_ei %B", sk_ei);
117 if (!crypter_i->set_key(crypter_i, *sk_ei))
118 {
119 return;
120 }
121 DBG4(DBG_IKE, "Sk_er %B", sk_er);
122 if (!crypter_r->set_key(crypter_r, *sk_er))
123 {
124 return;
125 }
126
127 if (initiator)
128 {
129 *in = aead_create(crypter_r, signer_r);
130 *out = aead_create(crypter_i, signer_i);
131 }
132 else
133 {
134 *in = aead_create(crypter_i, signer_i);
135 *out = aead_create(crypter_r, signer_r);
136 }
137 }
138
139 METHOD(keymat_t, get_version, ike_version_t,
140 private_tkm_keymat_t *this)
141 {
142 return IKEV2;
143 }
144
145 METHOD(keymat_t, create_dh, diffie_hellman_t*,
146 private_tkm_keymat_t *this, diffie_hellman_group_t group)
147 {
148 return lib->crypto->create_dh(lib->crypto, group);
149 }
150
151 METHOD(keymat_t, create_nonce_gen, nonce_gen_t*,
152 private_tkm_keymat_t *this)
153 {
154 return lib->crypto->create_nonce_gen(lib->crypto);
155 }
156
157 METHOD(tkm_keymat_t, derive_ike_keys, bool,
158 private_tkm_keymat_t *this, proposal_t *proposal, diffie_hellman_t *dh,
159 chunk_t nonce_i, chunk_t nonce_r, ike_sa_id_t *id,
160 pseudo_random_function_t rekey_function, chunk_t rekey_skd)
161 {
162 /* Check encryption and integrity algorithms */
163 u_int16_t enc_alg, int_alg, key_size;
164 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &enc_alg, &key_size))
165 {
166 DBG1(DBG_IKE, "no %N selected", transform_type_names,
167 ENCRYPTION_ALGORITHM);
168 return FALSE;
169 }
170 if (encryption_algorithm_is_aead(enc_alg))
171 {
172 DBG1(DBG_IKE, "AEAD algorithm %N not supported",
173 encryption_algorithm_names, enc_alg);
174 return FALSE;
175 }
176 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &int_alg, NULL))
177 {
178 DBG1(DBG_IKE, "no %N selected", transform_type_names,
179 INTEGRITY_ALGORITHM);
180 return FALSE;
181 }
182 DBG2(DBG_IKE, "using %N for encryption, %N for integrity",
183 encryption_algorithm_names, enc_alg,
184 integrity_algorithm_names, int_alg);
185
186 /* Acquire nonce context id */
187 chunk_t * const nonce = this->initiator ? &nonce_i : &nonce_r;
188 const uint64_t nc_id = tkm->chunk_map->get_id(tkm->chunk_map, nonce);
189 if (!nc_id)
190 {
191 DBG1(DBG_IKE, "unable to acquire context id for nonce");
192 return FALSE;
193 }
194
195 /* Get DH context id */
196 tkm_diffie_hellman_t * const tkm_dh = (tkm_diffie_hellman_t *)dh;
197 const dh_id_type dh_id = tkm_dh->get_id(tkm_dh);
198
199 nonce_type nonce_rem;
200 u_int64_t spi_loc, spi_rem;
201
202 if (this->initiator)
203 {
204 chunk_to_sequence(&nonce_r, &nonce_rem);
205 spi_loc = id->get_initiator_spi(id);
206 spi_rem = id->get_responder_spi(id);
207 }
208 else
209 {
210 chunk_to_sequence(&nonce_i, &nonce_rem);
211 spi_loc = id->get_responder_spi(id);
212 spi_rem = id->get_initiator_spi(id);
213 }
214
215 key_type sk_ai, sk_ar, sk_ei, sk_er;
216 DBG1(DBG_IKE, "deriving IKE keys (nc: %llu, dh: %llu, spi_loc: %llx, "
217 "spi_rem: %llx)", nc_id, dh_id, spi_loc, spi_rem);
218 /* Fake some data for now */
219 if (ike_isa_create(1, 1, 1, dh_id, nc_id, nonce_rem, 1, spi_loc, spi_rem,
220 &sk_ai, &sk_ar, &sk_ei, &sk_er) != TKM_OK)
221 {
222 DBG1(DBG_IKE, "key derivation failed");
223 return FALSE;
224 }
225
226 chunk_t c_ai, c_ar, c_ei, c_er;
227 sequence_to_chunk(sk_ai.data, sk_ai.size, &c_ai);
228 sequence_to_chunk(sk_ar.data, sk_ar.size, &c_ar);
229 sequence_to_chunk(sk_ei.data, sk_ei.size, &c_ei);
230 sequence_to_chunk(sk_er.data, sk_er.size, &c_er);
231
232 aead_create_from_keys(&this->aead_in, &this->aead_out,
233 &c_ai, &c_ar, &c_ei, &c_er,
234 enc_alg, int_alg, key_size / 8, this->initiator);
235
236 chunk_clear(&c_ai);
237 chunk_clear(&c_ar);
238 chunk_clear(&c_ei);
239 chunk_clear(&c_er);
240
241 if (!this->aead_in || !this->aead_out)
242 {
243 DBG1(DBG_IKE, "could not initialize AEAD transforms");
244 return FALSE;
245 }
246
247 /* TODO: Add failure handler (see keymat_v2.c) */
248
249 if (this->proxy->derive_ike_keys(this->proxy, proposal, dh, nonce_i,
250 nonce_r, id, rekey_function, rekey_skd))
251 {
252 tkm->chunk_map->remove(tkm->chunk_map, nonce);
253 if (ike_nc_reset(nc_id) != TKM_OK)
254 {
255 DBG1(DBG_IKE, "failed to reset nonce context %llu", nc_id);
256 }
257 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, nc_id);
258
259 return TRUE;
260 }
261 return FALSE;
262 }
263
264 METHOD(tkm_keymat_t, derive_child_keys, bool,
265 private_tkm_keymat_t *this, proposal_t *proposal, diffie_hellman_t *dh,
266 chunk_t nonce_i, chunk_t nonce_r, chunk_t *encr_i, chunk_t *integ_i,
267 chunk_t *encr_r, chunk_t *integ_r)
268 {
269 DBG1(DBG_CHD, "deriving child keys");
270 return this->proxy->derive_child_keys(this->proxy, proposal, dh, nonce_i,
271 nonce_r, encr_i, integ_i, encr_r, integ_r);
272 }
273
274 METHOD(keymat_t, get_aead, aead_t*,
275 private_tkm_keymat_t *this, bool in)
276 {
277 return in ? this->aead_in : this->aead_out;
278 }
279
280 METHOD(tkm_keymat_t, get_auth_octets, bool,
281 private_tkm_keymat_t *this, bool verify, chunk_t ike_sa_init,
282 chunk_t nonce, identification_t *id, char reserved[3], chunk_t *octets)
283 {
284 DBG1(DBG_IKE, "returning auth octets");
285 return this->proxy->get_auth_octets(this->proxy, verify, ike_sa_init, nonce,
286 id, reserved, octets);
287 }
288
289 METHOD(tkm_keymat_t, get_skd, pseudo_random_function_t,
290 private_tkm_keymat_t *this, chunk_t *skd)
291 {
292 DBG1(DBG_IKE, "returning skd");
293 return this->proxy->get_skd(this->proxy, skd);
294 }
295
296 METHOD(tkm_keymat_t, get_psk_sig, bool,
297 private_tkm_keymat_t *this, bool verify, chunk_t ike_sa_init, chunk_t nonce,
298 chunk_t secret, identification_t *id, char reserved[3], chunk_t *sig)
299 {
300 DBG1(DBG_IKE, "returning PSK signature");
301 return this->proxy->get_psk_sig(this->proxy, verify, ike_sa_init, nonce,
302 secret, id, reserved, sig);
303 }
304
305 METHOD(keymat_t, destroy, void,
306 private_tkm_keymat_t *this)
307 {
308 DESTROY_IF(this->aead_in);
309 DESTROY_IF(this->aead_out);
310 this->proxy->keymat.destroy(&this->proxy->keymat);
311 free(this);
312 }
313
314 /**
315 * See header.
316 */
317 tkm_keymat_t *tkm_keymat_create(bool initiator)
318 {
319 private_tkm_keymat_t *this;
320
321 INIT(this,
322 .public = {
323 .keymat = {
324 .get_version = _get_version,
325 .create_dh = _create_dh,
326 .create_nonce_gen = _create_nonce_gen,
327 .get_aead = _get_aead,
328 .destroy = _destroy,
329 },
330 .derive_ike_keys = _derive_ike_keys,
331 .derive_child_keys = _derive_child_keys,
332 .get_skd = _get_skd,
333 .get_auth_octets = _get_auth_octets,
334 .get_psk_sig = _get_psk_sig,
335 },
336 .initiator = initiator,
337 .proxy = keymat_v2_create(initiator),
338 );
339
340 return &this->public;
341 }