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
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/param_build.h"
17 #include "internal/param_build_set.h"
18 #include "prov/implementations.h"
19 #include "prov/providercommon.h"
20 #include "prov/provider_ctx.h"
21 #include "prov/macsignature.h"
24 static OSSL_FUNC_keymgmt_new_fn mac_new
;
25 static OSSL_FUNC_keymgmt_free_fn mac_free
;
26 static OSSL_FUNC_keymgmt_gen_init_fn mac_gen_init
;
27 static OSSL_FUNC_keymgmt_gen_fn mac_gen
;
28 static OSSL_FUNC_keymgmt_gen_cleanup_fn mac_gen_cleanup
;
29 static OSSL_FUNC_keymgmt_get_params_fn mac_get_params
;
30 static OSSL_FUNC_keymgmt_gettable_params_fn mac_gettable_params
;
31 static OSSL_FUNC_keymgmt_set_params_fn mac_set_params
;
32 static OSSL_FUNC_keymgmt_settable_params_fn mac_settable_params
;
33 static OSSL_FUNC_keymgmt_has_fn mac_has
;
34 static OSSL_FUNC_keymgmt_match_fn mac_match
;
35 static OSSL_FUNC_keymgmt_import_fn mac_import
;
36 static OSSL_FUNC_keymgmt_import_types_fn mac_imexport_types
;
37 static OSSL_FUNC_keymgmt_export_fn mac_export
;
38 static OSSL_FUNC_keymgmt_export_types_fn mac_imexport_types
;
43 unsigned char *priv_key
;
47 MAC_KEY
*mac_key_new(OPENSSL_CTX
*libctx
)
49 MAC_KEY
*mackey
= OPENSSL_zalloc(sizeof(*mackey
));
54 mackey
->lock
= CRYPTO_THREAD_lock_new();
55 if (mackey
->lock
== NULL
) {
59 mackey
->libctx
= libctx
;
65 void mac_key_free(MAC_KEY
*mackey
)
72 CRYPTO_DOWN_REF(&mackey
->refcnt
, &ref
, mackey
->lock
);
76 OPENSSL_secure_clear_free(mackey
->priv_key
, mackey
->priv_key_len
);
77 CRYPTO_THREAD_lock_free(mackey
->lock
);
81 int mac_key_up_ref(MAC_KEY
*mackey
)
85 CRYPTO_UP_REF(&mackey
->refcnt
, &ref
, mackey
->lock
);
89 static void *mac_new(void *provctx
)
91 return mac_key_new(PROV_LIBRARY_CONTEXT_OF(provctx
));
94 static void mac_free(void *mackey
)
99 static int mac_has(void *keydata
, int selection
)
101 MAC_KEY
*key
= keydata
;
106 * MAC keys always have all the parameters they need (i.e. none).
107 * Therefore we always return with 1, if asked about parameters.
108 * Similarly for public keys.
112 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
113 ok
= key
->priv_key
!= NULL
;
118 static int mac_match(const void *keydata1
, const void *keydata2
, int selection
)
120 const MAC_KEY
*key1
= keydata1
;
121 const MAC_KEY
*key2
= keydata2
;
124 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
125 if ((key1
->priv_key
== NULL
&& key2
->priv_key
!= NULL
)
126 || (key1
->priv_key
!= NULL
&& key2
->priv_key
== NULL
)
127 || key1
->priv_key_len
!= key2
->priv_key_len
)
130 ok
= ok
&& (key1
->priv_key
== NULL
/* implies key2->privkey == NULL */
131 || CRYPTO_memcmp(key1
->priv_key
, key2
->priv_key
,
132 key1
->priv_key_len
) == 0);
137 static int mac_key_fromdata(MAC_KEY
*key
, const OSSL_PARAM params
[])
141 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
);
143 if (p
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
144 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
147 OPENSSL_secure_clear_free(key
->priv_key
, key
->priv_key_len
);
148 key
->priv_key
= OPENSSL_secure_malloc(p
->data_size
);
149 if (key
->priv_key
== NULL
) {
150 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
153 memcpy(key
->priv_key
, p
->data
, p
->data_size
);
154 key
->priv_key_len
= p
->data_size
;
161 static int mac_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
163 MAC_KEY
*key
= keydata
;
168 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) == 0)
171 return mac_key_fromdata(key
, params
);
174 static int key_to_params(MAC_KEY
*key
, OSSL_PARAM_BLD
*tmpl
,
180 if (key
->priv_key
!= NULL
181 && !ossl_param_build_set_octet_string(tmpl
, params
,
182 OSSL_PKEY_PARAM_PRIV_KEY
,
183 key
->priv_key
, key
->priv_key_len
))
189 static int mac_export(void *keydata
, int selection
, OSSL_CALLBACK
*param_cb
,
192 MAC_KEY
*key
= keydata
;
193 OSSL_PARAM_BLD
*tmpl
;
194 OSSL_PARAM
*params
= NULL
;
200 tmpl
= OSSL_PARAM_BLD_new();
204 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0
205 && !key_to_params(key
, tmpl
, NULL
))
208 params
= OSSL_PARAM_BLD_to_param(tmpl
);
212 ret
= param_cb(params
, cbarg
);
213 OSSL_PARAM_BLD_free_params(params
);
215 OSSL_PARAM_BLD_free(tmpl
);
219 static const OSSL_PARAM mac_key_types
[] = {
220 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
223 static const OSSL_PARAM
*mac_imexport_types(int selection
)
225 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
226 return mac_key_types
;
230 static int mac_get_params(void *key
, OSSL_PARAM params
[])
232 return key_to_params(key
, NULL
, params
);
235 static const OSSL_PARAM
*mac_gettable_params(void *provctx
)
237 static const OSSL_PARAM gettable_params
[] = {
238 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
241 return gettable_params
;
244 static int mac_set_params(void *keydata
, const OSSL_PARAM params
[])
246 MAC_KEY
*key
= keydata
;
252 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
);
254 return mac_key_fromdata(key
, params
);
259 static const OSSL_PARAM
*mac_settable_params(void *provctx
)
261 static const OSSL_PARAM settable_params
[] = {
262 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
265 return settable_params
;
268 static void *mac_gen_init(void *provctx
, int selection
)
270 OPENSSL_CTX
*libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
271 struct mac_gen_ctx
*gctx
= NULL
;
273 if ((gctx
= OPENSSL_malloc(sizeof(*gctx
))) != NULL
) {
274 gctx
->libctx
= libctx
;
275 gctx
->selection
= selection
;
281 static int mac_gen_set_params(void *genctx
, const OSSL_PARAM params
[])
283 struct mac_gen_ctx
*gctx
= genctx
;
289 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
);
291 if (p
->data_type
!= OSSL_PARAM_OCTET_STRING
) {
292 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_INVALID_ARGUMENT
);
295 gctx
->priv_key
= OPENSSL_secure_malloc(p
->data_size
);
296 if (gctx
->priv_key
== NULL
) {
297 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
300 memcpy(gctx
->priv_key
, p
->data
, p
->data_size
);
301 gctx
->priv_key_len
= p
->data_size
;
307 static const OSSL_PARAM
*mac_gen_settable_params(void *provctx
)
309 static OSSL_PARAM settable
[] = {
310 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
, NULL
, 0),
316 static void *mac_gen(void *genctx
, OSSL_CALLBACK
*cb
, void *cbarg
)
318 struct mac_gen_ctx
*gctx
= genctx
;
324 if ((key
= mac_key_new(gctx
->libctx
)) == NULL
) {
325 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
329 /* If we're doing parameter generation then we just return a blank key */
330 if ((gctx
->selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == 0)
333 if (gctx
->priv_key
== NULL
) {
334 ERR_raise(ERR_LIB_PROV
, EVP_R_INVALID_KEY
);
340 * This is horrible but required for backwards compatibility. We don't
341 * actually do real key generation at all. We simply copy the key that was
342 * previously set in the gctx. Hopefully at some point in the future all
343 * of this can be removed and we will only support the EVP_KDF APIs.
345 key
->priv_key
= gctx
->priv_key
;
346 key
->priv_key_len
= gctx
->priv_key_len
;
347 gctx
->priv_key_len
= 0;
348 gctx
->priv_key
= NULL
;
353 static void mac_gen_cleanup(void *genctx
)
355 struct mac_gen_ctx
*gctx
= genctx
;
357 OPENSSL_secure_clear_free(gctx
->priv_key
, gctx
->priv_key_len
);
361 const OSSL_DISPATCH mac_keymgmt_functions
[] = {
362 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))mac_new
},
363 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))mac_free
},
364 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))mac_get_params
},
365 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))mac_gettable_params
},
366 { OSSL_FUNC_KEYMGMT_SET_PARAMS
, (void (*) (void))mac_set_params
},
367 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
, (void (*) (void))mac_settable_params
},
368 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))mac_has
},
369 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))mac_match
},
370 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))mac_import
},
371 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))mac_imexport_types
},
372 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))mac_export
},
373 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))mac_imexport_types
},
374 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))mac_gen_init
},
375 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
, (void (*)(void))mac_gen_set_params
},
376 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
377 (void (*)(void))mac_gen_settable_params
},
378 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))mac_gen
},
379 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))mac_gen_cleanup
},