Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/evp/pmeth_lib.c
Line
Count
Source
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
/*
11
 * Low level key APIs (DH etc) are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include <stdlib.h>
18
#ifndef FIPS_MODULE
19
#include <openssl/engine.h>
20
#endif
21
#include <openssl/evp.h>
22
#include <openssl/core_names.h>
23
#include <openssl/dh.h>
24
#include <openssl/rsa.h>
25
#include <openssl/kdf.h>
26
#include "internal/cryptlib.h"
27
#ifndef FIPS_MODULE
28
#include "crypto/asn1.h"
29
#endif
30
#include "crypto/evp.h"
31
#include "crypto/dh.h"
32
#include "crypto/ec.h"
33
#include "internal/ffc.h"
34
#include "internal/numbers.h"
35
#include "internal/provider.h"
36
#include "evp_local.h"
37
38
#ifndef FIPS_MODULE
39
40
static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
41
    int keytype, int optype,
42
    int cmd, const char *name,
43
    const void *data, size_t data_len);
44
static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
45
    int cmd, const char *name);
46
static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx);
47
48
typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
49
typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
50
51
static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
52
53
/* This array needs to be in order of NIDs */
54
static pmeth_fn standard_methods[] = {
55
    ossl_rsa_pkey_method,
56
#ifndef OPENSSL_NO_DH
57
    ossl_dh_pkey_method,
58
#endif
59
#ifndef OPENSSL_NO_DSA
60
    ossl_dsa_pkey_method,
61
#endif
62
#ifndef OPENSSL_NO_EC
63
    ossl_ec_pkey_method,
64
#endif
65
    ossl_rsa_pss_pkey_method,
66
#ifndef OPENSSL_NO_DH
67
    ossl_dhx_pkey_method,
68
#endif
69
#ifndef OPENSSL_NO_ECX
70
    ossl_ecx25519_pkey_method,
71
    ossl_ecx448_pkey_method,
72
    ossl_ed25519_pkey_method,
73
    ossl_ed448_pkey_method,
74
#endif
75
};
76
77
DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
78
79
static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
80
0
{
81
0
    return ((*a)->pkey_id - ((**b)())->pkey_id);
82
0
}
83
84
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
85
86
static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
87
    const EVP_PKEY_METHOD *const *b)
88
0
{
89
0
    return ((*a)->pkey_id - (*b)->pkey_id);
90
0
}
91
92
static const EVP_PKEY_METHOD *evp_pkey_meth_find_added_by_application(int type)
93
3.87M
{
94
3.87M
    if (app_pkey_methods != NULL) {
95
0
        int idx;
96
0
        EVP_PKEY_METHOD tmp;
97
98
0
        tmp.pkey_id = type;
99
0
        idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
100
0
        if (idx >= 0)
101
0
            return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
102
0
    }
103
3.87M
    return NULL;
104
3.87M
}
105
106
const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
107
0
{
108
0
    pmeth_fn *ret;
109
0
    EVP_PKEY_METHOD tmp;
110
0
    const EVP_PKEY_METHOD *t;
111
112
0
    if ((t = evp_pkey_meth_find_added_by_application(type)) != NULL)
113
0
        return t;
114
115
0
    tmp.pkey_id = type;
116
0
    t = &tmp;
117
0
    ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
118
0
        OSSL_NELEM(standard_methods));
119
0
    if (ret == NULL || *ret == NULL)
120
0
        return NULL;
121
0
    return (**ret)();
122
0
}
123
124
EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
125
0
{
126
0
    EVP_PKEY_METHOD *pmeth;
127
128
0
    pmeth = OPENSSL_zalloc(sizeof(*pmeth));
129
0
    if (pmeth == NULL)
130
0
        return NULL;
131
132
0
    pmeth->pkey_id = id;
133
0
    pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
134
0
    return pmeth;
135
0
}
136
#endif /* FIPS_MODULE */
137
138
int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx)
139
88.7k
{
140
88.7k
    if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
141
0
        return EVP_PKEY_STATE_UNKNOWN;
142
143
88.7k
    if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
144
0
            && ctx->op.kex.algctx != NULL)
145
88.7k
        || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
146
49.3k
            && ctx->op.sig.algctx != NULL)
147
39.3k
        || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
148
8.08k
            && ctx->op.ciph.algctx != NULL)
149
31.2k
        || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
150
31.2k
            && ctx->op.keymgmt.genctx != NULL)
151
0
        || (EVP_PKEY_CTX_IS_KEM_OP(ctx)
152
0
            && ctx->op.encap.algctx != NULL))
153
88.7k
        return EVP_PKEY_STATE_PROVIDER;
154
155
0
    return EVP_PKEY_STATE_LEGACY;
156
88.7k
}
157
158
static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx,
159
    EVP_PKEY *pkey, ENGINE *e,
160
    const char *keytype, const char *propquery,
161
    int id)
162
163
1.79M
{
164
1.79M
    EVP_PKEY_CTX *ret = NULL;
165
1.79M
    const EVP_PKEY_METHOD *pmeth = NULL, *app_pmeth = NULL;
166
1.79M
    EVP_KEYMGMT *keymgmt = NULL;
167
168
    /* Code below to be removed when legacy support is dropped. */
169
    /* BEGIN legacy */
170
1.79M
    if (id == -1) {
171
1.79M
        if (pkey != NULL && !evp_pkey_is_provided(pkey)) {
172
1.66M
            id = pkey->type;
173
1.66M
        } else {
174
125k
            if (pkey != NULL) {
175
                /* Must be provided if we get here */
176
71.0k
                keytype = EVP_KEYMGMT_get0_name(pkey->keymgmt);
177
71.0k
            }
178
125k
#ifndef FIPS_MODULE
179
125k
            if (keytype != NULL) {
180
124k
                id = evp_pkey_name2type(keytype);
181
124k
                if (id == NID_undef)
182
14.4k
                    id = -1;
183
124k
            }
184
125k
#endif
185
125k
        }
186
1.79M
    }
187
    /* If no ID was found here, we can only resort to find a keymgmt */
188
1.79M
    if (id == -1) {
189
14.7k
#ifndef FIPS_MODULE
190
        /* Using engine with a key without id will not work */
191
14.7k
        if (e != NULL) {
192
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
193
0
            return NULL;
194
0
        }
195
14.7k
#endif
196
14.7k
        goto common;
197
14.7k
    }
198
199
1.77M
#ifndef FIPS_MODULE
200
    /*
201
     * Here, we extract what information we can for the purpose of
202
     * supporting usage with implementations from providers, to make
203
     * for a smooth transition from legacy stuff to provider based stuff.
204
     *
205
     * If an engine is given, this is entirely legacy, and we should not
206
     * pretend anything else, so we clear the name.
207
     */
208
1.77M
    if (e != NULL)
209
0
        keytype = NULL;
210
1.77M
    if (e == NULL && (pkey == NULL || pkey->foreign == 0))
211
1.77M
        keytype = OBJ_nid2sn(id);
212
213
1.77M
#ifndef OPENSSL_NO_ENGINE
214
1.77M
    if (e == NULL && pkey != NULL)
215
1.73M
        e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
216
    /* Try to find an ENGINE which implements this method */
217
1.77M
    if (e != NULL) {
218
0
        if (!ENGINE_init(e)) {
219
0
            ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
220
0
            return NULL;
221
0
        }
222
1.77M
    } else {
223
1.77M
        e = ENGINE_get_pkey_meth_engine(id);
224
1.77M
    }
225
226
    /*
227
     * If an ENGINE handled this method look it up. Otherwise use internal
228
     * tables.
229
     */
230
1.77M
    if (e != NULL)
231
0
        pmeth = ENGINE_get_pkey_meth(e, id);
232
1.77M
    else
233
1.77M
#endif /* OPENSSL_NO_ENGINE */
234
1.77M
        if (pkey != NULL && pkey->foreign)
235
0
            pmeth = EVP_PKEY_meth_find(id);
236
1.77M
        else
237
1.77M
            app_pmeth = pmeth = evp_pkey_meth_find_added_by_application(id);
238
239
        /* END legacy */
240
1.77M
#endif /* FIPS_MODULE */
241
1.79M
common:
242
    /*
243
     * If there's no engine and no app supplied pmeth and there's a name, we try
244
     * fetching a provider implementation.
245
     */
246
1.79M
    if (e == NULL && app_pmeth == NULL && keytype != NULL) {
247
        /*
248
         * If |pkey| is given and is provided, we take a reference to its
249
         * keymgmt.  Otherwise, we fetch one for the keytype we got. This
250
         * is to ensure that operation init functions can access what they
251
         * need through this single pointer.
252
         */
253
1.79M
        if (pkey != NULL && pkey->keymgmt != NULL) {
254
71.0k
            if (!EVP_KEYMGMT_up_ref(pkey->keymgmt))
255
71.0k
                ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
256
71.0k
            else
257
71.0k
                keymgmt = pkey->keymgmt;
258
1.72M
        } else {
259
1.72M
            keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
260
1.72M
        }
261
1.79M
        if (keymgmt == NULL)
262
0
            return NULL; /* EVP_KEYMGMT_fetch() recorded an error */
263
264
1.79M
#ifndef FIPS_MODULE
265
        /*
266
         * Chase down the legacy NID, as that might be needed for diverse
267
         * purposes, such as ensure that EVP_PKEY_type() can return sensible
268
         * values. We go through all keymgmt names, because the keytype
269
         * that's passed to this function doesn't necessarily translate
270
         * directly.
271
         */
272
1.79M
        if (keymgmt != NULL) {
273
1.79M
            int tmp_id = evp_keymgmt_get_legacy_alg(keymgmt);
274
275
1.79M
            if (tmp_id != NID_undef) {
276
1.77M
                if (id == -1) {
277
0
                    id = tmp_id;
278
1.77M
                } else {
279
                    /*
280
                     * It really really shouldn't differ.  If it still does,
281
                     * something is very wrong.
282
                     */
283
1.77M
                    if (!ossl_assert(id == tmp_id)) {
284
0
                        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
285
0
                        EVP_KEYMGMT_free(keymgmt);
286
0
                        return NULL;
287
0
                    }
288
1.77M
                }
289
1.77M
            }
290
1.79M
        }
291
1.79M
#endif
292
1.79M
    }
293
294
1.79M
    if (pmeth == NULL && keymgmt == NULL) {
295
288
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
296
1.79M
    } else {
297
1.79M
        ret = OPENSSL_zalloc(sizeof(*ret));
298
1.79M
    }
299
300
1.79M
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
301
1.79M
    if ((ret == NULL || pmeth == NULL) && e != NULL)
302
0
        ENGINE_finish(e);
303
1.79M
#endif
304
305
1.79M
    if (ret == NULL) {
306
288
        EVP_KEYMGMT_free(keymgmt);
307
288
        return NULL;
308
288
    }
309
1.79M
    if (propquery != NULL) {
310
0
        ret->propquery = OPENSSL_strdup(propquery);
311
0
        if (ret->propquery == NULL) {
312
0
            OPENSSL_free(ret);
313
0
            EVP_KEYMGMT_free(keymgmt);
314
0
            return NULL;
315
0
        }
316
0
    }
317
1.79M
    ret->libctx = libctx;
318
1.79M
    ret->keytype = keytype;
319
1.79M
    ret->keymgmt = keymgmt;
320
1.79M
    ret->legacy_keytype = id;
321
1.79M
    ret->engine = e;
322
1.79M
    ret->pmeth = pmeth;
323
1.79M
    ret->operation = EVP_PKEY_OP_UNDEFINED;
324
1.79M
    ret->pkey = pkey;
325
1.79M
    if (pkey != NULL)
326
1.73M
        EVP_PKEY_up_ref(pkey);
327
328
1.79M
    if (pmeth != NULL && pmeth->init != NULL) {
329
0
        if (pmeth->init(ret) <= 0) {
330
0
            ret->pmeth = NULL;
331
0
            EVP_PKEY_CTX_free(ret);
332
0
            return NULL;
333
0
        }
334
0
    }
335
336
1.79M
    return ret;
337
1.79M
}
338
339
/*- All methods below can also be used in FIPS_MODULE */
340
341
EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx,
342
    const char *name,
