VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.5/crypto/dsa/dsa_lib.c@ 104078

Last change on this file since 104078 was 104078, checked in by vboxsync, 11 months ago

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

File size: 7.6 KB
Line 
1/*
2 * Copyright 1995-2023 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 <openssl/bn.h>
17#ifndef FIPS_MODULE
18# include <openssl/engine.h>
19#endif
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"
25
26static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx);
27
28#ifndef FIPS_MODULE
29
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
35void *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
41DH *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 (!ossl_ffc_params_copy(ossl_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
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
106int 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_MODULE */
126
127
128const DSA_METHOD *DSA_get_method(DSA *d)
129{
130 return d->meth;
131}
132
133static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
134{
135 DSA *ret = OPENSSL_zalloc(sizeof(*ret));
136
137 if (ret == NULL) {
138 ERR_raise(ERR_LIB_DSA, 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 ERR_raise(ERR_LIB_DSA, 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_MODULE) && !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 ERR_raise(ERR_LIB_DSA, 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 ERR_raise(ERR_LIB_DSA, 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_MODULE
174 if (!ossl_crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret,
175 &ret->ex_data))
176 goto err;
177#endif
178
179 ossl_ffc_params_init(&ret->params);
180
181 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
182 ERR_raise(ERR_LIB_DSA, ERR_R_INIT_FAIL);
183 goto err;
184 }
185
186 return ret;
187
188 err:
189 DSA_free(ret);
190 return NULL;
191}
192
193DSA *DSA_new_method(ENGINE *engine)
194{
195 return dsa_new_intern(engine, NULL);
196}
197
198DSA *ossl_dsa_new(OSSL_LIB_CTX *libctx)
199{
200 return dsa_new_intern(NULL, libctx);
201}
202
203#ifndef FIPS_MODULE
204DSA *DSA_new(void)
205{
206 return dsa_new_intern(NULL, NULL);
207}
208#endif
209
210void DSA_free(DSA *r)
211{
212 int i;
213
214 if (r == NULL)
215 return;
216
217 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
218 REF_PRINT_COUNT("DSA", r);
219 if (i > 0)
220 return;
221 REF_ASSERT_ISNT(i < 0);
222
223 if (r->meth != NULL && r->meth->finish != NULL)
224 r->meth->finish(r);
225#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
226 ENGINE_finish(r->engine);
227#endif
228
229#ifndef FIPS_MODULE
230 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
231#endif
232
233 CRYPTO_THREAD_lock_free(r->lock);
234
235 ossl_ffc_params_cleanup(&r->params);
236 BN_clear_free(r->pub_key);
237 BN_clear_free(r->priv_key);
238 OPENSSL_free(r);
239}
240
241int DSA_up_ref(DSA *r)
242{
243 int i;
244
245 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
246 return 0;
247
248 REF_PRINT_COUNT("DSA", r);
249 REF_ASSERT_ISNT(i < 2);
250 return ((i > 1) ? 1 : 0);
251}
252
253void ossl_dsa_set0_libctx(DSA *d, OSSL_LIB_CTX *libctx)
254{
255 d->libctx = libctx;
256}
257
258void DSA_get0_pqg(const DSA *d,
259 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
260{
261 ossl_ffc_params_get0_pqg(&d->params, p, q, g);
262}
263
264int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
265{
266 /* If the fields p, q and g in d are NULL, the corresponding input
267 * parameters MUST be non-NULL.
268 */
269 if ((d->params.p == NULL && p == NULL)
270 || (d->params.q == NULL && q == NULL)
271 || (d->params.g == NULL && g == NULL))
272 return 0;
273
274 ossl_ffc_params_set0_pqg(&d->params, p, q, g);
275 d->dirty_cnt++;
276
277 return 1;
278}
279
280const BIGNUM *DSA_get0_p(const DSA *d)
281{
282 return d->params.p;
283}
284
285const BIGNUM *DSA_get0_q(const DSA *d)
286{
287 return d->params.q;
288}
289
290const BIGNUM *DSA_get0_g(const DSA *d)
291{
292 return d->params.g;
293}
294
295const BIGNUM *DSA_get0_pub_key(const DSA *d)
296{
297 return d->pub_key;
298}
299
300const BIGNUM *DSA_get0_priv_key(const DSA *d)
301{
302 return d->priv_key;
303}
304
305void DSA_get0_key(const DSA *d,
306 const BIGNUM **pub_key, const BIGNUM **priv_key)
307{
308 if (pub_key != NULL)
309 *pub_key = d->pub_key;
310 if (priv_key != NULL)
311 *priv_key = d->priv_key;
312}
313
314int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
315{
316 if (pub_key != NULL) {
317 BN_free(d->pub_key);
318 d->pub_key = pub_key;
319 }
320 if (priv_key != NULL) {
321 BN_free(d->priv_key);
322 d->priv_key = priv_key;
323 }
324 d->dirty_cnt++;
325
326 return 1;
327}
328
329int DSA_security_bits(const DSA *d)
330{
331 if (d->params.p != NULL && d->params.q != NULL)
332 return BN_security_bits(BN_num_bits(d->params.p),
333 BN_num_bits(d->params.q));
334 return -1;
335}
336
337int DSA_bits(const DSA *dsa)
338{
339 if (dsa->params.p != NULL)
340 return BN_num_bits(dsa->params.p);
341 return -1;
342}
343
344FFC_PARAMS *ossl_dsa_get0_params(DSA *dsa)
345{
346 return &dsa->params;
347}
348
349int ossl_dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[])
350{
351 int ret;
352 FFC_PARAMS *ffc;
353
354 if (dsa == NULL)
355 return 0;
356 ffc = ossl_dsa_get0_params(dsa);
357 if (ffc == NULL)
358 return 0;
359
360 ret = ossl_ffc_params_fromdata(ffc, params);
361 if (ret)
362 dsa->dirty_cnt++;
363 return ret;
364}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette