Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
474k
{
140
474k
    if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
141
0
        return EVP_PKEY_STATE_UNKNOWN;
142
143
474k
    if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
144
146
            && ctx->op.kex.algctx != NULL)
145
473k
        || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
146
217k
            && ctx->op.sig.algctx != NULL)
147
256k
        || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
148
26.1k
            && ctx->op.ciph.algctx != NULL)
149
230k
        || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
150
230k
            && ctx->op.keymgmt.genctx != NULL)
151
0
        || (EVP_PKEY_CTX_IS_KEM_OP(ctx)
152
0
            && ctx->op.encap.algctx != NULL))
153
474k
        return EVP_PKEY_STATE_PROVIDER;
154
155
0
    return EVP_PKEY_STATE_LEGACY;
156
474k
}
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
4.86M
{
356
4.86M
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
357
893k
        if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL)
358
893k
            ctx->op.sig.signature->freectx(ctx->op.sig.algctx);
359
893k
        EVP_SIGNATURE_free(ctx->op.sig.signature);
360
893k
        ctx->op.sig.algctx = NULL;
361
893k
        ctx->op.sig.signature = NULL;
362
3.97M
    } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
363
30.9k
        if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL)
364
30.8k
            ctx->op.kex.exchange->freectx(ctx->op.kex.algctx);
365
30.9k
        EVP_KEYEXCH_free(ctx->op.kex.exchange);
366
30.9k
        ctx->op.kex.algctx = NULL;
367
30.9k
        ctx->op.kex.exchange = NULL;
368
3.94M
    } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
369
256
        if (ctx->op.encap.algctx != NULL && ctx->op.encap.kem != NULL)
370
256
            ctx->op.encap.kem->freectx(ctx->op.encap.algctx);
371
256
        EVP_KEM_free(ctx->op.encap.kem);
372
256
        ctx->op.encap.algctx = NULL;
373
256
        ctx->op.encap.kem = NULL;
374
3.94M
    } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
375
12.3k
        if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL)
376
12.3k
            ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx);
377
12.3k
        EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
378
12.3k
        ctx->op.ciph.algctx = NULL;
379
12.3k
        ctx->op.ciph.cipher = NULL;
380
3.92M
    } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
381
241k
        if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
382
241k
            evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
383
241k
    }
384
4.86M
}
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
165k
{
450
165k
    EVP_PKEY_CTX *rctx;
451
452
165k
#ifndef OPENSSL_NO_ENGINE
453
    /* Make sure it's safe to copy a pkey context using an ENGINE */
454
165k
    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
165k
#endif
459
165k
    rctx = OPENSSL_zalloc(sizeof(*rctx));
460
165k
    if (rctx == NULL)
461
0
        return NULL;
462
463
165k
    if (pctx->pkey != NULL)
464
165k
        EVP_PKEY_up_ref(pctx->pkey);
465
165k
    rctx->pkey = pctx->pkey;
466
165k
    rctx->operation = pctx->operation;
467
165k
    rctx->libctx = pctx->libctx;
468
165k
    rctx->keytype = pctx->keytype;
469
165k
    rctx->propquery = NULL;
470
165k
    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
165k
    rctx->legacy_keytype = pctx->legacy_keytype;
476
477
165k
    if (pctx->keymgmt != NULL) {
478
165k
        if (!EVP_KEYMGMT_up_ref(pctx->keymgmt))
479
0
            goto err;
480
165k
        rctx->keymgmt = pctx->keymgmt;
481
165k
    }
482
483
165k
    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
165k
    } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
505
165k
        if (pctx->op.sig.signature != NULL) {
506
165k
            rctx->op.sig.signature = pctx->op.sig.signature;
507
165k
            if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
508
0
                goto err;
509
165k
        }
510
165k
        if (pctx->op.sig.algctx != NULL) {
511
165k
            if (!ossl_assert(pctx->op.sig.signature != NULL))
512
0
                goto err;
513
514
165k
            if (pctx->op.sig.signature->dupctx != NULL)
515
165k
                rctx->op.sig.algctx
516
165k
                    = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx);
517
518
165k
            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
165k
            return rctx;
524
165k
        }
525
165k
    } 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
426k
{
673
426k
    switch (evp_pkey_ctx_state(ctx)) {
674
426k
    case EVP_PKEY_STATE_PROVIDER:
675
426k
        if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
676
146
            && ctx->op.kex.exchange != NULL
677
146
            && ctx->op.kex.exchange->set_ctx_params != NULL)
678
146
            return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx,
679
146
                params);
680
426k
        if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
681
178k
            && ctx->op.sig.signature != NULL
682
178k
            && ctx->op.sig.signature->set_ctx_params != NULL)
683
178k
            return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx,
684
178k
                params);
685
247k
        if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
686
17.4k
            && ctx->op.ciph.cipher != NULL
687
17.4k
            && ctx->op.ciph.cipher->set_ctx_params != NULL)
688
17.4k
            return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx,
689
17.4k
                params);
690
230k
        if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
691
230k
            && ctx->keymgmt != NULL
692
230k
            && ctx->keymgmt->gen_set_params != NULL)
693
230k
            return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
694
230k
                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
426k
#endif
707
426k
    }
708
0
    return 0;
709
426k
}
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
        if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
736
0
            && ctx->keymgmt != NULL
737
0
            && ctx->keymgmt->gen_get_params != NULL)
738
0
            return evp_keymgmt_gen_get_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
739
0
                params);
740
0
        break;
741
0
    case EVP_PKEY_STATE_UNKNOWN:
742
0
        break;
743
0
#ifndef FIPS_MODULE
744
0
    case EVP_PKEY_STATE_LEGACY:
745
0
        return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
746
0
#endif
747
0
    }
748
0
    return 0;
749
0
}
750
751
#ifndef FIPS_MODULE
752
const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx)
753
0
{
754
0
    void *provctx;
755
756
0
    if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
757
0
        && ctx->op.kex.exchange != NULL
758
0
        && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
759
0
        provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
760
0
        return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx,
761
0
            provctx);
762
0
    }
763
0
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
764
0
        && ctx->op.sig.signature != NULL
