Coverage Report

Created: 2025-12-31 06:58

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