Coverage Report

Created: 2025-12-31 06:58

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
319
{
23
319
    EVP_KEYEXCH_free(data);
24
319
}
25
26
static int evp_keyexch_up_ref(void *data)
27
204k
{
28
204k
    return EVP_KEYEXCH_up_ref(data);
29
204k
}
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
411k
{
155
411k
    int i;
156
157
411k
    if (exchange == NULL)
158
41.9k
        return;
159
369k
    CRYPTO_DOWN_REF(&exchange->refcnt, &i);
160
369k
    if (i > 0)
161
369k
        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
369k
{
170
369k
    int ref = 0;
171
172
369k
    CRYPTO_UP_REF(&exchange->refcnt, &ref);
173
369k
    return 1;
174
369k
}
175
176
OSSL_PROVIDER *EVP_KEYEXCH_get0_provider(const EVP_KEYEXCH *exchange)
177
83.3k
{
178
83.3k
    return exchange->prov;
179
83.3k
}
180
181
EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
182
    const char *properties)
183
368k
{
184
368k
    return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
185
368k
        evp_keyexch_from_algorithm,
186
368k
        evp_keyexch_up_ref,
187
368k
        evp_keyexch_free);
188
368k
}
189
190
EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov,
191
    const char *algorithm,
192
    const char *properties)
193
72
{
194
72
    return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYEXCH,
195
72
        algorithm, properties,
196
72
        evp_keyexch_from_algorithm,
197
72
        evp_keyexch_up_ref,
198
72
        evp_keyexch_free);
199
72
}
200
201
int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
202
41.7k
{
203
41.7k
    return EVP_PKEY_derive_init_ex(ctx, NULL);
204
41.7k
}
205
206
int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
207
33.6k
{
208
33.6k
    int ret;
209
33.6k
    void *provkey = NULL;
210
33.6k
    EVP_KEYEXCH *exchange = NULL;
211
33.6k
    EVP_KEYMGMT *tmp_keymgmt = NULL;
212
33.6k
    const OSSL_PROVIDER *tmp_prov = NULL;
213
33.6k
    const char *supported_exch = NULL;
214
33.6k
    int iter;
215
216
33.6k
    if (ctx == NULL) {
217
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
218
0
        return -2;
219
0
    }
220
221
33.6k
    evp_pkey_ctx_free_old_ops(ctx);
222
33.6k
    ctx->operation = EVP_PKEY_OP_DERIVE;
223
224
33.6k
    ERR_set_mark();
225
226
33.6k
    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
33.6k
    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
33.6k
    if (!ossl_assert(ctx->pkey->keymgmt == NULL
251
33.6k
            || 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
33.6k
    supported_exch = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
257
33.6k
        OSSL_OP_KEYEXCH);
258
33.6k
    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
67.3k
    for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
282
33.7k
        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
33.7k
        EVP_KEYEXCH_free(exchange);
290
33.7k
        EVP_KEYMGMT_free(tmp_keymgmt);
291
292
33.7k
        switch (iter) {
293
33.6k
        case 1:
294
33.6k
            exchange = EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery);
295
33.6k
            if (exchange != NULL)
296
33.6k
                tmp_prov = EVP_KEYEXCH_get0_provider(exchange);
297
33.6k
            break;
298
42
        case 2:
299
42
            tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
300
42
            exchange = evp_keyexch_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
301
42
                supported_exch, ctx->propquery);
302
42
            if (exchange == NULL)
303
42
                goto legacy;
304
0
            break;
305
33.7k
        }
306
33.6k
        if (exchange == NULL)
307
42
            continue;
308
309
        /*
310
         * Ensure that the key is provided, either natively, or as a cached
311
         * export.  We start by fetching the keymgmt with the same name as
312
         * |ctx->keymgmt|, but from the provider of the exchange method, using
313
         * the same property query as when fetching the exchange method.
314
         * With the keymgmt we found (if we did), we try to export |ctx->pkey|
315
         * to it (evp_pkey_export_to_provider() is smart enough to only actually
316
         * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
317
         */
318
33.6k
        tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
319
33.6k
            EVP_KEYMGMT_get0_name(ctx->keymgmt),
320
33.6k
            ctx->propquery);
321
33.6k
        if (tmp_keymgmt != NULL)
322
33.6k
            provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
323
33.6k
                &tmp_keymgmt, ctx->propquery);
324
33.6k
        if (tmp_keymgmt == NULL)
325
0
            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
326
33.6k
    }
327
328
33.6k
    if (provkey == NULL) {
329
0
        EVP_KEYEXCH_free(exchange);
330
0
        goto legacy;
331
0
    }
332
333
33.6k
    ERR_pop_to_mark();
334
335
    /* No more legacy from here down to legacy: */