765
0
        && ctx->op.sig.signature->gettable_ctx_params != NULL) {
766
0
        provctx = ossl_provider_ctx(
767
0
            EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
768
0
        return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx,
769
0
            provctx);
770
0
    }
771
0
    if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
772
0
        && ctx->op.ciph.cipher != NULL
773
0
        && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
774
0
        provctx = ossl_provider_ctx(
775
0
            EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
776
0
        return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx,
777
0
            provctx);
778
0
    }
779
0
    if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
780
0
        && ctx->op.encap.kem != NULL
781
0
        && ctx->op.encap.kem->gettable_ctx_params != NULL) {
782
0
        provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
783
0
        return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx,
784
0
            provctx);
785
0
    }
786
0
    if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
787
0
        && ctx->keymgmt != NULL
788
0
        && ctx->keymgmt->gen_gettable_params != NULL) {
789
0
        provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
790
0
        return ctx->keymgmt->gen_gettable_params(ctx->op.keymgmt.genctx,
791
0
            provctx);
792
0
    }
793
0
    return NULL;
794
0
}
795
796
const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx)
797
47.6k
{
798
47.6k
    void *provctx;
799
800
47.6k
    if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
801
146
        && ctx->op.kex.exchange != NULL
802
146
        && ctx->op.kex.exchange->settable_ctx_params != NULL) {
803
146
        provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
804
146
        return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx,
805
146
            provctx);
806
146
    }
807
47.4k
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
808
38.7k
        && ctx->op.sig.signature != NULL
809
38.7k
        && ctx->op.sig.signature->settable_ctx_params != NULL) {
810
38.7k
        provctx = ossl_provider_ctx(
811
38.7k
            EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
812
38.7k
        return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx,
813
38.7k
            provctx);
814
38.7k
    }
815
8.70k
    if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
816
8.70k
        && ctx->op.ciph.cipher != NULL
817
8.70k
        && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
818
8.70k
        provctx = ossl_provider_ctx(
819
8.70k
            EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
820
8.70k
        return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx,
821
8.70k
            provctx);
822
8.70k
    }
823
0
    if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
824
0
        && ctx->keymgmt != NULL
825
0
        && ctx->keymgmt->gen_settable_params != NULL) {
826
0
        provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
827
0
        return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx,
828
0
            provctx);
829
0
    }
830
0
    if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
831
0
        && ctx->op.encap.kem != NULL
832
0
        && ctx->op.encap.kem->settable_ctx_params != NULL) {
833
0
        provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
834
0
        return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx,
835
0
            provctx);
836
0
    }
837
0
    return NULL;
838
0
}
839
840
/*
841
 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
842
 *
843
 * Return 1 on success, 0 or negative for errors.
844
 *
845
 * In particular they return -2 if any of the params is not supported.
846
 *
847
 * They are not available in FIPS_MODULE as they depend on
848
 *      - EVP_PKEY_CTX_{get,set}_params()
849
 *      - EVP_PKEY_CTX_{gettable,settable}_params()
850
 *
851
 */
852
int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
853
62.9k
{
854
62.9k
    if (ctx == NULL || params == NULL)
855
0
        return 0;
856
857
    /*
858
     * We only check for provider side EVP_PKEY_CTX.  For #legacy, we
859
     * depend on the translation that happens in EVP_PKEY_CTX_set_params()
860
     * call, and that the resulting ctrl call will return -2 if it doesn't
861
     * known the ctrl command number.
862
     */
863
62.9k
    if (evp_pkey_ctx_is_provided(ctx)) {
864
62.9k
        const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
865
62.9k
        const OSSL_PARAM *p;
866
867
125k
        for (p = params; p->key != NULL; p++) {
868
            /* Check the ctx actually understands this parameter */
869
62.9k
            if (OSSL_PARAM_locate_const(settable, p->key) == NULL)
870
0
                return -2;
871
62.9k
        }
872
62.9k
    }
873
874
62.9k
    return EVP_PKEY_CTX_set_params(ctx, params);
875
62.9k
}
876
877
int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
878
0
{
879
0
    if (ctx == NULL || params == NULL)
880
0
        return 0;
881
882
    /*
883
     * We only check for provider side EVP_PKEY_CTX.  For #legacy, we
884
     * depend on the translation that happens in EVP_PKEY_CTX_get_params()
885
     * call, and that the resulting ctrl call will return -2 if it doesn't
886
     * known the ctrl command number.
887
     */
888
0
    if (evp_pkey_ctx_is_provided(ctx)) {
889
0
        const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
890
0
        const OSSL_PARAM *p;
891
892
0
        for (p = params; p->key != NULL; p++) {
893
            /* Check the ctx actually understands this parameter */
894
0
            if (OSSL_PARAM_locate_const(gettable, p->key) == NULL)
895
0
                return -2;
896
0
        }
897
0
    }
898
899
0
    return EVP_PKEY_CTX_get_params(ctx, params);
900
0
}
901
902
int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
903
0
{
904
0
    OSSL_PARAM sig_md_params[2], *p = sig_md_params;
905
    /* 80 should be big enough */
906
0
    char name[80] = "";
907
0
    const EVP_MD *tmp;
908
909
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
910
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
911
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
912
0
        return -2;
913
0
    }
914
915
0
    if (ctx->op.sig.algctx == NULL)
916
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
917
0
            EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
918
919
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
920
0
        name,
921
0
        sizeof(name));
922
0
    *p = OSSL_PARAM_construct_end();
923
924
0
    if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
925
0
        return 0;
926
927
0
    tmp = evp_get_digestbyname_ex(ctx->libctx, name);
928
0
    if (tmp == NULL)
929
0
        return 0;
930
931
0
    *md = tmp;
932
933
0
    return 1;
934
0
}
935
936
static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
937
    int fallback, const char *param, int op,
938
    int ctrl)
