Coverage Report

Created: 2023-06-08 06:40

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