Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/evp/exchange.c
Line
Count
Source
1
/*
2
 * Copyright 2019-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
#include <openssl/core_names.h>
11
#include <openssl/crypto.h>
12
#include <openssl/evp.h>
13
#include <openssl/err.h>
14
#include "internal/cryptlib.h"
15
#include "internal/refcount.h"
16
#include "internal/provider.h"
17
#include "internal/core.h"
18
#include "internal/numbers.h" /* includes SIZE_MAX */
19
#include "crypto/evp.h"
20
#include "evp_local.h"
21
22
static void evp_keyexch_free(void *data)
23
257
{
24
257
    EVP_KEYEXCH_free(data);
25
257
}
26
27
static int evp_keyexch_up_ref(void *data)
28
137k
{
29
137k
    return EVP_KEYEXCH_up_ref(data);
30
137k
}
31
32
static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov)
33
189
{
34
189
    EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH));
35
36
189
    if (exchange == NULL)
37
0
        return NULL;
38
39
189
    if (!CRYPTO_NEW_REF(&exchange->refcnt, 1)
40
189
        || !ossl_provider_up_ref(prov)) {
41
0
        CRYPTO_FREE_REF(&exchange->refcnt);
42
0
        OPENSSL_free(exchange);
43
0
        return NULL;
44
0
    }
45
189
    exchange->prov = prov;
46
47
189
    return exchange;
48
189
}
49
50
static void *evp_keyexch_from_algorithm(int name_id,
51
    const OSSL_ALGORITHM *algodef,
52
    OSSL_PROVIDER *prov)
