Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/dsa/dsa_lib.c
Line
Count
Source (jump to first uncovered 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
26
static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx);
27
28
#ifndef FIPS_MODULE
29
30
int DSA_set_ex_data(DSA *d, int idx, void *arg)
31
0
{
32
0
    return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
33
0
}
34
35
void *DSA_get_ex_data(const DSA *d, int idx)
36
0
{
37
0
    return CRYPTO_get_ex_data(&d->ex_data, idx);
38
0
}
39
40
# ifndef OPENSSL_NO_DH
41
DH *DSA_dup_DH(const DSA *r)
42
0
{
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
0
    DH *ret = NULL;
49
0
    BIGNUM *pub_key = NULL, *priv_key = NULL;
50
51
0
    if (r == NULL)
52
0
        goto err;
53
0
    ret = DH_new();
54
0
    if (ret == NULL)
55
0
        goto err;
56
57
0
    if (!ossl_ffc_params_copy(ossl_dh_get0_params(ret), &r->params))
58
0
        goto err;
59
60
0
    if (r->pub_key != NULL) {
61
0
        pub_key = BN_dup(r->pub_key);
62
0
        if (pub_key == NULL)
63
0
            goto err;
64
0
        if (r->priv_key != NULL) {
65
0
            priv_key = BN_dup(r->priv_key);
66
0
            if (priv_key == NULL)
67
0
                goto err;
68
0
        }
69
0
        if (!DH_set0_key(ret, pub_key, priv_key))
70
0
            goto err;
71
0
    } else if (r->priv_key != NULL) {
72
        /* Shouldn't happen */
73
0
        goto err;
74
0
    }
75
76
0
    return ret;
77
78
0
 err:
79
0
    BN_free(pub_key);
80
0
    BN_free(priv_key);
81
0
    DH_free(ret);
82
0
    return NULL;
83
0
}
84
# endif /*  OPENSSL_NO_DH */
85
86
void DSA_clear_flags(DSA *d, int flags)
87
0
{
88
0
    d->flags &= ~flags;
89
0
}
90
91
int DSA_test_flags(const DSA *d, int flags)
92
0
{
93
0
    return d->flags & flags;
94
0
}
95
96
void DSA_set_flags(DSA *d, int flags)
97
0
{
98
0
    d->flags |= flags;
99
0
}
100
101
ENGINE *DSA_get0_engine(DSA *d)
102
0
{
103
0
    return d->engine;
104
0
}
105
106
int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
107
0
{
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
0
    const DSA_METHOD *mtmp;
113
0
    mtmp = dsa->meth;
114
0
    if (mtmp->finish)
115
0
        mtmp->finish(dsa);
116
0
#ifndef OPENSSL_NO_ENGINE
117
0
    ENGINE_finish(dsa->engine);
118
0
    dsa->engine = NULL;
119
0
#endif
120
0
    dsa->meth = meth;
121
0
    if (meth->init)
122
0
        meth->init(dsa);
123
0
    return 1;
124
0
}
125
#endif /* FIPS_MODULE */
126
127
128
const DSA_METHOD *DSA_get_method(DSA *d)
129
90.3k
{
130
90.3k
    return d->meth;
131
90.3k
}
132
133
static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
134
200k
{
135
200k
    DSA *ret = OPENSSL_zalloc(sizeof(*ret));
136
137
200k
    if (ret == NULL)
138
0
        return NULL;
139
140
200k
    ret->lock = CRYPTO_THREAD_lock_new();
141
200k
    if (ret->lock == NULL) {
142
0
        ERR_raise(ERR_LIB_DSA, ERR_R_CRYPTO_LIB);
143
0
        OPENSSL_free(ret);
144
0
        return NULL;
145
0
    }
146
147
200k
    if (!CRYPTO_NEW_REF(&ret->references, 1)) {
148
0
        CRYPTO_THREAD_lock_free(ret->lock);
149
0
        OPENSSL_free(ret);
150
0
        return NULL;
151
0
    }
152
153
200k
    ret->libctx = libctx;
154
200k
    ret->meth = DSA_get_default_method();
155
200k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
156
200k
    ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
157
200k
    if (engine) {
158
0
        if (!ENGINE_init(engine)) {
159
0
            ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB);
160
0
            goto err;
161
0
        }
162
0
        ret->engine = engine;
163
0
    } else
164
200k
        ret->engine = ENGINE_get_default_DSA();
165
200k
    if (ret->engine) {
166
0
        ret->meth = ENGINE_get_DSA(ret->engine);
167
0
        if (ret->meth == NULL) {
168
0
            ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB);
169
0
            goto err;
170
0
        }
171
0
    }
172
200k
#endif
173
174
200k
    ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
175
176
200k
#ifndef FIPS_MODULE
177
200k
    if (!ossl_crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret,
178
200k
                                    &ret->ex_data))
179
0
        goto err;
180
200k
#endif
181
182
200k
    ossl_ffc_params_init(&ret->params);
183
184
200k
    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
185
0
        ERR_raise(ERR_LIB_DSA, ERR_R_INIT_FAIL);
186
0
        goto err;
187
0
    }
188
189
200k
    return ret;
190
191
0
 err:
192
0
    DSA_free(ret);
193
0
    return NULL;
