Coverage Report

Created: 2026-07-16 06:59

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