53
126
{
54
126
    const OSSL_DISPATCH *fns = algodef->implementation;
55
126
    EVP_KEYEXCH *exchange = NULL;
56
126
    int fncnt = 0, sparamfncnt = 0, gparamfncnt = 0, derive_found = 0;
57
58
126
    if ((exchange = evp_keyexch_new(prov)) == NULL) {
59
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
60
0
        goto err;
61
0
    }
62
63
126
    exchange->name_id = name_id;
64
126
    if ((exchange->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
65
0
        goto err;
66
126
    exchange->description = algodef->algorithm_description;
67
68
1.27k
    for (; fns->function_id != 0; fns++) {
69
1.15k
        switch (fns->function_id) {
70
126
        case OSSL_FUNC_KEYEXCH_NEWCTX:
71
126
            if (exchange->newctx != NULL)
72
0
                break;
73
126
            exchange->newctx = OSSL_FUNC_keyexch_newctx(fns);
74
126
            fncnt++;
75
126
            break;
76
126
        case OSSL_FUNC_KEYEXCH_INIT:
77
126
            if (exchange->init != NULL)
78
0
                break;
79
126
            exchange->init = OSSL_FUNC_keyexch_init(fns);
80
126
            fncnt++;
81
126
            break;
82
72
        case OSSL_FUNC_KEYEXCH_SET_PEER:
83
72
            if (exchange->set_peer != NULL)
84
0
                break;
85
72
            exchange->set_peer = OSSL_FUNC_keyexch_set_peer(fns);
86
72
            break;
87
126
        case OSSL_FUNC_KEYEXCH_DERIVE:
88
126
            if (exchange->derive != NULL)
89
0
                break;
90
126
            exchange->derive = OSSL_FUNC_keyexch_derive(fns);
91
126
            derive_found = 1;
92
126
            break;
93
126
        case OSSL_FUNC_KEYEXCH_FREECTX:
94
126
            if (exchange->freectx != NULL)
95
0
                break;
96
126
            exchange->freectx = OSSL_FUNC_keyexch_freectx(fns);
97
126
            fncnt++;
98
126
            break;
99
126
        case OSSL_FUNC_KEYEXCH_DUPCTX:
100
126
            if (exchange->dupctx != NULL)
101
0
                break;
102
126
            exchange->dupctx = OSSL_FUNC_keyexch_dupctx(fns);
103
126
            break;
104
126
        case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS:
105
126
            if (exchange->get_ctx_params != NULL)
106
0
                break;
107
126
            exchange->get_ctx_params = OSSL_FUNC_keyexch_get_ctx_params(fns);
108
126
            gparamfncnt++;
109
126
            break;
110
126
        case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS:
111
126
            if (exchange->gettable_ctx_params != NULL)
112
0
                break;
113
126
            exchange->gettable_ctx_params
114
126
                = OSSL_FUNC_keyexch_gettable_ctx_params(fns);
115
126
            gparamfncnt++;
116
126
            break;
117
90
        case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
118
90
            if (exchange->set_ctx_params != NULL)
119
0
                break;
120
90
            exchange->set_ctx_params = OSSL_FUNC_keyexch_set_ctx_params(fns);
121
90
            sparamfncnt++;
122
90
            break;
123
90
        case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
124
90
            if (exchange->settable_ctx_params != NULL)
125
0
                break;
126
90
            exchange->settable_ctx_params
127
90
                = OSSL_FUNC_keyexch_settable_ctx_params(fns);
128
90
            sparamfncnt++;
129
90
            break;
130
18
        case OSSL_FUNC_KEYEXCH_DERIVE_SKEY:
131
18
            if (exchange->derive_skey != NULL)
132
0
                break;
133
18
            exchange->derive_skey = OSSL_FUNC_keyexch_derive_skey(fns);
134
18
            derive_found = 1;
135
18
            break;
136
1.15k
        }
137
1.15k
    }
138
126
    fncnt += derive_found;
139
126
    if (fncnt != 4
140
126
        || (gparamfncnt != 0 && gparamfncnt != 2)
141
126
        || (sparamfncnt != 0 && sparamfncnt != 2)) {
142
        /*
143
         * In order to be a consistent set of functions we must have at least
144
         * a complete set of "exchange" functions: init, derive, newctx,
145
         * and freectx. The set_ctx_params and settable_ctx_params functions are
146
         * optional, but if one of them is present then the other one must also
147
         * be present. Same goes for get_ctx_params and gettable_ctx_params.
148
         * The dupctx and set_peer functions are optional.
149
         */
150
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
151
0
        goto err;
152
0
    }
153
154
126
    return exchange;
155
156
0
err:
157
0
    EVP_KEYEXCH_free(exchange);
158
0
    return NULL;
159
126
}
160
161
void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
162
345k
{
163
345k
    int i;
164
165
345k
    if (exchange == NULL)
166
34.5k
        return;
167
310k
    CRYPTO_DOWN_REF(&exchange->refcnt, &i);
168
310k
    if (i > 0)
169
310k
        return;
170
182
    OPENSSL_free(exchange->type_name);
171
182
    ossl_provider_free(exchange->prov);
172
182
    CRYPTO_FREE_REF(&exchange->refcnt);
173
182
    OPENSSL_free(exchange);
174
182
}
175
176
int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
177
310k
{
178
310k
    int ref = 0;
179
180
310k
    CRYPTO_UP_REF(&exchange->refcnt, &ref);
181
310k
    return 1;
182
310k
}
183
184
OSSL_PROVIDER *EVP_KEYEXCH_get0_provider(const EVP_KEYEXCH *exchange)
185
83.9k
{
186
83.9k
    return exchange->prov;
187
83.9k
}
188
189
EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
190
    const char *properties)
191
375k
{
192
375k
    return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
193
375k
        evp_keyexch_from_algorithm,
194
375k
        evp_keyexch_up_ref,
195
375k
        evp_keyexch_free);
196
375k
}
197
198
EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov,
199
    const char *algorithm,
200
    const char *properties)
201
102
{
202
102
    return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYEXCH,
203
102
        algorithm, properties,
204
102
        evp_keyexch_from_algorithm,
205
102
        evp_keyexch_up_ref,
206
102
        evp_keyexch_free);
