Coverage Report

Created: 2025-04-22 06:18

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