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 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, paramfncnt = 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_get_OP_keyexch_newctx(fns);
63 case OSSL_FUNC_KEYEXCH_INIT:
64 if (exchange->init != NULL)
66 exchange->init = OSSL_get_OP_keyexch_init(fns);
69 case OSSL_FUNC_KEYEXCH_SET_PEER:
70 if (exchange->set_peer != NULL)
72 exchange->set_peer = OSSL_get_OP_keyexch_set_peer(fns);
74 case OSSL_FUNC_KEYEXCH_DERIVE:
75 if (exchange->derive != NULL)
77 exchange->derive = OSSL_get_OP_keyexch_derive(fns);
80 case OSSL_FUNC_KEYEXCH_FREECTX:
81 if (exchange->freectx != NULL)
83 exchange->freectx = OSSL_get_OP_keyexch_freectx(fns);
86 case OSSL_FUNC_KEYEXCH_DUPCTX:
87 if (exchange->dupctx != NULL)
89 exchange->dupctx = OSSL_get_OP_keyexch_dupctx(fns);
91 case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
92 if (exchange->set_ctx_params != NULL)
94 exchange->set_ctx_params = OSSL_get_OP_keyexch_set_ctx_params(fns);
97 case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
98 if (exchange->settable_ctx_params != NULL)
100 exchange->settable_ctx_params
101 = OSSL_get_OP_keyexch_settable_ctx_params(fns);
106 if (fncnt != 4 || (paramfncnt != 0 && paramfncnt != 2)) {
108 * In order to be a consistent set of functions we must have at least
109 * a complete set of "exchange" functions: init, derive, newctx,
110 * and freectx. The set_ctx_params and settable_ctx_params functions are
111 * optional, but if one of them is present then the other one must also
112 * be present. The dupctx and set_peer functions are optional.
114 EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH,
115 EVP_R_INVALID_PROVIDER_FUNCTIONS);
122 EVP_KEYEXCH_free(exchange);
126 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
128 if (exchange != NULL) {
131 CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
134 ossl_provider_free(exchange->prov);
135 CRYPTO_THREAD_lock_free(exchange->lock);
136 OPENSSL_free(exchange);
140 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
144 CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
148 OSSL_PROVIDER *EVP_KEYEXCH_provider(const EVP_KEYEXCH *exchange)
150 return exchange->prov;
153 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OPENSSL_CTX *ctx, const char *algorithm,
154 const char *properties)
156 return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
157 evp_keyexch_from_dispatch,
158 (int (*)(void *))EVP_KEYEXCH_up_ref,
159 (void (*)(void *))EVP_KEYEXCH_free);
162 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
165 void *provkey = NULL;
166 EVP_KEYEXCH *exchange = NULL;
169 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
173 evp_pkey_ctx_free_old_ops(ctx);
174 ctx->operation = EVP_PKEY_OP_DERIVE;
176 if (ctx->engine != NULL || ctx->keytype == NULL)
179 if (ctx->keymgmt == NULL)
181 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, ctx->propquery);
182 if (ctx->keymgmt != NULL) {
183 const char *supported_exch = NULL;
185 if (ctx->keymgmt->query_operation_name != NULL)
187 ctx->keymgmt->query_operation_name(OSSL_OP_KEYEXCH);
190 * If we didn't get a supported exch, assume there is one with the
191 * same name as the key type.
193 if (supported_exch == NULL)
194 supported_exch = ctx->keytype;
197 * Because we cleared out old ops, we shouldn't need to worry about
198 * checking if exchange is already there.
201 EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery);
204 if (ctx->keymgmt == NULL
206 || (EVP_KEYMGMT_provider(ctx->keymgmt)
207 != EVP_KEYEXCH_provider(exchange))) {
209 * We don't have the full support we need with provided methods,
210 * let's go see if legacy does. Also, we don't need to free
211 * ctx->keymgmt here, as it's not necessarily tied to this
212 * operation. It will be freed by EVP_PKEY_CTX_free().
214 EVP_KEYEXCH_free(exchange);
219 ctx->op.kex.exchange = exchange;
221 if (ctx->pkey != NULL) {
222 provkey = evp_keymgmt_export_to_provider(ctx->pkey, ctx->keymgmt, 0);
223 /* If export failed, legacy may be able to pick it up */
227 ctx->op.kex.exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
228 if (ctx->op.kex.exchprovctx == NULL) {
229 /* The provider key can stay in the cache */
230 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
233 ret = exchange->init(ctx->op.kex.exchprovctx, provkey);
237 ctx->operation = EVP_PKEY_OP_UNDEFINED;
241 if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
242 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
246 if (ctx->pmeth->derive_init == NULL)
248 ret = ctx->pmeth->derive_init(ctx);
250 ctx->operation = EVP_PKEY_OP_UNDEFINED;
254 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
257 void *provkey = NULL;
260 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
261 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
265 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.exchprovctx == NULL)
268 if (ctx->op.kex.exchange->set_peer == NULL) {
269 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
270 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
274 provkey = evp_keymgmt_export_to_provider(peer, ctx->keymgmt, 0);
275 /* If export failed, legacy may be able to pick it up */
278 return ctx->op.kex.exchange->set_peer(ctx->op.kex.exchprovctx, provkey);
281 if (ctx->pmeth == NULL
282 || !(ctx->pmeth->derive != NULL
283 || ctx->pmeth->encrypt != NULL
284 || ctx->pmeth->decrypt != NULL)
285 || ctx->pmeth->ctrl == NULL) {
286 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
287 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
290 if (ctx->operation != EVP_PKEY_OP_DERIVE
291 && ctx->operation != EVP_PKEY_OP_ENCRYPT
292 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
293 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
294 EVP_R_OPERATON_NOT_INITIALIZED);
298 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
306 if (ctx->pkey == NULL) {
307 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
311 if (ctx->pkey->type != peer->type) {
312 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
317 * For clarity. The error is if parameters in peer are
318 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
319 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
320 * (different key types) is impossible here because it is checked earlier.
321 * -2 is OK for us here, as well as 1, so we can check for 0 only.
323 if (!EVP_PKEY_missing_parameters(peer) &&
324 !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
325 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
329 EVP_PKEY_free(ctx->peerkey);
332 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
339 EVP_PKEY_up_ref(peer);
343 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
348 EVPerr(EVP_F_EVP_PKEY_DERIVE,
349 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
353 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
354 EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
358 if (ctx->op.kex.exchprovctx == NULL)
361 ret = ctx->op.kex.exchange->derive(ctx->op.kex.exchprovctx, key, pkeylen,
366 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
367 EVPerr(EVP_F_EVP_PKEY_DERIVE,
368 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
372 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
373 return ctx->pmeth->derive(ctx, key, pkeylen);
376 int EVP_KEYEXCH_number(const EVP_KEYEXCH *keyexch)
378 return keyexch->name_id;
381 int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
383 return evp_is_a(keyexch->prov, keyexch->name_id, name);
386 void EVP_KEYEXCH_do_all_provided(OPENSSL_CTX *libctx,
387 void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
390 evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
391 (void (*)(void *, void *))fn, arg,
392 evp_keyexch_from_dispatch,
393 (void (*)(void *))EVP_KEYEXCH_free);
396 void EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
397 void (*fn)(const char *name, void *data),
400 if (keyexch->prov != NULL)
401 evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);