Coverage Report

Created: 2025-08-28 07:07

/src/openssl33/crypto/evp/pmeth_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2006-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
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
4.25M
{
94
4.25M
    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
4.25M
    return NULL;
104
4.25M
}
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
126k
{
140
126k
    if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
141
0
        return EVP_PKEY_STATE_UNKNOWN;
142
143
126k
    if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
144
126k
         && ctx->op.kex.algctx != NULL)
145
126k
        || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
146
126k
            && ctx->op.sig.algctx != NULL)
147
126k
        || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
148
61.7k
            && ctx->op.ciph.algctx != NULL)
149
126k
        || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
150
49.7k
            && ctx->op.keymgmt.genctx != NULL)
151
126k
        || (EVP_PKEY_CTX_IS_KEM_OP(ctx)
152
0
            && ctx->op.encap.algctx != NULL))
153
126k
        return EVP_PKEY_STATE_PROVIDER;
154
155
0
    return EVP_PKEY_STATE_LEGACY;
156
126k
}
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
2.22M
{
164
2.22M
    EVP_PKEY_CTX *ret = NULL;
165
2.22M
    const EVP_PKEY_METHOD *pmeth = NULL, *app_pmeth = NULL;
166
2.22M
    EVP_KEYMGMT *keymgmt = NULL;
167
168
    /* Code below to be removed when legacy support is dropped. */
169
    /* BEGIN legacy */
170
2.22M
    if (id == -1) {
171
2.22M
        if (pkey != NULL && !evp_pkey_is_provided(pkey)) {
172
2.05M
            id = pkey->type;
173
2.05M
        } else {
174
162k
            if (pkey != NULL) {
175
                /* Must be provided if we get here */
176
91.2k
                keytype = EVP_KEYMGMT_get0_name(pkey->keymgmt);
177
91.2k
            }
178
162k
#ifndef FIPS_MODULE
179
162k
            if (keytype != NULL) {
180
162k
                id = evp_pkey_name2type(keytype);
181
162k
                if (id == NID_undef)
182
13.7k
                    id = -1;
183
162k
            }
184
162k
#endif
185
162k
        }
186
2.22M
    }
187
    /* If no ID was found here, we can only resort to find a keymgmt */
188
2.22M
    if (id == -1) {
189
14.0k
#ifndef FIPS_MODULE
190
        /* Using engine with a key without id will not work */
191
14.0k
        if (e != NULL) {
192
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
193
0
            return NULL;
194
0
        }
195
14.0k
#endif
196
14.0k
        goto common;
197
14.0k
    }
198
199
2.20M
#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
2.20M
    if (e != NULL)
209
0
        keytype = NULL;
210
2.20M
    if (e == NULL && (pkey == NULL || pkey->foreign == 0))
211
2.20M
        keytype = OBJ_nid2sn(id);
212
213
2.20M
# ifndef OPENSSL_NO_ENGINE
214
2.20M
    if (e == NULL && pkey != NULL)
215
2.14M
        e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
216
    /* Try to find an ENGINE which implements this method */
217
2.20M
    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
2.20M
    } else {
223
2.20M
        e = ENGINE_get_pkey_meth_engine(id);
224
2.20M
    }
225
226
    /*
227
     * If an ENGINE handled this method look it up. Otherwise use internal
228
     * tables.
229
     */
230
2.20M
    if (e != NULL)
231
0
        pmeth = ENGINE_get_pkey_meth(e, id);
232
2.20M
    else
233
2.20M
# endif /* OPENSSL_NO_ENGINE */
234
2.20M
    if (pkey != NULL && pkey->foreign)
235
0
        pmeth = EVP_PKEY_meth_find(id);
236
2.20M
    else
237
2.20M
        app_pmeth = pmeth = evp_pkey_meth_find_added_by_application(id);
238
239
    /* END legacy */
240
2.20M
#endif /* FIPS_MODULE */
241
2.22M
 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
2.22M
    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
2.22M
        if (pkey != NULL && pkey->keymgmt != NULL) {
254
91.2k
            if (!EVP_KEYMGMT_up_ref(pkey->keymgmt))
255
91.2k
                ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
256
91.2k
            else
257
91.2k
                keymgmt = pkey->keymgmt;
258
2.12M
        } else {
259
2.12M
            keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
260
2.12M
        }
261
2.22M
        if (keymgmt == NULL)
262
0
            return NULL;   /* EVP_KEYMGMT_fetch() recorded an error */
263
264
2.22M
#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
2.22M
        if (keymgmt != NULL) {
273
2.22M
            int tmp_id = evp_keymgmt_get_legacy_alg(keymgmt);
274
275
2.22M
            if (tmp_id != NID_undef) {
276
2.20M
                if (id == -1) {
277
0
                    id = tmp_id;
278
2.20M
                } else {
279
                    /*
280
                     * It really really shouldn't differ.  If it still does,
281
                     * something is very wrong.
282
                     */
283
2.20M
                    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
2.20M
                }
289
2.20M
            }
290
2.22M
        }
291
2.22M
#endif
292
2.22M
    }
293
294
2.22M
    if (pmeth == NULL && keymgmt == NULL) {
295
300
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
296
2.22M
    } else {
297
2.22M
        ret = OPENSSL_zalloc(sizeof(*ret));
298
2.22M
    }
299
300
2.22M
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
301
2.22M
    if ((ret == NULL || pmeth == NULL) && e != NULL)
302
0
        ENGINE_finish(e);
303
2.22M
#endif
304
305
2.22M
    if (ret == NULL) {
306
300
        EVP_KEYMGMT_free(keymgmt);
307
300
        return NULL;
308
300
    }
309
2.22M
    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
2.22M
    ret->libctx = libctx;
318
2.22M
    ret->keytype = keytype;
319
2.22M
    ret->keymgmt = keymgmt;
320
2.22M
    ret->legacy_keytype = id;
321
2.22M
    ret->engine = e;
322
2.22M
    ret->pmeth = pmeth;
323
2.22M
    ret->operation = EVP_PKEY_OP_UNDEFINED;
324
2.22M
    ret->pkey = pkey;
325
2.22M
    if (pkey != NULL)
326
2.14M
        EVP_PKEY_up_ref(pkey);
327
328
2.22M
    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
2.22M
    return ret;
337
2.22M
}
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
232k
{
345
232k
    return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
346
232k
}
347
348
EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey,
349
                                         const char *propquery)
350
4.09M
{
351
4.09M
    return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
352
4.09M
}
353
354
void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
355
2.11M
{
356
2.11M
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
357
280k
        if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL)
358
280k
            ctx->op.sig.signature->freectx(ctx->op.sig.algctx);
359
280k
        EVP_SIGNATURE_free(ctx->op.sig.signature);
360
280k
        ctx->op.sig.algctx = NULL;
361
280k
        ctx->op.sig.signature = NULL;
362
1.83M
    } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
363
17.3k
        if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL)
364
17.3k
            ctx->op.kex.exchange->freectx(ctx->op.kex.algctx);
365
17.3k
        EVP_KEYEXCH_free(ctx->op.kex.exchange);
