Invoke tear_down when exiting test_encode_tls_sct() prematurely
[openssl.git] / providers / implementations / exchange / ecdh_exch.c
1 /*
2  * Copyright 2020-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 /*
11  * ECDH low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <string.h>
17 #include <openssl/crypto.h>
18 #include <openssl/evp.h>
19 #include <openssl/core_dispatch.h>
20 #include <openssl/core_names.h>
21 #include <openssl/ec.h>
22 #include <openssl/params.h>
23 #include <openssl/err.h>
24 #include "prov/provider_ctx.h"
25 #include "prov/providercommon.h"
26 #include "prov/implementations.h"
27 #include "prov/securitycheck.h"
28 #include "crypto/ec.h" /* ossl_ecdh_kdf_X9_63() */
29
30 static OSSL_FUNC_keyexch_newctx_fn ecdh_newctx;
31 static OSSL_FUNC_keyexch_init_fn ecdh_init;
32 static OSSL_FUNC_keyexch_set_peer_fn ecdh_set_peer;
33 static OSSL_FUNC_keyexch_derive_fn ecdh_derive;
34 static OSSL_FUNC_keyexch_freectx_fn ecdh_freectx;
35 static OSSL_FUNC_keyexch_dupctx_fn ecdh_dupctx;
36 static OSSL_FUNC_keyexch_set_ctx_params_fn ecdh_set_ctx_params;
37 static OSSL_FUNC_keyexch_settable_ctx_params_fn ecdh_settable_ctx_params;
38 static OSSL_FUNC_keyexch_get_ctx_params_fn ecdh_get_ctx_params;
39 static OSSL_FUNC_keyexch_gettable_ctx_params_fn ecdh_gettable_ctx_params;
40
41 enum kdf_type {
42     PROV_ECDH_KDF_NONE = 0,
43     PROV_ECDH_KDF_X9_63
44 };
45
46 /*
47  * What's passed as an actual key is defined by the KEYMGMT interface.
48  * We happen to know that our KEYMGMT simply passes EC_KEY structures, so
49  * we use that here too.
50  */
51
52 typedef struct {
53     OSSL_LIB_CTX *libctx;
54
55     EC_KEY *k;
56     EC_KEY *peerk;
57
58     /*
59      * ECDH cofactor mode:
60      *
61      *  . 0  disabled
62      *  . 1  enabled
63      *  . -1 use cofactor mode set for k
64      */
65     int cofactor_mode;
66
67     /************
68      * ECDH KDF *
69      ************/
70     /* KDF (if any) to use for ECDH */
71     enum kdf_type kdf_type;
72     /* Message digest to use for key derivation */
73     EVP_MD *kdf_md;
74     /* User key material */
75     unsigned char *kdf_ukm;
76     size_t kdf_ukmlen;
77     /* KDF output length */
78     size_t kdf_outlen;
79 } PROV_ECDH_CTX;
80
81 static
82 void *ecdh_newctx(void *provctx)
83 {
84     PROV_ECDH_CTX *pectx;
85
86     if (!ossl_prov_is_running())
87         return NULL;
88
89     pectx = OPENSSL_zalloc(sizeof(*pectx));
90     if (pectx == NULL)
91         return NULL;
92
93     pectx->libctx = PROV_LIBCTX_OF(provctx);
94     pectx->cofactor_mode = -1;
95     pectx->kdf_type = PROV_ECDH_KDF_NONE;
96
97     return (void *)pectx;
98 }
99
100 static
101 int ecdh_init(void *vpecdhctx, void *vecdh)
102 {
103     PROV_ECDH_CTX *pecdhctx = (PROV_ECDH_CTX *)vpecdhctx;
104
105     if (!ossl_prov_is_running()
106             || pecdhctx == NULL
107             || vecdh == NULL
108             || !EC_KEY_up_ref(vecdh))
109         return 0;
110     EC_KEY_free(pecdhctx->k);
111     pecdhctx->k = vecdh;
112     pecdhctx->cofactor_mode = -1;
113     pecdhctx->kdf_type = PROV_ECDH_KDF_NONE;
114     return ossl_ec_check_key(vecdh, 1);
115 }
116
117 static
118 int ecdh_set_peer(void *vpecdhctx, void *vecdh)
119 {
120     PROV_ECDH_CTX *pecdhctx = (PROV_ECDH_CTX *)vpecdhctx;
121
122     if (!ossl_prov_is_running()
123             || pecdhctx == NULL
124             || vecdh == NULL
125             || !EC_KEY_up_ref(vecdh))
126         return 0;
127     EC_KEY_free(pecdhctx->peerk);
128     pecdhctx->peerk = vecdh;
129     return ossl_ec_check_key(vecdh, 1);
130 }
131
132 static
133 void ecdh_freectx(void *vpecdhctx)
134 {
135     PROV_ECDH_CTX *pecdhctx = (PROV_ECDH_CTX *)vpecdhctx;
136
137     EC_KEY_free(pecdhctx->k);
138     EC_KEY_free(pecdhctx->peerk);
139
140     EVP_MD_free(pecdhctx->kdf_md);
141     OPENSSL_clear_free(pecdhctx->kdf_ukm, pecdhctx->kdf_ukmlen);
142
143     OPENSSL_free(pecdhctx);
144 }
145
146 static
147 void *ecdh_dupctx(void *vpecdhctx)
148 {
149     PROV_ECDH_CTX *srcctx = (PROV_ECDH_CTX *)vpecdhctx;
150     PROV_ECDH_CTX *dstctx;
151
152     if (!ossl_prov_is_running())
153         return NULL;
154
155     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
156     if (dstctx == NULL)
157         return NULL;
158
159     *dstctx = *srcctx;
160
161     /* clear all pointers */
162
163     dstctx->k= NULL;
164     dstctx->peerk = NULL;
165     dstctx->kdf_md = NULL;
166     dstctx->kdf_ukm = NULL;
167
168     /* up-ref all ref-counted objects referenced in dstctx */
169
170     if (srcctx->k != NULL && !EC_KEY_up_ref(srcctx->k))
171         goto err;
172     else
173         dstctx->k = srcctx->k;
174
175     if (srcctx->peerk != NULL && !EC_KEY_up_ref(srcctx->peerk))
176         goto err;
177     else
178         dstctx->peerk = srcctx->peerk;
179
180     if (srcctx->kdf_md != NULL && !EVP_MD_up_ref(srcctx->kdf_md))
181         goto err;
182     else
183         dstctx->kdf_md = srcctx->kdf_md;
184
185     /* Duplicate UKM data if present */
186     if (srcctx->kdf_ukm != NULL && srcctx->kdf_ukmlen > 0) {
187         dstctx->kdf_ukm = OPENSSL_memdup(srcctx->kdf_ukm,
188                                          srcctx->kdf_ukmlen);
189         if (dstctx->kdf_ukm == NULL)
190             goto err;
191     }
192
193     return dstctx;
194
195  err:
196     ecdh_freectx(dstctx);
197     return NULL;
198 }
199
200 static
201 int ecdh_set_ctx_params(void *vpecdhctx, const OSSL_PARAM params[])
202 {
203     char name[80] = { '\0' }; /* should be big enough */
204     char *str = NULL;
205     PROV_ECDH_CTX *pectx = (PROV_ECDH_CTX *)vpecdhctx;
206     const OSSL_PARAM *p;
207
208     if (pectx == NULL || params == NULL)
209         return 0;
210
211     p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE);
212     if (p != NULL) {
213         int mode;
214
215         if (!OSSL_PARAM_get_int(p, &mode))
216             return 0;
217
218         if (mode < -1 || mode > 1)
219             return 0;
220
221         pectx->cofactor_mode = mode;
222     }
223
224     p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_KDF_TYPE);
225     if (p != NULL) {
226         str = name;
227         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(name)))
228             return 0;
229
230         if (name[0] == '\0')
231             pectx->kdf_type = PROV_ECDH_KDF_NONE;
232         else if (strcmp(name, OSSL_KDF_NAME_X963KDF) == 0)
233             pectx->kdf_type = PROV_ECDH_KDF_X9_63;
234         else
235             return 0;
236     }
237
238     p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_KDF_DIGEST);
239     if (p != NULL) {
240         char mdprops[80] = { '\0' }; /* should be big enough */
241
242         str = name;
243         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(name)))
244             return 0;
245
246         str = mdprops;
247         p = OSSL_PARAM_locate_const(params,
248                                     OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS);
249
250         if (p != NULL) {
251             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
252                 return 0;
253         }
254
255         EVP_MD_free(pectx->kdf_md);
256         pectx->kdf_md = EVP_MD_fetch(pectx->libctx, name, mdprops);
257         if (!ossl_digest_is_allowed(pectx->kdf_md)) {
258             EVP_MD_free(pectx->kdf_md);
259             pectx->kdf_md = NULL;
260         }
261         if (pectx->kdf_md == NULL)
262             return 0;
263     }
264
265     p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_KDF_OUTLEN);
266     if (p != NULL) {
267         size_t outlen;
268
269         if (!OSSL_PARAM_get_size_t(p, &outlen))
270             return 0;
271         pectx->kdf_outlen = outlen;
272     }
273
274     p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_KDF_UKM);
275     if (p != NULL) {
276         void *tmp_ukm = NULL;
277         size_t tmp_ukmlen;
278
279         if (!OSSL_PARAM_get_octet_string(p, &tmp_ukm, 0, &tmp_ukmlen))
280             return 0;
281         OPENSSL_free(pectx->kdf_ukm);
282         pectx->kdf_ukm = tmp_ukm;
283         pectx->kdf_ukmlen = tmp_ukmlen;
284     }
285
286     return 1;
287 }
288
289 static const OSSL_PARAM known_settable_ctx_params[] = {
290     OSSL_PARAM_int(OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE, NULL),
291     OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE, NULL, 0),
292     OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST, NULL, 0),
293     OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS, NULL, 0),
294     OSSL_PARAM_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN, NULL),
295     OSSL_PARAM_octet_string(OSSL_EXCHANGE_PARAM_KDF_UKM, NULL, 0),
296     OSSL_PARAM_END
297 };
298
299 static
300 const OSSL_PARAM *ecdh_settable_ctx_params(ossl_unused void *provctx)
301 {
302     return known_settable_ctx_params;
303 }
304
305 static
306 int ecdh_get_ctx_params(void *vpecdhctx, OSSL_PARAM params[])
307 {
308     PROV_ECDH_CTX *pectx = (PROV_ECDH_CTX *)vpecdhctx;
309     OSSL_PARAM *p;
310
311     if (pectx == NULL || params == NULL)
312         return 0;
313
314     p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE);
315     if (p != NULL) {
316         int mode = pectx->cofactor_mode;
317
318         if (mode == -1) {
319             /* check what is the default for pecdhctx->k */
320             mode = EC_KEY_get_flags(pectx->k) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
321         }
322
323         if (!OSSL_PARAM_set_int(p, mode))
324             return 0;
325     }
326
327     p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_KDF_TYPE);
328     if (p != NULL) {
329         const char *kdf_type = NULL;
330
331         switch (pectx->kdf_type) {
332             case PROV_ECDH_KDF_NONE:
333                 kdf_type = "";
334                 break;
335             case PROV_ECDH_KDF_X9_63:
336                 kdf_type = OSSL_KDF_NAME_X963KDF;
337                 break;
338             default:
339                 return 0;
340         }
341
342         if (!OSSL_PARAM_set_utf8_string(p, kdf_type))
343             return 0;
344     }
345
346     p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_KDF_DIGEST);
347     if (p != NULL
348             && !OSSL_PARAM_set_utf8_string(p, pectx->kdf_md == NULL
349                                            ? ""
350                                            : EVP_MD_name(pectx->kdf_md))){
351         return 0;
352     }
353
354     p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_KDF_OUTLEN);
355     if (p != NULL && !OSSL_PARAM_set_size_t(p, pectx->kdf_outlen))
356         return 0;
357
358     p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_KDF_UKM);
359     if (p != NULL &&
360         !OSSL_PARAM_set_octet_ptr(p, pectx->kdf_ukm, pectx->kdf_ukmlen))
361         return 0;
362
363     return 1;
364 }
365
366 static const OSSL_PARAM known_gettable_ctx_params[] = {
367     OSSL_PARAM_int(OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE, NULL),
368     OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE, NULL, 0),
369     OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST, NULL, 0),
370     OSSL_PARAM_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN, NULL),
371     OSSL_PARAM_DEFN(OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR,
372                     NULL, 0),
373     OSSL_PARAM_END
374 };
375
376 static
377 const OSSL_PARAM *ecdh_gettable_ctx_params(ossl_unused void *provctx)
378 {
379     return known_gettable_ctx_params;
380 }
381
382 static ossl_inline
383 size_t ecdh_size(const EC_KEY *k)
384 {
385     size_t degree = 0;
386     const EC_GROUP *group;
387
388     if (k == NULL
389             || (group = EC_KEY_get0_group(k)) == NULL)
390         return 0;
391
392     degree = EC_GROUP_get_degree(group);
393
394     return (degree + 7) / 8;
395 }
396
397 static ossl_inline
398 int ecdh_plain_derive(void *vpecdhctx, unsigned char *secret,
399                       size_t *psecretlen, size_t outlen)
400 {
401     PROV_ECDH_CTX *pecdhctx = (PROV_ECDH_CTX *)vpecdhctx;
402     int retlen, ret = 0;
403     size_t ecdhsize, size;
404     const EC_POINT *ppubkey = NULL;
405     EC_KEY *privk = NULL;
406     const EC_GROUP *group;
407     const BIGNUM *cofactor;
408     int key_cofactor_mode;
409
410     if (pecdhctx->k == NULL || pecdhctx->peerk == NULL) {
411         ERR_raise(ERR_LIB_PROV, EC_R_KEYS_NOT_SET);
412         return 0;
413     }
414
415     ecdhsize = ecdh_size(pecdhctx->k);
416     if (secret == NULL) {
417         *psecretlen = ecdhsize;
418         return 1;
419     }
420
421     if ((group = EC_KEY_get0_group(pecdhctx->k)) == NULL
422             || (cofactor = EC_GROUP_get0_cofactor(group)) == NULL )
423         return 0;
424
425     /*
426      * NB: unlike PKCS#3 DH, if outlen is less than maximum size this is not
427      * an error, the result is truncated.
428      */
429     size = outlen < ecdhsize ? outlen : ecdhsize;
430
431     /*
432      * The ctx->cofactor_mode flag has precedence over the
433      * cofactor_mode flag set on ctx->k.
434      *
435      * - if ctx->cofactor_mode == -1, use ctx->k directly
436      * - if ctx->cofactor_mode == key_cofactor_mode, use ctx->k directly
437      * - if ctx->cofactor_mode != key_cofactor_mode:
438      *     - if ctx->k->cofactor == 1, the cofactor_mode flag is irrelevant, use
439      *          ctx->k directly
440      *     - if ctx->k->cofactor != 1, use a duplicate of ctx->k with the flag
441      *          set to ctx->cofactor_mode
442      */
443     key_cofactor_mode =
444         (EC_KEY_get_flags(pecdhctx->k) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
445     if (pecdhctx->cofactor_mode != -1
446             && pecdhctx->cofactor_mode != key_cofactor_mode
447             && !BN_is_one(cofactor)) {
448         if ((privk = EC_KEY_dup(pecdhctx->k)) == NULL)
449             return 0;
450
451         if (pecdhctx->cofactor_mode == 1)
452             EC_KEY_set_flags(privk, EC_FLAG_COFACTOR_ECDH);
453         else
454             EC_KEY_clear_flags(privk, EC_FLAG_COFACTOR_ECDH);
455     } else {
456         privk = pecdhctx->k;
457     }
458
459     ppubkey = EC_KEY_get0_public_key(pecdhctx->peerk);
460
461     retlen = ECDH_compute_key(secret, size, ppubkey, privk, NULL);
462
463     if (retlen <= 0)
464         goto end;
465
466     *psecretlen = retlen;
467     ret = 1;
468
469  end:
470     if (privk != pecdhctx->k)
471         EC_KEY_free(privk);
472     return ret;
473 }
474
475 static ossl_inline
476 int ecdh_X9_63_kdf_derive(void *vpecdhctx, unsigned char *secret,
477                           size_t *psecretlen, size_t outlen)
478 {
479     PROV_ECDH_CTX *pecdhctx = (PROV_ECDH_CTX *)vpecdhctx;
480     unsigned char *stmp = NULL;
481     size_t stmplen;
482     int ret = 0;
483
484     if (secret == NULL) {
485         *psecretlen = pecdhctx->kdf_outlen;
486         return 1;
487     }
488
489     if (pecdhctx->kdf_outlen > outlen)
490         return 0;
491     if (!ecdh_plain_derive(vpecdhctx, NULL, &stmplen, 0))
492         return 0;
493     if ((stmp = OPENSSL_secure_malloc(stmplen)) == NULL) {
494         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
495         return 0;
496     }
497     if (!ecdh_plain_derive(vpecdhctx, stmp, &stmplen, stmplen))
498         goto err;
499
500     /* Do KDF stuff */
501     if (!ossl_ecdh_kdf_X9_63(secret, pecdhctx->kdf_outlen,
502                              stmp, stmplen,
503                              pecdhctx->kdf_ukm,
504                              pecdhctx->kdf_ukmlen,
505                              pecdhctx->kdf_md,
506                              pecdhctx->libctx, NULL))
507         goto err;
508     *psecretlen = pecdhctx->kdf_outlen;
509     ret = 1;
510
511  err:
512     OPENSSL_secure_clear_free(stmp, stmplen);
513     return ret;
514 }
515
516 static
517 int ecdh_derive(void *vpecdhctx, unsigned char *secret,
518                 size_t *psecretlen, size_t outlen)
519 {
520     PROV_ECDH_CTX *pecdhctx = (PROV_ECDH_CTX *)vpecdhctx;
521
522     switch (pecdhctx->kdf_type) {
523         case PROV_ECDH_KDF_NONE:
524             return ecdh_plain_derive(vpecdhctx, secret, psecretlen, outlen);
525         case PROV_ECDH_KDF_X9_63:
526             return ecdh_X9_63_kdf_derive(vpecdhctx, secret, psecretlen, outlen);
527         default:
528             break;
529     }
530     return 0;
531 }
532
533 const OSSL_DISPATCH ossl_ecdh_keyexch_functions[] = {
534     { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))ecdh_newctx },
535     { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecdh_init },
536     { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecdh_derive },
537     { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecdh_set_peer },
538     { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecdh_freectx },
539     { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecdh_dupctx },
540     { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))ecdh_set_ctx_params },
541     { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS,
542       (void (*)(void))ecdh_settable_ctx_params },
543     { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))ecdh_get_ctx_params },
544     { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS,
545       (void (*)(void))ecdh_gettable_ctx_params },
546     { 0, NULL }
547 };