2 * Copyright 2019-2022 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 exchange->lock = CRYPTO_THREAD_lock_new();
29 if (exchange->lock == NULL) {
30 ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
31 OPENSSL_free(exchange);
34 exchange->prov = prov;
35 ossl_provider_up_ref(prov);
41 static void *evp_keyexch_from_algorithm(int name_id,
42 const OSSL_ALGORITHM *algodef,
45 const OSSL_DISPATCH *fns = algodef->implementation;
46 EVP_KEYEXCH *exchange = NULL;
47 int fncnt = 0, sparamfncnt = 0, gparamfncnt = 0;
49 if ((exchange = evp_keyexch_new(prov)) == NULL) {
50 ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
54 exchange->name_id = name_id;
55 if ((exchange->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
57 exchange->description = algodef->algorithm_description;
59 for (; fns->function_id != 0; fns++) {
60 switch (fns->function_id) {
61 case OSSL_FUNC_KEYEXCH_NEWCTX:
62 if (exchange->newctx != NULL)
64 exchange->newctx = OSSL_FUNC_keyexch_newctx(fns);
67 case OSSL_FUNC_KEYEXCH_INIT:
68 if (exchange->init != NULL)
70 exchange->init = OSSL_FUNC_keyexch_init(fns);
73 case OSSL_FUNC_KEYEXCH_SET_PEER:
74 if (exchange->set_peer != NULL)
76 exchange->set_peer = OSSL_FUNC_keyexch_set_peer(fns);
78 case OSSL_FUNC_KEYEXCH_DERIVE:
79 if (exchange->derive != NULL)
81 exchange->derive = OSSL_FUNC_keyexch_derive(fns);
84 case OSSL_FUNC_KEYEXCH_FREECTX:
85 if (exchange->freectx != NULL)
87 exchange->freectx = OSSL_FUNC_keyexch_freectx(fns);
90 case OSSL_FUNC_KEYEXCH_DUPCTX:
91 if (exchange->dupctx != NULL)
93 exchange->dupctx = OSSL_FUNC_keyexch_dupctx(fns);
95 case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS:
96 if (exchange->get_ctx_params != NULL)
98 exchange->get_ctx_params = OSSL_FUNC_keyexch_get_ctx_params(fns);
101 case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS:
102 if (exchange->gettable_ctx_params != NULL)
104 exchange->gettable_ctx_params
105 = OSSL_FUNC_keyexch_gettable_ctx_params(fns);
108 case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
109 if (exchange->set_ctx_params != NULL)
111 exchange->set_ctx_params = OSSL_FUNC_keyexch_set_ctx_params(fns);
114 case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
115 if (exchange->settable_ctx_params != NULL)
117 exchange->settable_ctx_params
118 = OSSL_FUNC_keyexch_settable_ctx_params(fns);
124 || (gparamfncnt != 0 && gparamfncnt != 2)
125 || (sparamfncnt != 0 && sparamfncnt != 2)) {
127 * In order to be a consistent set of functions we must have at least
128 * a complete set of "exchange" functions: init, derive, newctx,
129 * and freectx. The set_ctx_params and settable_ctx_params functions are
130 * optional, but if one of them is present then the other one must also
131 * be present. Same goes for get_ctx_params and gettable_ctx_params.
132 * The dupctx and set_peer functions are optional.
134 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
141 EVP_KEYEXCH_free(exchange);
145 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
149 if (exchange == NULL)
151 CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
154 OPENSSL_free(exchange->type_name);
155 ossl_provider_free(exchange->prov);
156 CRYPTO_THREAD_lock_free(exchange->lock);
157 OPENSSL_free(exchange);
160 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
164 CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
168 OSSL_PROVIDER *EVP_KEYEXCH_get0_provider(const EVP_KEYEXCH *exchange)
170 return exchange->prov;
173 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
174 const char *properties)
176 return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
177 evp_keyexch_from_algorithm,
178 (int (*)(void *))EVP_KEYEXCH_up_ref,
179 (void (*)(void *))EVP_KEYEXCH_free);
182 EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov,
183 const char *algorithm,
184 const char *properties)
186 return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYEXCH,
187 algorithm, properties,
188 evp_keyexch_from_algorithm,
189 (int (*)(void *))EVP_KEYEXCH_up_ref,
190 (void (*)(void *))EVP_KEYEXCH_free);
193 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
195 return EVP_PKEY_derive_init_ex(ctx, NULL);
198 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
201 void *provkey = NULL;
202 EVP_KEYEXCH *exchange = NULL;
203 EVP_KEYMGMT *tmp_keymgmt = NULL;
204 const OSSL_PROVIDER *tmp_prov = NULL;
205 const char *supported_exch = NULL;
209 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
213 evp_pkey_ctx_free_old_ops(ctx);
214 ctx->operation = EVP_PKEY_OP_DERIVE;
218 if (evp_pkey_ctx_is_legacy(ctx))
222 * Some algorithms (e.g. legacy KDFs) don't have a pkey - so we create
225 if (ctx->pkey == NULL) {
226 EVP_PKEY *pkey = EVP_PKEY_new();
229 || !EVP_PKEY_set_type_by_keymgmt(pkey, ctx->keymgmt)
230 || (pkey->keydata = evp_keymgmt_newdata(ctx->keymgmt)) == NULL) {
231 ERR_clear_last_mark();
233 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
240 * Try to derive the supported exch from |ctx->keymgmt|.
242 if (!ossl_assert(ctx->pkey->keymgmt == NULL
243 || ctx->pkey->keymgmt == ctx->keymgmt)) {
244 ERR_clear_last_mark();
245 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
248 supported_exch = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
250 if (supported_exch == NULL) {
251 ERR_clear_last_mark();
252 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
258 * We perform two iterations:
260 * 1. Do the normal exchange fetch, using the fetching data given by
262 * 2. Do the provider specific exchange fetch, from the same provider
265 * We then try to fetch the keymgmt from the same provider as the
266 * exchange, and try to export |ctx->pkey| to that keymgmt (when
267 * this keymgmt happens to be the same as |ctx->keymgmt|, the export
268 * is a no-op, but we call it anyway to not complicate the code even
270 * If the export call succeeds (returns a non-NULL provider key pointer),
271 * we're done and can perform the operation itself. If not, we perform
272 * the second iteration, or jump to legacy.
274 for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
275 EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
278 * If we're on the second iteration, free the results from the first.
279 * They are NULL on the first iteration, so no need to check what
280 * iteration we're on.
282 EVP_KEYEXCH_free(exchange);
283 EVP_KEYMGMT_free(tmp_keymgmt);
288 EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery);
289 if (exchange != NULL)
290 tmp_prov = EVP_KEYEXCH_get0_provider(exchange);
293 tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
295 evp_keyexch_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
296 supported_exch, ctx->propquery);
297 if (exchange == NULL)
301 if (exchange == NULL)
305 * Ensure that the key is provided, either natively, or as a cached
306 * export. We start by fetching the keymgmt with the same name as
307 * |ctx->keymgmt|, but from the provider of the exchange method, using
308 * the same property query as when fetching the exchange method.
309 * With the keymgmt we found (if we did), we try to export |ctx->pkey|
310 * to it (evp_pkey_export_to_provider() is smart enough to only actually
311 * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
313 tmp_keymgmt_tofree = tmp_keymgmt =
314 evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
315 EVP_KEYMGMT_get0_name(ctx->keymgmt),
317 if (tmp_keymgmt != NULL)
318 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
319 &tmp_keymgmt, ctx->propquery);
320 if (tmp_keymgmt == NULL)
321 EVP_KEYMGMT_free(tmp_keymgmt_tofree);
324 if (provkey == NULL) {
325 EVP_KEYEXCH_free(exchange);
331 /* No more legacy from here down to legacy: */
333 /* A Coverity false positive with up_ref/down_ref and free */
334 /* coverity[use_after_free] */
335 ctx->op.kex.exchange = exchange;
336 /* A Coverity false positive with up_ref/down_ref and free */
337 /* coverity[deref_arg] */
338 ctx->op.kex.algctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
339 if (ctx->op.kex.algctx == NULL) {
340 /* The provider key can stay in the cache */
341 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
344 ret = exchange->init(ctx->op.kex.algctx, provkey, params);
346 EVP_KEYMGMT_free(tmp_keymgmt);
349 evp_pkey_ctx_free_old_ops(ctx);
350 ctx->operation = EVP_PKEY_OP_UNDEFINED;
351 EVP_KEYMGMT_free(tmp_keymgmt);
356 * If we don't have the full support we need with provided methods,
357 * let's go see if legacy does.
364 if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
365 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
369 if (ctx->pmeth->derive_init == NULL)
371 ret = ctx->pmeth->derive_init(ctx);
373 ctx->operation = EVP_PKEY_OP_UNDEFINED;
374 EVP_KEYMGMT_free(tmp_keymgmt);
379 int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer,
383 void *provkey = NULL;
384 EVP_PKEY_CTX *check_ctx = NULL;
385 EVP_KEYMGMT *tmp_keymgmt = NULL, *tmp_keymgmt_tofree = NULL;
388 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
392 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.algctx == NULL)
395 if (ctx->op.kex.exchange->set_peer == NULL) {
396 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
401 check_ctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, peer, ctx->propquery);
402 if (check_ctx == NULL)
404 check = EVP_PKEY_public_check(check_ctx);
405 EVP_PKEY_CTX_free(check_ctx);
411 * Ensure that the |peer| is provided, either natively, or as a cached
412 * export. We start by fetching the keymgmt with the same name as
413 * |ctx->keymgmt|, but from the provider of the exchange method, using
414 * the same property query as when fetching the exchange method.
415 * With the keymgmt we found (if we did), we try to export |peer|
416 * to it (evp_pkey_export_to_provider() is smart enough to only actually
417 * export it if |tmp_keymgmt| is different from |peer|'s keymgmt)
419 tmp_keymgmt_tofree = tmp_keymgmt =
420 evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)
421 EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange),
422 EVP_KEYMGMT_get0_name(ctx->keymgmt),
424 if (tmp_keymgmt != NULL)
425 /* A Coverity issue with up_ref/down_ref and free */
426 /* coverity[pass_freed_arg] */
427 provkey = evp_pkey_export_to_provider(peer, ctx->libctx,
428 &tmp_keymgmt, ctx->propquery);
429 EVP_KEYMGMT_free(tmp_keymgmt_tofree);
432 * If making the key provided wasn't possible, legacy may be able to pick
437 return ctx->op.kex.exchange->set_peer(ctx->op.kex.algctx, provkey);
443 if (ctx->pmeth == NULL
444 || !(ctx->pmeth->derive != NULL
445 || ctx->pmeth->encrypt != NULL
446 || ctx->pmeth->decrypt != NULL)
447 || ctx->pmeth->ctrl == NULL) {
448 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
451 if (ctx->operation != EVP_PKEY_OP_DERIVE
452 && ctx->operation != EVP_PKEY_OP_ENCRYPT
453 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
454 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
458 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
466 if (ctx->pkey == NULL) {
467 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
471 if (ctx->pkey->type != peer->type) {
472 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
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.
483 if (!EVP_PKEY_missing_parameters(peer) &&
484 !EVP_PKEY_parameters_eq(ctx->pkey, peer)) {
485 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
489 EVP_PKEY_free(ctx->peerkey);
492 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
499 EVP_PKEY_up_ref(peer);
504 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
506 return EVP_PKEY_derive_set_peer_ex(ctx, peer, 1);
509 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
513 if (ctx == NULL || pkeylen == NULL) {
514 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
518 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
519 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
523 if (ctx->op.kex.algctx == NULL)
526 ret = ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, pkeylen,
527 key != NULL ? *pkeylen : 0);
531 if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
532 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
536 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
537 return ctx->pmeth->derive(ctx, key, pkeylen);
540 int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch)
542 return keyexch->name_id;
545 const char *EVP_KEYEXCH_get0_name(const EVP_KEYEXCH *keyexch)
547 return keyexch->type_name;
550 const char *EVP_KEYEXCH_get0_description(const EVP_KEYEXCH *keyexch)
552 return keyexch->description;
555 int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
557 return keyexch != NULL
558 && evp_is_a(keyexch->prov, keyexch->name_id, NULL, name);
561 void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx,
562 void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
565 evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
566 (void (*)(void *, void *))fn, arg,
567 evp_keyexch_from_algorithm,
568 (int (*)(void *))EVP_KEYEXCH_up_ref,
569 (void (*)(void *))EVP_KEYEXCH_free);
572 int EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
573 void (*fn)(const char *name, void *data),
576 if (keyexch->prov != NULL)
577 return evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);
582 const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch)
586 if (keyexch == NULL || keyexch->gettable_ctx_params == NULL)
589 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
590 return keyexch->gettable_ctx_params(NULL, provctx);
593 const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch)
597 if (keyexch == NULL || keyexch->settable_ctx_params == NULL)
599 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
600 return keyexch->settable_ctx_params(NULL, provctx);