/src/openssl30/crypto/dsa/dsa_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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 | | /* |
11 | | * DSA low level APIs are deprecated for public use, but still ok for |
12 | | * internal use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <openssl/bn.h> |
17 | | #ifndef FIPS_MODULE |
18 | | # include <openssl/engine.h> |
19 | | #endif |
20 | | #include "internal/cryptlib.h" |
21 | | #include "internal/refcount.h" |
22 | | #include "crypto/dsa.h" |
23 | | #include "crypto/dh.h" /* required by DSA_dup_DH() */ |
24 | | #include "dsa_local.h" |
25 | | |
26 | | static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx); |
27 | | |
28 | | #ifndef FIPS_MODULE |
29 | | |
30 | | int DSA_set_ex_data(DSA *d, int idx, void *arg) |
31 | 0 | { |
32 | 0 | return CRYPTO_set_ex_data(&d->ex_data, idx, arg); |
33 | 0 | } |
34 | | |
35 | | void *DSA_get_ex_data(const DSA *d, int idx) |
36 | 0 | { |
37 | 0 | return CRYPTO_get_ex_data(&d->ex_data, idx); |
38 | 0 | } |
39 | | |
40 | | # ifndef OPENSSL_NO_DH |
41 | | DH *DSA_dup_DH(const DSA *r) |
42 | 0 | { |
43 | | /* |
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. |
47 | | */ |
48 | 0 | DH *ret = NULL; |
49 | 0 | BIGNUM *pub_key = NULL, *priv_key = NULL; |
50 | |
|
51 | 0 | if (r == NULL) |
52 | 0 | goto err; |
53 | 0 | ret = DH_new(); |
54 | 0 | if (ret == NULL) |
55 | 0 | goto err; |
56 | | |
57 | 0 | if (!ossl_ffc_params_copy(ossl_dh_get0_params(ret), &r->params)) |
58 | 0 | goto err; |
59 | | |
60 | 0 | if (r->pub_key != NULL) { |
61 | 0 | pub_key = BN_dup(r->pub_key); |
62 | 0 | if (pub_key == NULL) |
63 | 0 | goto err; |
64 | 0 | if (r->priv_key != NULL) { |
65 | 0 | priv_key = BN_dup(r->priv_key); |
66 | 0 | if (priv_key == NULL) |
67 | 0 | goto err; |
68 | 0 | } |
69 | 0 | if (!DH_set0_key(ret, pub_key, priv_key)) |
70 | 0 | goto err; |
71 | 0 | } else if (r->priv_key != NULL) { |
72 | | /* Shouldn't happen */ |
73 | 0 | goto err; |
74 | 0 | } |
75 | | |
76 | 0 | return ret; |
77 | | |
78 | 0 | err: |
79 | 0 | BN_free(pub_key); |
80 | 0 | BN_free(priv_key); |
81 | 0 | DH_free(ret); |
82 | 0 | return NULL; |
83 | 0 | } |
84 | | # endif /* OPENSSL_NO_DH */ |
85 | | |
86 | | void DSA_clear_flags(DSA *d, int flags) |
87 | 0 | { |
88 | 0 | d->flags &= ~flags; |
89 | 0 | } |
90 | | |
91 | | int DSA_test_flags(const DSA *d, int flags) |
92 | 0 | { |
93 | 0 | return d->flags & flags; |
94 | 0 | } |
95 | | |
96 | | void DSA_set_flags(DSA *d, int flags) |
97 | 0 | { |
98 | 0 | d->flags |= flags; |
99 | 0 | } |
100 | | |
101 | | ENGINE *DSA_get0_engine(DSA *d) |
102 | 0 | { |
103 | 0 | return d->engine; |
104 | 0 | } |
105 | | |
106 | | int DSA_set_method(DSA *dsa, const DSA_METHOD *meth) |
107 | 0 | { |
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 | 0 | const DSA_METHOD *mtmp; |
113 | 0 | mtmp = dsa->meth; |
114 | 0 | if (mtmp->finish) |
115 | 0 | mtmp->finish(dsa); |
116 | 0 | #ifndef OPENSSL_NO_ENGINE |
117 | 0 | ENGINE_finish(dsa->engine); |
118 | 0 | dsa->engine = NULL; |
119 | 0 | #endif |
120 | 0 | dsa->meth = meth; |
121 | 0 | if (meth->init) |
122 | 0 | meth->init(dsa); |
123 | 0 | return 1; |
124 | 0 | } |
125 | | #endif /* FIPS_MODULE */ |
126 | | |
127 | | |
128 | | const DSA_METHOD *DSA_get_method(DSA *d) |
129 | 90.3k | { |
130 | 90.3k | return d->meth; |
131 | 90.3k | } |
132 | | |
133 | | static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx) |
134 | 202k | { |
135 | 202k | DSA *ret = OPENSSL_zalloc(sizeof(*ret)); |
136 | | |
137 | 202k | if (ret == NULL) { |
138 | 0 | ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); |
139 | 0 | return NULL; |
140 | 0 | } |
141 | | |
142 | 202k | ret->references = 1; |
143 | 202k | ret->lock = CRYPTO_THREAD_lock_new(); |
144 | 202k | if (ret->lock == NULL) { |
145 | 0 | ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); |
146 | 0 | OPENSSL_free(ret); |
147 | 0 | return NULL; |
148 | 0 | } |
149 | | |
150 | 202k | ret->libctx = libctx; |
151 | 202k | ret->meth = DSA_get_default_method(); |
152 | 202k | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE) |
153 | 202k | ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */ |
154 | 202k | if (engine) { |
155 | 0 | if (!ENGINE_init(engine)) { |
156 | 0 | ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB); |
157 | 0 | goto err; |
158 | 0 | } |
159 | 0 | ret->engine = engine; |
160 | 0 | } else |
161 | 202k | ret->engine = ENGINE_get_default_DSA(); |
162 | 202k | if (ret->engine) { |
163 | 0 | ret->meth = ENGINE_get_DSA(ret->engine); |
164 | 0 | if (ret->meth == NULL) { |
165 | 0 | ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB); |
166 | 0 | goto err; |
167 | 0 | } |
168 | 0 | } |
169 | 202k | #endif |
170 | | |
171 | 202k | ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; |
172 | | |
173 | 202k | #ifndef FIPS_MODULE |
174 | 202k | if (!ossl_crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret, |
175 | 202k | &ret->ex_data)) |
176 | 0 | goto err; |
177 | 202k | #endif |
178 | | |
179 | 202k | ossl_ffc_params_init(&ret->params); |
180 | | |
181 | 202k | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { |
182 | 0 | ERR_raise(ERR_LIB_DSA, ERR_R_INIT_FAIL); |
183 | 0 | goto err; |
184 | 0 | } |
185 | | |
186 | 202k | return ret; |
187 | | |
188 | 0 | err: |
189 | 0 | DSA_free(ret); |
190 | 0 | return NULL; |
191 | 202k | } |
192 | | |
193 | | DSA *DSA_new_method(ENGINE *engine) |
194 | 0 | { |
195 | 0 | return dsa_new_intern(engine, NULL); |
196 | 0 | } |
197 | | |
198 | | DSA *ossl_dsa_new(OSSL_LIB_CTX *libctx) |
199 | 51.8k | { |
200 | 51.8k | return dsa_new_intern(NULL, libctx); |
201 | 51.8k | } |
202 | | |
203 | | #ifndef FIPS_MODULE |
204 | | DSA *DSA_new(void) |
205 | 351k | { |
206 | 351k | return dsa_new_intern(NULL, NULL); |
207 | 351k | } |
208 | | #endif |
209 | | |
210 | | void DSA_free(DSA *r) |
211 | 1.09M | { |
212 | 1.09M | int i; |
213 | | |
214 | 1.09M | if (r == NULL) |
215 | 624k | return; |
216 | | |
217 | 469k | CRYPTO_DOWN_REF(&r->references, &i, r->lock); |
218 | 469k | REF_PRINT_COUNT("DSA", r); |
219 | 469k | if (i > 0) |
220 | 66.2k | return; |
221 | 403k | REF_ASSERT_ISNT(i < 0); |
222 | | |
223 | 403k | if (r->meth != NULL && r->meth->finish != NULL) |
224 | 403k | r->meth->finish(r); |
225 | 403k | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE) |
226 | 403k | ENGINE_finish(r->engine); |
227 | 403k | #endif |
228 | | |
229 | 403k | #ifndef FIPS_MODULE |
230 | 403k | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); |
231 | 403k | #endif |
232 | | |
233 | 403k | CRYPTO_THREAD_lock_free(r->lock); |
234 | | |
235 | 403k | ossl_ffc_params_cleanup(&r->params); |
236 | 403k | BN_clear_free(r->pub_key); |
237 | 403k | BN_clear_free(r->priv_key); |
238 | 403k | OPENSSL_free(r); |
239 | 403k | } |
240 | | |
241 | | int DSA_up_ref(DSA *r) |
242 | 66.2k | { |
243 | 66.2k | int i; |
244 | | |
245 | 66.2k | if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0) |
246 | 0 | return 0; |
247 | | |
248 | 66.2k | REF_PRINT_COUNT("DSA", r); |
249 | 66.2k | REF_ASSERT_ISNT(i < 2); |
250 | 66.2k | return ((i > 1) ? 1 : 0); |
251 | 66.2k | } |
252 | | |
253 | | void ossl_dsa_set0_libctx(DSA *d, OSSL_LIB_CTX *libctx) |
254 | 63.8k | { |
255 | 63.8k | d->libctx = libctx; |
256 | 63.8k | } |
257 | | |
258 | | void DSA_get0_pqg(const DSA *d, |
259 | | const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) |
260 | 38.2k | { |
261 | 38.2k | ossl_ffc_params_get0_pqg(&d->params, p, q, g); |
262 | 38.2k | } |
263 | | |
264 | | int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) |
265 | 420 | { |
266 | | /* If the fields p, q and g in d are NULL, the corresponding input |
267 | | * parameters MUST be non-NULL. |
268 | | */ |
269 | 420 | if ((d->params.p == NULL && p == NULL) |
270 | 420 | || (d->params.q == NULL && q == NULL) |
271 | 420 | || (d->params.g == NULL && g == NULL)) |
272 | 0 | return 0; |
273 | | |
274 | 420 | ossl_ffc_params_set0_pqg(&d->params, p, q, g); |
275 | 420 | d->dirty_cnt++; |
276 | | |
277 | 420 | return 1; |
278 | 420 | } |
279 | | |
280 | | const BIGNUM *DSA_get0_p(const DSA *d) |
281 | 84.1k | { |
282 | 84.1k | return d->params.p; |
283 | 84.1k | } |
284 | | |
285 | | const BIGNUM *DSA_get0_q(const DSA *d) |
286 | 25.4k | { |
287 | 25.4k | return d->params.q; |
288 | 25.4k | } |
289 | | |
290 | | const BIGNUM *DSA_get0_g(const DSA *d) |
291 | 79.5k | { |
292 | 79.5k | return d->params.g; |
293 | 79.5k | } |
294 | | |
295 | | const BIGNUM *DSA_get0_pub_key(const DSA *d) |
296 | 109k | { |
297 | 109k | return d->pub_key; |
298 | 109k | } |
299 | | |
300 | | const BIGNUM *DSA_get0_priv_key(const DSA *d) |
301 | 55.3k | { |
302 | 55.3k | return d->priv_key; |
303 | 55.3k | } |
304 | | |
305 | | void DSA_get0_key(const DSA *d, |
306 | | const BIGNUM **pub_key, const BIGNUM **priv_key) |
307 | 92.8k | { |
308 | 92.8k | if (pub_key != NULL) |
309 | 92.1k | *pub_key = d->pub_key; |
310 | 92.8k | if (priv_key != NULL) |
311 | 91.3k | *priv_key = d->priv_key; |
312 | 92.8k | } |
313 | | |
314 | | int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) |
315 | 53.1k | { |
316 | 53.1k | if (pub_key != NULL) { |
317 | 53.1k | BN_free(d->pub_key); |
318 | 53.1k | d->pub_key = pub_key; |
319 | 53.1k | } |
320 | 53.1k | if (priv_key != NULL) { |
321 | 53.1k | BN_free(d->priv_key); |
322 | 53.1k | d->priv_key = priv_key; |
323 | 53.1k | } |
324 | 53.1k | d->dirty_cnt++; |
325 | | |
326 | 53.1k | return 1; |
327 | 53.1k | } |
328 | | |
329 | | int DSA_security_bits(const DSA *d) |
330 | 65.2k | { |
331 | 65.2k | if (d->params.p != NULL && d->params.q != NULL) |
332 | 65.2k | return BN_security_bits(BN_num_bits(d->params.p), |
333 | 65.2k | BN_num_bits(d->params.q)); |
334 | 0 | return -1; |
335 | 65.2k | } |
336 | | |
337 | | int DSA_bits(const DSA *dsa) |
338 | 65.5k | { |
339 | 65.5k | if (dsa->params.p != NULL) |
340 | 65.5k | return BN_num_bits(dsa->params.p); |
341 | 0 | return -1; |
342 | 65.5k | } |
343 | | |
344 | | FFC_PARAMS *ossl_dsa_get0_params(DSA *dsa) |
345 | 249k | { |
346 | 249k | return &dsa->params; |
347 | 249k | } |
348 | | |
349 | | int ossl_dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[]) |
350 | 20.1k | { |
351 | 20.1k | int ret; |
352 | 20.1k | FFC_PARAMS *ffc; |
353 | | |
354 | 20.1k | if (dsa == NULL) |
355 | 0 | return 0; |
356 | 20.1k | ffc = ossl_dsa_get0_params(dsa); |
357 | 20.1k | if (ffc == NULL) |
358 | 0 | return 0; |
359 | | |
360 | 20.1k | ret = ossl_ffc_params_fromdata(ffc, params); |
361 | 20.1k | if (ret) |
362 | 20.1k | dsa->dirty_cnt++; |
363 | 20.1k | return ret; |
364 | 20.1k | } |