Coverage Report

Created: 2025-06-13 06:58

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