]>
Commit | Line | Data |
---|---|---|
d2e9e320 | 1 | /* |
da1c088f | 2 | * Copyright 1995-2023 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 | ||
ec577822 | 16 | #include <openssl/bn.h> |
3f773c91 TM |
17 | #ifndef FIPS_MODULE |
18 | # include <openssl/engine.h> | |
19 | #endif | |
53cefef6 TM |
20 | #include "internal/cryptlib.h" |
21 | #include "internal/refcount.h" | |
e683582b | 22 | #include "crypto/dsa.h" |
dc8de3e6 | 23 | #include "crypto/dh.h" /* required by DSA_dup_DH() */ |
53cefef6 | 24 | #include "dsa_local.h" |
e683582b | 25 | |
b4250010 | 26 | static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx); |
8083fd3a | 27 | |
f844f9eb | 28 | #ifndef FIPS_MODULE |
d02b48c6 | 29 | |
e683582b SL |
30 | int DSA_set_ex_data(DSA *d, int idx, void *arg) |
31 | { | |
32 | return CRYPTO_set_ex_data(&d->ex_data, idx, arg); | |
33 | } | |
34 | ||
8cc86b81 | 35 | void *DSA_get_ex_data(const DSA *d, int idx) |
e683582b SL |
36 | { |
37 | return CRYPTO_get_ex_data(&d->ex_data, idx); | |
38 | } | |
39 | ||
dc8de3e6 | 40 | # ifndef OPENSSL_NO_DH |
e683582b SL |
41 | DH *DSA_dup_DH(const DSA *r) |
42 | { | |
43 | /* | |
dc8de3e6 SL |
44 | * DSA has p, q, g, optional pub_key, optional priv_key. |
45 | * DH has p, optional length, g, optional pub_key, | |
46 | * optional priv_key, optional q. | |
e683582b | 47 | */ |
e683582b | 48 | DH *ret = NULL; |
dc8de3e6 | 49 | BIGNUM *pub_key = NULL, *priv_key = NULL; |
e683582b SL |
50 | |
51 | if (r == NULL) | |
52 | goto err; | |
53 | ret = DH_new(); | |
54 | if (ret == NULL) | |
55 | goto err; | |
dc8de3e6 | 56 | |
19dbb742 | 57 | if (!ossl_ffc_params_copy(ossl_dh_get0_params(ret), &r->params)) |
dc8de3e6 | 58 | goto err; |
e683582b SL |
59 | |
60 | if (r->pub_key != NULL) { | |
61 | pub_key = BN_dup(r->pub_key); | |
62 | if (pub_key == NULL) | |
63 | goto err; | |
64 | if (r->priv_key != NULL) { | |
65 | priv_key = BN_dup(r->priv_key); | |
66 | if (priv_key == NULL) | |
67 | goto err; | |
68 | } | |
69 | if (!DH_set0_key(ret, pub_key, priv_key)) | |
70 | goto err; | |
71 | } else if (r->priv_key != NULL) { | |
72 | /* Shouldn't happen */ | |
73 | goto err; | |
74 | } | |
75 | ||
76 | return ret; | |
77 | ||
78 | err: | |
e683582b SL |
79 | BN_free(pub_key); |
80 | BN_free(priv_key); | |
81 | DH_free(ret); | |
82 | return NULL; | |
83 | } | |
dc8de3e6 | 84 | # endif /* OPENSSL_NO_DH */ |
e683582b | 85 | |
e683582b SL |
86 | void DSA_clear_flags(DSA *d, int flags) |
87 | { | |
88 | d->flags &= ~flags; | |
89 | } | |
90 | ||
91 | int DSA_test_flags(const DSA *d, int flags) | |
92 | { | |
93 | return d->flags & flags; | |
94 | } | |
95 | ||
96 | void DSA_set_flags(DSA *d, int flags) | |
97 | { | |
98 | d->flags |= flags; | |
99 | } | |
100 | ||
101 | ENGINE *DSA_get0_engine(DSA *d) | |
102 | { | |
103 | return d->engine; | |
104 | } | |
105 | ||
cb78486d | 106 | int DSA_set_method(DSA *dsa, const DSA_METHOD *meth) |
0f113f3e MC |
107 | { |
108 | /* | |
109 | * NB: The caller is specifically setting a method, so it's not up to us | |
110 | * to deal with which ENGINE it comes from. | |
111 | */ | |
112 | const DSA_METHOD *mtmp; | |
113 | mtmp = dsa->meth; | |
114 | if (mtmp->finish) | |
115 | mtmp->finish(dsa); | |
0b13e9f0 | 116 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd RS |
117 | ENGINE_finish(dsa->engine); |
118 | dsa->engine = NULL; | |
0b13e9f0 | 119 | #endif |
0f113f3e MC |
120 | dsa->meth = meth; |
121 | if (meth->init) | |
122 | meth->init(dsa); | |
123 | return 1; | |
124 | } | |
f844f9eb | 125 | #endif /* FIPS_MODULE */ |
e683582b | 126 | |
c0711f7f | 127 | |
6e9fa57c MC |
128 | const DSA_METHOD *DSA_get_method(DSA *d) |
129 | { | |
130 | return d->meth; | |
131 | } | |
132 | ||
b4250010 | 133 | static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx) |
0f113f3e | 134 | { |
2bbf0baa | 135 | DSA *ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e | 136 | |
e077455e | 137 | if (ret == NULL) |
d188a536 | 138 | return NULL; |
2bbf0baa | 139 | |
2bbf0baa F |
140 | ret->lock = CRYPTO_THREAD_lock_new(); |
141 | if (ret->lock == NULL) { | |
e077455e | 142 | ERR_raise(ERR_LIB_DSA, ERR_R_CRYPTO_LIB); |
2bbf0baa F |
143 | OPENSSL_free(ret); |
144 | return NULL; | |
145 | } | |
146 | ||
97beb77f P |
147 | if (!CRYPTO_NEW_REF(&ret->references, 1)) { |
148 | CRYPTO_THREAD_lock_free(ret->lock); | |
149 | OPENSSL_free(ret); | |
150 | return NULL; | |
151 | } | |
495e6d3b | 152 | |
8083fd3a | 153 | ret->libctx = libctx; |
0f113f3e | 154 | ret->meth = DSA_get_default_method(); |
f844f9eb | 155 | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE) |
2bbf0baa | 156 | ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */ |
0f113f3e MC |
157 | if (engine) { |
158 | if (!ENGINE_init(engine)) { | |
9311d0c4 | 159 | ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB); |
2bbf0baa | 160 | goto err; |
0f113f3e MC |
161 | } |
162 | ret->engine = engine; | |
163 | } else | |
164 | ret->engine = ENGINE_get_default_DSA(); | |
165 | if (ret->engine) { | |
166 | ret->meth = ENGINE_get_DSA(ret->engine); | |
7c96dbcd | 167 | if (ret->meth == NULL) { |
9311d0c4 | 168 | ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB); |
2bbf0baa | 169 | goto err; |
0f113f3e MC |
170 | } |
171 | } | |
0b13e9f0 | 172 | #endif |
0c9de428 | 173 | |
0f113f3e | 174 | ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; |
d188a536 | 175 | |
f844f9eb | 176 | #ifndef FIPS_MODULE |
e4bec869 SL |
177 | if (!ossl_crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret, |
178 | &ret->ex_data)) | |
2bbf0baa | 179 | goto err; |
a3327784 | 180 | #endif |
d188a536 | 181 | |
706512ec MSP |
182 | ossl_ffc_params_init(&ret->params); |
183 | ||
d188a536 | 184 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { |
9311d0c4 | 185 | ERR_raise(ERR_LIB_DSA, ERR_R_INIT_FAIL); |
544648a8 | 186 | goto err; |
0f113f3e MC |
187 | } |
188 | ||
d188a536 | 189 | return ret; |
544648a8 NT |
190 | |
191 | err: | |
192 | DSA_free(ret); | |
193 | return NULL; | |
0f113f3e | 194 | } |
d02b48c6 | 195 | |
e683582b SL |
196 | DSA *DSA_new_method(ENGINE *engine) |
197 | { | |
8083fd3a SL |
198 | return dsa_new_intern(engine, NULL); |
199 | } | |
200 | ||
5af02212 | 201 | DSA *ossl_dsa_new(OSSL_LIB_CTX *libctx) |
8083fd3a SL |
202 | { |
203 | return dsa_new_intern(NULL, libctx); | |
e683582b SL |
204 | } |
205 | ||
f844f9eb | 206 | #ifndef FIPS_MODULE |
a3327784 | 207 | DSA *DSA_new(void) |
e683582b | 208 | { |
8083fd3a | 209 | return dsa_new_intern(NULL, NULL); |
e683582b | 210 | } |
8083fd3a | 211 | #endif |
e683582b | 212 | |
6b691a5c | 213 | void DSA_free(DSA *r) |
0f113f3e MC |
214 | { |
215 | int i; | |
d02b48c6 | 216 | |
0f113f3e MC |
217 | if (r == NULL) |
218 | return; | |
d02b48c6 | 219 | |
495e6d3b | 220 | CRYPTO_DOWN_REF(&r->references, &i); |
dc10ffc2 | 221 | REF_PRINT_COUNT("DSA", i, r); |
0f113f3e MC |
222 | if (i > 0) |
223 | return; | |
f3f1cf84 | 224 | REF_ASSERT_ISNT(i < 0); |
d02b48c6 | 225 | |
0c5d725e | 226 | if (r->meth != NULL && r->meth->finish != NULL) |
0f113f3e | 227 | r->meth->finish(r); |
f844f9eb | 228 | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE) |
17fa4e8e | 229 | ENGINE_finish(r->engine); |
0b13e9f0 | 230 | #endif |
c0711f7f | 231 | |
f844f9eb | 232 | #ifndef FIPS_MODULE |
0f113f3e | 233 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); |
a3327784 | 234 | #endif |
0f113f3e | 235 | |
d188a536 | 236 | CRYPTO_THREAD_lock_free(r->lock); |
495e6d3b | 237 | CRYPTO_FREE_REF(&r->references); |
d188a536 | 238 | |
5357c106 | 239 | ossl_ffc_params_cleanup(&r->params); |
23a1d5e9 RS |
240 | BN_clear_free(r->pub_key); |
241 | BN_clear_free(r->priv_key); | |
0f113f3e MC |
242 | OPENSSL_free(r); |
243 | } | |
d02b48c6 | 244 | |
6ac4e8bd | 245 | int DSA_up_ref(DSA *r) |
0f113f3e | 246 | { |
d188a536 AG |
247 | int i; |
248 | ||
495e6d3b | 249 | if (CRYPTO_UP_REF(&r->references, &i) <= 0) |
d188a536 | 250 | return 0; |
f3f1cf84 | 251 | |
dc10ffc2 | 252 | REF_PRINT_COUNT("DSA", i, r); |
f3f1cf84 | 253 | REF_ASSERT_ISNT(i < 2); |
0f113f3e MC |
254 | return ((i > 1) ? 1 : 0); |
255 | } | |
5cbc2e8b | 256 | |
6963979f RL |
257 | void ossl_dsa_set0_libctx(DSA *d, OSSL_LIB_CTX *libctx) |
258 | { | |
259 | d->libctx = libctx; | |
260 | } | |
261 | ||
fd809cfd RL |
262 | void DSA_get0_pqg(const DSA *d, |
263 | const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) | |
1258396d | 264 | { |
5357c106 | 265 | ossl_ffc_params_get0_pqg(&d->params, p, q, g); |
1258396d MC |
266 | } |
267 | ||
268 | int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) | |
269 | { | |
fd809cfd | 270 | /* If the fields p, q and g in d are NULL, the corresponding input |
1da12e34 | 271 | * parameters MUST be non-NULL. |
1da12e34 | 272 | */ |
dc8de3e6 SL |
273 | if ((d->params.p == NULL && p == NULL) |
274 | || (d->params.q == NULL && q == NULL) | |
275 | || (d->params.g == NULL && g == NULL)) | |
1258396d | 276 | return 0; |
1da12e34 | 277 | |
5357c106 | 278 | ossl_ffc_params_set0_pqg(&d->params, p, q, g); |
4889dadc | 279 | d->dirty_cnt++; |
1258396d MC |
280 | |
281 | return 1; | |
282 | } | |
283 | ||
b305452f RL |
284 | const BIGNUM *DSA_get0_p(const DSA *d) |
285 | { | |
286 | return d->params.p; | |
287 | } | |
288 | ||
289 | const BIGNUM *DSA_get0_q(const DSA *d) | |
290 | { | |
291 | return d->params.q; | |
292 | } | |
293 | ||
294 | const BIGNUM *DSA_get0_g(const DSA *d) | |
295 | { | |
296 | return d->params.g; | |
297 | } | |
298 | ||
299 | const BIGNUM *DSA_get0_pub_key(const DSA *d) | |
300 | { | |
301 | return d->pub_key; | |
302 | } | |
303 | ||
304 | const BIGNUM *DSA_get0_priv_key(const DSA *d) | |
305 | { | |
306 | return d->priv_key; | |
307 | } | |
308 | ||
fd809cfd RL |
309 | void DSA_get0_key(const DSA *d, |
310 | const BIGNUM **pub_key, const BIGNUM **priv_key) | |
1258396d | 311 | { |
6e9fa57c MC |
312 | if (pub_key != NULL) |
313 | *pub_key = d->pub_key; | |
314 | if (priv_key != NULL) | |
315 | *priv_key = d->priv_key; | |
1258396d MC |
316 | } |
317 | ||
6e9fa57c | 318 | int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) |
1258396d | 319 | { |
1da12e34 RL |
320 | if (pub_key != NULL) { |
321 | BN_free(d->pub_key); | |
322 | d->pub_key = pub_key; | |
323 | } | |
324 | if (priv_key != NULL) { | |
325 | BN_free(d->priv_key); | |
326 | d->priv_key = priv_key; | |
327 | } | |
4889dadc | 328 | d->dirty_cnt++; |
1258396d MC |
329 | |
330 | return 1; | |
331 | } | |
332 | ||
806253f3 RL |
333 | int DSA_security_bits(const DSA *d) |
334 | { | |
dc8de3e6 SL |
335 | if (d->params.p != NULL && d->params.q != NULL) |
336 | return BN_security_bits(BN_num_bits(d->params.p), | |
337 | BN_num_bits(d->params.q)); | |
806253f3 RL |
338 | return -1; |
339 | } | |
340 | ||
341 | int DSA_bits(const DSA *dsa) | |
342 | { | |
ecfbe2f0 RL |
343 | if (dsa->params.p != NULL) |
344 | return BN_num_bits(dsa->params.p); | |
345 | return -1; | |
806253f3 | 346 | } |
2888fc15 | 347 | |
5af02212 | 348 | FFC_PARAMS *ossl_dsa_get0_params(DSA *dsa) |
2888fc15 RL |
349 | { |
350 | return &dsa->params; | |
351 | } | |
b03ec3b5 | 352 | |
5af02212 | 353 | int ossl_dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[]) |
b03ec3b5 SL |
354 | { |
355 | int ret; | |
a76ccb9d | 356 | FFC_PARAMS *ffc = ossl_dsa_get0_params(dsa); |
b03ec3b5 | 357 | |
5357c106 | 358 | ret = ossl_ffc_params_fromdata(ffc, params); |
b03ec3b5 SL |
359 | if (ret) |
360 | dsa->dirty_cnt++; | |
361 | return ret; | |
362 | } |