]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dsa/dsa_lib.c
Stop raising ERR_R_MALLOC_FAILURE in most places
[thirdparty/openssl.git] / crypto / dsa / dsa_lib.c
CommitLineData
d2e9e320 1/*
4333b89f 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
3cdbea65 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
d2e9e320
RS
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
d02b48c6
RE
8 */
9
f41ac0ee
P
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
ec577822 16#include <openssl/bn.h>
3f773c91
TM
17#ifndef FIPS_MODULE
18# include <openssl/engine.h>
19#endif
53cefef6
TM
20#include "internal/cryptlib.h"
21#include "internal/refcount.h"
e683582b 22#include "crypto/dsa.h"
dc8de3e6 23#include "crypto/dh.h" /* required by DSA_dup_DH() */
53cefef6 24#include "dsa_local.h"
e683582b 25
b4250010 26static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx);
8083fd3a 27
f844f9eb 28#ifndef FIPS_MODULE
d02b48c6 29
e683582b
SL
30int DSA_set_ex_data(DSA *d, int idx, void *arg)
31{
32 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
33}
34
8cc86b81 35void *DSA_get_ex_data(const DSA *d, int idx)
e683582b
SL
36{
37 return CRYPTO_get_ex_data(&d->ex_data, idx);
38}
39
dc8de3e6 40# ifndef OPENSSL_NO_DH
e683582b
SL
41DH *DSA_dup_DH(const DSA *r)
42{
43 /*
dc8de3e6
SL
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.
e683582b 47 */
e683582b 48 DH *ret = NULL;
dc8de3e6 49 BIGNUM *pub_key = NULL, *priv_key = NULL;
e683582b
SL
50
51 if (r == NULL)
52 goto err;
53 ret = DH_new();
54 if (ret == NULL)
55 goto err;
dc8de3e6 56
19dbb742 57 if (!ossl_ffc_params_copy(ossl_dh_get0_params(ret), &r->params))
dc8de3e6 58 goto err;
e683582b
SL
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:
e683582b
SL
79 BN_free(pub_key);
80 BN_free(priv_key);
81 DH_free(ret);
82 return NULL;
83}
dc8de3e6 84# endif /* OPENSSL_NO_DH */
e683582b 85
e683582b
SL
86void DSA_clear_flags(DSA *d, int flags)
87{
88 d->flags &= ~flags;
89}
90
91int DSA_test_flags(const DSA *d, int flags)
92{
93 return d->flags & flags;
94}
95
96void DSA_set_flags(DSA *d, int flags)
97{
98 d->flags |= flags;
99}
100
101ENGINE *DSA_get0_engine(DSA *d)
102{
103 return d->engine;
104}
105
cb78486d 106int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
0f113f3e
MC
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);
0b13e9f0 116#ifndef OPENSSL_NO_ENGINE
7c96dbcd
RS
117 ENGINE_finish(dsa->engine);
118 dsa->engine = NULL;
0b13e9f0 119#endif
0f113f3e
MC
120 dsa->meth = meth;
121 if (meth->init)
122 meth->init(dsa);
123 return 1;
124}
f844f9eb 125#endif /* FIPS_MODULE */
e683582b 126
c0711f7f 127
6e9fa57c
MC
128const DSA_METHOD *DSA_get_method(DSA *d)
129{
130 return d->meth;
131}
132
b4250010 133static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
0f113f3e 134{
2bbf0baa 135 DSA *ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e 136
e077455e 137 if (ret == NULL)
d188a536 138 return NULL;
2bbf0baa
F
139
140 ret->references = 1;
141 ret->lock = CRYPTO_THREAD_lock_new();
142 if (ret->lock == NULL) {
e077455e 143 ERR_raise(ERR_LIB_DSA, ERR_R_CRYPTO_LIB);
2bbf0baa
F
144 OPENSSL_free(ret);
145 return NULL;
146 }
147
8083fd3a 148 ret->libctx = libctx;
0f113f3e 149 ret->meth = DSA_get_default_method();
f844f9eb 150#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
2bbf0baa 151 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
0f113f3e
MC
152 if (engine) {
153 if (!ENGINE_init(engine)) {
9311d0c4 154 ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB);
2bbf0baa 155 goto err;
0f113f3e
MC
156 }
157 ret->engine = engine;
158 } else
159 ret->engine = ENGINE_get_default_DSA();
160 if (ret->engine) {
161 ret->meth = ENGINE_get_DSA(ret->engine);
7c96dbcd 162 if (ret->meth == NULL) {
9311d0c4 163 ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB);
2bbf0baa 164 goto err;
0f113f3e
MC
165 }
166 }
0b13e9f0 167#endif
0c9de428 168
0f113f3e 169 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
d188a536 170
f844f9eb 171#ifndef FIPS_MODULE
e4bec869
SL
172 if (!ossl_crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret,
173 &ret->ex_data))
2bbf0baa 174 goto err;
a3327784 175#endif
d188a536
AG
176
177 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
9311d0c4 178 ERR_raise(ERR_LIB_DSA, ERR_R_INIT_FAIL);
544648a8 179 goto err;
0f113f3e
MC
180 }
181
d188a536 182 return ret;
544648a8
NT
183
184 err:
185 DSA_free(ret);
186 return NULL;
0f113f3e 187}
d02b48c6 188
e683582b
SL
189DSA *DSA_new_method(ENGINE *engine)
190{
8083fd3a
SL
191 return dsa_new_intern(engine, NULL);
192}
193
5af02212 194DSA *ossl_dsa_new(OSSL_LIB_CTX *libctx)
8083fd3a
SL
195{
196 return dsa_new_intern(NULL, libctx);
e683582b
SL
197}
198
f844f9eb 199#ifndef FIPS_MODULE
a3327784 200DSA *DSA_new(void)
e683582b 201{
8083fd3a 202 return dsa_new_intern(NULL, NULL);
e683582b 203}
8083fd3a 204#endif
e683582b 205
6b691a5c 206void DSA_free(DSA *r)
0f113f3e
MC
207{
208 int i;
d02b48c6 209
0f113f3e
MC
210 if (r == NULL)
211 return;
d02b48c6 212
2f545ae4 213 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
f3f1cf84 214 REF_PRINT_COUNT("DSA", r);
0f113f3e
MC
215 if (i > 0)
216 return;
f3f1cf84 217 REF_ASSERT_ISNT(i < 0);
d02b48c6 218
0c5d725e 219 if (r->meth != NULL && r->meth->finish != NULL)
0f113f3e 220 r->meth->finish(r);
f844f9eb 221#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
17fa4e8e 222 ENGINE_finish(r->engine);
0b13e9f0 223#endif
c0711f7f 224
f844f9eb 225#ifndef FIPS_MODULE
0f113f3e 226 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
a3327784 227#endif
0f113f3e 228
d188a536
AG
229 CRYPTO_THREAD_lock_free(r->lock);
230
5357c106 231 ossl_ffc_params_cleanup(&r->params);
23a1d5e9
RS
232 BN_clear_free(r->pub_key);
233 BN_clear_free(r->priv_key);
0f113f3e
MC
234 OPENSSL_free(r);
235}
d02b48c6 236
6ac4e8bd 237int DSA_up_ref(DSA *r)
0f113f3e 238{
d188a536
AG
239 int i;
240
2f545ae4 241 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
d188a536 242 return 0;
f3f1cf84
RS
243
244 REF_PRINT_COUNT("DSA", r);
245 REF_ASSERT_ISNT(i < 2);
0f113f3e
MC
246 return ((i > 1) ? 1 : 0);
247}
5cbc2e8b 248
6963979f
RL
249void ossl_dsa_set0_libctx(DSA *d, OSSL_LIB_CTX *libctx)
250{
251 d->libctx = libctx;
252}
253
fd809cfd
RL
254void DSA_get0_pqg(const DSA *d,
255 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
1258396d 256{
5357c106 257 ossl_ffc_params_get0_pqg(&d->params, p, q, g);
1258396d
MC
258}
259
260int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
261{
fd809cfd 262 /* If the fields p, q and g in d are NULL, the corresponding input
1da12e34 263 * parameters MUST be non-NULL.
1da12e34 264 */
dc8de3e6
SL
265 if ((d->params.p == NULL && p == NULL)
266 || (d->params.q == NULL && q == NULL)
267 || (d->params.g == NULL && g == NULL))
1258396d 268 return 0;
1da12e34 269
5357c106 270 ossl_ffc_params_set0_pqg(&d->params, p, q, g);
4889dadc 271 d->dirty_cnt++;
1258396d
MC
272
273 return 1;
274}
275
b305452f
RL
276const BIGNUM *DSA_get0_p(const DSA *d)
277{
278 return d->params.p;
279}
280
281const BIGNUM *DSA_get0_q(const DSA *d)
282{
283 return d->params.q;
284}
285
286const BIGNUM *DSA_get0_g(const DSA *d)
287{
288 return d->params.g;
289}
290
291const BIGNUM *DSA_get0_pub_key(const DSA *d)
292{
293 return d->pub_key;
294}
295
296const BIGNUM *DSA_get0_priv_key(const DSA *d)
297{
298 return d->priv_key;
299}
300
fd809cfd
RL
301void DSA_get0_key(const DSA *d,
302 const BIGNUM **pub_key, const BIGNUM **priv_key)
1258396d 303{
6e9fa57c
MC
304 if (pub_key != NULL)
305 *pub_key = d->pub_key;
306 if (priv_key != NULL)
307 *priv_key = d->priv_key;
1258396d
MC
308}
309
6e9fa57c 310int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
1258396d 311{
1da12e34
RL
312 if (pub_key != NULL) {
313 BN_free(d->pub_key);
314 d->pub_key = pub_key;
315 }
316 if (priv_key != NULL) {
317 BN_free(d->priv_key);
318 d->priv_key = priv_key;
319 }
4889dadc 320 d->dirty_cnt++;
1258396d
MC
321
322 return 1;
323}
324
806253f3
RL
325int DSA_security_bits(const DSA *d)
326{
dc8de3e6
SL
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));
806253f3
RL
330 return -1;
331}
332
333int DSA_bits(const DSA *dsa)
334{
ecfbe2f0
RL
335 if (dsa->params.p != NULL)
336 return BN_num_bits(dsa->params.p);
337 return -1;
806253f3 338}
2888fc15 339
5af02212 340FFC_PARAMS *ossl_dsa_get0_params(DSA *dsa)
2888fc15
RL
341{
342 return &dsa->params;
343}
b03ec3b5 344
5af02212 345int ossl_dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[])
b03ec3b5
SL
346{
347 int ret;
348 FFC_PARAMS *ffc;
349
350 if (dsa == NULL)
351 return 0;
5af02212 352 ffc = ossl_dsa_get0_params(dsa);
b03ec3b5
SL
353 if (ffc == NULL)
354 return 0;
355
5357c106 356 ret = ossl_ffc_params_fromdata(ffc, params);
b03ec3b5
SL
357 if (ret)
358 dsa->dirty_cnt++;
359 return ret;
360}