2 * Copyright 2019-2021 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));
25 if (exchange == NULL) {
26 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
30 exchange->lock = CRYPTO_THREAD_lock_new();
31 if (exchange->lock == NULL) {
32 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
33 OPENSSL_free(exchange);
36 exchange->prov = prov;
37 ossl_provider_up_ref(prov);
43 static void *evp_keyexch_from_algorithm(int name_id,
44 const OSSL_ALGORITHM *algodef,
47 const OSSL_DISPATCH *fns = algodef->implementation;
48 EVP_KEYEXCH *exchange = NULL;
49 int fncnt = 0, sparamfncnt = 0, gparamfncnt = 0;
51 if ((exchange = evp_keyexch_new(prov)) == NULL) {
52 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
56 exchange->name_id = name_id;
57 if ((exchange->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
59 exchange->description = algodef->algorithm_description;
61 for (; fns->function_id != 0; fns++) {
62 switch (fns->function_id) {
63 case OSSL_FUNC_KEYEXCH_NEWCTX:
64 if (exchange->newctx != NULL)
66 exchange->newctx = OSSL_FUNC_keyexch_newctx(fns);
69 case OSSL_FUNC_KEYEXCH_INIT:
70 if (exchange->init != NULL)
72 exchange->init = OSSL_FUNC_keyexch_init(fns);
75 case OSSL_FUNC_KEYEXCH_SET_PEER:
76 if (exchange->set_peer != NULL)
78 exchange->set_peer = OSSL_FUNC_keyexch_set_peer(fns);
80 case OSSL_FUNC_KEYEXCH_DERIVE:
81 if (exchange->derive != NULL)
83 exchange->derive = OSSL_FUNC_keyexch_derive(fns);
86 case OSSL_FUNC_KEYEXCH_FREECTX:
87 if (exchange->freectx != NULL)
89 exchange->freectx = OSSL_FUNC_keyexch_freectx(fns);
92 case OSSL_FUNC_KEYEXCH_DUPCTX:
93 if (exchange->dupctx != NULL)
95 exchange->dupctx = OSSL_FUNC_keyexch_dupctx(fns);
97 case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS:
98 if (exchange->get_ctx_params != NULL)
100 exchange->get_ctx_params = OSSL_FUNC_keyexch_get_ctx_params(fns);
103 case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS:
104 if (exchange->gettable_ctx_params != NULL)
106 exchange->gettable_ctx_params
107 = OSSL_FUNC_keyexch_gettable_ctx_params(fns);
110 case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
111 if (exchange->set_ctx_params != NULL)
113 exchange->set_ctx_params = OSSL_FUNC_keyexch_set_ctx_params(fns);
116 case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
117 if (exchange->settable_ctx_params != NULL)
119 exchange->settable_ctx_params
120 = OSSL_FUNC_keyexch_settable_ctx_params(fns);
126 || (gparamfncnt != 0 && gparamfncnt != 2)
127 || (sparamfncnt != 0 && sparamfncnt != 2)) {
129 * In order to be a consistent set of functions we must have at least
130 * a complete set of "exchange" functions: init, derive, newctx,
131 * and freectx. The set_ctx_params and settable_ctx_params functions are
132 * optional, but if one of them is present then the other one must also
133 * be present. Same goes for get_ctx_params and gettable_ctx_params.
134 * The dupctx and set_peer functions are optional.
136 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
143 EVP_KEYEXCH_free(exchange);
147 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
151 if (exchange == NULL)
153 CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
156 OPENSSL_free(exchange->type_name);
157 ossl_provider_free(exchange->prov);
158 CRYPTO_THREAD_lock_free(exchange->lock);
159 OPENSSL_free(exchange);
162 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
166 CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
170 OSSL_PROVIDER *EVP_KEYEXCH_get0_provider(const EVP_KEYEXCH *exchange)
172 return exchange->prov;
175 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
176 const char *properties)
178 return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
179 evp_keyexch_from_algorithm,
180 (int (*)(void *))EVP_KEYEXCH_up_ref,
181 (void (*)(void *))EVP_KEYEXCH_free);
184 EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov,
185 const char *algorithm,
186 const char *properties)
188 return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYEXCH,
189 algorithm, properties,
190 evp_keyexch_from_algorithm,
191 (int (*)(void *))EVP_KEYEXCH_up_ref,
192 (void (*)(void *))EVP_KEYEXCH_free);
195 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
197 return EVP_PKEY_derive_init_ex(ctx, NULL);
200 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
203 void *provkey = NULL;
204 EVP_KEYEXCH *exchange = NULL;
205 EVP_KEYMGMT *tmp_keymgmt = NULL;
206 const OSSL_PROVIDER *tmp_prov = NULL;
207 const char *supported_exch = NULL;
211 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
215 evp_pkey_ctx_free_old_ops(ctx);
216 ctx->operation = EVP_PKEY_OP_DERIVE;
220 if (evp_pkey_ctx_is_legacy(ctx))
224 * Some algorithms (e.g. legacy KDFs) don't have a pkey - so we create
227 if (ctx->pkey == NULL) {
228 EVP_PKEY *pkey = EVP_PKEY_new();
231 || !EVP_PKEY_set_type_by_keymgmt(pkey, ctx->keymgmt)
232 || (pkey->keydata = evp_keymgmt_newdata(ctx->keymgmt)) == NULL) {
233 ERR_clear_last_mark();
235 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
242 * Try to derive the supported exch from |ctx->keymgmt|.
244 if (!ossl_assert(ctx->pkey->keymgmt == NULL
245 || ctx->pkey->keymgmt == ctx->keymgmt)) {
246 ERR_clear_last_mark();
247 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
250 supported_exch = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
252 if (supported_exch == NULL) {
253 ERR_clear_last_mark();
254 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
260 * We perform two iterations:
262 * 1. Do the normal exchange fetch, using the fetching data given by
264 * 2. Do the provider specific exchange fetch, from the same provider
267 * We then try to fetch the keymgmt from the same provider as the
268 * exchange, and try to export |ctx->pkey| to that keymgmt (when
269 * this keymgmt happens to be the same as |ctx->keymgmt|, the export
270 * is a no-op, but we call it anyway to not complicate the code even
272 * If the export call succeeds (returns a non-NULL provider key pointer),
273 * we're done and can perform the operation itself. If not, we perform
274 * the second iteration, or jump to legacy.
276 for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
277 EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
280 * If we're on the second iteration, free the results from the first.
281 * They are NULL on the first iteration, so no need to check what
282 * iteration we're on.
284 EVP_KEYEXCH_free(exchange);
285 EVP_KEYMGMT_free(tmp_keymgmt);
290 EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery);
291 if (exchange != NULL)
292 tmp_prov = EVP_KEYEXCH_get0_provider(exchange);
295 tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
297 evp_keyexch_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
298 supported_exch, ctx->propquery);
299 if (exchange == NULL)
303 if (exchange == NULL)
307 * Ensure that the key is provided, either natively, or as a cached
308 * export. We start by fetching the keymgmt with the same name as
309 * |ctx->keymgmt|, but from the provider of the exchange method, using
310 * the same property query as when fetching the exchange method.
311 * With the keymgmt we found (if we did), we try to export |ctx->pkey|
312 * to it (evp_pkey_export_to_provider() is smart enough to only actually
313 * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
315 tmp_keymgmt_tofree = tmp_keymgmt =
316 evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
317 EVP_KEYMGMT_get0_name(ctx->keymgmt),
319 if (tmp_keymgmt != NULL)
320 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
321 &tmp_keymgmt, ctx->propquery);
322 if (tmp_keymgmt == NULL)
323 EVP_KEYMGMT_free(tmp_keymgmt_tofree);
326 if (provkey == NULL) {
327 EVP_KEYEXCH_free(exchange);
333 /* No more legacy from here down to legacy: */
335 ctx->op.kex.exchange = exchange;
336 ctx->op.kex.algctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
337 if (ctx->op.kex.algctx == NULL) {
338 /* The provider key can stay in the cache */
339 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
342 ret = exchange->init(ctx->op.kex.algctx, provkey, params);
344 EVP_KEYMGMT_free(tmp_keymgmt);
347 evp_pkey_ctx_free_old_ops(ctx);
348 ctx->operation = EVP_PKEY_OP_UNDEFINED;
349 EVP_KEYMGMT_free(tmp_keymgmt);
354 * If we don't have the full support we need with provided methods,
355 * let's go see if legacy does.
362 if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
363 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
367 if (ctx->pmeth->derive_init == NULL)
369 ret = ctx->pmeth->derive_init(ctx);
371 ctx->operation = EVP_PKEY_OP_UNDEFINED;
372 EVP_KEYMGMT_free(tmp_keymgmt);
377 int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer,
381 void *provkey = NULL;
382 EVP_PKEY_CTX *check_ctx = NULL;
383 EVP_KEYMGMT *tmp_keymgmt = NULL, *tmp_keymgmt_tofree = NULL;
386 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
390 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.algctx == NULL)
393 if (ctx->op.kex.exchange->set_peer == NULL) {
394 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
399 check_ctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, peer, ctx->propquery);
400 if (check_ctx == NULL)
402 check = EVP_PKEY_public_check(check_ctx);
403 EVP_PKEY_CTX_free(check_ctx);
409 * Ensure that the |peer| is provided, either natively, or as a cached
410 * export. We start by fetching the keymgmt with the same name as
411 * |ctx->keymgmt|, but from the provider of the exchange method, using
412 * the same property query as when fetching the exchange method.
413 * With the keymgmt we found (if we did), we try to export |peer|
414 * to it (evp_pkey_export_to_provider() is smart enough to only actually
415 * export it if |tmp_keymgmt| is different from |peer|'s keymgmt)
417 tmp_keymgmt_tofree = tmp_keymgmt =
418 evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)
419 EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange),
420 EVP_KEYMGMT_get0_name(ctx->keymgmt),
422 if (tmp_keymgmt != NULL)
423 provkey = evp_pkey_export_to_provider(peer, ctx->libctx,
424 &tmp_keymgmt, ctx->propquery);
425 EVP_KEYMGMT_free(tmp_keymgmt_tofree);
428 * If making the key provided wasn't possible, legacy may be able to pick
433 return ctx->op.kex.exchange->set_peer(ctx->op.kex.algctx, provkey);
439 if (ctx->pmeth == NULL
440 || !(ctx->pmeth->derive != NULL
441 || ctx->pmeth->encrypt != NULL
442 || ctx->pmeth->decrypt != NULL)
443 || ctx->pmeth->ctrl == NULL) {
444 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
447 if (ctx->operation != EVP_PKEY_OP_DERIVE
448 && ctx->operation != EVP_PKEY_OP_ENCRYPT
449 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
450 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
454 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
462 if (ctx->pkey == NULL) {
463 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
467 if (ctx->pkey->type != peer->type) {
468 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
473 * For clarity. The error is if parameters in peer are
474 * present (!missing) but don't match. EVP_PKEY_parameters_eq may return
475 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
476 * (different key types) is impossible here because it is checked earlier.
477 * -2 is OK for us here, as well as 1, so we can check for 0 only.
479 if (!EVP_PKEY_missing_parameters(peer) &&
480 !EVP_PKEY_parameters_eq(ctx->pkey, peer)) {
481 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
485 EVP_PKEY_free(ctx->peerkey);
488 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
495 EVP_PKEY_up_ref(peer);
500 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
502 return EVP_PKEY_derive_set_peer_ex(ctx, peer, 1);
505 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
509 if (ctx == NULL || pkeylen == NULL) {
510 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
514 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
515 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
519 if (ctx->op.kex.algctx == NULL)
522 ret = ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, pkeylen,
523 key != NULL ? *pkeylen : 0);
527 if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
528 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
532 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
533 return ctx->pmeth->derive(ctx, key, pkeylen);
536 int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch)
538 return keyexch->name_id;
541 const char *EVP_KEYEXCH_get0_name(const EVP_KEYEXCH *keyexch)
543 return keyexch->type_name;
546 const char *EVP_KEYEXCH_get0_description(const EVP_KEYEXCH *keyexch)
548 return keyexch->description;
551 int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
553 return evp_is_a(keyexch->prov, keyexch->name_id, NULL, name);
556 void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx,
557 void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
560 evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
561 (void (*)(void *, void *))fn, arg,
562 evp_keyexch_from_algorithm,
563 (int (*)(void *))EVP_KEYEXCH_up_ref,
564 (void (*)(void *))EVP_KEYEXCH_free);
567 int EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
568 void (*fn)(const char *name, void *data),
571 if (keyexch->prov != NULL)
572 return evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);
577 const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch)
581 if (keyexch == NULL || keyexch->gettable_ctx_params == NULL)
584 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
585 return keyexch->gettable_ctx_params(NULL, provctx);
588 const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch)
592 if (keyexch == NULL || keyexch->settable_ctx_params == NULL)
594 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
595 return keyexch->settable_ctx_params(NULL, provctx);