]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dsa/dsa_lib.c
2 * Copyright 1995-2021 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"
16 #include <openssl/bn.h>
17 #include <openssl/engine.h>
18 #include "internal/cryptlib.h"
19 #include "internal/refcount.h"
20 #include "crypto/dsa.h"
21 #include "crypto/dh.h" /* required by DSA_dup_DH() */
22 #include "dsa_local.h"
24 static DSA
*dsa_new_intern(ENGINE
*engine
, OSSL_LIB_CTX
*libctx
);
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(const 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 (!ossl_ffc_params_copy(ossl_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_MODULE */
126 const DSA_METHOD
*DSA_get_method(DSA
*d
)
131 static DSA
*dsa_new_intern(ENGINE
*engine
, OSSL_LIB_CTX
*libctx
)
133 DSA
*ret
= OPENSSL_zalloc(sizeof(*ret
));
136 ERR_raise(ERR_LIB_DSA
, ERR_R_MALLOC_FAILURE
);
141 ret
->lock
= CRYPTO_THREAD_lock_new();
142 if (ret
->lock
== NULL
) {
143 ERR_raise(ERR_LIB_DSA
, ERR_R_MALLOC_FAILURE
);
148 ret
->libctx
= libctx
;
149 ret
->meth
= DSA_get_default_method();
150 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
151 ret
->flags
= ret
->meth
->flags
& ~DSA_FLAG_NON_FIPS_ALLOW
; /* early default init */
153 if (!ENGINE_init(engine
)) {
154 ERR_raise(ERR_LIB_DSA
, ERR_R_ENGINE_LIB
);
157 ret
->engine
= engine
;
159 ret
->engine
= ENGINE_get_default_DSA();
161 ret
->meth
= ENGINE_get_DSA(ret
->engine
);
162 if (ret
->meth
== NULL
) {
163 ERR_raise(ERR_LIB_DSA
, ERR_R_ENGINE_LIB
);
169 ret
->flags
= ret
->meth
->flags
& ~DSA_FLAG_NON_FIPS_ALLOW
;
172 if (!ossl_crypto_new_ex_data_ex(libctx
, CRYPTO_EX_INDEX_DSA
, ret
,
177 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
178 ERR_raise(ERR_LIB_DSA
, ERR_R_INIT_FAIL
);
189 DSA
*DSA_new_method(ENGINE
*engine
)
191 return dsa_new_intern(engine
, NULL
);
194 DSA
*ossl_dsa_new(OSSL_LIB_CTX
*libctx
)
196 return dsa_new_intern(NULL
, libctx
);
202 return dsa_new_intern(NULL
, NULL
);
206 void DSA_free(DSA
*r
)
213 CRYPTO_DOWN_REF(&r
->references
, &i
, r
->lock
);
214 REF_PRINT_COUNT("DSA", r
);
217 REF_ASSERT_ISNT(i
< 0);
219 if (r
->meth
!= NULL
&& r
->meth
->finish
!= NULL
)
221 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
222 ENGINE_finish(r
->engine
);
226 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA
, r
, &r
->ex_data
);
229 CRYPTO_THREAD_lock_free(r
->lock
);
231 ossl_ffc_params_cleanup(&r
->params
);
232 BN_clear_free(r
->pub_key
);
233 BN_clear_free(r
->priv_key
);
237 int DSA_up_ref(DSA
*r
)
241 if (CRYPTO_UP_REF(&r
->references
, &i
, r
->lock
) <= 0)
244 REF_PRINT_COUNT("DSA", r
);
245 REF_ASSERT_ISNT(i
< 2);
246 return ((i
> 1) ? 1 : 0);
249 void ossl_dsa_set0_libctx(DSA
*d
, OSSL_LIB_CTX
*libctx
)
254 void DSA_get0_pqg(const DSA
*d
,
255 const BIGNUM
**p
, const BIGNUM
**q
, const BIGNUM
**g
)
257 ossl_ffc_params_get0_pqg(&d
->params
, p
, q
, g
);
260 int DSA_set0_pqg(DSA
*d
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
262 /* If the fields p, q and g in d are NULL, the corresponding input
263 * parameters MUST be non-NULL.
265 if ((d
->params
.p
== NULL
&& p
== NULL
)
266 || (d
->params
.q
== NULL
&& q
== NULL
)
267 || (d
->params
.g
== NULL
&& g
== NULL
))
270 ossl_ffc_params_set0_pqg(&d
->params
, p
, q
, g
);
276 const BIGNUM
*DSA_get0_p(const DSA
*d
)
281 const BIGNUM
*DSA_get0_q(const DSA
*d
)
286 const BIGNUM
*DSA_get0_g(const DSA
*d
)
291 const BIGNUM
*DSA_get0_pub_key(const DSA
*d
)
296 const BIGNUM
*DSA_get0_priv_key(const DSA
*d
)
301 void DSA_get0_key(const DSA
*d
,
302 const BIGNUM
**pub_key
, const BIGNUM
**priv_key
)
305 *pub_key
= d
->pub_key
;
306 if (priv_key
!= NULL
)
307 *priv_key
= d
->priv_key
;
310 int DSA_set0_key(DSA
*d
, BIGNUM
*pub_key
, BIGNUM
*priv_key
)
312 if (pub_key
!= NULL
) {
314 d
->pub_key
= pub_key
;
316 if (priv_key
!= NULL
) {
317 BN_free(d
->priv_key
);
318 d
->priv_key
= priv_key
;
325 int DSA_security_bits(const DSA
*d
)
327 if (d
->params
.p
!= NULL
&& d
->params
.q
!= NULL
)
328 return BN_security_bits(BN_num_bits(d
->params
.p
),
329 BN_num_bits(d
->params
.q
));
333 int DSA_bits(const DSA
*dsa
)
335 if (dsa
->params
.p
!= NULL
)
336 return BN_num_bits(dsa
->params
.p
);
340 FFC_PARAMS
*ossl_dsa_get0_params(DSA
*dsa
)
345 int ossl_dsa_ffc_params_fromdata(DSA
*dsa
, const OSSL_PARAM params
[])
352 ffc
= ossl_dsa_get0_params(dsa
);
356 ret
= ossl_ffc_params_fromdata(ffc
, params
);