Coverage Report

Created: 2026-09-12 06:55

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