]>
Commit | Line | Data |
---|---|---|
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 | 20 | static const DSA_METHOD *default_DSA_method = NULL; |
c0711f7f | 21 | |
cb78486d | 22 | void DSA_set_default_method(const DSA_METHOD *meth) |
0f113f3e MC |
23 | { |
24 | default_DSA_method = meth; | |
25 | } | |
c0711f7f | 26 | |
cb78486d | 27 | const 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 | 34 | DSA *DSA_new(void) |
0f113f3e MC |
35 | { |
36 | return DSA_new_method(NULL); | |
37 | } | |
c0711f7f | 38 | |
cb78486d | 39 | int 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 |
59 | const DSA_METHOD *DSA_get_method(DSA *d) |
60 | { | |
61 | return d->meth; | |
62 | } | |
63 | ||
5270e702 | 64 | DSA *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) { | |
b2b361f6 | 76 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); |
2bbf0baa F |
77 | OPENSSL_free(ret); |
78 | return NULL; | |
79 | } | |
80 | ||
0f113f3e | 81 | ret->meth = DSA_get_default_method(); |
0b13e9f0 | 82 | #ifndef OPENSSL_NO_ENGINE |
2bbf0baa | 83 | ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */ |
0f113f3e MC |
84 | if (engine) { |
85 | if (!ENGINE_init(engine)) { | |
86 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); | |
2bbf0baa | 87 | goto err; |
0f113f3e MC |
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); | |
7c96dbcd | 94 | if (ret->meth == NULL) { |
0f113f3e | 95 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); |
2bbf0baa | 96 | goto err; |
0f113f3e MC |
97 | } |
98 | } | |
0b13e9f0 | 99 | #endif |
0c9de428 | 100 | |
0f113f3e | 101 | ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; |
d188a536 | 102 | |
2bbf0baa F |
103 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data)) |
104 | goto err; | |
d188a536 AG |
105 | |
106 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { | |
2bbf0baa F |
107 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL); |
108 | err: | |
d188a536 | 109 | DSA_free(ret); |
0f113f3e MC |
110 | ret = NULL; |
111 | } | |
112 | ||
d188a536 | 113 | return ret; |
0f113f3e | 114 | } |
d02b48c6 | 115 | |
6b691a5c | 116 | void DSA_free(DSA *r) |
0f113f3e MC |
117 | { |
118 | int i; | |
d02b48c6 | 119 | |
0f113f3e MC |
120 | if (r == NULL) |
121 | return; | |
d02b48c6 | 122 | |
2f545ae4 | 123 | CRYPTO_DOWN_REF(&r->references, &i, r->lock); |
f3f1cf84 | 124 | REF_PRINT_COUNT("DSA", r); |
0f113f3e MC |
125 | if (i > 0) |
126 | return; | |
f3f1cf84 | 127 | REF_ASSERT_ISNT(i < 0); |
d02b48c6 | 128 | |
0f113f3e MC |
129 | if (r->meth->finish) |
130 | r->meth->finish(r); | |
0b13e9f0 | 131 | #ifndef OPENSSL_NO_ENGINE |
17fa4e8e | 132 | ENGINE_finish(r->engine); |
0b13e9f0 | 133 | #endif |
c0711f7f | 134 | |
0f113f3e MC |
135 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); |
136 | ||
d188a536 AG |
137 | CRYPTO_THREAD_lock_free(r->lock); |
138 | ||
23a1d5e9 RS |
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); | |
0f113f3e MC |
144 | OPENSSL_free(r); |
145 | } | |
d02b48c6 | 146 | |
6ac4e8bd | 147 | int DSA_up_ref(DSA *r) |
0f113f3e | 148 | { |
d188a536 AG |
149 | int i; |
150 | ||
2f545ae4 | 151 | if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0) |
d188a536 | 152 | return 0; |
f3f1cf84 RS |
153 | |
154 | REF_PRINT_COUNT("DSA", r); | |
155 | REF_ASSERT_ISNT(i < 2); | |
0f113f3e MC |
156 | return ((i > 1) ? 1 : 0); |
157 | } | |
5cbc2e8b | 158 | |
a4aba800 | 159 | int DSA_size(const DSA *r) |
0f113f3e MC |
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 | } | |
d02b48c6 | 180 | |
dd9d233e | 181 | int DSA_set_ex_data(DSA *d, int idx, void *arg) |
0f113f3e MC |
182 | { |
183 | return (CRYPTO_set_ex_data(&d->ex_data, idx, arg)); | |
184 | } | |
c0711f7f | 185 | |
dd9d233e | 186 | void *DSA_get_ex_data(DSA *d, int idx) |
0f113f3e MC |
187 | { |
188 | return (CRYPTO_get_ex_data(&d->ex_data, idx)); | |
189 | } | |
c0711f7f | 190 | |
2514fa79 | 191 | int DSA_security_bits(const DSA *d) |
0f113f3e | 192 | { |
72245f34 DSH |
193 | if (d->p && d->q) |
194 | return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q)); | |
195 | return -1; | |
0f113f3e | 196 | } |
2514fa79 | 197 | |
cf1b7d96 | 198 | #ifndef OPENSSL_NO_DH |
a4aba800 | 199 | DH *DSA_dup_DH(const DSA *r) |
0f113f3e MC |
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; | |
0aeddcfa | 207 | BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL; |
0f113f3e MC |
208 | |
209 | if (r == NULL) | |
210 | goto err; | |
211 | ret = DH_new(); | |
212 | if (ret == NULL) | |
213 | goto err; | |
0aeddcfa MC |
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 */ | |
0f113f3e | 217 | goto err; |
0aeddcfa MC |
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)) | |
0f113f3e | 223 | goto err; |
998f2cb8 | 224 | p = g = q = NULL; |
0f113f3e | 225 | } |
0aeddcfa MC |
226 | |
227 | if (r->pub_key != NULL) { | |
228 | pub_key = BN_dup(r->pub_key); | |
229 | if (pub_key == NULL) | |
0f113f3e | 230 | goto err; |
0aeddcfa MC |
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)) | |
0f113f3e | 237 | goto err; |
0aeddcfa MC |
238 | } else if (r->priv_key != NULL) { |
239 | /* Shouldn't happen */ | |
240 | goto err; | |
241 | } | |
0f113f3e MC |
242 | |
243 | return ret; | |
48c843c3 BM |
244 | |
245 | err: | |
0aeddcfa MC |
246 | BN_free(p); |
247 | BN_free(g); | |
248 | BN_free(q); | |
249 | BN_free(pub_key); | |
250 | BN_free(priv_key); | |
d6407083 | 251 | DH_free(ret); |
0f113f3e MC |
252 | return NULL; |
253 | } | |
48c843c3 | 254 | #endif |
1258396d | 255 | |
fd809cfd RL |
256 | void DSA_get0_pqg(const DSA *d, |
257 | const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) | |
1258396d | 258 | { |
6e9fa57c MC |
259 | if (p != NULL) |
260 | *p = d->p; | |
261 | if (q != NULL) | |
262 | *q = d->q; | |
263 | if (g != NULL) | |
264 | *g = d->g; | |
1258396d MC |
265 | } |
266 | ||
267 | int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) | |
268 | { | |
fd809cfd | 269 | /* If the fields p, q and g in d are NULL, the corresponding input |
1da12e34 | 270 | * parameters MUST be non-NULL. |
1da12e34 | 271 | */ |
b84e1226 MC |
272 | if ((d->p == NULL && p == NULL) |
273 | || (d->q == NULL && q == NULL) | |
274 | || (d->g == NULL && g == NULL)) | |
1258396d | 275 | return 0; |
1da12e34 RL |
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 | } | |
1258396d MC |
289 | |
290 | return 1; | |
291 | } | |
292 | ||
fd809cfd RL |
293 | void DSA_get0_key(const DSA *d, |
294 | const BIGNUM **pub_key, const BIGNUM **priv_key) | |
1258396d | 295 | { |
6e9fa57c MC |
296 | if (pub_key != NULL) |
297 | *pub_key = d->pub_key; | |
298 | if (priv_key != NULL) | |
299 | *priv_key = d->priv_key; | |
1258396d MC |
300 | } |
301 | ||
6e9fa57c | 302 | int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) |
1258396d | 303 | { |
fd809cfd | 304 | /* If the field pub_key in d is NULL, the corresponding input |
1da12e34 RL |
305 | * parameters MUST be non-NULL. The priv_key field may |
306 | * be left NULL. | |
1da12e34 | 307 | */ |
fd809cfd | 308 | if (d->pub_key == NULL && pub_key == NULL) |
1258396d MC |
309 | return 0; |
310 | ||
1da12e34 RL |
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 | } | |
1258396d MC |
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 | } | |
5d8d9a8e DSH |
342 | |
343 | int DSA_bits(const DSA *dsa) | |
344 | { | |
345 | return BN_num_bits(dsa->p); | |
346 | } |