207
102
}
208
209
int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
210
42.1k
{
211
42.1k
    return EVP_PKEY_derive_init_ex(ctx, NULL);
212
42.1k
}
213
214
int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
215
34.4k
{
216
34.4k
    int ret;
217
34.4k
    void *provkey = NULL;
218
34.4k
    EVP_KEYEXCH *exchange = NULL;
219
34.4k
    EVP_KEYMGMT *tmp_keymgmt = NULL;
220
34.4k
    const OSSL_PROVIDER *tmp_prov = NULL;
221
34.4k
    const char *supported_exch = NULL;
222
34.4k
    int iter;
223
224
34.4k
    if (ctx == NULL) {
225
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
226
0
        return -2;
227
0
    }
228
229
34.4k
    evp_pkey_ctx_free_old_ops(ctx);
230
34.4k
    ctx->operation = EVP_PKEY_OP_DERIVE;
231
232
34.4k
    ERR_set_mark();
233
234
34.4k
    if (evp_pkey_ctx_is_legacy(ctx))
235
0
        goto legacy;
236
237
    /*
238
     * Some algorithms (e.g. legacy KDFs) don't have a pkey - so we create
239
     * a blank one.
240
     */
241
34.4k
    if (ctx->pkey == NULL) {
242
0
        EVP_PKEY *pkey = EVP_PKEY_new();
243
244
0
        if (pkey == NULL
245
0
            || !EVP_PKEY_set_type_by_keymgmt(pkey, ctx->keymgmt)
246
0
            || (pkey->keydata = evp_keymgmt_newdata(ctx->keymgmt)) == NULL) {
247
0
            ERR_clear_last_mark();
248
0
            EVP_PKEY_free(pkey);
249
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
250
0
            goto err;
251
0
        }
252
0
        ctx->pkey = pkey;
253
0
    }
254
255
    /*
256
     * Try to derive the supported exch from |ctx->keymgmt|.
257
     */
258
34.4k
    if (!ossl_assert(ctx->pkey->keymgmt == NULL
259
34.4k
            || ctx->pkey->keymgmt == ctx->keymgmt)) {
260
0
        ERR_clear_last_mark();
261
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
262
0
        goto err;
263
0
    }
264
34.4k
    supported_exch = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
265
34.4k
        OSSL_OP_KEYEXCH);
266
34.4k
    if (supported_exch == NULL) {
267
0
        ERR_clear_last_mark();
268
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
269
0
        goto err;
270
0
    }
271
272
    /*
273
     * We perform two iterations:
274
     *
275
     * 1.  Do the normal exchange fetch, using the fetching data given by
276
     *     the EVP_PKEY_CTX.
277
     * 2.  Do the provider specific exchange fetch, from the same provider
278
     *     as |ctx->keymgmt|
279
     *
280
     * We then try to fetch the keymgmt from the same provider as the
281
     * exchange, and try to export |ctx->pkey| to that keymgmt (when
282
     * this keymgmt happens to be the same as |ctx->keymgmt|, the export
283
     * is a no-op, but we call it anyway to not complicate the code even
284
     * more).
285
     * If the export call succeeds (returns a non-NULL provider key pointer),
286
     * we're done and can perform the operation itself.  If not, we perform
287
     * the second iteration, or jump to legacy.
288
     */
289
68.8k
    for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
290
34.4k
        EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
291
292
        /*
293
         * If we're on the second iteration, free the results from the first.
294
         * They are NULL on the first iteration, so no need to check what
295
         * iteration we're on.
296
         */
297
34.4k
        EVP_KEYEXCH_free(exchange);
298
34.4k
        exchange = NULL;
299
34.4k
        EVP_KEYMGMT_free(tmp_keymgmt);
300
34.4k
        tmp_keymgmt = NULL;
301
302
34.4k
        switch (iter) {
303
34.4k
        case 1:
304
34.4k
            exchange = EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery);
305
34.4k
            if (exchange != NULL)
306
34.3k
                tmp_prov = EVP_KEYEXCH_get0_provider(exchange);
307
34.4k
            break;
308
70
        case 2:
309
70
            tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
310
70
            exchange = evp_keyexch_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
311
70
                supported_exch, ctx->propquery);
312
70
            if (exchange == NULL)
313
70
                goto legacy;
314
0
            break;
315
34.4k
        }
316
34.4k
        if (exchange == NULL)
317
70
            continue;
318
319
        /*
320
         * Ensure that the key is provided, either natively, or as a cached
321
         * export.  We start by fetching the keymgmt with the same name as
322
         * |ctx->keymgmt|, but from the provider of the exchange method, using
323
         * the same property query as when fetching the exchange method.
324
         * With the keymgmt we found (if we did), we try to export |ctx->pkey|
325
         * to it (evp_pkey_export_to_provider() is smart enough to only actually
326
         * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
327
         */
328
34.3k
        tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
329
34.3k
            EVP_KEYMGMT_get0_name(ctx->keymgmt),
330
34.3k
            ctx->propquery);
331
34.3k
        if (tmp_keymgmt != NULL)
332
34.3k
            provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
333
34.3k
                &tmp_keymgmt, ctx->propquery);
334
34.3k
        if (tmp_keymgmt == NULL)
335
0
            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
336
34.3k
    }
337
338
34.3k
    if (provkey == NULL) {
339
0
        EVP_KEYEXCH_free(exchange);
340
0
        goto legacy;
341
0
    }
342
343
34.3k
    ERR_pop_to_mark();
344
345
    /* No more legacy from here down to legacy: */
346
347
    /* A Coverity false positive with up_ref/down_ref and free */