366
17.3k
        ctx->op.kex.algctx = NULL;
367
17.3k
        ctx->op.kex.exchange = NULL;
368
1.81M
    } 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
0
    }
375
1.81M
    else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
376
5.03k
        if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL)
377
5.03k
            ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx);
378
5.03k
        EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
379
5.03k
        ctx->op.ciph.algctx = NULL;
380
5.03k
        ctx->op.ciph.cipher = NULL;
381
1.81M
    } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
382
55.2k
        if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
383
55.2k
            evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
384
55.2k
    }
385
2.11M
}
386
387
void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
388
29.6M
{
389
29.6M
    if (ctx == NULL)
390
24.4M
        return;
391
5.23M
    if (ctx->pmeth && ctx->pmeth->cleanup)
392
0
        ctx->pmeth->cleanup(ctx);
393
394
5.23M
    evp_pkey_ctx_free_old_ops(ctx);
395
5.23M
#ifndef FIPS_MODULE
396
5.23M
    evp_pkey_ctx_free_all_cached_data(ctx);
397
5.23M
#endif
398
5.23M
    EVP_KEYMGMT_free(ctx->keymgmt);
399
400
5.23M
    OPENSSL_free(ctx->propquery);
401
5.23M
    EVP_PKEY_free(ctx->pkey);
402
5.23M
    EVP_PKEY_free(ctx->peerkey);
403
5.23M
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
404
5.23M
    ENGINE_finish(ctx->engine);
405
5.23M
#endif
406
5.23M
    BN_free(ctx->rsa_pubexp);
407
5.23M
    OPENSSL_free(ctx);
408
5.23M
}
409
410
#ifndef FIPS_MODULE
411
412
void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
413
                             const EVP_PKEY_METHOD *meth)
414
0
{
415
0
    if (ppkey_id)
416
0
        *ppkey_id = meth->pkey_id;
417
0
    if (pflags)
418
0
        *pflags = meth->flags;
419
0
}
420
421
void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
422
0
{
423
0
    int pkey_id = dst->pkey_id;
424
0
    int flags = dst->flags;
425
426
0
    *dst = *src;
427
428
    /* We only copy the function pointers so restore the other values */
429
0
    dst->pkey_id = pkey_id;
430
0
    dst->flags = flags;
431
0
}
432
433
void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
434
0
{
435
0
    if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
436
0
        OPENSSL_free(pmeth);
437
0
}
438
439
EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
440
19.2k
{
441
19.2k
    return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
442
19.2k
}
443
444
EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
445
0
{
446
0
    return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
447
0
}
448
449
EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
450
235k
{
451
235k
    EVP_PKEY_CTX *rctx;
452
453
235k
# ifndef OPENSSL_NO_ENGINE
454
    /* Make sure it's safe to copy a pkey context using an ENGINE */
455
235k
    if (pctx->engine && !ENGINE_init(pctx->engine)) {
456
0
        ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
457
0
        return 0;
458
0
    }
459
235k
# endif
460
235k
    rctx = OPENSSL_zalloc(sizeof(*rctx));
461
235k
    if (rctx == NULL)
462
0
        return NULL;
463
464
235k
    if (pctx->pkey != NULL)
465
235k
        EVP_PKEY_up_ref(pctx->pkey);
466
235k
    rctx->pkey = pctx->pkey;
467
235k
    rctx->operation = pctx->operation;
468
235k
    rctx->libctx = pctx->libctx;
469
235k
    rctx->keytype = pctx->keytype;
470
235k
    rctx->propquery = NULL;
471
235k
    if (pctx->propquery != NULL) {
472
0
        rctx->propquery = OPENSSL_strdup(pctx->propquery);
473
0
        if (rctx->propquery == NULL)
474
0
            goto err;
475
0
    }
476
235k
    rctx->legacy_keytype = pctx->legacy_keytype;
477
478
235k
    if (pctx->keymgmt != NULL) {
479
235k
        if (!EVP_KEYMGMT_up_ref(pctx->keymgmt))
480
0
            goto err;
481
235k
        rctx->keymgmt = pctx->keymgmt;
482
235k
    }
483
484
235k
    if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
485
0
        if (pctx->op.kex.exchange != NULL) {
486
0
            rctx->op.kex.exchange = pctx->op.kex.exchange;
487
0
            if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange))
488
0
                goto err;
489
0
        }
490
0
        if (pctx->op.kex.algctx != NULL) {
491
0
            if (!ossl_assert(pctx->op.kex.exchange != NULL))
492
0
                goto err;
493
494
0
            if (pctx->op.kex.exchange->dupctx != NULL)
495
0
                rctx->op.kex.algctx
496
0
                    = pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx);
497
498
0
            if (rctx->op.kex.algctx == NULL) {
499
0
                EVP_KEYEXCH_free(rctx->op.kex.exchange);
500
0
                rctx->op.kex.exchange = NULL;
501
0
                goto err;
502
0
            }
503
0
            return rctx;
504
0
        }
505
235k
    } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
506
235k
        if (pctx->op.sig.signature != NULL) {
507
235k
            rctx->op.sig.signature = pctx->op.sig.signature;
508
235k
            if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
509
0
                goto err;
510
235k
        }
511
235k
        if (pctx->op.sig.algctx != NULL) {
512
235k
            if (!ossl_assert(pctx->op.sig.signature != NULL))
513
0
                goto err;
514
515
235k
            if (pctx->op.sig.signature->dupctx != NULL)
516
235k
                rctx->op.sig.algctx
517
235k
                    = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx);
518
519
235k
            if (rctx->op.sig.algctx == NULL) {
520
0
                EVP_SIGNATURE_free(rctx->op.sig.signature);
521
0
                rctx->op.sig.signature = NULL;
522
0
                goto err;
523
0
            }
524
235k
            return rctx;
525
235k
        }
526
235k
    } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
527
0
        if (pctx->op.ciph.cipher != NULL) {
528
0
            rctx->op.ciph.cipher = pctx->op.ciph.cipher;
529
0
            if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
530
0
                goto err;
531
0
        }
532
0
        if (pctx->op.ciph.algctx != NULL) {
533
0
            if (!ossl_assert(pctx->op.ciph.cipher != NULL))
534
0
                goto err;
535
536
0
            if (pctx->op.ciph.cipher->dupctx != NULL)
537
0
                rctx->op.ciph.algctx
538
0
                    = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx);
539
540
0
            if (rctx->op.ciph.algctx == NULL) {
541
0
                EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
542
0
                rctx->op.ciph.cipher = NULL;
543
0
                goto err;
544
0
            }
545
0
            return rctx;
546
0
        }
547
0
    } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) {
548
0
        if (pctx->op.encap.kem != NULL) {
549
0
            rctx->op.encap.kem = pctx->op.encap.kem;
550
0
            if (!EVP_KEM_up_ref(rctx->op.encap.kem))
551
0
                goto err;
552
0
        }
553
0
        if (pctx->op.encap.algctx != NULL) {
554
0
            if (!ossl_assert(pctx->op.encap.kem != NULL))
555
0
                goto err;
556
557
0
            if (pctx->op.encap.kem->dupctx != NULL)
558
0
                rctx->op.encap.algctx
559
0
                    = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx);
560
561
0
            if (rctx->op.encap.algctx == NULL) {
562
0
                EVP_KEM_free(rctx->op.encap.kem);
563
0
                rctx->op.encap.kem = NULL;
564
0
                goto err;
565
0
            }
566
0
            return rctx;
567
0
        }
