Coverage Report

Created: 2026-11-16 07:12

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-2026 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
#endif /* FIPS_MODULE */
47
48
int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx)
49
0
{
50
0
    if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
51
0
        return EVP_PKEY_STATE_UNKNOWN;
52
53
0
    if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
54
0
            && ctx->op.kex.algctx != NULL)
55
0
        || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
56
0
            && ctx->op.sig.algctx != NULL)
57
0
        || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
58
0
            && ctx->op.ciph.algctx != NULL)
59
0
        || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
60
0
            && ctx->op.keymgmt.genctx != NULL)
61
0
        || (EVP_PKEY_CTX_IS_KEM_OP(ctx)
62
0
            && ctx->op.encap.algctx != NULL))
63
0
        return EVP_PKEY_STATE_PROVIDER;
64
65
0
    return EVP_PKEY_STATE_LEGACY;
66
0
}
67
68
static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey,
69
    const char *keytype, const char *propquery,
70
    int id)
71
0
{
72
0
    EVP_PKEY_CTX *ret = NULL;
73
0
    EVP_KEYMGMT *keymgmt = NULL;
74
75
    /* Code below to be removed when legacy support is dropped. */
76
    /* BEGIN legacy */
77
0
    if (id == -1) {
78
0
        if (pkey != NULL && !evp_pkey_is_provided(pkey)) {
79
0
            id = pkey->type;
80
0
        } else {
81
0
            if (pkey != NULL) {
82
                /* Must be provided if we get here */
83
0
                keytype = EVP_KEYMGMT_get0_name(pkey->keymgmt);
84
0
            }
85
0
#ifndef FIPS_MODULE
86
0
            if (keytype != NULL) {
87
0
                id = evp_pkey_name2type(keytype);
88
0
                if (id == NID_undef)
89
0
                    id = -1;
90
0
            }
91
0
#endif
92
0
        }
93
0
    }
94
95
0
#ifndef FIPS_MODULE
96
    /*
97
     * Here, we extract what information we can for the purpose of
98
     * supporting usage with implementations from providers, to make
99
     * for a smooth transition from legacy stuff to provider based stuff.
100
     */
101
0
    if (id != -1)
102
0
        keytype = OBJ_nid2sn(id);
103
104
    /* END legacy */
105
0
#endif /* FIPS_MODULE */
106
    /* We try fetching a provider implementation. */
107
0
    if (keytype != NULL) {
108
        /*
109
         * If |pkey| is given and is provided, we take a reference to its
110
         * keymgmt.  Otherwise, we fetch one for the keytype we got. This
111
         * is to ensure that operation init functions can access what they
112
         * need through this single pointer.
113
         */
114
0
        if (pkey != NULL && pkey->keymgmt != NULL) {
115
0
            if (!EVP_KEYMGMT_up_ref(pkey->keymgmt))
116
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
117
0
            else
118
0
                keymgmt = pkey->keymgmt;
119
0
        } else {
120
0
            keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
121
0
        }
122
0
        if (keymgmt == NULL)
123
0
            return NULL; /* EVP_KEYMGMT_fetch() recorded an error */
124
125
0
#ifndef FIPS_MODULE
126
        /*
127
         * Chase down the legacy NID, as that might be needed for diverse
128
         * purposes, such as ensure that EVP_PKEY_type() can return sensible
129
         * values. We go through all keymgmt names, because the keytype
130
         * that's passed to this function doesn't necessarily translate
131
         * directly.
132
         */
133
0
        if (keymgmt != NULL) {
134
0
            int tmp_id = evp_keymgmt_get_legacy_alg(keymgmt);
135
136
0
            if (tmp_id != NID_undef) {
137
0
                if (id == -1) {
138
0
                    id = tmp_id;
139
0
                } else {
140
                    /*
141
                     * It really really shouldn't differ.  If it still does,
142
                     * something is very wrong.
143
                     */
144
0
                    if (!ossl_assert(id == tmp_id)) {
145
0
                        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
146
0
                        EVP_KEYMGMT_free(keymgmt);
147
0
                        return NULL;
148
0
                    }
149
0
                }
150
0
            }
151
0
        }
152
0
#endif
153
0
    }
154
155
0
    if (keymgmt == NULL) {
156
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
157
0
    } else {
158
0
        ret = OPENSSL_zalloc(sizeof(*ret));
159
0
    }
160
161
0
    if (ret == NULL) {
162
0
        EVP_KEYMGMT_free(keymgmt);
163
0
        return NULL;
164
0
    }
165
0
    if (propquery != NULL) {
166
0
        ret->propquery = OPENSSL_strdup(propquery);
167
0
        if (ret->propquery == NULL) {
168
0
            OPENSSL_free(ret);
169
0
            EVP_KEYMGMT_free(keymgmt);
170
0
            return NULL;
171
0
        }
172
0
    }
173
0
    ret->libctx = libctx;
174
0
    ret->keytype = keytype;
175
0
    ret->keymgmt = keymgmt;
176
0
    ret->legacy_keytype = id;
177
0
    ret->operation = EVP_PKEY_OP_UNDEFINED;
178
179
0
    if (pkey != NULL && !EVP_PKEY_up_ref(pkey)) {
180
0
        EVP_PKEY_CTX_free(ret);
181
0
        return NULL;
182
0
    }
183
184
0
    ret->pkey = pkey;
185
186
0
    return ret;
187
0
}
188
189
/*- All methods below can also be used in FIPS_MODULE */
190
191
EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx,
192
    const char *name,
193
    const char *propquery)