348
    /* coverity[use_after_free] */
349
34.3k
    ctx->op.kex.exchange = exchange;
350
    /* A Coverity false positive with up_ref/down_ref and free */
351
    /* coverity[deref_arg] */
352
34.3k
    ctx->op.kex.algctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
353
34.3k
    if (ctx->op.kex.algctx == NULL) {
354
        /* The provider key can stay in the cache */
355
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
356
0
        goto err;
357
0
    }
358
34.3k
    ret = exchange->init(ctx->op.kex.algctx, provkey, params);
359
360
34.3k
    EVP_KEYMGMT_free(tmp_keymgmt);
361
34.3k
    return ret ? 1 : 0;
362
0
err:
363
0
    evp_pkey_ctx_free_old_ops(ctx);
364
0
    ctx->operation = EVP_PKEY_OP_UNDEFINED;
365
0
    EVP_KEYMGMT_free(tmp_keymgmt);
366
0
    return 0;
367
368
70
legacy:
369
    /*
370
     * If we don't have the full support we need with provided methods,
371
     * let's go see if legacy does.
372
     */
373
70
    ERR_pop_to_mark();
374
375
#ifdef FIPS_MODULE
376
    return 0;
377
#else
378
70
    if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
379
70
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
380
70
        return -2;
381
70
    }
382
383
0
    if (ctx->pmeth->derive_init == NULL)
384
0
        return 1;
385
0
    ret = ctx->pmeth->derive_init(ctx);
386
0
    if (ret <= 0)
387
0
        ctx->operation = EVP_PKEY_OP_UNDEFINED;
388
0
    EVP_KEYMGMT_free(tmp_keymgmt);
389
0
    return ret;
390
0
#endif
391
0
}
392
393
int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer,
394
    int validate_peer)
395
14.7k
{
396
14.7k
    int ret = 0, check;
397
14.7k
    void *provkey = NULL;
398
14.7k
    EVP_PKEY_CTX *check_ctx = NULL;
399
14.7k
    EVP_KEYMGMT *tmp_keymgmt = NULL, *tmp_keymgmt_tofree = NULL;
400
401
14.7k
    if (ctx == NULL) {
402
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
403
0
        return -1;
404
0
    }
405
406
14.7k
    if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.algctx == NULL)
407
70
        goto legacy;
408
409
14.6k
    if (ctx->op.kex.exchange->set_peer == NULL) {
410
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
411
0
        return -2;
412
0
    }
413
414
14.6k
    if (validate_peer) {
415
14.6k
        check_ctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, peer, ctx->propquery);
416
14.6k
        if (check_ctx == NULL)
417
0
            return -1;
418
14.6k
        check = EVP_PKEY_public_check(check_ctx);
419
14.6k
        EVP_PKEY_CTX_free(check_ctx);
420
14.6k
        if (check <= 0)
421
15
            return -1;
422
14.6k
    }
423
424
    /*
425
     * Ensure that the |peer| is provided, either natively, or as a cached
426
     * export.  We start by fetching the keymgmt with the same name as
427
     * |ctx->keymgmt|, but from the provider of the exchange method, using
428
     * the same property query as when fetching the exchange method.
429
     * With the keymgmt we found (if we did), we try to export |peer|
430
     * to it (evp_pkey_export_to_provider() is smart enough to only actually
431
     * export it if |tmp_keymgmt| is different from |peer|'s keymgmt)
432
     */
433
14.6k
    tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)
434
14.6k
                                                                       EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange),
435
14.6k
        EVP_KEYMGMT_get0_name(ctx->keymgmt),
436
14.6k
        ctx->propquery);
437
14.6k
    if (tmp_keymgmt != NULL)
438
        /* A Coverity issue with up_ref/down_ref and free */
439
        /* coverity[pass_freed_arg] */
440
14.6k
        provkey = evp_pkey_export_to_provider(peer, ctx->libctx,
441
14.6k
            &tmp_keymgmt, ctx->propquery);
442
14.6k
    EVP_KEYMGMT_free(tmp_keymgmt_tofree);
443
444
    /*
445
     * If making the key provided wasn't possible, legacy may be able to pick
446
     * it up
447
     */
448
14.6k
    if (provkey == NULL)
449
0
        goto legacy;
450
14.6k
    ret = ctx->op.kex.exchange->set_peer(ctx->op.kex.algctx, provkey);
451
14.6k
    if (ret <= 0)
452
0
        return ret;
453
14.6k
    goto common;
