]>
Commit | Line | Data |
---|---|---|
e538294f | 1 | /* |
a28d06f3 | 2 | * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. |
e538294f MC |
3 | * |
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
2ef9a7ac MC |
10 | /* We need to use some engine deprecated APIs */ |
11 | #define OPENSSL_SUPPRESS_DEPRECATED | |
12 | ||
e538294f MC |
13 | #include <string.h> |
14 | #include <openssl/core_dispatch.h> | |
15 | #include <openssl/core_names.h> | |
16 | #include <openssl/params.h> | |
17 | #include <openssl/err.h> | |
18 | #include <openssl/evp.h> | |
f5f29796 | 19 | #include <openssl/proverr.h> |
6229815a | 20 | #include <openssl/param_build.h> |
3f773c91 TM |
21 | #ifndef FIPS_MODULE |
22 | # include <openssl/engine.h> | |
23 | #endif | |
e538294f MC |
24 | #include "internal/param_build_set.h" |
25 | #include "prov/implementations.h" | |
26 | #include "prov/providercommon.h" | |
27 | #include "prov/provider_ctx.h" | |
28 | #include "prov/macsignature.h" | |
a540ef90 | 29 | #include "e_os.h" /* strcasecmp */ |
e538294f MC |
30 | |
31 | static OSSL_FUNC_keymgmt_new_fn mac_new; | |
32 | static OSSL_FUNC_keymgmt_free_fn mac_free; | |
33 | static OSSL_FUNC_keymgmt_gen_init_fn mac_gen_init; | |
34 | static OSSL_FUNC_keymgmt_gen_fn mac_gen; | |
35 | static OSSL_FUNC_keymgmt_gen_cleanup_fn mac_gen_cleanup; | |
422cbcee P |
36 | static OSSL_FUNC_keymgmt_gen_set_params_fn mac_gen_set_params; |
37 | static OSSL_FUNC_keymgmt_gen_settable_params_fn mac_gen_settable_params; | |
e538294f MC |
38 | static OSSL_FUNC_keymgmt_get_params_fn mac_get_params; |
39 | static OSSL_FUNC_keymgmt_gettable_params_fn mac_gettable_params; | |
40 | static OSSL_FUNC_keymgmt_set_params_fn mac_set_params; | |
41 | static OSSL_FUNC_keymgmt_settable_params_fn mac_settable_params; | |
42 | static OSSL_FUNC_keymgmt_has_fn mac_has; | |
43 | static OSSL_FUNC_keymgmt_match_fn mac_match; | |
44 | static OSSL_FUNC_keymgmt_import_fn mac_import; | |
45 | static OSSL_FUNC_keymgmt_import_types_fn mac_imexport_types; | |
46 | static OSSL_FUNC_keymgmt_export_fn mac_export; | |
47 | static OSSL_FUNC_keymgmt_export_types_fn mac_imexport_types; | |
48 | ||
422cbcee P |
49 | static OSSL_FUNC_keymgmt_new_fn mac_new_cmac; |
50 | static OSSL_FUNC_keymgmt_gettable_params_fn cmac_gettable_params; | |
51 | static OSSL_FUNC_keymgmt_import_types_fn cmac_imexport_types; | |
52 | static OSSL_FUNC_keymgmt_export_types_fn cmac_imexport_types; | |
f9562909 | 53 | static OSSL_FUNC_keymgmt_gen_init_fn cmac_gen_init; |
422cbcee P |
54 | static OSSL_FUNC_keymgmt_gen_set_params_fn cmac_gen_set_params; |
55 | static OSSL_FUNC_keymgmt_gen_settable_params_fn cmac_gen_settable_params; | |
56 | ||
e538294f | 57 | struct mac_gen_ctx { |
b4250010 | 58 | OSSL_LIB_CTX *libctx; |
e538294f MC |
59 | int selection; |
60 | unsigned char *priv_key; | |
61 | size_t priv_key_len; | |
2ef9a7ac | 62 | PROV_CIPHER cipher; |
e538294f MC |
63 | }; |
64 | ||
893d3df9 | 65 | MAC_KEY *ossl_mac_key_new(OSSL_LIB_CTX *libctx, int cmac) |
e538294f | 66 | { |
422cbcee P |
67 | MAC_KEY *mackey; |
68 | ||
69 | if (!ossl_prov_is_running()) | |
70 | return NULL; | |
e538294f | 71 | |
422cbcee | 72 | mackey = OPENSSL_zalloc(sizeof(*mackey)); |
e538294f MC |
73 | if (mackey == NULL) |
74 | return NULL; | |
75 | ||
76 | mackey->lock = CRYPTO_THREAD_lock_new(); | |
77 | if (mackey->lock == NULL) { | |
78 | OPENSSL_free(mackey); | |
79 | return NULL; | |
80 | } | |
81 | mackey->libctx = libctx; | |
82 | mackey->refcnt = 1; | |
a540ef90 | 83 | mackey->cmac = cmac; |
e538294f MC |
84 | |
85 | return mackey; | |
86 | } | |
87 | ||
893d3df9 | 88 | void ossl_mac_key_free(MAC_KEY *mackey) |
e538294f MC |
89 | { |
90 | int ref = 0; | |
91 | ||
92 | if (mackey == NULL) | |
93 | return; | |
94 | ||
95 | CRYPTO_DOWN_REF(&mackey->refcnt, &ref, mackey->lock); | |
96 | if (ref > 0) | |
97 | return; | |
98 | ||
99 | OPENSSL_secure_clear_free(mackey->priv_key, mackey->priv_key_len); | |
2ef9a7ac MC |
100 | OPENSSL_free(mackey->properties); |
101 | ossl_prov_cipher_reset(&mackey->cipher); | |
e538294f MC |
102 | CRYPTO_THREAD_lock_free(mackey->lock); |
103 | OPENSSL_free(mackey); | |
104 | } | |
105 | ||
893d3df9 | 106 | int ossl_mac_key_up_ref(MAC_KEY *mackey) |
e538294f MC |
107 | { |
108 | int ref = 0; | |
109 | ||
422cbcee P |
110 | /* This is effectively doing a new operation on the MAC_KEY and should be |
111 | * adequately guarded again modules' error states. However, both current | |
112 | * calls here are guarded propery in signature/mac_legacy.c. Thus, it | |
113 | * could be removed here. The concern is that something in the future | |
114 | * might call this function without adequate guards. It's a cheap call, | |
115 | * it seems best to leave it even though it is currently redundant. | |
116 | */ | |
117 | if (!ossl_prov_is_running()) | |
118 | return 0; | |
119 | ||
e538294f MC |
120 | CRYPTO_UP_REF(&mackey->refcnt, &ref, mackey->lock); |
121 | return 1; | |
122 | } | |
123 | ||
124 | static void *mac_new(void *provctx) | |
125 | { | |
893d3df9 | 126 | return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 0); |
a540ef90 MC |
127 | } |
128 | ||
129 | static void *mac_new_cmac(void *provctx) | |
130 | { | |
893d3df9 | 131 | return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 1); |
e538294f MC |
132 | } |
133 | ||
134 | static void mac_free(void *mackey) | |
135 | { | |
893d3df9 | 136 | ossl_mac_key_free(mackey); |
e538294f MC |
137 | } |
138 | ||
3d914185 | 139 | static int mac_has(const void *keydata, int selection) |
e538294f | 140 | { |
3d914185 | 141 | const MAC_KEY *key = keydata; |
e538294f MC |
142 | int ok = 0; |
143 | ||
422cbcee | 144 | if (ossl_prov_is_running() && key != NULL) { |
e538294f MC |
145 | /* |
146 | * MAC keys always have all the parameters they need (i.e. none). | |
147 | * Therefore we always return with 1, if asked about parameters. | |
148 | * Similarly for public keys. | |
149 | */ | |
150 | ok = 1; | |
151 | ||
152 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) | |
153 | ok = key->priv_key != NULL; | |
154 | } | |
155 | return ok; | |
156 | } | |
157 | ||
158 | static int mac_match(const void *keydata1, const void *keydata2, int selection) | |
159 | { | |
160 | const MAC_KEY *key1 = keydata1; | |
161 | const MAC_KEY *key2 = keydata2; | |
162 | int ok = 1; | |
163 | ||
422cbcee P |
164 | if (!ossl_prov_is_running()) |
165 | return 0; | |
166 | ||
e538294f MC |
167 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
168 | if ((key1->priv_key == NULL && key2->priv_key != NULL) | |
169 | || (key1->priv_key != NULL && key2->priv_key == NULL) | |
a540ef90 | 170 | || key1->priv_key_len != key2->priv_key_len |
2ef9a7ac MC |
171 | || (key1->cipher.cipher == NULL && key2->cipher.cipher != NULL) |
172 | || (key1->cipher.cipher != NULL && key2->cipher.cipher == NULL)) | |
e538294f MC |
173 | ok = 0; |
174 | else | |
175 | ok = ok && (key1->priv_key == NULL /* implies key2->privkey == NULL */ | |
176 | || CRYPTO_memcmp(key1->priv_key, key2->priv_key, | |
177 | key1->priv_key_len) == 0); | |
2ef9a7ac MC |
178 | if (key1->cipher.cipher != NULL) |
179 | ok = ok && EVP_CIPHER_is_a(key1->cipher.cipher, | |
ed576acd | 180 | EVP_CIPHER_get0_name(key2->cipher.cipher)); |
e538294f MC |
181 | } |
182 | return ok; | |
183 | } | |
184 | ||
185 | static int mac_key_fromdata(MAC_KEY *key, const OSSL_PARAM params[]) | |
186 | { | |
187 | const OSSL_PARAM *p; | |
188 | ||
189 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY); | |
190 | if (p != NULL) { | |
191 | if (p->data_type != OSSL_PARAM_OCTET_STRING) { | |
192 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); | |
193 | return 0; | |
194 | } | |
195 | OPENSSL_secure_clear_free(key->priv_key, key->priv_key_len); | |
92b83537 TM |
196 | /* allocate at least one byte to distinguish empty key from no key set */ |
197 | key->priv_key = OPENSSL_secure_malloc(p->data_size > 0 ? p->data_size : 1); | |
e538294f MC |
198 | if (key->priv_key == NULL) { |
199 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
200 | return 0; | |
201 | } | |
202 | memcpy(key->priv_key, p->data, p->data_size); | |
203 | key->priv_key_len = p->data_size; | |
a540ef90 | 204 | } |
e538294f | 205 | |
2ef9a7ac MC |
206 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PROPERTIES); |
207 | if (p != NULL) { | |
208 | if (p->data_type != OSSL_PARAM_UTF8_STRING) { | |
209 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); | |
210 | return 0; | |
a540ef90 | 211 | } |
2ef9a7ac MC |
212 | OPENSSL_free(key->properties); |
213 | key->properties = OPENSSL_strdup(p->data); | |
214 | if (key->properties == NULL) { | |
215 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
216 | return 0; | |
a540ef90 | 217 | } |
e538294f | 218 | } |
a540ef90 | 219 | |
2ef9a7ac MC |
220 | if (key->cmac && !ossl_prov_cipher_load_from_params(&key->cipher, params, |
221 | key->libctx)) { | |
222 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); | |
223 | return 0; | |
224 | } | |
225 | ||
226 | if (key->priv_key != NULL) | |
a540ef90 MC |
227 | return 1; |
228 | ||
e538294f MC |
229 | return 0; |
230 | } | |
231 | ||
232 | static int mac_import(void *keydata, int selection, const OSSL_PARAM params[]) | |
233 | { | |
234 | MAC_KEY *key = keydata; | |
235 | ||
422cbcee | 236 | if (!ossl_prov_is_running() || key == NULL) |
e538294f MC |
237 | return 0; |
238 | ||
239 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0) | |
240 | return 0; | |
241 | ||
242 | return mac_key_fromdata(key, params); | |
243 | } | |
244 | ||
245 | static int key_to_params(MAC_KEY *key, OSSL_PARAM_BLD *tmpl, | |
246 | OSSL_PARAM params[]) | |
247 | { | |
248 | if (key == NULL) | |
249 | return 0; | |
250 | ||
251 | if (key->priv_key != NULL | |
252 | && !ossl_param_build_set_octet_string(tmpl, params, | |
253 | OSSL_PKEY_PARAM_PRIV_KEY, | |
254 | key->priv_key, key->priv_key_len)) | |
255 | return 0; | |
256 | ||
2ef9a7ac | 257 | if (key->cipher.cipher != NULL |
a540ef90 MC |
258 | && !ossl_param_build_set_utf8_string(tmpl, params, |
259 | OSSL_PKEY_PARAM_CIPHER, | |
ed576acd | 260 | EVP_CIPHER_get0_name(key->cipher.cipher))) |
a540ef90 MC |
261 | return 0; |
262 | ||
2ef9a7ac MC |
263 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
264 | if (key->cipher.engine != NULL | |
a540ef90 MC |
265 | && !ossl_param_build_set_utf8_string(tmpl, params, |
266 | OSSL_PKEY_PARAM_ENGINE, | |
2ef9a7ac | 267 | ENGINE_get_id(key->cipher.engine))) |
a540ef90 | 268 | return 0; |
2ef9a7ac | 269 | #endif |
a540ef90 | 270 | |
e538294f MC |
271 | return 1; |
272 | } | |
273 | ||
274 | static int mac_export(void *keydata, int selection, OSSL_CALLBACK *param_cb, | |
275 | void *cbarg) | |
276 | { | |
277 | MAC_KEY *key = keydata; | |
278 | OSSL_PARAM_BLD *tmpl; | |
279 | OSSL_PARAM *params = NULL; | |
280 | int ret = 0; | |
281 | ||
422cbcee | 282 | if (!ossl_prov_is_running() || key == NULL) |
e538294f MC |
283 | return 0; |
284 | ||
285 | tmpl = OSSL_PARAM_BLD_new(); | |
286 | if (tmpl == NULL) | |
287 | return 0; | |
288 | ||
289 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0 | |
290 | && !key_to_params(key, tmpl, NULL)) | |
291 | goto err; | |
292 | ||
293 | params = OSSL_PARAM_BLD_to_param(tmpl); | |
294 | if (params == NULL) | |
295 | goto err; | |
296 | ||
297 | ret = param_cb(params, cbarg); | |
3f883c7c | 298 | OSSL_PARAM_free(params); |
e538294f MC |
299 | err: |
300 | OSSL_PARAM_BLD_free(tmpl); | |
301 | return ret; | |
302 | } | |
303 | ||
304 | static const OSSL_PARAM mac_key_types[] = { | |
305 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), | |
2ef9a7ac | 306 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0), |
e538294f MC |
307 | OSSL_PARAM_END |
308 | }; | |
309 | static const OSSL_PARAM *mac_imexport_types(int selection) | |
310 | { | |
311 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) | |
312 | return mac_key_types; | |
313 | return NULL; | |
314 | } | |
315 | ||
a540ef90 MC |
316 | static const OSSL_PARAM cmac_key_types[] = { |
317 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), | |
318 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER, NULL, 0), | |
319 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_ENGINE, NULL, 0), | |
2ef9a7ac | 320 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0), |
a540ef90 MC |
321 | OSSL_PARAM_END |
322 | }; | |
323 | static const OSSL_PARAM *cmac_imexport_types(int selection) | |
324 | { | |
325 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) | |
326 | return cmac_key_types; | |
327 | return NULL; | |
328 | } | |
329 | ||
e538294f MC |
330 | static int mac_get_params(void *key, OSSL_PARAM params[]) |
331 | { | |
332 | return key_to_params(key, NULL, params); | |
333 | } | |
334 | ||
335 | static const OSSL_PARAM *mac_gettable_params(void *provctx) | |
336 | { | |
337 | static const OSSL_PARAM gettable_params[] = { | |
338 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), | |
339 | OSSL_PARAM_END | |
340 | }; | |
341 | return gettable_params; | |
342 | } | |
343 | ||
a540ef90 MC |
344 | static const OSSL_PARAM *cmac_gettable_params(void *provctx) |
345 | { | |
346 | static const OSSL_PARAM gettable_params[] = { | |
347 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), | |
348 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER, NULL, 0), | |
349 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_ENGINE, NULL, 0), | |
350 | OSSL_PARAM_END | |
351 | }; | |
352 | return gettable_params; | |
353 | } | |
354 | ||
e538294f MC |
355 | static int mac_set_params(void *keydata, const OSSL_PARAM params[]) |
356 | { | |
357 | MAC_KEY *key = keydata; | |
358 | const OSSL_PARAM *p; | |
359 | ||
360 | if (key == NULL) | |
361 | return 0; | |
362 | ||
363 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY); | |
364 | if (p != NULL) | |
365 | return mac_key_fromdata(key, params); | |
366 | ||
367 | return 1; | |
368 | } | |
369 | ||
370 | static const OSSL_PARAM *mac_settable_params(void *provctx) | |
371 | { | |
372 | static const OSSL_PARAM settable_params[] = { | |
373 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), | |
374 | OSSL_PARAM_END | |
375 | }; | |
376 | return settable_params; | |
377 | } | |
378 | ||
f9562909 | 379 | static void *mac_gen_init_common(void *provctx, int selection) |
e538294f | 380 | { |
a829b735 | 381 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); |
e538294f MC |
382 | struct mac_gen_ctx *gctx = NULL; |
383 | ||
422cbcee P |
384 | if (!ossl_prov_is_running()) |
385 | return NULL; | |
386 | ||
a540ef90 | 387 | if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { |
e538294f MC |
388 | gctx->libctx = libctx; |
389 | gctx->selection = selection; | |
390 | } | |
391 | return gctx; | |
392 | } | |
393 | ||
f9562909 P |
394 | static void *mac_gen_init(void *provctx, int selection, |
395 | const OSSL_PARAM params[]) | |
396 | { | |
397 | struct mac_gen_ctx *gctx = mac_gen_init_common(provctx, selection); | |
398 | ||
399 | if (gctx != NULL && !mac_gen_set_params(gctx, params)) { | |
400 | OPENSSL_free(gctx); | |
401 | gctx = NULL; | |
402 | } | |
403 | return gctx; | |
404 | } | |
405 | ||
406 | static void *cmac_gen_init(void *provctx, int selection, | |
407 | const OSSL_PARAM params[]) | |
408 | { | |
409 | struct mac_gen_ctx *gctx = mac_gen_init_common(provctx, selection); | |
410 | ||
411 | if (gctx != NULL && !cmac_gen_set_params(gctx, params)) { | |
412 | OPENSSL_free(gctx); | |
413 | gctx = NULL; | |
414 | } | |
415 | return gctx; | |
416 | } | |
417 | ||
e538294f MC |
418 | static int mac_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
419 | { | |
420 | struct mac_gen_ctx *gctx = genctx; | |
421 | const OSSL_PARAM *p; | |
422 | ||
423 | if (gctx == NULL) | |
424 | return 0; | |
425 | ||
426 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY); | |
427 | if (p != NULL) { | |
428 | if (p->data_type != OSSL_PARAM_OCTET_STRING) { | |
429 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); | |
430 | return 0; | |
431 | } | |
432 | gctx->priv_key = OPENSSL_secure_malloc(p->data_size); | |
433 | if (gctx->priv_key == NULL) { | |
434 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
435 | return 0; | |
436 | } | |
437 | memcpy(gctx->priv_key, p->data, p->data_size); | |
438 | gctx->priv_key_len = p->data_size; | |
439 | } | |
440 | ||
441 | return 1; | |
442 | } | |
443 | ||
a540ef90 MC |
444 | static int cmac_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
445 | { | |
446 | struct mac_gen_ctx *gctx = genctx; | |
a540ef90 MC |
447 | |
448 | if (!mac_gen_set_params(genctx, params)) | |
449 | return 0; | |
450 | ||
2ef9a7ac MC |
451 | if (!ossl_prov_cipher_load_from_params(&gctx->cipher, params, |
452 | gctx->libctx)) { | |
453 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); | |
454 | return 0; | |
a540ef90 MC |
455 | } |
456 | ||
457 | return 1; | |
458 | } | |
459 | ||
fb67126e TM |
460 | static const OSSL_PARAM *mac_gen_settable_params(ossl_unused void *genctx, |
461 | ossl_unused void *provctx) | |
e538294f MC |
462 | { |
463 | static OSSL_PARAM settable[] = { | |
464 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), | |
465 | OSSL_PARAM_END | |
466 | }; | |
467 | return settable; | |
468 | } | |
469 | ||
fb67126e TM |
470 | static const OSSL_PARAM *cmac_gen_settable_params(ossl_unused void *genctx, |
471 | ossl_unused void *provctx) | |
a540ef90 MC |
472 | { |
473 | static OSSL_PARAM settable[] = { | |
474 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), | |
475 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER, NULL, 0), | |
476 | OSSL_PARAM_END | |
477 | }; | |
478 | return settable; | |
479 | } | |
480 | ||
e538294f MC |
481 | static void *mac_gen(void *genctx, OSSL_CALLBACK *cb, void *cbarg) |
482 | { | |
483 | struct mac_gen_ctx *gctx = genctx; | |
484 | MAC_KEY *key; | |
485 | ||
422cbcee | 486 | if (!ossl_prov_is_running() || gctx == NULL) |
e538294f MC |
487 | return NULL; |
488 | ||
893d3df9 | 489 | if ((key = ossl_mac_key_new(gctx->libctx, 0)) == NULL) { |
e538294f MC |
490 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); |
491 | return NULL; | |
492 | } | |
493 | ||
494 | /* If we're doing parameter generation then we just return a blank key */ | |
495 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) | |
496 | return key; | |
497 | ||
498 | if (gctx->priv_key == NULL) { | |
f5f29796 | 499 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
893d3df9 | 500 | ossl_mac_key_free(key); |
e538294f MC |
501 | return NULL; |
502 | } | |
503 | ||
504 | /* | |
505 | * This is horrible but required for backwards compatibility. We don't | |
506 | * actually do real key generation at all. We simply copy the key that was | |
507 | * previously set in the gctx. Hopefully at some point in the future all | |
508 | * of this can be removed and we will only support the EVP_KDF APIs. | |
509 | */ | |
2ef9a7ac MC |
510 | if (!ossl_prov_cipher_copy(&key->cipher, &gctx->cipher)) { |
511 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); | |
512 | return NULL; | |
513 | } | |
514 | ossl_prov_cipher_reset(&gctx->cipher); | |
e538294f MC |
515 | key->priv_key = gctx->priv_key; |
516 | key->priv_key_len = gctx->priv_key_len; | |
517 | gctx->priv_key_len = 0; | |
518 | gctx->priv_key = NULL; | |
519 | ||
520 | return key; | |
521 | } | |
522 | ||
523 | static void mac_gen_cleanup(void *genctx) | |
524 | { | |
525 | struct mac_gen_ctx *gctx = genctx; | |
526 | ||
527 | OPENSSL_secure_clear_free(gctx->priv_key, gctx->priv_key_len); | |
2ef9a7ac | 528 | ossl_prov_cipher_reset(&gctx->cipher); |
e538294f MC |
529 | OPENSSL_free(gctx); |
530 | } | |
531 | ||
1be63951 | 532 | const OSSL_DISPATCH ossl_mac_legacy_keymgmt_functions[] = { |
e538294f MC |
533 | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new }, |
534 | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free }, | |
535 | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))mac_get_params }, | |
536 | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))mac_gettable_params }, | |
537 | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))mac_set_params }, | |
538 | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))mac_settable_params }, | |
539 | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has }, | |
540 | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match }, | |
541 | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))mac_import }, | |
542 | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))mac_imexport_types }, | |
543 | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export }, | |
544 | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))mac_imexport_types }, | |
545 | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))mac_gen_init }, | |
546 | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))mac_gen_set_params }, | |
547 | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, | |
548 | (void (*)(void))mac_gen_settable_params }, | |
549 | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen }, | |
550 | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup }, | |
551 | { 0, NULL } | |
552 | }; | |
a540ef90 | 553 | |
e2f5df36 | 554 | const OSSL_DISPATCH ossl_cmac_legacy_keymgmt_functions[] = { |
a540ef90 MC |
555 | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new_cmac }, |
556 | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free }, | |
557 | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))mac_get_params }, | |
558 | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))cmac_gettable_params }, | |
559 | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))mac_set_params }, | |
560 | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))mac_settable_params }, | |
561 | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has }, | |
562 | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match }, | |
563 | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))mac_import }, | |
564 | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))cmac_imexport_types }, | |
565 | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export }, | |
566 | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))cmac_imexport_types }, | |
f9562909 | 567 | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))cmac_gen_init }, |
a540ef90 MC |
568 | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))cmac_gen_set_params }, |
569 | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, | |
570 | (void (*)(void))cmac_gen_settable_params }, | |
571 | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen }, | |
572 | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup }, | |
573 | { 0, NULL } | |
574 | }; | |
422cbcee | 575 |