194
200k
}
195
196
DSA *DSA_new_method(ENGINE *engine)
197
0
{
198
0
    return dsa_new_intern(engine, NULL);
199
0
}
200
201
DSA *ossl_dsa_new(OSSL_LIB_CTX *libctx)
202
51.8k
{
203
51.8k
    return dsa_new_intern(NULL, libctx);
204
51.8k
}
205
206
#ifndef FIPS_MODULE
207
DSA *DSA_new(void)
208
351k
{
209
351k
    return dsa_new_intern(NULL, NULL);
210
351k
}
211
#endif
212
213
void DSA_free(DSA *r)
214
1.09M
{
215
1.09M
    int i;
216
217
1.09M
    if (r == NULL)
218
624k
        return;
219
220
469k
    CRYPTO_DOWN_REF(&r->references, &i);
221
469k
    REF_PRINT_COUNT("DSA", i, r);
222
469k
    if (i > 0)
223
66.2k
        return;
224
403k
    REF_ASSERT_ISNT(i < 0);
225
226
403k
    if (r->meth != NULL && r->meth->finish != NULL)
227
403k
        r->meth->finish(r);
228
403k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
229
403k
    ENGINE_finish(r->engine);
230
403k
#endif
231
232
403k
#ifndef FIPS_MODULE
233
403k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
234
403k
#endif
235
236
403k
    CRYPTO_THREAD_lock_free(r->lock);
237
403k
    CRYPTO_FREE_REF(&r->references);
238
239
403k
    ossl_ffc_params_cleanup(&r->params);
240
403k
    BN_clear_free(r->pub_key);
241
403k
    BN_clear_free(r->priv_key);
242
403k
    OPENSSL_free(r);
243
403k
}
244
245
int DSA_up_ref(DSA *r)
246
66.2k
{
247
66.2k
    int i;
248
249
66.2k
    if (CRYPTO_UP_REF(&r->references, &i) <= 0)
250
0
        return 0;
251
252
66.2k
    REF_PRINT_COUNT("DSA", i, r);
253
66.2k
    REF_ASSERT_ISNT(i < 2);
254
66.2k
    return ((i > 1) ? 1 : 0);
255
66.2k
}
256
257
void ossl_dsa_set0_libctx(DSA *d, OSSL_LIB_CTX *libctx)
258
63.8k
{
259
63.8k
    d->libctx = libctx;
260
63.8k
}
261
262
void DSA_get0_pqg(const DSA *d,
263
                  const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
264
38.2k
{
265
38.2k
    ossl_ffc_params_get0_pqg(&d->params, p, q, g);
266
38.2k
}
267
268
int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
269
420
{
270
    /* If the fields p, q and g in d are NULL, the corresponding input
271
     * parameters MUST be non-NULL.
272
     */
273
420
    if ((d->params.p == NULL && p == NULL)
274
420
        || (d->params.q == NULL && q == NULL)
275
420
        || (d->params.g == NULL && g == NULL))
276
0
        return 0;
277
278
420
    ossl_ffc_params_set0_pqg(&d->params, p, q, g);
279
420
    d->dirty_cnt++;
280
281
420
    return 1;
282
420
}
283
284
const BIGNUM *DSA_get0_p(const DSA *d)
285
84.1k
{
286
84.1k
    return d->params.p;
287
84.1k
}
288
289
const BIGNUM *DSA_get0_q(const DSA *d)
290
25.4k
{
291
25.4k
    return d->params.q;
292
25.4k
}
293
294
const BIGNUM *DSA_get0_g(const DSA *d)
295
79.5k
{
296
79.5k
    return d->params.g;
297
79.5k
}
298
299
const BIGNUM *DSA_get0_pub_key(const DSA *d)
300
109k
{
301
109k
    return d->pub_key;
302
109k
}
303
304
const BIGNUM *DSA_get0_priv_key(const DSA *d)
305
55.3k
{
306
55.3k
    return d->priv_key;
307
55.3k
}
308
309
void DSA_get0_key(const DSA *d,
310
                  const BIGNUM **pub_key, const BIGNUM **priv_key)
311
92.8k
{
312
92.8k
    if (pub_key != NULL)
313
92.1k
        *pub_key = d->pub_key;
314
92.8k
    if (priv_key != NULL)
315
91.3k
        *priv_key = d->priv_key;
316
92.8k
}
317
318
int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
319
53.1k
{
320
53.1k
    if (pub_key != NULL) {
321
53.1k
        BN_free(d->pub_key);
322
53.1k
        d->pub_key = pub_key;
323
53.1k
    }
324
53.1k
    if (priv_key != NULL) {
325
53.1k
        BN_free(d->priv_key);
326
53.1k
        d->priv_key = priv_key;
327
53.1k
    }
328
53.1k
    d->dirty_cnt++;
329
330
53.1k
    return 1;
331
53.1k
}
332
333
int DSA_security_bits(const DSA *d)
334
65.2k
{
335
65.2k
    if (d->params.p != NULL && d->params.q != NULL)
336
65.2k
        return BN_security_bits(BN_num_bits(d->params.p),
337
65.2k
                                BN_num_bits(d->params.q));
338
0
    return -1;
339
65.2k
}
340
341
int DSA_bits(const DSA *dsa)
342
65.5k
{
343
65.5k
    if (dsa->params.p != NULL)
344
65.5k
        return BN_num_bits(dsa->params.p);
345
0
    return -1;
346
65.5k
}
347
348
FFC_PARAMS *ossl_dsa_get0_params(DSA *dsa)
349
249k
{
350
249k
    return &dsa->params;
351
249k
}
352
353
int ossl_dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[])
354
30.6k
{
355
30.6k
    int ret;
356
30.6k
    FFC_PARAMS *ffc = ossl_dsa_get0_params(dsa);
357
358
30.6k
    ret = ossl_ffc_params_fromdata(ffc, params);
359
30.6k
    if (ret)
360
30.6k
        dsa->dirty_cnt++;
361
30.6k
    return ret;
362
30.6k
}