454
455
14.6k
legacy:
456
#ifdef FIPS_MODULE
457
    return ret;
458
#else
459
70
    if (ctx->pmeth == NULL
460
0
        || !(ctx->pmeth->derive != NULL
461
0
            || ctx->pmeth->encrypt != NULL
462
0
            || ctx->pmeth->decrypt != NULL)
463
70
        || ctx->pmeth->ctrl == NULL) {
464
70
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
465
70
        return -2;
466
70
    }
467
0
    if (ctx->operation != EVP_PKEY_OP_DERIVE
468
0
        && ctx->operation != EVP_PKEY_OP_ENCRYPT
469
0
        && ctx->operation != EVP_PKEY_OP_DECRYPT) {
470
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
471
0
        return -1;
472
0
    }
473
474
0
    ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
475
476
0
    if (ret <= 0)
477
0
        return ret;
478
479
0
    if (ret == 2)
480
0
        return 1;
481
482
0
    if (ctx->pkey == NULL) {
483
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
484
0
        return -1;
485
0
    }
486
487
0
    if (ctx->pkey->type != peer->type) {
488
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
489
0
        return -1;
490
0
    }
491
492
    /*
493
     * For clarity.  The error is if parameters in peer are
494
     * present (!missing) but don't match.  EVP_PKEY_parameters_eq may return
495
     * 1 (match), 0 (don't match) and -2 (comparison is not defined).  -1
496
     * (different key types) is impossible here because it is checked earlier.
497
     * -2 is OK for us here, as well as 1, so we can check for 0 only.
498
     */
499
0
    if (!EVP_PKEY_missing_parameters(peer) && !EVP_PKEY_parameters_eq(ctx->pkey, peer)) {
500
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
501
0
        return -1;
502
0
    }
503
504
0
    ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
505
0
    if (ret <= 0)
506
0
        return ret;
507
0
#endif
508
509
14.6k
common:
510
14.6k
    if (!EVP_PKEY_up_ref(peer))
511
0
        return -1;
512
513
14.6k
    EVP_PKEY_free(ctx->peerkey);
514
14.6k
    ctx->peerkey = peer;
515
516
14.6k
    return 1;
517
14.6k
}
518
519
int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
520
42.1k
{
521
42.1k
    return EVP_PKEY_derive_set_peer_ex(ctx, peer, 1);
522
42.1k
}
523
524
int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
525
45.4k
{
526
45.4k
    int ret;
527
528
45.4k
    if (ctx == NULL || pkeylen == NULL) {
529
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
530
0
        return -1;
531
0
    }
532
533
45.4k
    if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
534
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
535
0
        return -1;
536
0
    }
537
538
45.4k
    if (ctx->op.kex.algctx == NULL)
539
70
        goto legacy;
540
541
45.4k
    ret = ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, pkeylen,
542
45.4k
        key != NULL ? *pkeylen : 0);
543
544
45.4k
    return ret;
545
70
legacy:
546
70
    if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
547
70
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
548
70
        return -2;
549
70
    }
550
551
0
    M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE) return ctx->pmeth->derive(ctx, key, pkeylen);
552
0
}
553
554
EVP_SKEY *EVP_PKEY_derive_SKEY(EVP_PKEY_CTX *ctx, EVP_SKEYMGMT *mgmt,
555
    const char *key_type, const char *propquery,
556
    size_t keylen, const OSSL_PARAM params[])
557
0
{
558
0
    EVP_SKEYMGMT *skeymgmt = NULL;
559
0
    EVP_SKEY *ret = NULL;
560
561
0
    if (ctx == NULL || key_type == NULL) {
562
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
563
0
        return NULL;
564
0
    }
565
566
0
    if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
567
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
568
0
        return NULL;
569
0
    }
570
571
0
    if (ctx->op.kex.algctx == NULL) {
572
0
        ERR_raise(ERR_R_EVP_LIB, ERR_R_UNSUPPORTED);
573
0
        return NULL;
574
0
    }
575
576
0
    if (mgmt != NULL) {
577
0
        skeymgmt = mgmt;
578
0
    } else {
579
0
        skeymgmt = evp_skeymgmt_fetch_from_prov(ctx->op.kex.exchange->prov,
580
0
            key_type, propquery);
581
0
        if (skeymgmt == NULL) {
582
            /*
583
             * The provider does not support skeymgmt, let's try to fallback
584
             * to a provider that supports it
585
             */
586
0
            skeymgmt = EVP_SKEYMGMT_fetch(ctx->libctx, key_type, propquery);
587
0
        }
588
0
        if (skeymgmt == NULL) {
589
0
            ERR_raise(ERR_LIB_EVP, ERR_R_FETCH_FAILED);
590
0
            return NULL;
591
0
        }
592
0
    }
