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_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
;
46 unsigned char *priv_key
;
51 MAC_KEY
*mac_key_new(OPENSSL_CTX
*libctx
, int cmac
)
53 MAC_KEY
*mackey
= OPENSSL_zalloc(sizeof(*mackey
));
58 mackey
->lock
= CRYPTO_THREAD_lock_new();
59 if (mackey
->lock
== NULL
) {
63 mackey
->libctx
= libctx
;
70 void mac_key_free(MAC_KEY
*mackey
)
77 CRYPTO_DOWN_REF(&mackey
->refcnt
, &ref
, mackey
->lock
);
81 OPENSSL_secure_clear_free(mackey
->priv_key
, mackey
->priv_key_len
);
82 OPENSSL_free(mackey
->properties
);
83 ossl_prov_cipher_reset(&mackey
->cipher
);
84 CRYPTO_THREAD_lock_free(mackey
->lock
);
88 int mac_key_up_ref(MAC_KEY
*mackey
)
92 CRYPTO_UP_REF(&mackey
->refcnt
, &ref
, mackey
->lock
);
96 static void *mac_new(void *provctx
)
98 return mac_key_new(PROV_LIBRARY_CONTEXT_OF(provctx
), 0);
101 static void *mac_new_cmac(void *provctx
)
103 return mac_key_new(PROV_LIBRARY_CONTEXT_OF(provctx
), 1);
106 static void mac_free(void *mackey
)
108 mac_key_free(mackey
);
111 static int mac_has(void *keydata
, int selection
)
113 MAC_KEY
*key
= keydata
;
118 * MAC keys always have all the parameters they need (i.e. none).
119 * Therefore we always return with 1, if asked about parameters.
120 * Similarly for public keys.
124 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
125 ok
= key
->priv_key
!= NULL
;
130 static int mac_match(const void *keydata1
, const void *keydata2
, int selection
)
132 const MAC_KEY
*key1
= keydata1
;
133 const MAC_KEY
*key2
= keydata2
;
136 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
137 if ((key1
->priv_key
== NULL
&& key2
->priv_key
!= NULL
)
138 || (key1
->priv_key
!= NULL
&& key2
->priv_key
== NULL
)
139 || key1
->priv_key_len
!= key2
->priv_key_len
140 || (key1
->cipher
.cipher
== NULL
&& key2
->cipher
.cipher
!= NULL
)
141 || (key1
->cipher
.cipher
!= NULL
&& key2
->cipher
.cipher
== NULL
))
144 ok
= ok
&& (key1
->priv_key
== NULL
/* implies key2->privkey == NULL */
145 || CRYPTO_memcmp(key1
->priv_key
, key2
->priv_key
,
146 key1
->priv_key_len
) == 0);
147 if (key1
->cipher
.cipher
!= NULL
)
148 ok
= ok
&& EVP_CIPHER_is_a(key1
->cipher
.cipher
,
149 EVP_CIPHER_name(key2
->cipher
.cipher
));
154 static int mac_key_fromdata(MAC_KEY
*key
, const OSSL_PARAM params
[])
158 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
);
160 if (p
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
161 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
164 OPENSSL_secure_clear_free(key
->priv_key
, key
->priv_key_len
);
165 key
->priv_key
= OPENSSL_secure_malloc(p
->data_size
);
166 if (key
->priv_key
== NULL
) {
167 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
170 memcpy(key
->priv_key
, p
->data
, p
->data_size
);
171 key
->priv_key_len
= p
->data_size
;
174 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PROPERTIES
);
176 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
) {
177 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
180 OPENSSL_free(key
->properties
);
181 key
->properties
= OPENSSL_strdup(p
->data
);
182 if (key
->properties
== NULL
) {
183 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
188 if (key
->cmac
&& !ossl_prov_cipher_load_from_params(&key
->cipher
, params
,
190 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
194 if (key
->priv_key
!= NULL
)
200 static int mac_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
202 MAC_KEY
*key
= keydata
;
207 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) == 0)
210 return mac_key_fromdata(key
, params
);
213 static int key_to_params(MAC_KEY
*key
, OSSL_PARAM_BLD
*tmpl
,
219 if (key
->priv_key
!= NULL
220 && !ossl_param_build_set_octet_string(tmpl
, params
,
221 OSSL_PKEY_PARAM_PRIV_KEY
,
222 key
->priv_key
, key
->priv_key_len
))
225 if (key
->cipher
.cipher
!= NULL
226 && !ossl_param_build_set_utf8_string(tmpl
, params
,
227 OSSL_PKEY_PARAM_CIPHER
,
228 EVP_CIPHER_name(key
->cipher
.cipher
)))
231 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
232 if (key
->cipher
.engine
!= NULL
233 && !ossl_param_build_set_utf8_string(tmpl
, params
,
234 OSSL_PKEY_PARAM_ENGINE
,
235 ENGINE_get_id(key
->cipher
.engine
)))
242 static int mac_export(void *keydata
, int selection
, OSSL_CALLBACK
*param_cb
,
245 MAC_KEY
*key
= keydata
;
246 OSSL_PARAM_BLD
*tmpl
;
247 OSSL_PARAM
*params
= NULL
;
253 tmpl
= OSSL_PARAM_BLD_new();
257 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
258 && !key_to_params(key
, tmpl
, NULL
))
261 params
= OSSL_PARAM_BLD_to_param(tmpl
);
265 ret
= param_cb(params
, cbarg
);
266 OSSL_PARAM_BLD_free_params(params
);
268 OSSL_PARAM_BLD_free(tmpl
);
272 static const OSSL_PARAM mac_key_types
[] = {
273 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
274 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES
, NULL
, 0),
277 static const OSSL_PARAM
*mac_imexport_types(int selection
)
279 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
280 return mac_key_types
;
284 static const OSSL_PARAM cmac_key_types
[] = {
285 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
286 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER
, NULL
, 0),
287 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_ENGINE
, NULL
, 0),
288 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES
, NULL
, 0),
291 static const OSSL_PARAM
*cmac_imexport_types(int selection
)
293 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
294 return cmac_key_types
;
298 static int mac_get_params(void *key
, OSSL_PARAM params
[])
300 return key_to_params(key
, NULL
, params
);
303 static const OSSL_PARAM
*mac_gettable_params(void *provctx
)
305 static const OSSL_PARAM gettable_params
[] = {
306 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
309 return gettable_params
;
312 static const OSSL_PARAM
*cmac_gettable_params(void *provctx
)
314 static const OSSL_PARAM gettable_params
[] = {
315 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
316 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER
, NULL
, 0),
317 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_ENGINE
, NULL
, 0),
320 return gettable_params
;
324 static int mac_set_params(void *keydata
, const OSSL_PARAM params
[])
326 MAC_KEY
*key
= keydata
;
332 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
);
334 return mac_key_fromdata(key
, params
);
339 static const OSSL_PARAM
*mac_settable_params(void *provctx
)
341 static const OSSL_PARAM settable_params
[] = {
342 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
345 return settable_params
;
348 static void *mac_gen_init(void *provctx
, int selection
)
350 OPENSSL_CTX
*libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
351 struct mac_gen_ctx
*gctx
= NULL
;
353 if ((gctx
= OPENSSL_zalloc(sizeof(*gctx
))) != NULL
) {
354 gctx
->libctx
= libctx
;
355 gctx
->selection
= selection
;
360 static int mac_gen_set_params(void *genctx
, const OSSL_PARAM params
[])
362 struct mac_gen_ctx
*gctx
= genctx
;
368 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
);
370 if (p
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
371 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
374 gctx
->priv_key
= OPENSSL_secure_malloc(p
->data_size
);
375 if (gctx
->priv_key
== NULL
) {
376 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
379 memcpy(gctx
->priv_key
, p
->data
, p
->data_size
);
380 gctx
->priv_key_len
= p
->data_size
;
386 static int cmac_gen_set_params(void *genctx
, const OSSL_PARAM params
[])
388 struct mac_gen_ctx
*gctx
= genctx
;
390 if (!mac_gen_set_params(genctx
, params
))
393 if (!ossl_prov_cipher_load_from_params(&gctx
->cipher
, params
,
395 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
402 static const OSSL_PARAM
*mac_gen_settable_params(void *provctx
)
404 static OSSL_PARAM settable
[] = {
405 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
411 static const OSSL_PARAM
*cmac_gen_settable_params(void *provctx
)
413 static OSSL_PARAM settable
[] = {
414 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
415 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER
, NULL
, 0),
421 static void *mac_gen(void *genctx
, OSSL_CALLBACK
*cb
, void *cbarg
)
423 struct mac_gen_ctx
*gctx
= genctx
;
429 if ((key
= mac_key_new(gctx
->libctx
, 0)) == NULL
) {
430 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
434 /* If we're doing parameter generation then we just return a blank key */
435 if ((gctx
->selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == 0)
438 if (gctx
->priv_key
== NULL
) {
439 ERR_raise(ERR_LIB_PROV
, EVP_R_INVALID_KEY
);
445 * This is horrible but required for backwards compatibility. We don't
446 * actually do real key generation at all. We simply copy the key that was
447 * previously set in the gctx. Hopefully at some point in the future all
448 * of this can be removed and we will only support the EVP_KDF APIs.
450 if (!ossl_prov_cipher_copy(&key
->cipher
, &gctx
->cipher
)) {
451 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
454 ossl_prov_cipher_reset(&gctx
->cipher
);
455 key
->priv_key
= gctx
->priv_key
;
456 key
->priv_key_len
= gctx
->priv_key_len
;
457 gctx
->priv_key_len
= 0;
458 gctx
->priv_key
= NULL
;
463 static void mac_gen_cleanup(void *genctx
)
465 struct mac_gen_ctx
*gctx
= genctx
;
467 OPENSSL_secure_clear_free(gctx
->priv_key
, gctx
->priv_key_len
);
468 ossl_prov_cipher_reset(&gctx
->cipher
);
472 const OSSL_DISPATCH mac_keymgmt_functions
[] = {
473 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))mac_new
},
474 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))mac_free
},
475 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))mac_get_params
},
476 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))mac_gettable_params
},
477 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))mac_set_params
},
478 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))mac_settable_params
},
479 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))mac_has
},
480 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))mac_match
},
481 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))mac_import
},
482 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))mac_imexport_types
},
483 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))mac_export
},
484 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))mac_imexport_types
},
485 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))mac_gen_init
},
486 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))mac_gen_set_params
},
487 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
488 (void (*)(void))mac_gen_settable_params
},
489 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))mac_gen
},
490 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))mac_gen_cleanup
},
494 const OSSL_DISPATCH cmac_keymgmt_functions
[] = {
495 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))mac_new_cmac
},
496 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))mac_free
},
497 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))mac_get_params
},
498 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))cmac_gettable_params
},
499 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))mac_set_params
},
500 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))mac_settable_params
},
501 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))mac_has
},
502 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))mac_match
},
503 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))mac_import
},
504 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))cmac_imexport_types
},
505 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))mac_export
},
506 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))cmac_imexport_types
},
507 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))mac_gen_init
},
508 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))cmac_gen_set_params
},
509 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
510 (void (*)(void))cmac_gen_settable_params
},
511 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))mac_gen
},
512 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))mac_gen_cleanup
},