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_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_MALLOC_FAILURE);
54 exchange->name_id = name_id;
55 exchange->description = algodef->algorithm_description;
57 for (; fns->function_id != 0; fns++) {
58 switch (fns->function_id) {
59 case OSSL_FUNC_KEYEXCH_NEWCTX:
60 if (exchange->newctx != NULL)
62 exchange->newctx = OSSL_FUNC_keyexch_newctx(fns);
65 case OSSL_FUNC_KEYEXCH_INIT:
66 if (exchange->init != NULL)
68 exchange->init = OSSL_FUNC_keyexch_init(fns);
71 case OSSL_FUNC_KEYEXCH_SET_PEER:
72 if (exchange->set_peer != NULL)
74 exchange->set_peer = OSSL_FUNC_keyexch_set_peer(fns);
76 case OSSL_FUNC_KEYEXCH_DERIVE:
77 if (exchange->derive != NULL)
79 exchange->derive = OSSL_FUNC_keyexch_derive(fns);
82 case OSSL_FUNC_KEYEXCH_FREECTX:
83 if (exchange->freectx != NULL)
85 exchange->freectx = OSSL_FUNC_keyexch_freectx(fns);
88 case OSSL_FUNC_KEYEXCH_DUPCTX:
89 if (exchange->dupctx != NULL)
91 exchange->dupctx = OSSL_FUNC_keyexch_dupctx(fns);
93 case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS:
94 if (exchange->get_ctx_params != NULL)
96 exchange->get_ctx_params = OSSL_FUNC_keyexch_get_ctx_params(fns);
99 case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS:
100 if (exchange->gettable_ctx_params != NULL)
102 exchange->gettable_ctx_params
103 = OSSL_FUNC_keyexch_gettable_ctx_params(fns);
106 case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
107 if (exchange->set_ctx_params != NULL)
109 exchange->set_ctx_params = OSSL_FUNC_keyexch_set_ctx_params(fns);
112 case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
113 if (exchange->settable_ctx_params != NULL)
115 exchange->settable_ctx_params
116 = OSSL_FUNC_keyexch_settable_ctx_params(fns);
122 || (gparamfncnt != 0 && gparamfncnt != 2)
123 || (sparamfncnt != 0 && sparamfncnt != 2)) {
125 * In order to be a consistent set of functions we must have at least
126 * a complete set of "exchange" functions: init, derive, newctx,
127 * and freectx. The set_ctx_params and settable_ctx_params functions are
128 * optional, but if one of them is present then the other one must also
129 * be present. Same goes for get_ctx_params and gettable_ctx_params.
130 * The dupctx and set_peer functions are optional.
132 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
139 EVP_KEYEXCH_free(exchange);
143 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
145 if (exchange != NULL) {
148 CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
151 ossl_provider_free(exchange->prov);
152 CRYPTO_THREAD_lock_free(exchange->lock);
153 OPENSSL_free(exchange);
157 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
161 CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
165 OSSL_PROVIDER *EVP_KEYEXCH_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 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
181 return EVP_PKEY_derive_init_ex(ctx, NULL);
184 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
187 void *provkey = NULL;
188 EVP_KEYEXCH *exchange = NULL;
189 EVP_KEYMGMT *tmp_keymgmt = NULL;
190 const char *supported_exch = NULL;
193 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
197 evp_pkey_ctx_free_old_ops(ctx);
198 ctx->operation = EVP_PKEY_OP_DERIVE;
201 * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
202 * calls can be removed.
206 if (evp_pkey_ctx_is_legacy(ctx))
210 * Ensure that the key is provided, either natively, or as a cached export.
211 * If not, goto legacy
213 tmp_keymgmt = ctx->keymgmt;
214 if (ctx->pkey == NULL) {
216 * Some algorithms (e.g. legacy KDFs) don't have a pkey - so we create
219 EVP_PKEY *pkey = EVP_PKEY_new();
221 if (pkey == NULL || !EVP_PKEY_set_type_by_keymgmt(pkey, tmp_keymgmt)) {
222 ERR_clear_last_mark();
224 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
227 provkey = pkey->keydata = evp_keymgmt_newdata(tmp_keymgmt);
233 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
234 &tmp_keymgmt, ctx->propquery);
238 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
239 ERR_clear_last_mark();
240 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
243 EVP_KEYMGMT_free(ctx->keymgmt);
244 ctx->keymgmt = tmp_keymgmt;
246 if (ctx->keymgmt->query_operation_name != NULL)
247 supported_exch = ctx->keymgmt->query_operation_name(OSSL_OP_KEYEXCH);
250 * If we didn't get a supported exch, assume there is one with the
251 * same name as the key type.
253 if (supported_exch == NULL)
254 supported_exch = ctx->keytype;
257 * Because we cleared out old ops, we shouldn't need to worry about
258 * checking if exchange is already there.
260 exchange = EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery);
263 || (EVP_KEYMGMT_provider(ctx->keymgmt)
264 != EVP_KEYEXCH_provider(exchange))) {
266 * We don't need to free ctx->keymgmt here, as it's not necessarily
267 * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
269 EVP_KEYEXCH_free(exchange);
274 * TODO remove this when legacy is gone
275 * If we don't have the full support we need with provided methods,
276 * let's go see if legacy does.
280 /* No more legacy from here down to legacy: */
282 ctx->op.kex.exchange = exchange;
283 ctx->op.kex.exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
284 if (ctx->op.kex.exchprovctx == NULL) {
285 /* The provider key can stay in the cache */
286 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
289 ret = exchange->init(ctx->op.kex.exchprovctx, provkey, params);
293 evp_pkey_ctx_free_old_ops(ctx);
294 ctx->operation = EVP_PKEY_OP_UNDEFINED;
299 * TODO remove this when legacy is gone
300 * If we don't have the full support we need with provided methods,
301 * let's go see if legacy does.
308 if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
309 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
313 if (ctx->pmeth->derive_init == NULL)
315 ret = ctx->pmeth->derive_init(ctx);
317 ctx->operation = EVP_PKEY_OP_UNDEFINED;
322 int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer,
326 void *provkey = NULL;
327 EVP_PKEY_CTX *check_ctx = NULL;
330 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
334 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.exchprovctx == NULL)
337 if (ctx->op.kex.exchange->set_peer == NULL) {
338 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
343 check_ctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, peer, ctx->propquery);
344 if (check_ctx == NULL)
346 check = EVP_PKEY_public_check(check_ctx);
347 EVP_PKEY_CTX_free(check_ctx);
352 provkey = evp_pkey_export_to_provider(peer, ctx->libctx, &ctx->keymgmt,
355 * If making the key provided wasn't possible, legacy may be able to pick
360 return ctx->op.kex.exchange->set_peer(ctx->op.kex.exchprovctx, provkey);
366 if (ctx->pmeth == NULL
367 || !(ctx->pmeth->derive != NULL
368 || ctx->pmeth->encrypt != NULL
369 || ctx->pmeth->decrypt != NULL)
370 || ctx->pmeth->ctrl == NULL) {
371 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
374 if (ctx->operation != EVP_PKEY_OP_DERIVE
375 && ctx->operation != EVP_PKEY_OP_ENCRYPT
376 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
377 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
381 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
389 if (ctx->pkey == NULL) {
390 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
394 if (ctx->pkey->type != peer->type) {
395 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
400 * For clarity. The error is if parameters in peer are
401 * present (!missing) but don't match. EVP_PKEY_parameters_eq may return
402 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
403 * (different key types) is impossible here because it is checked earlier.
404 * -2 is OK for us here, as well as 1, so we can check for 0 only.
406 if (!EVP_PKEY_missing_parameters(peer) &&
407 !EVP_PKEY_parameters_eq(ctx->pkey, peer)) {
408 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
412 EVP_PKEY_free(ctx->peerkey);
415 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
422 EVP_PKEY_up_ref(peer);
427 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
429 return EVP_PKEY_derive_set_peer_ex(ctx, peer, 1);
432 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
436 if (ctx == NULL || pkeylen == NULL) {
437 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
441 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
442 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
446 if (ctx->op.kex.exchprovctx == NULL)
449 ret = ctx->op.kex.exchange->derive(ctx->op.kex.exchprovctx, key, pkeylen,
450 key != NULL ? *pkeylen : 0);
454 if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
455 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
459 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
460 return ctx->pmeth->derive(ctx, key, pkeylen);
463 int EVP_KEYEXCH_number(const EVP_KEYEXCH *keyexch)
465 return keyexch->name_id;
468 const char *EVP_KEYEXCH_description(const EVP_KEYEXCH *keyexch)
470 return keyexch->description;
473 int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
475 return evp_is_a(keyexch->prov, keyexch->name_id, NULL, name);
478 void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx,
479 void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
482 evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
483 (void (*)(void *, void *))fn, arg,
484 evp_keyexch_from_algorithm,
485 (void (*)(void *))EVP_KEYEXCH_free);
488 int EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
489 void (*fn)(const char *name, void *data),
492 if (keyexch->prov != NULL)
493 return evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);
498 const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch)
502 if (keyexch == NULL || keyexch->gettable_ctx_params == NULL)
505 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(keyexch));
506 return keyexch->gettable_ctx_params(NULL, provctx);
509 const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch)
513 if (keyexch == NULL || keyexch->settable_ctx_params == NULL)
515 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(keyexch));
516 return keyexch->settable_ctx_params(NULL, provctx);