194
0
{
195
0
    return int_ctx_new(libctx, NULL, name, propquery, -1);
196
0
}
197
198
EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey,
199
    const char *propquery)
200
0
{
201
0
    return int_ctx_new(libctx, pkey, NULL, propquery, -1);
202
0
}
203
204
void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
205
0
{
206
0
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
207
0
        if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL)
208
0
            ctx->op.sig.signature->freectx(ctx->op.sig.algctx);
209
0
        EVP_SIGNATURE_free(ctx->op.sig.signature);
210
0
        ctx->op.sig.algctx = NULL;
211
0
        ctx->op.sig.signature = NULL;
212
0
    } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
213
0
        if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL)
214
0
            ctx->op.kex.exchange->freectx(ctx->op.kex.algctx);
215
0
        EVP_KEYEXCH_free(ctx->op.kex.exchange);
216
0
        ctx->op.kex.algctx = NULL;
217
0
        ctx->op.kex.exchange = NULL;
218
0
    } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
219
0
        if (ctx->op.encap.algctx != NULL && ctx->op.encap.kem != NULL)
220
0
            ctx->op.encap.kem->freectx(ctx->op.encap.algctx);
221
0
        EVP_KEM_free(ctx->op.encap.kem);
222
0
        ctx->op.encap.algctx = NULL;
223
0
        ctx->op.encap.kem = NULL;
224
0
    } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
225
0
        if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL)
226
0
            ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx);
227
0
        EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
228
0
        ctx->op.ciph.algctx = NULL;
229
0
        ctx->op.ciph.cipher = NULL;
230
0
    } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
231
0
        if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
232
0
            evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
233
0
        ctx->op.keymgmt.genctx = NULL;
234
0
    }
235
0
}
236
237
void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
238
81.4k
{
239
81.4k
    if (ctx == NULL)
240
81.4k
        return;
241
242
0
    evp_pkey_ctx_free_old_ops(ctx);
243
0
#ifndef FIPS_MODULE
244
0
    evp_pkey_ctx_free_all_cached_data(ctx);
245
0
#endif
246
0
    EVP_KEYMGMT_free(ctx->keymgmt);
247
248
0
    OPENSSL_free(ctx->propquery);
249
0
    EVP_PKEY_free(ctx->pkey);
250
0
    EVP_PKEY_free(ctx->peerkey);
251
0
    BN_free(ctx->rsa_pubexp);
252
0
    OPENSSL_free(ctx);
253
0
}
254
255
#ifndef FIPS_MODULE
256
EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
257
0
{
258
0
    if (!ossl_assert(e == NULL))
259
0
        return NULL;
260
0
    return int_ctx_new(NULL, pkey, NULL, NULL, -1);
261
0
}
262
263
EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
264
0
{
265
0
    if (!ossl_assert(e == NULL))
266
0
        return NULL;
267
0
    return int_ctx_new(NULL, NULL, NULL, NULL, id);
268
0
}
269
270
EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
271
0
{
272
0
    EVP_PKEY_CTX *rctx;
273
274
0
    rctx = OPENSSL_zalloc(sizeof(*rctx));
275
0
    if (rctx == NULL)
276
0
        return NULL;
277
278
0
    if (pctx->pkey != NULL && !EVP_PKEY_up_ref(pctx->pkey))
279
0
        goto err;
280
281
0
    rctx->pkey = pctx->pkey;
282
0
    rctx->operation = pctx->operation;
283
0
    rctx->libctx = pctx->libctx;
284
0
    rctx->keytype = pctx->keytype;
285
0
    rctx->propquery = NULL;
286
0
    if (pctx->propquery != NULL) {
287
0
        rctx->propquery = OPENSSL_strdup(pctx->propquery);
288
0
        if (rctx->propquery == NULL)
289
0
            goto err;
290
0
    }
291
0
    rctx->legacy_keytype = pctx->legacy_keytype;
292
293
0
    if (pctx->keymgmt != NULL) {
294
0
        if (!EVP_KEYMGMT_up_ref(pctx->keymgmt))
295
0
            goto err;
296
0
        rctx->keymgmt = pctx->keymgmt;
297
0
    }
298
299
0
    if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
300
0
        if (pctx->op.kex.exchange != NULL) {
301
0
            rctx->op.kex.exchange = pctx->op.kex.exchange;
302
0
            if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange))
303
0
                goto err;
304
0
        }
305
0
        if (pctx->op.kex.algctx != NULL) {
306
0
            if (!ossl_assert(pctx->op.kex.exchange != NULL))
307
0
                goto err;
308
309
0
            if (pctx->op.kex.exchange->dupctx != NULL)
310
0
                rctx->op.kex.algctx
311
0
                    = pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx);
312
313
0
            if (rctx->op.kex.algctx == NULL) {
314
0
                EVP_KEYEXCH_free(rctx->op.kex.exchange);
315
0
                rctx->op.kex.exchange = NULL;
316
0
                goto err;
317
0
            }
318
0
            return rctx;
319
0
        }
320
0
    } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
321
0
        if (pctx->op.sig.signature != NULL) {
322
0
            rctx->op.sig.signature = pctx->op.sig.signature;
323
0
            if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
324
0
                goto err;
325
0
        }
326
0
        if (pctx->op.sig.algctx != NULL) {
327
0
            if (!ossl_assert(pctx->op.sig.signature != NULL))
328
0
                goto err;
329
330
0
            if (pctx->op.sig.signature->dupctx != NULL)
331
0
                rctx->op.sig.algctx
332
0
                    = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx);
