Coverage Report

Created: 2025-06-13 06:58

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