939
0
{
940
0
    OSSL_PARAM md_params[2], *p = md_params;
941
0
    const char *name;
942
943
0
    if (ctx == NULL || (ctx->operation & op) == 0) {
944
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
945
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
946
0
        return -2;
947
0
    }
948
949
0
    if (fallback)
950
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
951
952
0
    if (md == NULL) {
953
0
        name = "";
954
0
    } else {
955
0
        name = EVP_MD_get0_name(md);
956
0
    }
957
958
0
    *p++ = OSSL_PARAM_construct_utf8_string(param,
959
        /*
960
         * Cast away the const. This is read
961
         * only so should be safe
962
         */
963
0
        (char *)name, 0);
964
0
    *p = OSSL_PARAM_construct_end();
965
966
0
    return EVP_PKEY_CTX_set_params(ctx, md_params);
967
0
}
968
969
int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
970
0
{
971
0
    return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL,
972
0
        OSSL_SIGNATURE_PARAM_DIGEST,
973
0
        EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
974
0
}
975
976
int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
977
0
{
978
0
    return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
979
0
        OSSL_KDF_PARAM_DIGEST,
980
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
981
0
}
982
983
static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
984
    const char *param, int op, int ctrl,
985
    const unsigned char *data,
986
    int datalen)
987
0
{
988
0
    OSSL_PARAM octet_string_params[2], *p = octet_string_params;
989
990
0
    if (ctx == NULL || (ctx->operation & op) == 0) {
991
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
992
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
993
0
        return -2;
994
0
    }
995
996
    /* Code below to be removed when legacy support is dropped. */
997
0
    if (fallback)
998
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
999
    /* end of legacy support */
1000
1001
0
    if (datalen < 0) {
1002
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
1003
0
        return 0;
1004
0
    }
1005
1006
0
    *p++ = OSSL_PARAM_construct_octet_string(param,
1007
        /*
1008
         * Cast away the const. This is read
1009
         * only so should be safe
1010
         */
1011
0
        (unsigned char *)data,
1012
0
        (size_t)datalen);
1013
0
    *p = OSSL_PARAM_construct_end();
1014
1015
0
    return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
1016
0
}
1017
1018
static int evp_pkey_ctx_add1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
1019
    const char *param, int op, int ctrl,
1020
    const unsigned char *data,
1021
    int datalen)
1022
0
{
1023
0
    OSSL_PARAM os_params[2];
1024
0
    const OSSL_PARAM *gettables;
1025
0
    unsigned char *info = NULL;
1026
0
    size_t info_len = 0;
1027
0
    size_t info_alloc = 0;
1028
0
    int ret = 0;
1029
1030
0
    if (ctx == NULL || (ctx->operation & op) == 0) {
1031
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1032
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1033
0
        return -2;
1034
0
    }
1035
1036
    /* Code below to be removed when legacy support is dropped. */
1037
0
    if (fallback)
1038
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
1039
    /* end of legacy support */
1040
1041
0
    if (datalen < 0) {
1042
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
1043
0
        return 0;
1044
0
    } else if (datalen == 0) {
1045
0
        return 1;
1046
0
    }
1047
1048
    /* Check for older provider that doesn't support getting this parameter */
1049
0
    gettables = EVP_PKEY_CTX_gettable_params(ctx);
1050
0
    if (gettables == NULL || OSSL_PARAM_locate_const(gettables, param) == NULL)
1051
0
        return evp_pkey_ctx_set1_octet_string(ctx, fallback, param, op, ctrl,
1052
0
            data, datalen);
1053
1054
    /* Get the original value length */
1055
0
    os_params[0] = OSSL_PARAM_construct_octet_string(param, NULL, 0);
1056
0
    os_params[1] = OSSL_PARAM_construct_end();
1057
1058
0
    if (!EVP_PKEY_CTX_get_params(ctx, os_params))
1059
0
        return 0;
1060
1061
    /* This should not happen but check to be sure. */
1062
0
    if (os_params[0].return_size == OSSL_PARAM_UNMODIFIED)
1063
0
        return 0;
1064
1065
0
    info_alloc = os_params[0].return_size + datalen;
1066
0
    if (info_alloc == 0)
1067
0
        return 0;
1068
0
    info = OPENSSL_zalloc(info_alloc);
1069
0
    if (info == NULL)
1070
0
        return 0;
1071
0
    info_len = os_params[0].return_size;
1072
1073
0
    os_params[0] = OSSL_PARAM_construct_octet_string(param, info, info_alloc);
1074
1075
    /* if we have data, then go get it */
1076
0
    if (info_len > 0) {
1077
0
        if (!EVP_PKEY_CTX_get_params(ctx, os_params))
1078
0
            goto error;
1079
0
    }
1080
1081
    /* Copy the input data */
1082
0
    memcpy(&info[info_len], data, datalen);
1083
0
    ret = EVP_PKEY_CTX_set_params(ctx, os_params);
1084
1085
0
error:
1086
0
    OPENSSL_clear_free(info, info_alloc);
1087
0
    return ret;
1088
0
}
1089
1090
int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
1091
    const unsigned char *sec, int seclen)
1092
0
{
1093
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1094
0
        OSSL_KDF_PARAM_SECRET,
1095
0
        EVP_PKEY_OP_DERIVE,
1096
0
        EVP_PKEY_CTRL_TLS_SECRET,
1097
0
        sec, seclen);
1098
0
}
1099
1100
int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
1101
    const unsigned char *seed, int seedlen)
1102
0
{
1103
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1104
0
        OSSL_KDF_PARAM_SEED,
1105
0
        EVP_PKEY_OP_DERIVE,
1106
0
        EVP_PKEY_CTRL_TLS_SEED,
1107
0
        seed, seedlen);
1108
0
}
1109
1110
int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1111
0
{
1112
0
    return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
1113
0
        OSSL_KDF_PARAM_DIGEST,
1114
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
1115
0
}
1116
1117
int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
1118
    const unsigned char *salt, int saltlen)
1119
0
{
1120
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1121
0
        OSSL_KDF_PARAM_SALT,
1122
0
        EVP_PKEY_OP_DERIVE,
1123
0
        EVP_PKEY_CTRL_HKDF_SALT,
1124
0
        salt, saltlen);
1125
0
}
1126
1127
int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
1128
    const unsigned char *key, int keylen)
