VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.3/crypto/evp/keymgmt_meth.c@ 96662

Last change on this file since 96662 was 94320, checked in by vboxsync, 3 years ago

libs/openssl-3.0.1: Export to OSE and fix copyright headers in Makefiles, bugref:10128

File size: 16.0 KB
Line 
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 "internal/core.h"
17#include "crypto/evp.h"
18#include "evp_local.h"
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 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
28 return NULL;
29 }
30
31 keymgmt->refcnt = 1;
32
33 return keymgmt;
34}
35
36static void *keymgmt_from_algorithm(int name_id,
37 const OSSL_ALGORITHM *algodef,
38 OSSL_PROVIDER *prov)
39{
40 const OSSL_DISPATCH *fns = algodef->implementation;
41 EVP_KEYMGMT *keymgmt = NULL;
42 int setparamfncnt = 0, getparamfncnt = 0;
43 int setgenparamfncnt = 0;
44 int importfncnt = 0, exportfncnt = 0;
45
46 if ((keymgmt = keymgmt_new()) == NULL)
47 return NULL;
48
49 keymgmt->name_id = name_id;
50 if ((keymgmt->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
51 EVP_KEYMGMT_free(keymgmt);
52 return NULL;
53 }
54 keymgmt->description = algodef->algorithm_description;
55
56 for (; fns->function_id != 0; fns++) {
57 switch (fns->function_id) {
58 case OSSL_FUNC_KEYMGMT_NEW:
59 if (keymgmt->new == NULL)
60 keymgmt->new = OSSL_FUNC_keymgmt_new(fns);
61 break;
62 case OSSL_FUNC_KEYMGMT_GEN_INIT:
63 if (keymgmt->gen_init == NULL)
64 keymgmt->gen_init = OSSL_FUNC_keymgmt_gen_init(fns);
65 break;
66 case OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE:
67 if (keymgmt->gen_set_template == NULL)
68 keymgmt->gen_set_template =
69 OSSL_FUNC_keymgmt_gen_set_template(fns);
70 break;
71 case OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS:
72 if (keymgmt->gen_set_params == NULL) {
73 setgenparamfncnt++;
74 keymgmt->gen_set_params =
75 OSSL_FUNC_keymgmt_gen_set_params(fns);
76 }
77 break;
78 case OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS:
79 if (keymgmt->gen_settable_params == NULL) {
80 setgenparamfncnt++;
81 keymgmt->gen_settable_params =
82 OSSL_FUNC_keymgmt_gen_settable_params(fns);
83 }
84 break;
85 case OSSL_FUNC_KEYMGMT_GEN:
86 if (keymgmt->gen == NULL)
87 keymgmt->gen = OSSL_FUNC_keymgmt_gen(fns);
88 break;
89 case OSSL_FUNC_KEYMGMT_GEN_CLEANUP:
90 if (keymgmt->gen_cleanup == NULL)
91 keymgmt->gen_cleanup = OSSL_FUNC_keymgmt_gen_cleanup(fns);
92 break;
93 case OSSL_FUNC_KEYMGMT_FREE:
94 if (keymgmt->free == NULL)
95 keymgmt->free = OSSL_FUNC_keymgmt_free(fns);
96 break;
97 case OSSL_FUNC_KEYMGMT_LOAD:
98 if (keymgmt->load == NULL)
99 keymgmt->load = OSSL_FUNC_keymgmt_load(fns);
100 break;
101 case OSSL_FUNC_KEYMGMT_GET_PARAMS:
102 if (keymgmt->get_params == NULL) {
103 getparamfncnt++;
104 keymgmt->get_params = OSSL_FUNC_keymgmt_get_params(fns);
105 }
106 break;
107 case OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS:
108 if (keymgmt->gettable_params == NULL) {
109 getparamfncnt++;
110 keymgmt->gettable_params =
111 OSSL_FUNC_keymgmt_gettable_params(fns);
112 }
113 break;
114 case OSSL_FUNC_KEYMGMT_SET_PARAMS:
115 if (keymgmt->set_params == NULL) {
116 setparamfncnt++;
117 keymgmt->set_params = OSSL_FUNC_keymgmt_set_params(fns);
118 }
119 break;
120 case OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS:
121 if (keymgmt->settable_params == NULL) {
122 setparamfncnt++;
123 keymgmt->settable_params =
124 OSSL_FUNC_keymgmt_settable_params(fns);
125 }
126 break;
127 case OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME:
128 if (keymgmt->query_operation_name == NULL)
129 keymgmt->query_operation_name =
130 OSSL_FUNC_keymgmt_query_operation_name(fns);
131 break;
132 case OSSL_FUNC_KEYMGMT_HAS:
133 if (keymgmt->has == NULL)
134 keymgmt->has = OSSL_FUNC_keymgmt_has(fns);
135 break;
136 case OSSL_FUNC_KEYMGMT_DUP:
137 if (keymgmt->dup == NULL)
138 keymgmt->dup = OSSL_FUNC_keymgmt_dup(fns);
139 break;
140 case OSSL_FUNC_KEYMGMT_VALIDATE:
141 if (keymgmt->validate == NULL)
142 keymgmt->validate = OSSL_FUNC_keymgmt_validate(fns);
143 break;
144 case OSSL_FUNC_KEYMGMT_MATCH:
145 if (keymgmt->match == NULL)
146 keymgmt->match = OSSL_FUNC_keymgmt_match(fns);
147 break;
148 case OSSL_FUNC_KEYMGMT_IMPORT:
149 if (keymgmt->import == NULL) {
150 importfncnt++;
151 keymgmt->import = OSSL_FUNC_keymgmt_import(fns);
152 }
153 break;
154 case OSSL_FUNC_KEYMGMT_IMPORT_TYPES:
155 if (keymgmt->import_types == NULL) {
156 importfncnt++;
157 keymgmt->import_types = OSSL_FUNC_keymgmt_import_types(fns);
158 }
159 break;
160 case OSSL_FUNC_KEYMGMT_EXPORT:
161 if (keymgmt->export == NULL) {
162 exportfncnt++;
163 keymgmt->export = OSSL_FUNC_keymgmt_export(fns);
164 }
165 break;
166 case OSSL_FUNC_KEYMGMT_EXPORT_TYPES:
167 if (keymgmt->export_types == NULL) {
168 exportfncnt++;
169 keymgmt->export_types = OSSL_FUNC_keymgmt_export_types(fns);
170 }
171 break;
172 }
173 }
174 /*
175 * Try to check that the method is sensible.
176 * At least one constructor and the destructor are MANDATORY
177 * The functions 'has' is MANDATORY
178 * It makes no sense being able to free stuff if you can't create it.
179 * It makes no sense providing OSSL_PARAM descriptors for import and
180 * export if you can't import or export.
181 */
182 if (keymgmt->free == NULL
183 || (keymgmt->new == NULL
184 && keymgmt->gen == NULL
185 && keymgmt->load == NULL)
186 || keymgmt->has == NULL
187 || (getparamfncnt != 0 && getparamfncnt != 2)
188 || (setparamfncnt != 0 && setparamfncnt != 2)
189 || (setgenparamfncnt != 0 && setgenparamfncnt != 2)
190 || (importfncnt != 0 && importfncnt != 2)
191 || (exportfncnt != 0 && exportfncnt != 2)
192 || (keymgmt->gen != NULL
193 && (keymgmt->gen_init == NULL
194 || keymgmt->gen_cleanup == NULL))) {
195 EVP_KEYMGMT_free(keymgmt);
196 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
197 return NULL;
198 }
199 keymgmt->prov = prov;
200 if (prov != NULL)
201 ossl_provider_up_ref(prov);
202
203 return keymgmt;
204}
205
206EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OSSL_LIB_CTX *ctx, int name_id,
207 const char *properties)
208{
209 return evp_generic_fetch_by_number(ctx,
210 OSSL_OP_KEYMGMT, name_id, properties,
211 keymgmt_from_algorithm,
212 (int (*)(void *))EVP_KEYMGMT_up_ref,
213 (void (*)(void *))EVP_KEYMGMT_free);
214}
215
216EVP_KEYMGMT *evp_keymgmt_fetch_from_prov(OSSL_PROVIDER *prov,
217 const char *name,
218 const char *properties)
219{
220 return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYMGMT,
221 name, properties,
222 keymgmt_from_algorithm,
223 (int (*)(void *))EVP_KEYMGMT_up_ref,
224 (void (*)(void *))EVP_KEYMGMT_free);
225}
226
227EVP_KEYMGMT *EVP_KEYMGMT_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
228 const char *properties)
229{
230 return evp_generic_fetch(ctx, OSSL_OP_KEYMGMT, algorithm, properties,
231 keymgmt_from_algorithm,
232 (int (*)(void *))EVP_KEYMGMT_up_ref,
233 (void (*)(void *))EVP_KEYMGMT_free);
234}
235
236int EVP_KEYMGMT_up_ref(EVP_KEYMGMT *keymgmt)
237{
238 int ref = 0;
239
240 CRYPTO_UP_REF(&keymgmt->refcnt, &ref, keymgmt->lock);
241 return 1;
242}
243
244void EVP_KEYMGMT_free(EVP_KEYMGMT *keymgmt)
245{
246 int ref = 0;
247
248 if (keymgmt == NULL)
249 return;
250
251 CRYPTO_DOWN_REF(&keymgmt->refcnt, &ref, keymgmt->lock);
252 if (ref > 0)
253 return;
254 OPENSSL_free(keymgmt->type_name);
255 ossl_provider_free(keymgmt->prov);
256 CRYPTO_THREAD_lock_free(keymgmt->lock);
257 OPENSSL_free(keymgmt);
258}
259
260const OSSL_PROVIDER *EVP_KEYMGMT_get0_provider(const EVP_KEYMGMT *keymgmt)
261{
262 return keymgmt->prov;
263}
264
265int evp_keymgmt_get_number(const EVP_KEYMGMT *keymgmt)
266{
267 return keymgmt->name_id;
268}
269
270const char *EVP_KEYMGMT_get0_description(const EVP_KEYMGMT *keymgmt)
271{
272 return keymgmt->description;
273}
274
275const char *EVP_KEYMGMT_get0_name(const EVP_KEYMGMT *keymgmt)
276{
277 return keymgmt->type_name;
278}
279
280int EVP_KEYMGMT_is_a(const EVP_KEYMGMT *keymgmt, const char *name)
281{
282 return evp_is_a(keymgmt->prov, keymgmt->name_id, NULL, name);
283}
284
285void EVP_KEYMGMT_do_all_provided(OSSL_LIB_CTX *libctx,
286 void (*fn)(EVP_KEYMGMT *keymgmt, void *arg),
287 void *arg)
288{
289 evp_generic_do_all(libctx, OSSL_OP_KEYMGMT,
290 (void (*)(void *, void *))fn, arg,
291 keymgmt_from_algorithm,
292 (int (*)(void *))EVP_KEYMGMT_up_ref,
293 (void (*)(void *))EVP_KEYMGMT_free);
294}
295
296int EVP_KEYMGMT_names_do_all(const EVP_KEYMGMT *keymgmt,
297 void (*fn)(const char *name, void *data),
298 void *data)
299{
300 if (keymgmt->prov != NULL)
301 return evp_names_do_all(keymgmt->prov, keymgmt->name_id, fn, data);
302
303 return 1;
304}
305
306/*
307 * Internal API that interfaces with the method function pointers
308 */
309void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt)
310{
311 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
312
313 /*
314 * 'new' is currently mandatory on its own, but when new
315 * constructors appear, it won't be quite as mandatory,
316 * so we have a check for future cases.
317 */
318 if (keymgmt->new == NULL)
319 return NULL;
320 return keymgmt->new(provctx);
321}
322
323void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keydata)
324{
325 /* This is mandatory, no need to check for its presence */
326 keymgmt->free(keydata);
327}
328
329void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection,
330 const OSSL_PARAM params[])
331{
332 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
333
334 if (keymgmt->gen_init == NULL)
335 return NULL;
336 return keymgmt->gen_init(provctx, selection, params);
337}
338
339int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
340 void *template)
341{
342 /*
343 * It's arguable if we actually should return success in this case, as
344 * it allows the caller to set a template key, which is then ignored.
345 * However, this is how the legacy methods (EVP_PKEY_METHOD) operate,
346 * so we do this in the interest of backward compatibility.
347 */
348 if (keymgmt->gen_set_template == NULL)
349 return 1;
350 return keymgmt->gen_set_template(genctx, template);
351}
352
353int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
354 const OSSL_PARAM params[])
355{
356 if (keymgmt->gen_set_params == NULL)
357 return 0;
358 return keymgmt->gen_set_params(genctx, params);
359}
360
361const OSSL_PARAM *EVP_KEYMGMT_gen_settable_params(const EVP_KEYMGMT *keymgmt)
362{
363 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
364
365 if (keymgmt->gen_settable_params == NULL)
366 return NULL;
367 return keymgmt->gen_settable_params(NULL, provctx);
368}
369
370void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
371 OSSL_CALLBACK *cb, void *cbarg)
372{
373 if (keymgmt->gen == NULL)
374 return NULL;
375 return keymgmt->gen(genctx, cb, cbarg);
376}
377
378void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx)
379{
380 if (keymgmt->gen != NULL)
381 keymgmt->gen_cleanup(genctx);
382}
383
384int evp_keymgmt_has_load(const EVP_KEYMGMT *keymgmt)
385{
386 return keymgmt != NULL && keymgmt->load != NULL;
387}
388
389void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
390 const void *objref, size_t objref_sz)
391{
392 if (evp_keymgmt_has_load(keymgmt))
393 return keymgmt->load(objref, objref_sz);
394 return NULL;
395}
396
397int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt, void *keydata,
398 OSSL_PARAM params[])
399{
400 if (keymgmt->get_params == NULL)
401 return 1;
402 return keymgmt->get_params(keydata, params);
403}
404
405const OSSL_PARAM *EVP_KEYMGMT_gettable_params(const EVP_KEYMGMT *keymgmt)
406{
407 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
408
409 if (keymgmt->gettable_params == NULL)
410 return NULL;
411 return keymgmt->gettable_params(provctx);
412}
413
414int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt, void *keydata,
415 const OSSL_PARAM params[])
416{
417 if (keymgmt->set_params == NULL)
418 return 1;
419 return keymgmt->set_params(keydata, params);
420}
421
422const OSSL_PARAM *EVP_KEYMGMT_settable_params(const EVP_KEYMGMT *keymgmt)
423{
424 void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
425
426 if (keymgmt->settable_params == NULL)
427 return NULL;
428 return keymgmt->settable_params(provctx);
429}
430
431int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keydata, int selection)
432{
433 /* This is mandatory, no need to check for its presence */
434 return keymgmt->has(keydata, selection);
435}
436
437int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
438 int selection, int checktype)
439{
440 /* We assume valid if the implementation doesn't have a function */
441 if (keymgmt->validate == NULL)
442 return 1;
443 return keymgmt->validate(keydata, selection, checktype);
444}
445
446int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
447 const void *keydata1, const void *keydata2,
448 int selection)
449{
450 /* We assume no match if the implementation doesn't have a function */
451 if (keymgmt->match == NULL)
452 return 0;
453 return keymgmt->match(keydata1, keydata2, selection);
454}
455
456int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
457 int selection, const OSSL_PARAM params[])
458{
459 if (keymgmt->import == NULL)
460 return 0;
461 return keymgmt->import(keydata, selection, params);
462}
463
464const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
465 int selection)
466{
467 if (keymgmt->import_types == NULL)
468 return NULL;
469 return keymgmt->import_types(selection);
470}
471
472int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
473 int selection, OSSL_CALLBACK *param_cb, void *cbarg)
474{
475 if (keymgmt->export == NULL)
476 return 0;
477 return keymgmt->export(keydata, selection, param_cb, cbarg);
478}
479
480const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
481 int selection)
482{
483 if (keymgmt->export_types == NULL)
484 return NULL;
485 return keymgmt->export_types(selection);
486}
487
488void *evp_keymgmt_dup(const EVP_KEYMGMT *keymgmt, const void *keydata_from,
489 int selection)
490{
491 /* We assume no dup if the implementation doesn't have a function */
492 if (keymgmt->dup == NULL)
493 return NULL;
494 return keymgmt->dup(keydata_from, selection);
495}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette