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 if (exchange
== NULL
) {
24 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
28 exchange
->lock
= CRYPTO_THREAD_lock_new();
29 if (exchange
->lock
== NULL
) {
30 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
31 OPENSSL_free(exchange
);
34 exchange
->prov
= prov
;
35 ossl_provider_up_ref(prov
);
41 static void *evp_keyexch_from_dispatch(int name_id
,
42 const OSSL_DISPATCH
*fns
,
45 EVP_KEYEXCH
*exchange
= NULL
;
46 int fncnt
= 0, sparamfncnt
= 0, gparamfncnt
= 0;
48 if ((exchange
= evp_keyexch_new(prov
)) == NULL
) {
49 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
53 exchange
->name_id
= name_id
;
55 for (; fns
->function_id
!= 0; fns
++) {
56 switch (fns
->function_id
) {
57 case OSSL_FUNC_KEYEXCH_NEWCTX
:
58 if (exchange
->newctx
!= NULL
)
60 exchange
->newctx
= OSSL_get_OP_keyexch_newctx(fns
);
63 case OSSL_FUNC_KEYEXCH_INIT
:
64 if (exchange
->init
!= NULL
)
66 exchange
->init
= OSSL_get_OP_keyexch_init(fns
);
69 case OSSL_FUNC_KEYEXCH_SET_PEER
:
70 if (exchange
->set_peer
!= NULL
)
72 exchange
->set_peer
= OSSL_get_OP_keyexch_set_peer(fns
);
74 case OSSL_FUNC_KEYEXCH_DERIVE
:
75 if (exchange
->derive
!= NULL
)
77 exchange
->derive
= OSSL_get_OP_keyexch_derive(fns
);
80 case OSSL_FUNC_KEYEXCH_FREECTX
:
81 if (exchange
->freectx
!= NULL
)
83 exchange
->freectx
= OSSL_get_OP_keyexch_freectx(fns
);
86 case OSSL_FUNC_KEYEXCH_DUPCTX
:
87 if (exchange
->dupctx
!= NULL
)
89 exchange
->dupctx
= OSSL_get_OP_keyexch_dupctx(fns
);
91 case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS
:
92 if (exchange
->get_ctx_params
!= NULL
)
94 exchange
->get_ctx_params
= OSSL_get_OP_keyexch_get_ctx_params(fns
);
97 case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS
:
98 if (exchange
->gettable_ctx_params
!= NULL
)
100 exchange
->gettable_ctx_params
101 = OSSL_get_OP_keyexch_gettable_ctx_params(fns
);
104 case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS
:
105 if (exchange
->set_ctx_params
!= NULL
)
107 exchange
->set_ctx_params
= OSSL_get_OP_keyexch_set_ctx_params(fns
);
110 case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS
:
111 if (exchange
->settable_ctx_params
!= NULL
)
113 exchange
->settable_ctx_params
114 = OSSL_get_OP_keyexch_settable_ctx_params(fns
);
120 || (gparamfncnt
!= 0 && gparamfncnt
!= 2)
121 || (sparamfncnt
!= 0 && sparamfncnt
!= 2)) {
123 * In order to be a consistent set of functions we must have at least
124 * a complete set of "exchange" functions: init, derive, newctx,
125 * and freectx. The set_ctx_params and settable_ctx_params functions are
126 * optional, but if one of them is present then the other one must also
127 * be present. Same goes for get_ctx_params and gettable_ctx_params.
128 * The dupctx and set_peer functions are optional.
130 EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH
,
131 EVP_R_INVALID_PROVIDER_FUNCTIONS
);
138 EVP_KEYEXCH_free(exchange
);
142 void EVP_KEYEXCH_free(EVP_KEYEXCH
*exchange
)
144 if (exchange
!= NULL
) {
147 CRYPTO_DOWN_REF(&exchange
->refcnt
, &i
, exchange
->lock
);
150 ossl_provider_free(exchange
->prov
);
151 CRYPTO_THREAD_lock_free(exchange
->lock
);
152 OPENSSL_free(exchange
);
156 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH
*exchange
)
160 CRYPTO_UP_REF(&exchange
->refcnt
, &ref
, exchange
->lock
);
164 OSSL_PROVIDER
*EVP_KEYEXCH_provider(const EVP_KEYEXCH
*exchange
)
166 return exchange
->prov
;
169 EVP_KEYEXCH
*EVP_KEYEXCH_fetch(OPENSSL_CTX
*ctx
, const char *algorithm
,
170 const char *properties
)
172 return evp_generic_fetch(ctx
, OSSL_OP_KEYEXCH
, algorithm
, properties
,
173 evp_keyexch_from_dispatch
,
174 (int (*)(void *))EVP_KEYEXCH_up_ref
,
175 (void (*)(void *))EVP_KEYEXCH_free
);
178 int EVP_PKEY_derive_init(EVP_PKEY_CTX
*ctx
)
181 void *provkey
= NULL
;
182 EVP_KEYEXCH
*exchange
= NULL
;
183 EVP_KEYMGMT
*tmp_keymgmt
= NULL
;
184 const char *supported_exch
= NULL
;
187 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
191 evp_pkey_ctx_free_old_ops(ctx
);
192 ctx
->operation
= EVP_PKEY_OP_DERIVE
;
195 * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
196 * calls can be removed.
200 if (ctx
->engine
!= NULL
|| ctx
->keytype
== NULL
)
203 /* Ensure that the key is provided. If not, go legacy */
204 tmp_keymgmt
= ctx
->keymgmt
;
205 provkey
= evp_pkey_make_provided(ctx
->pkey
, ctx
->libctx
,
206 &tmp_keymgmt
, ctx
->propquery
);
209 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt
)) {
210 ERR_clear_last_mark();
211 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
214 EVP_KEYMGMT_free(ctx
->keymgmt
);
215 ctx
->keymgmt
= tmp_keymgmt
;
217 if (ctx
->keymgmt
->query_operation_name
!= NULL
)
218 supported_exch
= ctx
->keymgmt
->query_operation_name(OSSL_OP_KEYEXCH
);
221 * If we didn't get a supported exch, assume there is one with the
222 * same name as the key type.
224 if (supported_exch
== NULL
)
225 supported_exch
= ctx
->keytype
;
228 * Because we cleared out old ops, we shouldn't need to worry about
229 * checking if exchange is already there.
231 exchange
= EVP_KEYEXCH_fetch(ctx
->libctx
, supported_exch
, ctx
->propquery
);
234 || (EVP_KEYMGMT_provider(ctx
->keymgmt
)
235 != EVP_KEYEXCH_provider(exchange
))) {
237 * We don't need to free ctx->keymgmt here, as it's not necessarily
238 * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
240 EVP_KEYEXCH_free(exchange
);
245 * TODO remove this when legacy is gone
246 * If we don't have the full support we need with provided methods,
247 * let's go see if legacy does.
251 /* No more legacy from here down to legacy: */
253 ctx
->op
.kex
.exchange
= exchange
;
254 ctx
->op
.kex
.exchprovctx
= exchange
->newctx(ossl_provider_ctx(exchange
->prov
));
255 if (ctx
->op
.kex
.exchprovctx
== NULL
) {
256 /* The provider key can stay in the cache */
257 EVPerr(0, EVP_R_INITIALIZATION_ERROR
);
260 ret
= exchange
->init(ctx
->op
.kex
.exchprovctx
, provkey
);
264 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
269 * TODO remove this when legacy is gone
270 * If we don't have the full support we need with provided methods,
271 * let's go see if legacy does.
278 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->derive
== NULL
) {
279 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
283 if (ctx
->pmeth
->derive_init
== NULL
)
285 ret
= ctx
->pmeth
->derive_init(ctx
);
287 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
292 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*peer
)
295 void *provkey
= NULL
;
298 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER
,
299 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
303 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx
) || ctx
->op
.kex
.exchprovctx
== NULL
)
306 if (ctx
->op
.kex
.exchange
->set_peer
== NULL
) {
307 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER
,
308 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
312 provkey
= evp_pkey_make_provided(peer
, ctx
->libctx
, &ctx
->keymgmt
,
315 * If making the key provided wasn't possible, legacy may be able to pick
320 return ctx
->op
.kex
.exchange
->set_peer(ctx
->op
.kex
.exchprovctx
, provkey
);
327 * TODO(3.0) investigate the case where the operation is deemed legacy,
328 * but the given peer key is provider only.
330 if (ctx
->pmeth
== NULL
331 || !(ctx
->pmeth
->derive
!= NULL
332 || ctx
->pmeth
->encrypt
!= NULL
333 || ctx
->pmeth
->decrypt
!= NULL
)
334 || ctx
->pmeth
->ctrl
== NULL
) {
335 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER
,
336 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
339 if (ctx
->operation
!= EVP_PKEY_OP_DERIVE
340 && ctx
->operation
!= EVP_PKEY_OP_ENCRYPT
341 && ctx
->operation
!= EVP_PKEY_OP_DECRYPT
) {
342 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER
,
343 EVP_R_OPERATON_NOT_INITIALIZED
);
347 ret
= ctx
->pmeth
->ctrl(ctx
, EVP_PKEY_CTRL_PEER_KEY
, 0, peer
);
355 if (ctx
->pkey
== NULL
) {
356 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER
, EVP_R_NO_KEY_SET
);
360 if (ctx
->pkey
->type
!= peer
->type
) {
361 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER
, EVP_R_DIFFERENT_KEY_TYPES
);
366 * For clarity. The error is if parameters in peer are
367 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
368 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
369 * (different key types) is impossible here because it is checked earlier.
370 * -2 is OK for us here, as well as 1, so we can check for 0 only.
372 if (!EVP_PKEY_missing_parameters(peer
) &&
373 !EVP_PKEY_cmp_parameters(ctx
->pkey
, peer
)) {
374 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER
, EVP_R_DIFFERENT_PARAMETERS
);
378 EVP_PKEY_free(ctx
->peerkey
);
381 ret
= ctx
->pmeth
->ctrl(ctx
, EVP_PKEY_CTRL_PEER_KEY
, 1, peer
);
388 EVP_PKEY_up_ref(peer
);
393 int EVP_PKEY_derive(EVP_PKEY_CTX
*ctx
, unsigned char *key
, size_t *pkeylen
)
398 EVPerr(EVP_F_EVP_PKEY_DERIVE
,
399 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
403 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
404 EVPerr(EVP_F_EVP_PKEY_DERIVE
, EVP_R_OPERATON_NOT_INITIALIZED
);
408 if (ctx
->op
.kex
.exchprovctx
== NULL
)
411 ret
= ctx
->op
.kex
.exchange
->derive(ctx
->op
.kex
.exchprovctx
, key
, pkeylen
,
416 if (ctx
== NULL
|| ctx
->pmeth
== NULL
|| ctx
->pmeth
->derive
== NULL
) {
417 EVPerr(EVP_F_EVP_PKEY_DERIVE
,
418 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
422 M_check_autoarg(ctx
, key
, pkeylen
, EVP_F_EVP_PKEY_DERIVE
)
423 return ctx
->pmeth
->derive(ctx
, key
, pkeylen
);
426 int EVP_KEYEXCH_number(const EVP_KEYEXCH
*keyexch
)
428 return keyexch
->name_id
;
431 int EVP_KEYEXCH_is_a(const EVP_KEYEXCH
*keyexch
, const char *name
)
433 return evp_is_a(keyexch
->prov
, keyexch
->name_id
, NULL
, name
);
436 void EVP_KEYEXCH_do_all_provided(OPENSSL_CTX
*libctx
,
437 void (*fn
)(EVP_KEYEXCH
*keyexch
, void *arg
),
440 evp_generic_do_all(libctx
, OSSL_OP_KEYEXCH
,
441 (void (*)(void *, void *))fn
, arg
,
442 evp_keyexch_from_dispatch
,
443 (void (*)(void *))EVP_KEYEXCH_free
);
446 void EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH
*keyexch
,
447 void (*fn
)(const char *name
, void *data
),
450 if (keyexch
->prov
!= NULL
)
451 evp_names_do_all(keyexch
->prov
, keyexch
->name_id
, fn
, data
);