568
0
    } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
569
        /* Not supported - This would need a gen_dupctx() to work */
570
0
        goto err;
571
0
    }
572
573
0
    rctx->pmeth = pctx->pmeth;
574
0
# ifndef OPENSSL_NO_ENGINE
575
0
    rctx->engine = pctx->engine;
576
0
# endif
577
578
0
    if (pctx->peerkey != NULL)
579
0
        EVP_PKEY_up_ref(pctx->peerkey);
580
0
    rctx->peerkey = pctx->peerkey;
581
582
0
    if (pctx->pmeth == NULL) {
583
0
        if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
584
0
            EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
585
0
            void *provkey;
586
587
0
            if (pctx->pkey == NULL)
588
0
                return rctx;
589
590
0
            provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
591
0
                                                  &tmp_keymgmt, pctx->propquery);
592
0
            if (provkey == NULL)
593
0
                goto err;
594
0
            if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
595
0
                goto err;
596
0
            EVP_KEYMGMT_free(rctx->keymgmt);
597
0
            rctx->keymgmt = tmp_keymgmt;
598
0
            return rctx;
599
0
        }
600
0
    } else if (pctx->pmeth->copy(rctx, pctx) > 0) {
601
0
        return rctx;
602
0
    }
603
0
err:
604
0
    rctx->pmeth = NULL;
605
0
    EVP_PKEY_CTX_free(rctx);
606
0
    return NULL;
607
0
}
608
609
int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
610
0
{
611
0
    if (app_pkey_methods == NULL) {
612
0
        app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
613
0
        if (app_pkey_methods == NULL) {
614
0
            ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
615
0
            return 0;
616
0
        }
617
0
    }
618
0
    if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
619
0
        ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
620
0
        return 0;
621
0
    }
622
0
    sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
623
0
    return 1;
624
0
}
625
626
void evp_app_cleanup_int(void)
627
252
{
628
252
    if (app_pkey_methods != NULL)
629
0
        sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
630
252
}
631
632
int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
633
0
{
634
0
    const EVP_PKEY_METHOD *ret;
635
636
0
    ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
637
638
0
    return ret == NULL ? 0 : 1;
639
0
}
640
641
size_t EVP_PKEY_meth_get_count(void)
642
0
{
643
0
    size_t rv = OSSL_NELEM(standard_methods);
644
645
0
    if (app_pkey_methods)
646
0
        rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
647
0
    return rv;
648
0
}
649
650
const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
651
0
{
652
0
    if (idx < OSSL_NELEM(standard_methods))
653
0
        return (standard_methods[idx])();
654
0
    if (app_pkey_methods == NULL)
655
0
        return NULL;
656
0
    idx -= OSSL_NELEM(standard_methods);
657
0
    if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
658
0
        return NULL;
659
0
    return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
660
0
}
661
#endif
662
663
int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype)
664
0
{
665
0
#ifndef FIPS_MODULE
666
0
    if (evp_pkey_ctx_is_legacy(ctx))
667
0
        return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype));
668
0
#endif
669
0
    return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype);
670
0
}
671
672
int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
673
100k
{
674
100k
    switch (evp_pkey_ctx_state(ctx)) {
675
100k
    case EVP_PKEY_STATE_PROVIDER:
676
100k
        if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
677
100k
            && ctx->op.kex.exchange != NULL
678
100k
            && ctx->op.kex.exchange->set_ctx_params != NULL)
679
0
            return
680
0
                ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx,
681
0
                                                     params);
682
100k
        if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
683
100k
            && ctx->op.sig.signature != NULL
684
100k
            && ctx->op.sig.signature->set_ctx_params != NULL)
685
43.0k
            return
686
43.0k
                ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx,
687
43.0k
                                                      params);
688
57.7k
        if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
689
57.7k
            && ctx->op.ciph.cipher != NULL
690
57.7k
            && ctx->op.ciph.cipher->set_ctx_params != NULL)
691
8.00k
            return
692
8.00k
                ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx,
693
8.00k
                                                    params);
694
49.7k
        if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
695
49.7k
            && ctx->keymgmt != NULL
696
49.7k
            && ctx->keymgmt->gen_set_params != NULL)
697
49.7k
            return
698
49.7k
                evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
699
49.7k
                                           params);
700
0
        if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
701
0
            && ctx->op.encap.kem != NULL
702
0
            && ctx->op.encap.kem->set_ctx_params != NULL)
703
0
            return
704
0
                ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx,
705
0
                                                  params);
706
0
        break;
707
0
    case EVP_PKEY_STATE_UNKNOWN:
708
0
        break;
709
0
#ifndef FIPS_MODULE
710
0
    case EVP_PKEY_STATE_LEGACY:
711
0
        return evp_pkey_ctx_set_params_to_ctrl(ctx, params);
712
100k
#endif
713
100k
    }
714
0
    return 0;
715
100k
}
716
717
int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
718
0
{
719
0
    switch (evp_pkey_ctx_state(ctx)) {
720
0
    case EVP_PKEY_STATE_PROVIDER:
721
0
        if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
722
0
            && ctx->op.kex.exchange != NULL
723
0
            && ctx->op.kex.exchange->get_ctx_params != NULL)
724
0
            return
725
0
                ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx,
726
0
                                                     params);
727
0
        if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
728
0
            && ctx->op.sig.signature != NULL
729
0
            && ctx->op.sig.signature->get_ctx_params != NULL)
730
0
            return
731
0
                ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx,
732
0
                                                      params);
733
0
        if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
734
0
            && ctx->op.ciph.cipher != NULL
735
0
            && ctx->op.ciph.cipher->get_ctx_params != NULL)
736
0
            return
737
0
                ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx,
738
0
                                                    params);
739
0
        if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
740
0
            && ctx->op.encap.kem != NULL
741
0
            && ctx->op.encap.kem->get_ctx_params != NULL)
742
0
            return
743
0
                ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx,
744
0
                                                  params);
745
0
        break;
746
0
    case EVP_PKEY_STATE_UNKNOWN:
747
0
        break;
748
0
#ifndef FIPS_MODULE
749
0
    case EVP_PKEY_STATE_LEGACY:
750
0
        return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
751
0
#endif
752
0
    }
753
0
    return 0;
754
0
}
755
756
#ifndef FIPS_MODULE
757
const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx)
758
0
{
759
0
    void *provctx;
760
761
0
    if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
762
0
            && ctx->op.kex.exchange != NULL
763
0
            && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
764
0
        provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
765
0
        return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx,
766
0
                                                         provctx);
767
0
    }
768
0
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
769
0
            && ctx->op.sig.signature != NULL
770
0
            && ctx->op.sig.signature->gettable_ctx_params != NULL) {
771
0
        provctx = ossl_provider_ctx(
772
0
                      EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
773
0
        return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx,
774
0
                                                          provctx);
775
0
    }