343
    const char *propquery)
344
295k
{
345
295k
    return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
346
295k
}
347
348
EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey,
349
    const char *propquery)
350
4.58M
{
351
4.58M
    return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
352
4.58M
}
353
354
void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
355
1.50M
{
356
1.50M
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
357
212k
        if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL)
358
212k
            ctx->op.sig.signature->freectx(ctx->op.sig.algctx);
359
212k
        EVP_SIGNATURE_free(ctx->op.sig.signature);
360
212k
        ctx->op.sig.algctx = NULL;
361
212k
        ctx->op.sig.signature = NULL;
362
1.29M
    } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
363
10.8k
        if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL)
364
10.8k
            ctx->op.kex.exchange->freectx(ctx->op.kex.algctx);
365
10.8k
        EVP_KEYEXCH_free(ctx->op.kex.exchange);
366
10.8k
        ctx->op.kex.algctx = NULL;
367
10.8k
        ctx->op.kex.exchange = NULL;
368
1.28M
    } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
369
0
        if (ctx->op.encap.algctx != NULL && ctx->op.encap.kem != NULL)
370
0
            ctx->op.encap.kem->freectx(ctx->op.encap.algctx);
371
0
        EVP_KEM_free(ctx->op.encap.kem);
372
0
        ctx->op.encap.algctx = NULL;
373
0
        ctx->op.encap.kem = NULL;
374
1.28M
    } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
375
4.60k
        if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL)
376
4.60k
            ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx);
377
4.60k
        EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
378
4.60k
        ctx->op.ciph.algctx = NULL;
379
4.60k
        ctx->op.ciph.cipher = NULL;
380
1.27M
    } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
381
35.0k
        if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
382
35.0k
            evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
383
35.0k
    }
384
1.50M
}
385
386
void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
387
27.3M
{
388
27.3M
    if (ctx == NULL)
389
22.6M
        return;
390
4.74M
    if (ctx->pmeth && ctx->pmeth->cleanup)
391
0
        ctx->pmeth->cleanup(ctx);
392
393
4.74M
    evp_pkey_ctx_free_old_ops(ctx);
394
4.74M
#ifndef FIPS_MODULE
395
4.74M
    evp_pkey_ctx_free_all_cached_data(ctx);
396
4.74M
#endif
397
4.74M
    EVP_KEYMGMT_free(ctx->keymgmt);
398
399
4.74M
    OPENSSL_free(ctx->propquery);
400
4.74M
    EVP_PKEY_free(ctx->pkey);
401
4.74M
    EVP_PKEY_free(ctx->peerkey);
402
4.74M
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
403
4.74M
    ENGINE_finish(ctx->engine);
404
4.74M
#endif
405
4.74M
    BN_free(ctx->rsa_pubexp);
406
4.74M
    OPENSSL_free(ctx);
407
4.74M
}
408
409
#ifndef FIPS_MODULE
410
411
void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
412
    const EVP_PKEY_METHOD *meth)
413
0
{
414
0
    if (ppkey_id)
415
0
        *ppkey_id = meth->pkey_id;
416
0
    if (pflags)
417
0
        *pflags = meth->flags;
418
0
}
419
420
void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
421
0
{
422
0
    int pkey_id = dst->pkey_id;
423
0
    int flags = dst->flags;
424
425
0
    *dst = *src;
426
427
    /* We only copy the function pointers so restore the other values */
428
0
    dst->pkey_id = pkey_id;
429
0
    dst->flags = flags;
430
0
}
431
432
void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
433
0
{
434
0
    if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
435
0
        OPENSSL_free(pmeth);
436
0
}
437
438
EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
439
16.8k
{
440
16.8k
    return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
441
16.8k
}
442
443
EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
444
0
{
445
0
    return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
446
0
}
447
448
EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
449
173k
{
450
173k
    EVP_PKEY_CTX *rctx;
451
452
173k
#ifndef OPENSSL_NO_ENGINE
453
    /* Make sure it's safe to copy a pkey context using an ENGINE */
454
173k
    if (pctx->engine && !ENGINE_init(pctx->engine)) {
455
0
        ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
456
0
        return 0;
457
0
    }
458
173k
#endif
459
173k
    rctx = OPENSSL_zalloc(sizeof(*rctx));
460
173k
    if (rctx == NULL)
461
0
        return NULL;
462
463
173k
    if (pctx->pkey != NULL)
464
173k
        EVP_PKEY_up_ref(pctx->pkey);
465
173k
    rctx->pkey = pctx->pkey;
466
173k
    rctx->operation = pctx->operation;
467
173k
    rctx->libctx = pctx->libctx;
468
173k
    rctx->keytype = pctx->keytype;
469
173k
    rctx->propquery = NULL;
470
173k
    if (pctx->propquery != NULL) {
471
0
        rctx->propquery = OPENSSL_strdup(pctx->propquery);
472
0
        if (rctx->propquery == NULL)
473
0
            goto err;
474
0
    }
475
173k
    rctx->legacy_keytype = pctx->legacy_keytype;
476
477
173k
    if (pctx->keymgmt != NULL) {
478
173k
        if (!EVP_KEYMGMT_up_ref(pctx->keymgmt))
479
0
            goto err;
480
173k
        rctx->keymgmt = pctx->keymgmt;
481
173k
    }
482
483
173k
    if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
484
0
        if (pctx->op.kex.exchange != NULL) {
485
0
            rctx->op.kex.exchange = pctx->op.kex.exchange;
486
0
            if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange))