333
334
0
            if (rctx->op.sig.algctx == NULL) {
335
0
                EVP_SIGNATURE_free(rctx->op.sig.signature);
336
0
                rctx->op.sig.signature = NULL;
337
0
                goto err;
338
0
            }
339
0
            return rctx;
340
0
        }
341
0
    } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
342
0
        if (pctx->op.ciph.cipher != NULL) {
343
0
            rctx->op.ciph.cipher = pctx->op.ciph.cipher;
344
0
            if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
345
0
                goto err;
346
0
        }
347
0
        if (pctx->op.ciph.algctx != NULL) {
348
0
            if (!ossl_assert(pctx->op.ciph.cipher != NULL))
349
0
                goto err;
350
351
0
            if (pctx->op.ciph.cipher->dupctx != NULL)
352
0
                rctx->op.ciph.algctx
353
0
                    = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx);
354
355
0
            if (rctx->op.ciph.algctx == NULL) {
356
0
                EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
357
0
                rctx->op.ciph.cipher = NULL;
358
0
                goto err;
359
0
            }
360
0
            return rctx;
361
0
        }
362
0
    } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) {
363
0
        if (pctx->op.encap.kem != NULL) {
364
0
            rctx->op.encap.kem = pctx->op.encap.kem;
365
0
            if (!EVP_KEM_up_ref(rctx->op.encap.kem))
366
0
                goto err;
367
0
        }
368
0
        if (pctx->op.encap.algctx != NULL) {
369
0
            if (!ossl_assert(pctx->op.encap.kem != NULL))
370
0
                goto err;
371
372
0
            if (pctx->op.encap.kem->dupctx != NULL)
373
0
                rctx->op.encap.algctx
374
0
                    = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx);
375
376
0
            if (rctx->op.encap.algctx == NULL) {
377
0
                EVP_KEM_free(rctx->op.encap.kem);
378
0
                rctx->op.encap.kem = NULL;
379
0
                goto err;
380
0
            }
381
0
            return rctx;
382
0
        }
383
0
    } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
384
        /* Not supported - This would need a gen_dupctx() to work */
385
0
        goto err;
386
0
    }
387
388
0
    if (pctx->peerkey != NULL && !EVP_PKEY_up_ref(pctx->peerkey))
389
0
        goto err;
390
391
0
    rctx->peerkey = pctx->peerkey;
392
393
0
    if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
394
0
        EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
395
0
        void *provkey;
396
397
0
        if (pctx->pkey == NULL)
398
0
            return rctx;
399
400
0
        provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
401
0
            &tmp_keymgmt, pctx->propquery);
402
0
        if (provkey == NULL)
403
0
            goto err;
404
0
        if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
405
0
            goto err;
406
0
        EVP_KEYMGMT_free(rctx->keymgmt);
407
0
        rctx->keymgmt = tmp_keymgmt;
408
0
        return rctx;
409
0
    }
410
0
err:
411
0
    EVP_PKEY_CTX_free(rctx);
412
0
    return NULL;
413
0
}
414
#endif
415
416
int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype)
417
0
{
418
0
#ifndef FIPS_MODULE
419
0
    if (evp_pkey_ctx_is_legacy(ctx))
420
0
        return (ctx->legacy_keytype == evp_pkey_name2type(keytype));
421
0
#endif
422
0
    return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype);
423
0
}
424
425
int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
426
0
{
427
0
    switch (evp_pkey_ctx_state(ctx)) {
428
0
    case EVP_PKEY_STATE_PROVIDER:
429
0
        if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
430
0
            && ctx->op.kex.exchange != NULL
431
0
            && ctx->op.kex.exchange->set_ctx_params != NULL)
432
0
            return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx,
433
0
                params);
434
0
        if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
435
0
            && ctx->op.sig.signature != NULL
436
0
            && ctx->op.sig.signature->set_ctx_params != NULL)
437
0
            return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx,
438
0
                params);
439
0
        if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
440
0
            && ctx->op.ciph.cipher != NULL
441
0
            && ctx->op.ciph.cipher->set_ctx_params != NULL)
442
0
            return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx,
443
0
                params);
444
0
        if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
445
0
            && ctx->keymgmt != NULL
446
0
            && ctx->keymgmt->gen_set_params != NULL)
447
0
            return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
448
0
                params);
449
0
        if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
450
0
            && ctx->op.encap.kem != NULL
451
0
            && ctx->op.encap.kem->set_ctx_params != NULL)
452
0
            return ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx,
453
0
                params);
454
0
        break;
455
0
    case EVP_PKEY_STATE_UNKNOWN:
456
0
        break;
457
0
#ifndef FIPS_MODULE
458
0
    case EVP_PKEY_STATE_LEGACY:
459
0
        return evp_pkey_ctx_set_params_to_ctrl(ctx, params);
460
0
#endif
461
0
    }
462
0
    return 0;
463
0
}
464
465
int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
466
0
{
467
0
    switch (evp_pkey_ctx_state(ctx)) {
468
0
    case EVP_PKEY_STATE_PROVIDER:
469
0
        if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
470
0
            && ctx->op.kex.exchange != NULL
471
0
            && ctx->op.kex.exchange->get_ctx_params != NULL)
472
0
            return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx,
473
0
                params);
474
0
        if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
475
0
            && ctx->op.sig.signature != NULL
476
0
            && ctx->op.sig.signature->get_ctx_params != NULL)
477
0
            return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx,
478
0
                params);
479
0
        if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
480
0
            && ctx->op.ciph.cipher != NULL
