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 "internal/evp_int.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
,
41 EVP_KEYMGMT
*keymgmt
= NULL
;
43 if ((keymgmt
= keymgmt_new()) == NULL
) {
44 EVP_KEYMGMT_free(keymgmt
);
47 keymgmt
->name_id
= name_id
;
49 for (; fns
->function_id
!= 0; fns
++) {
50 switch (fns
->function_id
) {
51 case OSSL_FUNC_KEYMGMT_IMPORTDOMPARAMS
:
52 if (keymgmt
->importdomparams
!= NULL
)
54 keymgmt
->importdomparams
=
55 OSSL_get_OP_keymgmt_importdomparams(fns
);
57 case OSSL_FUNC_KEYMGMT_GENDOMPARAMS
:
58 if (keymgmt
->gendomparams
!= NULL
)
60 keymgmt
->gendomparams
= OSSL_get_OP_keymgmt_gendomparams(fns
);
62 case OSSL_FUNC_KEYMGMT_FREEDOMPARAMS
:
63 if (keymgmt
->freedomparams
!= NULL
)
65 keymgmt
->freedomparams
= OSSL_get_OP_keymgmt_freedomparams(fns
);
67 case OSSL_FUNC_KEYMGMT_EXPORTDOMPARAMS
:
68 if (keymgmt
->exportdomparams
!= NULL
)
70 keymgmt
->exportdomparams
=
71 OSSL_get_OP_keymgmt_exportdomparams(fns
);
73 case OSSL_FUNC_KEYMGMT_IMPORTDOMPARAM_TYPES
:
74 if (keymgmt
->importdomparam_types
!= NULL
)
76 keymgmt
->importdomparam_types
=
77 OSSL_get_OP_keymgmt_importdomparam_types(fns
);
79 case OSSL_FUNC_KEYMGMT_EXPORTDOMPARAM_TYPES
:
80 if (keymgmt
->exportdomparam_types
!= NULL
)
82 keymgmt
->exportdomparam_types
=
83 OSSL_get_OP_keymgmt_exportdomparam_types(fns
);
85 case OSSL_FUNC_KEYMGMT_IMPORTKEY
:
86 if (keymgmt
->importkey
!= NULL
)
88 keymgmt
->importkey
= OSSL_get_OP_keymgmt_importkey(fns
);
90 case OSSL_FUNC_KEYMGMT_GENKEY
:
91 if (keymgmt
->genkey
!= NULL
)
93 keymgmt
->genkey
= OSSL_get_OP_keymgmt_genkey(fns
);
95 case OSSL_FUNC_KEYMGMT_LOADKEY
:
96 if (keymgmt
->loadkey
!= NULL
)
98 keymgmt
->loadkey
= OSSL_get_OP_keymgmt_loadkey(fns
);
100 case OSSL_FUNC_KEYMGMT_FREEKEY
:
101 if (keymgmt
->freekey
!= NULL
)
103 keymgmt
->freekey
= OSSL_get_OP_keymgmt_freekey(fns
);
105 case OSSL_FUNC_KEYMGMT_EXPORTKEY
:
106 if (keymgmt
->exportkey
!= NULL
)
108 keymgmt
->exportkey
= OSSL_get_OP_keymgmt_exportkey(fns
);
110 case OSSL_FUNC_KEYMGMT_IMPORTKEY_TYPES
:
111 if (keymgmt
->importkey_types
!= NULL
)
113 keymgmt
->importkey_types
=
114 OSSL_get_OP_keymgmt_importkey_types(fns
);
116 case OSSL_FUNC_KEYMGMT_EXPORTKEY_TYPES
:
117 if (keymgmt
->exportkey_types
!= NULL
)
119 keymgmt
->exportkey_types
=
120 OSSL_get_OP_keymgmt_exportkey_types(fns
);
125 * Try to check that the method is sensible.
126 * It makes no sense being able to free stuff if you can't create it.
127 * It makes no sense providing OSSL_PARAM descriptors for import and
128 * export if you can't import or export.
130 if ((keymgmt
->freedomparams
!= NULL
131 && (keymgmt
->importdomparams
== NULL
132 && keymgmt
->gendomparams
== NULL
))
133 || (keymgmt
->freekey
!= NULL
134 && (keymgmt
->importkey
== NULL
135 && keymgmt
->genkey
== NULL
136 && keymgmt
->loadkey
== NULL
))
137 || (keymgmt
->importdomparam_types
!= NULL
138 && keymgmt
->importdomparams
== NULL
)
139 || (keymgmt
->exportdomparam_types
!= NULL
140 && keymgmt
->exportdomparams
== NULL
)
141 || (keymgmt
->importkey_types
!= NULL
142 && keymgmt
->importkey
== NULL
)
143 || (keymgmt
->exportkey_types
!= NULL
144 && keymgmt
->exportkey
== NULL
)) {
145 EVP_KEYMGMT_free(keymgmt
);
146 EVPerr(0, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
149 keymgmt
->prov
= prov
;
151 ossl_provider_up_ref(prov
);
156 EVP_KEYMGMT
*evp_keymgmt_fetch_by_number(OPENSSL_CTX
*ctx
, int name_id
,
157 const char *properties
)
159 return evp_generic_fetch_by_number(ctx
,
160 OSSL_OP_KEYMGMT
, name_id
, properties
,
161 keymgmt_from_dispatch
, NULL
,
162 (int (*)(void *))EVP_KEYMGMT_up_ref
,
163 (void (*)(void *))EVP_KEYMGMT_free
);
166 EVP_KEYMGMT
*EVP_KEYMGMT_fetch(OPENSSL_CTX
*ctx
, const char *algorithm
,
167 const char *properties
)
169 return evp_generic_fetch(ctx
, OSSL_OP_KEYMGMT
, algorithm
, properties
,
170 keymgmt_from_dispatch
, NULL
,
171 (int (*)(void *))EVP_KEYMGMT_up_ref
,
172 (void (*)(void *))EVP_KEYMGMT_free
);
175 int EVP_KEYMGMT_up_ref(EVP_KEYMGMT
*keymgmt
)
179 CRYPTO_UP_REF(&keymgmt
->refcnt
, &ref
, keymgmt
->lock
);
183 void EVP_KEYMGMT_free(EVP_KEYMGMT
*keymgmt
)
190 CRYPTO_DOWN_REF(&keymgmt
->refcnt
, &ref
, keymgmt
->lock
);
193 ossl_provider_free(keymgmt
->prov
);
194 CRYPTO_THREAD_lock_free(keymgmt
->lock
);
195 OPENSSL_free(keymgmt
);
198 const OSSL_PROVIDER
*EVP_KEYMGMT_provider(const EVP_KEYMGMT
*keymgmt
)
200 return keymgmt
->prov
;