487
0
                goto err;
488
0
        }
489
0
        if (pctx->op.kex.algctx != NULL) {
490
0
            if (!ossl_assert(pctx->op.kex.exchange != NULL))
491
0
                goto err;
492
493
0
            if (pctx->op.kex.exchange->dupctx != NULL)
494
0
                rctx->op.kex.algctx
495
0
                    = pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx);
496
497
0
            if (rctx->op.kex.algctx == NULL) {
498
0
                EVP_KEYEXCH_free(rctx->op.kex.exchange);
499
0
                rctx->op.kex.exchange = NULL;
500
0
                goto err;
501
0
            }
502
0
            return rctx;
503
0
        }
504
173k
    } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
505
173k
        if (pctx->op.sig.signature != NULL) {
506
173k
            rctx->op.sig.signature = pctx->op.sig.signature;
507
173k
            if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
508
0
                goto err;
509
173k
        }
510
173k
        if (pctx->op.sig.algctx != NULL) {
511
173k
            if (!ossl_assert(pctx->op.sig.signature != NULL))
512
0
                goto err;
513
514
173k
            if (pctx->op.sig.signature->dupctx != NULL)
515
173k
                rctx->op.sig.algctx
516
173k
                    = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx);
517
518
173k
            if (rctx->op.sig.algctx == NULL) {
519
0
                EVP_SIGNATURE_free(rctx->op.sig.signature);
520
0
                rctx->op.sig.signature = NULL;
521
0
                goto err;
522
0
            }
523
173k
            return rctx;
524
173k
        }
525
173k
    } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
526
0
        if (pctx->op.ciph.cipher != NULL) {
527
0
            rctx->op.ciph.cipher = pctx->op.ciph.cipher;
528
0
            if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
529
0
                goto err;
530
0
        }
531
0
        if (pctx->op.ciph.algctx != NULL) {
532
0
            if (!ossl_assert(pctx->op.ciph.cipher != NULL))
533
0
                goto err;
534
535
0
            if (pctx->op.ciph.cipher->dupctx != NULL)
536
0
                rctx->op.ciph.algctx
537
0
                    = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx);
538
539
0
            if (rctx->op.ciph.algctx == NULL) {
540
0
                EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
541
0
                rctx->op.ciph.cipher = NULL;
542
0
                goto err;
543
0
            }
544
0
            return rctx;
545
0
        }
546
0
    } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) {
547
0
        if (pctx->op.encap.kem != NULL) {
548
0
            rctx->op.encap.kem = pctx->op.encap.kem;
549
0
            if (!EVP_KEM_up_ref(rctx->op.encap.kem))
550
0
                goto err;
551
0
        }
552
0
        if (pctx->op.encap.algctx != NULL) {
553
0
            if (!ossl_assert(pctx->op.encap.kem != NULL))
554
0
                goto err;
555
556
0
            if (pctx->op.encap.kem->dupctx != NULL)
557
0
                rctx->op.encap.algctx
558
0
                    = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx);
559
560
0
            if (rctx->op.encap.algctx == NULL) {
561
0
                EVP_KEM_free(rctx->op.encap.kem);
562
0
                rctx->op.encap.kem = NULL;
563
0
                goto err;
564
0
            }
565
0
            return rctx;
566
0
        }
567
0
    } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
568
        /* Not supported - This would need a gen_dupctx() to work */
569
0
        goto err;
570
0
    }
571
572
0
    rctx->pmeth = pctx->pmeth;
573
0
#ifndef OPENSSL_NO_ENGINE
574
0
    rctx->engine = pctx->engine;
575
0
#endif
576
577
0
    if (pctx->peerkey != NULL)
578
0
        EVP_PKEY_up_ref(pctx->peerkey);
579
0
    rctx->peerkey = pctx->peerkey;
580
581
0
    if (pctx->pmeth == NULL) {
582
0
        if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
583
0
            EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
584
0
            void *provkey;
585
586
0
            if (pctx->pkey == NULL)
587
0
                return rctx;
588
589
0
            provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
590
0
                &tmp_keymgmt, pctx->propquery);
591
0
            if (provkey == NULL)
592
0
                goto err;
593
0
            if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
594
0
                goto err;
595
0
            EVP_KEYMGMT_free(rctx->keymgmt);
596
0
            rctx->keymgmt = tmp_keymgmt;
597
0
            return rctx;
598
0
        }
599
0
    } else if (pctx->pmeth->copy(rctx, pctx) > 0) {
600
0
        return rctx;
601
0
    }
602
0
err:
603
0
    rctx->pmeth = NULL;
604
0
    EVP_PKEY_CTX_free(rctx);
605
0
    return NULL;
606
0
}
607
608
int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
609
0
{
610
0
    if (app_pkey_methods == NULL) {
611
0
        app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
612
0
        if (app_pkey_methods == NULL) {
613
0
            ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
614
0
            return 0;
615
0
        }
616
0
    }
617
0
    if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
618
0
        ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
619
0
        return 0;
620
0
    }
621
0
    sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
622
0
    return 1;
623
0
}
624
625
void evp_app_cleanup_int(void)
626
220
{
627
220
    if (app_pkey_methods != NULL)
628
0
        sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
629
220
}
630
631
int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
632
0
{
633
0
    const EVP_PKEY_METHOD *ret;
634
635
0
    ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
636
637
0
    return ret == NULL ? 0 : 1;
638
0
}
639
640
size_t EVP_PKEY_meth_get_count(void)
641
0
{
642
0
    size_t rv = OSSL_NELEM(standard_methods);
643
644
0
    if (app_pkey_methods)
645
0
        rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
646
0
    return rv;
647
0
}
648
649
const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
650
0
{
651
0
    if (idx < OSSL_NELEM(standard_methods))
652
0
        return (standard_methods[idx])();
653
0
    if (app_pkey_methods == NULL)
654
0
        return NULL;
655
0
    idx -= OSSL_NELEM(standard_methods);
656
0
    if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
657
0
        return NULL;
658
0
    return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
659
0
}
660
#endif
661
662
int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype)
663
0
{
664
0
#ifndef FIPS_MODULE
665
0
    if (evp_pkey_ctx_is_legacy(ctx))
666
0
        return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype));
667
0
#endif
668
0
    return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype);
669
0
}
670
671
int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
672
73.4k
{
673
73.4k
    switch (evp_pkey_ctx_state(ctx)) {
674
73.4k
    case EVP_PKEY_STATE_PROVIDER:
675
73.4k
        if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
676
0
            && ctx->op.kex.exchange != NULL
677
0
            && ctx->op.kex.exchange->set_ctx_params != NULL)
678
0
            return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx,
679
0
                params);
680
73.4k
        if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
681
36.7k
            && ctx->op.sig.signature != NULL
682
36.7k
            && ctx->op.sig.signature->set_ctx_params != NULL)
683
36.7k
            return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx,
684
36.7k
                params);
685
36.6k
        if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
686
5.39k
            && ctx->op.ciph.cipher != NULL
687
5.39k
            && ctx->op.ciph.cipher->set_ctx_params != NULL)
688
5.39k
            return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx,
689
5.39k
                params);
690
31.2k
        if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
691
31.2k
            && ctx->keymgmt != NULL
692
31.2k
            && ctx->keymgmt->gen_set_params != NULL)
693
31.2k
            return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
694
31.2k
                params);
695
0
        if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
696
0
            && ctx->op.encap.kem != NULL
697
0
            && ctx->op.encap.kem->set_ctx_params != NULL)
698
0
            return ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx,
699
0
                params);
700
0
        break;
701
0
    case EVP_PKEY_STATE_UNKNOWN:
702
0
        break;
703
0
#ifndef FIPS_MODULE
704
0
    case EVP_PKEY_STATE_LEGACY:
705
0
        return evp_pkey_ctx_set_params_to_ctrl(ctx, params);
706
73.4k
#endif
707
73.4k
    }
708
0
    return 0;
709
73.4k
}
710
711
int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
712
0
{
713
0
    switch (evp_pkey_ctx_state(ctx)) {
714
0
    case EVP_PKEY_STATE_PROVIDER:
715
0
        if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
716
0
            && ctx->op.kex.exchange != NULL
717
0
            && ctx->op.kex.exchange->get_ctx_params != NULL)
718
0
            return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx,
719
0
                params);
720
0
        if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
721
0
            && ctx->op.sig.signature != NULL
