Add FFC param/key validation
[openssl.git] / providers / implementations / exchange / dh_exch.c
1 /*
2  * Copyright 2019 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/core_numbers.h>
12 #include <openssl/core_names.h>
13 #include <openssl/dh.h>
14 #include <openssl/params.h>
15 #include "prov/implementations.h"
16 #include "prov/provider_ctx.h"
17 #include "crypto/dh.h"
18
19 static OSSL_OP_keyexch_newctx_fn dh_newctx;
20 static OSSL_OP_keyexch_init_fn dh_init;
21 static OSSL_OP_keyexch_set_peer_fn dh_set_peer;
22 static OSSL_OP_keyexch_derive_fn dh_derive;
23 static OSSL_OP_keyexch_freectx_fn dh_freectx;
24 static OSSL_OP_keyexch_dupctx_fn dh_dupctx;
25 static OSSL_OP_keyexch_set_ctx_params_fn dh_set_ctx_params;
26 static OSSL_OP_keyexch_settable_ctx_params_fn dh_settable_ctx_params;
27
28 /*
29  * What's passed as an actual key is defined by the KEYMGMT interface.
30  * We happen to know that our KEYMGMT simply passes DH structures, so
31  * we use that here too.
32  */
33
34 typedef struct {
35     OPENSSL_CTX *libctx;
36     DH *dh;
37     DH *dhpeer;
38     unsigned int pad : 1;
39 } PROV_DH_CTX;
40
41 static void *dh_newctx(void *provctx)
42 {
43     PROV_DH_CTX *pdhctx = OPENSSL_zalloc(sizeof(PROV_DH_CTX));
44
45     if (pdhctx == NULL)
46         return NULL;
47     pdhctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
48     return pdhctx;
49 }
50
51 static int dh_init(void *vpdhctx, void *vdh)
52 {
53     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
54
55     if (pdhctx == NULL || vdh == NULL || !DH_up_ref(vdh))
56         return 0;
57     DH_free(pdhctx->dh);
58     pdhctx->dh = vdh;
59     return 1;
60 }
61
62 static int dh_set_peer(void *vpdhctx, void *vdh)
63 {
64     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
65
66     if (pdhctx == NULL || vdh == NULL || !DH_up_ref(vdh))
67         return 0;
68     DH_free(pdhctx->dhpeer);
69     pdhctx->dhpeer = vdh;
70     return 1;
71 }
72
73 static int dh_derive(void *vpdhctx, unsigned char *secret, size_t *secretlen,
74                      size_t outlen)
75 {
76     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
77     int ret;
78     size_t dhsize;
79     const BIGNUM *pub_key = NULL;
80
81     /* TODO(3.0): Add errors to stack */
82     if (pdhctx->dh == NULL || pdhctx->dhpeer == NULL)
83         return 0;
84
85     dhsize = (size_t)DH_size(pdhctx->dh);
86     if (secret == NULL) {
87         *secretlen = dhsize;
88         return 1;
89     }
90     if (outlen < dhsize)
91         return 0;
92
93     DH_get0_key(pdhctx->dhpeer, &pub_key, NULL);
94     if (pdhctx->pad)
95         ret = DH_compute_key_padded(secret, pub_key, pdhctx->dh);
96     else
97         ret = DH_compute_key(secret, pub_key, pdhctx->dh);
98     if (ret <= 0)
99         return 0;
100
101     *secretlen = ret;
102     return 1;
103 }
104
105 static void dh_freectx(void *vpdhctx)
106 {
107     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
108
109     DH_free(pdhctx->dh);
110     DH_free(pdhctx->dhpeer);
111
112     OPENSSL_free(pdhctx);
113 }
114
115 static void *dh_dupctx(void *vpdhctx)
116 {
117     PROV_DH_CTX *srcctx = (PROV_DH_CTX *)vpdhctx;
118     PROV_DH_CTX *dstctx;
119
120     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
121     if (dstctx == NULL)
122         return NULL;
123
124     *dstctx = *srcctx;
125     if (dstctx->dh != NULL && !DH_up_ref(dstctx->dh)) {
126         OPENSSL_free(dstctx);
127         return NULL;
128     }
129
130     if (dstctx->dhpeer != NULL && !DH_up_ref(dstctx->dhpeer)) {
131         DH_free(dstctx->dh);
132         OPENSSL_free(dstctx);
133         return NULL;
134     }
135
136     return dstctx;
137 }
138
139 static int dh_set_ctx_params(void *vpdhctx, const OSSL_PARAM params[])
140 {
141     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
142     const OSSL_PARAM *p;
143     unsigned int pad;
144
145     if (pdhctx == NULL || params == NULL)
146         return 0;
147
148     p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_PAD);
149     if (p == NULL || !OSSL_PARAM_get_uint(p, &pad))
150         return 0;
151     pdhctx->pad = pad ? 1 : 0;
152     return 1;
153 }
154
155 static const OSSL_PARAM known_settable_ctx_params[] = {
156     OSSL_PARAM_int(OSSL_EXCHANGE_PARAM_PAD, NULL),
157     OSSL_PARAM_END
158 };
159
160 static const OSSL_PARAM *dh_settable_ctx_params(void)
161 {
162     return known_settable_ctx_params;
163 }
164
165 const OSSL_DISPATCH dh_keyexch_functions[] = {
166     { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))dh_newctx },
167     { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))dh_init },
168     { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))dh_derive },
169     { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))dh_set_peer },
170     { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))dh_freectx },
171     { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))dh_dupctx },
172     { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))dh_set_ctx_params },
173     { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS,
174       (void (*)(void))dh_settable_ctx_params },
175     { 0, NULL }
176 };