Coverage Report

Created: 2026-07-23 06:28

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