Coverage Report

Created: 2026-07-23 06:28

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