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