2 * Copyright 2020 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/param_build.h"
20 #include "internal/param_build_set.h"
21 #include "prov/implementations.h"
22 #include "prov/providercommon.h"
23 #include "prov/provider_ctx.h"
24 #include "prov/macsignature.h"
25 #include "e_os.h" /* strcasecmp */
27 static OSSL_FUNC_keymgmt_new_fn mac_new
;
28 static OSSL_FUNC_keymgmt_free_fn mac_free
;
29 static OSSL_FUNC_keymgmt_gen_init_fn mac_gen_init
;
30 static OSSL_FUNC_keymgmt_gen_fn mac_gen
;
31 static OSSL_FUNC_keymgmt_gen_cleanup_fn mac_gen_cleanup
;
32 static OSSL_FUNC_keymgmt_gen_set_params_fn mac_gen_set_params
;
33 static OSSL_FUNC_keymgmt_gen_settable_params_fn mac_gen_settable_params
;
34 static OSSL_FUNC_keymgmt_get_params_fn mac_get_params
;
35 static OSSL_FUNC_keymgmt_gettable_params_fn mac_gettable_params
;
36 static OSSL_FUNC_keymgmt_set_params_fn mac_set_params
;
37 static OSSL_FUNC_keymgmt_settable_params_fn mac_settable_params
;
38 static OSSL_FUNC_keymgmt_has_fn mac_has
;
39 static OSSL_FUNC_keymgmt_match_fn mac_match
;
40 static OSSL_FUNC_keymgmt_import_fn mac_import
;
41 static OSSL_FUNC_keymgmt_import_types_fn mac_imexport_types
;
42 static OSSL_FUNC_keymgmt_export_fn mac_export
;
43 static OSSL_FUNC_keymgmt_export_types_fn mac_imexport_types
;
45 static OSSL_FUNC_keymgmt_new_fn mac_new_cmac
;
46 static OSSL_FUNC_keymgmt_gettable_params_fn cmac_gettable_params
;
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_set_params_fn cmac_gen_set_params
;
50 static OSSL_FUNC_keymgmt_gen_settable_params_fn cmac_gen_settable_params
;
55 unsigned char *priv_key
;
60 MAC_KEY
*mac_key_new(OSSL_LIB_CTX
*libctx
, int cmac
)
64 if (!ossl_prov_is_running())
67 mackey
= OPENSSL_zalloc(sizeof(*mackey
));
71 mackey
->lock
= CRYPTO_THREAD_lock_new();
72 if (mackey
->lock
== NULL
) {
76 mackey
->libctx
= libctx
;
83 void mac_key_free(MAC_KEY
*mackey
)
90 CRYPTO_DOWN_REF(&mackey
->refcnt
, &ref
, mackey
->lock
);
94 OPENSSL_secure_clear_free(mackey
->priv_key
, mackey
->priv_key_len
);
95 OPENSSL_free(mackey
->properties
);
96 ossl_prov_cipher_reset(&mackey
->cipher
);
97 CRYPTO_THREAD_lock_free(mackey
->lock
);
101 int mac_key_up_ref(MAC_KEY
*mackey
)
105 /* This is effectively doing a new operation on the MAC_KEY and should be
106 * adequately guarded again modules' error states. However, both current
107 * calls here are guarded propery in signature/mac_legacy.c. Thus, it
108 * could be removed here. The concern is that something in the future
109 * might call this function without adequate guards. It's a cheap call,
110 * it seems best to leave it even though it is currently redundant.
112 if (!ossl_prov_is_running())
115 CRYPTO_UP_REF(&mackey
->refcnt
, &ref
, mackey
->lock
);
119 static void *mac_new(void *provctx
)
121 return mac_key_new(PROV_LIBRARY_CONTEXT_OF(provctx
), 0);
124 static void *mac_new_cmac(void *provctx
)
126 return mac_key_new(PROV_LIBRARY_CONTEXT_OF(provctx
), 1);
129 static void mac_free(void *mackey
)
131 mac_key_free(mackey
);
134 static int mac_has(void *keydata
, int selection
)
136 MAC_KEY
*key
= keydata
;
139 if (ossl_prov_is_running() && key
!= NULL
) {
141 * MAC keys always have all the parameters they need (i.e. none).
142 * Therefore we always return with 1, if asked about parameters.
143 * Similarly for public keys.
147 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
148 ok
= key
->priv_key
!= NULL
;
153 static int mac_match(const void *keydata1
, const void *keydata2
, int selection
)
155 const MAC_KEY
*key1
= keydata1
;
156 const MAC_KEY
*key2
= keydata2
;
159 if (!ossl_prov_is_running())
162 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
163 if ((key1
->priv_key
== NULL
&& key2
->priv_key
!= NULL
)
164 || (key1
->priv_key
!= NULL
&& key2
->priv_key
== NULL
)
165 || key1
->priv_key_len
!= key2
->priv_key_len
166 || (key1
->cipher
.cipher
== NULL
&& key2
->cipher
.cipher
!= NULL
)
167 || (key1
->cipher
.cipher
!= NULL
&& key2
->cipher
.cipher
== NULL
))
170 ok
= ok
&& (key1
->priv_key
== NULL
/* implies key2->privkey == NULL */
171 || CRYPTO_memcmp(key1
->priv_key
, key2
->priv_key
,
172 key1
->priv_key_len
) == 0);
173 if (key1
->cipher
.cipher
!= NULL
)
174 ok
= ok
&& EVP_CIPHER_is_a(key1
->cipher
.cipher
,
175 EVP_CIPHER_name(key2
->cipher
.cipher
));
180 static int mac_key_fromdata(MAC_KEY
*key
, const OSSL_PARAM params
[])
184 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
);
186 if (p
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
187 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
190 OPENSSL_secure_clear_free(key
->priv_key
, key
->priv_key_len
);
191 key
->priv_key
= OPENSSL_secure_malloc(p
->data_size
);
192 if (key
->priv_key
== NULL
) {
193 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
196 memcpy(key
->priv_key
, p
->data
, p
->data_size
);
197 key
->priv_key_len
= p
->data_size
;
200 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PROPERTIES
);
202 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
) {
203 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
206 OPENSSL_free(key
->properties
);
207 key
->properties
= OPENSSL_strdup(p
->data
);
208 if (key
->properties
== NULL
) {
209 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
214 if (key
->cmac
&& !ossl_prov_cipher_load_from_params(&key
->cipher
, params
,
216 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
220 if (key
->priv_key
!= NULL
)
226 static int mac_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
228 MAC_KEY
*key
= keydata
;
230 if (!ossl_prov_is_running() || key
== NULL
)
233 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) == 0)
236 return mac_key_fromdata(key
, params
);
239 static int key_to_params(MAC_KEY
*key
, OSSL_PARAM_BLD
*tmpl
,
245 if (key
->priv_key
!= NULL
246 && !ossl_param_build_set_octet_string(tmpl
, params
,
247 OSSL_PKEY_PARAM_PRIV_KEY
,
248 key
->priv_key
, key
->priv_key_len
))
251 if (key
->cipher
.cipher
!= NULL
252 && !ossl_param_build_set_utf8_string(tmpl
, params
,
253 OSSL_PKEY_PARAM_CIPHER
,
254 EVP_CIPHER_name(key
->cipher
.cipher
)))
257 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
258 if (key
->cipher
.engine
!= NULL
259 && !ossl_param_build_set_utf8_string(tmpl
, params
,
260 OSSL_PKEY_PARAM_ENGINE
,
261 ENGINE_get_id(key
->cipher
.engine
)))
268 static int mac_export(void *keydata
, int selection
, OSSL_CALLBACK
*param_cb
,
271 MAC_KEY
*key
= keydata
;
272 OSSL_PARAM_BLD
*tmpl
;
273 OSSL_PARAM
*params
= NULL
;
276 if (!ossl_prov_is_running() || key
== NULL
)
279 tmpl
= OSSL_PARAM_BLD_new();
283 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
284 && !key_to_params(key
, tmpl
, NULL
))
287 params
= OSSL_PARAM_BLD_to_param(tmpl
);
291 ret
= param_cb(params
, cbarg
);
292 OSSL_PARAM_BLD_free_params(params
);
294 OSSL_PARAM_BLD_free(tmpl
);
298 static const OSSL_PARAM mac_key_types
[] = {
299 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
300 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES
, NULL
, 0),
303 static const OSSL_PARAM
*mac_imexport_types(int selection
)
305 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
306 return mac_key_types
;
310 static const OSSL_PARAM cmac_key_types
[] = {
311 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
312 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER
, NULL
, 0),
313 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_ENGINE
, NULL
, 0),
314 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES
, NULL
, 0),
317 static const OSSL_PARAM
*cmac_imexport_types(int selection
)
319 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
320 return cmac_key_types
;
324 static int mac_get_params(void *key
, OSSL_PARAM params
[])
326 return key_to_params(key
, NULL
, params
);
329 static const OSSL_PARAM
*mac_gettable_params(void *provctx
)
331 static const OSSL_PARAM gettable_params
[] = {
332 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
335 return gettable_params
;
338 static const OSSL_PARAM
*cmac_gettable_params(void *provctx
)
340 static const OSSL_PARAM gettable_params
[] = {
341 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
342 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER
, NULL
, 0),
343 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_ENGINE
, NULL
, 0),
346 return gettable_params
;
349 static int mac_set_params(void *keydata
, const OSSL_PARAM params
[])
351 MAC_KEY
*key
= keydata
;
357 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
);
359 return mac_key_fromdata(key
, params
);
364 static const OSSL_PARAM
*mac_settable_params(void *provctx
)
366 static const OSSL_PARAM settable_params
[] = {
367 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
370 return settable_params
;
373 static void *mac_gen_init(void *provctx
, int selection
)
375 OSSL_LIB_CTX
*libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
376 struct mac_gen_ctx
*gctx
= NULL
;
378 if (!ossl_prov_is_running())
381 if ((gctx
= OPENSSL_zalloc(sizeof(*gctx
))) != NULL
) {
382 gctx
->libctx
= libctx
;
383 gctx
->selection
= selection
;
388 static int mac_gen_set_params(void *genctx
, const OSSL_PARAM params
[])
390 struct mac_gen_ctx
*gctx
= genctx
;
396 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
);
398 if (p
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
399 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
402 gctx
->priv_key
= OPENSSL_secure_malloc(p
->data_size
);
403 if (gctx
->priv_key
== NULL
) {
404 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
407 memcpy(gctx
->priv_key
, p
->data
, p
->data_size
);
408 gctx
->priv_key_len
= p
->data_size
;
414 static int cmac_gen_set_params(void *genctx
, const OSSL_PARAM params
[])
416 struct mac_gen_ctx
*gctx
= genctx
;
418 if (!mac_gen_set_params(genctx
, params
))
421 if (!ossl_prov_cipher_load_from_params(&gctx
->cipher
, params
,
423 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
430 static const OSSL_PARAM
*mac_gen_settable_params(void *provctx
)
432 static OSSL_PARAM settable
[] = {
433 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
439 static const OSSL_PARAM
*cmac_gen_settable_params(void *provctx
)
441 static OSSL_PARAM settable
[] = {
442 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
443 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER
, NULL
, 0),
449 static void *mac_gen(void *genctx
, OSSL_CALLBACK
*cb
, void *cbarg
)
451 struct mac_gen_ctx
*gctx
= genctx
;
454 if (!ossl_prov_is_running() || gctx
== NULL
)
457 if ((key
= mac_key_new(gctx
->libctx
, 0)) == NULL
) {
458 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
462 /* If we're doing parameter generation then we just return a blank key */
463 if ((gctx
->selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == 0)
466 if (gctx
->priv_key
== NULL
) {
467 ERR_raise(ERR_LIB_PROV
, EVP_R_INVALID_KEY
);
473 * This is horrible but required for backwards compatibility. We don't
474 * actually do real key generation at all. We simply copy the key that was
475 * previously set in the gctx. Hopefully at some point in the future all
476 * of this can be removed and we will only support the EVP_KDF APIs.
478 if (!ossl_prov_cipher_copy(&key
->cipher
, &gctx
->cipher
)) {
479 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
482 ossl_prov_cipher_reset(&gctx
->cipher
);
483 key
->priv_key
= gctx
->priv_key
;
484 key
->priv_key_len
= gctx
->priv_key_len
;
485 gctx
->priv_key_len
= 0;
486 gctx
->priv_key
= NULL
;
491 static void mac_gen_cleanup(void *genctx
)
493 struct mac_gen_ctx
*gctx
= genctx
;
495 OPENSSL_secure_clear_free(gctx
->priv_key
, gctx
->priv_key_len
);
496 ossl_prov_cipher_reset(&gctx
->cipher
);
500 const OSSL_DISPATCH ossl_mac_legacy_keymgmt_functions
[] = {
501 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))mac_new
},
502 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))mac_free
},
503 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))mac_get_params
},
504 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))mac_gettable_params
},
505 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))mac_set_params
},
506 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))mac_settable_params
},
507 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))mac_has
},
508 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))mac_match
},
509 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))mac_import
},
510 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))mac_imexport_types
},
511 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))mac_export
},
512 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))mac_imexport_types
},
513 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))mac_gen_init
},
514 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))mac_gen_set_params
},
515 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
516 (void (*)(void))mac_gen_settable_params
},
517 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))mac_gen
},
518 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))mac_gen_cleanup
},
522 const OSSL_DISPATCH ossl_cossl_mac_legacy_keymgmt_functions
[] = {
523 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))mac_new_cmac
},
524 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))mac_free
},
525 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))mac_get_params
},
526 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))cmac_gettable_params
},
527 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))mac_set_params
},
528 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))mac_settable_params
},
529 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))mac_has
},
530 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))mac_match
},
531 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))mac_import
},
532 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))cmac_imexport_types
},
533 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))mac_export
},
534 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))cmac_imexport_types
},
535 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))mac_gen_init
},
536 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))cmac_gen_set_params
},
537 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
538 (void (*)(void))cmac_gen_settable_params
},
539 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))mac_gen
},
540 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))mac_gen_cleanup
},