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"
18 static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov)
20 EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH));
22 exchange->lock = CRYPTO_THREAD_lock_new();
23 if (exchange->lock == NULL) {
24 OPENSSL_free(exchange);
27 exchange->prov = prov;
28 ossl_provider_up_ref(prov);
34 static void *evp_keyexch_from_dispatch(const char *name,
35 const OSSL_DISPATCH *fns,
38 EVP_KEYEXCH *exchange = NULL;
41 if ((exchange = evp_keyexch_new(prov)) == NULL
42 || (exchange->name = OPENSSL_strdup(name)) == NULL) {
43 EVP_KEYEXCH_free(exchange);
44 EVPerr(0, ERR_R_MALLOC_FAILURE);
48 for (; fns->function_id != 0; fns++) {
49 switch (fns->function_id) {
50 case OSSL_FUNC_KEYEXCH_NEWCTX:
51 if (exchange->newctx != NULL)
53 exchange->newctx = OSSL_get_OP_keyexch_newctx(fns);
56 case OSSL_FUNC_KEYEXCH_INIT:
57 if (exchange->init != NULL)
59 exchange->init = OSSL_get_OP_keyexch_init(fns);
62 case OSSL_FUNC_KEYEXCH_SET_PEER:
63 if (exchange->set_peer != NULL)
65 exchange->set_peer = OSSL_get_OP_keyexch_set_peer(fns);
67 case OSSL_FUNC_KEYEXCH_DERIVE:
68 if (exchange->derive != NULL)
70 exchange->derive = OSSL_get_OP_keyexch_derive(fns);
73 case OSSL_FUNC_KEYEXCH_FREECTX:
74 if (exchange->freectx != NULL)
76 exchange->freectx = OSSL_get_OP_keyexch_freectx(fns);
79 case OSSL_FUNC_KEYEXCH_DUPCTX:
80 if (exchange->dupctx != NULL)
82 exchange->dupctx = OSSL_get_OP_keyexch_dupctx(fns);
84 case OSSL_FUNC_KEYEXCH_SET_PARAMS:
85 if (exchange->set_params != NULL)
87 exchange->set_params = OSSL_get_OP_keyexch_set_params(fns);
93 * In order to be a consistent set of functions we must have at least
94 * a complete set of "exchange" functions: init, derive, newctx,
95 * and freectx. The dupctx, set_peer and set_params functions are
98 EVP_KEYEXCH_free(exchange);
99 EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH,
100 EVP_R_INVALID_PROVIDER_FUNCTIONS);
107 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
109 if (exchange != NULL) {
112 CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
115 EVP_KEYMGMT_free(exchange->keymgmt);
116 ossl_provider_free(exchange->prov);
117 OPENSSL_free(exchange->name);
118 CRYPTO_THREAD_lock_free(exchange->lock);
119 OPENSSL_free(exchange);
123 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
127 CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
131 OSSL_PROVIDER *EVP_KEYEXCH_provider(const EVP_KEYEXCH *exchange)
133 return exchange->prov;
136 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OPENSSL_CTX *ctx, const char *algorithm,
137 const char *properties)
140 * Key exchange cannot work without a key, and we key management
141 * from the same provider to manage its keys.
143 EVP_KEYEXCH *keyexch =
144 evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
145 evp_keyexch_from_dispatch,
146 (int (*)(void *))EVP_KEYEXCH_up_ref,
147 (void (*)(void *))EVP_KEYEXCH_free);
149 /* If the method is newly created, there's no keymgmt attached */
150 if (keyexch->keymgmt == NULL) {
151 EVP_KEYMGMT *keymgmt = EVP_KEYMGMT_fetch(ctx, algorithm, properties);
154 || (EVP_KEYEXCH_provider(keyexch)
155 != EVP_KEYMGMT_provider(keymgmt))) {
156 EVP_KEYEXCH_free(keyexch);
157 EVP_KEYMGMT_free(keymgmt);
158 EVPerr(EVP_F_EVP_KEYEXCH_FETCH, EVP_R_NO_KEYMGMT_PRESENT);
162 keyexch->keymgmt = keymgmt;
167 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, EVP_KEYEXCH *exchange)
170 void *provkey = NULL;
172 ctx->operation = EVP_PKEY_OP_DERIVE;
174 if (ctx->engine != NULL)
177 if (exchange != NULL) {
178 if (!EVP_KEYEXCH_up_ref(exchange))
181 int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
184 * TODO(3.0): Check for legacy handling. Remove this once all all
185 * algorithms are moved to providers.
187 if (ctx->pkey != NULL) {
188 switch (ctx->pkey->type) {
194 exchange = EVP_KEYEXCH_fetch(NULL, OBJ_nid2sn(nid), NULL);
199 if (exchange == NULL) {
200 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
205 if (ctx->exchprovctx != NULL && ctx->exchange != NULL)
206 ctx->exchange->freectx(ctx->exchprovctx);
207 EVP_KEYEXCH_free(ctx->exchange);
208 ctx->exchange = exchange;
209 if (ctx->pkey != NULL) {
210 provkey = evp_keymgmt_export_to_provider(ctx->pkey, exchange->keymgmt);
211 if (provkey == NULL) {
212 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
216 ctx->exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
217 if (ctx->exchprovctx == NULL) {
218 /* The provider key can stay in the cache */
219 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
222 ret = exchange->init(ctx->exchprovctx, provkey);
226 ctx->operation = EVP_PKEY_OP_UNDEFINED;
230 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
231 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX,
232 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
236 if (ctx->pmeth->derive_init == NULL)
238 ret = ctx->pmeth->derive_init(ctx);
240 ctx->operation = EVP_PKEY_OP_UNDEFINED;
244 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
246 return EVP_PKEY_derive_init_ex(ctx, NULL);
249 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
252 void *provkey = NULL;
255 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
256 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
260 if (ctx->exchprovctx == NULL)
263 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
264 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
265 EVP_R_OPERATON_NOT_INITIALIZED);
269 if (ctx->exchange->set_peer == NULL) {
270 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
271 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
275 provkey = evp_keymgmt_export_to_provider(peer, ctx->exchange->keymgmt);
276 if (provkey == NULL) {
277 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, ERR_R_INTERNAL_ERROR);
280 return ctx->exchange->set_peer(ctx->exchprovctx, provkey);
283 if (ctx->pmeth == NULL
284 || !(ctx->pmeth->derive != NULL
285 || ctx->pmeth->encrypt != NULL
286 || ctx->pmeth->decrypt != NULL)
287 || ctx->pmeth->ctrl == NULL) {
288 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
289 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
292 if (ctx->operation != EVP_PKEY_OP_DERIVE
293 && ctx->operation != EVP_PKEY_OP_ENCRYPT
294 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
295 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
296 EVP_R_OPERATON_NOT_INITIALIZED);
300 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
308 if (ctx->pkey == NULL) {
309 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
313 if (ctx->pkey->type != peer->type) {
314 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
319 * For clarity. The error is if parameters in peer are
320 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
321 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
322 * (different key types) is impossible here because it is checked earlier.
323 * -2 is OK for us here, as well as 1, so we can check for 0 only.
325 if (!EVP_PKEY_missing_parameters(peer) &&
326 !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
327 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
331 EVP_PKEY_free(ctx->peerkey);
334 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
341 EVP_PKEY_up_ref(peer);
345 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
350 EVPerr(EVP_F_EVP_PKEY_DERIVE,
351 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
355 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
356 EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
360 if (ctx->exchprovctx == NULL)
363 ret = ctx->exchange->derive(ctx->exchprovctx, key, pkeylen, SIZE_MAX);
367 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
368 EVPerr(EVP_F_EVP_PKEY_DERIVE,
369 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
373 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
374 return ctx->pmeth->derive(ctx, key, pkeylen);