1129
0
{
1130
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1131
0
        OSSL_KDF_PARAM_KEY,
1132
0
        EVP_PKEY_OP_DERIVE,
1133
0
        EVP_PKEY_CTRL_HKDF_KEY,
1134
0
        key, keylen);
1135
0
}
1136
1137
int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
1138
    const unsigned char *info, int infolen)
1139
0
{
1140
0
    return evp_pkey_ctx_add1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1141
0
        OSSL_KDF_PARAM_INFO,
1142
0
        EVP_PKEY_OP_DERIVE,
1143
0
        EVP_PKEY_CTRL_HKDF_INFO,
1144
0
        info, infolen);
1145
0
}
1146
1147
int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
1148
0
{
1149
0
    OSSL_PARAM int_params[2], *p = int_params;
1150
1151
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1152
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1153
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1154
0
        return -2;
1155
0
    }
1156
1157
    /* Code below to be removed when legacy support is dropped. */
1158
0
    if (ctx->op.kex.algctx == NULL)
1159
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
1160
0
            EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
1161
    /* end of legacy support */
1162
1163
0
    if (mode < 0) {
1164
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1165
0
        return 0;
1166
0
    }
1167
1168
0
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
1169
0
    *p = OSSL_PARAM_construct_end();
1170
1171
0
    return EVP_PKEY_CTX_set_params(ctx, int_params);
1172
0
}
1173
1174
int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
1175
    int passlen)
1176
0
{
1177
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1178
0
        OSSL_KDF_PARAM_PASSWORD,
1179
0
        EVP_PKEY_OP_DERIVE,
1180
0
        EVP_PKEY_CTRL_PASS,
1181
0
        (const unsigned char *)pass, passlen);
1182
0
}
1183
1184
int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
1185
    const unsigned char *salt, int saltlen)
1186
0
{
1187
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1188
0
        OSSL_KDF_PARAM_SALT,
1189
0
        EVP_PKEY_OP_DERIVE,
1190
0
        EVP_PKEY_CTRL_SCRYPT_SALT,
1191
0
        salt, saltlen);
1192
0
}
1193
1194
static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
1195
    int op, int ctrl, uint64_t val)
1196
0
{
1197
0
    OSSL_PARAM uint64_params[2], *p = uint64_params;
1198
1199
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1200
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1201
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1202
0
        return -2;
1203
0
    }
1204
1205
    /* Code below to be removed when legacy support is dropped. */
1206
0
    if (ctx->op.kex.algctx == NULL)
1207
0
        return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
1208
    /* end of legacy support */
1209
1210
0
    *p++ = OSSL_PARAM_construct_uint64(param, &val);
1211
0
    *p = OSSL_PARAM_construct_end();
1212
1213
0
    return EVP_PKEY_CTX_set_params(ctx, uint64_params);
1214
0
}
1215
1216
int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
1217
0
{
1218
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
1219
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
1220
0
        n);
1221
0
}
1222
1223
int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
1224
0
{
1225
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
1226
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
1227
0
        r);
1228
0
}
1229
1230
int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
1231
0
{
1232
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
1233
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
1234
0
        p);
1235
0
}
1236
1237
int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
1238
    uint64_t maxmem_bytes)
1239
0
{
1240
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
1241
0
        EVP_PKEY_OP_DERIVE,
1242
0
        EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
1243
0
        maxmem_bytes);
1244
0
}
1245
1246
int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1247
    int keylen)
1248
0
{
1249
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1250
0
        OSSL_PKEY_PARAM_PRIV_KEY,
1251
0
        EVP_PKEY_OP_KEYGEN,
1252
0
        EVP_PKEY_CTRL_SET_MAC_KEY,
1253
0
        key, keylen);
1254
0
}
1255
1256
int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
1257
0
{
1258
0
    OSSL_PARAM params[2], *p = params;
1259
1260
0
    if (ctx == NULL || op == NULL) {
1261
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1262
0
        return 0;
1263
0
    }
1264
0
    if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1265
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1266
0
        return -2;
1267
0
    }
1268
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
1269
0
        (char *)op, 0);
1270
0
    *p = OSSL_PARAM_construct_end();
1271
0
    return EVP_PKEY_CTX_set_params(ctx, params);
1272
0
}
1273
1274
int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
1275
0
{
1276
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1277
0
        EVP_PKEY_CTRL_SET1_ID, (int)len, (void *)(id));
1278
0
}
1279
1280
int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
1281
0
{
1282
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void *)id);
1283
0
}
1284
1285
int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
1286
0
{
1287
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1288
0
        EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void *)id_len);
1289
0
}
1290
1291
static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1292
    int cmd, int p1, void *p2)
1293
49.5k
{
1294
49.5k
    int ret = 0;
1295
1296
    /*
1297
     * If the method has a |digest_custom| function, we can relax the
1298
     * operation type check, since this can be called before the operation
1299
     * is initialized.
1300
     */
1301
49.5k
    if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
1302
49.5k
        if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
1303
0
            ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
1304
0
            return -1;
1305
0
        }
1306
1307
49.5k
        if ((optype != -1) && !(ctx->operation & optype)) {
1308
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1309
0
            return -1;
1310
0
        }
1311
49.5k
    }
1312
1313
49.5k
    switch (evp_pkey_ctx_state(ctx)) {
1314
49.5k
    case EVP_PKEY_STATE_PROVIDER:
1315
49.5k
        return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
1316
0
    case EVP_PKEY_STATE_UNKNOWN:
1317
0
    case EVP_PKEY_STATE_LEGACY:
1318
0
        if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
1319
0
            ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1320
0
            return -2;
1321
0
        }
1322
0
        if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1323
0
            return -1;
1324
1325
0
        ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
1326
1327
0
        if (ret == -2)
1328
0
            ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1329
0
        break;
1330
49.5k
    }
1331
0
    return ret;
1332
49.5k
}
1333
1334
int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1335
    int cmd, int p1, void *p2)
1336
62.7k
{
1337
62.7k
    int ret = 0;
1338
1339
62.7k
    if (ctx == NULL) {
1340
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1341
0
        return -2;
1342
0
    }
1343
    /* If unsupported, we don't want that reported here */
1344
62.7k
    ERR_set_mark();
1345
62.7k
    ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1346
62.7k
        cmd, NULL, p2, p1);