481
0
            && ctx->op.ciph.cipher->get_ctx_params != NULL)
482
0
            return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx,
483
0
                params);
484
0
        if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
485
0
            && ctx->op.encap.kem != NULL
486
0
            && ctx->op.encap.kem->get_ctx_params != NULL)
487
0
            return ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx,
488
0
                params);
489
0
        if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
490
0
            && ctx->keymgmt != NULL
491
0
            && ctx->keymgmt->gen_get_params != NULL)
492
0
            return evp_keymgmt_gen_get_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
493
0
                params);
494
0
        break;
495
0
    case EVP_PKEY_STATE_UNKNOWN:
496
0
        break;
497
0
#ifndef FIPS_MODULE
498
0
    case EVP_PKEY_STATE_LEGACY:
499
0
        return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
500
0
#endif
501
0
    }
502
0
    ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_GET_CTX_PARAMS_NOT_SUPPORTED,
503
0
        "EVP_PKEY_OP=0x%x", ctx->operation);
504
0
    return 0;
505
0
}
506
507
#ifndef FIPS_MODULE
508
const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx)
509
0
{
510
0
    void *provctx;
511
512
0
    if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
513
0
        && ctx->op.kex.exchange != NULL
514
0
        && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
515
0
        provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
516
0
        return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx,
517
0
            provctx);
518
0
    }
519
0
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
520
0
        && ctx->op.sig.signature != NULL
521
0
        && ctx->op.sig.signature->gettable_ctx_params != NULL) {
522
0
        provctx = ossl_provider_ctx(
523
0
            EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
524
0
        return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx,
525
0
            provctx);
526
0
    }
527
0
    if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
528
0
        && ctx->op.ciph.cipher != NULL
529
0
        && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
530
0
        provctx = ossl_provider_ctx(
531
0
            EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
532
0
        return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx,
533
0
            provctx);
534
0
    }
535
0
    if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
536
0
        && ctx->op.encap.kem != NULL
537
0
        && ctx->op.encap.kem->gettable_ctx_params != NULL) {
538
0
        provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
539
0
        return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx,
540
0
            provctx);
541
0
    }
542
0
    if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
543
0
        && ctx->keymgmt != NULL
544
0
        && ctx->keymgmt->gen_gettable_params != NULL) {
545
0
        provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
546
0
        return ctx->keymgmt->gen_gettable_params(ctx->op.keymgmt.genctx,
547
0
            provctx);
548
0
    }
549
0
    return NULL;
550
0
}
551
552
const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx)
553
0
{
554
0
    void *provctx;
555
556
0
    if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
557
0
        && ctx->op.kex.exchange != NULL
558
0
        && ctx->op.kex.exchange->settable_ctx_params != NULL) {
559
0
        provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
560
0
        return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx,
561
0
            provctx);
562
0
    }
563
0
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
564
0
        && ctx->op.sig.signature != NULL
565
0
        && ctx->op.sig.signature->settable_ctx_params != NULL) {
566
0
        provctx = ossl_provider_ctx(
567
0
            EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
568
0
        return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx,
569
0
            provctx);
570
0
    }
571
0
    if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
572
0
        && ctx->op.ciph.cipher != NULL
573
0
        && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
574
0
        provctx = ossl_provider_ctx(
575
0
            EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
576
0
        return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx,
577
0
            provctx);
578
0
    }
579
0
    if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
580
0
        && ctx->keymgmt != NULL
581
0
        && ctx->keymgmt->gen_settable_params != NULL) {
582
0
        provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
583
0
        return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx,
584
0
            provctx);
585
0
    }
586
0
    if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
587
0
        && ctx->op.encap.kem != NULL
588
0
        && ctx->op.encap.kem->settable_ctx_params != NULL) {
589
0
        provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
590
0
        return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx,
591
0
            provctx);
592
0
    }
593
0
    return NULL;
594
0
}
595
596
/*
597
 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
598
 *
599
 * Return 1 on success, 0 or negative for errors.
600
 *
601
 * In particular they return -2 if any of the params is not supported.
602
 *
603
 * They are not available in FIPS_MODULE as they depend on
604
 *      - EVP_PKEY_CTX_{get,set}_params()
605
 *      - EVP_PKEY_CTX_{gettable,settable}_params()
606
 *
607
 */
608
int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
609
0
{
610
0
    if (ctx == NULL || params == NULL)
611
0
        return 0;
612
613
    /*
614
     * We only check for provider side EVP_PKEY_CTX.  For #legacy, we
615
     * depend on the translation that happens in EVP_PKEY_CTX_set_params()
616
     * call, and that the resulting ctrl call will return -2 if it doesn't
617
     * known the ctrl command number.
618
     */
619
0
    if (evp_pkey_ctx_is_provided(ctx)) {
620
0
        const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
621
0
        const OSSL_PARAM *p;
622
623
0
        for (p = params; p->key != NULL; p++) {
624
            /* Check the ctx actually understands this parameter */
625
0
            if (OSSL_PARAM_locate_const(settable, p->key) == NULL)
626
0
                return -2;
627
0
        }
628
0
    }
629
630
0
    return EVP_PKEY_CTX_set_params(ctx, params);
631
0
}
632
633
int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
634
0
{
635
0
    if (ctx == NULL || params == NULL)
636
0
        return 0;
637
638
    /*
639
     * We only check for provider side EVP_PKEY_CTX.  For #legacy, we
640
     * depend on the translation that happens in EVP_PKEY_CTX_get_params()
641
     * call, and that the resulting ctrl call will return -2 if it doesn't
642
     * known the ctrl command number.
643
     */
644
0
    if (evp_pkey_ctx_is_provided(ctx)) {
645
0
        const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
646
0
        const OSSL_PARAM *p;
647
648
0
        for (p = params; p->key != NULL; p++) {
649
            /* Check the ctx actually understands this parameter */
650
0
            if (OSSL_PARAM_locate_const(gettable, p->key) == NULL)
651
0
                return -2;
652
0
        }
653
0
    }
654
655
0
    return EVP_PKEY_CTX_get_params(ctx, params);
656
0
}
657
658
int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
659
0
{
660
0
    OSSL_PARAM sig_md_params[2], *p = sig_md_params;
661
    /* 80 should be big enough */
662
0
    char name[80] = "";
663
0
    const EVP_MD *tmp;
664
665
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
666
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
667
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
668
0
        return -2;
669
0
    }
