1/*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
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"
18
19
20static void *keymgmt_new(void)
21{
22 EVP_KEYMGMT *keymgmt = NULL;
23
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);
28 return NULL;
29 }
30
31 keymgmt->refcnt = 1;
32
33 return keymgmt;
34}
35
36static void *keymgmt_from_dispatch(int name_id,
37 const OSSL_DISPATCH *fns,
38 OSSL_PROVIDER *prov)
39{
40 EVP_KEYMGMT *keymgmt = NULL;
41
42 if ((keymgmt = keymgmt_new()) == NULL) {
43 EVP_KEYMGMT_free(keymgmt);
44 return NULL;
45 }
46 keymgmt->name_id = name_id;
47
48 for (; fns->function_id != 0; fns++) {
49 switch (fns->function_id) {
50 case OSSL_FUNC_KEYMGMT_IMPORTDOMPARAMS:
51 if (keymgmt->importdomparams != NULL)
52 break;
53 keymgmt->importdomparams =
54 OSSL_get_OP_keymgmt_importdomparams(fns);
55 break;
56 case OSSL_FUNC_KEYMGMT_GENDOMPARAMS:
57 if (keymgmt->gendomparams != NULL)
58 break;
59 keymgmt->gendomparams = OSSL_get_OP_keymgmt_gendomparams(fns);
60 break;
61 case OSSL_FUNC_KEYMGMT_FREEDOMPARAMS:
62 if (keymgmt->freedomparams != NULL)
63 break;
64 keymgmt->freedomparams = OSSL_get_OP_keymgmt_freedomparams(fns);
65 break;
66 case OSSL_FUNC_KEYMGMT_EXPORTDOMPARAMS:
67 if (keymgmt->exportdomparams != NULL)
68 break;
69 keymgmt->exportdomparams =
70 OSSL_get_OP_keymgmt_exportdomparams(fns);
71 break;
72 case OSSL_FUNC_KEYMGMT_IMPORTDOMPARAM_TYPES:
73 if (keymgmt->importdomparam_types != NULL)
74 break;
75 keymgmt->importdomparam_types =
76 OSSL_get_OP_keymgmt_importdomparam_types(fns);
77 break;
78 case OSSL_FUNC_KEYMGMT_EXPORTDOMPARAM_TYPES:
79 if (keymgmt->exportdomparam_types != NULL)
80 break;
81 keymgmt->exportdomparam_types =
82 OSSL_get_OP_keymgmt_exportdomparam_types(fns);
83 break;
84 case OSSL_FUNC_KEYMGMT_IMPORTKEY:
85 if (keymgmt->importkey != NULL)
86 break;
87 keymgmt->importkey = OSSL_get_OP_keymgmt_importkey(fns);
88 break;
89 case OSSL_FUNC_KEYMGMT_GENKEY:
90 if (keymgmt->genkey != NULL)
91 break;
92 keymgmt->genkey = OSSL_get_OP_keymgmt_genkey(fns);
93 break;
94 case OSSL_FUNC_KEYMGMT_LOADKEY:
95 if (keymgmt->loadkey != NULL)
96 break;
97 keymgmt->loadkey = OSSL_get_OP_keymgmt_loadkey(fns);
98 break;
99 case OSSL_FUNC_KEYMGMT_FREEKEY:
100 if (keymgmt->freekey != NULL)
101 break;
102 keymgmt->freekey = OSSL_get_OP_keymgmt_freekey(fns);
103 break;
104 case OSSL_FUNC_KEYMGMT_EXPORTKEY:
105 if (keymgmt->exportkey != NULL)
106 break;
107 keymgmt->exportkey = OSSL_get_OP_keymgmt_exportkey(fns);
108 break;
109 case OSSL_FUNC_KEYMGMT_IMPORTKEY_TYPES:
110 if (keymgmt->importkey_types != NULL)
111 break;
112 keymgmt->importkey_types =
113 OSSL_get_OP_keymgmt_importkey_types(fns);
114 break;
115 case OSSL_FUNC_KEYMGMT_EXPORTKEY_TYPES:
116 if (keymgmt->exportkey_types != NULL)
117 break;
118 keymgmt->exportkey_types =
119 OSSL_get_OP_keymgmt_exportkey_types(fns);
120 break;
121 }
122 }
123 /*
124 * Try to check that the method is sensible.
125 * It makes no sense being able to free stuff if you can't create it.
126 * It makes no sense providing OSSL_PARAM descriptors for import and
127 * export if you can't import or export.
128 */
129 if ((keymgmt->freedomparams != NULL
130 && (keymgmt->importdomparams == NULL
131 && keymgmt->gendomparams == NULL))
132 || (keymgmt->freekey != NULL
133 && (keymgmt->importkey == NULL
134 && keymgmt->genkey == NULL
135 && keymgmt->loadkey == NULL))
136 || (keymgmt->importdomparam_types != NULL
137 && keymgmt->importdomparams == NULL)
138 || (keymgmt->exportdomparam_types != NULL
139 && keymgmt->exportdomparams == NULL)
140 || (keymgmt->importkey_types != NULL
141 && keymgmt->importkey == NULL)
142 || (keymgmt->exportkey_types != NULL
143 && keymgmt->exportkey == NULL)) {
144 EVP_KEYMGMT_free(keymgmt);
145 EVPerr(0, EVP_R_INVALID_PROVIDER_FUNCTIONS);
146 return NULL;
147 }
148 keymgmt->prov = prov;
149 if (prov != NULL)
150 ossl_provider_up_ref(prov);
151
152 return keymgmt;
153}
154
155EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OPENSSL_CTX *ctx, int name_id,
156 const char *properties)
157{
158 return evp_generic_fetch_by_number(ctx,
159 OSSL_OP_KEYMGMT, name_id, properties,
160 keymgmt_from_dispatch,
161 (int (*)(void *))EVP_KEYMGMT_up_ref,
162 (void (*)(void *))EVP_KEYMGMT_free);
163}
164
165EVP_KEYMGMT *EVP_KEYMGMT_fetch(OPENSSL_CTX *ctx, const char *algorithm,
166 const char *properties)
167{
168 return evp_generic_fetch(ctx, OSSL_OP_KEYMGMT, algorithm, properties,
169 keymgmt_from_dispatch,
170 (int (*)(void *))EVP_KEYMGMT_up_ref,
171 (void (*)(void *))EVP_KEYMGMT_free);
172}
173
174int EVP_KEYMGMT_up_ref(EVP_KEYMGMT *keymgmt)
175{
176 int ref = 0;
177
178 CRYPTO_UP_REF(&keymgmt->refcnt, &ref, keymgmt->lock);
179 return 1;
180}
181
182void EVP_KEYMGMT_free(EVP_KEYMGMT *keymgmt)
183{
184 int ref = 0;
185
186 if (keymgmt == NULL)
187 return;
188
189 CRYPTO_DOWN_REF(&keymgmt->refcnt, &ref, keymgmt->lock);
190 if (ref > 0)
191 return;
192 ossl_provider_free(keymgmt->prov);
193 CRYPTO_THREAD_lock_free(keymgmt->lock);
194 OPENSSL_free(keymgmt);
195}
196
197const OSSL_PROVIDER *EVP_KEYMGMT_provider(const EVP_KEYMGMT *keymgmt)
198{
199 return keymgmt->prov;
200}
201
202int EVP_KEYMGMT_number(const EVP_KEYMGMT *keymgmt)
203{
204 return keymgmt->name_id;
205}
206
207int EVP_KEYMGMT_is_a(const EVP_KEYMGMT *keymgmt, const char *name)
208{
209 return evp_is_a(keymgmt->prov, keymgmt->name_id, name);
210}
211
212void EVP_KEYMGMT_do_all_provided(OPENSSL_CTX *libctx,
213 void (*fn)(EVP_KEYMGMT *keymgmt, void *arg),
214 void *arg)
215{
216 evp_generic_do_all(libctx, OSSL_OP_KEYMGMT,
217 (void (*)(void *, void *))fn, arg,
218 keymgmt_from_dispatch,
219 (void (*)(void *))EVP_KEYMGMT_free);
220}
221
222void EVP_KEYMGMT_names_do_all(const EVP_KEYMGMT *keymgmt,
223 void (*fn)(const char *name, void *data),
224 void *data)
225{
226 if (keymgmt->prov != NULL)
227 evp_names_do_all(keymgmt->prov, keymgmt->name_id, fn, data);
228}
229