Coverage Report

Created: 2025-08-28 07:07

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