Coverage Report

Created: 2026-07-23 06:28

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