2 * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
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
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"
21 static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov)
23 EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH));
28 if (!CRYPTO_NEW_REF(&exchange->refcnt, 1)) {
29 OPENSSL_free(exchange);
32 exchange->prov = prov;
33 ossl_provider_up_ref(prov);
38 static void *evp_keyexch_from_algorithm(int name_id,
39 const OSSL_ALGORITHM *algodef,
42 const OSSL_DISPATCH *fns = algodef->implementation;
43 EVP_KEYEXCH *exchange = NULL;
44 int fncnt = 0, sparamfncnt = 0, gparamfncnt = 0;
46 if ((exchange = evp_keyexch_new(prov)) == NULL) {
47 ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
51 exchange->name_id = name_id;
52 if ((exchange->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
54 exchange->description = algodef->algorithm_description;
56 for (; fns->function_id != 0; fns++) {
57 switch (fns->function_id) {
58 case OSSL_FUNC_KEYEXCH_NEWCTX:
59 if (exchange->newctx != NULL)
61 exchange->newctx = OSSL_FUNC_keyexch_newctx(fns);
64 case OSSL_FUNC_KEYEXCH_INIT:
65 if (exchange->init != NULL)
67 exchange->init = OSSL_FUNC_keyexch_init(fns);
70 case OSSL_FUNC_KEYEXCH_SET_PEER:
71 if (exchange->set_peer != NULL)
73 exchange->set_peer = OSSL_FUNC_keyexch_set_peer(fns);
75 case OSSL_FUNC_KEYEXCH_DERIVE:
76 if (exchange->derive != NULL)
78 exchange->derive = OSSL_FUNC_keyexch_derive(fns);
81 case OSSL_FUNC_KEYEXCH_FREECTX:
82 if (exchange->freectx != NULL)
84 exchange->freectx = OSSL_FUNC_keyexch_freectx(fns);
87 case OSSL_FUNC_KEYEXCH_DUPCTX:
88 if (exchange->dupctx != NULL)
90 exchange->dupctx = OSSL_FUNC_keyexch_dupctx(fns);
92 case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS:
93 if (exchange->get_ctx_params != NULL)
95 exchange->get_ctx_params = OSSL_FUNC_keyexch_get_ctx_params(fns);
98 case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS:
99 if (exchange->gettable_ctx_params != NULL)
101 exchange->gettable_ctx_params
102 = OSSL_FUNC_keyexch_gettable_ctx_params(fns);
105 case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
106 if (exchange->set_ctx_params != NULL)
108 exchange->set_ctx_params = OSSL_FUNC_keyexch_set_ctx_params(fns);
111 case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
112 if (exchange->settable_ctx_params != NULL)
114 exchange->settable_ctx_params
115 = OSSL_FUNC_keyexch_settable_ctx_params(fns);
121 || (gparamfncnt != 0 && gparamfncnt != 2)
122 || (sparamfncnt != 0 && sparamfncnt != 2)) {
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.
131 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
138 EVP_KEYEXCH_free(exchange);
142 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
146 if (exchange == NULL)
148 CRYPTO_DOWN_REF(&exchange->refcnt, &i);
151 OPENSSL_free(exchange->type_name);
152 ossl_provider_free(exchange->prov);
153 CRYPTO_FREE_REF(&exchange->refcnt);
154 OPENSSL_free(exchange);
157 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
161 CRYPTO_UP_REF(&exchange->refcnt, &ref);
165 OSSL_PROVIDER *EVP_KEYEXCH_get0_provider(const EVP_KEYEXCH *exchange)
167 return exchange->prov;
170 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
171 const char *properties)
173 return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
174 evp_keyexch_from_algorithm,
175 (int (*)(void *))EVP_KEYEXCH_up_ref,
176 (void (*)(void *))EVP_KEYEXCH_free);
179 EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov,
180 const char *algorithm,
181 const char *properties)
183 return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYEXCH,
184 algorithm, properties,
185 evp_keyexch_from_algorithm,
186 (int (*)(void *))EVP_KEYEXCH_up_ref,
187 (void (*)(void *))EVP_KEYEXCH_free);
190 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
192 return EVP_PKEY_derive_init_ex(ctx, NULL);
195 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
198 void *provkey = NULL;
199 EVP_KEYEXCH *exchange = NULL;
200 EVP_KEYMGMT *tmp_keymgmt = NULL;
201 const OSSL_PROVIDER *tmp_prov = NULL;
202 const char *supported_exch = NULL;
206 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
210 evp_pkey_ctx_free_old_ops(ctx);
211 ctx->operation = EVP_PKEY_OP_DERIVE;
215 if (evp_pkey_ctx_is_legacy(ctx))
219 * Some algorithms (e.g. legacy KDFs) don't have a pkey - so we create
222 if (ctx->pkey == NULL) {
223 EVP_PKEY *pkey = EVP_PKEY_new();
226 || !EVP_PKEY_set_type_by_keymgmt(pkey, ctx->keymgmt)
227 || (pkey->keydata = evp_keymgmt_newdata(ctx->keymgmt)) == NULL) {
228 ERR_clear_last_mark();
230 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
237 * Try to derive the supported exch from |ctx->keymgmt|.
239 if (!ossl_assert(ctx->pkey->keymgmt == NULL
240 || ctx->pkey->keymgmt == ctx->keymgmt)) {
241 ERR_clear_last_mark();
242 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
245 supported_exch = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
247 if (supported_exch == NULL) {
248 ERR_clear_last_mark();
249 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
255 * We perform two iterations:
257 * 1. Do the normal exchange fetch, using the fetching data given by
259 * 2. Do the provider specific exchange fetch, from the same provider
262 * We then try to fetch the keymgmt from the same provider as the
263 * exchange, and try to export |ctx->pkey| to that keymgmt (when
264 * this keymgmt happens to be the same as |ctx->keymgmt|, the export
265 * is a no-op, but we call it anyway to not complicate the code even
267 * If the export call succeeds (returns a non-NULL provider key pointer),
268 * we're done and can perform the operation itself. If not, we perform
269 * the second iteration, or jump to legacy.
271 for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
272 EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
275 * If we're on the second iteration, free the results from the first.
276 * They are NULL on the first iteration, so no need to check what
277 * iteration we're on.
279 EVP_KEYEXCH_free(exchange);
280 EVP_KEYMGMT_free(tmp_keymgmt);
285 EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery);
286 if (exchange != NULL)
287 tmp_prov = EVP_KEYEXCH_get0_provider(exchange);
290 tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
292 evp_keyexch_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
293 supported_exch, ctx->propquery);
294 if (exchange == NULL)
298 if (exchange == NULL)
302 * Ensure that the key is provided, either natively, or as a cached
303 * export. We start by fetching the keymgmt with the same name as
304 * |ctx->keymgmt|, but from the provider of the exchange method, using
305 * the same property query as when fetching the exchange method.
306 * With the keymgmt we found (if we did), we try to export |ctx->pkey|
307 * to it (evp_pkey_export_to_provider() is smart enough to only actually
308 * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
310 tmp_keymgmt_tofree = tmp_keymgmt =
311 evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
312 EVP_KEYMGMT_get0_name(ctx->keymgmt),
314 if (tmp_keymgmt != NULL)
315 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
316 &tmp_keymgmt, ctx->propquery);
317 if (tmp_keymgmt == NULL)
318 EVP_KEYMGMT_free(tmp_keymgmt_tofree);
321 if (provkey == NULL) {
322 EVP_KEYEXCH_free(exchange);
328 /* No more legacy from here down to legacy: */
330 /* A Coverity false positive with up_ref/down_ref and free */
331 /* coverity[use_after_free] */
332 ctx->op.kex.exchange = exchange;
333 /* A Coverity false positive with up_ref/down_ref and free */
334 /* coverity[deref_arg] */
335 ctx->op.kex.algctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
336 if (ctx->op.kex.algctx == NULL) {
337 /* The provider key can stay in the cache */
338 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
341 ret = exchange->init(ctx->op.kex.algctx, provkey, params);
343 EVP_KEYMGMT_free(tmp_keymgmt);
346 evp_pkey_ctx_free_old_ops(ctx);
347 ctx->operation = EVP_PKEY_OP_UNDEFINED;
348 EVP_KEYMGMT_free(tmp_keymgmt);
353 * If we don't have the full support we need with provided methods,
354 * let's go see if legacy does.
361 if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
362 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
366 if (ctx->pmeth->derive_init == NULL)
368 ret = ctx->pmeth->derive_init(ctx);
370 ctx->operation = EVP_PKEY_OP_UNDEFINED;
371 EVP_KEYMGMT_free(tmp_keymgmt);
376 int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer,
380 void *provkey = NULL;
381 EVP_PKEY_CTX *check_ctx = NULL;
382 EVP_KEYMGMT *tmp_keymgmt = NULL, *tmp_keymgmt_tofree = NULL;
385 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
389 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.algctx == NULL)
392 if (ctx->op.kex.exchange->set_peer == NULL) {
393 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
398 check_ctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, peer, ctx->propquery);
399 if (check_ctx == NULL)
401 check = EVP_PKEY_public_check(check_ctx);
402 EVP_PKEY_CTX_free(check_ctx);
408 * Ensure that the |peer| is provided, either natively, or as a cached
409 * export. We start by fetching the keymgmt with the same name as
410 * |ctx->keymgmt|, but from the provider of the exchange method, using
411 * the same property query as when fetching the exchange method.
412 * With the keymgmt we found (if we did), we try to export |peer|
413 * to it (evp_pkey_export_to_provider() is smart enough to only actually
414 * export it if |tmp_keymgmt| is different from |peer|'s keymgmt)
416 tmp_keymgmt_tofree = tmp_keymgmt =
417 evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)
418 EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange),
419 EVP_KEYMGMT_get0_name(ctx->keymgmt),
421 if (tmp_keymgmt != NULL)
422 /* A Coverity issue with up_ref/down_ref and free */
423 /* coverity[pass_freed_arg] */
424 provkey = evp_pkey_export_to_provider(peer, ctx->libctx,
425 &tmp_keymgmt, ctx->propquery);
426 EVP_KEYMGMT_free(tmp_keymgmt_tofree);
429 * If making the key provided wasn't possible, legacy may be able to pick
434 return ctx->op.kex.exchange->set_peer(ctx->op.kex.algctx, provkey);
440 if (ctx->pmeth == NULL
441 || !(ctx->pmeth->derive != NULL
442 || ctx->pmeth->encrypt != NULL
443 || ctx->pmeth->decrypt != NULL)
444 || ctx->pmeth->ctrl == NULL) {
445 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
448 if (ctx->operation != EVP_PKEY_OP_DERIVE
449 && ctx->operation != EVP_PKEY_OP_ENCRYPT
450 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
451 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
455 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
463 if (ctx->pkey == NULL) {
464 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
468 if (ctx->pkey->type != peer->type) {
469 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
474 * For clarity. The error is if parameters in peer are
475 * present (!missing) but don't match. EVP_PKEY_parameters_eq may return
476 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
477 * (different key types) is impossible here because it is checked earlier.
478 * -2 is OK for us here, as well as 1, so we can check for 0 only.
480 if (!EVP_PKEY_missing_parameters(peer) &&
481 !EVP_PKEY_parameters_eq(ctx->pkey, peer)) {
482 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
486 EVP_PKEY_free(ctx->peerkey);
489 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
496 EVP_PKEY_up_ref(peer);
501 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
503 return EVP_PKEY_derive_set_peer_ex(ctx, peer, 1);
506 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
510 if (ctx == NULL || pkeylen == NULL) {
511 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
515 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
516 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
520 if (ctx->op.kex.algctx == NULL)
523 ret = ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, pkeylen,
524 key != NULL ? *pkeylen : 0);
528 if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
529 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
533 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
534 return ctx->pmeth->derive(ctx, key, pkeylen);
537 int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch)
539 return keyexch->name_id;
542 const char *EVP_KEYEXCH_get0_name(const EVP_KEYEXCH *keyexch)
544 return keyexch->type_name;
547 const char *EVP_KEYEXCH_get0_description(const EVP_KEYEXCH *keyexch)
549 return keyexch->description;
552 int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
554 return keyexch != NULL
555 && evp_is_a(keyexch->prov, keyexch->name_id, NULL, name);
558 void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx,
559 void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
562 evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
563 (void (*)(void *, void *))fn, arg,
564 evp_keyexch_from_algorithm,
565 (int (*)(void *))EVP_KEYEXCH_up_ref,
566 (void (*)(void *))EVP_KEYEXCH_free);
569 int EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
570 void (*fn)(const char *name, void *data),
573 if (keyexch->prov != NULL)
574 return evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);
579 const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch)
583 if (keyexch == NULL || keyexch->gettable_ctx_params == NULL)
586 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
587 return keyexch->gettable_ctx_params(NULL, provctx);
590 const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch)
594 if (keyexch == NULL || keyexch->settable_ctx_params == NULL)
596 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
597 return keyexch->settable_ctx_params(NULL, provctx);