1347
62.7k
    if (ret == -2) {
1348
62.7k
        ERR_pop_to_mark();
1349
62.7k
    } else {
1350
0
        ERR_clear_last_mark();
1351
        /*
1352
         * If there was an error, there was an error.
1353
         * If the operation isn't initialized yet, we also return, as
1354
         * the saved values will be used then anyway.
1355
         */
1356
0
        if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1357
0
            return ret;
1358
0
    }
1359
62.7k
    return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
1360
62.7k
}
1361
1362
int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
1363
    int cmd, uint64_t value)
1364
0
{
1365
0
    return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1366
0
}
1367
1368
static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1369
    const char *name, const char *value)
1370
0
{
1371
0
    int ret = 0;
1372
1373
0
    if (ctx == NULL) {
1374
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1375
0
        return -2;
1376
0
    }
1377
1378
0
    switch (evp_pkey_ctx_state(ctx)) {
1379
0
    case EVP_PKEY_STATE_PROVIDER:
1380
0
        return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
1381
0
    case EVP_PKEY_STATE_UNKNOWN:
1382
0
    case EVP_PKEY_STATE_LEGACY:
1383
0
        if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
1384
0
            ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1385
0
            return -2;
1386
0
        }
1387
0
        if (strcmp(name, "digest") == 0)
1388
0
            ret = EVP_PKEY_CTX_md(ctx,
1389
0
                EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1390
0
                EVP_PKEY_CTRL_MD, value);
1391
0
        else
1392
0
            ret = ctx->pmeth->ctrl_str(ctx, name, value);
1393
0
        break;
1394
0
    }
1395
1396
0
    return ret;
1397
0
}
1398
1399
int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1400
    const char *name, const char *value)
1401
0
{
1402
0
    int ret = 0;
1403
1404
    /* If unsupported, we don't want that reported here */
1405
0
    ERR_set_mark();
1406
0
    ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1407
0
        name, value, strlen(value) + 1);
1408
0
    if (ret == -2) {
1409
0
        ERR_pop_to_mark();
1410
0
    } else {
1411
0
        ERR_clear_last_mark();
1412
        /*
1413
         * If there was an error, there was an error.
1414
         * If the operation isn't initialized yet, we also return, as
1415
         * the saved values will be used then anyway.
1416
         */
1417
0
        if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1418
0
            return ret;
1419
0
    }
1420
1421
0
    return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1422
0
}
1423
1424
static int decode_cmd(int cmd, const char *name)
1425
5.95M
{
1426
5.95M
    if (cmd == -1) {
1427
        /*
1428
         * The consequence of the assertion not being true is that this
1429
         * function will return -1, which will cause the calling functions
1430
         * to signal that the command is unsupported...  in non-debug mode.
1431
         */
1432
0
        if (ossl_assert(name != NULL))
1433
0
            if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1434
0
                cmd = EVP_PKEY_CTRL_SET1_ID;
1435
0
    }
1436
1437
5.95M
    return cmd;
1438
5.95M
}
1439
1440
static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1441
    int keytype, int optype,
1442
    int cmd, const char *name,
1443
    const void *data, size_t data_len)
1444
49.5k
{
1445
    /*
1446
     * Check that it's one of the supported commands.  The ctrl commands
1447
     * number cases here must correspond to the cases in the bottom switch
1448
     * in this function.
1449
     */
1450
49.5k
    switch (cmd = decode_cmd(cmd, name)) {
1451
0
    case EVP_PKEY_CTRL_SET1_ID:
1452
0
        break;
1453
49.5k
    default:
1454
49.5k
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1455
49.5k
        return -2;
1456
49.5k
    }
1457
1458
0
    if (keytype != -1) {
1459
0
        switch (evp_pkey_ctx_state(ctx)) {
1460
0
        case EVP_PKEY_STATE_PROVIDER:
1461
0
            if (ctx->keymgmt == NULL) {
1462
0
                ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1463
0
                return -2;
1464
0
            }
1465
0
            if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
1466
0
                    evp_pkey_type2name(keytype))) {
1467
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1468
0
                return -1;
1469
0
            }
1470
0
            break;
1471
0
        case EVP_PKEY_STATE_UNKNOWN:
1472
0
        case EVP_PKEY_STATE_LEGACY:
1473
0
            if (ctx->pmeth == NULL) {
1474
0
                ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1475
0
                return -2;
1476
0
            }
1477
0
            if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
1478
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1479
0
                return -1;
1480
0
            }
1481
0
            break;
1482
0
        }
1483
0
    }
1484
0
    if (optype != -1 && (ctx->operation & optype) == 0) {
1485
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1486
0
        return -1;
1487
0
    }
1488
1489
0
    switch (cmd) {
1490
0
    case EVP_PKEY_CTRL_SET1_ID:
1491
0
        evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1492
0
        if (name != NULL) {
1493
0
            ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
1494
0
            if (ctx->cached_parameters.dist_id_name == NULL)
1495
0
                return 0;
1496
0
        }
1497
0
        if (data_len > 0) {
1498
0
            ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
1499
0
            if (ctx->cached_parameters.dist_id == NULL)
1500
0
                return 0;
1501
0
        }
1502
0
        ctx->cached_parameters.dist_id_set = 1;
1503
0
        ctx->cached_parameters.dist_id_len = data_len;
1504
0
        break;
1505
0
    }
1506
0
    return 1;
1507
0
}
1508
1509
static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
1510
    int cmd, const char *name)
