Useless header include of openssl/rand.h
[openssl.git] / crypto / dsa / dsa_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 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
11
12 #include <stdio.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/bn.h>
15 #include "dsa_locl.h"
16 #include <openssl/asn1.h>
17 #include <openssl/engine.h>
18 #include <openssl/dh.h>
19
20 static const DSA_METHOD *default_DSA_method = NULL;
21
22 void DSA_set_default_method(const DSA_METHOD *meth)
23 {
24     default_DSA_method = meth;
25 }
26
27 const DSA_METHOD *DSA_get_default_method(void)
28 {
29     if (!default_DSA_method)
30         default_DSA_method = DSA_OpenSSL();
31     return default_DSA_method;
32 }
33
34 DSA *DSA_new(void)
35 {
36     return DSA_new_method(NULL);
37 }
38
39 int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
40 {
41     /*
42      * NB: The caller is specifically setting a method, so it's not up to us
43      * to deal with which ENGINE it comes from.
44      */
45     const DSA_METHOD *mtmp;
46     mtmp = dsa->meth;
47     if (mtmp->finish)
48         mtmp->finish(dsa);
49 #ifndef OPENSSL_NO_ENGINE
50     ENGINE_finish(dsa->engine);
51     dsa->engine = NULL;
52 #endif
53     dsa->meth = meth;
54     if (meth->init)
55         meth->init(dsa);
56     return 1;
57 }
58
59 const DSA_METHOD *DSA_get_method(DSA *d)
60 {
61     return d->meth;
62 }
63
64 DSA *DSA_new_method(ENGINE *engine)
65 {
66     DSA *ret = OPENSSL_zalloc(sizeof(*ret));
67
68     if (ret == NULL) {
69         DSAerr(DSA_F_DSA_NEW_METHOD, 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         DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
77         OPENSSL_free(ret);
78         return NULL;
79     }
80
81     ret->meth = DSA_get_default_method();
82 #ifndef OPENSSL_NO_ENGINE
83     ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
84     if (engine) {
85         if (!ENGINE_init(engine)) {
86             DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
87             goto err;
88         }
89         ret->engine = engine;
90     } else
91         ret->engine = ENGINE_get_default_DSA();
92     if (ret->engine) {
93         ret->meth = ENGINE_get_DSA(ret->engine);
94         if (ret->meth == NULL) {
95             DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
96             goto err;
97         }
98     }
99 #endif
100
101     ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
102
103     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
104         goto err;
105
106     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
107         DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL);
108 err:
109         DSA_free(ret);
110         ret = NULL;
111     }
112
113     return ret;
114 }
115
116 void DSA_free(DSA *r)
117 {
118     int i;
119
120     if (r == NULL)
121         return;
122
123     CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
124     REF_PRINT_COUNT("DSA", r);
125     if (i > 0)
126         return;
127     REF_ASSERT_ISNT(i < 0);
128
129     if (r->meth->finish)
130         r->meth->finish(r);
131 #ifndef OPENSSL_NO_ENGINE
132     ENGINE_finish(r->engine);
133 #endif
134
135     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
136
137     CRYPTO_THREAD_lock_free(r->lock);
138
139     BN_clear_free(r->p);
140     BN_clear_free(r->q);
141     BN_clear_free(r->g);
142     BN_clear_free(r->pub_key);
143     BN_clear_free(r->priv_key);
144     OPENSSL_free(r);
145 }
146
147 int DSA_up_ref(DSA *r)
148 {
149     int i;
150
151     if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
152         return 0;
153
154     REF_PRINT_COUNT("DSA", r);
155     REF_ASSERT_ISNT(i < 2);
156     return ((i > 1) ? 1 : 0);
157 }
158
159 int DSA_size(const DSA *r)
160 {
161     int ret, i;
162     ASN1_INTEGER bs;
163     unsigned char buf[4];       /* 4 bytes looks really small. However,
164                                  * i2d_ASN1_INTEGER() will not look beyond
165                                  * the first byte, as long as the second
166                                  * parameter is NULL. */
167
168     i = BN_num_bits(r->q);
169     bs.length = (i + 7) / 8;
170     bs.data = buf;
171     bs.type = V_ASN1_INTEGER;
172     /* If the top bit is set the asn1 encoding is 1 larger. */
173     buf[0] = 0xff;
174
175     i = i2d_ASN1_INTEGER(&bs, NULL);
176     i += i;                     /* r and s */
177     ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
178     return (ret);
179 }
180
181 int DSA_set_ex_data(DSA *d, int idx, void *arg)
182 {
183     return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
184 }
185
186 void *DSA_get_ex_data(DSA *d, int idx)
187 {
188     return (CRYPTO_get_ex_data(&d->ex_data, idx));
189 }
190
191 int DSA_security_bits(const DSA *d)
192 {
193     if (d->p && d->q)
194         return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q));
195     return -1;
196 }
197
198 #ifndef OPENSSL_NO_DH
199 DH *DSA_dup_DH(const DSA *r)
200 {
201     /*
202      * DSA has p, q, g, optional pub_key, optional priv_key. DH has p,
203      * optional length, g, optional pub_key, optional priv_key, optional q.
204      */
205
206     DH *ret = NULL;
207     BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL;
208
209     if (r == NULL)
210         goto err;
211     ret = DH_new();
212     if (ret == NULL)
213         goto err;
214     if (r->p != NULL || r->g != NULL || r->q != NULL) {
215         if (r->p == NULL || r->g == NULL || r->q == NULL) {
216             /* Shouldn't happen */
217             goto err;
218         }
219         p = BN_dup(r->p);
220         g = BN_dup(r->g);
221         q = BN_dup(r->q);
222         if (p == NULL || g == NULL || q == NULL || !DH_set0_pqg(ret, p, q, g))
223             goto err;
224         p = g = q = NULL;
225     }
226
227     if (r->pub_key != NULL) {
228         pub_key = BN_dup(r->pub_key);
229         if (pub_key == NULL)
230             goto err;
231         if (r->priv_key != NULL) {
232             priv_key = BN_dup(r->priv_key);
233             if (priv_key == NULL)
234                 goto err;
235         }
236         if (!DH_set0_key(ret, pub_key, priv_key))
237             goto err;
238     } else if (r->priv_key != NULL) {
239         /* Shouldn't happen */
240         goto err;
241     }
242
243     return ret;
244
245  err:
246     BN_free(p);
247     BN_free(g);
248     BN_free(q);
249     BN_free(pub_key);
250     BN_free(priv_key);
251     DH_free(ret);
252     return NULL;
253 }
254 #endif
255
256 void DSA_get0_pqg(const DSA *d,
257                   const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
258 {
259     if (p != NULL)
260         *p = d->p;
261     if (q != NULL)
262         *q = d->q;
263     if (g != NULL)
264         *g = d->g;
265 }
266
267 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
268 {
269     /* If the fields p, q and g in d are NULL, the corresponding input
270      * parameters MUST be non-NULL.
271      */
272     if ((d->p == NULL && p == NULL)
273         || (d->q == NULL && q == NULL)
274         || (d->g == NULL && g == NULL))
275         return 0;
276
277     if (p != NULL) {
278         BN_free(d->p);
279         d->p = p;
280     }
281     if (q != NULL) {
282         BN_free(d->q);
283         d->q = q;
284     }
285     if (g != NULL) {
286         BN_free(d->g);
287         d->g = g;
288     }
289
290     return 1;
291 }
292
293 void DSA_get0_key(const DSA *d,
294                   const BIGNUM **pub_key, const BIGNUM **priv_key)
295 {
296     if (pub_key != NULL)
297         *pub_key = d->pub_key;
298     if (priv_key != NULL)
299         *priv_key = d->priv_key;
300 }
301
302 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
303 {
304     /* If the field pub_key in d is NULL, the corresponding input
305      * parameters MUST be non-NULL.  The priv_key field may
306      * be left NULL.
307      */
308     if (d->pub_key == NULL && pub_key == NULL)
309         return 0;
310
311     if (pub_key != NULL) {
312         BN_free(d->pub_key);
313         d->pub_key = pub_key;
314     }
315     if (priv_key != NULL) {
316         BN_free(d->priv_key);
317         d->priv_key = priv_key;
318     }
319
320     return 1;
321 }
322
323 void DSA_clear_flags(DSA *d, int flags)
324 {
325     d->flags &= ~flags;
326 }
327
328 int DSA_test_flags(const DSA *d, int flags)
329 {
330     return d->flags & flags;
331 }
332
333 void DSA_set_flags(DSA *d, int flags)
334 {
335     d->flags |= flags;
336 }
337
338 ENGINE *DSA_get0_engine(DSA *d)
339 {
340     return d->engine;
341 }