Coverage Report

Created: 2025-12-31 06:58

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