776
0
    if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
777
0
            && ctx->op.ciph.cipher != NULL
778
0
            && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
779
0
        provctx = ossl_provider_ctx(
780
0
                      EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
781
0
        return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx,
782
0
                                                        provctx);
783
0
    }
784
0
    if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
785
0
        && ctx->op.encap.kem != NULL
786
0
        && ctx->op.encap.kem->gettable_ctx_params != NULL) {
787
0
        provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
788
0
        return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx,
789
0
                                                      provctx);
790
0
    }
791
0
    return NULL;
792
0
}
793
794
const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx)
795
25.5k
{
796
25.5k
    void *provctx;
797
798
25.5k
    if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
799
25.5k
            && ctx->op.kex.exchange != NULL
800
25.5k
            && ctx->op.kex.exchange->settable_ctx_params != NULL) {
801
0
        provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
802
0
        return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx,
803
0
                                                         provctx);
804
0
    }
805
25.5k
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
806
25.5k
            && ctx->op.sig.signature != NULL
807
25.5k
            && ctx->op.sig.signature->settable_ctx_params != NULL) {
808
21.5k
        provctx = ossl_provider_ctx(
809
21.5k
                      EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
810
21.5k
        return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx,
811
21.5k
                                                          provctx);
812
21.5k
    }
813
4.00k
    if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
814
4.00k
            && ctx->op.ciph.cipher != NULL
815
4.00k
            && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
816
4.00k
        provctx = ossl_provider_ctx(
817
4.00k
                      EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
818
4.00k
        return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx,
819
4.00k
                                                        provctx);
820
4.00k
    }
821
0
    if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
822
0
            && ctx->keymgmt != NULL
823
0
            && ctx->keymgmt->gen_settable_params != NULL) {
824
0
        provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
825
0
        return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx,
826
0
                                                 provctx);
827
0
    }
828
0
    if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
829
0
        && ctx->op.encap.kem != NULL
830
0
        && ctx->op.encap.kem->settable_ctx_params != NULL) {
831
0
        provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
832
0
        return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx,
833
0
                                                      provctx);
834
0
    }
835
0
    return NULL;
836
0
}
837
838
/*
839
 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
840
 *
841
 * Return 1 on success, 0 or negative for errors.
842
 *
843
 * In particular they return -2 if any of the params is not supported.
844
 *
845
 * They are not available in FIPS_MODULE as they depend on
846
 *      - EVP_PKEY_CTX_{get,set}_params()
847
 *      - EVP_PKEY_CTX_{gettable,settable}_params()
848
 *
849
 */
850
int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
851
59.8k
{
852
59.8k
    if (ctx == NULL || params == NULL)
853
0
        return 0;
854
855
    /*
856
     * We only check for provider side EVP_PKEY_CTX.  For #legacy, we
857
     * depend on the translation that happens in EVP_PKEY_CTX_set_params()
858
     * call, and that the resulting ctrl call will return -2 if it doesn't
859
     * known the ctrl command number.
860
     */
861
59.8k
    if (evp_pkey_ctx_is_provided(ctx)) {
862
59.8k
        const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
863
59.8k
        const OSSL_PARAM *p;
864
865
119k
        for (p = params; p->key != NULL; p++) {
866
            /* Check the ctx actually understands this parameter */
867
59.8k
            if (OSSL_PARAM_locate_const(settable, p->key) == NULL)
868
0
                return -2;
869
59.8k
        }
870
59.8k
    }
871
872
59.8k
    return EVP_PKEY_CTX_set_params(ctx, params);
873
59.8k
}
874
875
int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
876
0
{
877
0
    if (ctx == NULL || params == NULL)
878
0
        return 0;
879
880
    /*
881
     * We only check for provider side EVP_PKEY_CTX.  For #legacy, we
882
     * depend on the translation that happens in EVP_PKEY_CTX_get_params()
883
     * call, and that the resulting ctrl call will return -2 if it doesn't
884
     * known the ctrl command number.
885
     */
886
0
    if (evp_pkey_ctx_is_provided(ctx)) {
887
0
        const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
888
0
        const OSSL_PARAM *p;
889
890
0
        for (p = params; p->key != NULL; p++) {
891
            /* Check the ctx actually understands this parameter */
892
0
            if (OSSL_PARAM_locate_const(gettable, p->key) == NULL)
893
0
                return -2;
894
0
        }
895
0
    }
896
897
0
    return EVP_PKEY_CTX_get_params(ctx, params);
898
0
}
899
900
int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
901
0
{
902
0
    OSSL_PARAM sig_md_params[2], *p = sig_md_params;
903
    /* 80 should be big enough */
904
0
    char name[80] = "";
905
0
    const EVP_MD *tmp;
906
907
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
908
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
909
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
910
0
        return -2;
911
0
    }
912
913
0
    if (ctx->op.sig.algctx == NULL)
914
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
915
0
                                 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
916
917
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
918
0
                                            name,
919
0
                                            sizeof(name));
920
0
    *p = OSSL_PARAM_construct_end();
921
922
0
    if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
923
0
        return 0;
924
925
0
    tmp = evp_get_digestbyname_ex(ctx->libctx, name);
926
0
    if (tmp == NULL)
927
0
        return 0;
928
929
0
    *md = tmp;
930
931
0
    return 1;
932
0
}
933
934
static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
935
                               int fallback, const char *param, int op,
936
                               int ctrl)
937
0
{
938
0
    OSSL_PARAM md_params[2], *p = md_params;
939
0
    const char *name;
940
941
0
    if (ctx == NULL || (ctx->operation & op) == 0) {
942
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
943
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
944
0
        return -2;
945
0
    }
946
947
0
    if (fallback)
948
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
949
950
0
    if (md == NULL) {
951
0
        name = "";
952
0
    } else {
953
0
        name = EVP_MD_get0_name(md);
954
0
    }
955
956
0
    *p++ = OSSL_PARAM_construct_utf8_string(param,
957
                                            /*
958
                                             * Cast away the const. This is read
959
                                             * only so should be safe
960
                                             */
961
0
                                            (char *)name, 0);
962
0
    *p = OSSL_PARAM_construct_end();
963
964
0
    return EVP_PKEY_CTX_set_params(ctx, md_params);
965
0
}
966
967
int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
968
0
{
969
0
    return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL,
970
0
                               OSSL_SIGNATURE_PARAM_DIGEST,
971
0
                               EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
972
0
}
973
974
int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
975
0
{
976
0
    return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
977
0
                               OSSL_KDF_PARAM_DIGEST,
978
0
                               EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
979
0
}
980
981
static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
982
                                          const char *param, int op, int ctrl,
983
                                          const unsigned char *data,
984
                                          int datalen)
