Coverage Report

Created: 2023-06-08 06:41

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