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,
39 EVP_KEYEXCH *exchange = NULL;
40 int fncnt = 0, paramfncnt = 0;
42 if ((exchange = evp_keyexch_new(prov)) == NULL) {
43 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
47 exchange->name_id = name_id;
49 for (; fns->function_id != 0; fns++) {
50 switch (fns->function_id) {
51 case OSSL_FUNC_KEYEXCH_NEWCTX:
52 if (exchange->newctx != NULL)
54 exchange->newctx = OSSL_get_OP_keyexch_newctx(fns);
57 case OSSL_FUNC_KEYEXCH_INIT:
58 if (exchange->init != NULL)
60 exchange->init = OSSL_get_OP_keyexch_init(fns);
63 case OSSL_FUNC_KEYEXCH_SET_PEER:
64 if (exchange->set_peer != NULL)
66 exchange->set_peer = OSSL_get_OP_keyexch_set_peer(fns);
68 case OSSL_FUNC_KEYEXCH_DERIVE:
69 if (exchange->derive != NULL)
71 exchange->derive = OSSL_get_OP_keyexch_derive(fns);
74 case OSSL_FUNC_KEYEXCH_FREECTX:
75 if (exchange->freectx != NULL)
77 exchange->freectx = OSSL_get_OP_keyexch_freectx(fns);
80 case OSSL_FUNC_KEYEXCH_DUPCTX:
81 if (exchange->dupctx != NULL)
83 exchange->dupctx = OSSL_get_OP_keyexch_dupctx(fns);
85 case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
86 if (exchange->set_ctx_params != NULL)
88 exchange->set_ctx_params = OSSL_get_OP_keyexch_set_ctx_params(fns);
91 case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
92 if (exchange->settable_ctx_params != NULL)
94 exchange->settable_ctx_params
95 = OSSL_get_OP_keyexch_settable_ctx_params(fns);
100 if (fncnt != 4 || (paramfncnt != 0 && paramfncnt != 2)) {
102 * In order to be a consistent set of functions we must have at least
103 * a complete set of "exchange" functions: init, derive, newctx,
104 * and freectx. The set_ctx_params and settable_ctx_params functions are
105 * optional, but if one of them is present then the other one must also
106 * be present. The dupctx and set_peer functions are optional.
108 EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH,
109 EVP_R_INVALID_PROVIDER_FUNCTIONS);
116 EVP_KEYEXCH_free(exchange);
120 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
122 if (exchange != NULL) {
125 CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
128 ossl_provider_free(exchange->prov);
129 CRYPTO_THREAD_lock_free(exchange->lock);
130 OPENSSL_free(exchange);
134 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
138 CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
142 OSSL_PROVIDER *EVP_KEYEXCH_provider(const EVP_KEYEXCH *exchange)
144 return exchange->prov;
147 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OPENSSL_CTX *ctx, const char *algorithm,
148 const char *properties)
150 return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
151 evp_keyexch_from_dispatch,
152 (int (*)(void *))EVP_KEYEXCH_up_ref,
153 (void (*)(void *))EVP_KEYEXCH_free);
156 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
159 void *provkey = NULL;
160 EVP_KEYEXCH *exchange = NULL;
163 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
167 evp_pkey_ctx_free_old_ops(ctx);
168 ctx->operation = EVP_PKEY_OP_DERIVE;
170 if (ctx->engine != NULL || ctx->algorithm == NULL)
174 * Because we cleared out old ops, we shouldn't need to worry about
175 * checking if exchange is already there. Keymgmt is a different
176 * matter, as it isn't tied to a specific EVP_PKEY op.
178 exchange = EVP_KEYEXCH_fetch(ctx->libctx, ctx->algorithm, ctx->propquery);
179 if (exchange != NULL && ctx->keymgmt == NULL) {
180 int name_id = EVP_KEYEXCH_number(exchange);
183 evp_keymgmt_fetch_by_number(ctx->libctx, name_id, ctx->propquery);
186 if (ctx->keymgmt == NULL
188 || (EVP_KEYMGMT_provider(ctx->keymgmt)
189 != EVP_KEYEXCH_provider(exchange))) {
191 * We don't have the full support we need with provided methods,
192 * let's go see if legacy does. Also, we don't need to free
193 * ctx->keymgmt here, as it's not necessarily tied to this
194 * operation. It will be freed by EVP_PKEY_CTX_free().
196 EVP_KEYEXCH_free(exchange);
201 ctx->op.kex.exchange = exchange;
203 if (ctx->pkey != NULL) {
204 provkey = evp_keymgmt_export_to_provider(ctx->pkey, ctx->keymgmt, 0);
205 if (provkey == NULL) {
206 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
210 ctx->op.kex.exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
211 if (ctx->op.kex.exchprovctx == NULL) {
212 /* The provider key can stay in the cache */
213 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
216 ret = exchange->init(ctx->op.kex.exchprovctx, provkey);
220 ctx->operation = EVP_PKEY_OP_UNDEFINED;
224 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
225 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
229 if (ctx->pmeth->derive_init == NULL)
231 ret = ctx->pmeth->derive_init(ctx);
233 ctx->operation = EVP_PKEY_OP_UNDEFINED;
237 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
240 void *provkey = NULL;
243 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
244 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
248 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.exchprovctx == NULL)
251 if (ctx->op.kex.exchange->set_peer == NULL) {
252 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
253 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
257 provkey = evp_keymgmt_export_to_provider(peer, ctx->keymgmt, 0);
258 if (provkey == NULL) {
259 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, ERR_R_INTERNAL_ERROR);
262 return ctx->op.kex.exchange->set_peer(ctx->op.kex.exchprovctx, provkey);
265 if (ctx->pmeth == NULL
266 || !(ctx->pmeth->derive != NULL
267 || ctx->pmeth->encrypt != NULL
268 || ctx->pmeth->decrypt != NULL)
269 || ctx->pmeth->ctrl == NULL) {
270 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
271 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
274 if (ctx->operation != EVP_PKEY_OP_DERIVE
275 && ctx->operation != EVP_PKEY_OP_ENCRYPT
276 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
277 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
278 EVP_R_OPERATON_NOT_INITIALIZED);
282 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
290 if (ctx->pkey == NULL) {
291 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
295 if (ctx->pkey->type != peer->type) {
296 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
301 * For clarity. The error is if parameters in peer are
302 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
303 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
304 * (different key types) is impossible here because it is checked earlier.
305 * -2 is OK for us here, as well as 1, so we can check for 0 only.
307 if (!EVP_PKEY_missing_parameters(peer) &&
308 !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
309 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
313 EVP_PKEY_free(ctx->peerkey);
316 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
323 EVP_PKEY_up_ref(peer);
327 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
332 EVPerr(EVP_F_EVP_PKEY_DERIVE,
333 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
337 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
338 EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
342 if (ctx->op.kex.exchprovctx == NULL)
345 ret = ctx->op.kex.exchange->derive(ctx->op.kex.exchprovctx, key, pkeylen,
350 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
351 EVPerr(EVP_F_EVP_PKEY_DERIVE,
352 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
356 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
357 return ctx->pmeth->derive(ctx, key, pkeylen);
360 int EVP_KEYEXCH_number(const EVP_KEYEXCH *keyexch)
362 return keyexch->name_id;
365 int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
367 return evp_is_a(keyexch->prov, keyexch->name_id, name);
370 void EVP_KEYEXCH_do_all_provided(OPENSSL_CTX *libctx,
371 void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
374 evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
375 (void (*)(void *, void *))fn, arg,
376 evp_keyexch_from_dispatch,
377 (void (*)(void *))EVP_KEYEXCH_free);
380 void EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
381 void (*fn)(const char *name, void *data),
384 if (keyexch->prov != NULL)
385 evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);