1511
5.88M
{
1512
5.88M
    cmd = decode_cmd(cmd, name);
1513
5.88M
    switch (cmd) {
1514
5.88M
    case EVP_PKEY_CTRL_SET1_ID:
1515
5.88M
        OPENSSL_free(ctx->cached_parameters.dist_id);
1516
5.88M
        OPENSSL_free(ctx->cached_parameters.dist_id_name);
1517
5.88M
        ctx->cached_parameters.dist_id = NULL;
1518
5.88M
        ctx->cached_parameters.dist_id_name = NULL;
1519
5.88M
        break;
1520
5.88M
    }
1521
5.88M
}
1522
1523
static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
1524
5.88M
{
1525
5.88M
    evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
1526
5.88M
}
1527
1528
int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
1529
117k
{
1530
117k
    int ret = 1;
1531
1532
117k
    if (ret && ctx->cached_parameters.dist_id_set) {
1533
0
        const char *name = ctx->cached_parameters.dist_id_name;
1534
0
        const void *val = ctx->cached_parameters.dist_id;
1535
0
        size_t len = ctx->cached_parameters.dist_id_len;
1536
1537
0
        if (name != NULL)
1538
0
            ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
1539
0
        else
1540
0
            ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
1541
0
                EVP_PKEY_CTRL_SET1_ID,
1542
0
                (int)len, (void *)val);
1543
0
    }
1544
1545
117k
    return ret;
1546
117k
}
1547
1548
OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
1549
0
{
1550
0
    return ctx->libctx;
1551
0
}
1552
1553
const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx)
1554
0
{
1555
0
    return ctx->propquery;
1556
0
}
1557
1558
const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx)
1559
0
{
1560
0
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1561
0
        if (ctx->op.sig.signature != NULL)
1562
0
            return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature);
1563
0
    } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1564
0
        if (ctx->op.kex.exchange != NULL)
1565
0
            return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange);
1566
0
    } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1567
0
        if (ctx->op.encap.kem != NULL)
1568
0
            return EVP_KEM_get0_provider(ctx->op.encap.kem);
1569
0
    } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1570
0
        if (ctx->op.ciph.cipher != NULL)
1571
0
            return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher);
1572
0
    } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1573
0
        if (ctx->keymgmt != NULL)
1574
0
            return EVP_KEYMGMT_get0_provider(ctx->keymgmt);
1575
0
    }
1576
1577
0
    return NULL;
1578
0
}
1579
1580
/* Utility functions to send a string of hex string to a ctrl */
1581
1582
int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1583
0
{
1584
0
    size_t len;
1585
1586
0
    len = strlen(str);
1587
0
    if (len > INT_MAX)
1588
0
        return -1;
1589
0
    return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1590
0
}
1591
1592
int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1593
0
{
1594
0
    unsigned char *bin;
1595
0
    long binlen;
1596
0
    int rv = -1;
1597
1598
0
    bin = OPENSSL_hexstr2buf(hex, &binlen);
1599
0
    if (bin == NULL)
1600
0
        return 0;
1601
0
    if (binlen <= INT_MAX)
1602
0
        rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1603
0
    OPENSSL_free(bin);
1604
0
    return rv;
1605
0
}
1606
1607
/* Pass a message digest to a ctrl */
1608
int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1609
0
{
1610
0
    const EVP_MD *m;
1611
1612
0
    if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1613
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
1614
0
        return 0;
1615
0
    }
1616
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1617
0
}
1618
1619
int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1620
0
{
1621
0
    return ctx->operation;
1622
0
}
1623
1624
void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1625
0
{
1626
0
    ctx->keygen_info = dat;
1627
0
    ctx->keygen_info_count = datlen;
1628
0
}
1629
1630
void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1631
0
{
1632
0
    ctx->data = data;
1633
0
}
1634
1635
void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1636
0
{
1637
0
    return ctx->data;
1638
0
}
1639
1640
EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1641
139k
{
1642
139k
    return ctx->pkey;
1643
139k
}
1644
1645
EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1646
0
{
1647
0
    return ctx->peerkey;
1648
0
}
1649
1650
void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1651
0
{
1652
0
    ctx->app_data = data;
1653
0
}
1654
1655
void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1656
0
{
1657
0
    return ctx->app_data;
1658
0
}
1659
1660
void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
1661
    int (*init)(EVP_PKEY_CTX *ctx))
1662
0
{
1663
0
    pmeth->init = init;
1664
0
}
1665
1666
void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
1667
    int (*copy)(EVP_PKEY_CTX *dst,
1668
        const EVP_PKEY_CTX *src))
1669
0
{
1670
0
    pmeth->copy = copy;
1671
0
}
1672
1673
void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1674
    void (*cleanup)(EVP_PKEY_CTX *ctx))
1675
0
{
1676
0
    pmeth->cleanup = cleanup;
1677
0
}
1678
1679
void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1680
    int (*paramgen_init)(EVP_PKEY_CTX *ctx),
1681
    int (*paramgen)(EVP_PKEY_CTX *ctx,
1682
        EVP_PKEY *pkey))
1683
0
{
1684
0
    pmeth->paramgen_init = paramgen_init;
1685
0
    pmeth->paramgen = paramgen;
1686
0
}
1687
1688
void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1689
    int (*keygen_init)(EVP_PKEY_CTX *ctx),
1690
    int (*keygen)(EVP_PKEY_CTX *ctx,
1691
        EVP_PKEY *pkey))
1692
0
{
1693
0
    pmeth->keygen_init = keygen_init;
1694
0
    pmeth->keygen = keygen;
1695
0
}
1696
1697
void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1698
    int (*sign_init)(EVP_PKEY_CTX *ctx),
1699
    int (*sign)(EVP_PKEY_CTX *ctx,
1700
        unsigned char *sig, size_t *siglen,
1701
        const unsigned char *tbs,
1702
        size_t tbslen))
1703
0
{
1704
0
    pmeth->sign_init = sign_init;
1705
0
    pmeth->sign = sign;
1706
0
}
1707
1708
void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1709
    int (*verify_init)(EVP_PKEY_CTX *ctx),
1710
    int (*verify)(EVP_PKEY_CTX *ctx,
1711
        const unsigned char *sig,
1712
        size_t siglen,
1713
        const unsigned char *tbs,
1714
        size_t tbslen))
1715
0
{
1716
0
    pmeth->verify_init = verify_init;
1717
0
    pmeth->verify = verify;
1718
0
}
1719
1720
void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1721
    int (*verify_recover_init)(EVP_PKEY_CTX
1722
            *ctx),