336
337
    /* A Coverity false positive with up_ref/down_ref and free */
338
    /* coverity[use_after_free] */
339
33.6k
    ctx->op.kex.exchange = exchange;
340
    /* A Coverity false positive with up_ref/down_ref and free */
341
    /* coverity[deref_arg] */
342
33.6k
    ctx->op.kex.algctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
343
33.6k
    if (ctx->op.kex.algctx == NULL) {
344
        /* The provider key can stay in the cache */
345
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
346
0
        goto err;
347
0
    }
348
33.6k
    ret = exchange->init(ctx->op.kex.algctx, provkey, params);
349
350
33.6k
    EVP_KEYMGMT_free(tmp_keymgmt);
351
33.6k
    return ret ? 1 : 0;
352
0
err:
353
0
    evp_pkey_ctx_free_old_ops(ctx);
354
0
    ctx->operation = EVP_PKEY_OP_UNDEFINED;
355
0
    EVP_KEYMGMT_free(tmp_keymgmt);
356
0
    return 0;
357
358
42
legacy:
359
    /*
360
     * If we don't have the full support we need with provided methods,
361
     * let's go see if legacy does.
362
     */
363
42
    ERR_pop_to_mark();
364
365
#ifdef FIPS_MODULE
366
    return 0;
367
#else
368
42
    if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
369
42
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
370
42
        return -2;
371
42
    }
372
373
0
    if (ctx->pmeth->derive_init == NULL)
374
0
        return 1;
375
0
    ret = ctx->pmeth->derive_init(ctx);
376
0
    if (ret <= 0)
377
0
        ctx->operation = EVP_PKEY_OP_UNDEFINED;
378
0
    EVP_KEYMGMT_free(tmp_keymgmt);
379
0
    return ret;
380
0
#endif
381
0
}
382
383
int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer,
384
    int validate_peer)
385
14.8k
{
386
14.8k
    int ret = 0, check;
387
14.8k
    void *provkey = NULL;
388
14.8k
    EVP_PKEY_CTX *check_ctx = NULL;
389
14.8k
    EVP_KEYMGMT *tmp_keymgmt = NULL, *tmp_keymgmt_tofree = NULL;
390
391
14.8k
    if (ctx == NULL) {
392
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
393
0
        return -1;
394
0
    }
395
396
14.8k
    if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.algctx == NULL)
397
42
        goto legacy;
398
399
14.7k
    if (ctx->op.kex.exchange->set_peer == NULL) {
400
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
401
0
        return -2;
402
0
    }
403
404
14.7k
    if (validate_peer) {
405
14.7k
        check_ctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, peer, ctx->propquery);
406
14.7k
        if (check_ctx == NULL)
407
0
            return -1;
408
14.7k
        check = EVP_PKEY_public_check(check_ctx);
409
14.7k
        EVP_PKEY_CTX_free(check_ctx);
410
14.7k
        if (check <= 0)
411
12
            return -1;
412
14.7k
    }
413
414
    /*
415
     * Ensure that the |peer| is provided, either natively, or as a cached
416
     * export.  We start by fetching the keymgmt with the same name as
417
     * |ctx->keymgmt|, but from the provider of the exchange method, using
418
     * the same property query as when fetching the exchange method.
419
     * With the keymgmt we found (if we did), we try to export |peer|
420
     * to it (evp_pkey_export_to_provider() is smart enough to only actually
421
     * export it if |tmp_keymgmt| is different from |peer|'s keymgmt)
422
     */
423
14.7k
    tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)
424
14.7k
                                                                       EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange),
425
14.7k
        EVP_KEYMGMT_get0_name(ctx->keymgmt),
426
14.7k
        ctx->propquery);
427
14.7k
    if (tmp_keymgmt != NULL)
428
        /* A Coverity issue with up_ref/down_ref and free */
429
        /* coverity[pass_freed_arg] */
430
14.7k
        provkey = evp_pkey_export_to_provider(peer, ctx->libctx,
431
14.7k
            &tmp_keymgmt, ctx->propquery);
432
14.7k
    EVP_KEYMGMT_free(tmp_keymgmt_tofree);
433
434
    /*
435
     * If making the key provided wasn't possible, legacy may be able to pick
436
     * it up
437
     */
438
14.7k
    if (provkey == NULL)
439
0
        goto legacy;
440
14.7k
    ret = ctx->op.kex.exchange->set_peer(ctx->op.kex.algctx, provkey);
441
14.7k
    if (ret <= 0)
442
0
        return ret;
443
14.7k
    goto common;
444
445
14.7k
legacy:
446
#ifdef FIPS_MODULE
447
    return ret;
