Coverage Report

Created: 2025-08-28 07:07

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