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