1723
    int (*verify_recover)(EVP_PKEY_CTX
1724
                              *ctx,
1725
        unsigned char
1726
            *sig,
1727
        size_t *siglen,
1728
        const unsigned char *tbs,
1729
        size_t tbslen))
1730
0
{
1731
0
    pmeth->verify_recover_init = verify_recover_init;
1732
0
    pmeth->verify_recover = verify_recover;
1733
0
}
1734
1735
void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1736
    int (*signctx_init)(EVP_PKEY_CTX *ctx,
1737
        EVP_MD_CTX *mctx),
1738
    int (*signctx)(EVP_PKEY_CTX *ctx,
1739
        unsigned char *sig,
1740
        size_t *siglen,
1741
        EVP_MD_CTX *mctx))
1742
0
{
1743
0
    pmeth->signctx_init = signctx_init;
1744
0
    pmeth->signctx = signctx;
1745
0
}
1746
1747
void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1748
    int (*verifyctx_init)(EVP_PKEY_CTX *ctx,
1749
        EVP_MD_CTX *mctx),
1750
    int (*verifyctx)(EVP_PKEY_CTX *ctx,
1751
        const unsigned char *sig,
1752
        int siglen,
1753
        EVP_MD_CTX *mctx))
1754
0
{
1755
0
    pmeth->verifyctx_init = verifyctx_init;
1756
0
    pmeth->verifyctx = verifyctx;
1757
0
}
1758
1759
void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1760
    int (*encrypt_init)(EVP_PKEY_CTX *ctx),
1761
    int (*encryptfn)(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->encrypt_init = encrypt_init;
1768
0
    pmeth->encrypt = encryptfn;
1769
0
}
1770
1771
void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1772
    int (*decrypt_init)(EVP_PKEY_CTX *ctx),
1773
    int (*decrypt)(EVP_PKEY_CTX *ctx,
1774
        unsigned char *out,
1775
        size_t *outlen,
1776
        const unsigned char *in,
1777
        size_t inlen))
1778
0
{
1779
0
    pmeth->decrypt_init = decrypt_init;
1780
0
    pmeth->decrypt = decrypt;
1781
0
}
1782
1783
void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1784
    int (*derive_init)(EVP_PKEY_CTX *ctx),
1785
    int (*derive)(EVP_PKEY_CTX *ctx,
1786
        unsigned char *key,
1787
        size_t *keylen))
1788
0
{
1789
0
    pmeth->derive_init = derive_init;
1790
0
    pmeth->derive = derive;
1791
0
}
1792
1793
void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1794
    int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1,
1795
        void *p2),
1796
    int (*ctrl_str)(EVP_PKEY_CTX *ctx,
1797
        const char *type,
1798
        const char *value))
1799
0
{
1800
0
    pmeth->ctrl = ctrl;
1801
0
    pmeth->ctrl_str = ctrl_str;
1802
0
}
1803
1804
void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1805
    int (*digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1806
        const unsigned char *tbs, size_t tbslen))
1807
0
{
1808
0
    pmeth->digestsign = digestsign;
1809
0
}
1810
1811
void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1812
    int (*digestverify)(EVP_MD_CTX *ctx, const unsigned char *sig,
1813
        size_t siglen, const unsigned char *tbs,
1814
        size_t tbslen))
1815
0
{
1816
0
    pmeth->digestverify = digestverify;
1817
0
}
1818
1819
void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1820
    int (*check)(EVP_PKEY *pkey))
1821
0
{
1822
0
    pmeth->check = check;
1823
0
}
1824
1825
void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1826
    int (*check)(EVP_PKEY *pkey))
1827
0
{
1828
0
    pmeth->public_check = check;
1829
0
}
1830
1831
void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1832
    int (*check)(EVP_PKEY *pkey))
1833
0
{
1834
0
    pmeth->param_check = check;
1835
0
}
1836
1837
void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1838
    int (*digest_custom)(EVP_PKEY_CTX *ctx,
1839
        EVP_MD_CTX *mctx))
1840
0
{
1841
0
    pmeth->digest_custom = digest_custom;
1842
0
}
1843
1844
void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1845
    int (**pinit)(EVP_PKEY_CTX *ctx))
1846
0
{
1847
0
    *pinit = pmeth->init;
1848
0
}
1849
1850
void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1851
    int (**pcopy)(EVP_PKEY_CTX *dst,
1852
        const EVP_PKEY_CTX *src))
1853
0
{
1854
0
    *pcopy = pmeth->copy;
1855
0
}
1856
1857
void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1858
    void (**pcleanup)(EVP_PKEY_CTX *ctx))
1859
0
{
1860
0
    *pcleanup = pmeth->cleanup;
1861
0
}
1862
1863
void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1864
    int (**pparamgen_init)(EVP_PKEY_CTX *ctx),
1865
    int (**pparamgen)(EVP_PKEY_CTX *ctx,
1866
        EVP_PKEY *pkey))
1867
0
{
1868
0
    if (pparamgen_init)
1869
0
        *pparamgen_init = pmeth->paramgen_init;
1870
0
    if (pparamgen)
1871
0
        *pparamgen = pmeth->paramgen;
1872
0
}
1873
1874
void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1875
    int (**pkeygen_init)(EVP_PKEY_CTX *ctx),
1876
    int (**pkeygen)(EVP_PKEY_CTX *ctx,
1877
        EVP_PKEY *pkey))
1878
0
{
1879
0
    if (pkeygen_init)
1880
0
        *pkeygen_init = pmeth->keygen_init;
1881
0
    if (pkeygen)
1882
0
        *pkeygen = pmeth->keygen;
1883
0
}
1884
1885
void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1886
    int (**psign_init)(EVP_PKEY_CTX *ctx),
1887
    int (**psign)(EVP_PKEY_CTX *ctx,
1888
        unsigned char *sig, size_t *siglen,
1889
        const unsigned char *tbs,
1890
        size_t tbslen))
1891
0
{
1892
0
    if (psign_init)
1893
0
        *psign_init = pmeth->sign_init;
1894
0
    if (psign)
1895
0
        *psign = pmeth->sign;
1896
0
}
1897
1898
void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1899
    int (**pverify_init)(EVP_PKEY_CTX *ctx),
