2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
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 #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"
26 #include "e_os.h" /* strcasecmp */
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;
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;
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;
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;
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;
56 unsigned char *priv_key;
61 MAC_KEY *ossl_mac_key_new(OSSL_LIB_CTX *libctx, int cmac)
65 if (!ossl_prov_is_running())
68 mackey = OPENSSL_zalloc(sizeof(*mackey));
72 mackey->lock = CRYPTO_THREAD_lock_new();
73 if (mackey->lock == NULL) {
77 mackey->libctx = libctx;
84 void ossl_mac_key_free(MAC_KEY *mackey)
91 CRYPTO_DOWN_REF(&mackey->refcnt, &ref, mackey->lock);
95 OPENSSL_secure_clear_free(mackey->priv_key, mackey->priv_key_len);
96 OPENSSL_free(mackey->properties);
97 ossl_prov_cipher_reset(&mackey->cipher);
98 CRYPTO_THREAD_lock_free(mackey->lock);
102 int ossl_mac_key_up_ref(MAC_KEY *mackey)
106 /* This is effectively doing a new operation on the MAC_KEY and should be
107 * adequately guarded again modules' error states. However, both current
108 * calls here are guarded propery in signature/mac_legacy.c. Thus, it
109 * could be removed here. The concern is that something in the future
110 * might call this function without adequate guards. It's a cheap call,
111 * it seems best to leave it even though it is currently redundant.
113 if (!ossl_prov_is_running())
116 CRYPTO_UP_REF(&mackey->refcnt, &ref, mackey->lock);
120 static void *mac_new(void *provctx)
122 return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 0);
125 static void *mac_new_cmac(void *provctx)
127 return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 1);
130 static void mac_free(void *mackey)
132 ossl_mac_key_free(mackey);
135 static int mac_has(const void *keydata, int selection)
137 const MAC_KEY *key = keydata;
140 if (ossl_prov_is_running() && key != NULL) {
142 * MAC keys always have all the parameters they need (i.e. none).
143 * Therefore we always return with 1, if asked about parameters.
144 * Similarly for public keys.
148 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
149 ok = key->priv_key != NULL;
154 static int mac_match(const void *keydata1, const void *keydata2, int selection)
156 const MAC_KEY *key1 = keydata1;
157 const MAC_KEY *key2 = keydata2;
160 if (!ossl_prov_is_running())
163 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
164 if ((key1->priv_key == NULL && key2->priv_key != NULL)
165 || (key1->priv_key != NULL && key2->priv_key == NULL)
166 || key1->priv_key_len != key2->priv_key_len
167 || (key1->cipher.cipher == NULL && key2->cipher.cipher != NULL)
168 || (key1->cipher.cipher != NULL && key2->cipher.cipher == NULL))
171 ok = ok && (key1->priv_key == NULL /* implies key2->privkey == NULL */
172 || CRYPTO_memcmp(key1->priv_key, key2->priv_key,
173 key1->priv_key_len) == 0);
174 if (key1->cipher.cipher != NULL)
175 ok = ok && EVP_CIPHER_is_a(key1->cipher.cipher,
176 EVP_CIPHER_name(key2->cipher.cipher));
181 static int mac_key_fromdata(MAC_KEY *key, const OSSL_PARAM params[])
185 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
187 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
188 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
191 OPENSSL_secure_clear_free(key->priv_key, key->priv_key_len);
192 key->priv_key = OPENSSL_secure_malloc(p->data_size);
193 if (key->priv_key == NULL) {
194 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
197 memcpy(key->priv_key, p->data, p->data_size);
198 key->priv_key_len = p->data_size;
201 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PROPERTIES);
203 if (p->data_type != OSSL_PARAM_UTF8_STRING) {
204 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
207 OPENSSL_free(key->properties);
208 key->properties = OPENSSL_strdup(p->data);
209 if (key->properties == NULL) {
210 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
215 if (key->cmac && !ossl_prov_cipher_load_from_params(&key->cipher, params,
217 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
221 if (key->priv_key != NULL)
227 static int mac_import(void *keydata, int selection, const OSSL_PARAM params[])
229 MAC_KEY *key = keydata;
231 if (!ossl_prov_is_running() || key == NULL)
234 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0)
237 return mac_key_fromdata(key, params);
240 static int key_to_params(MAC_KEY *key, OSSL_PARAM_BLD *tmpl,
246 if (key->priv_key != NULL
247 && !ossl_param_build_set_octet_string(tmpl, params,
248 OSSL_PKEY_PARAM_PRIV_KEY,
249 key->priv_key, key->priv_key_len))
252 if (key->cipher.cipher != NULL
253 && !ossl_param_build_set_utf8_string(tmpl, params,
254 OSSL_PKEY_PARAM_CIPHER,
255 EVP_CIPHER_name(key->cipher.cipher)))
258 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
259 if (key->cipher.engine != NULL
260 && !ossl_param_build_set_utf8_string(tmpl, params,
261 OSSL_PKEY_PARAM_ENGINE,
262 ENGINE_get_id(key->cipher.engine)))
269 static int mac_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
272 MAC_KEY *key = keydata;
273 OSSL_PARAM_BLD *tmpl;
274 OSSL_PARAM *params = NULL;
277 if (!ossl_prov_is_running() || key == NULL)
280 tmpl = OSSL_PARAM_BLD_new();
284 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
285 && !key_to_params(key, tmpl, NULL))
288 params = OSSL_PARAM_BLD_to_param(tmpl);
292 ret = param_cb(params, cbarg);
293 OSSL_PARAM_BLD_free_params(params);
295 OSSL_PARAM_BLD_free(tmpl);
299 static const OSSL_PARAM mac_key_types[] = {
300 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
301 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
304 static const OSSL_PARAM *mac_imexport_types(int selection)
306 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
307 return mac_key_types;
311 static const OSSL_PARAM cmac_key_types[] = {
312 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
313 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER, NULL, 0),
314 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_ENGINE, NULL, 0),
315 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
318 static const OSSL_PARAM *cmac_imexport_types(int selection)
320 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
321 return cmac_key_types;
325 static int mac_get_params(void *key, OSSL_PARAM params[])
327 return key_to_params(key, NULL, params);
330 static const OSSL_PARAM *mac_gettable_params(void *provctx)
332 static const OSSL_PARAM gettable_params[] = {
333 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
336 return gettable_params;
339 static const OSSL_PARAM *cmac_gettable_params(void *provctx)
341 static const OSSL_PARAM gettable_params[] = {
342 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
343 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER, NULL, 0),
344 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_ENGINE, NULL, 0),
347 return gettable_params;
350 static int mac_set_params(void *keydata, const OSSL_PARAM params[])
352 MAC_KEY *key = keydata;
358 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
360 return mac_key_fromdata(key, params);
365 static const OSSL_PARAM *mac_settable_params(void *provctx)
367 static const OSSL_PARAM settable_params[] = {
368 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
371 return settable_params;
374 static void *mac_gen_init(void *provctx, int selection)
376 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
377 struct mac_gen_ctx *gctx = NULL;
379 if (!ossl_prov_is_running())
382 if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
383 gctx->libctx = libctx;
384 gctx->selection = selection;
389 static int mac_gen_set_params(void *genctx, const OSSL_PARAM params[])
391 struct mac_gen_ctx *gctx = genctx;
397 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
399 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
400 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
403 gctx->priv_key = OPENSSL_secure_malloc(p->data_size);
404 if (gctx->priv_key == NULL) {
405 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
408 memcpy(gctx->priv_key, p->data, p->data_size);
409 gctx->priv_key_len = p->data_size;
415 static int cmac_gen_set_params(void *genctx, const OSSL_PARAM params[])
417 struct mac_gen_ctx *gctx = genctx;
419 if (!mac_gen_set_params(genctx, params))
422 if (!ossl_prov_cipher_load_from_params(&gctx->cipher, params,
424 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
431 static const OSSL_PARAM *mac_gen_settable_params(void *provctx)
433 static OSSL_PARAM settable[] = {
434 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
440 static const OSSL_PARAM *cmac_gen_settable_params(void *provctx)
442 static OSSL_PARAM settable[] = {
443 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
444 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER, NULL, 0),
450 static void *mac_gen(void *genctx, OSSL_CALLBACK *cb, void *cbarg)
452 struct mac_gen_ctx *gctx = genctx;
455 if (!ossl_prov_is_running() || gctx == NULL)
458 if ((key = ossl_mac_key_new(gctx->libctx, 0)) == NULL) {
459 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
463 /* If we're doing parameter generation then we just return a blank key */
464 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
467 if (gctx->priv_key == NULL) {
468 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
469 ossl_mac_key_free(key);
474 * This is horrible but required for backwards compatibility. We don't
475 * actually do real key generation at all. We simply copy the key that was
476 * previously set in the gctx. Hopefully at some point in the future all
477 * of this can be removed and we will only support the EVP_KDF APIs.
479 if (!ossl_prov_cipher_copy(&key->cipher, &gctx->cipher)) {
480 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
483 ossl_prov_cipher_reset(&gctx->cipher);
484 key->priv_key = gctx->priv_key;
485 key->priv_key_len = gctx->priv_key_len;
486 gctx->priv_key_len = 0;
487 gctx->priv_key = NULL;
492 static void mac_gen_cleanup(void *genctx)
494 struct mac_gen_ctx *gctx = genctx;
496 OPENSSL_secure_clear_free(gctx->priv_key, gctx->priv_key_len);
497 ossl_prov_cipher_reset(&gctx->cipher);
501 const OSSL_DISPATCH ossl_mac_legacy_keymgmt_functions[] = {
502 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new },
503 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free },
504 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))mac_get_params },
505 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))mac_gettable_params },
506 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))mac_set_params },
507 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))mac_settable_params },
508 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has },
509 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match },
510 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))mac_import },
511 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))mac_imexport_types },
512 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export },
513 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))mac_imexport_types },
514 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))mac_gen_init },
515 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))mac_gen_set_params },
516 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
517 (void (*)(void))mac_gen_settable_params },
518 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen },
519 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup },
523 const OSSL_DISPATCH ossl_cossl_mac_legacy_keymgmt_functions[] = {
524 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new_cmac },
525 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free },
526 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))mac_get_params },
527 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))cmac_gettable_params },
528 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))mac_set_params },
529 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))mac_settable_params },
530 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has },
531 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match },
532 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))mac_import },
533 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))cmac_imexport_types },
534 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export },
535 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))cmac_imexport_types },
536 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))mac_gen_init },
537 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))cmac_gen_set_params },
538 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
539 (void (*)(void))cmac_gen_settable_params },
540 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen },
541 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup },