29152dca4d0d924a1d5706485f7d06f0af39ee51
[oweals/openssl.git] / crypto / dh / dh_lib.c
1 /*
2  * Copyright 1995-2018 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  * DH low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include <openssl/bn.h>
18 #include <openssl/engine.h>
19 #include <openssl/obj_mac.h>
20 #include "internal/cryptlib.h"
21 #include "internal/refcount.h"
22 #include "crypto/dh.h"
23 #include "dh_local.h"
24
25 static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
26
27 #ifndef FIPS_MODE
28 int DH_set_method(DH *dh, const DH_METHOD *meth)
29 {
30     /*
31      * NB: The caller is specifically setting a method, so it's not up to us
32      * to deal with which ENGINE it comes from.
33      */
34     const DH_METHOD *mtmp;
35     mtmp = dh->meth;
36     if (mtmp->finish)
37         mtmp->finish(dh);
38 #ifndef OPENSSL_NO_ENGINE
39     ENGINE_finish(dh->engine);
40     dh->engine = NULL;
41 #endif
42     dh->meth = meth;
43     if (meth->init)
44         meth->init(dh);
45     return 1;
46 }
47
48 DH *DH_new(void)
49 {
50     return dh_new_intern(NULL, NULL);
51 }
52
53 DH *DH_new_method(ENGINE *engine)
54 {
55     return dh_new_intern(engine, NULL);
56 }
57 #endif /* !FIPS_MODE */
58
59 DH *dh_new_with_ctx(OPENSSL_CTX *libctx)
60 {
61     return dh_new_intern(NULL, libctx);
62 }
63
64 static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
65 {
66     DH *ret = OPENSSL_zalloc(sizeof(*ret));
67
68     if (ret == NULL) {
69         DHerr(0, ERR_R_MALLOC_FAILURE);
70         return NULL;
71     }
72
73     ret->references = 1;
74     ret->lock = CRYPTO_THREAD_lock_new();
75     if (ret->lock == NULL) {
76         DHerr(0, ERR_R_MALLOC_FAILURE);
77         OPENSSL_free(ret);
78         return NULL;
79     }
80
81     ret->libctx = libctx;
82     ret->meth = DH_get_default_method();
83 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
84     ret->flags = ret->meth->flags;  /* early default init */
85     if (engine) {
86         if (!ENGINE_init(engine)) {
87             DHerr(0, ERR_R_ENGINE_LIB);
88             goto err;
89         }
90         ret->engine = engine;
91     } else
92         ret->engine = ENGINE_get_default_DH();
93     if (ret->engine) {
94         ret->meth = ENGINE_get_DH(ret->engine);
95         if (ret->meth == NULL) {
96             DHerr(0, ERR_R_ENGINE_LIB);
97             goto err;
98         }
99     }
100 #endif
101
102     ret->flags = ret->meth->flags;
103
104 #ifndef FIPS_MODE
105     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
106         goto err;
107 #endif /* FIPS_MODE */
108
109     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
110         DHerr(0, ERR_R_INIT_FAIL);
111         goto err;
112     }
113
114     return ret;
115
116  err:
117     DH_free(ret);
118     return NULL;
119 }
120
121 void DH_free(DH *r)
122 {
123     int i;
124
125     if (r == NULL)
126         return;
127
128     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
129     REF_PRINT_COUNT("DH", r);
130     if (i > 0)
131         return;
132     REF_ASSERT_ISNT(i < 0);
133
134     if (r->meth != NULL && r->meth->finish != NULL)
135         r->meth->finish(r);
136 #if !defined(FIPS_MODE)
137 # if !defined(OPENSSL_NO_ENGINE)
138     ENGINE_finish(r->engine);
139 # endif
140     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
141 #endif
142
143     CRYPTO_THREAD_lock_free(r->lock);
144
145     ffc_params_cleanup(&r->params);
146     BN_clear_free(r->pub_key);
147     BN_clear_free(r->priv_key);
148     OPENSSL_free(r);
149 }
150
151 int DH_up_ref(DH *r)
152 {
153     int i;
154
155     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
156         return 0;
157
158     REF_PRINT_COUNT("DH", r);
159     REF_ASSERT_ISNT(i < 2);
160     return ((i > 1) ? 1 : 0);
161 }
162
163 #ifndef FIPS_MODE
164 int DH_set_ex_data(DH *d, int idx, void *arg)
165 {
166     return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
167 }
168
169 void *DH_get_ex_data(DH *d, int idx)
170 {
171     return CRYPTO_get_ex_data(&d->ex_data, idx);
172 }
173 #endif
174
175 int DH_bits(const DH *dh)
176 {
177     return BN_num_bits(dh->params.p);
178 }
179
180 int DH_size(const DH *dh)
181 {
182     return BN_num_bytes(dh->params.p);
183 }
184
185 int DH_security_bits(const DH *dh)
186 {
187     int N;
188     if (dh->params.q != NULL)
189         N = BN_num_bits(dh->params.q);
190     else if (dh->length)
191         N = dh->length;
192     else
193         N = -1;
194     return BN_security_bits(BN_num_bits(dh->params.p), N);
195 }
196
197 void DH_get0_pqg(const DH *dh,
198                  const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
199 {
200     ffc_params_get0_pqg(&dh->params, p, q, g);
201 }
202
203 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
204 {
205     /* If the fields p and g in d are NULL, the corresponding input
206      * parameters MUST be non-NULL.  q may remain NULL.
207      */
208     if ((dh->params.p == NULL && p == NULL)
209         || (dh->params.g == NULL && g == NULL))
210         return 0;
211
212     ffc_params_set0_pqg(&dh->params, p, q, g);
213     dh->params.nid = NID_undef;
214     /*
215      * Check if this is a named group. If it finds a named group then the
216      * 'q' and 'length' value are either already set or are set by the
217      * call.
218      */
219     if (DH_get_nid(dh) == NID_undef) {
220         /* If its not a named group then set the 'length' if q is not NULL */
221         if (q != NULL)
222             dh->length = BN_num_bits(q);
223     }
224     dh->dirty_cnt++;
225     return 1;
226 }
227
228 long DH_get_length(const DH *dh)
229 {
230     return dh->length;
231 }
232
233 int DH_set_length(DH *dh, long length)
234 {
235     dh->length = length;
236     return 1;
237 }
238
239 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
240 {
241     if (pub_key != NULL)
242         *pub_key = dh->pub_key;
243     if (priv_key != NULL)
244         *priv_key = dh->priv_key;
245 }
246
247 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
248 {
249     if (pub_key != NULL) {
250         BN_clear_free(dh->pub_key);
251         dh->pub_key = pub_key;
252     }
253     if (priv_key != NULL) {
254         BN_clear_free(dh->priv_key);
255         dh->priv_key = priv_key;
256     }
257
258     dh->dirty_cnt++;
259     return 1;
260 }
261
262 const BIGNUM *DH_get0_p(const DH *dh)
263 {
264     return dh->params.p;
265 }
266
267 const BIGNUM *DH_get0_q(const DH *dh)
268 {
269     return dh->params.q;
270 }
271
272 const BIGNUM *DH_get0_g(const DH *dh)
273 {
274     return dh->params.g;
275 }
276
277 const BIGNUM *DH_get0_priv_key(const DH *dh)
278 {
279     return dh->priv_key;
280 }
281
282 const BIGNUM *DH_get0_pub_key(const DH *dh)
283 {
284     return dh->pub_key;
285 }
286
287 void DH_clear_flags(DH *dh, int flags)
288 {
289     dh->flags &= ~flags;
290 }
291
292 int DH_test_flags(const DH *dh, int flags)
293 {
294     return dh->flags & flags;
295 }
296
297 void DH_set_flags(DH *dh, int flags)
298 {
299     dh->flags |= flags;
300 }
301
302 #ifndef FIPS_MODE
303 ENGINE *DH_get0_engine(DH *dh)
304 {
305     return dh->engine;
306 }
307 #endif /*FIPS_MODE */
308
309 FFC_PARAMS *dh_get0_params(DH *dh)
310 {
311     return &dh->params;
312 }
313 int dh_get0_nid(const DH *dh)
314 {
315     return dh->params.nid;
316 }