Coverage Report

Created: 2026-07-24 06:26

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
    return CRYPTO_UP_REF(&exchange->refcnt, &ref);
44
0
}
45
46
static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov)
47
0
{
48
0
    EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH));
49
50
0
    if (exchange == NULL)
51
0
        return NULL;
52
53
0
    if (!CRYPTO_NEW_REF(&exchange->refcnt, 1)
54
0
        || !ossl_provider_up_ref(prov)) {
55
0
        CRYPTO_FREE_REF(&exchange->refcnt);
56
0
        OPENSSL_free(exchange);
57
0
        return NULL;
58
0
    }
59
0
    exchange->prov = prov;
60
61
0
    return exchange;
62
0
}
63
64
static void *evp_keyexch_from_algorithm(int name_id,
65
    const OSSL_ALGORITHM *algodef,
66
    OSSL_PROVIDER *prov, int no_store)
67
0
{
68
0
    const OSSL_DISPATCH *fns = algodef->implementation;
69
0
    EVP_KEYEXCH *exchange = NULL;
70
0
    int fncnt = 0, sparamfncnt = 0, gparamfncnt = 0, derive_found = 0;
71
72
0
    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
0
    exchange->name_id = name_id;
78
0
    exchange->no_store = no_store;
79
0
    if ((exchange->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
80
0
        goto err;
81
0
    exchange->description = algodef->algorithm_description;
82
83
0
    for (; fns->function_id != 0; fns++) {
84
0
        switch (fns->function_id) {
85
0
        case OSSL_FUNC_KEYEXCH_NEWCTX:
86
0
            if (exchange->newctx != NULL)
87
0
                break;
88
0
            exchange->newctx = OSSL_FUNC_keyexch_newctx(fns);
89
0
            fncnt++;
90
0
            break;
91
0
        case OSSL_FUNC_KEYEXCH_INIT:
92
0
            if (exchange->init != NULL)
93
0
                break;
94
0
            exchange->init = OSSL_FUNC_keyexch_init(fns);
95
0
            fncnt++;
96
0
            break;
97
0
        case OSSL_FUNC_KEYEXCH_SET_PEER:
98
0
            if (exchange->set_peer != NULL)
99
0
                break;
100
0
            exchange->set_peer = OSSL_FUNC_keyexch_set_peer(fns);
101
0
            break;
102
0
        case OSSL_FUNC_KEYEXCH_DERIVE:
103
0
            if (exchange->derive != NULL)
104
0
                break;
105
0
            exchange->derive = OSSL_FUNC_keyexch_derive(fns);
106
0
            derive_found = 1;
107
0
            break;
108
0
        case OSSL_FUNC_KEYEXCH_FREECTX:
109
0
            if (exchange->freectx != NULL)
110
0
                break;
111
0
            exchange->freectx = OSSL_FUNC_keyexch_freectx(fns);
112
0
            fncnt++;
113
0
            break;
114
0
        case OSSL_FUNC_KEYEXCH_DUPCTX:
115
0
            if (exchange->dupctx != NULL)
116
0
                break;
117
0
            exchange->dupctx = OSSL_FUNC_keyexch_dupctx(fns);
118
0
            break;
119
0
        case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS:
120
0
            if (exchange->get_ctx_params != NULL)
121
0
                break;
122
0
            exchange->get_ctx_params = OSSL_FUNC_keyexch_get_ctx_params(fns);
123
0
            gparamfncnt++;
124
0
            break;
125
0
        case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS:
126
0
            if (exchange->gettable_ctx_params != NULL)
127
0
                break;
128
0
            exchange->gettable_ctx_params
129
0
                = OSSL_FUNC_keyexch_gettable_ctx_params(fns);
130
0
            gparamfncnt++;
131
0
            break;
132
0
        case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
133
0
            if (exchange->set_ctx_params != NULL)
134
0
                break;
135
0
            exchange->set_ctx_params = OSSL_FUNC_keyexch_set_ctx_params(fns);
136
0
            sparamfncnt++;
137
0
            break;
138
0
        case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
139
0
            if (exchange->settable_ctx_params != NULL)
140
0
                break;
141
0
            exchange->settable_ctx_params
142
0
                = OSSL_FUNC_keyexch_settable_ctx_params(fns);
143
0
            sparamfncnt++;
144
0
            break;
145
0
        case OSSL_FUNC_KEYEXCH_DERIVE_SKEY:
146
0
            if (exchange->derive_skey != NULL)
147
0
                break;
148
0
            exchange->derive_skey = OSSL_FUNC_keyexch_derive_skey(fns);
149
0
            derive_found = 1;
150
0
            break;
151
0
        }
152
0
    }
153
0
    fncnt += derive_found;
154
0
    if (fncnt != 4
155
0
        || (gparamfncnt != 0 && gparamfncnt != 2)
156
0
        || (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
0
    return exchange;
170
171
0
err:
172
0
    evp_keyexch_free(exchange);
173
0
    return NULL;
174
0
}
175
176
void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
177
0
{
178
#ifdef OPENSSL_NO_CACHED_FETCH
179
    evp_keyexch_free(exchange);
180
#else
181
0
    if (exchange != NULL && (exchange->no_store != 0))
182
0
        evp_keyexch_free(exchange);
183
0
#endif
184
0
}
185
186
int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
187
0
{
188
#ifdef OPENSSL_NO_CACHED_FETCH
189
    return evp_keyexch_up_ref(exchange);
190
#else
191
0
    if (exchange->no_store != 0)
192
0
        return evp_keyexch_up_ref(exchange);
193
0
    return 1;
194
0
#endif
195
0
}
196
197
OSSL_PROVIDER *EVP_KEYEXCH_get0_provider(const EVP_KEYEXCH *exchange)
198
0
{
199
0
    return exchange->prov;
200
0
}
201
202
EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
203
    const char *properties)
204
0
{
205
0
    return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
206
0
        evp_keyexch_from_algorithm,
207
0
        evp_keyexch_up_ref,
208
0
        evp_keyexch_free);
209
0
}
210
211
EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov,
212
    const char *algorithm,
213
    const char *properties)
214
0
{
215
0
    return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYEXCH,
216
0
        algorithm, properties,
217
0
        evp_keyexch_from_algorithm,
218
0
        evp_keyexch_up_ref,
219
0
        evp_keyexch_free);
220
0
}
221
222
int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
223
0
{
224
0
    return EVP_PKEY_derive_init_ex(ctx, NULL);
225
0
}
226
227
int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
228
0
{
229
0
    int ret;
230
0
    void *provkey = NULL;
231
0
    EVP_KEYEXCH *exchange = NULL;
232
0
    EVP_KEYMGMT *tmp_keymgmt = NULL;
233
0
    const OSSL_PROVIDER *tmp_prov = NULL;
234
0
    const char *supported_exch = NULL;
235
0
    int iter;
236
237
0
    if (ctx == NULL) {
238
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
239
0
        return -2;
240
0
    }
241
242
0
    evp_pkey_ctx_free_old_ops(ctx);
243
0
    ctx->operation = EVP_PKEY_OP_DERIVE;
244
245
0
    ERR_set_mark();
246
247
0
    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
0
    if (ctx->pkey == NULL) {
255
0
        EVP_PKEY *pkey = EVP_PKEY_new();
256
257
0
        if (pkey == NULL
258
0
            || !EVP_PKEY_set_type_by_keymgmt(pkey, ctx->keymgmt)
259
0
            || (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
0
        ctx->pkey = pkey;
266
0
    }
267
268
    /*
269
     * Try to derive the supported exch from |ctx->keymgmt|.
270
     */
271
0
    if (!ossl_assert(ctx->pkey->keymgmt == NULL
272
0
            || 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
0
    supported_exch = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
278
0
        OSSL_OP_KEYEXCH);
279
0
    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
0
    for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
303
0
        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
0
        EVP_KEYEXCH_free(exchange);
311
0
        exchange = NULL;
312
0
        EVP_KEYMGMT_free(tmp_keymgmt);
313
0
        tmp_keymgmt = NULL;
314
315
0
        switch (iter) {
316
0
        case 1:
317
0
            exchange = EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery);
318
0
            if (exchange != NULL)
319
0
                tmp_prov = EVP_KEYEXCH_get0_provider(exchange);
320
0
            break;
321
0
        case 2:
322
0
            tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
323
0
            exchange = evp_keyexch_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
324
0
                supported_exch, ctx->propquery);
325
0
            if (exchange == NULL) {
326
0
                ERR_pop_to_mark();
327
0
                ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
328
0
                return -2;
329
0
            }
330
0
            break;
331
0
        }
332
0
        if (exchange == NULL)
333
0
            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
0
        tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
345
0
            EVP_KEYMGMT_get0_name(ctx->keymgmt),
346
0
            ctx->propquery);
347
0
        if (tmp_keymgmt != NULL)
348
0
            provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
349
0
                &tmp_keymgmt, ctx->propquery);
