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