Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/dsa/dsa_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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
#include "internal/cryptlib.h"
18
#include "internal/refcount.h"
19
#include "internal/common.h"
20
#include "crypto/dsa.h"
21
#include "crypto/dh.h" /* required by DSA_dup_DH() */
22
#include "dsa_local.h"
23
24
static DSA *dsa_new_intern(OSSL_LIB_CTX *libctx);
25
26
#ifndef FIPS_MODULE
27
28
int DSA_set_ex_data(DSA *d, int idx, void *arg)
29
0
{
30
0
    return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
31
0
}
32
33
void *DSA_get_ex_data(const DSA *d, int idx)
34
0
{
35
0
    return CRYPTO_get_ex_data(&d->ex_data, idx);
36
0
}
37
38
#ifndef OPENSSL_NO_DH
39
DH *DSA_dup_DH(const DSA *r)
40
0
{
41
    /*
42
     * DSA has p, q, g, optional pub_key, optional priv_key.
43
     * DH has p, optional length, g, optional pub_key,
44
     * optional priv_key, optional q.
45
     */
46
0
    DH *ret = NULL;
47
0
    BIGNUM *pub_key = NULL, *priv_key = NULL;
48
49
0
    if (r == NULL)
50
0
        goto err;
51
0
    ret = DH_new();
52
0
    if (ret == NULL)
53
0
        goto err;
54
55
0
    if (!ossl_ffc_params_copy(ossl_dh_get0_params(ret), &r->params))
56
0
        goto err;
57
58
0
    if (r->pub_key != NULL) {
59
0
        pub_key = BN_dup(r->pub_key);
60
0
        if (pub_key == NULL)
61
0
            goto err;
62
0
        if (r->priv_key != NULL) {
63
0
            priv_key = BN_dup(r->priv_key);
64
0
            if (priv_key == NULL)
65
0
                goto err;
66
0
        }
67
0
        if (!DH_set0_key(ret, pub_key, priv_key))
68
0
            goto err;
69
0
    } else if (r->priv_key != NULL) {
70
        /* Shouldn't happen */
71
0
        goto err;
72
0
    }
73
74
0
    return ret;
75
76
0
err:
77
0
    BN_free(pub_key);
78
0
    BN_free(priv_key);
79
0
    DH_free(ret);
80
0
    return NULL;
81
0
}
82
#endif /*  OPENSSL_NO_DH */
83
84
void DSA_clear_flags(DSA *d, int flags)
85
0
{
86
0
    d->flags &= ~flags;
87
0
}
88
89
int DSA_test_flags(const DSA *d, int flags)
90
0
{
91
0
    return d->flags & flags;
92
0
}
93
94
void DSA_set_flags(DSA *d, int flags)
95
0
{
96
0
    d->flags |= flags;
97
0
}
98
99
int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
100
0
{
101
    /*
102
     * NB: The caller is specifically setting a method, so it's not up to us
103
     * to deal with which ENGINE it comes from.
104
     */
105
0
    const DSA_METHOD *mtmp;
106
0
    mtmp = dsa->meth;
107
0
    if (mtmp->finish)
108
0
        mtmp->finish(dsa);
109
0
    dsa->meth = meth;
110
0
    if (meth->init)
111
0
        meth->init(dsa);
112
0
    return 1;
113
0
}
114
#endif /* FIPS_MODULE */
115
116
const DSA_METHOD *DSA_get_method(DSA *d)
117
127k
{
118
127k
    return d->meth;
119
127k
}
120
121
static DSA *dsa_new_intern(OSSL_LIB_CTX *libctx)
122
130k
{
123
130k
    DSA *ret = OPENSSL_zalloc(sizeof(*ret));
124
125
130k
    if (ret == NULL)
126
0
        return NULL;
127
128
130k
    ret->lock = CRYPTO_THREAD_lock_new();
129
130k
    if (ret->lock == NULL) {
130
0
        ERR_raise(ERR_LIB_DSA, ERR_R_CRYPTO_LIB);
131
0
        OPENSSL_free(ret);
132
0
        return NULL;
133
0
    }
134
135
130k
    if (!CRYPTO_NEW_REF(&ret->references, 1)) {
136
0
        CRYPTO_THREAD_lock_free(ret->lock);
137
0
        OPENSSL_free(ret);
138
0
        return NULL;
139
0
    }
140
141
130k
    ret->libctx = libctx;
142
130k
    ret->meth = DSA_get_default_method();
143
144
130k
    ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
145
146
130k
#ifndef FIPS_MODULE
147
130k
    if (!ossl_crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret,
148
130k
            &ret->ex_data))
149
0
        goto err;
150
130k
#endif
151
152
130k
    ossl_ffc_params_init(&ret->params);
153
154
130k
    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
155
0
        ERR_raise(ERR_LIB_DSA, ERR_R_INIT_FAIL);
156
0
        goto err;
157
0
    }
158
159
130k
    return ret;
160
161
0
err:
162
0
    DSA_free(ret);
163
0
    return NULL;