350
0
        if (tmp_keymgmt == NULL)
351
0
            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
352
0
    }
353
354
0
    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
0
    ERR_pop_to_mark();
362
363
    /* A Coverity false positive with up_ref/down_ref and free */
364
    /* coverity[use_after_free] */
365
0
    ctx->op.kex.exchange = exchange;
366
    /* A Coverity false positive with up_ref/down_ref and free */
367
    /* coverity[deref_arg] */
368
0
    ctx->op.kex.algctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
369
0
    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
0
    ret = exchange->init(ctx->op.kex.algctx, provkey, params);
375
376
0
    EVP_KEYMGMT_free(tmp_keymgmt);
377
0
    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
0
}
384
385
int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer,
386
    int validate_peer)
387
0
{
388
0
    int ret = 0, check;
389
0
    void *provkey = NULL;
390
0
    EVP_PKEY_CTX *check_ctx = NULL;
391
0
    EVP_KEYMGMT *tmp_keymgmt = NULL, *tmp_keymgmt_tofree = NULL;
392
393
0
    if (ctx == NULL) {
394
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
395
0
        return -1;
396
0
    }
397
398
0
    if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.algctx == NULL) {
399
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
400
0
        return -2;
401
0
    }
402
403
0
    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
0
    if (validate_peer) {
409
0
        check_ctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, peer, ctx->propquery);
