Coverage Report

Created: 2023-06-07 07:24

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