593
594
    /* Fallback to raw derive + import if necessary */
595
0
    if (skeymgmt->prov != ctx->op.kex.exchange->prov || ctx->op.kex.exchange->derive_skey == NULL) {
596
0
        size_t tmplen = keylen;
597
0
        unsigned char *key = NULL;
598
0
        OSSL_PARAM import_params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
599
600
0
        if (ctx->op.kex.exchange->derive == NULL) {
601
0
            ERR_raise(ERR_R_EVP_LIB, ERR_R_UNSUPPORTED);
602
0
            goto cleanup;
603
0
        }
604
605
0
        key = OPENSSL_zalloc(keylen);
606
0
        if (key == NULL) {
607
0
            ERR_raise(ERR_R_EVP_LIB, ERR_R_CRYPTO_LIB);
608
0
            goto cleanup;
609
0
        }
610
611
0
        if (!ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, &tmplen,
612
0
                tmplen)) {
613
0
            OPENSSL_free(key);
614
0
            goto cleanup;
615
0
        }
616
617
0
        if (keylen != tmplen) {
618
0
            OPENSSL_free(key);
619
0
            ERR_raise(ERR_R_EVP_LIB, ERR_R_INTERNAL_ERROR);
620
0
            goto cleanup;
621
0
        }
622
0
        import_params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES,
623
0
            key, keylen);
624
625
0
        ret = EVP_SKEY_import_SKEYMGMT(ctx->libctx, skeymgmt,
626
0
            OSSL_SKEYMGMT_SELECT_SECRET_KEY, import_params);
627
0
        OPENSSL_clear_free(key, keylen);
628
0
        goto cleanup;
629
0
    }
630
631
0
    ret = evp_skey_alloc(skeymgmt);
632
0
    if (ret == NULL)
633
0
        goto cleanup;
634
635
0
    ret->keydata = ctx->op.kex.exchange->derive_skey(ctx->op.kex.algctx, key_type,
636
0
        ossl_provider_ctx(skeymgmt->prov),
637
0
        skeymgmt->import, keylen, params);
638
639
0
    if (ret->keydata == NULL) {
640
0
        EVP_SKEY_free(ret);
641
0
        ret = NULL;
642
0
        goto cleanup;
643
0
    }
644
0
cleanup:
645
0
    if (mgmt != skeymgmt)
646
0
        EVP_SKEYMGMT_free(skeymgmt);
647
0
    return ret;
648
0
}
649
650
int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch)
651
0
{
652
0
    return keyexch->name_id;
653
0
}
654
655
const char *EVP_KEYEXCH_get0_name(const EVP_KEYEXCH *keyexch)
656
0
{
657
0
    return keyexch->type_name;
658
0
}
659
660
const char *EVP_KEYEXCH_get0_description(const EVP_KEYEXCH *keyexch)
661
0
{
662
0
    return keyexch->description;
663
0
}
664
665
int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
666
0
{
667
0
    return keyexch != NULL
668
0
        && evp_is_a(keyexch->prov, keyexch->name_id, NULL, name);
669
0
}
670
671
void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx,
672
    void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
673
    void *arg)
674
8
{
675
8
    evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
676
8
        (void (*)(void *, void *))fn, arg,
677
8
        evp_keyexch_from_algorithm,
678
8
        evp_keyexch_up_ref,
679
8
        evp_keyexch_free);
680
8
}
681
682
int EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
683
    void (*fn)(const char *name, void *data),
684
    void *data)
685
0
{
686
0
    if (keyexch->prov != NULL)
687
0
        return evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);
688
689
0
    return 1;
690
0
}
691
692
const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch)
693
0
{
694
0
    void *provctx;
695
696
0
    if (keyexch == NULL || keyexch->gettable_ctx_params == NULL)
697
0
        return NULL;
698
699
0
    provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
700
0
    return keyexch->gettable_ctx_params(NULL, provctx);
701
0
}
702
703
const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch)
704
83
{
705
83
    void *provctx;
706
707
83
    if (keyexch == NULL || keyexch->settable_ctx_params == NULL)
708
4
        return NULL;
709
79
    provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
710
    return keyexch->settable_ctx_params(NULL, provctx);
711
83
}