]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/keymgmt_meth.c
Don't hold a lock when calling a callback in ossl_namemap_doall_names
[thirdparty/openssl.git] / crypto / evp / keymgmt_meth.c
1 /*
2 * Copyright 2019-2021 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_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"
18
19 static void *keymgmt_new(void)
20 {
21 EVP_KEYMGMT *keymgmt = NULL;
22
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);
27 return NULL;
28 }
29
30 keymgmt->refcnt = 1;
31
32 return keymgmt;
33 }
34
35 static void *keymgmt_from_dispatch(int name_id,
36 const OSSL_DISPATCH *fns,
37 OSSL_PROVIDER *prov)
38 {
39 EVP_KEYMGMT *keymgmt = NULL;
40 int setparamfncnt = 0, getparamfncnt = 0;
41 int setgenparamfncnt = 0;
42 int importfncnt = 0, exportfncnt = 0;
43
44 if ((keymgmt = keymgmt_new()) == NULL) {
45 EVP_KEYMGMT_free(keymgmt);
46 return NULL;
47 }
48 keymgmt->name_id = name_id;
49
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);
55 break;
56 case OSSL_FUNC_KEYMGMT_GEN_INIT:
57 if (keymgmt->gen_init == NULL)
58 keymgmt->gen_init = OSSL_FUNC_keymgmt_gen_init(fns);
59 break;
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);
64 break;
65 case OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS:
66 if (keymgmt->gen_set_params == NULL) {
67 setgenparamfncnt++;
68 keymgmt->gen_set_params =
69 OSSL_FUNC_keymgmt_gen_set_params(fns);
70 }
71 break;
72 case OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS:
73 if (keymgmt->gen_settable_params == NULL) {
74 setgenparamfncnt++;
75 keymgmt->gen_settable_params =
76 OSSL_FUNC_keymgmt_gen_settable_params(fns);
77 }
78 break;
79 case OSSL_FUNC_KEYMGMT_GEN:
80 if (keymgmt->gen == NULL)
81 keymgmt->gen = OSSL_FUNC_keymgmt_gen(fns);
82 break;
83 case OSSL_FUNC_KEYMGMT_GEN_CLEANUP:
84 if (keymgmt->gen_cleanup == NULL)
85 keymgmt->gen_cleanup = OSSL_FUNC_keymgmt_gen_cleanup(fns);
86 break;
87 case OSSL_FUNC_KEYMGMT_FREE:
88 if (keymgmt->free == NULL)
89 keymgmt->free = OSSL_FUNC_keymgmt_free(fns);
90 break;
91 case OSSL_FUNC_KEYMGMT_LOAD:
92 if (keymgmt->load == NULL)
93 keymgmt->load = OSSL_FUNC_keymgmt_load(fns);
94 break;
95 case OSSL_FUNC_KEYMGMT_GET_PARAMS:
96 if (keymgmt->get_params == NULL) {
97 getparamfncnt++;
98 keymgmt->get_params = OSSL_FUNC_keymgmt_get_params(fns);
99 }
100 break;
101 case OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS:
102 if (keymgmt->gettable_params == NULL) {
103 getparamfncnt++;
104 keymgmt->gettable_params =
105 OSSL_FUNC_keymgmt_gettable_params(fns);
106 }
107 break;
108 case OSSL_FUNC_KEYMGMT_SET_PARAMS:
109 if (keymgmt->set_params == NULL) {
110 setparamfncnt++;
111 keymgmt->set_params = OSSL_FUNC_keymgmt_set_params(fns);
112 }
113 break;
114 case OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS:
115 if (keymgmt->settable_params == NULL) {
116 setparamfncnt++;
117 keymgmt->settable_params =
118 OSSL_FUNC_keymgmt_settable_params(fns);
119 }
120 break;
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);
125 break;
126 case OSSL_FUNC_KEYMGMT_HAS:
127 if (keymgmt->has == NULL)
128 keymgmt->has = OSSL_FUNC_keymgmt_has(fns);
129 break;
130 case OSSL_FUNC_KEYMGMT_COPY:
131 if (keymgmt->copy == NULL)
132 keymgmt->copy = OSSL_FUNC_keymgmt_copy(fns);
133 break;
134 case OSSL_FUNC_KEYMGMT_VALIDATE:
135 if (keymgmt->validate == NULL)
136 keymgmt->validate = OSSL_FUNC_keymgmt_validate(fns);
137 break;
138 case OSSL_FUNC_KEYMGMT_MATCH:
139 if (keymgmt->match == NULL)
140 keymgmt->match = OSSL_FUNC_keymgmt_match(fns);
141 break;
142 case OSSL_FUNC_KEYMGMT_IMPORT:
143 if (keymgmt->import == NULL) {
144 importfncnt++;
145 keymgmt->import = OSSL_FUNC_keymgmt_import(fns);
146 }
147 break;
148 case OSSL_FUNC_KEYMGMT_IMPORT_TYPES:
149 if (keymgmt->import_types == NULL) {
150 importfncnt++;
151 keymgmt->import_types = OSSL_FUNC_keymgmt_import_types(fns);
152 }
153 break;
154 case OSSL_FUNC_KEYMGMT_EXPORT:
155 if (keymgmt->export == NULL) {
156 exportfncnt++;
157 keymgmt->export = OSSL_FUNC_keymgmt_export(fns);
158 }
159 break;
160 case OSSL_FUNC_KEYMGMT_EXPORT_TYPES:
161 if (keymgmt->export_types == NULL) {
162 exportfncnt++;
163 keymgmt->export_types = OSSL_FUNC_keymgmt_export_types(fns);
164 }
165 break;
166 }
167 }
168 /*
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.
175 */
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);
191 return NULL;
192 }
193 keymgmt->prov = prov;
194 if (prov != NULL)
195 ossl_provider_up_ref(prov);
196
197 return keymgmt;
198 }
199
200 EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OSSL_LIB_CTX *ctx, int name_id,
201 const char *properties)
202 {
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);
208 }
209
210 EVP_KEYMGMT *EVP_KEYMGMT_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
211 const char *properties)
212 {
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);
217 }
218
219 int EVP_KEYMGMT_up_ref(EVP_KEYMGMT *keymgmt)
220 {
221 int ref = 0;
222
223 CRYPTO_UP_REF(&keymgmt->refcnt, &ref, keymgmt->lock);
224 return 1;
225 }
226
227 void EVP_KEYMGMT_free(EVP_KEYMGMT *keymgmt)
228 {
229 int ref = 0;
230
231 if (keymgmt == NULL)
232 return;
233
234 CRYPTO_DOWN_REF(&keymgmt->refcnt, &ref, keymgmt->lock);
235 if (ref > 0)
236 return;
237 ossl_provider_free(keymgmt->prov);
238 CRYPTO_THREAD_lock_free(keymgmt->lock);
239 OPENSSL_free(keymgmt);
240 }
241
242 const OSSL_PROVIDER *EVP_KEYMGMT_provider(const EVP_KEYMGMT *keymgmt)
243 {
244 return keymgmt->prov;
245 }
246
247 int EVP_KEYMGMT_number(const EVP_KEYMGMT *keymgmt)
248 {
249 return keymgmt->name_id;
250 }
251
252 const char *EVP_KEYMGMT_get0_first_name(const EVP_KEYMGMT *keymgmt)
253 {
254 return evp_first_name(keymgmt->prov, keymgmt->name_id);
255 }
256
257 int EVP_KEYMGMT_is_a(const EVP_KEYMGMT *keymgmt, const char *name)
258 {
259 return evp_is_a(keymgmt->prov, keymgmt->name_id, NULL, name);
260 }
261
262 void EVP_KEYMGMT_do_all_provided(OSSL_LIB_CTX *libctx,
263 void (*fn)(EVP_KEYMGMT *keymgmt, void *arg),
264 void *arg)
265 {
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);
270 }
271
272 int EVP_KEYMGMT_names_do_all(const EVP_KEYMGMT *keymgmt,
273 void (*fn)(const char *name, void *data),
274 void *data)
275 {
276 if (keymgmt->prov != NULL)
277 return evp_names_do_all(keymgmt->prov, keymgmt->name_id, fn, data);
278
279 return 1;
280 }
281
282 /*
283 * Internal API that interfaces with the method function pointers
284 */
285 void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt)
286 {
287 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
288
289 /*
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.
293 */
294 if (keymgmt->new == NULL)
295 return NULL;
296 return keymgmt->new(provctx);
297 }
298
299 void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keydata)
300 {
301 /* This is mandatory, no need to check for its presence */
302 keymgmt->free(keydata);
303 }
304
305 void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection)
306 {
307 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
308
309 if (keymgmt->gen_init == NULL)
310 return NULL;
311 return keymgmt->gen_init(provctx, selection);
312 }
313
314 int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
315 void *template)
316 {
317 /*
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.
323 */
324 if (keymgmt->gen_set_template == NULL)
325 return 1;
326 return keymgmt->gen_set_template(genctx, template);
327 }
328
329 int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
330 const OSSL_PARAM params[])
331 {
332 if (keymgmt->gen_set_params == NULL)
333 return 0;
334 return keymgmt->gen_set_params(genctx, params);
335 }
336
337 const OSSL_PARAM *EVP_KEYMGMT_gen_settable_params(const EVP_KEYMGMT *keymgmt)
338 {
339 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
340
341 if (keymgmt->gen_settable_params == NULL)
342 return NULL;
343 return keymgmt->gen_settable_params(provctx);
344 }
345
346 void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
347 OSSL_CALLBACK *cb, void *cbarg)
348 {
349 if (keymgmt->gen == NULL)
350 return NULL;
351 return keymgmt->gen(genctx, cb, cbarg);
352 }
353
354 void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx)
355 {
356 if (keymgmt->gen != NULL)
357 keymgmt->gen_cleanup(genctx);
358 }
359
360 void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
361 const void *objref, size_t objref_sz)
362 {
363 if (keymgmt->load != NULL)
364 return keymgmt->load(objref, objref_sz);
365 return NULL;
366 }
367
368 int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt, void *keydata,
369 OSSL_PARAM params[])
370 {
371 if (keymgmt->get_params == NULL)
372 return 1;
373 return keymgmt->get_params(keydata, params);
374 }
375
376 const OSSL_PARAM *EVP_KEYMGMT_gettable_params(const EVP_KEYMGMT *keymgmt)
377 {
378 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
379
380 if (keymgmt->gettable_params == NULL)
381 return NULL;
382 return keymgmt->gettable_params(provctx);
383 }
384
385 int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt, void *keydata,
386 const OSSL_PARAM params[])
387 {
388 if (keymgmt->set_params == NULL)
389 return 1;
390 return keymgmt->set_params(keydata, params);
391 }
392
393 const OSSL_PARAM *EVP_KEYMGMT_settable_params(const EVP_KEYMGMT *keymgmt)
394 {
395 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_provider(keymgmt));
396
397 if (keymgmt->settable_params == NULL)
398 return NULL;
399 return keymgmt->settable_params(provctx);
400 }
401
402 int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keydata, int selection)
403 {
404 /* This is mandatory, no need to check for its presence */
405 return keymgmt->has(keydata, selection);
406 }
407
408 int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
409 int selection, int checktype)
410 {
411 /* We assume valid if the implementation doesn't have a function */
412 if (keymgmt->validate == NULL)
413 return 1;
414 return keymgmt->validate(keydata, selection, checktype);
415 }
416
417 int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
418 const void *keydata1, const void *keydata2,
419 int selection)
420 {
421 /* We assume no match if the implementation doesn't have a function */
422 if (keymgmt->match == NULL)
423 return 0;
424 return keymgmt->match(keydata1, keydata2, selection);
425 }
426
427 int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
428 int selection, const OSSL_PARAM params[])
429 {
430 if (keymgmt->import == NULL)
431 return 0;
432 return keymgmt->import(keydata, selection, params);
433 }
434
435 const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
436 int selection)
437 {
438 if (keymgmt->import_types == NULL)
439 return NULL;
440 return keymgmt->import_types(selection);
441 }
442
443 int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
444 int selection, OSSL_CALLBACK *param_cb, void *cbarg)
445 {
446 if (keymgmt->export == NULL)
447 return 0;
448 return keymgmt->export(keydata, selection, param_cb, cbarg);
449 }
450
451 const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
452 int selection)
453 {
454 if (keymgmt->export_types == NULL)
455 return NULL;
456 return keymgmt->export_types(selection);
457 }
458
459 int evp_keymgmt_copy(const EVP_KEYMGMT *keymgmt,
460 void *keydata_to, const void *keydata_from,
461 int selection)
462 {
463 /* We assume no copy if the implementation doesn't have a function */
464 if (keymgmt->copy == NULL)
465 return 0;
466 return keymgmt->copy(keydata_to, keydata_from, selection);
467 }