985
0
{
986
0
    OSSL_PARAM octet_string_params[2], *p = octet_string_params;
987
988
0
    if (ctx == NULL || (ctx->operation & op) == 0) {
989
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
990
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
991
0
        return -2;
992
0
    }
993
994
    /* Code below to be removed when legacy support is dropped. */
995
0
    if (fallback)
996
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
997
    /* end of legacy support */
998
999
0
    if (datalen < 0) {
1000
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
1001
0
        return 0;
1002
0
    }
1003
1004
0
    *p++ = OSSL_PARAM_construct_octet_string(param,
1005
                                            /*
1006
                                             * Cast away the const. This is read
1007
                                             * only so should be safe
1008
                                             */
1009
0
                                            (unsigned char *)data,
1010
0
                                            (size_t)datalen);
1011
0
    *p = OSSL_PARAM_construct_end();
1012
1013
0
    return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
1014
0
}
1015
1016
static int evp_pkey_ctx_add1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
1017
                                          const char *param, int op, int ctrl,
1018
                                          const unsigned char *data,
1019
                                          int datalen)
1020
0
{
1021
0
    OSSL_PARAM os_params[2];
1022
0
    const OSSL_PARAM *gettables;
1023
0
    unsigned char *info = NULL;
1024
0
    size_t info_len = 0;
1025
0
    size_t info_alloc = 0;
1026
0
    int ret = 0;
1027
1028
0
    if (ctx == NULL || (ctx->operation & op) == 0) {
1029
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1030
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1031
0
        return -2;
1032
0
    }
1033
1034
    /* Code below to be removed when legacy support is dropped. */
1035
0
    if (fallback)
1036
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
1037
    /* end of legacy support */
1038
1039
0
    if (datalen < 0) {
1040
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
1041
0
        return 0;
1042
0
    } else if (datalen == 0) {
1043
0
        return 1;
1044
0
    }
1045
1046
    /* Check for older provider that doesn't support getting this parameter */
1047
0
    gettables = EVP_PKEY_CTX_gettable_params(ctx);
1048
0
    if (gettables == NULL || OSSL_PARAM_locate_const(gettables, param) == NULL)
1049
0
        return evp_pkey_ctx_set1_octet_string(ctx, fallback, param, op, ctrl,
1050
0
                                              data, datalen);
1051
1052
    /* Get the original value length */
1053
0
    os_params[0] = OSSL_PARAM_construct_octet_string(param, NULL, 0);
1054
0
    os_params[1] = OSSL_PARAM_construct_end();
1055
1056
0
    if (!EVP_PKEY_CTX_get_params(ctx, os_params))
1057
0
        return 0;
1058
1059
    /* This should not happen but check to be sure. */
1060
0
    if (os_params[0].return_size == OSSL_PARAM_UNMODIFIED)
1061
0
        return 0;
1062
1063
0
    info_alloc = os_params[0].return_size + datalen;
1064
0
    if (info_alloc == 0)
1065
0
        return 0;
1066
0
    info = OPENSSL_zalloc(info_alloc);
1067
0
    if (info == NULL)
1068
0
        return 0;
1069
0
    info_len = os_params[0].return_size;
1070
1071
0
    os_params[0] = OSSL_PARAM_construct_octet_string(param, info, info_alloc);
1072
1073
    /* if we have data, then go get it */
1074
0
    if (info_len > 0) {
1075
0
        if (!EVP_PKEY_CTX_get_params(ctx, os_params))
1076
0
            goto error;
1077
0
    }
1078
1079
    /* Copy the input data */
1080
0
    memcpy(&info[info_len], data, datalen);
1081
0
    ret = EVP_PKEY_CTX_set_params(ctx, os_params);
1082
1083
0
 error:
1084
0
    OPENSSL_clear_free(info, info_alloc);
1085
0
    return ret;
1086
0
}
1087
1088
int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
1089
                                      const unsigned char *sec, int seclen)
1090
0
{
1091
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1092
0
                                          OSSL_KDF_PARAM_SECRET,
1093
0
                                          EVP_PKEY_OP_DERIVE,
1094
0
                                          EVP_PKEY_CTRL_TLS_SECRET,
1095
0
                                          sec, seclen);
1096
0
}
1097
1098
int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
1099
                                    const unsigned char *seed, int seedlen)
1100
0
{
1101
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1102
0
                                          OSSL_KDF_PARAM_SEED,
1103
0
                                          EVP_PKEY_OP_DERIVE,
1104
0
                                          EVP_PKEY_CTRL_TLS_SEED,
1105
0
                                          seed, seedlen);
1106
0
}
1107
1108
int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1109
0
{
1110
0
    return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
1111
0
                               OSSL_KDF_PARAM_DIGEST,
1112
0
                               EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
1113
0
}
1114
1115
int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
1116
                                const unsigned char *salt, int saltlen)
1117
0
{
1118
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1119
0
                                          OSSL_KDF_PARAM_SALT,
1120
0
                                          EVP_PKEY_OP_DERIVE,
1121
0
                                          EVP_PKEY_CTRL_HKDF_SALT,
1122
0
                                          salt, saltlen);
1123
0
}
1124
1125
int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
1126
                                      const unsigned char *key, int keylen)
1127
0
{
1128
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1129
0
                                          OSSL_KDF_PARAM_KEY,
1130
0
                                          EVP_PKEY_OP_DERIVE,
1131
0
                                          EVP_PKEY_CTRL_HKDF_KEY,
1132
0
                                          key, keylen);
1133
0
}
1134
1135
int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
1136
                                      const unsigned char *info, int infolen)
1137
0
{
1138
0
    return evp_pkey_ctx_add1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1139
0
                                          OSSL_KDF_PARAM_INFO,
1140
0
                                          EVP_PKEY_OP_DERIVE,
1141
0
                                          EVP_PKEY_CTRL_HKDF_INFO,
1142
0
                                          info, infolen);
1143
0
}
1144
1145
int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
1146
0
{
1147
0
    OSSL_PARAM int_params[2], *p = int_params;
1148
1149
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1150
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1151
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1152
0
        return -2;
1153
0
    }
1154
1155
    /* Code below to be removed when legacy support is dropped. */
1156
0
    if (ctx->op.kex.algctx == NULL)
1157
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
1158
0
                                 EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
1159
    /* end of legacy support */
1160
1161
0
    if (mode < 0) {
1162
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1163
0
        return 0;
1164
0
    }
1165
1166
0
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
1167
0
    *p = OSSL_PARAM_construct_end();
1168
1169
0
    return EVP_PKEY_CTX_set_params(ctx, int_params);
1170
0
}
1171
1172
int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
1173
                               int passlen)
1174
0
{
1175
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1176
0
                                          OSSL_KDF_PARAM_PASSWORD,
1177
0
                                          EVP_PKEY_OP_DERIVE,
1178
0
                                          EVP_PKEY_CTRL_PASS,
1179
0
                                          (const unsigned char *)pass, passlen);
1180
0
}
1181
1182
int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
1183
                                  const unsigned char *salt, int saltlen)
1184
0
{
1185
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1186
0
                                          OSSL_KDF_PARAM_SALT,
1187
0
                                          EVP_PKEY_OP_DERIVE,
1188
0
                                          EVP_PKEY_CTRL_SCRYPT_SALT,
1189
0
                                          salt, saltlen);
1190
0
}
1191
1192
static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
1193
                                   int op, int ctrl, uint64_t val)
1194
0
{
1195
0
    OSSL_PARAM uint64_params[2], *p = uint64_params;
1196
1197
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1198
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1199
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1200
0
        return -2;
1201
0
    }
1202
1203
    /* Code below to be removed when legacy support is dropped. */
1204
0
    if (ctx->op.kex.algctx == NULL)
1205
0
        return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
1206
    /* end of legacy support */
1207
1208
0
    *p++ = OSSL_PARAM_construct_uint64(param, &val);
1209
0
    *p = OSSL_PARAM_construct_end();
1210
1211
0
    return EVP_PKEY_CTX_set_params(ctx, uint64_params);
1212
0
}
1213
1214
int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
1215
0
{
1216
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
1217
0
                                   EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
1218
0
                                   n);
1219
0
}
1220
1221
int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
1222
0
{
1223
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
1224
0
                                   EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
1225
0
                                   r);
1226
0
}
1227
1228
int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
1229
0
{
1230
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
1231
0
                                   EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
1232
0
                                   p);
1233
0
}
1234
1235
int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
1236
                                         uint64_t maxmem_bytes)
1237
0
{
1238
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
1239
0
                                   EVP_PKEY_OP_DERIVE,
1240
0
                                   EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
1241
0
                                   maxmem_bytes);
1242
0
}
1243
1244
int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1245
                             int keylen)
1246
0
{
1247
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1248
0
                                          OSSL_PKEY_PARAM_PRIV_KEY,
1249
0
                                          EVP_PKEY_OP_KEYGEN,
1250
0
                                          EVP_PKEY_CTRL_SET_MAC_KEY,
1251
0
                                          key, keylen);
1252
0
}
1253
1254
int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
1255
0
{
1256
0
    OSSL_PARAM params[2], *p = params;
1257
1258
0
    if (ctx == NULL || op == NULL) {
1259
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1260
0
        return 0;
1261
0
    }
1262
0
    if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1263
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1264
0
        return -2;
1265
0
    }
1266
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
1267
0
                                            (char *)op, 0);
1268
0
    *p = OSSL_PARAM_construct_end();
1269
0
    return EVP_PKEY_CTX_set_params(ctx, params);
1270
0
}
1271
1272
int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
1273
0
{
1274
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1275
0
                             EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id));
1276
0
}
1277
1278
int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
1279
0
{
1280
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id);
1281
0
}
1282
1283
int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
1284
0
{
1285
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1286
0
                             EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len);
1287
0
}
1288
1289
static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1290
                                 int cmd, int p1, void *p2)
1291
59.7k
{
1292
59.7k
    int ret = 0;
1293
1294
    /*
1295
     * If the method has a |digest_custom| function, we can relax the
1296
     * operation type check, since this can be called before the operation
1297
     * is initialized.
1298
     */
1299
59.7k
    if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
1300
59.7k
        if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
1301
0
            ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
1302
0
            return -1;
1303
0
        }
1304
1305
59.7k
        if ((optype != -1) && !(ctx->operation & optype)) {
1306
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1307
0
            return -1;
1308
0
        }
1309
59.7k
    }
1310
1311
59.7k
    switch (evp_pkey_ctx_state(ctx)) {
1312
59.7k
    case EVP_PKEY_STATE_PROVIDER:
1313
59.7k
        return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
1314
0
    case EVP_PKEY_STATE_UNKNOWN:
1315
0
    case EVP_PKEY_STATE_LEGACY:
1316
0
        if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
1317
0
            ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1318
0
            return -2;
1319
0
        }
1320
0
        if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1321
0
            return -1;
1322
1323
0
        ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
1324
1325
0
        if (ret == -2)
1326
0
            ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1327
0
        break;
1328
59.7k
    }
1329
0
    return ret;
1330
59.7k
}
1331
1332
int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1333
                      int cmd, int p1, void *p2)
1334
59.7k
{
1335
59.7k
    int ret = 0;
1336
1337
59.7k
    if (ctx == NULL) {
1338
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1339
0
        return -2;
1340
0
    }
1341
    /* If unsupported, we don't want that reported here */
1342
59.7k
    ERR_set_mark();
1343
59.7k
    ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1344
59.7k
                                         cmd, NULL, p2, p1);
1345
59.7k
    if (ret == -2) {
1346
59.7k
        ERR_pop_to_mark();
1347
59.7k
    } else {
1348
0
        ERR_clear_last_mark();
1349
        /*
1350
         * If there was an error, there was an error.
1351
         * If the operation isn't initialized yet, we also return, as
1352
         * the saved values will be used then anyway.
1353
         */
1354
0
        if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1355
0
            return ret;
1356
0
    }
1357
59.7k
    return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
1358
59.7k
}
1359
1360
int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
1361
                             int cmd, uint64_t value)
1362
0
{
1363
0
    return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1364
0
}
1365
1366
1367
static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1368
                                     const char *name, const char *value)
1369
0
{
1370
0
    int ret = 0;
1371
1372
0
    if (ctx == NULL) {
1373
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1374
0
        return -2;
1375
0
    }
1376
1377
0
    switch (evp_pkey_ctx_state(ctx)) {
1378
0
    case EVP_PKEY_STATE_PROVIDER:
1379
0
        return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
1380
0
    case EVP_PKEY_STATE_UNKNOWN:
1381
0
    case EVP_PKEY_STATE_LEGACY:
1382
0
        if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
1383
0
            ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1384
0
            return -2;
1385
0
        }
1386
0
        if (strcmp(name, "digest") == 0)
1387
0
            ret = EVP_PKEY_CTX_md(ctx,
1388
0
                                  EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1389
0
                                  EVP_PKEY_CTRL_MD, value);
1390
0
        else
1391
0
            ret = ctx->pmeth->ctrl_str(ctx, name, value);
1392
0
        break;
1393
0
    }
1394
1395
0
    return ret;
1396
0
}
1397
1398
int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1399
                          const char *name, const char *value)
1400
0
{
1401
0
    int ret = 0;
1402
1403
    /* If unsupported, we don't want that reported here */
1404
0
    ERR_set_mark();
1405
0
    ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1406
0
                                         name, value, strlen(value) + 1);
1407
0
    if (ret == -2) {
1408
0
        ERR_pop_to_mark();
1409
0
    } else {
1410
0
        ERR_clear_last_mark();
1411
        /*
1412
         * If there was an error, there was an error.
1413
         * If the operation isn't initialized yet, we also return, as
1414
         * the saved values will be used then anyway.
1415
         */
1416
0
        if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1417
0
            return ret;
1418
0
    }
1419
1420
0
    return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1421
0
}
1422
1423
static int decode_cmd(int cmd, const char *name)
1424
5.29M
{
1425
5.29M
    if (cmd == -1) {
1426
        /*
1427
         * The consequence of the assertion not being true is that this
1428
         * function will return -1, which will cause the calling functions
1429
         * to signal that the command is unsupported...  in non-debug mode.
1430
         */
1431
0
        if (ossl_assert(name != NULL))
1432
0
            if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1433
0
                cmd = EVP_PKEY_CTRL_SET1_ID;
1434
0
    }
1435
1436
5.29M
    return cmd;
1437
5.29M
}
1438
1439
static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1440
                                          int keytype, int optype,
