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/refcount.h"
14 #include "crypto/evp.h"
15 #include "internal/provider.h"
16 #include "internal/numbers.h" /* includes SIZE_MAX */
17 #include "evp_local.h"
19 static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov)
21 EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH));
23 if (exchange == NULL) {
24 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
28 exchange->lock = CRYPTO_THREAD_lock_new();
29 if (exchange->lock == NULL) {
30 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
31 OPENSSL_free(exchange);
34 exchange->prov = prov;
35 ossl_provider_up_ref(prov);
41 static void *evp_keyexch_from_dispatch(int name_id,
42 const OSSL_DISPATCH *fns,
45 EVP_KEYEXCH *exchange = NULL;
46 int fncnt = 0, sparamfncnt = 0, gparamfncnt = 0;
48 if ((exchange = evp_keyexch_new(prov)) == NULL) {
49 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
53 exchange->name_id = name_id;
55 for (; fns->function_id != 0; fns++) {
56 switch (fns->function_id) {
57 case OSSL_FUNC_KEYEXCH_NEWCTX:
58 if (exchange->newctx != NULL)
60 exchange->newctx = OSSL_FUNC_keyexch_newctx(fns);
63 case OSSL_FUNC_KEYEXCH_INIT:
64 if (exchange->init != NULL)
66 exchange->init = OSSL_FUNC_keyexch_init(fns);
69 case OSSL_FUNC_KEYEXCH_SET_PEER:
70 if (exchange->set_peer != NULL)
72 exchange->set_peer = OSSL_FUNC_keyexch_set_peer(fns);
74 case OSSL_FUNC_KEYEXCH_DERIVE:
75 if (exchange->derive != NULL)
77 exchange->derive = OSSL_FUNC_keyexch_derive(fns);
80 case OSSL_FUNC_KEYEXCH_FREECTX:
81 if (exchange->freectx != NULL)
83 exchange->freectx = OSSL_FUNC_keyexch_freectx(fns);
86 case OSSL_FUNC_KEYEXCH_DUPCTX:
87 if (exchange->dupctx != NULL)
89 exchange->dupctx = OSSL_FUNC_keyexch_dupctx(fns);
91 case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS:
92 if (exchange->get_ctx_params != NULL)
94 exchange->get_ctx_params = OSSL_FUNC_keyexch_get_ctx_params(fns);
97 case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS:
98 if (exchange->gettable_ctx_params != NULL)
100 exchange->gettable_ctx_params
101 = OSSL_FUNC_keyexch_gettable_ctx_params(fns);
104 case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
105 if (exchange->set_ctx_params != NULL)
107 exchange->set_ctx_params = OSSL_FUNC_keyexch_set_ctx_params(fns);
110 case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
111 if (exchange->settable_ctx_params != NULL)
113 exchange->settable_ctx_params
114 = OSSL_FUNC_keyexch_settable_ctx_params(fns);
120 || (gparamfncnt != 0 && gparamfncnt != 2)
121 || (sparamfncnt != 0 && sparamfncnt != 2)) {
123 * In order to be a consistent set of functions we must have at least
124 * a complete set of "exchange" functions: init, derive, newctx,
125 * and freectx. The set_ctx_params and settable_ctx_params functions are
126 * optional, but if one of them is present then the other one must also
127 * be present. Same goes for get_ctx_params and gettable_ctx_params.
128 * The dupctx and set_peer functions are optional.
130 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
137 EVP_KEYEXCH_free(exchange);
141 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
143 if (exchange != NULL) {
146 CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
149 ossl_provider_free(exchange->prov);
150 CRYPTO_THREAD_lock_free(exchange->lock);
151 OPENSSL_free(exchange);
155 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
159 CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
163 OSSL_PROVIDER *EVP_KEYEXCH_provider(const EVP_KEYEXCH *exchange)
165 return exchange->prov;
168 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
169 const char *properties)
171 return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
172 evp_keyexch_from_dispatch,
173 (int (*)(void *))EVP_KEYEXCH_up_ref,
174 (void (*)(void *))EVP_KEYEXCH_free);
177 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
179 return EVP_PKEY_derive_init_ex(ctx, NULL);
182 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
185 void *provkey = NULL;
186 EVP_KEYEXCH *exchange = NULL;
187 EVP_KEYMGMT *tmp_keymgmt = NULL;
188 const char *supported_exch = NULL;
191 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
195 evp_pkey_ctx_free_old_ops(ctx);
196 ctx->operation = EVP_PKEY_OP_DERIVE;
199 * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
200 * calls can be removed.
204 if (evp_pkey_ctx_is_legacy(ctx))
208 * Ensure that the key is provided, either natively, or as a cached export.
209 * If not, goto legacy
211 tmp_keymgmt = ctx->keymgmt;
212 if (ctx->pkey == NULL) {
214 * Some algorithms (e.g. legacy KDFs) don't have a pkey - so we create
217 EVP_PKEY *pkey = EVP_PKEY_new();
219 if (pkey == NULL || !EVP_PKEY_set_type_by_keymgmt(pkey, tmp_keymgmt)) {
220 ERR_clear_last_mark();
222 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
225 provkey = pkey->keydata = evp_keymgmt_newdata(tmp_keymgmt);
231 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
232 &tmp_keymgmt, ctx->propquery);
236 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
237 ERR_clear_last_mark();
238 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
241 EVP_KEYMGMT_free(ctx->keymgmt);
242 ctx->keymgmt = tmp_keymgmt;
244 if (ctx->keymgmt->query_operation_name != NULL)
245 supported_exch = ctx->keymgmt->query_operation_name(OSSL_OP_KEYEXCH);
248 * If we didn't get a supported exch, assume there is one with the
249 * same name as the key type.
251 if (supported_exch == NULL)
252 supported_exch = ctx->keytype;
255 * Because we cleared out old ops, we shouldn't need to worry about
256 * checking if exchange is already there.
258 exchange = EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery);
261 || (EVP_KEYMGMT_provider(ctx->keymgmt)
262 != EVP_KEYEXCH_provider(exchange))) {
264 * We don't need to free ctx->keymgmt here, as it's not necessarily
265 * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
267 EVP_KEYEXCH_free(exchange);
272 * TODO remove this when legacy is gone
273 * If we don't have the full support we need with provided methods,
274 * let's go see if legacy does.
278 /* No more legacy from here down to legacy: */
280 ctx->op.kex.exchange = exchange;
281 ctx->op.kex.exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
282 if (ctx->op.kex.exchprovctx == NULL) {
283 /* The provider key can stay in the cache */
284 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
287 ret = exchange->init(ctx->op.kex.exchprovctx, provkey, params);
291 evp_pkey_ctx_free_old_ops(ctx);
292 ctx->operation = EVP_PKEY_OP_UNDEFINED;
297 * TODO remove this when legacy is gone
298 * If we don't have the full support we need with provided methods,
299 * let's go see if legacy does.
306 if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
307 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
311 if (ctx->pmeth->derive_init == NULL)
313 ret = ctx->pmeth->derive_init(ctx);
315 ctx->operation = EVP_PKEY_OP_UNDEFINED;
320 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
323 void *provkey = NULL;
326 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
330 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.exchprovctx == NULL)
333 if (ctx->op.kex.exchange->set_peer == NULL) {
334 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
338 provkey = evp_pkey_export_to_provider(peer, ctx->libctx, &ctx->keymgmt,
341 * If making the key provided wasn't possible, legacy may be able to pick
346 return ctx->op.kex.exchange->set_peer(ctx->op.kex.exchprovctx, provkey);
352 if (ctx->pmeth == NULL
353 || !(ctx->pmeth->derive != NULL
354 || ctx->pmeth->encrypt != NULL
355 || ctx->pmeth->decrypt != NULL)
356 || ctx->pmeth->ctrl == NULL) {
357 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
360 if (ctx->operation != EVP_PKEY_OP_DERIVE
361 && ctx->operation != EVP_PKEY_OP_ENCRYPT
362 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
363 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
367 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
375 if (ctx->pkey == NULL) {
376 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
380 if (ctx->pkey->type != peer->type) {
381 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
386 * For clarity. The error is if parameters in peer are
387 * present (!missing) but don't match. EVP_PKEY_parameters_eq may return
388 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
389 * (different key types) is impossible here because it is checked earlier.
390 * -2 is OK for us here, as well as 1, so we can check for 0 only.
392 if (!EVP_PKEY_missing_parameters(peer) &&
393 !EVP_PKEY_parameters_eq(ctx->pkey, peer)) {
394 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
398 EVP_PKEY_free(ctx->peerkey);
401 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
408 EVP_PKEY_up_ref(peer);
413 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
417 if (ctx == NULL || pkeylen == NULL) {
418 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
422 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
423 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
427 if (ctx->op.kex.exchprovctx == NULL)
430 ret = ctx->op.kex.exchange->derive(ctx->op.kex.exchprovctx, key, pkeylen,
431 key != NULL ? *pkeylen : 0);
435 if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
436 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
440 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
441 return ctx->pmeth->derive(ctx, key, pkeylen);
444 int EVP_KEYEXCH_number(const EVP_KEYEXCH *keyexch)
446 return keyexch->name_id;
449 int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
451 return evp_is_a(keyexch->prov, keyexch->name_id, NULL, name);
454 void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx,
455 void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
458 evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
459 (void (*)(void *, void *))fn, arg,
460 evp_keyexch_from_dispatch,
461 (void (*)(void *))EVP_KEYEXCH_free);
464 int EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
465 void (*fn)(const char *name, void *data),
468 if (keyexch->prov != NULL)
469 return evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);
474 const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch)
478 if (keyexch == NULL || keyexch->gettable_ctx_params == NULL)
481 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(keyexch));
482 return keyexch->gettable_ctx_params(NULL, provctx);
485 const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch)
489 if (keyexch == NULL || keyexch->settable_ctx_params == NULL)
491 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(keyexch));
492 return keyexch->settable_ctx_params(NULL, provctx);