Coverage Report

Created: 2025-12-10 06:24

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