670
671
0
    if (ctx->op.sig.algctx == NULL)
672
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
673
0
            EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
674
675
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
676
0
        name,
677
0
        sizeof(name));
678
0
    *p = OSSL_PARAM_construct_end();
679
680
0
    if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
681
0
        return 0;
682
683
0
    tmp = evp_get_digestbyname_ex(ctx->libctx, name);
684
0
    if (tmp == NULL)
685
0
        return 0;
686
687
0
    *md = tmp;
688
689
0
    return 1;
690
0
}
691
692
static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
693
    int fallback, const char *param, int op,
694
    int ctrl)
695
0
{
696
0
    OSSL_PARAM md_params[2], *p = md_params;
697
0
    const char *name;
698
699
0
    if (ctx == NULL || (ctx->operation & op) == 0) {
700
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
701
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
702
0
        return -2;
703
0
    }
704
705
0
    if (fallback)
706
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
707
708
0
    if (md == NULL) {
709
0
        name = "";
710
0
    } else {
711
0
        name = EVP_MD_get0_name(md);
712
0
    }
713
714
0
    *p++ = OSSL_PARAM_construct_utf8_string(param,
715
        /*
716
         * Cast away the const. This is read
717
         * only so should be safe
718
         */
719
0
        (char *)name, 0);
720
0
    *p = OSSL_PARAM_construct_end();
721
722
0
    return EVP_PKEY_CTX_set_params(ctx, md_params);
723
0
}
724
725
int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
726
0
{
727
0
    return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL,
728
0
        OSSL_SIGNATURE_PARAM_DIGEST,
729
0
        EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
730
0
}
731
732
int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
733
0
{
734
0
    return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
735
0
        OSSL_KDF_PARAM_DIGEST,
736
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
737
0
}
738
739
static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
740
    const char *param, int op, int ctrl,
741
    const unsigned char *data,
742
    int datalen)
743
0
{
744
0
    OSSL_PARAM octet_string_params[2], *p = octet_string_params;
745
746
0
    if (ctx == NULL || (ctx->operation & op) == 0) {
747
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
748
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
749
0
        return -2;
750
0
    }
751
752
    /* Code below to be removed when legacy support is dropped. */
753
0
    if (fallback)
754
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
755
    /* end of legacy support */
756
757
0
    if (datalen < 0) {
758
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
759
0
        return 0;
760
0
    }
761
762
0
    *p++ = OSSL_PARAM_construct_octet_string(param,
763
        /*
764
         * Cast away the const. This is read
765
         * only so should be safe
766
         */
767
0
        (unsigned char *)data,
768
0
        (size_t)datalen);
769
0
    *p = OSSL_PARAM_construct_end();
770
771
0
    return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
772
0
}
773
774
static int evp_pkey_ctx_add1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
775
    const char *param, int op, int ctrl,
776
    const unsigned char *data,
777
    int datalen)
778
0
{
779
0
    OSSL_PARAM os_params[2];
780
0
    const OSSL_PARAM *gettables;
781
0
    unsigned char *info = NULL;
782
0
    size_t info_len = 0;
783
0
    size_t info_alloc = 0;
784
0
    int ret = 0;
785
786
0
    if (ctx == NULL || (ctx->operation & op) == 0) {
787
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
788
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
789
0
        return -2;
790
0
    }
791
792
    /* Code below to be removed when legacy support is dropped. */
793
0
    if (fallback)
794
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
795
    /* end of legacy support */
796
797
0
    if (datalen < 0) {
798
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
799
0
        return 0;
800
0
    } else if (datalen == 0) {
801
0
        return 1;
802
0
    }
803
804
    /* Check for older provider that doesn't support getting this parameter */
805
0
    gettables = EVP_PKEY_CTX_gettable_params(ctx);
806
0
    if (gettables == NULL || OSSL_PARAM_locate_const(gettables, param) == NULL)
807
0
        return evp_pkey_ctx_set1_octet_string(ctx, fallback, param, op, ctrl,
808
0
            data, datalen);
809
810
    /* Get the original value length */
811
0
    os_params[0] = OSSL_PARAM_construct_octet_string(param, NULL, 0);
812
0
    os_params[1] = OSSL_PARAM_construct_end();
813
814
0
    if (!EVP_PKEY_CTX_get_params(ctx, os_params))
815
0
        return 0;
816
817
    /* This should not happen but check to be sure. */
818
0
    if (os_params[0].return_size == OSSL_PARAM_UNMODIFIED)
819
0
        return 0;
820
821
0
    info_alloc = os_params[0].return_size + datalen;
822
0
    if (info_alloc == 0)
823
0
        return 0;
824
0
    info = OPENSSL_zalloc(info_alloc);
825
0
    if (info == NULL)
826
0
        return 0;
827
0
    info_len = os_params[0].return_size;
828
829
0
    os_params[0] = OSSL_PARAM_construct_octet_string(param, info, info_alloc);
830
831
    /* if we have data, then go get it */
832
0
    if (info_len > 0) {
833
0
        if (!EVP_PKEY_CTX_get_params(ctx, os_params))
834
0
            goto error;
835
0
    }
836
837
    /* Copy the input data */
838
0
    memcpy(&info[info_len], data, datalen);
839
0
    ret = EVP_PKEY_CTX_set_params(ctx, os_params);
840
841
0
error:
842
0
    OPENSSL_clear_free(info, info_alloc);
843
0
    return ret;
844
0
}
845
846
int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
847
    const unsigned char *sec, int seclen)
