2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include "internal/refcount.h"
19 #include <openssl/bn.h>
20 #include <openssl/asn1.h>
21 #include <openssl/engine.h>
22 #include "dsa_local.h"
23 #include "crypto/dsa.h"
24 #include "crypto/dh.h" /* required by DSA_dup_DH() */
28 int DSA_set_ex_data(DSA *d, int idx, void *arg)
30 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
33 void *DSA_get_ex_data(DSA *d, int idx)
35 return CRYPTO_get_ex_data(&d->ex_data, idx);
38 # ifndef OPENSSL_NO_DH
39 DH *DSA_dup_DH(const DSA *r)
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.
47 BIGNUM *pub_key = NULL, *priv_key = NULL;
55 if (!ffc_params_copy(dh_get0_params(ret), &r->params))
58 if (r->pub_key != NULL) {
59 pub_key = BN_dup(r->pub_key);
62 if (r->priv_key != NULL) {
63 priv_key = BN_dup(r->priv_key);
67 if (!DH_set0_key(ret, pub_key, priv_key))
69 } else if (r->priv_key != NULL) {
70 /* Shouldn't happen */
82 # endif /* OPENSSL_NO_DH */
84 void DSA_clear_flags(DSA *d, int flags)
89 int DSA_test_flags(const DSA *d, int flags)
91 return d->flags & flags;
94 void DSA_set_flags(DSA *d, int flags)
99 ENGINE *DSA_get0_engine(DSA *d)
104 int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
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.
110 const DSA_METHOD *mtmp;
114 #ifndef OPENSSL_NO_ENGINE
115 ENGINE_finish(dsa->engine);
123 #endif /* FIPS_MODE */
126 const DSA_METHOD *DSA_get_method(DSA *d)
131 static DSA *dsa_new_method(OPENSSL_CTX *libctx, ENGINE *engine)
133 DSA *ret = OPENSSL_zalloc(sizeof(*ret));
136 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
141 ret->lock = CRYPTO_THREAD_lock_new();
142 if (ret->lock == NULL) {
143 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
148 ret->meth = DSA_get_default_method();
149 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
150 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
152 if (!ENGINE_init(engine)) {
153 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
156 ret->engine = engine;
158 ret->engine = ENGINE_get_default_DSA();
160 ret->meth = ENGINE_get_DSA(ret->engine);
161 if (ret->meth == NULL) {
162 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
168 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
171 if (!crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
175 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
176 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL);
187 DSA *DSA_new_method(ENGINE *engine)
189 return dsa_new_method(NULL, engine);
194 return DSA_new_method(NULL);
197 void DSA_free(DSA *r)
204 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
205 REF_PRINT_COUNT("DSA", r);
208 REF_ASSERT_ISNT(i < 0);
210 if (r->meth != NULL && r->meth->finish != NULL)
212 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
213 ENGINE_finish(r->engine);
217 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
220 CRYPTO_THREAD_lock_free(r->lock);
222 ffc_params_cleanup(&r->params);
223 BN_clear_free(r->pub_key);
224 BN_clear_free(r->priv_key);
228 int DSA_up_ref(DSA *r)
232 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
235 REF_PRINT_COUNT("DSA", r);
236 REF_ASSERT_ISNT(i < 2);
237 return ((i > 1) ? 1 : 0);
240 void DSA_get0_pqg(const DSA *d,
241 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
243 ffc_params_get0_pqg(&d->params, p, q, g);
246 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
248 /* If the fields p, q and g in d are NULL, the corresponding input
249 * parameters MUST be non-NULL.
251 if ((d->params.p == NULL && p == NULL)
252 || (d->params.q == NULL && q == NULL)
253 || (d->params.g == NULL && g == NULL))
256 ffc_params_set0_pqg(&d->params, p, q, g);
262 const BIGNUM *DSA_get0_p(const DSA *d)
267 const BIGNUM *DSA_get0_q(const DSA *d)
272 const BIGNUM *DSA_get0_g(const DSA *d)
277 const BIGNUM *DSA_get0_pub_key(const DSA *d)
282 const BIGNUM *DSA_get0_priv_key(const DSA *d)
287 void DSA_get0_key(const DSA *d,
288 const BIGNUM **pub_key, const BIGNUM **priv_key)
291 *pub_key = d->pub_key;
292 if (priv_key != NULL)
293 *priv_key = d->priv_key;
296 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
298 /* If the field pub_key in d is NULL, the corresponding input
299 * parameters MUST be non-NULL. The priv_key field may
302 if (d->pub_key == NULL && pub_key == NULL)
305 if (pub_key != NULL) {
307 d->pub_key = pub_key;
309 if (priv_key != NULL) {
310 BN_free(d->priv_key);
311 d->priv_key = priv_key;
318 int DSA_security_bits(const DSA *d)
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));
326 int DSA_bits(const DSA *dsa)
328 return BN_num_bits(dsa->params.p);