Make sure crypto-mdebug-backtrace must be enabled explicitely
[openssl.git] / crypto / dh / dh_lib.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
13 #include "dh_locl.h"
14 #include <openssl/engine.h>
15
16 static const DH_METHOD *default_DH_method = NULL;
17
18 void DH_set_default_method(const DH_METHOD *meth)
19 {
20     default_DH_method = meth;
21 }
22
23 const DH_METHOD *DH_get_default_method(void)
24 {
25     if (!default_DH_method)
26         default_DH_method = DH_OpenSSL();
27     return default_DH_method;
28 }
29
30 int DH_set_method(DH *dh, const DH_METHOD *meth)
31 {
32     /*
33      * NB: The caller is specifically setting a method, so it's not up to us
34      * to deal with which ENGINE it comes from.
35      */
36     const DH_METHOD *mtmp;
37     mtmp = dh->meth;
38     if (mtmp->finish)
39         mtmp->finish(dh);
40 #ifndef OPENSSL_NO_ENGINE
41     ENGINE_finish(dh->engine);
42     dh->engine = NULL;
43 #endif
44     dh->meth = meth;
45     if (meth->init)
46         meth->init(dh);
47     return 1;
48 }
49
50 DH *DH_new(void)
51 {
52     return DH_new_method(NULL);
53 }
54
55 DH *DH_new_method(ENGINE *engine)
56 {
57     DH *ret = OPENSSL_zalloc(sizeof(*ret));
58
59     if (ret == NULL) {
60         DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
61         return NULL;
62     }
63
64     ret->references = 1;
65     ret->lock = CRYPTO_THREAD_lock_new();
66     if (ret->lock == NULL) {
67         OPENSSL_free(ret);
68         return NULL;
69     }
70
71     ret->meth = DH_get_default_method();
72 #ifndef OPENSSL_NO_ENGINE
73     ret->flags = ret->meth->flags;  /* early default init */
74     if (engine) {
75         if (!ENGINE_init(engine)) {
76             DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
77             goto err;
78         }
79         ret->engine = engine;
80     } else
81         ret->engine = ENGINE_get_default_DH();
82     if (ret->engine) {
83         ret->meth = ENGINE_get_DH(ret->engine);
84         if (ret->meth == NULL) {
85             DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
86             goto err;
87         }
88     }
89 #endif
90
91     ret->flags = ret->meth->flags;
92
93     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
94         goto err;
95
96     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
97         DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
98 err:
99         DH_free(ret);
100         ret = NULL;
101     }
102
103     return ret;
104 }
105
106 void DH_free(DH *r)
107 {
108     int i;
109
110     if (r == NULL)
111         return;
112
113     CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
114     REF_PRINT_COUNT("DH", r);
115     if (i > 0)
116         return;
117     REF_ASSERT_ISNT(i < 0);
118
119     if (r->meth->finish)
120         r->meth->finish(r);
121 #ifndef OPENSSL_NO_ENGINE
122     ENGINE_finish(r->engine);
123 #endif
124
125     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
126
127     CRYPTO_THREAD_lock_free(r->lock);
128
129     BN_clear_free(r->p);
130     BN_clear_free(r->g);
131     BN_clear_free(r->q);
132     BN_clear_free(r->j);
133     OPENSSL_free(r->seed);
134     BN_clear_free(r->counter);
135     BN_clear_free(r->pub_key);
136     BN_clear_free(r->priv_key);
137     OPENSSL_free(r);
138 }
139
140 int DH_up_ref(DH *r)
141 {
142     int i;
143
144     if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
145         return 0;
146
147     REF_PRINT_COUNT("DH", r);
148     REF_ASSERT_ISNT(i < 2);
149     return ((i > 1) ? 1 : 0);
150 }
151
152 int DH_set_ex_data(DH *d, int idx, void *arg)
153 {
154     return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
155 }
156
157 void *DH_get_ex_data(DH *d, int idx)
158 {
159     return (CRYPTO_get_ex_data(&d->ex_data, idx));
160 }
161
162 int DH_bits(const DH *dh)
163 {
164     return BN_num_bits(dh->p);
165 }
166
167 int DH_size(const DH *dh)
168 {
169     return (BN_num_bytes(dh->p));
170 }
171
172 int DH_security_bits(const DH *dh)
173 {
174     int N;
175     if (dh->q)
176         N = BN_num_bits(dh->q);
177     else if (dh->length)
178         N = dh->length;
179     else
180         N = -1;
181     return BN_security_bits(BN_num_bits(dh->p), N);
182 }
183
184
185 void DH_get0_pqg(const DH *dh, BIGNUM **p, BIGNUM **q, BIGNUM **g)
186 {
187     if (p != NULL)
188         *p = dh->p;
189     if (q != NULL)
190         *q = dh->q;
191     if (g != NULL)
192         *g = dh->g;
193 }
194
195 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
196 {
197     /* If the fields p and g in d are NULL, the corresponding input
198      * parameters MUST be non-NULL.  q may remain NULL.
199      *
200      * It is an error to give the results from get0 on d
201      * as input parameters.
202      */
203     if (p == dh->p || (dh->q != NULL && q == dh->q) || g == dh->g)
204         return 0;
205
206     if (p != NULL) {
207         BN_free(dh->p);
208         dh->p = p;
209     }
210     if (q != NULL) {
211         BN_free(dh->q);
212         dh->q = q;
213     }
214     if (g != NULL) {
215         BN_free(dh->g);
216         dh->g = g;
217     }
218
219     if (q != NULL) {
220         dh->length = BN_num_bits(q);
221     }
222
223     return 1;
224 }
225
226 long DH_get_length(const DH *dh)
227 {
228     return dh->length;
229 }
230
231 int DH_set_length(DH *dh, long length)
232 {
233     dh->length = length;
234     return 1;
235 }
236
237 void DH_get0_key(const DH *dh, BIGNUM **pub_key, BIGNUM **priv_key)
238 {
239     if (pub_key != NULL)
240         *pub_key = dh->pub_key;
241     if (priv_key != NULL)
242         *priv_key = dh->priv_key;
243 }
244
245 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
246 {
247     /* If the pub_key in dh is NULL, the corresponding input
248      * parameters MUST be non-NULL.  The priv_key field may
249      * be left NULL.
250      *
251      * It is an error to give the results from get0 on dh
252      * as input parameters.
253      */
254     if (dh->pub_key == pub_key
255         || (dh->priv_key != NULL && priv_key == dh->priv_key))
256         return 0;
257
258     if (pub_key != NULL) {
259         BN_free(dh->pub_key);
260         dh->pub_key = pub_key;
261     }
262     if (priv_key != NULL) {
263         BN_free(dh->priv_key);
264         dh->priv_key = priv_key;
265     }
266
267     return 1;
268 }
269
270 void DH_clear_flags(DH *dh, int flags)
271 {
272     dh->flags &= ~flags;
273 }
274
275 int DH_test_flags(const DH *dh, int flags)
276 {
277     return dh->flags & flags;
278 }
279
280 void DH_set_flags(DH *dh, int flags)
281 {
282     dh->flags |= flags;
283 }
284
285 ENGINE *DH_get0_engine(DH *dh)
286 {
287     return dh->engine;
288 }