1441
                                          int cmd, const char *name,
1442
                                          const void *data, size_t data_len)
1443
59.7k
{
1444
    /*
1445
     * Check that it's one of the supported commands.  The ctrl commands
1446
     * number cases here must correspond to the cases in the bottom switch
1447
     * in this function.
1448
     */
1449
59.7k
    switch (cmd = decode_cmd(cmd, name)) {
1450
0
    case EVP_PKEY_CTRL_SET1_ID:
1451
0
        break;
1452
59.7k
    default:
1453
59.7k
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1454
59.7k
        return -2;
1455
59.7k
    }
1456
1457
0
    if (keytype != -1) {
1458
0
        switch (evp_pkey_ctx_state(ctx)) {
1459
0
        case EVP_PKEY_STATE_PROVIDER:
1460
0
            if (ctx->keymgmt == NULL) {
1461
0
                ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1462
0
                return -2;
1463
0
            }
1464
0
            if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
1465
0
                                  evp_pkey_type2name(keytype))) {
1466
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1467
0
                return -1;
1468
0
            }
1469
0
            break;
1470
0
        case EVP_PKEY_STATE_UNKNOWN:
1471
0
        case EVP_PKEY_STATE_LEGACY:
1472
0
            if (ctx->pmeth == NULL) {
1473
0
                ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1474
0
                return -2;
1475
0
            }
1476
0
            if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
1477
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1478
0
                return -1;
1479
0
            }
1480
0
            break;
1481
0
        }
1482
0
    }
1483
0
    if (optype != -1 && (ctx->operation & optype) == 0) {
1484
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1485
0
        return -1;
1486
0
    }
1487
1488
0
    switch (cmd) {
1489
0
    case EVP_PKEY_CTRL_SET1_ID:
1490
0
        evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1491
0
        if (name != NULL) {
1492
0
            ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
1493
0
            if (ctx->cached_parameters.dist_id_name == NULL)
1494
0
                return 0;
1495
0
        }
1496
0
        if (data_len > 0) {
1497
0
            ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
1498
0
            if (ctx->cached_parameters.dist_id == NULL)
1499
0
                return 0;
1500
0
        }
1501
0
        ctx->cached_parameters.dist_id_set = 1;
1502
0
        ctx->cached_parameters.dist_id_len = data_len;
1503
0
        break;
1504
0
    }
1505
0
    return 1;
1506
0
}
1507
1508
static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
1509
                                          int cmd, const char *name)
1510
5.23M
{
1511
5.23M
    cmd = decode_cmd(cmd, name);
1512
5.23M
    switch (cmd) {
1513
5.23M
    case EVP_PKEY_CTRL_SET1_ID:
1514
5.23M
        OPENSSL_free(ctx->cached_parameters.dist_id);
1515
5.23M
        OPENSSL_free(ctx->cached_parameters.dist_id_name);
1516
5.23M
        ctx->cached_parameters.dist_id = NULL;
1517
5.23M
        ctx->cached_parameters.dist_id_name = NULL;
1518
5.23M
        break;
1519
5.23M
    }
1520
5.23M
}
1521
1522
static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
1523
5.23M
{
1524
5.23M
    evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
1525
5.23M
}
1526
1527
int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
1528
98.3k
{
1529
98.3k
    int ret = 1;
1530
1531
98.3k
    if (ret && ctx->cached_parameters.dist_id_set) {
1532
0
        const char *name = ctx->cached_parameters.dist_id_name;
1533
0
        const void *val = ctx->cached_parameters.dist_id;
1534
0
        size_t len = ctx->cached_parameters.dist_id_len;
1535
1536
0
        if (name != NULL)
1537
0
            ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
1538
0
        else
1539
0
            ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
1540
0
                                        EVP_PKEY_CTRL_SET1_ID,
1541
0
                                        (int)len, (void *)val);
1542
0
    }
1543
1544
98.3k
    return ret;
1545
98.3k
}
1546
1547
OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
1548
0
{
1549
0
    return ctx->libctx;
1550
0
}
1551
1552
const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx)
1553
0
{
1554
0
    return ctx->propquery;
1555
0
}
1556
1557
const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx)
1558
0
{
1559
0
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1560
0
        if (ctx->op.sig.signature != NULL)
1561
0
            return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature);
1562
0
    } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1563
0
        if (ctx->op.kex.exchange != NULL)
1564
0
            return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange);
1565
0
    } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1566
0
        if (ctx->op.encap.kem != NULL)
1567
0
            return EVP_KEM_get0_provider(ctx->op.encap.kem);
1568
0
    } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1569
0
        if (ctx->op.ciph.cipher != NULL)
1570
0
            return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher);
1571
0
    } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1572
0
        if (ctx->keymgmt != NULL)
1573
0
            return EVP_KEYMGMT_get0_provider(ctx->keymgmt);
1574
0
    }
1575
1576
0
    return NULL;
1577
0
}
1578
1579
/* Utility functions to send a string of hex string to a ctrl */
1580
1581
int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1582
0
{
1583
0
    size_t len;
1584
1585
0
    len = strlen(str);
1586
0
    if (len > INT_MAX)
1587
0
        return -1;
1588
0
    return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1589
0
}
1590
1591
int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1592
0
{
1593
0
    unsigned char *bin;
1594
0
    long binlen;
1595
0
    int rv = -1;
1596
1597
0
    bin = OPENSSL_hexstr2buf(hex, &binlen);
1598
0
    if (bin == NULL)
1599
0
        return 0;
1600
0
    if (binlen <= INT_MAX)
1601
0
        rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1602
0
    OPENSSL_free(bin);
1603
0
    return rv;
1604
0
}
1605
1606
/* Pass a message digest to a ctrl */
1607
int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1608
0
{
1609
0
    const EVP_MD *m;
1610
1611
0
    if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1612
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
1613
0
        return 0;
1614
0
    }
1615
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1616
0
}
1617
1618
int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1619
0
{
1620
0
    return ctx->operation;
1621
0
}
1622
1623
void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1624
0
{
1625
0
    ctx->keygen_info = dat;
1626
0
    ctx->keygen_info_count = datlen;
1627
0
}
1628
1629
void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1630
0
{
1631
0
    ctx->data = data;
1632
0
}
1633
1634
void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1635
0
{
1636
0
    return ctx->data;
1637
0
}
1638
1639
EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1640
115k
{
1641
115k
    return ctx->pkey;
1642
115k
}
1643
1644
EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1645
0
{
1646
0
    return ctx->peerkey;
1647
0
}
1648
1649
void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1650
0
{
1651
0
    ctx->app_data = data;
1652
0
}
1653
1654
void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1655
0
{
1656
0
    return ctx->app_data;
1657
0
}
1658
1659
void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
1660
                            int (*init) (EVP_PKEY_CTX *ctx))
1661
0
{
1662
0
    pmeth->init = init;
1663
0
}
1664
1665
void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
1666
                            int (*copy) (EVP_PKEY_CTX *dst,
1667
                                         const EVP_PKEY_CTX *src))
