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