xref: /freebsd/crypto/openssl/crypto/evp/asymcipher.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2006-2025 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 <stdio.h>
11 #include <stdlib.h>
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include "internal/cryptlib.h"
15 #include "internal/provider.h"
16 #include "internal/core.h"
17 #include "crypto/evp.h"
18 #include "evp_local.h"
19 
evp_asym_cipher_free(void * data)20 static void evp_asym_cipher_free(void *data)
21 {
22     EVP_ASYM_CIPHER_free(data);
23 }
24 
evp_asym_cipher_up_ref(void * data)25 static int evp_asym_cipher_up_ref(void *data)
26 {
27     return EVP_ASYM_CIPHER_up_ref(data);
28 }
29 
evp_pkey_asym_cipher_init(EVP_PKEY_CTX * ctx,int operation,const OSSL_PARAM params[])30 static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX *ctx, int operation,
31     const OSSL_PARAM params[])
32 {
33     int ret = 0;
34     void *provkey = NULL;
35     EVP_ASYM_CIPHER *cipher = NULL;
36     const char *desc;
37     EVP_KEYMGMT *tmp_keymgmt = NULL;
38     const OSSL_PROVIDER *tmp_prov = NULL;
39     const char *supported_ciph = NULL;
40     int iter;
41 
42     if (ctx == NULL) {
43         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
44         return -2;
45     }
46 
47     evp_pkey_ctx_free_old_ops(ctx);
48     ctx->operation = operation;
49 
50     ERR_set_mark();
51 
52     if (evp_pkey_ctx_is_legacy(ctx))
53         goto legacy;
54 
55     if (ctx->pkey == NULL) {
56         ERR_clear_last_mark();
57         ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
58         goto err;
59     }
60 
61     /*
62      * Try to derive the supported asym cipher from |ctx->keymgmt|.
63      */
64     if (!ossl_assert(ctx->pkey->keymgmt == NULL
65             || ctx->pkey->keymgmt == ctx->keymgmt)) {
66         ERR_clear_last_mark();
67         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
68         goto err;
69     }
70     supported_ciph
71         = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
72             OSSL_OP_ASYM_CIPHER);
73     if (supported_ciph == NULL) {
74         ERR_clear_last_mark();
75         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
76         goto err;
77     }
78 
79     /*
80      * We perform two iterations:
81      *
82      * 1.  Do the normal asym cipher fetch, using the fetching data given by
83      *     the EVP_PKEY_CTX.
84      * 2.  Do the provider specific asym cipher fetch, from the same provider
85      *     as |ctx->keymgmt|
86      *
87      * We then try to fetch the keymgmt from the same provider as the
88      * asym cipher, and try to export |ctx->pkey| to that keymgmt (when
89      * this keymgmt happens to be the same as |ctx->keymgmt|, the export
90      * is a no-op, but we call it anyway to not complicate the code even
91      * more).
92      * If the export call succeeds (returns a non-NULL provider key pointer),
93      * we're done and can perform the operation itself.  If not, we perform
94      * the second iteration, or jump to legacy.
95      */
96     for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
97         EVP_KEYMGMT *tmp_keymgmt_tofree;
98 
99         /*
100          * If we're on the second iteration, free the results from the first.
101          * They are NULL on the first iteration, so no need to check what
102          * iteration we're on.
103          */
104         EVP_ASYM_CIPHER_free(cipher);
105         EVP_KEYMGMT_free(tmp_keymgmt);
106 
107         switch (iter) {
108         case 1:
109             cipher = EVP_ASYM_CIPHER_fetch(ctx->libctx, supported_ciph,
110                 ctx->propquery);
111             if (cipher != NULL)
112                 tmp_prov = EVP_ASYM_CIPHER_get0_provider(cipher);
113             break;
114         case 2:
115             tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
116             cipher = evp_asym_cipher_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
117                 supported_ciph, ctx->propquery);
118             if (cipher == NULL)
119                 goto legacy;
120             break;
121         }
122         if (cipher == NULL)
123             continue;
124 
125         /*
126          * Ensure that the key is provided, either natively, or as a cached
127          * export.  We start by fetching the keymgmt with the same name as
128          * |ctx->pkey|, but from the provider of the asym cipher method, using
129          * the same property query as when fetching the asym cipher method.
130          * With the keymgmt we found (if we did), we try to export |ctx->pkey|
131          * to it (evp_pkey_export_to_provider() is smart enough to only actually
132          * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
133          */
134         tmp_keymgmt_tofree = tmp_keymgmt
135             = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
136                 EVP_KEYMGMT_get0_name(ctx->keymgmt),
137                 ctx->propquery);
138         if (tmp_keymgmt != NULL)
139             provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
140                 &tmp_keymgmt, ctx->propquery);
141         if (tmp_keymgmt == NULL)
142             EVP_KEYMGMT_free(tmp_keymgmt_tofree);
143     }
144 
145     if (provkey == NULL) {
146         EVP_ASYM_CIPHER_free(cipher);
147         goto legacy;
148     }
149 
150     ERR_pop_to_mark();
151 
152     /* No more legacy from here down to legacy: */
153 
154     ctx->op.ciph.cipher = cipher;
155     ctx->op.ciph.algctx = cipher->newctx(ossl_provider_ctx(cipher->prov));
156     if (ctx->op.ciph.algctx == NULL) {
157         /* The provider key can stay in the cache */
158         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
159         goto err;
160     }
161 
162     desc = cipher->description != NULL ? cipher->description : "";
163     switch (operation) {
164     case EVP_PKEY_OP_ENCRYPT:
165         if (cipher->encrypt_init == NULL) {
166             ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_NOT_SUPPORTED,
167                 "%s encrypt_init:%s", cipher->type_name, desc);
168             ret = -2;
169             goto err;
170         }
171         ret = cipher->encrypt_init(ctx->op.ciph.algctx, provkey, params);
172         break;
173     case EVP_PKEY_OP_DECRYPT:
174         if (cipher->decrypt_init == NULL) {
175             ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_NOT_SUPPORTED,
176                 "%s decrypt_init:%s", cipher->type_name, desc);
177             ret = -2;
178             goto err;
179         }
180         ret = cipher->decrypt_init(ctx->op.ciph.algctx, provkey, params);
181         break;
182     default:
183         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
184         goto err;
185     }
186 
187     if (ret <= 0)
188         goto err;
189     EVP_KEYMGMT_free(tmp_keymgmt);
190     return 1;
191 
192 legacy:
193     /*
194      * If we don't have the full support we need with provided methods,
195      * let's go see if legacy does.
196      */
197     ERR_pop_to_mark();
198     EVP_KEYMGMT_free(tmp_keymgmt);
199     tmp_keymgmt = NULL;
200 
201     if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
202         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
203         return -2;
204     }
205     switch (ctx->operation) {
206     case EVP_PKEY_OP_ENCRYPT:
207         if (ctx->pmeth->encrypt_init == NULL)
208             return 1;
209         ret = ctx->pmeth->encrypt_init(ctx);
210         break;
211     case EVP_PKEY_OP_DECRYPT:
212         if (ctx->pmeth->decrypt_init == NULL)
213             return 1;
214         ret = ctx->pmeth->decrypt_init(ctx);
215         break;
216     default:
217         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
218         ret = -1;
219     }
220 
221 err:
222     if (ret <= 0) {
223         evp_pkey_ctx_free_old_ops(ctx);
224         ctx->operation = EVP_PKEY_OP_UNDEFINED;
225     }
226     EVP_KEYMGMT_free(tmp_keymgmt);
227     return ret;
228 }
229 
EVP_PKEY_encrypt_init(EVP_PKEY_CTX * ctx)230 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
231 {
232     return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, NULL);
233 }
234 
EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX * ctx,const OSSL_PARAM params[])235 int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
236 {
237     return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, params);
238 }
239 
EVP_PKEY_encrypt(EVP_PKEY_CTX * ctx,unsigned char * out,size_t * outlen,const unsigned char * in,size_t inlen)240 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
241     unsigned char *out, size_t *outlen,
242     const unsigned char *in, size_t inlen)
243 {
244     EVP_ASYM_CIPHER *cipher;
245     const char *desc;
246     int ret;
247 
248     if (ctx == NULL) {
249         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
250         return -2;
251     }
252 
253     if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
254         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
255         return -1;
256     }
257 
258     if (ctx->op.ciph.algctx == NULL)
259         goto legacy;
260 
261     cipher = ctx->op.ciph.cipher;
262     desc = cipher->description != NULL ? cipher->description : "";
263     ERR_set_mark();
264     ret = cipher->encrypt(ctx->op.ciph.algctx, out, outlen, (out == NULL ? 0 : *outlen), in, inlen);
265     if (ret <= 0 && ERR_count_to_mark() == 0)
266         ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_FAILURE,
267             "%s encrypt:%s", cipher->type_name, desc);
268     ERR_clear_last_mark();
269     return ret;
270 
271 legacy:
272     if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
273         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
274         return -2;
275     }
276     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT) return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
277 }
278 
EVP_PKEY_decrypt_init(EVP_PKEY_CTX * ctx)279 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
280 {
281     return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, NULL);
282 }
283 
EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX * ctx,const OSSL_PARAM params[])284 int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
285 {
286     return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, params);
287 }
288 
EVP_PKEY_decrypt(EVP_PKEY_CTX * ctx,unsigned char * out,size_t * outlen,const unsigned char * in,size_t inlen)289 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
290     unsigned char *out, size_t *outlen,
291     const unsigned char *in, size_t inlen)
292 {
293     EVP_ASYM_CIPHER *cipher;
294     const char *desc;
295     int ret;
296 
297     if (ctx == NULL) {
298         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
299         return -2;
300     }
301 
302     if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
303         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
304         return -1;
305     }
306 
307     if (ctx->op.ciph.algctx == NULL)
308         goto legacy;
309 
310     cipher = ctx->op.ciph.cipher;
311     desc = cipher->description != NULL ? cipher->description : "";
312     ERR_set_mark();
313     ret = cipher->decrypt(ctx->op.ciph.algctx, out, outlen, (out == NULL ? 0 : *outlen), in, inlen);
314     if (ret <= 0 && ERR_count_to_mark() == 0)
315         ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_FAILURE,
316             "%s decrypt:%s", cipher->type_name, desc);
317     ERR_clear_last_mark();
318 
319     return ret;
320 
321 legacy:
322     if (ctx->pmeth == NULL || ctx->pmeth->decrypt == NULL) {
323         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
324         return -2;
325     }
326     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT) return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
327 }
328 
329 /* decrypt to new buffer of dynamic size, checking any pre-determined size */
evp_pkey_decrypt_alloc(EVP_PKEY_CTX * ctx,unsigned char ** outp,size_t * outlenp,size_t expected_outlen,const unsigned char * in,size_t inlen)330 int evp_pkey_decrypt_alloc(EVP_PKEY_CTX *ctx, unsigned char **outp,
331     size_t *outlenp, size_t expected_outlen,
332     const unsigned char *in, size_t inlen)
333 {
334     if (EVP_PKEY_decrypt(ctx, NULL, outlenp, in, inlen) <= 0
335         || (*outp = OPENSSL_malloc(*outlenp)) == NULL)
336         return -1;
337     if (EVP_PKEY_decrypt(ctx, *outp, outlenp, in, inlen) <= 0
338         || *outlenp == 0
339         || (expected_outlen != 0 && *outlenp != expected_outlen)) {
340         ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
341         OPENSSL_clear_free(*outp, *outlenp);
342         *outp = NULL;
343         return 0;
344     }
345     return 1;
346 }
347 
evp_asym_cipher_new(OSSL_PROVIDER * prov)348 static EVP_ASYM_CIPHER *evp_asym_cipher_new(OSSL_PROVIDER *prov)
349 {
350     EVP_ASYM_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER));
351 
352     if (cipher == NULL)
353         return NULL;
354 
355     if (!CRYPTO_NEW_REF(&cipher->refcnt, 1)
356         || !ossl_provider_up_ref(prov)) {
357         CRYPTO_FREE_REF(&cipher->refcnt);
358         OPENSSL_free(cipher);
359         return NULL;
360     }
361     cipher->prov = prov;
362 
363     return cipher;
364 }
365 
evp_asym_cipher_from_algorithm(int name_id,const OSSL_ALGORITHM * algodef,OSSL_PROVIDER * prov)366 static void *evp_asym_cipher_from_algorithm(int name_id,
367     const OSSL_ALGORITHM *algodef,
368     OSSL_PROVIDER *prov)
369 {
370     const OSSL_DISPATCH *fns = algodef->implementation;
371     EVP_ASYM_CIPHER *cipher = NULL;
372     int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
373     int gparamfncnt = 0, sparamfncnt = 0;
374 
375     if ((cipher = evp_asym_cipher_new(prov)) == NULL) {
376         ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
377         goto err;
378     }
379 
380     cipher->name_id = name_id;
381     if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
382         goto err;
383     cipher->description = algodef->algorithm_description;
384 
385     for (; fns->function_id != 0; fns++) {
386         switch (fns->function_id) {
387         case OSSL_FUNC_ASYM_CIPHER_NEWCTX:
388             if (cipher->newctx != NULL)
389                 break;
390             cipher->newctx = OSSL_FUNC_asym_cipher_newctx(fns);
391             ctxfncnt++;
392             break;
393         case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT:
394             if (cipher->encrypt_init != NULL)
395                 break;
396             cipher->encrypt_init = OSSL_FUNC_asym_cipher_encrypt_init(fns);
397             encfncnt++;
398             break;
399         case OSSL_FUNC_ASYM_CIPHER_ENCRYPT:
400             if (cipher->encrypt != NULL)
401                 break;
402             cipher->encrypt = OSSL_FUNC_asym_cipher_encrypt(fns);
403             encfncnt++;
404             break;
405         case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT:
406             if (cipher->decrypt_init != NULL)
407                 break;
408             cipher->decrypt_init = OSSL_FUNC_asym_cipher_decrypt_init(fns);
409             decfncnt++;
410             break;
411         case OSSL_FUNC_ASYM_CIPHER_DECRYPT:
412             if (cipher->decrypt != NULL)
413                 break;
414             cipher->decrypt = OSSL_FUNC_asym_cipher_decrypt(fns);
415             decfncnt++;
416             break;
417         case OSSL_FUNC_ASYM_CIPHER_FREECTX:
418             if (cipher->freectx != NULL)
419                 break;
420             cipher->freectx = OSSL_FUNC_asym_cipher_freectx(fns);
421             ctxfncnt++;
422             break;
423         case OSSL_FUNC_ASYM_CIPHER_DUPCTX:
424             if (cipher->dupctx != NULL)
425                 break;
426             cipher->dupctx = OSSL_FUNC_asym_cipher_dupctx(fns);
427             break;
428         case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS:
429             if (cipher->get_ctx_params != NULL)
430                 break;
431             cipher->get_ctx_params
432                 = OSSL_FUNC_asym_cipher_get_ctx_params(fns);
433             gparamfncnt++;
434             break;
435         case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS:
436             if (cipher->gettable_ctx_params != NULL)
437                 break;
438             cipher->gettable_ctx_params
439                 = OSSL_FUNC_asym_cipher_gettable_ctx_params(fns);
440             gparamfncnt++;
441             break;
442         case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS:
443             if (cipher->set_ctx_params != NULL)
444                 break;
445             cipher->set_ctx_params
446                 = OSSL_FUNC_asym_cipher_set_ctx_params(fns);
447             sparamfncnt++;
448             break;
449         case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS:
450             if (cipher->settable_ctx_params != NULL)
451                 break;
452             cipher->settable_ctx_params
453                 = OSSL_FUNC_asym_cipher_settable_ctx_params(fns);
454             sparamfncnt++;
455             break;
456         }
457     }
458     if (ctxfncnt != 2
459         || (encfncnt != 0 && encfncnt != 2)
460         || (decfncnt != 0 && decfncnt != 2)
461         || (encfncnt != 2 && decfncnt != 2)
462         || (gparamfncnt != 0 && gparamfncnt != 2)
463         || (sparamfncnt != 0 && sparamfncnt != 2)) {
464         /*
465          * In order to be a consistent set of functions we must have at least
466          * a set of context functions (newctx and freectx) as well as a pair of
467          * "cipher" functions: (encrypt_init, encrypt) or
468          * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are
469          * optional, but if one of them is present then the other one must also
470          * be present. The same applies to get_ctx_params and
471          * gettable_ctx_params. The dupctx function is optional.
472          */
473         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
474         goto err;
475     }
476 
477     return cipher;
478 err:
479     EVP_ASYM_CIPHER_free(cipher);
480     return NULL;
481 }
482 
EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER * cipher)483 void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER *cipher)
484 {
485     int i;
486 
487     if (cipher == NULL)
488         return;
489     CRYPTO_DOWN_REF(&cipher->refcnt, &i);
490     if (i > 0)
491         return;
492     OPENSSL_free(cipher->type_name);
493     ossl_provider_free(cipher->prov);
494     CRYPTO_FREE_REF(&cipher->refcnt);
495     OPENSSL_free(cipher);
496 }
497 
EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER * cipher)498 int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER *cipher)
499 {
500     int ref = 0;
501 
502     CRYPTO_UP_REF(&cipher->refcnt, &ref);
503     return 1;
504 }
505 
EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER * cipher)506 OSSL_PROVIDER *EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER *cipher)
507 {
508     return cipher->prov;
509 }
510 
EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX * ctx,const char * algorithm,const char * properties)511 EVP_ASYM_CIPHER *EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
512     const char *properties)
513 {
514     return evp_generic_fetch(ctx, OSSL_OP_ASYM_CIPHER, algorithm, properties,
515         evp_asym_cipher_from_algorithm,
516         evp_asym_cipher_up_ref,
517         evp_asym_cipher_free);
518 }
519 
evp_asym_cipher_fetch_from_prov(OSSL_PROVIDER * prov,const char * algorithm,const char * properties)520 EVP_ASYM_CIPHER *evp_asym_cipher_fetch_from_prov(OSSL_PROVIDER *prov,
521     const char *algorithm,
522     const char *properties)
523 {
524     return evp_generic_fetch_from_prov(prov, OSSL_OP_ASYM_CIPHER,
525         algorithm, properties,
526         evp_asym_cipher_from_algorithm,
527         evp_asym_cipher_up_ref,
528         evp_asym_cipher_free);
529 }
530 
EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER * cipher,const char * name)531 int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER *cipher, const char *name)
532 {
533     return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
534 }
535 
evp_asym_cipher_get_number(const EVP_ASYM_CIPHER * cipher)536 int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher)
537 {
538     return cipher->name_id;
539 }
540 
EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER * cipher)541 const char *EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER *cipher)
542 {
543     return cipher->type_name;
544 }
545 
EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER * cipher)546 const char *EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER *cipher)
547 {
548     return cipher->description;
549 }
550 
EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX * libctx,void (* fn)(EVP_ASYM_CIPHER * cipher,void * arg),void * arg)551 void EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
552     void (*fn)(EVP_ASYM_CIPHER *cipher,
553         void *arg),
554     void *arg)
555 {
556     evp_generic_do_all(libctx, OSSL_OP_ASYM_CIPHER,
557         (void (*)(void *, void *))fn, arg,
558         evp_asym_cipher_from_algorithm,
559         evp_asym_cipher_up_ref,
560         evp_asym_cipher_free);
561 }
562 
EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER * cipher,void (* fn)(const char * name,void * data),void * data)563 int EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER *cipher,
564     void (*fn)(const char *name, void *data),
565     void *data)
566 {
567     if (cipher->prov != NULL)
568         return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
569 
570     return 1;
571 }
572 
EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER * cip)573 const OSSL_PARAM *EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER *cip)
574 {
575     void *provctx;
576 
577     if (cip == NULL || cip->gettable_ctx_params == NULL)
578         return NULL;
579 
580     provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));
581     return cip->gettable_ctx_params(NULL, provctx);
582 }
583 
EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER * cip)584 const OSSL_PARAM *EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER *cip)
585 {
586     void *provctx;
587 
588     if (cip == NULL || cip->settable_ctx_params == NULL)
589         return NULL;
590 
591     provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));
592     return cip->settable_ctx_params(NULL, provctx);
593 }
594