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 struct keymgmt_data_st {
37 const char *properties;
40 static void *evp_keyexch_from_dispatch(const char *name,
41 const OSSL_DISPATCH *fns,
46 * Key exchange cannot work without a key, and key management
47 * from the same provider to manage its keys. We therefore fetch
48 * a key management method using the same algorithm and properties
49 * and pass that down to evp_generic_fetch to be passed on to our
50 * evp_keyexch_from_dispatch, which will attach the key management
51 * method to the newly created key exchange method as long as the
54 struct keymgmt_data_st *keymgmt_data = vkeymgmt_data;
55 EVP_KEYMGMT *keymgmt = EVP_KEYMGMT_fetch(keymgmt_data->ctx, name,
56 keymgmt_data->properties);
57 EVP_KEYEXCH *exchange = NULL;
60 if (keymgmt == NULL || EVP_KEYMGMT_provider(keymgmt) != prov) {
61 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEYMGMT_AVAILABLE);
65 if ((exchange = evp_keyexch_new(prov)) == NULL
66 || (exchange->name = OPENSSL_strdup(name)) == NULL) {
67 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
71 exchange->keymgmt = keymgmt;
72 keymgmt = NULL; /* avoid double free on failure below */
74 for (; fns->function_id != 0; fns++) {
75 switch (fns->function_id) {
76 case OSSL_FUNC_KEYEXCH_NEWCTX:
77 if (exchange->newctx != NULL)
79 exchange->newctx = OSSL_get_OP_keyexch_newctx(fns);
82 case OSSL_FUNC_KEYEXCH_INIT:
83 if (exchange->init != NULL)
85 exchange->init = OSSL_get_OP_keyexch_init(fns);
88 case OSSL_FUNC_KEYEXCH_SET_PEER:
89 if (exchange->set_peer != NULL)
91 exchange->set_peer = OSSL_get_OP_keyexch_set_peer(fns);
93 case OSSL_FUNC_KEYEXCH_DERIVE:
94 if (exchange->derive != NULL)
96 exchange->derive = OSSL_get_OP_keyexch_derive(fns);
99 case OSSL_FUNC_KEYEXCH_FREECTX:
100 if (exchange->freectx != NULL)
102 exchange->freectx = OSSL_get_OP_keyexch_freectx(fns);
105 case OSSL_FUNC_KEYEXCH_DUPCTX:
106 if (exchange->dupctx != NULL)
108 exchange->dupctx = OSSL_get_OP_keyexch_dupctx(fns);
110 case OSSL_FUNC_KEYEXCH_SET_PARAMS:
111 if (exchange->set_params != NULL)
113 exchange->set_params = OSSL_get_OP_keyexch_set_params(fns);
119 * In order to be a consistent set of functions we must have at least
120 * a complete set of "exchange" functions: init, derive, newctx,
121 * and freectx. The dupctx, set_peer and set_params functions are
124 EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH,
125 EVP_R_INVALID_PROVIDER_FUNCTIONS);
132 EVP_KEYEXCH_free(exchange);
133 EVP_KEYMGMT_free(keymgmt);
137 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
139 if (exchange != NULL) {
142 CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
145 EVP_KEYMGMT_free(exchange->keymgmt);
146 ossl_provider_free(exchange->prov);
147 OPENSSL_free(exchange->name);
148 CRYPTO_THREAD_lock_free(exchange->lock);
149 OPENSSL_free(exchange);
153 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
157 CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
161 OSSL_PROVIDER *EVP_KEYEXCH_provider(const EVP_KEYEXCH *exchange)
163 return exchange->prov;
166 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OPENSSL_CTX *ctx, const char *algorithm,
167 const char *properties)
169 EVP_KEYEXCH *keyexch = NULL;
170 struct keymgmt_data_st keymgmt_data;
172 keymgmt_data.ctx = ctx;
173 keymgmt_data.properties = properties;
174 keyexch = evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
175 evp_keyexch_from_dispatch, &keymgmt_data,
176 (int (*)(void *))EVP_KEYEXCH_up_ref,
177 (void (*)(void *))EVP_KEYEXCH_free);
182 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, EVP_KEYEXCH *exchange)
185 void *provkey = NULL;
187 ctx->operation = EVP_PKEY_OP_DERIVE;
189 if (ctx->engine != NULL)
192 if (exchange != NULL) {
193 if (!EVP_KEYEXCH_up_ref(exchange))
196 int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
199 * TODO(3.0): Check for legacy handling. Remove this once all all
200 * algorithms are moved to providers.
202 if (ctx->pkey != NULL) {
203 switch (ctx->pkey->type) {
209 exchange = EVP_KEYEXCH_fetch(NULL, OBJ_nid2sn(nid), NULL);
214 if (exchange == NULL) {
215 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
220 if (ctx->exchprovctx != NULL && ctx->exchange != NULL)
221 ctx->exchange->freectx(ctx->exchprovctx);
222 EVP_KEYEXCH_free(ctx->exchange);
223 ctx->exchange = exchange;
224 if (ctx->pkey != NULL) {
225 provkey = evp_keymgmt_export_to_provider(ctx->pkey, exchange->keymgmt);
226 if (provkey == NULL) {
227 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
231 ctx->exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
232 if (ctx->exchprovctx == NULL) {
233 /* The provider key can stay in the cache */
234 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
237 ret = exchange->init(ctx->exchprovctx, provkey);
241 ctx->operation = EVP_PKEY_OP_UNDEFINED;
245 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
246 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX,
247 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
251 if (ctx->pmeth->derive_init == NULL)
253 ret = ctx->pmeth->derive_init(ctx);
255 ctx->operation = EVP_PKEY_OP_UNDEFINED;
259 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
261 return EVP_PKEY_derive_init_ex(ctx, NULL);
264 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
267 void *provkey = NULL;
270 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
271 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
275 if (ctx->exchprovctx == NULL)
278 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
279 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
280 EVP_R_OPERATON_NOT_INITIALIZED);
284 if (ctx->exchange->set_peer == NULL) {
285 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
286 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
290 provkey = evp_keymgmt_export_to_provider(peer, ctx->exchange->keymgmt);
291 if (provkey == NULL) {
292 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, ERR_R_INTERNAL_ERROR);
295 return ctx->exchange->set_peer(ctx->exchprovctx, provkey);
298 if (ctx->pmeth == NULL
299 || !(ctx->pmeth->derive != NULL
300 || ctx->pmeth->encrypt != NULL
301 || ctx->pmeth->decrypt != NULL)
302 || ctx->pmeth->ctrl == NULL) {
303 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
304 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
307 if (ctx->operation != EVP_PKEY_OP_DERIVE
308 && ctx->operation != EVP_PKEY_OP_ENCRYPT
309 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
310 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
311 EVP_R_OPERATON_NOT_INITIALIZED);
315 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
323 if (ctx->pkey == NULL) {
324 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
328 if (ctx->pkey->type != peer->type) {
329 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
334 * For clarity. The error is if parameters in peer are
335 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
336 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
337 * (different key types) is impossible here because it is checked earlier.
338 * -2 is OK for us here, as well as 1, so we can check for 0 only.
340 if (!EVP_PKEY_missing_parameters(peer) &&
341 !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
342 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
346 EVP_PKEY_free(ctx->peerkey);
349 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
356 EVP_PKEY_up_ref(peer);
360 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
365 EVPerr(EVP_F_EVP_PKEY_DERIVE,
366 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
370 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
371 EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
375 if (ctx->exchprovctx == NULL)
378 ret = ctx->exchange->derive(ctx->exchprovctx, key, pkeylen, SIZE_MAX);
382 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
383 EVPerr(EVP_F_EVP_PKEY_DERIVE,
384 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
388 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
389 return ctx->pmeth->derive(ctx, key, pkeylen);