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