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