2 * Copyright 2019 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_numbers.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"
20 static void *keymgmt_new(void)
22 EVP_KEYMGMT
*keymgmt
= NULL
;
24 if ((keymgmt
= OPENSSL_zalloc(sizeof(*keymgmt
))) == NULL
25 || (keymgmt
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
26 EVP_KEYMGMT_free(keymgmt
);
27 EVPerr(0, ERR_R_MALLOC_FAILURE
);
36 static void *keymgmt_from_dispatch(int name_id
,
37 const OSSL_DISPATCH
*fns
,
40 EVP_KEYMGMT
*keymgmt
= NULL
;
42 if ((keymgmt
= keymgmt_new()) == NULL
) {
43 EVP_KEYMGMT_free(keymgmt
);
46 keymgmt
->name_id
= name_id
;
48 for (; fns
->function_id
!= 0; fns
++) {
49 switch (fns
->function_id
) {
50 case OSSL_FUNC_KEYMGMT_IMPORTDOMPARAMS
:
51 if (keymgmt
->importdomparams
!= NULL
)
53 keymgmt
->importdomparams
=
54 OSSL_get_OP_keymgmt_importdomparams(fns
);
56 case OSSL_FUNC_KEYMGMT_GENDOMPARAMS
:
57 if (keymgmt
->gendomparams
!= NULL
)
59 keymgmt
->gendomparams
= OSSL_get_OP_keymgmt_gendomparams(fns
);
61 case OSSL_FUNC_KEYMGMT_FREEDOMPARAMS
:
62 if (keymgmt
->freedomparams
!= NULL
)
64 keymgmt
->freedomparams
= OSSL_get_OP_keymgmt_freedomparams(fns
);
66 case OSSL_FUNC_KEYMGMT_EXPORTDOMPARAMS
:
67 if (keymgmt
->exportdomparams
!= NULL
)
69 keymgmt
->exportdomparams
=
70 OSSL_get_OP_keymgmt_exportdomparams(fns
);
72 case OSSL_FUNC_KEYMGMT_IMPORTDOMPARAM_TYPES
:
73 if (keymgmt
->importdomparam_types
!= NULL
)
75 keymgmt
->importdomparam_types
=
76 OSSL_get_OP_keymgmt_importdomparam_types(fns
);
78 case OSSL_FUNC_KEYMGMT_EXPORTDOMPARAM_TYPES
:
79 if (keymgmt
->exportdomparam_types
!= NULL
)
81 keymgmt
->exportdomparam_types
=
82 OSSL_get_OP_keymgmt_exportdomparam_types(fns
);
84 case OSSL_FUNC_KEYMGMT_IMPORTKEY
:
85 if (keymgmt
->importkey
!= NULL
)
87 keymgmt
->importkey
= OSSL_get_OP_keymgmt_importkey(fns
);
89 case OSSL_FUNC_KEYMGMT_GENKEY
:
90 if (keymgmt
->genkey
!= NULL
)
92 keymgmt
->genkey
= OSSL_get_OP_keymgmt_genkey(fns
);
94 case OSSL_FUNC_KEYMGMT_LOADKEY
:
95 if (keymgmt
->loadkey
!= NULL
)
97 keymgmt
->loadkey
= OSSL_get_OP_keymgmt_loadkey(fns
);
99 case OSSL_FUNC_KEYMGMT_FREEKEY
:
100 if (keymgmt
->freekey
!= NULL
)
102 keymgmt
->freekey
= OSSL_get_OP_keymgmt_freekey(fns
);
104 case OSSL_FUNC_KEYMGMT_EXPORTKEY
:
105 if (keymgmt
->exportkey
!= NULL
)
107 keymgmt
->exportkey
= OSSL_get_OP_keymgmt_exportkey(fns
);
109 case OSSL_FUNC_KEYMGMT_IMPORTKEY_TYPES
:
110 if (keymgmt
->importkey_types
!= NULL
)
112 keymgmt
->importkey_types
=
113 OSSL_get_OP_keymgmt_importkey_types(fns
);
115 case OSSL_FUNC_KEYMGMT_EXPORTKEY_TYPES
:
116 if (keymgmt
->exportkey_types
!= NULL
)
118 keymgmt
->exportkey_types
=
119 OSSL_get_OP_keymgmt_exportkey_types(fns
);
121 case OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME
:
122 if (keymgmt
->query_operation_name
!= NULL
)
124 keymgmt
->query_operation_name
=
125 OSSL_get_OP_keymgmt_query_operation_name(fns
);
130 * Try to check that the method is sensible.
131 * It makes no sense being able to free stuff if you can't create it.
132 * It makes no sense providing OSSL_PARAM descriptors for import and
133 * export if you can't import or export.
135 if ((keymgmt
->freedomparams
!= NULL
136 && (keymgmt
->importdomparams
== NULL
137 && keymgmt
->gendomparams
== NULL
))
138 || (keymgmt
->freekey
!= NULL
139 && (keymgmt
->importkey
== NULL
140 && keymgmt
->genkey
== NULL
141 && keymgmt
->loadkey
== NULL
))
142 || (keymgmt
->importdomparam_types
!= NULL
143 && keymgmt
->importdomparams
== NULL
)
144 || (keymgmt
->exportdomparam_types
!= NULL
145 && keymgmt
->exportdomparams
== NULL
)
146 || (keymgmt
->importkey_types
!= NULL
147 && keymgmt
->importkey
== NULL
)
148 || (keymgmt
->exportkey_types
!= NULL
149 && keymgmt
->exportkey
== NULL
)) {
150 EVP_KEYMGMT_free(keymgmt
);
151 EVPerr(0, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
154 keymgmt
->prov
= prov
;
156 ossl_provider_up_ref(prov
);
161 EVP_KEYMGMT
*evp_keymgmt_fetch_by_number(OPENSSL_CTX
*ctx
, int name_id
,
162 const char *properties
)
164 return evp_generic_fetch_by_number(ctx
,
165 OSSL_OP_KEYMGMT
, name_id
, properties
,
166 keymgmt_from_dispatch
,
167 (int (*)(void *))EVP_KEYMGMT_up_ref
,
168 (void (*)(void *))EVP_KEYMGMT_free
);
171 EVP_KEYMGMT
*EVP_KEYMGMT_fetch(OPENSSL_CTX
*ctx
, const char *algorithm
,
172 const char *properties
)
174 return evp_generic_fetch(ctx
, OSSL_OP_KEYMGMT
, algorithm
, properties
,
175 keymgmt_from_dispatch
,
176 (int (*)(void *))EVP_KEYMGMT_up_ref
,
177 (void (*)(void *))EVP_KEYMGMT_free
);
180 int EVP_KEYMGMT_up_ref(EVP_KEYMGMT
*keymgmt
)
184 CRYPTO_UP_REF(&keymgmt
->refcnt
, &ref
, keymgmt
->lock
);
188 void EVP_KEYMGMT_free(EVP_KEYMGMT
*keymgmt
)
195 CRYPTO_DOWN_REF(&keymgmt
->refcnt
, &ref
, keymgmt
->lock
);
198 ossl_provider_free(keymgmt
->prov
);
199 CRYPTO_THREAD_lock_free(keymgmt
->lock
);
200 OPENSSL_free(keymgmt
);
203 const OSSL_PROVIDER
*EVP_KEYMGMT_provider(const EVP_KEYMGMT
*keymgmt
)
205 return keymgmt
->prov
;
208 int EVP_KEYMGMT_number(const EVP_KEYMGMT
*keymgmt
)
210 return keymgmt
->name_id
;
213 int EVP_KEYMGMT_is_a(const EVP_KEYMGMT
*keymgmt
, const char *name
)
215 return evp_is_a(keymgmt
->prov
, keymgmt
->name_id
, name
);
218 void EVP_KEYMGMT_do_all_provided(OPENSSL_CTX
*libctx
,
219 void (*fn
)(EVP_KEYMGMT
*keymgmt
, void *arg
),
222 evp_generic_do_all(libctx
, OSSL_OP_KEYMGMT
,
223 (void (*)(void *, void *))fn
, arg
,
224 keymgmt_from_dispatch
,
225 (void (*)(void *))EVP_KEYMGMT_free
);
228 void EVP_KEYMGMT_names_do_all(const EVP_KEYMGMT
*keymgmt
,
229 void (*fn
)(const char *name
, void *data
),
232 if (keymgmt
->prov
!= NULL
)
233 evp_names_do_all(keymgmt
->prov
, keymgmt
->name_id
, fn
, data
);