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