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