848
0
{
849
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
850
0
        OSSL_KDF_PARAM_SECRET,
851
0
        EVP_PKEY_OP_DERIVE,
852
0
        EVP_PKEY_CTRL_TLS_SECRET,
853
0
        sec, seclen);
854
0
}
855
856
int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
857
    const unsigned char *seed, int seedlen)
858
0
{
859
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
860
0
        OSSL_KDF_PARAM_SEED,
861
0
        EVP_PKEY_OP_DERIVE,
862
0
        EVP_PKEY_CTRL_TLS_SEED,
863
0
        seed, seedlen);
864
0
}
865
866
int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
867
0
{
868
0
    return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
869
0
        OSSL_KDF_PARAM_DIGEST,
870
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
871
0
}
872
873
int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
874
    const unsigned char *salt, int saltlen)
875
0
{
876
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
877
0
        OSSL_KDF_PARAM_SALT,
878
0
        EVP_PKEY_OP_DERIVE,
879
0
        EVP_PKEY_CTRL_HKDF_SALT,
880
0
        salt, saltlen);
881
0
}
882
883
int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
884
    const unsigned char *key, int keylen)
885
0
{
886
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
887
0
        OSSL_KDF_PARAM_KEY,
888
0
        EVP_PKEY_OP_DERIVE,
889
0
        EVP_PKEY_CTRL_HKDF_KEY,
890
0
        key, keylen);
891
0
}
892
893
int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
894
    const unsigned char *info, int infolen)
895
0
{
896
0
    return evp_pkey_ctx_add1_octet_string(ctx, ctx->op.kex.algctx == NULL,
897
0
        OSSL_KDF_PARAM_INFO,
898
0
        EVP_PKEY_OP_DERIVE,
899
0
        EVP_PKEY_CTRL_HKDF_INFO,
900
0
        info, infolen);
901
0
}
902
903
int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
904
0
{
905
0
    OSSL_PARAM int_params[2], *p = int_params;
906
907
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
908
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
909
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
910
0
        return -2;
911
0
    }
912
913
    /* Code below to be removed when legacy support is dropped. */
914
0
    if (ctx->op.kex.algctx == NULL)
915
0
        return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
916
0
            EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
917
    /* end of legacy support */
918
919
0
    if (mode < 0) {
920
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
921
0
        return 0;
922
0
    }
923
924
0
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
925
0
    *p = OSSL_PARAM_construct_end();
926
927
0
    return EVP_PKEY_CTX_set_params(ctx, int_params);
928
0
}
929
930
int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
931
    int passlen)
932
0
{
933
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
934
0
        OSSL_KDF_PARAM_PASSWORD,
935
0
        EVP_PKEY_OP_DERIVE,
936
0
        EVP_PKEY_CTRL_PASS,
937
0
        (const unsigned char *)pass, passlen);
938
0
}
939
940
int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
941
    const unsigned char *salt, int saltlen)
942
0
{
943
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
944
0
        OSSL_KDF_PARAM_SALT,
945
0
        EVP_PKEY_OP_DERIVE,
946
0
        EVP_PKEY_CTRL_SCRYPT_SALT,
947
0
        salt, saltlen);
948
0
}
949
950
static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
951
    int op, int ctrl, uint64_t val)
952
0
{
953
0
    OSSL_PARAM uint64_params[2], *p = uint64_params;
954
955
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
956
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
957
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
958
0
        return -2;
959
0
    }
960
961
    /* Code below to be removed when legacy support is dropped. */
962
0
    if (ctx->op.kex.algctx == NULL)
963
0
        return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
964
    /* end of legacy support */
965
966
0
    *p++ = OSSL_PARAM_construct_uint64(param, &val);
967
0
    *p = OSSL_PARAM_construct_end();
968
969
0
    return EVP_PKEY_CTX_set_params(ctx, uint64_params);
970
0
}
971
972
int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
973
0
{
974
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
975
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
976
0
        n);
977
0
}
978
979
int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
980
0
{
981
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
982
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
983
0
        r);
984
0
}
985
986
int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
987
0
{
988
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
989
0
        EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
990
0
        p);
991
0
}
992
993
int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
994
    uint64_t maxmem_bytes)
995
0
{
996
0
    return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
997
0
        EVP_PKEY_OP_DERIVE,
998
0
        EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
999
0
        maxmem_bytes);
1000
0
}
1001
1002
int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1003
    int keylen)
1004
0
{
1005
0
    return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1006
0
        OSSL_PKEY_PARAM_PRIV_KEY,
1007
0
        EVP_PKEY_OP_KEYGEN,
1008
0
        EVP_PKEY_CTRL_SET_MAC_KEY,
1009
0
        key, keylen);
