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
DSA_set_ex_data(DSA * d,int idx,void * arg)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
DSA_get_ex_data(const DSA * d,int idx)35 void *DSA_get_ex_data(const DSA *d, int idx)
36 {
37 return CRYPTO_get_ex_data(&d->ex_data, idx);
38 }
39
40 #ifndef OPENSSL_NO_DH
DSA_dup_DH(const DSA * r)41 DH *DSA_dup_DH(const DSA *r)
42 {
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 DH *ret = NULL;
49 BIGNUM *pub_key = NULL, *priv_key = NULL;
50
51 if (r == NULL)
52 goto err;
53 ret = DH_new();
54 if (ret == NULL)
55 goto err;
56
57 if (!ossl_ffc_params_copy(ossl_dh_get0_params(ret), &r->params))
58 goto err;
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:
79 BN_free(pub_key);
80 BN_free(priv_key);
81 DH_free(ret);
82 return NULL;
83 }
84 #endif /* OPENSSL_NO_DH */
85
DSA_clear_flags(DSA * d,int flags)86 void DSA_clear_flags(DSA *d, int flags)
87 {
88 d->flags &= ~flags;
89 }
90
DSA_test_flags(const DSA * d,int flags)91 int DSA_test_flags(const DSA *d, int flags)
92 {
93 return d->flags & flags;
94 }
95
DSA_set_flags(DSA * d,int flags)96 void DSA_set_flags(DSA *d, int flags)
97 {
98 d->flags |= flags;
99 }
100
DSA_get0_engine(DSA * d)101 ENGINE *DSA_get0_engine(DSA *d)
102 {
103 return d->engine;
104 }
105
DSA_set_method(DSA * dsa,const DSA_METHOD * meth)106 int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
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);
116 #ifndef OPENSSL_NO_ENGINE
117 ENGINE_finish(dsa->engine);
118 dsa->engine = NULL;
119 #endif
120 dsa->meth = meth;
121 if (meth->init)
122 meth->init(dsa);
123 return 1;
124 }
125 #endif /* FIPS_MODULE */
126
DSA_get_method(DSA * d)127 const DSA_METHOD *DSA_get_method(DSA *d)
128 {
129 return d->meth;
130 }
131
dsa_new_intern(ENGINE * engine,OSSL_LIB_CTX * libctx)132 static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
133 {
134 DSA *ret = OPENSSL_zalloc(sizeof(*ret));
135
136 if (ret == NULL)
137 return NULL;
138
139 ret->lock = CRYPTO_THREAD_lock_new();
140 if (ret->lock == NULL) {
141 ERR_raise(ERR_LIB_DSA, ERR_R_CRYPTO_LIB);
142 OPENSSL_free(ret);
143 return NULL;
144 }
145
146 if (!CRYPTO_NEW_REF(&ret->references, 1)) {
147 CRYPTO_THREAD_lock_free(ret->lock);
148 OPENSSL_free(ret);
149 return NULL;
150 }
151
152 ret->libctx = libctx;
153 ret->meth = DSA_get_default_method();
154 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
155 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
156 if (engine) {
157 if (!ENGINE_init(engine)) {
158 ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB);
159 goto err;
160 }
161 ret->engine = engine;
162 } else
163 ret->engine = ENGINE_get_default_DSA();
164 if (ret->engine) {
165 ret->meth = ENGINE_get_DSA(ret->engine);
166 if (ret->meth == NULL) {
167 ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB);
168 goto err;
169 }
170 }
171 #endif
172
173 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
174
175 #ifndef FIPS_MODULE
176 if (!ossl_crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret,
177 &ret->ex_data))
178 goto err;
179 #endif
180
181 ossl_ffc_params_init(&ret->params);
182
183 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
184 ERR_raise(ERR_LIB_DSA, ERR_R_INIT_FAIL);
185 goto err;
186 }
187
188 return ret;
189
190 err:
191 DSA_free(ret);
192 return NULL;
193 }
194
DSA_new_method(ENGINE * engine)195 DSA *DSA_new_method(ENGINE *engine)
196 {
197 return dsa_new_intern(engine, NULL);
198 }
199
ossl_dsa_new(OSSL_LIB_CTX * libctx)200 DSA *ossl_dsa_new(OSSL_LIB_CTX *libctx)
201 {
202 return dsa_new_intern(NULL, libctx);
203 }
204
205 #ifndef FIPS_MODULE
DSA_new(void)206 DSA *DSA_new(void)
207 {
208 return dsa_new_intern(NULL, NULL);
209 }
210 #endif
211
DSA_free(DSA * r)212 void DSA_free(DSA *r)
213 {
214 int i;
215
216 if (r == NULL)
217 return;
218
219 CRYPTO_DOWN_REF(&r->references, &i);
220 REF_PRINT_COUNT("DSA", i, r);
221 if (i > 0)
222 return;
223 REF_ASSERT_ISNT(i < 0);
224
225 if (r->meth != NULL && r->meth->finish != NULL)
226 r->meth->finish(r);
227 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
228 ENGINE_finish(r->engine);
229 #endif
230
231 #ifndef FIPS_MODULE
232 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
233 #endif
234
235 CRYPTO_THREAD_lock_free(r->lock);
236 CRYPTO_FREE_REF(&r->references);
237
238 ossl_ffc_params_cleanup(&r->params);
239 BN_clear_free(r->pub_key);
240 BN_clear_free(r->priv_key);
241 OPENSSL_free(r);
242 }
243
DSA_up_ref(DSA * r)244 int DSA_up_ref(DSA *r)
245 {
246 int i;
247
248 if (CRYPTO_UP_REF(&r->references, &i) <= 0)
249 return 0;
250
251 REF_PRINT_COUNT("DSA", i, r);
252 REF_ASSERT_ISNT(i < 2);
253 return ((i > 1) ? 1 : 0);
254 }
255
ossl_dsa_set0_libctx(DSA * d,OSSL_LIB_CTX * libctx)256 void ossl_dsa_set0_libctx(DSA *d, OSSL_LIB_CTX *libctx)
257 {
258 d->libctx = libctx;
259 }
260
DSA_get0_pqg(const DSA * d,const BIGNUM ** p,const BIGNUM ** q,const BIGNUM ** g)261 void DSA_get0_pqg(const DSA *d,
262 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
263 {
264 ossl_ffc_params_get0_pqg(&d->params, p, q, g);
265 }
266
DSA_set0_pqg(DSA * d,BIGNUM * p,BIGNUM * q,BIGNUM * g)267 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
268 {
269 /* If the fields p, q and g in d are NULL, the corresponding input
270 * parameters MUST be non-NULL.
271 */
272 if ((d->params.p == NULL && p == NULL)
273 || (d->params.q == NULL && q == NULL)
274 || (d->params.g == NULL && g == NULL))
275 return 0;
276
277 ossl_ffc_params_set0_pqg(&d->params, p, q, g);
278 d->dirty_cnt++;
279
280 return 1;
281 }
282
DSA_get0_p(const DSA * d)283 const BIGNUM *DSA_get0_p(const DSA *d)
284 {
285 return d->params.p;
286 }
287
DSA_get0_q(const DSA * d)288 const BIGNUM *DSA_get0_q(const DSA *d)
289 {
290 return d->params.q;
291 }
292
DSA_get0_g(const DSA * d)293 const BIGNUM *DSA_get0_g(const DSA *d)
294 {
295 return d->params.g;
296 }
297
DSA_get0_pub_key(const DSA * d)298 const BIGNUM *DSA_get0_pub_key(const DSA *d)
299 {
300 return d->pub_key;
301 }
302
DSA_get0_priv_key(const DSA * d)303 const BIGNUM *DSA_get0_priv_key(const DSA *d)
304 {
305 return d->priv_key;
306 }
307
DSA_get0_key(const DSA * d,const BIGNUM ** pub_key,const BIGNUM ** priv_key)308 void DSA_get0_key(const DSA *d,
309 const BIGNUM **pub_key, const BIGNUM **priv_key)
310 {
311 if (pub_key != NULL)
312 *pub_key = d->pub_key;
313 if (priv_key != NULL)
314 *priv_key = d->priv_key;
315 }
316
DSA_set0_key(DSA * d,BIGNUM * pub_key,BIGNUM * priv_key)317 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
318 {
319 if (pub_key != NULL) {
320 BN_free(d->pub_key);
321 d->pub_key = pub_key;
322 }
323 if (priv_key != NULL) {
324 BN_free(d->priv_key);
325 d->priv_key = priv_key;
326 }
327 d->dirty_cnt++;
328
329 return 1;
330 }
331
DSA_security_bits(const DSA * d)332 int DSA_security_bits(const DSA *d)
333 {
334 if (d->params.p != NULL && d->params.q != NULL)
335 return BN_security_bits(BN_num_bits(d->params.p),
336 BN_num_bits(d->params.q));
337 return -1;
338 }
339
DSA_bits(const DSA * dsa)340 int DSA_bits(const DSA *dsa)
341 {
342 if (dsa->params.p != NULL)
343 return BN_num_bits(dsa->params.p);
344 return -1;
345 }
346
ossl_dsa_get0_params(DSA * dsa)347 FFC_PARAMS *ossl_dsa_get0_params(DSA *dsa)
348 {
349 return &dsa->params;
350 }
351
ossl_dsa_ffc_params_fromdata(DSA * dsa,const OSSL_PARAM params[])352 int ossl_dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[])
353 {
354 int ret;
355 FFC_PARAMS *ffc = ossl_dsa_get0_params(dsa);
356
357 ret = ossl_ffc_params_fromdata(ffc, params);
358 if (ret)
359 dsa->dirty_cnt++;
360 return ret;
361 }
362