410
0
        if (check_ctx == NULL)
411
0
            return -1;
412
0
        check = EVP_PKEY_public_check(check_ctx);
413
0
        EVP_PKEY_CTX_free(check_ctx);
414
0
        if (check <= 0)
415
0
            return -1;
416
0
    }
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
0
    tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)
428
0
                                                                       EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange),
429
0
        EVP_KEYMGMT_get0_name(ctx->keymgmt),
430
0
        ctx->propquery);
431
0
    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
0
    provkey = evp_pkey_export_to_provider(peer, ctx->libctx,
438
0
        &tmp_keymgmt, ctx->propquery);
439
0
    EVP_KEYMGMT_free(tmp_keymgmt_tofree);
440
441
0
    if (provkey == NULL) {
442
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
443
0
        return -1;
444
0
    }
445
0
    ret = ctx->op.kex.exchange->set_peer(ctx->op.kex.algctx, provkey);
446
0
    if (ret <= 0)
447
0
        return ret;
448
449
0
    if (!EVP_PKEY_up_ref(peer))
450
0
        return -1;
451
452
0
    EVP_PKEY_free(ctx->peerkey);
453
0
    ctx->peerkey = peer;
454
455
0
    return 1;
456
0
}
457
458
int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
459
0
{
460
0
    return EVP_PKEY_derive_set_peer_ex(ctx, peer, 1);
461
0
}
462
463
int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
464
0
{
465
0
    int ret;
466
467
0
    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
0
    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
0
    if (ctx->op.kex.algctx == NULL) {
478
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
479
0
        return -2;
480
0
    }
481
482
0
    ret = ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, pkeylen,
483
0
        key != NULL ? *pkeylen : 0);
484
485
0
    return ret;
486
0
}
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
0
{
609
0
    struct EVP_KEYEXCH_do_all_provided_thunk t;
610
611
0
    t.fn = fn;
612
0
    t.arg = arg;
613
0
    evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
614
0
        EVP_KEYEXCH_do_all_provided_thunk, &t,
615
0
        evp_keyexch_from_algorithm,
616
0
        evp_keyexch_up_ref,
617
0
        evp_keyexch_free);
618
0
}
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
0
{
643
0
    void *provctx;
644
645
0
    if (keyexch == NULL || keyexch->settable_ctx_params == NULL)
646
0
        return NULL;
647
0
    provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
648
    return keyexch->settable_ctx_params(NULL, provctx);
649
0
}