1010
0
}
1011
1012
int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
1013
0
{
1014
0
    OSSL_PARAM params[2], *p = params;
1015
1016
0
    if (ctx == NULL || op == NULL) {
1017
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1018
0
        return 0;
1019
0
    }
1020
0
    if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1021
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1022
0
        return -2;
1023
0
    }
1024
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
1025
0
        (char *)op, 0);
1026
0
    *p = OSSL_PARAM_construct_end();
1027
0
    return EVP_PKEY_CTX_set_params(ctx, params);
1028
0
}
1029
1030
int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
1031
0
{
1032
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1033
0
        EVP_PKEY_CTRL_SET1_ID, (int)len, (void *)(id));
1034
0
}
1035
1036
int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
1037
0
{
1038
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void *)id);
1039
0
}
1040
1041
int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
1042
0
{
1043
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1044
0
        EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void *)id_len);
1045
0
}
1046
1047
static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1048
    int cmd, int p1, void *p2)
1049
0
{
1050
0
    int ret = 0;
1051
1052
0
    if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
1053
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
1054
0
        return -1;
1055
0
    }
1056
1057
0
    if ((optype != -1) && !(ctx->operation & optype)) {
1058
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1059
0
        return -1;
1060
0
    }
1061
1062
0
    switch (evp_pkey_ctx_state(ctx)) {
1063
0
    case EVP_PKEY_STATE_PROVIDER:
1064
0
        return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
1065
0
    case EVP_PKEY_STATE_UNKNOWN:
1066
0
    case EVP_PKEY_STATE_LEGACY:
1067
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1068
0
        return -2;
1069
0
    }
1070
0
    return ret;
1071
0
}
1072
1073
int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1074
    int cmd, int p1, void *p2)
1075
0
{
1076
0
    int ret = 0;
1077
1078
0
    if (ctx == NULL) {
1079
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1080
0
        return -2;
1081
0
    }
1082
    /* If unsupported, we don't want that reported here */
1083
0
    ERR_set_mark();
1084
0
    ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1085
0
        cmd, NULL, p2, p1);
1086
0
    if (ret == -2) {
1087
0
        ERR_pop_to_mark();
1088
0
    } else {
1089
0
        ERR_clear_last_mark();
1090
        /*
1091
         * If there was an error, there was an error.
1092
         * If the operation isn't initialized yet, we also return, as
1093
         * the saved values will be used then anyway.
1094
         */
1095
0
        if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1096
0
            return ret;
1097
0
    }
1098
0
    return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
1099
0
}
1100
1101
int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
1102
    int cmd, uint64_t value)
1103
0
{
1104
0
    return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1105
0
}
1106
1107
static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1108
    const char *name, const char *value)
1109
0
{
1110
0
    int ret = 0;
1111
1112
0
    if (ctx == NULL) {
1113
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1114
0
        return -2;
1115
0
    }
1116
1117
0
    switch (evp_pkey_ctx_state(ctx)) {
1118
0
    case EVP_PKEY_STATE_PROVIDER:
1119
0
        return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
1120
0
    case EVP_PKEY_STATE_UNKNOWN:
1121
0
    case EVP_PKEY_STATE_LEGACY:
1122
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1123
0
        return -2;
1124
0
    }
1125
1126
0
    return ret;
1127
0
}
1128
1129
int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1130
    const char *name, const char *value)
1131
0
{
1132
0
    int ret = 0;
1133
1134
    /* If unsupported, we don't want that reported here */
1135
0
    ERR_set_mark();
1136
0
    ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1137
0
        name, value, strlen(value) + 1);
1138
0
    if (ret == -2) {
1139
0
        ERR_pop_to_mark();
1140
0
    } else {
1141
0
        ERR_clear_last_mark();
1142
        /*
1143
         * If there was an error, there was an error.
1144
         * If the operation isn't initialized yet, we also return, as
1145
         * the saved values will be used then anyway.
1146
         */
1147
0
        if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1148
0
            return ret;
1149
0
    }
1150
1151
0
    return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1152
0
}
1153
1154
static int decode_cmd(int cmd, const char *name)
1155
0
{
1156
0
    if (cmd == -1) {
1157
        /*
1158
         * The consequence of the assertion not being true is that this
1159
         * function will return -1, which will cause the calling functions
1160
         * to signal that the command is unsupported...  in non-debug mode.
1161
         */
1162
0
        if (ossl_assert(name != NULL))
1163
0
            if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1164
0
                cmd = EVP_PKEY_CTRL_SET1_ID;
1165
0
    }
1166
1167
0
    return cmd;
1168
0
}
1169
1170
static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1171
    int keytype, int optype,
1172
    int cmd, const char *name,
1173
    const void *data, size_t data_len)
1174
0
{
1175
    /*
1176
     * Check that it's one of the supported commands.  The ctrl commands
1177
     * number cases here must correspond to the cases in the bottom switch
1178
     * in this function.
1179
     */
1180
0
    switch (cmd = decode_cmd(cmd, name)) {
1181
0
    case EVP_PKEY_CTRL_SET1_ID:
1182
0
        break;
1183
0
    default:
1184
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1185
0
        return -2;
1186
0
    }
1187
1188
0
    if (keytype != -1) {
1189
0
        switch (evp_pkey_ctx_state(ctx)) {
1190
0
        case EVP_PKEY_STATE_PROVIDER:
1191
0
            if (ctx->keymgmt == NULL) {
1192
0
                ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1193
0
                return -2;
1194
0
            }
1195
0
            if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
1196
0
                    evp_pkey_type2name(keytype))) {
1197
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1198
0
                return -1;
1199
0
            }
1200
0
            break;
1201
0
        case EVP_PKEY_STATE_UNKNOWN:
1202
0
        case EVP_PKEY_STATE_LEGACY:
1203
0
            ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1204
0
            return -2;
1205
0
        }
