Coverage Report

Created: 2024-07-27 06:39

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