Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/dsa/dsa_lib.c
Line
Count
Source
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
const DSA_METHOD *DSA_get_method(DSA *d)
128
175k
{
129
175k
    return d->meth;
130
175k
}
131
132
static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
133
396k
{
134
396k
    DSA *ret = OPENSSL_zalloc(sizeof(*ret));
135
136
396k
    if (ret == NULL)
137
0
        return NULL;
138
139
396k
    ret->lock = CRYPTO_THREAD_lock_new();
140
396k
    if (ret->lock == NULL) {
141
0
        ERR_raise(ERR_LIB_DSA, ERR_R_CRYPTO_LIB);
142
0
        OPENSSL_free(ret);
143
0
        return NULL;
144
0
    }
145
146
396k
    if (!CRYPTO_NEW_REF(&ret->references, 1)) {
147
0
        CRYPTO_THREAD_lock_free(ret->lock);
148
0
        OPENSSL_free(ret);
149
0
        return NULL;
150
0
    }
151
152
396k
    ret->libctx = libctx;
153
396k
    ret->meth = DSA_get_default_method();
154
396k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
155
396k
    ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
156
396k
    if (engine) {
157
0
        if (!ENGINE_init(engine)) {
158
0
            ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB);
159
0
            goto err;
160
0
        }
161
0
        ret->engine = engine;
162
0
    } else
163
396k
        ret->engine = ENGINE_get_default_DSA();
164
396k
    if (ret->engine) {
165
0
        ret->meth = ENGINE_get_DSA(ret->engine);
166
0
        if (ret->meth == NULL) {
167
0
            ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB);
168
0
            goto err;
169
0
        }
170
0
    }
171
396k
#endif
172
173
396k
    ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
174
175
396k
#ifndef FIPS_MODULE
176
396k
    if (!ossl_crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret,
177
396k
            &ret->ex_data))
178
0
        goto err;
179
396k
#endif
180
181
396k
    ossl_ffc_params_init(&ret->params);
182
183
396k
    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
184
0
        ERR_raise(ERR_LIB_DSA, ERR_R_INIT_FAIL);
185
0
        goto err;
186
0
    }
187
188
396k
    return ret;
189
190
0
err:
191
0
    DSA_free(ret);
192
0
    return NULL;