1900
    int (**pverify)(EVP_PKEY_CTX *ctx,
1901
        const unsigned char *sig,
1902
        size_t siglen,
1903
        const unsigned char *tbs,
1904
        size_t tbslen))
1905
0
{
1906
0
    if (pverify_init)
1907
0
        *pverify_init = pmeth->verify_init;
1908
0
    if (pverify)
1909
0
        *pverify = pmeth->verify;
1910
0
}
1911
1912
void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1913
    int (**pverify_recover_init)(EVP_PKEY_CTX
1914
            *ctx),
1915
    int (**pverify_recover)(EVP_PKEY_CTX
1916
                                *ctx,
1917
        unsigned char
1918
            *sig,
1919
        size_t *siglen,
1920
        const unsigned char *tbs,
1921
        size_t tbslen))
1922
0
{
1923
0
    if (pverify_recover_init)
1924
0
        *pverify_recover_init = pmeth->verify_recover_init;
1925
0
    if (pverify_recover)
1926
0
        *pverify_recover = pmeth->verify_recover;
1927
0
}
1928
1929
void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1930
    int (**psignctx_init)(EVP_PKEY_CTX *ctx,
1931
        EVP_MD_CTX *mctx),
1932
    int (**psignctx)(EVP_PKEY_CTX *ctx,
1933
        unsigned char *sig,
1934
        size_t *siglen,
1935
        EVP_MD_CTX *mctx))
1936
0
{
1937
0
    if (psignctx_init)
1938
0
        *psignctx_init = pmeth->signctx_init;
1939
0
    if (psignctx)
1940
0
        *psignctx = pmeth->signctx;
1941
0
}
1942
1943
void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1944
    int (**pverifyctx_init)(EVP_PKEY_CTX *ctx,
1945
        EVP_MD_CTX *mctx),
1946
    int (**pverifyctx)(EVP_PKEY_CTX *ctx,
1947
        const unsigned char *sig,
1948
        int siglen,
1949
        EVP_MD_CTX *mctx))
1950
0
{
1951
0
    if (pverifyctx_init)
1952
0
        *pverifyctx_init = pmeth->verifyctx_init;
1953
0
    if (pverifyctx)
1954
0
        *pverifyctx = pmeth->verifyctx;
1955
0
}
1956
1957
void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1958
    int (**pencrypt_init)(EVP_PKEY_CTX *ctx),
1959
    int (**pencryptfn)(EVP_PKEY_CTX *ctx,
1960
        unsigned char *out,
1961
        size_t *outlen,
1962
        const unsigned char *in,
1963
        size_t inlen))
1964
0
{
1965
0
    if (pencrypt_init)
1966
0
        *pencrypt_init = pmeth->encrypt_init;
1967
0
    if (pencryptfn)
1968
0
        *pencryptfn = pmeth->encrypt;
1969
0
}
1970
1971
void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1972
    int (**pdecrypt_init)(EVP_PKEY_CTX *ctx),
1973
    int (**pdecrypt)(EVP_PKEY_CTX *ctx,
1974
        unsigned char *out,
1975
        size_t *outlen,
1976
        const unsigned char *in,
1977
        size_t inlen))
1978
0
{
1979
0
    if (pdecrypt_init)
1980
0
        *pdecrypt_init = pmeth->decrypt_init;
1981
0
    if (pdecrypt)
1982
0
        *pdecrypt = pmeth->decrypt;
1983
0
}
1984
1985
void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1986
    int (**pderive_init)(EVP_PKEY_CTX *ctx),
1987
    int (**pderive)(EVP_PKEY_CTX *ctx,
1988
        unsigned char *key,
1989
        size_t *keylen))
1990
0
{
1991
0
    if (pderive_init)
1992
0
        *pderive_init = pmeth->derive_init;
1993
0
    if (pderive)
1994
0
        *pderive = pmeth->derive;
1995
0
}
1996
1997
void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1998
    int (**pctrl)(EVP_PKEY_CTX *ctx, int type, int p1,
1999
        void *p2),
2000
    int (**pctrl_str)(EVP_PKEY_CTX *ctx,
2001
        const char *type,
2002
        const char *value))
2003
0
{
2004
0
    if (pctrl)
2005
0
        *pctrl = pmeth->ctrl;
2006
0
    if (pctrl_str)
2007
0
        *pctrl_str = pmeth->ctrl_str;
2008
0
}
2009
2010
void EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD *pmeth,
2011
    int (**digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
2012
        const unsigned char *tbs, size_t tbslen))
2013
0
{
2014
0
    if (digestsign)
2015
0
        *digestsign = pmeth->digestsign;
2016
0
}
2017
2018
void EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD *pmeth,
2019
    int (**digestverify)(EVP_MD_CTX *ctx, const unsigned char *sig,
2020
        size_t siglen, const unsigned char *tbs,
2021
        size_t tbslen))
2022
0
{
2023
0
    if (digestverify)
2024
0
        *digestverify = pmeth->digestverify;
2025
0
}
2026
2027
void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2028
    int (**pcheck)(EVP_PKEY *pkey))
2029
0
{
2030
0
    if (pcheck != NULL)
2031
0
        *pcheck = pmeth->check;
2032
0
}
2033
2034
void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
2035
    int (**pcheck)(EVP_PKEY *pkey))
2036
0
{
2037
0
    if (pcheck != NULL)
2038
0
        *pcheck = pmeth->public_check;
2039
0
}
2040
2041
void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
2042
    int (**pcheck)(EVP_PKEY *pkey))
2043
0
{
2044
0
    if (pcheck != NULL)
2045
0
        *pcheck = pmeth->param_check;
2046
0
}
2047
2048
void EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD *pmeth,
2049
    int (**pdigest_custom)(EVP_PKEY_CTX *ctx,
2050
        EVP_MD_CTX *mctx))
2051
0
{
2052
0
    if (pdigest_custom != NULL)
2053
0
        *pdigest_custom = pmeth->digest_custom;
2054
0
}
2055
2056
#endif /* FIPS_MODULE */