1206
0
    }
1207
0
    if (optype != -1 && (ctx->operation & optype) == 0) {
1208
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1209
0
        return -1;
1210
0
    }
1211
1212
0
    switch (cmd) {
1213
0
    case EVP_PKEY_CTRL_SET1_ID:
1214
0
        evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1215
0
        if (name != NULL) {
1216
0
            ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
1217
0
            if (ctx->cached_parameters.dist_id_name == NULL)
1218
0
                return 0;
1219
0
        }
1220
0
        if (data != NULL) {
1221
0
            ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
1222
0
            if (ctx->cached_parameters.dist_id == NULL)
1223
0
                return 0;
1224
0
        }
1225
0
        ctx->cached_parameters.dist_id_set = 1;
1226
0
        ctx->cached_parameters.dist_id_len = data_len;
1227
0
        break;
1228
0
    }
1229
0
    return 1;
1230
0
}
1231
1232
static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
1233
    int cmd, const char *name)
1234
0
{
1235
0
    cmd = decode_cmd(cmd, name);
1236
0
    switch (cmd) {
1237
0
    case EVP_PKEY_CTRL_SET1_ID:
1238
0
        OPENSSL_free(ctx->cached_parameters.dist_id);
1239
0
        OPENSSL_free(ctx->cached_parameters.dist_id_name);
1240
0
        ctx->cached_parameters.dist_id = NULL;
1241
0
        ctx->cached_parameters.dist_id_name = NULL;
1242
0
        break;
1243
0
    }
1244
0
}
1245
1246
static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
1247
0
{
1248
0
    evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
1249
0
}
1250
1251
int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
1252
0
{
1253
0
    int ret = 1;
1254
1255
0
    if (ret && ctx->cached_parameters.dist_id_set) {
1256
0
        const char *name = ctx->cached_parameters.dist_id_name;
1257
0
        const void *val = ctx->cached_parameters.dist_id;
1258
0
        size_t len = ctx->cached_parameters.dist_id_len;
1259
1260
0
        if (name != NULL)
1261
0
            ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
1262
0
        else
1263
0
            ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
1264
0
                EVP_PKEY_CTRL_SET1_ID,
1265
0
                (int)len, (void *)val);
1266
0
    }
1267
1268
0
    return ret;
1269
0
}
1270
1271
OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
1272
0
{
1273
0
    return ctx->libctx;
1274
0
}
1275
1276
const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx)
1277
0
{
1278
0
    return ctx->propquery;
1279
0
}
1280
1281
const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx)
1282
0
{
1283
0
    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1284
0
        if (ctx->op.sig.signature != NULL)
1285
0
            return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature);
1286
0
    } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1287
0
        if (ctx->op.kex.exchange != NULL)
1288
0
            return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange);
1289
0
    } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1290
0
        if (ctx->op.encap.kem != NULL)
1291
0
            return EVP_KEM_get0_provider(ctx->op.encap.kem);
1292
0
    } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1293
0
        if (ctx->op.ciph.cipher != NULL)
1294
0
            return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher);
1295
0
    } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1296
0
        if (ctx->keymgmt != NULL)
1297
0
            return EVP_KEYMGMT_get0_provider(ctx->keymgmt);
1298
0
    }
1299
1300
0
    return NULL;
1301
0
}
1302
1303
/* Utility functions to send a string of hex string to a ctrl */
1304
1305
int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1306
0
{
1307
0
    size_t len;
1308
1309
0
    len = strlen(str);
1310
0
    if (len > INT_MAX)
1311
0
        return -1;
1312
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, -1, cmd, (int)len, (void *)str);
1313
0
}
1314
1315
int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1316
0
{
1317
0
    unsigned char *bin;
1318
0
    long binlen;
1319
0
    int rv = -1;
1320
1321
0
    bin = OPENSSL_hexstr2buf(hex, &binlen);
1322
0
    if (bin == NULL)
1323
0
        return 0;
1324
0
    if (binlen <= INT_MAX)
1325
0
        rv = EVP_PKEY_CTX_ctrl(ctx, -1, -1, cmd, binlen, bin);
1326
0
    OPENSSL_free(bin);
1327
0
    return rv;
1328
0
}
1329
1330
/* Pass a message digest to a ctrl */
1331
int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1332
0
{
1333
0
    const EVP_MD *m;
1334
1335
0
    if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1336
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
1337
0
        return 0;
1338
0
    }
1339
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1340
0
}
1341
1342
int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1343
0
{
1344
0
    return ctx->operation;
1345
0
}
1346
1347
void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1348
0
{
1349
0
    ctx->keygen_info = dat;
1350
0
    ctx->keygen_info_count = datlen;
1351
0
}
1352
1353
void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1354
0
{
1355
0
    ctx->data = data;
1356
0
}
1357
1358
void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1359
0
{
1360
0
    return ctx->data;
1361
0
}
1362
1363
EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1364
0
{
1365
0
    return ctx->pkey;
1366
0
}
1367
1368
EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1369
0
{
1370
0
    return ctx->peerkey;
1371
0
}
1372
1373
void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1374
0
{
1375
0
    ctx->app_data = data;
1376
0
}
1377
1378
void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1379
0
{
1380
0
    return ctx->app_data;
1381
0
}
1382
#endif /* FIPS_MODULE */