Coverage Report

Created: 2025-07-23 06:49

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