Coverage Report

Created: 2025-12-31 06:58

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