Coverage Report

Created: 2024-11-21 07:03

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