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 OSSL_DISPATCH *fns,
37 EVP_KEYEXCH *exchange = NULL;
40 if ((exchange = evp_keyexch_new(prov)) == NULL)
43 for (; fns->function_id != 0; fns++) {
44 switch (fns->function_id) {
45 case OSSL_FUNC_KEYEXCH_NEWCTX:
46 if (exchange->newctx != NULL)
48 exchange->newctx = OSSL_get_OP_keyexch_newctx(fns);
51 case OSSL_FUNC_KEYEXCH_INIT:
52 if (exchange->init != NULL)
54 exchange->init = OSSL_get_OP_keyexch_init(fns);
57 case OSSL_FUNC_KEYEXCH_SET_PEER:
58 if (exchange->set_peer != NULL)
60 exchange->set_peer = OSSL_get_OP_keyexch_set_peer(fns);
62 case OSSL_FUNC_KEYEXCH_DERIVE:
63 if (exchange->derive != NULL)
65 exchange->derive = OSSL_get_OP_keyexch_derive(fns);
68 case OSSL_FUNC_KEYEXCH_FREECTX:
69 if (exchange->freectx != NULL)
71 exchange->freectx = OSSL_get_OP_keyexch_freectx(fns);
74 case OSSL_FUNC_KEYEXCH_DUPCTX:
75 if (exchange->dupctx != NULL)
77 exchange->dupctx = OSSL_get_OP_keyexch_dupctx(fns);
83 * In order to be a consistent set of functions we must have at least
84 * a complete set of "exchange" functions: init, derive, newctx,
85 * and freectx. The dupctx and set_peer functions are optional.
87 EVP_KEYEXCH_free(exchange);
88 EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH,
89 EVP_R_INVALID_PROVIDER_FUNCTIONS);
96 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
98 if (exchange != NULL) {
101 CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
104 ossl_provider_free(exchange->prov);
105 CRYPTO_THREAD_lock_free(exchange->lock);
106 OPENSSL_free(exchange);
110 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
114 CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
118 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OPENSSL_CTX *ctx, const char *algorithm,
119 const char *properties)
121 return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
122 evp_keyexch_from_dispatch,
123 (int (*)(void *))EVP_KEYEXCH_up_ref,
124 (void (*)(void *))EVP_KEYEXCH_free);
127 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, EVP_KEYEXCH *exchange)
130 OSSL_PARAM *param = NULL;
133 ctx->operation = EVP_PKEY_OP_DERIVE;
135 if (ctx->engine != NULL)
138 if (exchange != NULL) {
139 if (!EVP_KEYEXCH_up_ref(exchange))
142 int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
145 * TODO(3.0): Check for legacy handling. Remove this once all all
146 * algorithms are moved to providers.
148 if (ctx->pkey != NULL) {
149 switch (ctx->pkey->type) {
157 exchange = EVP_KEYEXCH_fetch(NULL, OBJ_nid2sn(nid), NULL);
162 if (exchange == NULL) {
163 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
168 if (ctx->exchprovctx != NULL && ctx->exchange != NULL)
169 ctx->exchange->freectx(ctx->exchprovctx);
170 EVP_KEYEXCH_free(ctx->exchange);
171 ctx->exchange = exchange;
172 if (ctx->pkey != NULL) {
173 param = evp_pkey_to_param(ctx->pkey, ¶msz);
175 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
179 ctx->exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
180 if (ctx->exchprovctx == NULL) {
181 OPENSSL_secure_clear_free(param, paramsz);
182 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
185 ret = exchange->init(ctx->exchprovctx, param);
187 * TODO(3.0): Really we should detect whether to call OPENSSL_free or
188 * OPENSSL_secure_clear_free based on the presence of a private key or not.
189 * Since we always expect a private key to be present we just call
190 * OPENSSL_secure_clear_free for now.
192 OPENSSL_secure_clear_free(param, paramsz);
196 ctx->operation = EVP_PKEY_OP_UNDEFINED;
200 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
201 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX,
202 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
206 if (ctx->pmeth->derive_init == NULL)
208 ret = ctx->pmeth->derive_init(ctx);
210 ctx->operation = EVP_PKEY_OP_UNDEFINED;
214 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
216 return EVP_PKEY_derive_init_ex(ctx, NULL);
219 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
222 OSSL_PARAM *param = NULL;
225 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
226 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
230 if (ctx->exchprovctx == NULL)
233 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
234 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
235 EVP_R_OPERATON_NOT_INITIALIZED);
239 if (ctx->exchange->set_peer == NULL) {
240 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
241 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
245 param = evp_pkey_to_param(peer, NULL);
247 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, ERR_R_INTERNAL_ERROR);
250 ret = ctx->exchange->set_peer(ctx->exchprovctx, param);
252 * TODO(3.0): Really we should detect whether to call OPENSSL_free or
253 * OPENSSL_secure_clear_free based on the presence of a private key or not.
254 * Since we always expect a public key to be present we just call
255 * OPENSSL_free for now.
262 if (ctx->pmeth == NULL
263 || !(ctx->pmeth->derive != NULL
264 || ctx->pmeth->encrypt != NULL
265 || ctx->pmeth->decrypt != NULL)
266 || ctx->pmeth->ctrl == NULL) {
267 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
268 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
271 if (ctx->operation != EVP_PKEY_OP_DERIVE
272 && ctx->operation != EVP_PKEY_OP_ENCRYPT
273 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
274 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
275 EVP_R_OPERATON_NOT_INITIALIZED);
279 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
287 if (ctx->pkey == NULL) {
288 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
292 if (ctx->pkey->type != peer->type) {
293 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
298 * For clarity. The error is if parameters in peer are
299 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
300 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
301 * (different key types) is impossible here because it is checked earlier.
302 * -2 is OK for us here, as well as 1, so we can check for 0 only.
304 if (!EVP_PKEY_missing_parameters(peer) &&
305 !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
306 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
310 EVP_PKEY_free(ctx->peerkey);
313 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
320 EVP_PKEY_up_ref(peer);
324 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
329 EVPerr(EVP_F_EVP_PKEY_DERIVE,
330 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
334 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
335 EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
339 if (ctx->exchprovctx == NULL)
342 ret = ctx->exchange->derive(ctx->exchprovctx, key, pkeylen, SIZE_MAX);
346 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
347 EVPerr(EVP_F_EVP_PKEY_DERIVE,
348 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
352 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
353 return ctx->pmeth->derive(ctx, key, pkeylen);