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