Coverage Report

Created: 2025-10-10 07:01

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