Coverage Report

Created: 2026-09-12 06:55

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