193
396k
}
194
195
DSA *DSA_new_method(ENGINE *engine)
196
0
{
197
0
    return dsa_new_intern(engine, NULL);
198
0
}
199
200
DSA *ossl_dsa_new(OSSL_LIB_CTX *libctx)
201
94.9k
{
202
94.9k
    return dsa_new_intern(NULL, libctx);
203
94.9k
}
204
205
#ifndef FIPS_MODULE
206
DSA *DSA_new(void)
207
504k
{
208
504k
    return dsa_new_intern(NULL, NULL);
209
504k
}
210
#endif
211
212
void DSA_free(DSA *r)
213
1.60M
{
214
1.60M
    int i;
215
216
1.60M
    if (r == NULL)
217
872k
        return;
218
219
735k
    CRYPTO_DOWN_REF(&r->references, &i);
220
735k
    REF_PRINT_COUNT("DSA", i, r);
221
735k
    if (i > 0)
222
136k
        return;
223
599k
    REF_ASSERT_ISNT(i < 0);
224
225
599k
    if (r->meth != NULL && r->meth->finish != NULL)
226
599k
        r->meth->finish(r);
227
599k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
228
599k
    ENGINE_finish(r->engine);
229
599k
#endif
230
231
599k
#ifndef FIPS_MODULE
232
599k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
233
599k
#endif
234
235
599k
    CRYPTO_THREAD_lock_free(r->lock);
236
599k
    CRYPTO_FREE_REF(&r->references);
237
238
599k
    ossl_ffc_params_cleanup(&r->params);
239
599k
    BN_clear_free(r->pub_key);
240
599k
    BN_clear_free(r->priv_key);
241
599k
    OPENSSL_free(r);
242
599k
}
243
244
int DSA_up_ref(DSA *r)
245
136k
{
246
136k
    int i;
247
248
136k
    if (CRYPTO_UP_REF(&r->references, &i) <= 0)
249
0
        return 0;
250
251
136k
    REF_PRINT_COUNT("DSA", i, r);
252
136k
    REF_ASSERT_ISNT(i < 2);
253
136k
    return ((i > 1) ? 1 : 0);
254
136k
}
255
256
void ossl_dsa_set0_libctx(DSA *d, OSSL_LIB_CTX *libctx)
257
125k
{
258
125k
    d->libctx = libctx;
259
125k
}
260
261
void DSA_get0_pqg(const DSA *d,
262
    const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
263
71.9k
{
264
71.9k
    ossl_ffc_params_get0_pqg(&d->params, p, q, g);
265
71.9k
}
266
267
int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
268
2.13k
{
269
    /* If the fields p, q and g in d are NULL, the corresponding input
270
     * parameters MUST be non-NULL.
271
     */
272
2.13k
    if ((d->params.p == NULL && p == NULL)
273
2.13k
        || (d->params.q == NULL && q == NULL)
274
2.13k
        || (d->params.g == NULL && g == NULL))
275
0
        return 0;
276
277
2.13k
    ossl_ffc_params_set0_pqg(&d->params, p, q, g);
278
2.13k
    d->dirty_cnt++;
279
280
2.13k
    return 1;
281
2.13k
}
282
283
const BIGNUM *DSA_get0_p(const DSA *d)
284
160k
{
285
160k
    return d->params.p;
286
160k
}
287
288
const BIGNUM *DSA_get0_q(const DSA *d)
289
41.4k
{
290
41.4k
    return d->params.q;
291
41.4k
}
292
293
const BIGNUM *DSA_get0_g(const DSA *d)
294
147k
{
295
147k
    return d->params.g;
296
147k
}
297
298
const BIGNUM *DSA_get0_pub_key(const DSA *d)
299
215k
{
300
215k
    return d->pub_key;
301
215k
}
302
303
const BIGNUM *DSA_get0_priv_key(const DSA *d)
304
101k
{
305
101k
    return d->priv_key;
306
101k
}
307
308
void DSA_get0_key(const DSA *d,
309
    const BIGNUM **pub_key, const BIGNUM **priv_key)
310
190k
{
311
190k
    if (pub_key != NULL)
312
187k
        *pub_key = d->pub_key;
313
190k
    if (priv_key != NULL)
314
183k
        *priv_key = d->priv_key;
315
190k
}
316
317
int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
318
96.8k
{
319
96.8k
    if (pub_key != NULL) {
320
96.8k
        BN_free(d->pub_key);
321
96.8k
        d->pub_key = pub_key;
322
96.8k
    }
323
96.8k
    if (priv_key != NULL) {
324
96.0k
        BN_free(d->priv_key);
325
96.0k
        d->priv_key = priv_key;
326
96.0k
    }
327
96.8k
    d->dirty_cnt++;
328
329
96.8k
    return 1;
330
96.8k
}
331
332
int DSA_security_bits(const DSA *d)
333
131k
{
334
131k
    if (d->params.p != NULL && d->params.q != NULL)
335
131k
        return BN_security_bits(BN_num_bits(d->params.p),
336
131k
            BN_num_bits(d->params.q));
337
0
    return -1;
338
131k
}
339
340
int DSA_bits(const DSA *dsa)
341
131k
{
342
131k
    if (dsa->params.p != NULL)
343
131k
        return BN_num_bits(dsa->params.p);
344
0
    return -1;
345
131k
}
346
347
FFC_PARAMS *ossl_dsa_get0_params(DSA *dsa)
348
492k
{
349
492k
    return &dsa->params;
350
492k
}
351
352
int ossl_dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[])
353
82.2k
{
354
82.2k
    int ret;
355
82.2k
    FFC_PARAMS *ffc = ossl_dsa_get0_params(dsa);
356
357
82.2k
    ret = ossl_ffc_params_fromdata(ffc, params);
358
82.2k
    if (ret)
359
82.2k
        dsa->dirty_cnt++;
360
82.2k
    return ret;
361
82.2k
}