722
0
            && ctx->op.sig.signature->get_ctx_params != NULL)
723
0
            return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx,
724
0
                params);
725
0
        if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
726
0
            && ctx->op.ciph.cipher != NULL
727
0
            && ctx->op.ciph.cipher->get_ctx_params != NULL)
728
0
            return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx,
729
0
                params);
730
0
        if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
731
0
            && ctx->op.encap.kem != NULL
732
0
            && ctx->op.encap.kem->get_ctx_params != NULL)
733
0
            return ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx,
734
0
                params);
735
0
        break;
736
0
    case EVP_PKEY_STATE_UNKNOWN:
737
0
        break;
738
0
#ifndef FIPS_MODULE
739
0
    case EVP_PKEY_STATE_LEGACY:
740
0
        return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
741
0
#endif
742
0
    }
743
0
    return 0;
744
0
}
745
746
#ifndef FIPS_MODULE
747
const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx)
748
0
{
749
0
    void *provctx;
750
751
0
    if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
752
0
        && ctx->op.kex.exchange != NULL
753
0
        && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
754
0
        provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
755
0
        return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx,
756
0
            provctx);
757
0
    }
758
0
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
759
0
        && ctx->op.sig.signature != NULL
760
0
        && ctx->op.sig.signature->gettable_ctx_params != NULL) {
761
0
        provctx = ossl_provider_ctx(
762
0
            EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
763
0
        return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx,
764
0
            provctx);
765
0
    }
766
0
    if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
767
0
        && ctx->op.ciph.cipher != NULL
768
0
        && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
769
0
        provctx = ossl_provider_ctx(
770
0
            EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
771
0
        return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx,
772
0
            provctx);
773
0
    }
774
0
    if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
775
0
        && ctx->op.encap.kem != NULL
776
0
        && ctx->op.encap.kem->gettable_ctx_params != NULL) {
777
0
        provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
778
0
        return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx,
779
0
            provctx);
780
0
    }
781
0
    return NULL;
782
0
}
783
784
const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx)
785
15.3k
{
786
15.3k
    void *provctx;
787
788
15.3k
    if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
789
0
        && ctx->op.kex.exchange != NULL
790
0
        && ctx->op.kex.exchange->settable_ctx_params != NULL) {
791
0
        provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
792
0
        return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx,
793
0
            provctx);
794
0
    }
795
15.3k
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
796
12.6k
        && ctx->op.sig.signature != NULL
797
12.6k
        && ctx->op.sig.signature->settable_ctx_params != NULL) {
798
12.6k
        provctx = ossl_provider_ctx(
799
12.6k
            EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
800
12.6k
        return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx,
801
12.6k
            provctx);
802
12.6k
    }
803
2.69k
    if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
804
2.69k
        && ctx->op.ciph.cipher != NULL
805
2.69k
        && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
806
2.69k
        provctx = ossl_provider_ctx(
807
2.69k
            EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
808
2.69k
        return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx,
809
2.69k
            provctx);
810
2.69k
    }
811
0
    if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
812
0
        && ctx->keymgmt != NULL
813
0
        && ctx->keymgmt->gen_settable_params != NULL) {
814
0
        provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
815
0
        return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx,
816
0
            provctx);
817
0
    }
818
0
    if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
819
0
        && ctx->op.encap.kem != NULL
820
0
        && ctx->op.encap.kem->settable_ctx_params != NULL) {
821
0
        provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
822
0
        return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx,
823
0
            provctx);
824
0
    }
825
0
    return NULL;
826
0
}
827
828
/*
829
 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
830
 *
831
 * Return 1 on success, 0 or negative for errors.
832
 *
833
 * In particular they return -2 if any of the params is not supported.
834
 *
835
 * They are not available in FIPS_MODULE as they depend on
836
 *      - EVP_PKEY_CTX_{get,set}_params()
837
 *      - EVP_PKEY_CTX_{gettable,settable}_params()
838
 *
839
 */
840
int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
841
62.9k
{
842
62.9k
    if (ctx == NULL || params == NULL)
843
0
        return 0;
844
845
    /*
846
     * We only check for provider side EVP_PKEY_CTX.  For #legacy, we
847
     * depend on the translation that happens in EVP_PKEY_CTX_set_params()
848
     * call, and that the resulting ctrl call will return -2 if it doesn't
849
     * known the ctrl command number.
850
     */
851
62.9k
    if (evp_pkey_ctx_is_provided(ctx)) {
852
62.9k
        const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
853
62.9k
        const OSSL_PARAM *p;
854
855
125k
        for (p = params; p->key != NULL; p++) {
856
            /* Check the ctx actually understands this parameter */
857
62.9k
            if (OSSL_PARAM_locate_const(settable, p->key) == NULL)
858
0
                return -2;
859
62.9k
        }
860
62.9k
    }
861
862
62.9k
    return EVP_PKEY_CTX_set_params(ctx, params);
863
62.9k
}
864
865
int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
866
0
{
867
0
    if (ctx == NULL || params == NULL)
868
0
        return 0;
869
870
    /*
871
     * We only check for provider side EVP_PKEY_CTX.  For #legacy, we
872
     * depend on the translation that happens in EVP_PKEY_CTX_get_params()
873
     * call, and that the resulting ctrl call will return -2 if it doesn't
874
     * known the ctrl command number.
875
     */
876
0
    if (evp_pkey_ctx_is_provided(ctx)) {
877
0
        const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
878
0
        const OSSL_PARAM *p;
879
880
0
        for (p = params; p->key != NULL; p++) {
881
            /* Check the ctx actually understands this parameter */
882
0
            if (OSSL_PARAM_locate_const(gettable, p->key) == NULL)
883
0
                return -2;
884
0
        }
885
0
    }
886
887
0
    return EVP_PKEY_CTX_get_params(ctx, params);
888
0
}
889
890
int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
891
0
{
892
0
    OSSL_PARAM sig_md_params[2], *p = sig_md_params;
893
    /* 80 should be big enough */
894
0
    char name[80] = "";
895
0
    const EVP_MD *tmp;
896
897
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
898
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
899
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
900
0
        return -2;
901
0
    }
902
903
0
    if (ctx->op.sig.algctx == NULL)
904
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
905
0
            EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
906
907
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
908
0
        name,
909
0
        sizeof(name));
910
0
    *p = OSSL_PARAM_construct_end();
911
912
0
    if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
913
0
        return 0;
914
915
0
    tmp = evp_get_digestbyname_ex(ctx->libctx, name);
916
0
    if (tmp == NULL)
917
0
        return 0;
918
919
0
    *md = tmp;
920
921
0
    return 1;
922
0
}
923
924
static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
925
    int fallback, const char *param, int op,
926
    int ctrl)
927
0
{
928
0
    OSSL_PARAM md_params[2], *p = md_params;
929
0
    const char *name;
930
931
0
    if (ctx == NULL || (ctx->operation & op) == 0) {
932
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
933
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
934
0
        return -2;
935
0
    }
936
937
0
    if (fallback)
938
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
939
940
0
    if (md == NULL) {
941
0
        name = "";
942
0
    } else {
943
0
        name = EVP_MD_get0_name(md);
944
0
    }
945
946
0
    *p++ = OSSL_PARAM_construct_utf8_string(param,
947
        /*
948
         * Cast away the const. This is read
949
         * only so should be safe
950
         */
951
0
        (char *)name, 0);
952
0
    *p = OSSL_PARAM_construct_end();
953
954
0
    return EVP_PKEY_CTX_set_params(ctx, md_params);
955
0
}
956
957
int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
958
0
{
959
0
    return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL,
960
0
        OSSL_SIGNATURE_PARAM_DIGEST,
961
0
        EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
962
0
}
963
964
int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
965
0
{
966
0
    return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
967
0
        OSSL_KDF_PARAM_DIGEST,
968
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
969
0
}
970
971
static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
972
    const char *param, int op, int ctrl,
973
    const unsigned char *data,
974
    int datalen)
975
0
{
976
0
    OSSL_PARAM octet_string_params[2], *p = octet_string_params;
977
978
0
    if (ctx == NULL || (ctx->operation & op) == 0) {
979
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
980
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
981
0
        return -2;
982
0
    }
983
984
    /* Code below to be removed when legacy support is dropped. */
985
0
    if (fallback)
986
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
987
    /* end of legacy support */
988
989
0
    if (datalen < 0) {
990
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
991
0
        return 0;
992
0
    }
993
994
0
    *p++ = OSSL_PARAM_construct_octet_string(param,
995
        /*
996
         * Cast away the const. This is read
997
         * only so should be safe
998
         */
999
0
        (unsigned char *)data,
1000
0
        (size_t)datalen);
1001
0
    *p = OSSL_PARAM_construct_end();
1002
1003
0
    return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
1004
0
}
1005
1006
static int evp_pkey_ctx_add1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
1007
    const char *param, int op, int ctrl,
