]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dsa/dsa_lib.c
Make sure we always send an alert in libssl if we hit a fatal error
[thirdparty/openssl.git] / crypto / dsa / dsa_lib.c
1 /*
2 * Copyright 1995-2020 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 <stdio.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() */
25
26 static DSA *dsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
27
28 #ifndef FIPS_MODE
29
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
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
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 (!ffc_params_copy(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
86 void DSA_clear_flags(DSA *d, int flags)
87 {
88 d->flags &= ~flags;
89 }
90
91 int DSA_test_flags(const DSA *d, int flags)
92 {
93 return d->flags & flags;
94 }
95
96 void DSA_set_flags(DSA *d, int flags)
97 {
98 d->flags |= flags;
99 }
100
101 ENGINE *DSA_get0_engine(DSA *d)
102 {
103 return d->engine;
104 }
105
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_MODE */
126
127
128 const DSA_METHOD *DSA_get_method(DSA *d)
129 {
130 return d->meth;
131 }
132
133 static DSA *dsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
134 {
135 DSA *ret = OPENSSL_zalloc(sizeof(*ret));
136
137 if (ret == NULL) {
138 DSAerr(0, ERR_R_MALLOC_FAILURE);
139 return NULL;
140 }
141
142 ret->references = 1;
143 ret->lock = CRYPTO_THREAD_lock_new();
144 if (ret->lock == NULL) {
145 DSAerr(0, ERR_R_MALLOC_FAILURE);
146 OPENSSL_free(ret);
147 return NULL;
148 }
149
150 ret->libctx = libctx;
151 ret->meth = DSA_get_default_method();
152 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
153 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
154 if (engine) {
155 if (!ENGINE_init(engine)) {
156 DSAerr(0, ERR_R_ENGINE_LIB);
157 goto err;
158 }
159 ret->engine = engine;
160 } else
161 ret->engine = ENGINE_get_default_DSA();
162 if (ret->engine) {
163 ret->meth = ENGINE_get_DSA(ret->engine);
164 if (ret->meth == NULL) {
165 DSAerr(0, ERR_R_ENGINE_LIB);
166 goto err;
167 }
168 }
169 #endif
170
171 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
172
173 #ifndef FIPS_MODE
174 if (!crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
175 goto err;
176 #endif
177
178 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
179 DSAerr(0, ERR_R_INIT_FAIL);
180 goto err;
181 }
182
183 return ret;
184
185 err:
186 DSA_free(ret);
187 return NULL;
188 }
189
190 DSA *DSA_new_method(ENGINE *engine)
191 {
192 return dsa_new_intern(engine, NULL);
193 }
194
195 DSA *dsa_new_with_ctx(OPENSSL_CTX *libctx)
196 {
197 return dsa_new_intern(NULL, libctx);
198 }
199
200 #ifndef FIPS_MODE
201 DSA *DSA_new(void)
202 {
203 return dsa_new_intern(NULL, NULL);
204 }
205 #endif
206
207 void DSA_free(DSA *r)
208 {
209 int i;
210
211 if (r == NULL)
212 return;
213
214 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
215 REF_PRINT_COUNT("DSA", r);
216 if (i > 0)
217 return;
218 REF_ASSERT_ISNT(i < 0);
219
220 if (r->meth != NULL && r->meth->finish != NULL)
221 r->meth->finish(r);
222 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
223 ENGINE_finish(r->engine);
224 #endif
225
226 #ifndef FIPS_MODE
227 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
228 #endif
229
230 CRYPTO_THREAD_lock_free(r->lock);
231
232 ffc_params_cleanup(&r->params);
233 BN_clear_free(r->pub_key);
234 BN_clear_free(r->priv_key);
235 OPENSSL_free(r);
236 }
237
238 int DSA_up_ref(DSA *r)
239 {
240 int i;
241
242 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
243 return 0;
244
245 REF_PRINT_COUNT("DSA", r);
246 REF_ASSERT_ISNT(i < 2);
247 return ((i > 1) ? 1 : 0);
248 }
249
250 void DSA_get0_pqg(const DSA *d,
251 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
252 {
253 ffc_params_get0_pqg(&d->params, p, q, g);
254 }
255
256 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
257 {
258 /* If the fields p, q and g in d are NULL, the corresponding input
259 * parameters MUST be non-NULL.
260 */
261 if ((d->params.p == NULL && p == NULL)
262 || (d->params.q == NULL && q == NULL)
263 || (d->params.g == NULL && g == NULL))
264 return 0;
265
266 ffc_params_set0_pqg(&d->params, p, q, g);
267 d->dirty_cnt++;
268
269 return 1;
270 }
271
272 const BIGNUM *DSA_get0_p(const DSA *d)
273 {
274 return d->params.p;
275 }
276
277 const BIGNUM *DSA_get0_q(const DSA *d)
278 {
279 return d->params.q;
280 }
281
282 const BIGNUM *DSA_get0_g(const DSA *d)
283 {
284 return d->params.g;
285 }
286
287 const BIGNUM *DSA_get0_pub_key(const DSA *d)
288 {
289 return d->pub_key;
290 }
291
292 const BIGNUM *DSA_get0_priv_key(const DSA *d)
293 {
294 return d->priv_key;
295 }
296
297 void DSA_get0_key(const DSA *d,
298 const BIGNUM **pub_key, const BIGNUM **priv_key)
299 {
300 if (pub_key != NULL)
301 *pub_key = d->pub_key;
302 if (priv_key != NULL)
303 *priv_key = d->priv_key;
304 }
305
306 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
307 {
308 /* If the field pub_key in d is NULL, the corresponding input
309 * parameters MUST be non-NULL. The priv_key field may
310 * be left NULL.
311 */
312 if (d->pub_key == NULL && pub_key == NULL)
313 return 0;
314
315 if (pub_key != NULL) {
316 BN_free(d->pub_key);
317 d->pub_key = pub_key;
318 }
319 if (priv_key != NULL) {
320 BN_free(d->priv_key);
321 d->priv_key = priv_key;
322 }
323 d->dirty_cnt++;
324
325 return 1;
326 }
327
328 int DSA_security_bits(const DSA *d)
329 {
330 if (d->params.p != NULL && d->params.q != NULL)
331 return BN_security_bits(BN_num_bits(d->params.p),
332 BN_num_bits(d->params.q));
333 return -1;
334 }
335
336 int DSA_bits(const DSA *dsa)
337 {
338 return BN_num_bits(dsa->params.p);
339 }
340
341 FFC_PARAMS *dsa_get0_params(DSA *dsa)
342 {
343 return &dsa->params;
344 }