Fix: PEM_read_bio_PrivateKey with no-ui / no-stdio
[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         OPENSSL_free(ret);
77         return NULL;
78     }
79
80     ret->meth = DSA_get_default_method();
81 #ifndef OPENSSL_NO_ENGINE
82     ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
83     if (engine) {
84         if (!ENGINE_init(engine)) {
85             DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
86             goto err;
87         }
88         ret->engine = engine;
89     } else
90         ret->engine = ENGINE_get_default_DSA();
91     if (ret->engine) {
92         ret->meth = ENGINE_get_DSA(ret->engine);
93         if (ret->meth == NULL) {
94             DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
95             goto err;
96         }
97     }
98 #endif
99
100     ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
101
102     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
103         goto err;
104
105     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
106         DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL);
107 err:
108         DSA_free(ret);
109         ret = NULL;
110     }
111
112     return ret;
113 }
114
115 void DSA_free(DSA *r)
116 {
117     int i;
118
119     if (r == NULL)
120         return;
121
122     CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
123     REF_PRINT_COUNT("DSA", r);
124     if (i > 0)
125         return;
126     REF_ASSERT_ISNT(i < 0);
127
128     if (r->meth->finish)
129         r->meth->finish(r);
130 #ifndef OPENSSL_NO_ENGINE
131     ENGINE_finish(r->engine);
132 #endif
133
134     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
135
136     CRYPTO_THREAD_lock_free(r->lock);
137
138     BN_clear_free(r->p);
139     BN_clear_free(r->q);
140     BN_clear_free(r->g);
141     BN_clear_free(r->pub_key);
142     BN_clear_free(r->priv_key);
143     OPENSSL_free(r);
144 }
145
146 int DSA_up_ref(DSA *r)
147 {
148     int i;
149
150     if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
151         return 0;
152
153     REF_PRINT_COUNT("DSA", r);
154     REF_ASSERT_ISNT(i < 2);
155     return ((i > 1) ? 1 : 0);
156 }
157
158 int DSA_size(const DSA *r)
159 {
160     int ret, i;
161     ASN1_INTEGER bs;
162     unsigned char buf[4];       /* 4 bytes looks really small. However,
163                                  * i2d_ASN1_INTEGER() will not look beyond
164                                  * the first byte, as long as the second
165                                  * parameter is NULL. */
166
167     i = BN_num_bits(r->q);
168     bs.length = (i + 7) / 8;
169     bs.data = buf;
170     bs.type = V_ASN1_INTEGER;
171     /* If the top bit is set the asn1 encoding is 1 larger. */
172     buf[0] = 0xff;
173
174     i = i2d_ASN1_INTEGER(&bs, NULL);
175     i += i;                     /* r and s */
176     ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
177     return (ret);
178 }
179
180 int DSA_set_ex_data(DSA *d, int idx, void *arg)
181 {
182     return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
183 }
184
185 void *DSA_get_ex_data(DSA *d, int idx)
186 {
187     return (CRYPTO_get_ex_data(&d->ex_data, idx));
188 }
189
190 int DSA_security_bits(const DSA *d)
191 {
192     if (d->p && d->q)
193         return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q));
194     return -1;
195 }
196
197 #ifndef OPENSSL_NO_DH
198 DH *DSA_dup_DH(const DSA *r)
199 {
200     /*
201      * DSA has p, q, g, optional pub_key, optional priv_key. DH has p,
202      * optional length, g, optional pub_key, optional priv_key, optional q.
203      */
204
205     DH *ret = NULL;
206     BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL;
207
208     if (r == NULL)
209         goto err;
210     ret = DH_new();
211     if (ret == NULL)
212         goto err;
213     if (r->p != NULL || r->g != NULL || r->q != NULL) {
214         if (r->p == NULL || r->g == NULL || r->q == NULL) {
215             /* Shouldn't happen */
216             goto err;
217         }
218         p = BN_dup(r->p);
219         g = BN_dup(r->g);
220         q = BN_dup(r->q);
221         if (p == NULL || g == NULL || q == NULL || !DH_set0_pqg(ret, p, q, g))
222             goto err;
223         p = g = q = NULL;
224     }
225
226     if (r->pub_key != NULL) {
227         pub_key = BN_dup(r->pub_key);
228         if (pub_key == NULL)
229             goto err;
230         if (r->priv_key != NULL) {
231             priv_key = BN_dup(r->priv_key);
232             if (priv_key == NULL)
233                 goto err;
234         }
235         if (!DH_set0_key(ret, pub_key, priv_key))
236             goto err;
237     } else if (r->priv_key != NULL) {
238         /* Shouldn't happen */
239         goto err;
240     }
241
242     return ret;
243
244  err:
245     BN_free(p);
246     BN_free(g);
247     BN_free(q);
248     BN_free(pub_key);
249     BN_free(priv_key);
250     DH_free(ret);
251     return NULL;
252 }
253 #endif
254
255 void DSA_get0_pqg(const DSA *d, BIGNUM **p, BIGNUM **q, BIGNUM **g)
256 {
257     if (p != NULL)
258         *p = d->p;
259     if (q != NULL)
260         *q = d->q;
261     if (g != NULL)
262         *g = d->g;
263 }
264
265 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
266 {
267     /* If the fields in d are NULL, the corresponding input
268      * parameters MUST be non-NULL.
269      *
270      * It is an error to give the results from get0 on d
271      * as input parameters.
272      */
273     if (p == d->p || q == d->q || g == d->g)
274         return 0;
275
276     if (p != NULL) {
277         BN_free(d->p);
278         d->p = p;
279     }
280     if (q != NULL) {
281         BN_free(d->q);
282         d->q = q;
283     }
284     if (g != NULL) {
285         BN_free(d->g);
286         d->g = g;
287     }
288
289     return 1;
290 }
291
292 void DSA_get0_key(const DSA *d, BIGNUM **pub_key, BIGNUM **priv_key)
293 {
294     if (pub_key != NULL)
295         *pub_key = d->pub_key;
296     if (priv_key != NULL)
297         *priv_key = d->priv_key;
298 }
299
300 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
301 {
302     /* If the pub_key in d is NULL, the corresponding input
303      * parameters MUST be non-NULL.  The priv_key field may
304      * be left NULL.
305      *
306      * It is an error to give the results from get0 on d
307      * as input parameters.
308      */
309     if (d->pub_key == pub_key
310         || (d->priv_key != NULL && priv_key == d->priv_key))
311         return 0;
312
313     if (pub_key != NULL) {
314         BN_free(d->pub_key);
315         d->pub_key = pub_key;
316     }
317     if (priv_key != NULL) {
318         BN_free(d->priv_key);
319         d->priv_key = priv_key;
320     }
321
322     return 1;
323 }
324
325 void DSA_clear_flags(DSA *d, int flags)
326 {
327     d->flags &= ~flags;
328 }
329
330 int DSA_test_flags(const DSA *d, int flags)
331 {
332     return d->flags & flags;
333 }
334
335 void DSA_set_flags(DSA *d, int flags)
336 {
337     d->flags |= flags;
338 }
339
340 ENGINE *DSA_get0_engine(DSA *d)
341 {
342     return d->engine;
343 }