]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/exchange.c
Rename OPENSSL_CTX prefix to OSSL_LIB_CTX
[thirdparty/openssl.git] / crypto / evp / exchange.c
CommitLineData
ff64702b 1/*
33388b44 2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
ff64702b
MC
3 *
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
8 */
9
10#include <openssl/crypto.h>
11#include <openssl/evp.h>
12#include <openssl/err.h>
13#include "internal/refcount.h"
25f2138b 14#include "crypto/evp.h"
ff64702b 15#include "internal/provider.h"
ac5a61ca 16#include "internal/numbers.h" /* includes SIZE_MAX */
706457b7 17#include "evp_local.h"
ff64702b
MC
18
19static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov)
20{
21 EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH));
22
c1ff5994
MC
23 if (exchange == NULL) {
24 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
25 return NULL;
26 }
27
ff64702b
MC
28 exchange->lock = CRYPTO_THREAD_lock_new();
29 if (exchange->lock == NULL) {
c1ff5994 30 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
ff64702b
MC
31 OPENSSL_free(exchange);
32 return NULL;
33 }
34 exchange->prov = prov;
35 ossl_provider_up_ref(prov);
36 exchange->refcnt = 1;
37
38 return exchange;
39}
40
f7c16d48 41static void *evp_keyexch_from_dispatch(int name_id,
6b9e3724 42 const OSSL_DISPATCH *fns,
0ddf74bf 43 OSSL_PROVIDER *prov)
ff64702b
MC
44{
45 EVP_KEYEXCH *exchange = NULL;
4fe54d67 46 int fncnt = 0, sparamfncnt = 0, gparamfncnt = 0;
ff64702b 47
f7c16d48 48 if ((exchange = evp_keyexch_new(prov)) == NULL) {
3ca9d210
RL
49 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
50 goto err;
6b9e3724 51 }
ff64702b 52
f7c16d48 53 exchange->name_id = name_id;
3ca9d210 54
ff64702b
MC
55 for (; fns->function_id != 0; fns++) {
56 switch (fns->function_id) {
57 case OSSL_FUNC_KEYEXCH_NEWCTX:
58 if (exchange->newctx != NULL)
59 break;
363b1e5d 60 exchange->newctx = OSSL_FUNC_keyexch_newctx(fns);
ff64702b
MC
61 fncnt++;
62 break;
63 case OSSL_FUNC_KEYEXCH_INIT:
64 if (exchange->init != NULL)
65 break;
363b1e5d 66 exchange->init = OSSL_FUNC_keyexch_init(fns);
ff64702b
MC
67 fncnt++;
68 break;
69 case OSSL_FUNC_KEYEXCH_SET_PEER:
70 if (exchange->set_peer != NULL)
71 break;
363b1e5d 72 exchange->set_peer = OSSL_FUNC_keyexch_set_peer(fns);
ff64702b
MC
73 break;
74 case OSSL_FUNC_KEYEXCH_DERIVE:
75 if (exchange->derive != NULL)
76 break;
363b1e5d 77 exchange->derive = OSSL_FUNC_keyexch_derive(fns);
ff64702b
MC
78 fncnt++;
79 break;
80 case OSSL_FUNC_KEYEXCH_FREECTX:
81 if (exchange->freectx != NULL)
82 break;
363b1e5d 83 exchange->freectx = OSSL_FUNC_keyexch_freectx(fns);
ff64702b
MC
84 fncnt++;
85 break;
86 case OSSL_FUNC_KEYEXCH_DUPCTX:
87 if (exchange->dupctx != NULL)
88 break;
363b1e5d 89 exchange->dupctx = OSSL_FUNC_keyexch_dupctx(fns);
ff64702b 90 break;
4fe54d67
NT
91 case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS:
92 if (exchange->get_ctx_params != NULL)
93 break;
363b1e5d 94 exchange->get_ctx_params = OSSL_FUNC_keyexch_get_ctx_params(fns);
4fe54d67
NT
95 gparamfncnt++;
96 break;
97 case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS:
98 if (exchange->gettable_ctx_params != NULL)
99 break;
100 exchange->gettable_ctx_params
363b1e5d 101 = OSSL_FUNC_keyexch_gettable_ctx_params(fns);
4fe54d67
NT
102 gparamfncnt++;
103 break;
9c45222d
MC
104 case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
105 if (exchange->set_ctx_params != NULL)
35aca9ec 106 break;
363b1e5d 107 exchange->set_ctx_params = OSSL_FUNC_keyexch_set_ctx_params(fns);
4fe54d67 108 sparamfncnt++;
9c45222d
MC
109 break;
110 case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
111 if (exchange->settable_ctx_params != NULL)
112 break;
113 exchange->settable_ctx_params
363b1e5d 114 = OSSL_FUNC_keyexch_settable_ctx_params(fns);
4fe54d67 115 sparamfncnt++;
35aca9ec 116 break;
ff64702b
MC
117 }
118 }
4fe54d67
NT
119 if (fncnt != 4
120 || (gparamfncnt != 0 && gparamfncnt != 2)
121 || (sparamfncnt != 0 && sparamfncnt != 2)) {
ff64702b
MC
122 /*
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,
9c45222d
MC
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
4fe54d67
NT
127 * be present. Same goes for get_ctx_params and gettable_ctx_params.
128 * The dupctx and set_peer functions are optional.
ff64702b 129 */
ff64702b
MC
130 EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH,
131 EVP_R_INVALID_PROVIDER_FUNCTIONS);
3ca9d210 132 goto err;
ff64702b
MC
133 }
134
135 return exchange;
3ca9d210
RL
136
137 err:
138 EVP_KEYEXCH_free(exchange);
3ca9d210 139 return NULL;
ff64702b
MC
140}
141
142void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
143{
144 if (exchange != NULL) {
145 int i;
146
147 CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
148 if (i > 0)
149 return;
150 ossl_provider_free(exchange->prov);
151 CRYPTO_THREAD_lock_free(exchange->lock);
152 OPENSSL_free(exchange);
153 }
154}
155
156int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
157{
158 int ref = 0;
159
160 CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
161 return 1;
162}
163
8b84b075
RL
164OSSL_PROVIDER *EVP_KEYEXCH_provider(const EVP_KEYEXCH *exchange)
165{
166 return exchange->prov;
167}
168
b4250010 169EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
ff64702b
MC
170 const char *properties)
171{
0ddf74bf
RL
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);
ff64702b
MC
176}
177
c0e0984f 178int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
ff64702b
MC
179{
180 int ret;
8b84b075 181 void *provkey = NULL;
c0e0984f 182 EVP_KEYEXCH *exchange = NULL;
f6aa5774
RL
183 EVP_KEYMGMT *tmp_keymgmt = NULL;
184 const char *supported_exch = NULL;
c0e0984f
RL
185
186 if (ctx == NULL) {
187 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
188 return -2;
189 }
ff64702b 190
864b89ce 191 evp_pkey_ctx_free_old_ops(ctx);
ff64702b
MC
192 ctx->operation = EVP_PKEY_OP_DERIVE;
193
0b9dd384
RL
194 /*
195 * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
196 * calls can be removed.
197 */
198 ERR_set_mark();
199
f21c9c64 200 if (evp_pkey_ctx_is_legacy(ctx))
ff64702b
MC
201 goto legacy;
202
3c6ed955
RL
203 /*
204 * Ensure that the key is provided, either natively, or as a cached export.
ac2d58c7 205 * If not, goto legacy
3c6ed955 206 */
f6aa5774 207 tmp_keymgmt = ctx->keymgmt;
ac2d58c7
MC
208 if (ctx->pkey == NULL) {
209 /*
210 * Some algorithms (e.g. legacy KDFs) don't have a pkey - so we create
211 * a blank one.
212 */
213 EVP_PKEY *pkey = EVP_PKEY_new();
214
215 if (pkey == NULL || !EVP_PKEY_set_type_by_keymgmt(pkey, tmp_keymgmt)) {
216 ERR_clear_last_mark();
217 EVP_PKEY_free(pkey);
218 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
219 goto err;
220 }
221 provkey = pkey->keydata = evp_keymgmt_newdata(tmp_keymgmt);
222 if (provkey == NULL)
223 EVP_PKEY_free(pkey);
224 else
225 ctx->pkey = pkey;
226 } else {
227 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
228 &tmp_keymgmt, ctx->propquery);
229 }
f6aa5774
RL
230 if (provkey == NULL)
231 goto legacy;
232 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
0b9dd384 233 ERR_clear_last_mark();
f6aa5774
RL
234 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
235 goto err;
c0e0984f 236 }
f6aa5774
RL
237 EVP_KEYMGMT_free(ctx->keymgmt);
238 ctx->keymgmt = tmp_keymgmt;
239
240 if (ctx->keymgmt->query_operation_name != NULL)
241 supported_exch = ctx->keymgmt->query_operation_name(OSSL_OP_KEYEXCH);
242
243 /*
244 * If we didn't get a supported exch, assume there is one with the
245 * same name as the key type.
246 */
247 if (supported_exch == NULL)
248 supported_exch = ctx->keytype;
249
250 /*
251 * Because we cleared out old ops, we shouldn't need to worry about
252 * checking if exchange is already there.
253 */
254 exchange = EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery);
255
256 if (exchange == NULL
c0e0984f
RL
257 || (EVP_KEYMGMT_provider(ctx->keymgmt)
258 != EVP_KEYEXCH_provider(exchange))) {
ff64702b 259 /*
0b9dd384
RL
260 * We don't need to free ctx->keymgmt here, as it's not necessarily
261 * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
ff64702b 262 */
c0e0984f
RL
263 EVP_KEYEXCH_free(exchange);
264 goto legacy;
ff64702b
MC
265 }
266
0b9dd384
RL
267 /*
268 * TODO remove this when legacy is gone
269 * If we don't have the full support we need with provided methods,
270 * let's go see if legacy does.
271 */
272 ERR_pop_to_mark();
273
274 /* No more legacy from here down to legacy: */
c0e0984f 275
864b89ce 276 ctx->op.kex.exchange = exchange;
864b89ce
MC
277 ctx->op.kex.exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
278 if (ctx->op.kex.exchprovctx == NULL) {
8b84b075 279 /* The provider key can stay in the cache */
c0e0984f 280 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
ff64702b
MC
281 goto err;
282 }
864b89ce 283 ret = exchange->init(ctx->op.kex.exchprovctx, provkey);
ff64702b
MC
284
285 return ret ? 1 : 0;
286 err:
c7fa9297 287 evp_pkey_ctx_free_old_ops(ctx);
ff64702b
MC
288 ctx->operation = EVP_PKEY_OP_UNDEFINED;
289 return 0;
290
291 legacy:
0b9dd384
RL
292 /*
293 * TODO remove this when legacy is gone
294 * If we don't have the full support we need with provided methods,
295 * let's go see if legacy does.
296 */
297 ERR_pop_to_mark();
298
f844f9eb 299#ifdef FIPS_MODULE
62f49b90
SL
300 return 0;
301#else
e0d8523e 302 if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
c0e0984f 303 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
ff64702b
MC
304 return -2;
305 }
306
307 if (ctx->pmeth->derive_init == NULL)
308 return 1;
309 ret = ctx->pmeth->derive_init(ctx);
310 if (ret <= 0)
311 ctx->operation = EVP_PKEY_OP_UNDEFINED;
312 return ret;
62f49b90 313#endif
ff64702b
MC
314}
315
ff64702b
MC
316int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
317{
62f49b90 318 int ret = 0;
8b84b075 319 void *provkey = NULL;
ff64702b
MC
320
321 if (ctx == NULL) {
322 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
323 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
324 return -2;
325 }
326
864b89ce 327 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.exchprovctx == NULL)
ff64702b
MC
328 goto legacy;
329
864b89ce 330 if (ctx->op.kex.exchange->set_peer == NULL) {
ff64702b
MC
331 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
332 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
333 return -2;
334 }
335
3c6ed955
RL
336 provkey = evp_pkey_export_to_provider(peer, ctx->libctx, &ctx->keymgmt,
337 ctx->propquery);
3f7ce7f1
RL
338 /*
339 * If making the key provided wasn't possible, legacy may be able to pick
340 * it up
341 */
e0d8523e
RL
342 if (provkey == NULL)
343 goto legacy;
864b89ce 344 return ctx->op.kex.exchange->set_peer(ctx->op.kex.exchprovctx, provkey);
ff64702b
MC
345
346 legacy:
f844f9eb 347#ifdef FIPS_MODULE
62f49b90
SL
348 return ret;
349#else
3f7ce7f1
RL
350 /*
351 * TODO(3.0) investigate the case where the operation is deemed legacy,
352 * but the given peer key is provider only.
353 */
ff64702b
MC
354 if (ctx->pmeth == NULL
355 || !(ctx->pmeth->derive != NULL
356 || ctx->pmeth->encrypt != NULL
357 || ctx->pmeth->decrypt != NULL)
358 || ctx->pmeth->ctrl == NULL) {
359 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
360 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
361 return -2;
362 }
363 if (ctx->operation != EVP_PKEY_OP_DERIVE
364 && ctx->operation != EVP_PKEY_OP_ENCRYPT
365 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
366 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
367 EVP_R_OPERATON_NOT_INITIALIZED);
368 return -1;
369 }
370
371 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
372
373 if (ret <= 0)
374 return ret;
375
376 if (ret == 2)
377 return 1;
378
379 if (ctx->pkey == NULL) {
380 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
381 return -1;
382 }
383
384 if (ctx->pkey->type != peer->type) {
385 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
386 return -1;
387 }
388
389 /*
390 * For clarity. The error is if parameters in peer are
c74aaa39 391 * present (!missing) but don't match. EVP_PKEY_parameters_eq may return
ff64702b
MC
392 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
393 * (different key types) is impossible here because it is checked earlier.
394 * -2 is OK for us here, as well as 1, so we can check for 0 only.
395 */
396 if (!EVP_PKEY_missing_parameters(peer) &&
c74aaa39 397 !EVP_PKEY_parameters_eq(ctx->pkey, peer)) {
ff64702b
MC
398 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
399 return -1;
400 }
401
402 EVP_PKEY_free(ctx->peerkey);
403 ctx->peerkey = peer;
404
405 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
406
407 if (ret <= 0) {
408 ctx->peerkey = NULL;
409 return ret;
410 }
411
412 EVP_PKEY_up_ref(peer);
413 return 1;
62f49b90 414#endif
ff64702b
MC
415}
416
417int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
418{
419 int ret;
420
421 if (ctx == NULL) {
422 EVPerr(EVP_F_EVP_PKEY_DERIVE,
423 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
424 return -2;
425 }
426
864b89ce 427 if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
ff64702b
MC
428 EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
429 return -1;
430 }
431
864b89ce 432 if (ctx->op.kex.exchprovctx == NULL)
ff64702b
MC
433 goto legacy;
434
864b89ce
MC
435 ret = ctx->op.kex.exchange->derive(ctx->op.kex.exchprovctx, key, pkeylen,
436 SIZE_MAX);
ff64702b
MC
437
438 return ret;
439 legacy:
440 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
441 EVPerr(EVP_F_EVP_PKEY_DERIVE,
442 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
443 return -2;
444 }
445
446 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
447 return ctx->pmeth->derive(ctx, key, pkeylen);
448}
251e610c 449
506cb0f6
RL
450int EVP_KEYEXCH_number(const EVP_KEYEXCH *keyexch)
451{
452 return keyexch->name_id;
453}
454
251e610c
RL
455int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
456{
e4a1d023 457 return evp_is_a(keyexch->prov, keyexch->name_id, NULL, name);
251e610c
RL
458}
459
b4250010 460void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx,
251e610c
RL
461 void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
462 void *arg)
463{
251e610c
RL
464 evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
465 (void (*)(void *, void *))fn, arg,
0ddf74bf 466 evp_keyexch_from_dispatch,
251e610c
RL
467 (void (*)(void *))EVP_KEYEXCH_free);
468}
f651c727
RL
469
470void EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
471 void (*fn)(const char *name, void *data),
472 void *data)
473{
474 if (keyexch->prov != NULL)
475 evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);
476}
e3efe7a5
SL
477
478const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch)
479{
480 void *provctx;
481
482 if (keyexch == NULL || keyexch->gettable_ctx_params == NULL)
483 return NULL;
484
485 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(keyexch));
486 return keyexch->gettable_ctx_params(provctx);
487}
488
489const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch)
490{
491 void *provctx;
492
493 if (keyexch == NULL || keyexch->settable_ctx_params == NULL)
494 return NULL;
495 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(keyexch));
496 return keyexch->settable_ctx_params(provctx);
497}