]>
Commit | Line | Data |
---|---|---|
d2e9e320 | 1 | /* |
dc8de3e6 | 2 | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
3cdbea65 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
d2e9e320 RS |
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 | ||
f41ac0ee P |
10 | /* |
11 | * DSA low level APIs are deprecated for public use, but still ok for | |
12 | * internal use. | |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
d02b48c6 | 16 | #include <stdio.h> |
b39fc560 | 17 | #include "internal/cryptlib.h" |
cd420b0b | 18 | #include "internal/refcount.h" |
ec577822 | 19 | #include <openssl/bn.h> |
ec577822 | 20 | #include <openssl/asn1.h> |
3c27208f | 21 | #include <openssl/engine.h> |
dc8de3e6 | 22 | #include "dsa_local.h" |
e683582b | 23 | #include "crypto/dsa.h" |
dc8de3e6 | 24 | #include "crypto/dh.h" /* required by DSA_dup_DH() */ |
e683582b SL |
25 | |
26 | #ifndef FIPS_MODE | |
d02b48c6 | 27 | |
e683582b SL |
28 | int DSA_set_ex_data(DSA *d, int idx, void *arg) |
29 | { | |
30 | return CRYPTO_set_ex_data(&d->ex_data, idx, arg); | |
31 | } | |
32 | ||
33 | void *DSA_get_ex_data(DSA *d, int idx) | |
34 | { | |
35 | return CRYPTO_get_ex_data(&d->ex_data, idx); | |
36 | } | |
37 | ||
dc8de3e6 | 38 | # ifndef OPENSSL_NO_DH |
e683582b SL |
39 | DH *DSA_dup_DH(const DSA *r) |
40 | { | |
41 | /* | |
dc8de3e6 SL |
42 | * DSA has p, q, g, optional pub_key, optional priv_key. |
43 | * DH has p, optional length, g, optional pub_key, | |
44 | * optional priv_key, optional q. | |
e683582b | 45 | */ |
e683582b | 46 | DH *ret = NULL; |
dc8de3e6 | 47 | BIGNUM *pub_key = NULL, *priv_key = NULL; |
e683582b SL |
48 | |
49 | if (r == NULL) | |
50 | goto err; | |
51 | ret = DH_new(); | |
52 | if (ret == NULL) | |
53 | goto err; | |
dc8de3e6 SL |
54 | |
55 | if (!ffc_params_copy(dh_get0_params(ret), &r->params)) | |
56 | goto err; | |
e683582b SL |
57 | |
58 | if (r->pub_key != NULL) { | |
59 | pub_key = BN_dup(r->pub_key); | |
60 | if (pub_key == NULL) | |
61 | goto err; | |
62 | if (r->priv_key != NULL) { | |
63 | priv_key = BN_dup(r->priv_key); | |
64 | if (priv_key == NULL) | |
65 | goto err; | |
66 | } | |
67 | if (!DH_set0_key(ret, pub_key, priv_key)) | |
68 | goto err; | |
69 | } else if (r->priv_key != NULL) { | |
70 | /* Shouldn't happen */ | |
71 | goto err; | |
72 | } | |
73 | ||
74 | return ret; | |
75 | ||
76 | err: | |
e683582b SL |
77 | BN_free(pub_key); |
78 | BN_free(priv_key); | |
79 | DH_free(ret); | |
80 | return NULL; | |
81 | } | |
dc8de3e6 | 82 | # endif /* OPENSSL_NO_DH */ |
e683582b | 83 | |
e683582b SL |
84 | void DSA_clear_flags(DSA *d, int flags) |
85 | { | |
86 | d->flags &= ~flags; | |
87 | } | |
88 | ||
89 | int DSA_test_flags(const DSA *d, int flags) | |
90 | { | |
91 | return d->flags & flags; | |
92 | } | |
93 | ||
94 | void DSA_set_flags(DSA *d, int flags) | |
95 | { | |
96 | d->flags |= flags; | |
97 | } | |
98 | ||
99 | ENGINE *DSA_get0_engine(DSA *d) | |
100 | { | |
101 | return d->engine; | |
102 | } | |
103 | ||
cb78486d | 104 | int DSA_set_method(DSA *dsa, const DSA_METHOD *meth) |
0f113f3e MC |
105 | { |
106 | /* | |
107 | * NB: The caller is specifically setting a method, so it's not up to us | |
108 | * to deal with which ENGINE it comes from. | |
109 | */ | |
110 | const DSA_METHOD *mtmp; | |
111 | mtmp = dsa->meth; | |
112 | if (mtmp->finish) | |
113 | mtmp->finish(dsa); | |
0b13e9f0 | 114 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd RS |
115 | ENGINE_finish(dsa->engine); |
116 | dsa->engine = NULL; | |
0b13e9f0 | 117 | #endif |
0f113f3e MC |
118 | dsa->meth = meth; |
119 | if (meth->init) | |
120 | meth->init(dsa); | |
121 | return 1; | |
122 | } | |
e683582b SL |
123 | #endif /* FIPS_MODE */ |
124 | ||
c0711f7f | 125 | |
6e9fa57c MC |
126 | const DSA_METHOD *DSA_get_method(DSA *d) |
127 | { | |
128 | return d->meth; | |
129 | } | |
130 | ||
e683582b | 131 | static DSA *dsa_new_method(OPENSSL_CTX *libctx, ENGINE *engine) |
0f113f3e | 132 | { |
2bbf0baa | 133 | DSA *ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e | 134 | |
0f113f3e MC |
135 | if (ret == NULL) { |
136 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); | |
d188a536 | 137 | return NULL; |
0f113f3e | 138 | } |
2bbf0baa F |
139 | |
140 | ret->references = 1; | |
141 | ret->lock = CRYPTO_THREAD_lock_new(); | |
142 | if (ret->lock == NULL) { | |
b2b361f6 | 143 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); |
2bbf0baa F |
144 | OPENSSL_free(ret); |
145 | return NULL; | |
146 | } | |
147 | ||
0f113f3e | 148 | ret->meth = DSA_get_default_method(); |
e683582b | 149 | #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE) |
2bbf0baa | 150 | ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */ |
0f113f3e MC |
151 | if (engine) { |
152 | if (!ENGINE_init(engine)) { | |
153 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); | |
2bbf0baa | 154 | goto err; |
0f113f3e MC |
155 | } |
156 | ret->engine = engine; | |
157 | } else | |
158 | ret->engine = ENGINE_get_default_DSA(); | |
159 | if (ret->engine) { | |
160 | ret->meth = ENGINE_get_DSA(ret->engine); | |
7c96dbcd | 161 | if (ret->meth == NULL) { |
0f113f3e | 162 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); |
2bbf0baa | 163 | goto err; |
0f113f3e MC |
164 | } |
165 | } | |
0b13e9f0 | 166 | #endif |
0c9de428 | 167 | |
0f113f3e | 168 | ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; |
d188a536 | 169 | |
a3327784 | 170 | #ifndef FIPS_MODE |
e683582b | 171 | if (!crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data)) |
2bbf0baa | 172 | goto err; |
a3327784 | 173 | #endif |
d188a536 AG |
174 | |
175 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { | |
2bbf0baa | 176 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL); |
544648a8 | 177 | goto err; |
0f113f3e MC |
178 | } |
179 | ||
d188a536 | 180 | return ret; |
544648a8 NT |
181 | |
182 | err: | |
183 | DSA_free(ret); | |
184 | return NULL; | |
0f113f3e | 185 | } |
d02b48c6 | 186 | |
e683582b SL |
187 | DSA *DSA_new_method(ENGINE *engine) |
188 | { | |
189 | return dsa_new_method(NULL, engine); | |
190 | } | |
191 | ||
a3327784 | 192 | DSA *DSA_new(void) |
e683582b | 193 | { |
a3327784 | 194 | return DSA_new_method(NULL); |
e683582b SL |
195 | } |
196 | ||
6b691a5c | 197 | void DSA_free(DSA *r) |
0f113f3e MC |
198 | { |
199 | int i; | |
d02b48c6 | 200 | |
0f113f3e MC |
201 | if (r == NULL) |
202 | return; | |
d02b48c6 | 203 | |
2f545ae4 | 204 | CRYPTO_DOWN_REF(&r->references, &i, r->lock); |
f3f1cf84 | 205 | REF_PRINT_COUNT("DSA", r); |
0f113f3e MC |
206 | if (i > 0) |
207 | return; | |
f3f1cf84 | 208 | REF_ASSERT_ISNT(i < 0); |
d02b48c6 | 209 | |
0c5d725e | 210 | if (r->meth != NULL && r->meth->finish != NULL) |
0f113f3e | 211 | r->meth->finish(r); |
e683582b | 212 | #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE) |
17fa4e8e | 213 | ENGINE_finish(r->engine); |
0b13e9f0 | 214 | #endif |
c0711f7f | 215 | |
a3327784 | 216 | #ifndef FIPS_MODE |
0f113f3e | 217 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); |
a3327784 | 218 | #endif |
0f113f3e | 219 | |
d188a536 AG |
220 | CRYPTO_THREAD_lock_free(r->lock); |
221 | ||
dc8de3e6 | 222 | ffc_params_cleanup(&r->params); |
23a1d5e9 RS |
223 | BN_clear_free(r->pub_key); |
224 | BN_clear_free(r->priv_key); | |
0f113f3e MC |
225 | OPENSSL_free(r); |
226 | } | |
d02b48c6 | 227 | |
6ac4e8bd | 228 | int DSA_up_ref(DSA *r) |
0f113f3e | 229 | { |
d188a536 AG |
230 | int i; |
231 | ||
2f545ae4 | 232 | if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0) |
d188a536 | 233 | return 0; |
f3f1cf84 RS |
234 | |
235 | REF_PRINT_COUNT("DSA", r); | |
236 | REF_ASSERT_ISNT(i < 2); | |
0f113f3e MC |
237 | return ((i > 1) ? 1 : 0); |
238 | } | |
5cbc2e8b | 239 | |
fd809cfd RL |
240 | void DSA_get0_pqg(const DSA *d, |
241 | const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) | |
1258396d | 242 | { |
dc8de3e6 | 243 | ffc_params_get0_pqg(&d->params, p, q, g); |
1258396d MC |
244 | } |
245 | ||
246 | int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) | |
247 | { | |
fd809cfd | 248 | /* If the fields p, q and g in d are NULL, the corresponding input |
1da12e34 | 249 | * parameters MUST be non-NULL. |
1da12e34 | 250 | */ |
dc8de3e6 SL |
251 | if ((d->params.p == NULL && p == NULL) |
252 | || (d->params.q == NULL && q == NULL) | |
253 | || (d->params.g == NULL && g == NULL)) | |
1258396d | 254 | return 0; |
1da12e34 | 255 | |
dc8de3e6 | 256 | ffc_params_set0_pqg(&d->params, p, q, g); |
4889dadc | 257 | d->dirty_cnt++; |
1258396d MC |
258 | |
259 | return 1; | |
260 | } | |
261 | ||
b305452f RL |
262 | const BIGNUM *DSA_get0_p(const DSA *d) |
263 | { | |
264 | return d->params.p; | |
265 | } | |
266 | ||
267 | const BIGNUM *DSA_get0_q(const DSA *d) | |
268 | { | |
269 | return d->params.q; | |
270 | } | |
271 | ||
272 | const BIGNUM *DSA_get0_g(const DSA *d) | |
273 | { | |
274 | return d->params.g; | |
275 | } | |
276 | ||
277 | const BIGNUM *DSA_get0_pub_key(const DSA *d) | |
278 | { | |
279 | return d->pub_key; | |
280 | } | |
281 | ||
282 | const BIGNUM *DSA_get0_priv_key(const DSA *d) | |
283 | { | |
284 | return d->priv_key; | |
285 | } | |
286 | ||
fd809cfd RL |
287 | void DSA_get0_key(const DSA *d, |
288 | const BIGNUM **pub_key, const BIGNUM **priv_key) | |
1258396d | 289 | { |
6e9fa57c MC |
290 | if (pub_key != NULL) |
291 | *pub_key = d->pub_key; | |
292 | if (priv_key != NULL) | |
293 | *priv_key = d->priv_key; | |
1258396d MC |
294 | } |
295 | ||
6e9fa57c | 296 | int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) |
1258396d | 297 | { |
fd809cfd | 298 | /* If the field pub_key in d is NULL, the corresponding input |
1da12e34 RL |
299 | * parameters MUST be non-NULL. The priv_key field may |
300 | * be left NULL. | |
1da12e34 | 301 | */ |
fd809cfd | 302 | if (d->pub_key == NULL && pub_key == NULL) |
1258396d MC |
303 | return 0; |
304 | ||
1da12e34 RL |
305 | if (pub_key != NULL) { |
306 | BN_free(d->pub_key); | |
307 | d->pub_key = pub_key; | |
308 | } | |
309 | if (priv_key != NULL) { | |
310 | BN_free(d->priv_key); | |
311 | d->priv_key = priv_key; | |
312 | } | |
4889dadc | 313 | d->dirty_cnt++; |
1258396d MC |
314 | |
315 | return 1; | |
316 | } | |
317 | ||
806253f3 RL |
318 | int DSA_security_bits(const DSA *d) |
319 | { | |
dc8de3e6 SL |
320 | if (d->params.p != NULL && d->params.q != NULL) |
321 | return BN_security_bits(BN_num_bits(d->params.p), | |
322 | BN_num_bits(d->params.q)); | |
806253f3 RL |
323 | return -1; |
324 | } | |
325 | ||
326 | int DSA_bits(const DSA *dsa) | |
327 | { | |
dc8de3e6 | 328 | return BN_num_bits(dsa->params.p); |
806253f3 | 329 | } |