1008
    const unsigned char *data,
1009
    int datalen)
1010
0
{
1011
0
    OSSL_PARAM os_params[2];
1012
0
    const OSSL_PARAM *gettables;
1013
0
    unsigned char *info = NULL;
1014
0
    size_t info_len = 0;
1015
0
    size_t info_alloc = 0;
1016
0
    int ret = 0;
1017
1018
0
    if (ctx == NULL || (ctx->operation & op) == 0) {
1019
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1020
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1021
0
        return -2;
1022
0
    }
1023
1024
    /* Code below to be removed when legacy support is dropped. */
1025
0
    if (fallback)
1026
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
1027
    /* end of legacy support */
1028
1029
0
    if (datalen < 0) {
1030
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
1031
0
        return 0;
1032
0
    } else if (datalen == 0) {
1033
0
        return 1;
1034
0
    }
1035
1036
    /* Check for older provider that doesn't support getting this parameter */
1037
0
    gettables = EVP_PKEY_CTX_gettable_params(ctx);
1038
0
    if (gettables == NULL || OSSL_PARAM_locate_const(gettables, param) == NULL)
1039
0
        return evp_pkey_ctx_set1_octet_string(ctx, fallback, param, op, ctrl,
1040
0
            data, datalen);
1041
1042
    /* Get the original value length */
1043
0
    os_params[0] = OSSL_PARAM_construct_octet_string(param, NULL, 0);
1044
0
    os_params[1] = OSSL_PARAM_construct_end();
1045
1046
0
    if (!EVP_PKEY_CTX_get_params(ctx, os_params))
1047
0
        return 0;
1048
1049
    /* This should not happen but check to be sure. */
1050
0
    if (os_params[0].return_size == OSSL_PARAM_UNMODIFIED)
1051
0
        return 0;
1052
1053
0
    info_alloc = os_params[0].return_size + datalen;
1054
0
    if (info_alloc == 0)
1055
0
        return 0;
1056
0
    info = OPENSSL_zalloc(info_alloc);
1057
0
    if (info == NULL)
1058
0
        return 0;
1059
0
    info_len = os_params[0].return_size;
1060
1061
0
    os_params[0] = OSSL_PARAM_construct_octet_string(param, info, info_alloc);
1062
1063
    /* if we have data, then go get it */
1064
0
    if (info_len > 0) {
1065
0
        if (!EVP_PKEY_CTX_get_params(ctx, os_params))
1066
0
            goto error;
1067
0
    }
1068
1069
    /* Copy the input data */
1070
0
    memcpy(&info[info_len], data, datalen);
1071
0
    ret = EVP_PKEY_CTX_set_params(ctx, os_params);
1072
1073
0
error:
1074
0
    OPENSSL_clear_free(info, info_alloc);
1075
0
    return ret;
1076
0
}
1077
1078
int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
1079
    const unsigned char *sec, int seclen)
1080
0
{
1081
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1082
0
        OSSL_KDF_PARAM_SECRET,
1083
0
        EVP_PKEY_OP_DERIVE,
1084
0
        EVP_PKEY_CTRL_TLS_SECRET,
1085
0
        sec, seclen);
1086
0
}
1087
1088
int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
1089
    const unsigned char *seed, int seedlen)
1090
0
{
1091
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1092
0
        OSSL_KDF_PARAM_SEED,
1093
0
        EVP_PKEY_OP_DERIVE,
1094
0
        EVP_PKEY_CTRL_TLS_SEED,
1095
0
        seed, seedlen);
1096
0
}
1097
1098
int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1099
0
{
1100
0
    return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
1101
0
        OSSL_KDF_PARAM_DIGEST,
1102
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
1103
0
}
1104
1105
int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
1106
    const unsigned char *salt, int saltlen)
1107
0
{
1108
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1109
0
        OSSL_KDF_PARAM_SALT,
1110
0
        EVP_PKEY_OP_DERIVE,
1111
0
        EVP_PKEY_CTRL_HKDF_SALT,
1112
0
        salt, saltlen);
1113
0
}
1114
1115
int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
1116
    const unsigned char *key, int keylen)
1117
0
{
1118
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1119
0
        OSSL_KDF_PARAM_KEY,
1120
0
        EVP_PKEY_OP_DERIVE,
1121
0
        EVP_PKEY_CTRL_HKDF_KEY,
1122
0
        key, keylen);
1123
0
}
1124
1125
int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
1126
    const unsigned char *info, int infolen)
1127
0
{
1128
0
    return evp_pkey_ctx_add1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1129
0
        OSSL_KDF_PARAM_INFO,
1130
0
        EVP_PKEY_OP_DERIVE,
1131
0
        EVP_PKEY_CTRL_HKDF_INFO,
1132
0
        info, infolen);
1133
0
}
1134
1135
int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
1136
0
{
1137
0
    OSSL_PARAM int_params[2], *p = int_params;
1138
1139
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1140
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1141
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1142
0
        return -2;
1143
0
    }
1144
1145
    /* Code below to be removed when legacy support is dropped. */
1146
0
    if (ctx->op.kex.algctx == NULL)
1147
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
1148
0
            EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
1149
    /* end of legacy support */
1150
1151
0
    if (mode < 0) {
1152
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1153
0
        return 0;
1154
0
    }
1155
1156
0
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
1157
0
    *p = OSSL_PARAM_construct_end();
1158
1159
0
    return EVP_PKEY_CTX_set_params(ctx, int_params);
1160
0
}
1161
1162
int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
1163
    int passlen)
1164
0
{
1165
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1166
0
        OSSL_KDF_PARAM_PASSWORD,
1167
0
        EVP_PKEY_OP_DERIVE,
1168
0
        EVP_PKEY_CTRL_PASS,
1169
0
        (const unsigned char *)pass, passlen);
1170
0
}
1171
1172
int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
1173
    const unsigned char *salt, int saltlen)
1174
0
{
1175
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1176
0
        OSSL_KDF_PARAM_SALT,
1177
0
        EVP_PKEY_OP_DERIVE,
1178
0
        EVP_PKEY_CTRL_SCRYPT_SALT,
1179
0
        salt, saltlen);
1180
0
}
1181
1182
static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
1183
    int op, int ctrl, uint64_t val)
1184
0
{
1185
0
    OSSL_PARAM uint64_params[2], *p = uint64_params;
1186
1187
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1188
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1189
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1190
0
        return -2;
1191
0
    }
1192
1193
    /* Code below to be removed when legacy support is dropped. */
1194
0
    if (ctx->op.kex.algctx == NULL)
1195
0
        return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
1196
    /* end of legacy support */
1197
1198
0
    *p++ = OSSL_PARAM_construct_uint64(param, &val);
1199
0
    *p = OSSL_PARAM_construct_end();
1200
1201
0
    return EVP_PKEY_CTX_set_params(ctx, uint64_params);
1202
0
}
1203
1204
int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
1205
0
{
1206
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
1207
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
1208
0
        n);
1209
0
}
1210
1211
int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
1212
0
{
1213
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
1214
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
1215
0
        r);
1216
0
}
1217
1218
int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
1219
0
{
1220
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
1221
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
1222
0
        p);
1223
0
}
1224
1225
int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
1226
    uint64_t maxmem_bytes)
1227
0
{
1228
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
1229
0
        EVP_PKEY_OP_DERIVE,
1230
0
        EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
1231
0
        maxmem_bytes);
1232
0
}
1233
1234
int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1235
    int keylen)
1236
0
{
1237
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1238
0
        OSSL_PKEY_PARAM_PRIV_KEY,
1239
0
        EVP_PKEY_OP_KEYGEN,
1240
0
        EVP_PKEY_CTRL_SET_MAC_KEY,
1241
0
        key, keylen);
1242
0
}
1243
1244
int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
1245
0
{
1246
0
    OSSL_PARAM params[2], *p = params;
1247
1248
0
    if (ctx == NULL || op == NULL) {
1249
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1250
0
        return 0;
1251
0
    }
1252
0
    if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1253
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1254
0
        return -2;
1255
0
    }
1256
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
1257
0
        (char *)op, 0);
1258
0
    *p = OSSL_PARAM_construct_end();
1259
0
    return EVP_PKEY_CTX_set_params(ctx, params);
1260
0
}
1261
1262
int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
1263
0
{
1264
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1265
0
        EVP_PKEY_CTRL_SET1_ID, (int)len, (void *)(id));
1266
0
}
1267
1268
int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
1269
0
{
1270
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void *)id);
1271
0
}
1272
1273
int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
1274
0
{
1275
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1276
0
        EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void *)id_len);