164
130k
}
165
166
DSA *DSA_new_method(ENGINE *engine)
167
0
{
168
0
    if (!ossl_assert(engine == NULL))
169
0
        return NULL;
170
0
    return dsa_new_intern(NULL);
171
0
}
172
173
DSA *ossl_dsa_new(OSSL_LIB_CTX *libctx)
174
61.1k
{
175
61.1k
    return dsa_new_intern(libctx);
176
61.1k
}
177
178
#ifndef FIPS_MODULE
179
DSA *DSA_new(void)
180
358k
{
181
358k
    return dsa_new_intern(NULL);
182
358k
}
183
#endif
184
185
void DSA_free(DSA *r)
186
1.11M
{
187
1.11M
    int i;
188
189
1.11M
    if (r == NULL)
190
591k
        return;
191
192
525k
    CRYPTO_DOWN_REF(&r->references, &i);
193
525k
    REF_PRINT_COUNT("DSA", i, r);
194
525k
    if (i > 0)
195
105k
        return;
196
419k
    REF_ASSERT_ISNT(i < 0);
197
198
419k
    if (r->meth != NULL && r->meth->finish != NULL)
199
419k
        r->meth->finish(r);
200
201
419k
#ifndef FIPS_MODULE
202
419k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
203
419k
#endif
204
205
419k
    CRYPTO_THREAD_lock_free(r->lock);
206
419k
    CRYPTO_FREE_REF(&r->references);
207
208
419k
    ossl_ffc_params_cleanup(&r->params);
209
419k
    BN_clear_free(r->pub_key);
210
419k
    BN_clear_free(r->priv_key);
211
419k
    OPENSSL_free(r);
212
419k
}
213
214
int DSA_up_ref(DSA *r)
215
16.0k
{
216
16.0k
    int i;
217
218
16.0k
    if (!CRYPTO_UP_REF(&r->references, &i))
219
0
        return 0;
220
221
16.0k
    REF_PRINT_COUNT("DSA", i, r);
222
16.0k
    REF_ASSERT_ISNT(i < 2);
223
16.0k
    return ((i > 1) ? 1 : 0);
224
16.0k
}
225
226
OSSL_LIB_CTX *ossl_dsa_get0_libctx(const DSA *d)
227
6.84k
{
228
6.84k
    return d->libctx;
229
6.84k
}
230
231
void ossl_dsa_set0_libctx(DSA *d, OSSL_LIB_CTX *libctx)
232
103k
{
233
103k
    d->libctx = libctx;
234
103k
}
235
236
void DSA_get0_pqg(const DSA *d,
237
    const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
238
59.3k
{
239
59.3k
    ossl_ffc_params_get0_pqg(&d->params, p, q, g);
240
59.3k
}
241
242
int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
243
1.42k
{
244
    /* If the fields p, q and g in d are NULL, the corresponding input
245
     * parameters MUST be non-NULL.
246
     */
247
1.42k
    if ((d->params.p == NULL && p == NULL)
248
1.42k
        || (d->params.q == NULL && q == NULL)
249
1.42k
        || (d->params.g == NULL && g == NULL))
250
0
        return 0;
251
252
1.42k
    ossl_ffc_params_set0_pqg(&d->params, p, q, g);
253
1.42k
    d->dirty_cnt++;
254
255
1.42k
    return 1;
256
1.42k
}
257
258
const BIGNUM *DSA_get0_p(const DSA *d)
259
132k
{
260
132k
    return d->params.p;
261
132k
}
262
263
const BIGNUM *DSA_get0_q(const DSA *d)
264
25.8k
{
265
25.8k
    return d->params.q;
266
25.8k
}
267
268
const BIGNUM *DSA_get0_g(const DSA *d)
269
122k
{
270
122k
    return d->params.g;
271
122k
}
272
273
const BIGNUM *DSA_get0_pub_key(const DSA *d)
274
177k
{
275
177k
    return d->pub_key;
276
177k
}
277
278
const BIGNUM *DSA_get0_priv_key(const DSA *d)
279
74.8k
{
280
74.8k
    return d->priv_key;
281
74.8k
}
282
283
void DSA_get0_key(const DSA *d,
284
    const BIGNUM **pub_key, const BIGNUM **priv_key)
285
147k
{
286
147k
    if (pub_key != NULL)
287
144k
        *pub_key = d->pub_key;
288
147k
    if (priv_key != NULL)
289
142k
        *priv_key = d->priv_key;
290
147k
}
291
292
int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
293
62.1k
{
294
62.1k
    if (pub_key != NULL) {
295
62.1k
        BN_free(d->pub_key);
296
62.1k
        d->pub_key = pub_key;
297
62.1k
    }
298
62.1k
    if (priv_key != NULL) {
299
61.6k
        BN_free(d->priv_key);
300
61.6k
        d->priv_key = priv_key;
301
61.6k
    }
302
62.1k
    d->dirty_cnt++;
303
304
62.1k
    return 1;
305
62.1k
}
306
307
int DSA_security_bits(const DSA *d)
308
107k
{
309
107k
    if (d->params.p != NULL && d->params.q != NULL)
310
107k
        return BN_security_bits(BN_num_bits(d->params.p),
311
107k
            BN_num_bits(d->params.q));
312
0
    return -1;
313
107k
}
314
315
int DSA_bits(const DSA *dsa)
316
107k
{
317
107k
    if (dsa->params.p != NULL)
318
107k
        return BN_num_bits(dsa->params.p);
319
0
    return -1;
320
107k
}
321
322
FFC_PARAMS *ossl_dsa_get0_params(DSA *dsa)
323
380k
{
324
380k
    return &dsa->params;
325
380k
}
326
327
int ossl_dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[])
328
58.4k
{
329
58.4k
    int ret;
330
58.4k
    FFC_PARAMS *ffc = ossl_dsa_get0_params(dsa);
331
332
58.4k
    ret = ossl_ffc_params_fromdata(ffc, params);
333
58.4k
    if (ret)
334
58.4k
        dsa->dirty_cnt++;
335
58.4k
    return ret;
336
58.4k
}