2 * Copyright 2019-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 #include <openssl/crypto.h>
11 #include <openssl/core_dispatch.h>
12 #include <openssl/evp.h>
13 #include <openssl/err.h>
14 #include "internal/provider.h"
15 #include "internal/refcount.h"
16 #include "crypto/evp.h"
17 #include "evp_local.h"
19 static void *keymgmt_new(void)
21 EVP_KEYMGMT
*keymgmt
= NULL
;
23 if ((keymgmt
= OPENSSL_zalloc(sizeof(*keymgmt
))) == NULL
24 || (keymgmt
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
25 EVP_KEYMGMT_free(keymgmt
);
26 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
35 static void *keymgmt_from_dispatch(int name_id
,
36 const OSSL_DISPATCH
*fns
,
39 EVP_KEYMGMT
*keymgmt
= NULL
;
40 int setparamfncnt
= 0, getparamfncnt
= 0;
41 int setgenparamfncnt
= 0;
42 int importfncnt
= 0, exportfncnt
= 0;
44 if ((keymgmt
= keymgmt_new()) == NULL
) {
45 EVP_KEYMGMT_free(keymgmt
);
48 keymgmt
->name_id
= name_id
;
50 for (; fns
->function_id
!= 0; fns
++) {
51 switch (fns
->function_id
) {
52 case OSSL_FUNC_KEYMGMT_NEW
:
53 if (keymgmt
->new == NULL
)
54 keymgmt
->new = OSSL_FUNC_keymgmt_new(fns
);
56 case OSSL_FUNC_KEYMGMT_GEN_INIT
:
57 if (keymgmt
->gen_init
== NULL
)
58 keymgmt
->gen_init
= OSSL_FUNC_keymgmt_gen_init(fns
);
60 case OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE
:
61 if (keymgmt
->gen_set_template
== NULL
)
62 keymgmt
->gen_set_template
=
63 OSSL_FUNC_keymgmt_gen_set_template(fns
);
65 case OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
:
66 if (keymgmt
->gen_set_params
== NULL
) {
68 keymgmt
->gen_set_params
=
69 OSSL_FUNC_keymgmt_gen_set_params(fns
);
72 case OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
:
73 if (keymgmt
->gen_settable_params
== NULL
) {
75 keymgmt
->gen_settable_params
=
76 OSSL_FUNC_keymgmt_gen_settable_params(fns
);
79 case OSSL_FUNC_KEYMGMT_GEN
:
80 if (keymgmt
->gen
== NULL
)
81 keymgmt
->gen
= OSSL_FUNC_keymgmt_gen(fns
);
83 case OSSL_FUNC_KEYMGMT_GEN_CLEANUP
:
84 if (keymgmt
->gen_cleanup
== NULL
)
85 keymgmt
->gen_cleanup
= OSSL_FUNC_keymgmt_gen_cleanup(fns
);
87 case OSSL_FUNC_KEYMGMT_FREE
:
88 if (keymgmt
->free
== NULL
)
89 keymgmt
->free
= OSSL_FUNC_keymgmt_free(fns
);
91 case OSSL_FUNC_KEYMGMT_LOAD
:
92 if (keymgmt
->load
== NULL
)
93 keymgmt
->load
= OSSL_FUNC_keymgmt_load(fns
);
95 case OSSL_FUNC_KEYMGMT_GET_PARAMS
:
96 if (keymgmt
->get_params
== NULL
) {
98 keymgmt
->get_params
= OSSL_FUNC_keymgmt_get_params(fns
);
101 case OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
:
102 if (keymgmt
->gettable_params
== NULL
) {
104 keymgmt
->gettable_params
=
105 OSSL_FUNC_keymgmt_gettable_params(fns
);
108 case OSSL_FUNC_KEYMGMT_SET_PARAMS
:
109 if (keymgmt
->set_params
== NULL
) {
111 keymgmt
->set_params
= OSSL_FUNC_keymgmt_set_params(fns
);
114 case OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS
:
115 if (keymgmt
->settable_params
== NULL
) {
117 keymgmt
->settable_params
=
118 OSSL_FUNC_keymgmt_settable_params(fns
);
121 case OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME
:
122 if (keymgmt
->query_operation_name
== NULL
)
123 keymgmt
->query_operation_name
=
124 OSSL_FUNC_keymgmt_query_operation_name(fns
);
126 case OSSL_FUNC_KEYMGMT_HAS
:
127 if (keymgmt
->has
== NULL
)
128 keymgmt
->has
= OSSL_FUNC_keymgmt_has(fns
);
130 case OSSL_FUNC_KEYMGMT_COPY
:
131 if (keymgmt
->copy
== NULL
)
132 keymgmt
->copy
= OSSL_FUNC_keymgmt_copy(fns
);
134 case OSSL_FUNC_KEYMGMT_VALIDATE
:
135 if (keymgmt
->validate
== NULL
)
136 keymgmt
->validate
= OSSL_FUNC_keymgmt_validate(fns
);
138 case OSSL_FUNC_KEYMGMT_MATCH
:
139 if (keymgmt
->match
== NULL
)
140 keymgmt
->match
= OSSL_FUNC_keymgmt_match(fns
);
142 case OSSL_FUNC_KEYMGMT_IMPORT
:
143 if (keymgmt
->import
== NULL
) {
145 keymgmt
->import
= OSSL_FUNC_keymgmt_import(fns
);
148 case OSSL_FUNC_KEYMGMT_IMPORT_TYPES
:
149 if (keymgmt
->import_types
== NULL
) {
151 keymgmt
->import_types
= OSSL_FUNC_keymgmt_import_types(fns
);
154 case OSSL_FUNC_KEYMGMT_EXPORT
:
155 if (keymgmt
->export
== NULL
) {
157 keymgmt
->export
= OSSL_FUNC_keymgmt_export(fns
);
160 case OSSL_FUNC_KEYMGMT_EXPORT_TYPES
:
161 if (keymgmt
->export_types
== NULL
) {
163 keymgmt
->export_types
= OSSL_FUNC_keymgmt_export_types(fns
);
169 * Try to check that the method is sensible.
170 * At least one constructor and the destructor are MANDATORY
171 * The functions 'has' is MANDATORY
172 * It makes no sense being able to free stuff if you can't create it.
173 * It makes no sense providing OSSL_PARAM descriptors for import and
174 * export if you can't import or export.
176 if (keymgmt
->free
== NULL
177 || (keymgmt
->new == NULL
178 && keymgmt
->gen
== NULL
179 && keymgmt
->load
== NULL
)
180 || keymgmt
->has
== NULL
181 || (getparamfncnt
!= 0 && getparamfncnt
!= 2)
182 || (setparamfncnt
!= 0 && setparamfncnt
!= 2)
183 || (setgenparamfncnt
!= 0 && setgenparamfncnt
!= 2)
184 || (importfncnt
!= 0 && importfncnt
!= 2)
185 || (exportfncnt
!= 0 && exportfncnt
!= 2)
186 || (keymgmt
->gen
!= NULL
187 && (keymgmt
->gen_init
== NULL
188 || keymgmt
->gen_cleanup
== NULL
))) {
189 EVP_KEYMGMT_free(keymgmt
);
190 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
193 keymgmt
->prov
= prov
;
195 ossl_provider_up_ref(prov
);
200 EVP_KEYMGMT
*evp_keymgmt_fetch_by_number(OSSL_LIB_CTX
*ctx
, int name_id
,
201 const char *properties
)
203 return evp_generic_fetch_by_number(ctx
,
204 OSSL_OP_KEYMGMT
, name_id
, properties
,
205 keymgmt_from_dispatch
,
206 (int (*)(void *))EVP_KEYMGMT_up_ref
,
207 (void (*)(void *))EVP_KEYMGMT_free
);
210 EVP_KEYMGMT
*EVP_KEYMGMT_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
211 const char *properties
)
213 return evp_generic_fetch(ctx
, OSSL_OP_KEYMGMT
, algorithm
, properties
,
214 keymgmt_from_dispatch
,
215 (int (*)(void *))EVP_KEYMGMT_up_ref
,
216 (void (*)(void *))EVP_KEYMGMT_free
);
219 int EVP_KEYMGMT_up_ref(EVP_KEYMGMT
*keymgmt
)
223 CRYPTO_UP_REF(&keymgmt
->refcnt
, &ref
, keymgmt
->lock
);
227 void EVP_KEYMGMT_free(EVP_KEYMGMT
*keymgmt
)
234 CRYPTO_DOWN_REF(&keymgmt
->refcnt
, &ref
, keymgmt
->lock
);
237 ossl_provider_free(keymgmt
->prov
);
238 CRYPTO_THREAD_lock_free(keymgmt
->lock
);
239 OPENSSL_free(keymgmt
);
242 const OSSL_PROVIDER
*EVP_KEYMGMT_provider(const EVP_KEYMGMT
*keymgmt
)
244 return keymgmt
->prov
;
247 int EVP_KEYMGMT_number(const EVP_KEYMGMT
*keymgmt
)
249 return keymgmt
->name_id
;
252 const char *EVP_KEYMGMT_get0_first_name(const EVP_KEYMGMT
*keymgmt
)
254 return evp_first_name(keymgmt
->prov
, keymgmt
->name_id
);
257 int EVP_KEYMGMT_is_a(const EVP_KEYMGMT
*keymgmt
, const char *name
)
259 return evp_is_a(keymgmt
->prov
, keymgmt
->name_id
, NULL
, name
);
262 void EVP_KEYMGMT_do_all_provided(OSSL_LIB_CTX
*libctx
,
263 void (*fn
)(EVP_KEYMGMT
*keymgmt
, void *arg
),
266 evp_generic_do_all(libctx
, OSSL_OP_KEYMGMT
,
267 (void (*)(void *, void *))fn
, arg
,
268 keymgmt_from_dispatch
,
269 (void (*)(void *))EVP_KEYMGMT_free
);
272 int EVP_KEYMGMT_names_do_all(const EVP_KEYMGMT
*keymgmt
,
273 void (*fn
)(const char *name
, void *data
),
276 if (keymgmt
->prov
!= NULL
)
277 return evp_names_do_all(keymgmt
->prov
, keymgmt
->name_id
, fn
, data
);
283 * Internal API that interfaces with the method function pointers
285 void *evp_keymgmt_newdata(const EVP_KEYMGMT
*keymgmt
)
287 void *provctx
= ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt
));
290 * TODO(3.0) 'new' is currently mandatory on its own, but when new
291 * constructors appear, it won't be quite as mandatory, so we have
292 * a check for future cases.
294 if (keymgmt
->new == NULL
)
296 return keymgmt
->new(provctx
);
299 void evp_keymgmt_freedata(const EVP_KEYMGMT
*keymgmt
, void *keydata
)
301 /* This is mandatory, no need to check for its presence */
302 keymgmt
->free(keydata
);
305 void *evp_keymgmt_gen_init(const EVP_KEYMGMT
*keymgmt
, int selection
)
307 void *provctx
= ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt
));
309 if (keymgmt
->gen_init
== NULL
)
311 return keymgmt
->gen_init(provctx
, selection
);
314 int evp_keymgmt_gen_set_template(const EVP_KEYMGMT
*keymgmt
, void *genctx
,
318 * It's arguable if we actually should return success in this case, as
319 * it allows the caller to set a template key, which is then ignored.
320 * However, this is how the legacy methods (EVP_PKEY_METHOD) operate,
321 * so we do this in the interest of backward compatibility.
322 * TODO(3.0) Investigate if we should change this behaviour.
324 if (keymgmt
->gen_set_template
== NULL
)
326 return keymgmt
->gen_set_template(genctx
, template);
329 int evp_keymgmt_gen_set_params(const EVP_KEYMGMT
*keymgmt
, void *genctx
,
330 const OSSL_PARAM params
[])
332 if (keymgmt
->gen_set_params
== NULL
)
334 return keymgmt
->gen_set_params(genctx
, params
);
337 const OSSL_PARAM
*EVP_KEYMGMT_gen_settable_params(const EVP_KEYMGMT
*keymgmt
)
339 void *provctx
= ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt
));
341 if (keymgmt
->gen_settable_params
== NULL
)
343 return keymgmt
->gen_settable_params(provctx
);
346 void *evp_keymgmt_gen(const EVP_KEYMGMT
*keymgmt
, void *genctx
,
347 OSSL_CALLBACK
*cb
, void *cbarg
)
349 if (keymgmt
->gen
== NULL
)
351 return keymgmt
->gen(genctx
, cb
, cbarg
);
354 void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT
*keymgmt
, void *genctx
)
356 if (keymgmt
->gen
!= NULL
)
357 keymgmt
->gen_cleanup(genctx
);
360 void *evp_keymgmt_load(const EVP_KEYMGMT
*keymgmt
,
361 const void *objref
, size_t objref_sz
)
363 if (keymgmt
->load
!= NULL
)
364 return keymgmt
->load(objref
, objref_sz
);
368 int evp_keymgmt_get_params(const EVP_KEYMGMT
*keymgmt
, void *keydata
,
371 if (keymgmt
->get_params
== NULL
)
373 return keymgmt
->get_params(keydata
, params
);
376 const OSSL_PARAM
*EVP_KEYMGMT_gettable_params(const EVP_KEYMGMT
*keymgmt
)
378 void *provctx
= ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt
));
380 if (keymgmt
->gettable_params
== NULL
)
382 return keymgmt
->gettable_params(provctx
);
385 int evp_keymgmt_set_params(const EVP_KEYMGMT
*keymgmt
, void *keydata
,
386 const OSSL_PARAM params
[])
388 if (keymgmt
->set_params
== NULL
)
390 return keymgmt
->set_params(keydata
, params
);
393 const OSSL_PARAM
*EVP_KEYMGMT_settable_params(const EVP_KEYMGMT
*keymgmt
)
395 void *provctx
= ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt
));
397 if (keymgmt
->settable_params
== NULL
)
399 return keymgmt
->settable_params(provctx
);
402 int evp_keymgmt_has(const EVP_KEYMGMT
*keymgmt
, void *keydata
, int selection
)
404 /* This is mandatory, no need to check for its presence */
405 return keymgmt
->has(keydata
, selection
);
408 int evp_keymgmt_validate(const EVP_KEYMGMT
*keymgmt
, void *keydata
,
409 int selection
, int checktype
)
411 /* We assume valid if the implementation doesn't have a function */
412 if (keymgmt
->validate
== NULL
)
414 return keymgmt
->validate(keydata
, selection
, checktype
);
417 int evp_keymgmt_match(const EVP_KEYMGMT
*keymgmt
,
418 const void *keydata1
, const void *keydata2
,
421 /* We assume no match if the implementation doesn't have a function */
422 if (keymgmt
->match
== NULL
)
424 return keymgmt
->match(keydata1
, keydata2
, selection
);
427 int evp_keymgmt_import(const EVP_KEYMGMT
*keymgmt
, void *keydata
,
428 int selection
, const OSSL_PARAM params
[])
430 if (keymgmt
->import
== NULL
)
432 return keymgmt
->import(keydata
, selection
, params
);
435 const OSSL_PARAM
*evp_keymgmt_import_types(const EVP_KEYMGMT
*keymgmt
,
438 if (keymgmt
->import_types
== NULL
)
440 return keymgmt
->import_types(selection
);
443 int evp_keymgmt_export(const EVP_KEYMGMT
*keymgmt
, void *keydata
,
444 int selection
, OSSL_CALLBACK
*param_cb
, void *cbarg
)
446 if (keymgmt
->export
== NULL
)
448 return keymgmt
->export(keydata
, selection
, param_cb
, cbarg
);
451 const OSSL_PARAM
*evp_keymgmt_export_types(const EVP_KEYMGMT
*keymgmt
,
454 if (keymgmt
->export_types
== NULL
)
456 return keymgmt
->export_types(selection
);
459 int evp_keymgmt_copy(const EVP_KEYMGMT
*keymgmt
,
460 void *keydata_to
, const void *keydata_from
,
463 /* We assume no copy if the implementation doesn't have a function */
464 if (keymgmt
->copy
== NULL
)
466 return keymgmt
->copy(keydata_to
, keydata_from
, selection
);