1668
0
{
1669
0
    pmeth->copy = copy;
1670
0
}
1671
1672
void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1673
                               void (*cleanup) (EVP_PKEY_CTX *ctx))
1674
0
{
1675
0
    pmeth->cleanup = cleanup;
1676
0
}
1677
1678
void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1679
                                int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1680
                                int (*paramgen) (EVP_PKEY_CTX *ctx,
1681
                                                 EVP_PKEY *pkey))
1682
0
{
1683
0
    pmeth->paramgen_init = paramgen_init;
1684
0
    pmeth->paramgen = paramgen;
1685
0
}
1686
1687
void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1688
                              int (*keygen_init) (EVP_PKEY_CTX *ctx),
1689
                              int (*keygen) (EVP_PKEY_CTX *ctx,
1690
                                             EVP_PKEY *pkey))
1691
0
{
1692
0
    pmeth->keygen_init = keygen_init;
1693
0
    pmeth->keygen = keygen;
1694
0
}
1695
1696
void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1697
                            int (*sign_init) (EVP_PKEY_CTX *ctx),
1698
                            int (*sign) (EVP_PKEY_CTX *ctx,
1699
                                         unsigned char *sig, size_t *siglen,
1700
                                         const unsigned char *tbs,
1701
                                         size_t tbslen))
1702
0
{
1703
0
    pmeth->sign_init = sign_init;
1704
0
    pmeth->sign = sign;
1705
0
}
1706
1707
void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1708
                              int (*verify_init) (EVP_PKEY_CTX *ctx),
1709
                              int (*verify) (EVP_PKEY_CTX *ctx,
1710
                                             const unsigned char *sig,
1711
                                             size_t siglen,
1712
                                             const unsigned char *tbs,
1713
                                             size_t tbslen))
1714
0
{
1715
0
    pmeth->verify_init = verify_init;
1716
0
    pmeth->verify = verify;
1717
0
}
1718
1719
void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1720
                                      int (*verify_recover_init) (EVP_PKEY_CTX
1721
                                                                  *ctx),
1722
                                      int (*verify_recover) (EVP_PKEY_CTX
1723
                                                             *ctx,
1724
                                                             unsigned char
1725
                                                             *sig,
1726
                                                             size_t *siglen,
1727
                                                             const unsigned
1728
                                                             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
1921
                                                               char *tbs,
1922
                                                               size_t tbslen))
1923
0
{
1924
0
    if (pverify_recover_init)
1925
0
        *pverify_recover_init = pmeth->verify_recover_init;
1926
0
    if (pverify_recover)
1927
0
        *pverify_recover = pmeth->verify_recover;
1928
0
}
1929
1930
void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1931
                               int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1932
                                                      EVP_MD_CTX *mctx),
1933
                               int (**psignctx) (EVP_PKEY_CTX *ctx,
1934
                                                 unsigned char *sig,
1935
                                                 size_t *siglen,
1936
                                                 EVP_MD_CTX *mctx))
1937
0
{
1938
0
    if (psignctx_init)
1939
0
        *psignctx_init = pmeth->signctx_init;
1940
0
    if (psignctx)
1941
0
        *psignctx = pmeth->signctx;
1942
0
}
1943
1944
void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1945
                                 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1946
                                                          EVP_MD_CTX *mctx),
1947
                                 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1948
                                                     const unsigned char *sig,
1949
                                                     int siglen,
1950
                                                     EVP_MD_CTX *mctx))
1951
0
{
1952
0
    if (pverifyctx_init)
1953
0
        *pverifyctx_init = pmeth->verifyctx_init;
1954
0
    if (pverifyctx)
1955
0
        *pverifyctx = pmeth->verifyctx;
1956
0
}
1957
1958
void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1959
                               int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1960
                               int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1961
                                                   unsigned char *out,
1962
                                                   size_t *outlen,
1963
                                                   const unsigned char *in,
1964
                                                   size_t inlen))
1965
0
{
1966
0
    if (pencrypt_init)
1967
0
        *pencrypt_init = pmeth->encrypt_init;
1968
0
    if (pencryptfn)
1969
0
        *pencryptfn = pmeth->encrypt;
1970
0
}
1971
1972
void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1973
                               int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1974
                               int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1975
                                                 unsigned char *out,
1976
                                                 size_t *outlen,
1977
                                                 const unsigned char *in,
1978
                                                 size_t inlen))
1979
0
{
1980
0
    if (pdecrypt_init)
1981
0
        *pdecrypt_init = pmeth->decrypt_init;
1982
0
    if (pdecrypt)
1983
0
        *pdecrypt = pmeth->decrypt;
1984
0
}
1985
1986
void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1987
                              int (**pderive_init) (EVP_PKEY_CTX *ctx),
1988
                              int (**pderive) (EVP_PKEY_CTX *ctx,
1989
                                               unsigned char *key,
1990
                                               size_t *keylen))
1991
0
{
1992
0
    if (pderive_init)
1993
0
        *pderive_init = pmeth->derive_init;
1994
0
    if (pderive)
1995
0
        *pderive = pmeth->derive;
1996
0
}
1997
1998
void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1999
                            int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
2000
                                           void *p2),
2001
                            int (**pctrl_str) (EVP_PKEY_CTX *ctx,
2002
                                               const char *type,
2003
                                               const char *value))
2004
0
{
2005
0
    if (pctrl)
2006
0
        *pctrl = pmeth->ctrl;
2007
0
    if (pctrl_str)
2008
0
        *pctrl_str = pmeth->ctrl_str;
2009
0
}
2010
2011
void EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD *pmeth,
2012
    int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
2013
                        const unsigned char *tbs, size_t tbslen))
2014
0
{
2015
0
    if (digestsign)
2016
0
        *digestsign = pmeth->digestsign;
2017
0
}
2018
2019
void EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD *pmeth,
2020
    int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
2021
                          size_t siglen, const unsigned char *tbs,
2022
                          size_t tbslen))
2023
0
{
2024
0
    if (digestverify)
2025
0
        *digestverify = pmeth->digestverify;
2026
0
}
2027
2028
void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2029
                             int (**pcheck) (EVP_PKEY *pkey))
2030
0
{
2031
0
    if (pcheck != NULL)
2032
0
        *pcheck = pmeth->check;
2033
0
}
2034
2035
void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
2036
                                    int (**pcheck) (EVP_PKEY *pkey))
2037
0
{
2038
0
    if (pcheck != NULL)
2039
0
        *pcheck = pmeth->public_check;
2040
0
}
2041
2042
void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
2043
                                   int (**pcheck) (EVP_PKEY *pkey))
2044
0
{
2045
0
    if (pcheck != NULL)
2046
0
        *pcheck = pmeth->param_check;
2047
0
}
2048
2049
void EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD *pmeth,
2050
                                     int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
2051
                                                             EVP_MD_CTX *mctx))
2052
0
{
2053
0
    if (pdigest_custom != NULL)
2054
0
        *pdigest_custom = pmeth->digest_custom;
2055
0
}
2056
2057
#endif /* FIPS_MODULE */