448
#else
449
42
    if (ctx->pmeth == NULL
450
0
        || !(ctx->pmeth->derive != NULL
451
0
            || ctx->pmeth->encrypt != NULL
452
0
            || ctx->pmeth->decrypt != NULL)
453
42
        || ctx->pmeth->ctrl == NULL) {
454
42
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
455
42
        return -2;
456
42
    }
457
0
    if (ctx->operation != EVP_PKEY_OP_DERIVE
458
0
        && ctx->operation != EVP_PKEY_OP_ENCRYPT
459
0
        && ctx->operation != EVP_PKEY_OP_DECRYPT) {
460
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
461
0
        return -1;
462
0
    }
463
464
0
    ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
465
466
0
    if (ret <= 0)
467
0
        return ret;
468
469
0
    if (ret == 2)
470
0
        return 1;
471
472
0
    if (ctx->pkey == NULL) {
473
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
474
0
        return -1;
475
0
    }
476
477
0
    if (ctx->pkey->type != peer->type) {
478
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
479
0
        return -1;
480
0
    }
481
482
    /*
483
     * For clarity.  The error is if parameters in peer are
484
     * present (!missing) but don't match.  EVP_PKEY_parameters_eq may return
485
     * 1 (match), 0 (don't match) and -2 (comparison is not defined).  -1
486
     * (different key types) is impossible here because it is checked earlier.
487
     * -2 is OK for us here, as well as 1, so we can check for 0 only.
488
     */
489
0
    if (!EVP_PKEY_missing_parameters(peer) && !EVP_PKEY_parameters_eq(ctx->pkey, peer)) {
490
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
491
0
        return -1;
492
0
    }
493
494
0
    ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
495
0
    if (ret <= 0)
496
0
        return ret;
497
0
#endif
498
499
14.7k
common:
500
14.7k
    if (!EVP_PKEY_up_ref(peer))
501
0
        return -1;
502
503
14.7k
    EVP_PKEY_free(ctx->peerkey);
504
14.7k
    ctx->peerkey = peer;
505
506
14.7k
    return 1;
507
14.7k
}
508
509
int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
510
41.7k
{
511
41.7k
    return EVP_PKEY_derive_set_peer_ex(ctx, peer, 1);
512
41.7k
}
513
514
int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
515
45.6k
{
516
45.6k
    int ret;
517
518
45.6k
    if (ctx == NULL || pkeylen == NULL) {
519
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
520
0
        return -1;
521
0
    }
522
523
45.6k
    if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
524
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
525
0
        return -1;
526
0
    }
527
528
45.6k
    if (ctx->op.kex.algctx == NULL)
529
42
        goto legacy;
530
531
45.5k
    ret = ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, pkeylen,
532
45.5k
        key != NULL ? *pkeylen : 0);
533
534
45.5k
    return ret;
535
42
legacy:
536
42
    if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
537
42
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
538
42
        return -2;
539
42
    }
540
541
0
    M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE) return ctx->pmeth->derive(ctx, key, pkeylen);
542
0
}
543
544
int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch)
545
0
{
546
0
    return keyexch->name_id;
547
0
}
548
549
const char *EVP_KEYEXCH_get0_name(const EVP_KEYEXCH *keyexch)
550
0
{
551
0
    return keyexch->type_name;
552
0
}
553
554
const char *EVP_KEYEXCH_get0_description(const EVP_KEYEXCH *keyexch)
555
0
{
556
0
    return keyexch->description;
557
0
}
558
559
int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
560
0
{
561
0
    return keyexch != NULL
562
0
        && evp_is_a(keyexch->prov, keyexch->name_id, NULL, name);
563
0
}
564
565
void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx,
566
    void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
567
    void *arg)
568
8
{
569
8
    evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
570
8
        (void (*)(void *, void *))fn, arg,
571
8
        evp_keyexch_from_algorithm,
572
8
        evp_keyexch_up_ref,
573
8
        evp_keyexch_free);
574
8
}
575
576
int EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
577
    void (*fn)(const char *name, void *data),
578
    void *data)
579
0
{
580
0
    if (keyexch->prov != NULL)
581
0
        return evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);
582
583
0
    return 1;
584
0
}
585
586
const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch)
587
0
{
588
0
    void *provctx;
589
590
0
    if (keyexch == NULL || keyexch->gettable_ctx_params == NULL)
591
0
        return NULL;
592
593
0
    provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
594
0
    return keyexch->gettable_ctx_params(NULL, provctx);
595
0
}
596
597
const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch)
598
94
{
599
94
    void *provctx;
600
601
94
    if (keyexch == NULL || keyexch->settable_ctx_params == NULL)
602
4
        return NULL;
603
90
    provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
604
    return keyexch->settable_ctx_params(NULL, provctx);
605
94
}