Raise an Err when CRYPTO_THREAD_lock_new fails
[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         DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
68         OPENSSL_free(ret);
69         return NULL;
70     }
71
72     ret->meth = DH_get_default_method();
73 #ifndef OPENSSL_NO_ENGINE
74     ret->flags = ret->meth->flags;  /* early default init */
75     if (engine) {
76         if (!ENGINE_init(engine)) {
77             DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
78             goto err;
79         }
80         ret->engine = engine;
81     } else
82         ret->engine = ENGINE_get_default_DH();
83     if (ret->engine) {
84         ret->meth = ENGINE_get_DH(ret->engine);
85         if (ret->meth == NULL) {
86             DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
87             goto err;
88         }
89     }
90 #endif
91
92     ret->flags = ret->meth->flags;
93
94     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
95         goto err;
96
97     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
98         DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
99 err:
100         DH_free(ret);
101         ret = NULL;
102     }
103
104     return ret;
105 }
106
107 void DH_free(DH *r)
108 {
109     int i;
110
111     if (r == NULL)
112         return;
113
114     CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
115     REF_PRINT_COUNT("DH", r);
116     if (i > 0)
117         return;
118     REF_ASSERT_ISNT(i < 0);
119
120     if (r->meth->finish)
121         r->meth->finish(r);
122 #ifndef OPENSSL_NO_ENGINE
123     ENGINE_finish(r->engine);
124 #endif
125
126     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
127
128     CRYPTO_THREAD_lock_free(r->lock);
129
130     BN_clear_free(r->p);
131     BN_clear_free(r->g);
132     BN_clear_free(r->q);
133     BN_clear_free(r->j);
134     OPENSSL_free(r->seed);
135     BN_clear_free(r->counter);
136     BN_clear_free(r->pub_key);
137     BN_clear_free(r->priv_key);
138     OPENSSL_free(r);
139 }
140
141 int DH_up_ref(DH *r)
142 {
143     int i;
144
145     if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
146         return 0;
147
148     REF_PRINT_COUNT("DH", r);
149     REF_ASSERT_ISNT(i < 2);
150     return ((i > 1) ? 1 : 0);
151 }
152
153 int DH_set_ex_data(DH *d, int idx, void *arg)
154 {
155     return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
156 }
157
158 void *DH_get_ex_data(DH *d, int idx)
159 {
160     return (CRYPTO_get_ex_data(&d->ex_data, idx));
161 }
162
163 int DH_bits(const DH *dh)
164 {
165     return BN_num_bits(dh->p);
166 }
167
168 int DH_size(const DH *dh)
169 {
170     return (BN_num_bytes(dh->p));
171 }
172
173 int DH_security_bits(const DH *dh)
174 {
175     int N;
176     if (dh->q)
177         N = BN_num_bits(dh->q);
178     else if (dh->length)
179         N = dh->length;
180     else
181         N = -1;
182     return BN_security_bits(BN_num_bits(dh->p), N);
183 }
184
185
186 void DH_get0_pqg(const DH *dh, BIGNUM **p, BIGNUM **q, BIGNUM **g)
187 {
188     if (p != NULL)
189         *p = dh->p;
190     if (q != NULL)
191         *q = dh->q;
192     if (g != NULL)
193         *g = dh->g;
194 }
195
196 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
197 {
198     /* If the fields p and g in d are NULL, the corresponding input
199      * parameters MUST be non-NULL.  q may remain NULL.
200      *
201      * It is an error to give the results from get0 on d
202      * as input parameters.
203      */
204     if (p == dh->p || (dh->q != NULL && q == dh->q) || g == dh->g)
205         return 0;
206
207     if (p != NULL) {
208         BN_free(dh->p);
209         dh->p = p;
210     }
211     if (q != NULL) {
212         BN_free(dh->q);
213         dh->q = q;
214     }
215     if (g != NULL) {
216         BN_free(dh->g);
217         dh->g = g;
218     }
219
220     if (q != NULL) {
221         dh->length = BN_num_bits(q);
222     }
223
224     return 1;
225 }
226
227 long DH_get_length(const DH *dh)
228 {
229     return dh->length;
230 }
231
232 int DH_set_length(DH *dh, long length)
233 {
234     dh->length = length;
235     return 1;
236 }
237
238 void DH_get0_key(const DH *dh, BIGNUM **pub_key, BIGNUM **priv_key)
239 {
240     if (pub_key != NULL)
241         *pub_key = dh->pub_key;
242     if (priv_key != NULL)
243         *priv_key = dh->priv_key;
244 }
245
246 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
247 {
248     /* If the pub_key in dh is NULL, the corresponding input
249      * parameters MUST be non-NULL.  The priv_key field may
250      * be left NULL.
251      *
252      * It is an error to give the results from get0 on dh
253      * as input parameters.
254      */
255     if (dh->pub_key == pub_key
256         || (dh->priv_key != NULL && priv_key == dh->priv_key))
257         return 0;
258
259     if (pub_key != NULL) {
260         BN_free(dh->pub_key);
261         dh->pub_key = pub_key;
262     }
263     if (priv_key != NULL) {
264         BN_free(dh->priv_key);
265         dh->priv_key = priv_key;
266     }
267
268     return 1;
269 }
270
271 void DH_clear_flags(DH *dh, int flags)
272 {
273     dh->flags &= ~flags;
274 }
275
276 int DH_test_flags(const DH *dh, int flags)
277 {
278     return dh->flags & flags;
279 }
280
281 void DH_set_flags(DH *dh, int flags)
282 {
283     dh->flags |= flags;
284 }
285
286 ENGINE *DH_get0_engine(DH *dh)
287 {
288     return dh->engine;
289 }