1277
0
}
1278
1279
static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1280
    int cmd, int p1, void *p2)
1281
49.5k
{
1282
49.5k
    int ret = 0;
1283
1284
    /*
1285
     * If the method has a |digest_custom| function, we can relax the
1286
     * operation type check, since this can be called before the operation
1287
     * is initialized.
1288
     */
1289
49.5k
    if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
1290
49.5k
        if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
1291
0
            ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
1292
0
            return -1;
1293
0
        }
1294
1295
49.5k
        if ((optype != -1) && !(ctx->operation & optype)) {
1296
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1297
0
            return -1;
1298
0
        }
1299
49.5k
    }
1300
1301
49.5k
    switch (evp_pkey_ctx_state(ctx)) {
1302
49.5k
    case EVP_PKEY_STATE_PROVIDER:
1303
49.5k
        return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
1304
0
    case EVP_PKEY_STATE_UNKNOWN:
1305
0
    case EVP_PKEY_STATE_LEGACY:
1306
0
        if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
1307
0
            ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1308
0
            return -2;
1309
0
        }
1310
0
        if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1311
0
            return -1;
1312
1313
0
        ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
1314
1315
0
        if (ret == -2)
1316
0
            ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1317
0
        break;
1318
49.5k
    }
1319
0
    return ret;
1320
49.5k
}
1321
1322
int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1323
    int cmd, int p1, void *p2)
1324
62.7k
{
1325
62.7k
    int ret = 0;
1326
1327
62.7k
    if (ctx == NULL) {
1328
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1329
0
        return -2;
1330
0
    }
1331
    /* If unsupported, we don't want that reported here */
1332
62.7k
    ERR_set_mark();
1333
62.7k
    ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1334
62.7k
        cmd, NULL, p2, p1);
1335
62.7k
    if (ret == -2) {
1336
62.7k
        ERR_pop_to_mark();
1337
62.7k
    } else {
1338
0
        ERR_clear_last_mark();
1339
        /*
1340
         * If there was an error, there was an error.
1341
         * If the operation isn't initialized yet, we also return, as
1342
         * the saved values will be used then anyway.
1343
         */
1344
0
        if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1345
0
            return ret;
1346
0
    }
1347
62.7k
    return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
1348
62.7k
}
1349
1350
int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
1351
    int cmd, uint64_t value)
1352
0
{
1353
0
    return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1354
0
}
1355
1356
static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1357
    const char *name, const char *value)
1358
0
{
1359
0
    int ret = 0;
1360
1361
0
    if (ctx == NULL) {
1362
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1363
0
        return -2;
1364
0
    }
1365
1366
0
    switch (evp_pkey_ctx_state(ctx)) {
1367
0
    case EVP_PKEY_STATE_PROVIDER:
1368
0
        return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
1369
0
    case EVP_PKEY_STATE_UNKNOWN:
1370
0
    case EVP_PKEY_STATE_LEGACY:
1371
0
        if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
1372
0
            ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1373
0
            return -2;
1374
0
        }
1375
0
        if (strcmp(name, "digest") == 0)
1376
0
            ret = EVP_PKEY_CTX_md(ctx,
1377
0
                EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1378
0
                EVP_PKEY_CTRL_MD, value);
1379
0
        else
1380
0
            ret = ctx->pmeth->ctrl_str(ctx, name, value);
1381
0
        break;
1382
0
    }
1383
1384
0
    return ret;
1385
0
}
1386
1387
int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1388
    const char *name, const char *value)
1389
0
{
1390
0
    int ret = 0;
1391
1392
    /* If unsupported, we don't want that reported here */
1393
0
    ERR_set_mark();
1394
0
    ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1395
0
        name, value, strlen(value) + 1);
1396
0
    if (ret == -2) {
1397
0
        ERR_pop_to_mark();
1398
0
    } else {
1399
0
        ERR_clear_last_mark();
1400
        /*
1401
         * If there was an error, there was an error.
1402
         * If the operation isn't initialized yet, we also return, as
1403
         * the saved values will be used then anyway.
1404
         */
1405
0
        if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1406
0
            return ret;
1407
0
    }
1408
1409
0
    return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1410
0
}
1411
1412
static int decode_cmd(int cmd, const char *name)
1413
5.95M
{
1414
5.95M
    if (cmd == -1) {
1415
        /*
1416
         * The consequence of the assertion not being true is that this
1417
         * function will return -1, which will cause the calling functions
1418
         * to signal that the command is unsupported...  in non-debug mode.
1419
         */
1420
0
        if (ossl_assert(name != NULL))
1421
0
            if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1422
0
                cmd = EVP_PKEY_CTRL_SET1_ID;
1423
0
    }
1424
1425
5.95M
    return cmd;
1426
5.95M
}
1427
1428
static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1429
    int keytype, int optype,
1430
    int cmd, const char *name,
1431
    const void *data, size_t data_len)
1432
49.5k
{
1433
    /*
1434
     * Check that it's one of the supported commands.  The ctrl commands
1435
     * number cases here must correspond to the cases in the bottom switch
1436
     * in this function.
1437
     */
1438
49.5k
    switch (cmd = decode_cmd(cmd, name)) {
1439
0
    case EVP_PKEY_CTRL_SET1_ID:
1440
0
        break;
1441
49.5k
    default:
1442
49.5k
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1443
49.5k
        return -2;
1444
49.5k
    }
1445
1446
0
    if (keytype != -1) {
1447
0
        switch (evp_pkey_ctx_state(ctx)) {
1448
0
        case EVP_PKEY_STATE_PROVIDER:
1449
0
            if (ctx->keymgmt == NULL) {
1450
0
                ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1451
0
                return -2;
1452
0
            }
1453
0
            if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
1454
0
                    evp_pkey_type2name(keytype))) {
1455
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1456
0
                return -1;
1457
0
            }
1458
0
            break;
1459
0
        case EVP_PKEY_STATE_UNKNOWN:
1460
0
        case EVP_PKEY_STATE_LEGACY:
1461
0
            if (ctx->pmeth == NULL) {
1462
0
                ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1463
0
                return -2;
1464
0
            }
1465
0
            if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
1466
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1467
0
                return -1;
1468
0
            }
1469
0
            break;
1470
0
        }
1471
0
    }
1472
0
    if (optype != -1 && (ctx->operation & optype) == 0) {
1473
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1474
0
        return -1;
1475
0
    }
1476
1477
0
    switch (cmd) {
1478
0
    case EVP_PKEY_CTRL_SET1_ID:
1479
0
        evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1480
0
        if (name != NULL) {
1481
0
            ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
1482
0
            if (ctx->cached_parameters.dist_id_name == NULL)
1483
0
                return 0;
1484
0
        }
1485
0
        if (data_len > 0) {
1486
0
            ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
1487
0
            if (ctx->cached_parameters.dist_id == NULL)
1488
0
                return 0;
1489
0
        }
1490
0
        ctx->cached_parameters.dist_id_set = 1;
1491
0
        ctx->cached_parameters.dist_id_len = data_len;
1492
0
        break;
1493
0
    }
1494
0
    return 1;
1495
0
}
1496
1497
static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
1498
    int cmd, const char *name)
1499
5.88M
{
1500
5.88M
    cmd = decode_cmd(cmd, name);
1501
5.88M
    switch (cmd) {
1502
5.88M
    case EVP_PKEY_CTRL_SET1_ID:
1503
5.88M
        OPENSSL_free(ctx->cached_parameters.dist_id);
1504
5.88M
        OPENSSL_free(ctx->cached_parameters.dist_id_name);
1505
5.88M
        ctx->cached_parameters.dist_id = NULL;
1506
5.88M
        ctx->cached_parameters.dist_id_name = NULL;
1507
5.88M
        break;
1508
5.88M
    }
1509
5.88M
}
1510
1511
static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
1512
5.88M
{
1513
5.88M
    evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
1514
5.88M
}
1515
1516
int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
1517
117k
{
1518
117k
    int ret = 1;
1519
1520
117k
    if (ret && ctx->cached_parameters.dist_id_set) {
1521
0
        const char *name = ctx->cached_parameters.dist_id_name;
1522
0
        const void *val = ctx->cached_parameters.dist_id;
1523
0
        size_t len = ctx->cached_parameters.dist_id_len;
1524
1525
0
        if (name != NULL)
1526
0
            ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
1527
0
        else
1528
0
            ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
1529
0
                EVP_PKEY_CTRL_SET1_ID,
1530
0
                (int)len, (void *)val);
1531
0
    }
1532
1533
117k
    return ret;
