]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dsa/dsa_lib.c
db6e3b059b4deebc68f9bff66c071d87ac04f865
2 * Copyright 1995-2023 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>
18 # include <openssl/engine.h>
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"
26 static DSA
*dsa_new_intern(ENGINE
*engine
, OSSL_LIB_CTX
*libctx
);
30 int DSA_set_ex_data(DSA
*d
, int idx
, void *arg
)
32 return CRYPTO_set_ex_data(&d
->ex_data
, idx
, arg
);
35 void *DSA_get_ex_data(const DSA
*d
, int idx
)
37 return CRYPTO_get_ex_data(&d
->ex_data
, idx
);
40 # ifndef OPENSSL_NO_DH
41 DH
*DSA_dup_DH(const DSA
*r
)
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.
49 BIGNUM
*pub_key
= NULL
, *priv_key
= NULL
;
57 if (!ossl_ffc_params_copy(ossl_dh_get0_params(ret
), &r
->params
))
60 if (r
->pub_key
!= NULL
) {
61 pub_key
= BN_dup(r
->pub_key
);
64 if (r
->priv_key
!= NULL
) {
65 priv_key
= BN_dup(r
->priv_key
);
69 if (!DH_set0_key(ret
, pub_key
, priv_key
))
71 } else if (r
->priv_key
!= NULL
) {
72 /* Shouldn't happen */
84 # endif /* OPENSSL_NO_DH */
86 void DSA_clear_flags(DSA
*d
, int flags
)
91 int DSA_test_flags(const DSA
*d
, int flags
)
93 return d
->flags
& flags
;
96 void DSA_set_flags(DSA
*d
, int flags
)
101 ENGINE
*DSA_get0_engine(DSA
*d
)
106 int DSA_set_method(DSA
*dsa
, const DSA_METHOD
*meth
)
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.
112 const DSA_METHOD
*mtmp
;
116 #ifndef OPENSSL_NO_ENGINE
117 ENGINE_finish(dsa
->engine
);
125 #endif /* FIPS_MODULE */
128 const DSA_METHOD
*DSA_get_method(DSA
*d
)
133 static DSA
*dsa_new_intern(ENGINE
*engine
, OSSL_LIB_CTX
*libctx
)
135 DSA
*ret
= OPENSSL_zalloc(sizeof(*ret
));
140 ret
->lock
= CRYPTO_THREAD_lock_new();
141 if (ret
->lock
== NULL
) {
142 ERR_raise(ERR_LIB_DSA
, ERR_R_CRYPTO_LIB
);
147 if (!CRYPTO_NEW_REF(&ret
->references
, 1)) {
148 CRYPTO_THREAD_lock_free(ret
->lock
);
153 ret
->libctx
= libctx
;
154 ret
->meth
= DSA_get_default_method();
155 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
156 ret
->flags
= ret
->meth
->flags
& ~DSA_FLAG_NON_FIPS_ALLOW
; /* early default init */
158 if (!ENGINE_init(engine
)) {
159 ERR_raise(ERR_LIB_DSA
, ERR_R_ENGINE_LIB
);
162 ret
->engine
= engine
;
164 ret
->engine
= ENGINE_get_default_DSA();
166 ret
->meth
= ENGINE_get_DSA(ret
->engine
);
167 if (ret
->meth
== NULL
) {
168 ERR_raise(ERR_LIB_DSA
, ERR_R_ENGINE_LIB
);
174 ret
->flags
= ret
->meth
->flags
& ~DSA_FLAG_NON_FIPS_ALLOW
;
177 if (!ossl_crypto_new_ex_data_ex(libctx
, CRYPTO_EX_INDEX_DSA
, ret
,
182 ossl_ffc_params_init(&ret
->params
);
184 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
185 ERR_raise(ERR_LIB_DSA
, ERR_R_INIT_FAIL
);
196 DSA
*DSA_new_method(ENGINE
*engine
)
198 return dsa_new_intern(engine
, NULL
);
201 DSA
*ossl_dsa_new(OSSL_LIB_CTX
*libctx
)
203 return dsa_new_intern(NULL
, libctx
);
209 return dsa_new_intern(NULL
, NULL
);
213 void DSA_free(DSA
*r
)
220 CRYPTO_DOWN_REF(&r
->references
, &i
);
221 REF_PRINT_COUNT("DSA", i
, r
);
224 REF_ASSERT_ISNT(i
< 0);
226 if (r
->meth
!= NULL
&& r
->meth
->finish
!= NULL
)
228 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
229 ENGINE_finish(r
->engine
);
233 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA
, r
, &r
->ex_data
);
236 CRYPTO_THREAD_lock_free(r
->lock
);
237 CRYPTO_FREE_REF(&r
->references
);
239 ossl_ffc_params_cleanup(&r
->params
);
240 BN_clear_free(r
->pub_key
);
241 BN_clear_free(r
->priv_key
);
245 int DSA_up_ref(DSA
*r
)
249 if (CRYPTO_UP_REF(&r
->references
, &i
) <= 0)
252 REF_PRINT_COUNT("DSA", i
, r
);
253 REF_ASSERT_ISNT(i
< 2);
254 return ((i
> 1) ? 1 : 0);
257 void ossl_dsa_set0_libctx(DSA
*d
, OSSL_LIB_CTX
*libctx
)
262 void DSA_get0_pqg(const DSA
*d
,
263 const BIGNUM
**p
, const BIGNUM
**q
, const BIGNUM
**g
)
265 ossl_ffc_params_get0_pqg(&d
->params
, p
, q
, g
);
268 int DSA_set0_pqg(DSA
*d
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
270 /* If the fields p, q and g in d are NULL, the corresponding input
271 * parameters MUST be non-NULL.
273 if ((d
->params
.p
== NULL
&& p
== NULL
)
274 || (d
->params
.q
== NULL
&& q
== NULL
)
275 || (d
->params
.g
== NULL
&& g
== NULL
))
278 ossl_ffc_params_set0_pqg(&d
->params
, p
, q
, g
);
284 const BIGNUM
*DSA_get0_p(const DSA
*d
)
289 const BIGNUM
*DSA_get0_q(const DSA
*d
)
294 const BIGNUM
*DSA_get0_g(const DSA
*d
)
299 const BIGNUM
*DSA_get0_pub_key(const DSA
*d
)
304 const BIGNUM
*DSA_get0_priv_key(const DSA
*d
)
309 void DSA_get0_key(const DSA
*d
,
310 const BIGNUM
**pub_key
, const BIGNUM
**priv_key
)
313 *pub_key
= d
->pub_key
;
314 if (priv_key
!= NULL
)
315 *priv_key
= d
->priv_key
;
318 int DSA_set0_key(DSA
*d
, BIGNUM
*pub_key
, BIGNUM
*priv_key
)
320 if (pub_key
!= NULL
) {
322 d
->pub_key
= pub_key
;
324 if (priv_key
!= NULL
) {
325 BN_free(d
->priv_key
);
326 d
->priv_key
= priv_key
;
333 int DSA_security_bits(const DSA
*d
)
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
));
341 int DSA_bits(const DSA
*dsa
)
343 if (dsa
->params
.p
!= NULL
)
344 return BN_num_bits(dsa
->params
.p
);
348 FFC_PARAMS
*ossl_dsa_get0_params(DSA
*dsa
)
353 int ossl_dsa_ffc_params_fromdata(DSA
*dsa
, const OSSL_PARAM params
[])
356 FFC_PARAMS
*ffc
= ossl_dsa_get0_params(dsa
);
358 ret
= ossl_ffc_params_fromdata(ffc
, params
);