2 * Copyright 2019 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 exchange->lock = CRYPTO_THREAD_lock_new();
24 if (exchange->lock == NULL) {
25 OPENSSL_free(exchange);
28 exchange->prov = prov;
29 ossl_provider_up_ref(prov);
35 static void *evp_keyexch_from_dispatch(int name_id,
36 const OSSL_DISPATCH *fns,
40 EVP_KEYEXCH *exchange = NULL;
41 int fncnt = 0, paramfncnt = 0;
43 if ((exchange = evp_keyexch_new(prov)) == NULL) {
44 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
48 exchange->name_id = name_id;
50 for (; fns->function_id != 0; fns++) {
51 switch (fns->function_id) {
52 case OSSL_FUNC_KEYEXCH_NEWCTX:
53 if (exchange->newctx != NULL)
55 exchange->newctx = OSSL_get_OP_keyexch_newctx(fns);
58 case OSSL_FUNC_KEYEXCH_INIT:
59 if (exchange->init != NULL)
61 exchange->init = OSSL_get_OP_keyexch_init(fns);
64 case OSSL_FUNC_KEYEXCH_SET_PEER:
65 if (exchange->set_peer != NULL)
67 exchange->set_peer = OSSL_get_OP_keyexch_set_peer(fns);
69 case OSSL_FUNC_KEYEXCH_DERIVE:
70 if (exchange->derive != NULL)
72 exchange->derive = OSSL_get_OP_keyexch_derive(fns);
75 case OSSL_FUNC_KEYEXCH_FREECTX:
76 if (exchange->freectx != NULL)
78 exchange->freectx = OSSL_get_OP_keyexch_freectx(fns);
81 case OSSL_FUNC_KEYEXCH_DUPCTX:
82 if (exchange->dupctx != NULL)
84 exchange->dupctx = OSSL_get_OP_keyexch_dupctx(fns);
86 case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
87 if (exchange->set_ctx_params != NULL)
89 exchange->set_ctx_params = OSSL_get_OP_keyexch_set_ctx_params(fns);
92 case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
93 if (exchange->settable_ctx_params != NULL)
95 exchange->settable_ctx_params
96 = OSSL_get_OP_keyexch_settable_ctx_params(fns);
101 if (fncnt != 4 || (paramfncnt != 0 && paramfncnt != 2)) {
103 * In order to be a consistent set of functions we must have at least
104 * a complete set of "exchange" functions: init, derive, newctx,
105 * and freectx. The set_ctx_params and settable_ctx_params functions are
106 * optional, but if one of them is present then the other one must also
107 * be present. The dupctx and set_peer functions are optional.
109 EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH,
110 EVP_R_INVALID_PROVIDER_FUNCTIONS);
117 EVP_KEYEXCH_free(exchange);
121 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
123 if (exchange != NULL) {
126 CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
129 ossl_provider_free(exchange->prov);
130 CRYPTO_THREAD_lock_free(exchange->lock);
131 OPENSSL_free(exchange);
135 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
139 CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
143 OSSL_PROVIDER *EVP_KEYEXCH_provider(const EVP_KEYEXCH *exchange)
145 return exchange->prov;
148 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OPENSSL_CTX *ctx, const char *algorithm,
149 const char *properties)
151 EVP_KEYEXCH *keyexch = NULL;
153 keyexch = evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
154 evp_keyexch_from_dispatch, NULL,
155 (int (*)(void *))EVP_KEYEXCH_up_ref,
156 (void (*)(void *))EVP_KEYEXCH_free);
161 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
164 void *provkey = NULL;
165 EVP_KEYEXCH *exchange = NULL;
168 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
172 evp_pkey_ctx_free_old_ops(ctx);
173 ctx->operation = EVP_PKEY_OP_DERIVE;
175 if (ctx->engine != NULL || ctx->algorithm == NULL)
179 * Because we cleared out old ops, we shouldn't need to worry about
180 * checking if exchange is already there. Keymgmt is a different
181 * matter, as it isn't tied to a specific EVP_PKEY op.
183 exchange = EVP_KEYEXCH_fetch(ctx->libctx, ctx->algorithm, ctx->propquery);
184 if (exchange != NULL && ctx->keymgmt == NULL) {
185 int name_id = EVP_KEYEXCH_number(exchange);
188 evp_keymgmt_fetch_by_number(ctx->libctx, name_id, ctx->propquery);
191 if (ctx->keymgmt == NULL
193 || (EVP_KEYMGMT_provider(ctx->keymgmt)
194 != EVP_KEYEXCH_provider(exchange))) {
196 * We don't have the full support we need with provided methods,
197 * let's go see if legacy does. Also, we don't need to free
198 * ctx->keymgmt here, as it's not necessarily tied to this
199 * operation. It will be freed by EVP_PKEY_CTX_free().
201 EVP_KEYEXCH_free(exchange);
206 ctx->op.kex.exchange = exchange;
208 if (ctx->pkey != NULL) {
209 provkey = evp_keymgmt_export_to_provider(ctx->pkey, ctx->keymgmt, 0);
210 if (provkey == NULL) {
211 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
215 ctx->op.kex.exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
216 if (ctx->op.kex.exchprovctx == NULL) {
217 /* The provider key can stay in the cache */
218 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
221 ret = exchange->init(ctx->op.kex.exchprovctx, provkey);
225 ctx->operation = EVP_PKEY_OP_UNDEFINED;
229 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
230 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
234 if (ctx->pmeth->derive_init == NULL)
236 ret = ctx->pmeth->derive_init(ctx);
238 ctx->operation = EVP_PKEY_OP_UNDEFINED;
242 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
245 void *provkey = NULL;
248 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
249 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
253 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.exchprovctx == NULL)
256 if (ctx->op.kex.exchange->set_peer == NULL) {
257 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
258 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
262 provkey = evp_keymgmt_export_to_provider(peer, ctx->keymgmt, 0);
263 if (provkey == NULL) {
264 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, ERR_R_INTERNAL_ERROR);
267 return ctx->op.kex.exchange->set_peer(ctx->op.kex.exchprovctx, provkey);
270 if (ctx->pmeth == NULL
271 || !(ctx->pmeth->derive != NULL
272 || ctx->pmeth->encrypt != NULL
273 || ctx->pmeth->decrypt != NULL)
274 || ctx->pmeth->ctrl == NULL) {
275 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
276 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
279 if (ctx->operation != EVP_PKEY_OP_DERIVE
280 && ctx->operation != EVP_PKEY_OP_ENCRYPT
281 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
282 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
283 EVP_R_OPERATON_NOT_INITIALIZED);
287 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
295 if (ctx->pkey == NULL) {
296 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
300 if (ctx->pkey->type != peer->type) {
301 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
306 * For clarity. The error is if parameters in peer are
307 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
308 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
309 * (different key types) is impossible here because it is checked earlier.
310 * -2 is OK for us here, as well as 1, so we can check for 0 only.
312 if (!EVP_PKEY_missing_parameters(peer) &&
313 !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
314 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
318 EVP_PKEY_free(ctx->peerkey);
321 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
328 EVP_PKEY_up_ref(peer);
332 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
337 EVPerr(EVP_F_EVP_PKEY_DERIVE,
338 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
342 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
343 EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
347 if (ctx->op.kex.exchprovctx == NULL)
350 ret = ctx->op.kex.exchange->derive(ctx->op.kex.exchprovctx, key, pkeylen,
355 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
356 EVPerr(EVP_F_EVP_PKEY_DERIVE,
357 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
361 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
362 return ctx->pmeth->derive(ctx, key, pkeylen);
365 int EVP_KEYEXCH_number(const EVP_KEYEXCH *keyexch)
367 return keyexch->name_id;
370 int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
372 return evp_is_a(keyexch->prov, keyexch->name_id, name);
375 void EVP_KEYEXCH_do_all_provided(OPENSSL_CTX *libctx,
376 void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
379 evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
380 (void (*)(void *, void *))fn, arg,
381 evp_keyexch_from_dispatch, NULL,
382 (void (*)(void *))EVP_KEYEXCH_free);
385 void EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
386 void (*fn)(const char *name, void *data),
389 if (keyexch->prov != NULL)
390 evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);