1534
117k
}
1535
1536
OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
1537
0
{
1538
0
    return ctx->libctx;
1539
0
}
1540
1541
const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx)
1542
0
{
1543
0
    return ctx->propquery;
1544
0
}
1545
1546
const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx)
1547
0
{
1548
0
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1549
0
        if (ctx->op.sig.signature != NULL)
1550
0
            return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature);
1551
0
    } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1552
0
        if (ctx->op.kex.exchange != NULL)
1553
0
            return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange);
1554
0
    } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1555
0
        if (ctx->op.encap.kem != NULL)
1556
0
            return EVP_KEM_get0_provider(ctx->op.encap.kem);
1557
0
    } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1558
0
        if (ctx->op.ciph.cipher != NULL)
1559
0
            return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher);
1560
0
    } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1561
0
        if (ctx->keymgmt != NULL)
1562
0
            return EVP_KEYMGMT_get0_provider(ctx->keymgmt);
1563
0
    }
1564
1565
0
    return NULL;
1566
0
}
1567
1568
/* Utility functions to send a string of hex string to a ctrl */
1569
1570
int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1571
0
{
1572
0
    size_t len;
1573
1574
0
    len = strlen(str);
1575
0
    if (len > INT_MAX)
1576
0
        return -1;
1577
0
    return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1578
0
}
1579
1580
int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1581
0
{
1582
0
    unsigned char *bin;
1583
0
    long binlen;
1584
0
    int rv = -1;
1585
1586
0
    bin = OPENSSL_hexstr2buf(hex, &binlen);
1587
0
    if (bin == NULL)
1588
0
        return 0;
1589
0
    if (binlen <= INT_MAX)
1590
0
        rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1591
0
    OPENSSL_free(bin);
1592
0
    return rv;
1593
0
}
1594
1595
/* Pass a message digest to a ctrl */
1596
int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1597
0
{
1598
0
    const EVP_MD *m;
1599
1600
0
    if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1601
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
1602
0
        return 0;
1603
0
    }
1604
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1605
0
}
1606
1607
int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1608
0
{
1609
0
    return ctx->operation;
1610
0
}
1611
1612
void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1613
0
{
1614
0
    ctx->keygen_info = dat;
1615
0
    ctx->keygen_info_count = datlen;
1616
0
}
1617
1618
void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1619
0
{
1620
0
    ctx->data = data;
1621
0
}
1622
1623
void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1624
0
{
1625
0
    return ctx->data;
1626
0
}
1627
1628
EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1629
139k
{
1630
139k
    return ctx->pkey;
1631
139k
}
1632
1633
EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1634
0
{
1635
0
    return ctx->peerkey;
1636
0
}
1637
1638
void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1639
0
{
1640
0
    ctx->app_data = data;
1641
0
}
1642
1643
void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1644
0
{
1645
0
    return ctx->app_data;
1646
0
}
1647
1648
void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
1649
    int (*init)(EVP_PKEY_CTX *ctx))
1650
0
{
1651
0
    pmeth->init = init;
1652
0
}
1653
1654
void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
1655
    int (*copy)(EVP_PKEY_CTX *dst,
1656
        const EVP_PKEY_CTX *src))
1657
0
{
1658
0
    pmeth->copy = copy;
1659
0
}
1660
1661
void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1662
    void (*cleanup)(EVP_PKEY_CTX *ctx))
1663
0
{
1664
0
    pmeth->cleanup = cleanup;
1665
0
}
1666
1667
void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1668
    int (*paramgen_init)(EVP_PKEY_CTX *ctx),
1669
    int (*paramgen)(EVP_PKEY_CTX *ctx,
1670
        EVP_PKEY *pkey))
1671
0
{
1672
0
    pmeth->paramgen_init = paramgen_init;
1673
0
    pmeth->paramgen = paramgen;
1674
0
}
1675
1676
void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1677
    int (*keygen_init)(EVP_PKEY_CTX *ctx),
1678
    int (*keygen)(EVP_PKEY_CTX *ctx,
1679
        EVP_PKEY *pkey))
1680
0
{
1681
0
    pmeth->keygen_init = keygen_init;
1682
0
    pmeth->keygen = keygen;
1683
0
}
1684
1685
void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1686
    int (*sign_init)(EVP_PKEY_CTX *ctx),
1687
    int (*sign)(EVP_PKEY_CTX *ctx,
1688
        unsigned char *sig, size_t *siglen,
1689
        const unsigned char *tbs,
1690
        size_t tbslen))
1691
0
{
1692
0
    pmeth->sign_init = sign_init;
1693
0
    pmeth->sign = sign;
1694
0
}
1695
1696
void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1697
    int (*verify_init)(EVP_PKEY_CTX *ctx),
1698
    int (*verify)(EVP_PKEY_CTX *ctx,
1699
        const unsigned char *sig,
1700
        size_t siglen,
1701
        const unsigned char *tbs,
1702
        size_t tbslen))
1703
0
{
1704
0
    pmeth->verify_init = verify_init;
1705
0
    pmeth->verify = verify;
1706
0
}
1707
1708
void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1709
    int (*verify_recover_init)(EVP_PKEY_CTX
1710
            *ctx),
1711
    int (*verify_recover)(EVP_PKEY_CTX
1712
                              *ctx,
1713
        unsigned char
1714
            *sig,
1715
        size_t *siglen,
1716
        const unsigned char *tbs,
1717
        size_t tbslen))
1718
0
{
1719
0
    pmeth->verify_recover_init = verify_recover_init;
1720
0
    pmeth->verify_recover = verify_recover;
1721
0
}
1722
1723
void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1724
    int (*signctx_init)(EVP_PKEY_CTX *ctx,
1725
        EVP_MD_CTX *mctx),
1726
    int (*signctx)(EVP_PKEY_CTX *ctx,
1727
        unsigned char *sig,
1728
        size_t *siglen,
1729
        EVP_MD_CTX *mctx))
1730
0
{
1731
0
    pmeth->signctx_init = signctx_init;
1732
0
    pmeth->signctx = signctx;
1733
0
}
1734
1735
void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1736
    int (*verifyctx_init)(EVP_PKEY_CTX *ctx,
1737
        EVP_MD_CTX *mctx),
1738
    int (*verifyctx)(EVP_PKEY_CTX *ctx,
1739
        const unsigned char *sig,
1740
        int siglen,
1741
        EVP_MD_CTX *mctx))
1742
0
{
1743
0
    pmeth->verifyctx_init = verifyctx_init;
1744
0
    pmeth->verifyctx = verifyctx;
1745
0
}
1746
1747
void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1748
    int (*encrypt_init)(EVP_PKEY_CTX *ctx),
1749
    int (*encryptfn)(EVP_PKEY_CTX *ctx,
1750
        unsigned char *out,
1751
        size_t *outlen,
1752
        const unsigned char *in,
1753
        size_t inlen))
1754
0
{
1755
0
    pmeth->encrypt_init = encrypt_init;
1756
0
    pmeth->encrypt = encryptfn;
1757
0
}
1758
1759
void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1760
    int (*decrypt_init)(EVP_PKEY_CTX *ctx),
1761
    int (*decrypt)(EVP_PKEY_CTX *ctx,
1762
        unsigned char *out,
1763
        size_t *outlen,
1764
        const unsigned char *in,
1765
        size_t inlen))
1766
0
{
1767
0
    pmeth->decrypt_init = decrypt_init;
1768
0
    pmeth->decrypt = decrypt;
1769
0
}
1770
1771
void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1772
    int (*derive_init)(EVP_PKEY_CTX *ctx),
1773
    int (*derive)(EVP_PKEY_CTX *ctx,
1774
        unsigned char *key,
1775
        size_t *keylen))
1776
0
{
1777
0
    pmeth->derive_init = derive_init;
1778
0
    pmeth->derive = derive;
1779
0
}
1780
1781
void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1782
    int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1,
1783
        void *p2),
1784
    int (*ctrl_str)(EVP_PKEY_CTX *ctx,
1785
        const char *type,
1786
        const char *value))
1787
0
{
1788
0
    pmeth->ctrl = ctrl;
1789
0
    pmeth->ctrl_str = ctrl_str;
1790
0
}
1791
1792
void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1793
    int (*digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1794
        const unsigned char *tbs, size_t tbslen))
1795
0
{
1796
0
    pmeth->digestsign = digestsign;
1797
0
}
1798
1799
void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1800
    int (*digestverify)(EVP_MD_CTX *ctx, const unsigned char *sig,
1801
        size_t siglen, const unsigned char *tbs,
1802
        size_t tbslen))
1803
0
{
1804
0
    pmeth->digestverify = digestverify;
1805
0
}
1806
1807
void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1808
    int (*check)(EVP_PKEY *pkey))
