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 "internal/evp_int.h"
15 #include "internal/provider.h"
16 #include "internal/numbers.h" /* includes SIZE_MAX */
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(const char *name,
36 const OSSL_DISPATCH *fns,
41 * Key exchange cannot work without a key, and key management
42 * from the same provider to manage its keys. We therefore fetch
43 * a key management method using the same algorithm and properties
44 * and pass that down to evp_generic_fetch to be passed on to our
45 * evp_keyexch_from_dispatch, which will attach the key management
46 * method to the newly created key exchange method as long as the
49 struct keymgmt_data_st *keymgmt_data = vkeymgmt_data;
50 EVP_KEYMGMT *keymgmt = EVP_KEYMGMT_fetch(keymgmt_data->ctx, name,
51 keymgmt_data->properties);
52 EVP_KEYEXCH *exchange = NULL;
55 if (keymgmt == NULL || EVP_KEYMGMT_provider(keymgmt) != prov) {
56 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEYMGMT_AVAILABLE);
60 if ((exchange = evp_keyexch_new(prov)) == NULL
61 || (exchange->name = OPENSSL_strdup(name)) == NULL) {
62 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
66 exchange->keymgmt = keymgmt;
67 keymgmt = NULL; /* avoid double free on failure below */
69 for (; fns->function_id != 0; fns++) {
70 switch (fns->function_id) {
71 case OSSL_FUNC_KEYEXCH_NEWCTX:
72 if (exchange->newctx != NULL)
74 exchange->newctx = OSSL_get_OP_keyexch_newctx(fns);
77 case OSSL_FUNC_KEYEXCH_INIT:
78 if (exchange->init != NULL)
80 exchange->init = OSSL_get_OP_keyexch_init(fns);
83 case OSSL_FUNC_KEYEXCH_SET_PEER:
84 if (exchange->set_peer != NULL)
86 exchange->set_peer = OSSL_get_OP_keyexch_set_peer(fns);
88 case OSSL_FUNC_KEYEXCH_DERIVE:
89 if (exchange->derive != NULL)
91 exchange->derive = OSSL_get_OP_keyexch_derive(fns);
94 case OSSL_FUNC_KEYEXCH_FREECTX:
95 if (exchange->freectx != NULL)
97 exchange->freectx = OSSL_get_OP_keyexch_freectx(fns);
100 case OSSL_FUNC_KEYEXCH_DUPCTX:
101 if (exchange->dupctx != NULL)
103 exchange->dupctx = OSSL_get_OP_keyexch_dupctx(fns);
105 case OSSL_FUNC_KEYEXCH_SET_PARAMS:
106 if (exchange->set_params != NULL)
108 exchange->set_params = OSSL_get_OP_keyexch_set_params(fns);
114 * In order to be a consistent set of functions we must have at least
115 * a complete set of "exchange" functions: init, derive, newctx,
116 * and freectx. The dupctx, set_peer and set_params functions are
119 EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH,
120 EVP_R_INVALID_PROVIDER_FUNCTIONS);
127 EVP_KEYEXCH_free(exchange);
128 EVP_KEYMGMT_free(keymgmt);
132 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
134 if (exchange != NULL) {
137 CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
140 EVP_KEYMGMT_free(exchange->keymgmt);
141 ossl_provider_free(exchange->prov);
142 OPENSSL_free(exchange->name);
143 CRYPTO_THREAD_lock_free(exchange->lock);
144 OPENSSL_free(exchange);
148 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
152 CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
156 OSSL_PROVIDER *EVP_KEYEXCH_provider(const EVP_KEYEXCH *exchange)
158 return exchange->prov;
161 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OPENSSL_CTX *ctx, const char *algorithm,
162 const char *properties)
164 EVP_KEYEXCH *keyexch = NULL;
165 struct keymgmt_data_st keymgmt_data;
167 keymgmt_data.ctx = ctx;
168 keymgmt_data.properties = properties;
169 keyexch = evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
170 evp_keyexch_from_dispatch, &keymgmt_data,
171 (int (*)(void *))EVP_KEYEXCH_up_ref,
172 (void (*)(void *))EVP_KEYEXCH_free);
177 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, EVP_KEYEXCH *exchange)
180 void *provkey = NULL;
182 ctx->operation = EVP_PKEY_OP_DERIVE;
184 if (ctx->engine != NULL)
187 if (exchange != NULL) {
188 if (!EVP_KEYEXCH_up_ref(exchange))
191 int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
194 * TODO(3.0): Check for legacy handling. Remove this once all all
195 * algorithms are moved to providers.
197 if (ctx->pkey != NULL) {
198 switch (ctx->pkey->type) {
204 exchange = EVP_KEYEXCH_fetch(NULL, OBJ_nid2sn(nid), NULL);
209 if (exchange == NULL) {
210 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
215 if (ctx->exchprovctx != NULL && ctx->exchange != NULL)
216 ctx->exchange->freectx(ctx->exchprovctx);
217 EVP_KEYEXCH_free(ctx->exchange);
218 ctx->exchange = exchange;
219 if (ctx->pkey != NULL) {
220 provkey = evp_keymgmt_export_to_provider(ctx->pkey, exchange->keymgmt);
221 if (provkey == NULL) {
222 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
226 ctx->exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
227 if (ctx->exchprovctx == NULL) {
228 /* The provider key can stay in the cache */
229 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
232 ret = exchange->init(ctx->exchprovctx, provkey);
236 ctx->operation = EVP_PKEY_OP_UNDEFINED;
240 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
241 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX,
242 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_init(EVP_PKEY_CTX *ctx)
256 return EVP_PKEY_derive_init_ex(ctx, NULL);
259 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
262 void *provkey = NULL;
265 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
266 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
270 if (ctx->exchprovctx == NULL)
273 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
274 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
275 EVP_R_OPERATON_NOT_INITIALIZED);
279 if (ctx->exchange->set_peer == NULL) {
280 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
281 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
285 provkey = evp_keymgmt_export_to_provider(peer, ctx->exchange->keymgmt);
286 if (provkey == NULL) {
287 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, ERR_R_INTERNAL_ERROR);
290 return ctx->exchange->set_peer(ctx->exchprovctx, provkey);
293 if (ctx->pmeth == NULL
294 || !(ctx->pmeth->derive != NULL
295 || ctx->pmeth->encrypt != NULL
296 || ctx->pmeth->decrypt != NULL)
297 || ctx->pmeth->ctrl == NULL) {
298 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
299 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
302 if (ctx->operation != EVP_PKEY_OP_DERIVE
303 && ctx->operation != EVP_PKEY_OP_ENCRYPT
304 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
305 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
306 EVP_R_OPERATON_NOT_INITIALIZED);
310 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
318 if (ctx->pkey == NULL) {
319 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
323 if (ctx->pkey->type != peer->type) {
324 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
329 * For clarity. The error is if parameters in peer are
330 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
331 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
332 * (different key types) is impossible here because it is checked earlier.
333 * -2 is OK for us here, as well as 1, so we can check for 0 only.
335 if (!EVP_PKEY_missing_parameters(peer) &&
336 !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
337 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
341 EVP_PKEY_free(ctx->peerkey);
344 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
351 EVP_PKEY_up_ref(peer);
355 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
360 EVPerr(EVP_F_EVP_PKEY_DERIVE,
361 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
365 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
366 EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
370 if (ctx->exchprovctx == NULL)
373 ret = ctx->exchange->derive(ctx->exchprovctx, key, pkeylen, SIZE_MAX);
377 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
378 EVPerr(EVP_F_EVP_PKEY_DERIVE,
379 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
383 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
384 return ctx->pmeth->derive(ctx, key, pkeylen);