Coverage Report

Created: 2025-08-28 07:07

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