1809
0
{
1810
0
    pmeth->check = check;
1811
0
}
1812
1813
void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1814
    int (*check)(EVP_PKEY *pkey))
1815
0
{
1816
0
    pmeth->public_check = check;
1817
0
}
1818
1819
void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1820
    int (*check)(EVP_PKEY *pkey))
1821
0
{
1822
0
    pmeth->param_check = check;
1823
0
}
1824
1825
void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1826
    int (*digest_custom)(EVP_PKEY_CTX *ctx,
1827
        EVP_MD_CTX *mctx))
1828
0
{
1829
0
    pmeth->digest_custom = digest_custom;
1830
0
}
1831
1832
void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1833
    int (**pinit)(EVP_PKEY_CTX *ctx))
1834
0
{
1835
0
    *pinit = pmeth->init;
1836
0
}
1837
1838
void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1839
    int (**pcopy)(EVP_PKEY_CTX *dst,
1840
        const EVP_PKEY_CTX *src))
1841
0
{
1842
0
    *pcopy = pmeth->copy;
1843
0
}
1844
1845
void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1846
    void (**pcleanup)(EVP_PKEY_CTX *ctx))
1847
0
{
1848
0
    *pcleanup = pmeth->cleanup;
1849
0
}
1850
1851
void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1852
    int (**pparamgen_init)(EVP_PKEY_CTX *ctx),
1853
    int (**pparamgen)(EVP_PKEY_CTX *ctx,
1854
        EVP_PKEY *pkey))
1855
0
{
1856
0
    if (pparamgen_init)
1857
0
        *pparamgen_init = pmeth->paramgen_init;
1858
0
    if (pparamgen)
1859
0
        *pparamgen = pmeth->paramgen;
1860
0
}
1861
1862
void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1863
    int (**pkeygen_init)(EVP_PKEY_CTX *ctx),
1864
    int (**pkeygen)(EVP_PKEY_CTX *ctx,
1865
        EVP_PKEY *pkey))
1866
0
{
1867
0
    if (pkeygen_init)
1868
0
        *pkeygen_init = pmeth->keygen_init;
1869
0
    if (pkeygen)
1870
0
        *pkeygen = pmeth->keygen;
1871
0
}
1872
1873
void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1874
    int (**psign_init)(EVP_PKEY_CTX *ctx),
1875
    int (**psign)(EVP_PKEY_CTX *ctx,
1876
        unsigned char *sig, size_t *siglen,
1877
        const unsigned char *tbs,
1878
        size_t tbslen))
1879
0
{
1880
0
    if (psign_init)
1881
0
        *psign_init = pmeth->sign_init;
1882
0
    if (psign)
1883
0
        *psign = pmeth->sign;
1884
0
}
1885
1886
void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1887
    int (**pverify_init)(EVP_PKEY_CTX *ctx),
1888
    int (**pverify)(EVP_PKEY_CTX *ctx,
1889
        const unsigned char *sig,
1890
        size_t siglen,
1891
        const unsigned char *tbs,
1892
        size_t tbslen))
1893
0
{
1894
0
    if (pverify_init)
1895
0
        *pverify_init = pmeth->verify_init;
1896
0
    if (pverify)
1897
0
        *pverify = pmeth->verify;
1898
0
}
1899
1900
void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1901
    int (**pverify_recover_init)(EVP_PKEY_CTX
1902
            *ctx),
1903
    int (**pverify_recover)(EVP_PKEY_CTX
1904
                                *ctx,
1905
        unsigned char
1906
            *sig,
1907
        size_t *siglen,
1908
        const unsigned char *tbs,
1909
        size_t tbslen))
1910
0
{
1911
0
    if (pverify_recover_init)
1912
0
        *pverify_recover_init = pmeth->verify_recover_init;
1913
0
    if (pverify_recover)
1914
0
        *pverify_recover = pmeth->verify_recover;
1915
0
}
1916
1917
void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1918
    int (**psignctx_init)(EVP_PKEY_CTX *ctx,
1919
        EVP_MD_CTX *mctx),
1920
    int (**psignctx)(EVP_PKEY_CTX *ctx,
1921
        unsigned char *sig,
1922
        size_t *siglen,
1923
        EVP_MD_CTX *mctx))
1924
0
{
1925
0
    if (psignctx_init)
1926
0
        *psignctx_init = pmeth->signctx_init;
1927
0
    if (psignctx)
1928
0
        *psignctx = pmeth->signctx;
1929
0
}
1930
1931
void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1932
    int (**pverifyctx_init)(EVP_PKEY_CTX *ctx,
1933
        EVP_MD_CTX *mctx),
1934
    int (**pverifyctx)(EVP_PKEY_CTX *ctx,
1935
        const unsigned char *sig,
1936
        int siglen,
1937
        EVP_MD_CTX *mctx))
1938
0
{
1939
0
    if (pverifyctx_init)
1940
0
        *pverifyctx_init = pmeth->verifyctx_init;
1941
0
    if (pverifyctx)
1942
0
        *pverifyctx = pmeth->verifyctx;
1943
0
}
1944
1945
void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1946
    int (**pencrypt_init)(EVP_PKEY_CTX *ctx),
1947
    int (**pencryptfn)(EVP_PKEY_CTX *ctx,
1948
        unsigned char *out,
1949
        size_t *outlen,
1950
        const unsigned char *in,
1951
        size_t inlen))
1952
0
{
1953
0
    if (pencrypt_init)
1954
0
        *pencrypt_init = pmeth->encrypt_init;
1955
0
    if (pencryptfn)
1956
0
        *pencryptfn = pmeth->encrypt;
1957
0
}
1958
1959
void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1960
    int (**pdecrypt_init)(EVP_PKEY_CTX *ctx),
1961
    int (**pdecrypt)(EVP_PKEY_CTX *ctx,
1962
        unsigned char *out,
1963
        size_t *outlen,
1964
        const unsigned char *in,
1965
        size_t inlen))
1966
0
{
1967
0
    if (pdecrypt_init)
1968
0
        *pdecrypt_init = pmeth->decrypt_init;
1969
0
    if (pdecrypt)
1970
0
        *pdecrypt = pmeth->decrypt;
1971
0
}
1972
1973
void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1974
    int (**pderive_init)(EVP_PKEY_CTX *ctx),
1975
    int (**pderive)(EVP_PKEY_CTX *ctx,
1976
        unsigned char *key,
1977
        size_t *keylen))
1978
0
{
1979
0
    if (pderive_init)
1980
0
        *pderive_init = pmeth->derive_init;
1981
0
    if (pderive)
1982
0
        *pderive = pmeth->derive;
1983
0
}
1984
1985
void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1986
    int (**pctrl)(EVP_PKEY_CTX *ctx, int type, int p1,
1987
        void *p2),
1988
    int (**pctrl_str)(EVP_PKEY_CTX *ctx,
1989
        const char *type,
1990
        const char *value))
1991
0
{
1992
0
    if (pctrl)
1993
0
        *pctrl = pmeth->ctrl;
1994
0
    if (pctrl_str)
1995
0
        *pctrl_str = pmeth->ctrl_str;
1996
0
}
1997
1998
void EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD *pmeth,
1999
    int (**digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
2000
        const unsigned char *tbs, size_t tbslen))
2001
0
{
2002
0
    if (digestsign)
2003
0
        *digestsign = pmeth->digestsign;
2004
0
}
2005
2006
void EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD *pmeth,
2007
    int (**digestverify)(EVP_MD_CTX *ctx, const unsigned char *sig,
2008
        size_t siglen, const unsigned char *tbs,
2009
        size_t tbslen))
2010
0
{
2011
0
    if (digestverify)
2012
0
        *digestverify = pmeth->digestverify;
2013
0
}
2014
2015
void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2016
    int (**pcheck)(EVP_PKEY *pkey))
2017
0
{
2018
0
    if (pcheck != NULL)
2019
0
        *pcheck = pmeth->check;
2020
0
}
2021
2022
void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
2023
    int (**pcheck)(EVP_PKEY *pkey))
2024
0
{
2025
0
    if (pcheck != NULL)
2026
0
        *pcheck = pmeth->public_check;
2027
0
}
2028
2029
void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
2030
    int (**pcheck)(EVP_PKEY *pkey))
2031
0
{
2032
0
    if (pcheck != NULL)
2033
0
        *pcheck = pmeth->param_check;
2034
0
}
2035
2036
void EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD *pmeth,
2037
    int (**pdigest_custom)(EVP_PKEY_CTX *ctx,
2038
        EVP_MD_CTX *mctx))
2039
0
{
2040
0
    if (pdigest_custom != NULL)
2041
0
        *pdigest_custom = pmeth->digest_custom;
2042
0
}
2043
2044
#endif /* FIPS_MODULE */