]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dsa/dsa_lib.c
Copyright consolidation 07/10
[thirdparty/openssl.git] / crypto / dsa / dsa_lib.c
CommitLineData
d2e9e320
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
d2e9e320
RS
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
d02b48c6
RE
8 */
9
651d0aff 10/* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
d02b48c6
RE
11
12#include <stdio.h>
b39fc560 13#include "internal/cryptlib.h"
ec577822 14#include <openssl/bn.h>
1258396d 15#include "dsa_locl.h"
ec577822 16#include <openssl/asn1.h>
3c27208f
RS
17#include <openssl/engine.h>
18#include <openssl/dh.h>
d02b48c6 19
a75b1915 20static const DSA_METHOD *default_DSA_method = NULL;
c0711f7f 21
cb78486d 22void DSA_set_default_method(const DSA_METHOD *meth)
0f113f3e
MC
23{
24 default_DSA_method = meth;
25}
c0711f7f 26
cb78486d 27const DSA_METHOD *DSA_get_default_method(void)
0f113f3e
MC
28{
29 if (!default_DSA_method)
30 default_DSA_method = DSA_OpenSSL();
31 return default_DSA_method;
32}
c0711f7f 33
6b691a5c 34DSA *DSA_new(void)
0f113f3e
MC
35{
36 return DSA_new_method(NULL);
37}
c0711f7f 38
cb78486d 39int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
0f113f3e
MC
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);
0b13e9f0 49#ifndef OPENSSL_NO_ENGINE
7c96dbcd
RS
50 ENGINE_finish(dsa->engine);
51 dsa->engine = NULL;
0b13e9f0 52#endif
0f113f3e
MC
53 dsa->meth = meth;
54 if (meth->init)
55 meth->init(dsa);
56 return 1;
57}
c0711f7f 58
6e9fa57c
MC
59const DSA_METHOD *DSA_get_method(DSA *d)
60{
61 return d->meth;
62}
63
5270e702 64DSA *DSA_new_method(ENGINE *engine)
0f113f3e 65{
2bbf0baa 66 DSA *ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e 67
0f113f3e
MC
68 if (ret == NULL) {
69 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
d188a536 70 return NULL;
0f113f3e 71 }
2bbf0baa
F
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
0f113f3e 80 ret->meth = DSA_get_default_method();
0b13e9f0 81#ifndef OPENSSL_NO_ENGINE
2bbf0baa 82 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
0f113f3e
MC
83 if (engine) {
84 if (!ENGINE_init(engine)) {
85 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
2bbf0baa 86 goto err;
0f113f3e
MC
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);
7c96dbcd 93 if (ret->meth == NULL) {
0f113f3e 94 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
2bbf0baa 95 goto err;
0f113f3e
MC
96 }
97 }
0b13e9f0 98#endif
0c9de428 99
0f113f3e 100 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
d188a536 101
2bbf0baa
F
102 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
103 goto err;
d188a536
AG
104
105 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
2bbf0baa
F
106 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL);
107err:
d188a536 108 DSA_free(ret);
0f113f3e
MC
109 ret = NULL;
110 }
111
d188a536 112 return ret;
0f113f3e 113}
d02b48c6 114
6b691a5c 115void DSA_free(DSA *r)
0f113f3e
MC
116{
117 int i;
d02b48c6 118
0f113f3e
MC
119 if (r == NULL)
120 return;
d02b48c6 121
d188a536 122 CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
f3f1cf84 123 REF_PRINT_COUNT("DSA", r);
0f113f3e
MC
124 if (i > 0)
125 return;
f3f1cf84 126 REF_ASSERT_ISNT(i < 0);
d02b48c6 127
0f113f3e
MC
128 if (r->meth->finish)
129 r->meth->finish(r);
0b13e9f0 130#ifndef OPENSSL_NO_ENGINE
17fa4e8e 131 ENGINE_finish(r->engine);
0b13e9f0 132#endif
c0711f7f 133
0f113f3e
MC
134 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
135
d188a536
AG
136 CRYPTO_THREAD_lock_free(r->lock);
137
23a1d5e9
RS
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);
0f113f3e
MC
143 OPENSSL_free(r);
144}
d02b48c6 145
6ac4e8bd 146int DSA_up_ref(DSA *r)
0f113f3e 147{
d188a536
AG
148 int i;
149
150 if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
151 return 0;
f3f1cf84
RS
152
153 REF_PRINT_COUNT("DSA", r);
154 REF_ASSERT_ISNT(i < 2);
0f113f3e
MC
155 return ((i > 1) ? 1 : 0);
156}
5cbc2e8b 157
a4aba800 158int DSA_size(const DSA *r)
0f113f3e
MC
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}
d02b48c6 179
dd9d233e 180int DSA_set_ex_data(DSA *d, int idx, void *arg)
0f113f3e
MC
181{
182 return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
183}
c0711f7f 184
dd9d233e 185void *DSA_get_ex_data(DSA *d, int idx)
0f113f3e
MC
186{
187 return (CRYPTO_get_ex_data(&d->ex_data, idx));
188}
c0711f7f 189
2514fa79 190int DSA_security_bits(const DSA *d)
0f113f3e 191{
72245f34
DSH
192 if (d->p && d->q)
193 return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q));
194 return -1;
0f113f3e 195}
2514fa79 196
cf1b7d96 197#ifndef OPENSSL_NO_DH
a4aba800 198DH *DSA_dup_DH(const DSA *r)
0f113f3e
MC
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;
0aeddcfa 206 BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL;
0f113f3e
MC
207
208 if (r == NULL)
209 goto err;
210 ret = DH_new();
211 if (ret == NULL)
212 goto err;
0aeddcfa
MC
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 */
0f113f3e 216 goto err;
0aeddcfa
MC
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))
0f113f3e 222 goto err;
998f2cb8 223 p = g = q = NULL;
0f113f3e 224 }
0aeddcfa
MC
225
226 if (r->pub_key != NULL) {
227 pub_key = BN_dup(r->pub_key);
228 if (pub_key == NULL)
0f113f3e 229 goto err;
0aeddcfa
MC
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))
0f113f3e 236 goto err;
0aeddcfa
MC
237 } else if (r->priv_key != NULL) {
238 /* Shouldn't happen */
239 goto err;
240 }
0f113f3e
MC
241
242 return ret;
48c843c3
BM
243
244 err:
0aeddcfa
MC
245 BN_free(p);
246 BN_free(g);
247 BN_free(q);
248 BN_free(pub_key);
249 BN_free(priv_key);
d6407083 250 DH_free(ret);
0f113f3e
MC
251 return NULL;
252}
48c843c3 253#endif
1258396d 254
6e9fa57c 255void DSA_get0_pqg(const DSA *d, BIGNUM **p, BIGNUM **q, BIGNUM **g)
1258396d 256{
6e9fa57c
MC
257 if (p != NULL)
258 *p = d->p;
259 if (q != NULL)
260 *q = d->q;
261 if (g != NULL)
262 *g = d->g;
1258396d
MC
263}
264
265int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
266{
1da12e34
RL
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)
1258396d 274 return 0;
1da12e34
RL
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 }
1258396d
MC
288
289 return 1;
290}
291
6e9fa57c 292void DSA_get0_key(const DSA *d, BIGNUM **pub_key, BIGNUM **priv_key)
1258396d 293{
6e9fa57c
MC
294 if (pub_key != NULL)
295 *pub_key = d->pub_key;
296 if (priv_key != NULL)
297 *priv_key = d->priv_key;
1258396d
MC
298}
299
6e9fa57c 300int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
1258396d 301{
1da12e34
RL
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
4a397f51 310 || (d->priv_key != NULL && priv_key == d->priv_key))
1258396d
MC
311 return 0;
312
1da12e34
RL
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 }
1258396d
MC
321
322 return 1;
323}
324
325void DSA_clear_flags(DSA *d, int flags)
326{
327 d->flags &= ~flags;
328}
329
330int DSA_test_flags(const DSA *d, int flags)
331{
332 return d->flags & flags;
333}
334
335void DSA_set_flags(DSA *d, int flags)
336{
337 d->flags |= flags;
338}
339
340ENGINE *DSA_get0_engine(DSA *d)
341{
342 return d->engine;
343}