Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/dsa/dsa_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include "internal/refcount.h"
13
#include <openssl/bn.h>
14
#include "dsa_locl.h"
15
#include <openssl/asn1.h>
16
#include <openssl/engine.h>
17
#include <openssl/dh.h>
18
19
DSA *DSA_new(void)
20
0
{
21
0
    return DSA_new_method(NULL);
22
0
}
23
24
int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
25
0
{
26
0
    /*
27
0
     * NB: The caller is specifically setting a method, so it's not up to us
28
0
     * to deal with which ENGINE it comes from.
29
0
     */
30
0
    const DSA_METHOD *mtmp;
31
0
    mtmp = dsa->meth;
32
0
    if (mtmp->finish)
33
0
        mtmp->finish(dsa);
34
0
#ifndef OPENSSL_NO_ENGINE
35
0
    ENGINE_finish(dsa->engine);
36
0
    dsa->engine = NULL;
37
0
#endif
38
0
    dsa->meth = meth;
39
0
    if (meth->init)
40
0
        meth->init(dsa);
41
0
    return 1;
42
0
}
43
44
const DSA_METHOD *DSA_get_method(DSA *d)
45
0
{
46
0
    return d->meth;
47
0
}
48
49
DSA *DSA_new_method(ENGINE *engine)
50
0
{
51
0
    DSA *ret = OPENSSL_zalloc(sizeof(*ret));
52
0
53
0
    if (ret == NULL) {
54
0
        DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
55
0
        return NULL;
56
0
    }
57
0
58
0
    ret->references = 1;
59
0
    ret->lock = CRYPTO_THREAD_lock_new();
60
0
    if (ret->lock == NULL) {
61
0
        DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
62
0
        OPENSSL_free(ret);
63
0
        return NULL;
64
0
    }
65
0
66
0
    ret->meth = DSA_get_default_method();
67
0
#ifndef OPENSSL_NO_ENGINE
68
0
    ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
69
0
    if (engine) {
70
0
        if (!ENGINE_init(engine)) {
71
0
            DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
72
0
            goto err;
73
0
        }
74
0
        ret->engine = engine;
75
0
    } else
76
0
        ret->engine = ENGINE_get_default_DSA();
77
0
    if (ret->engine) {
78
0
        ret->meth = ENGINE_get_DSA(ret->engine);
79
0
        if (ret->meth == NULL) {
80
0
            DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
81
0
            goto err;
82
0
        }
83
0
    }
84
0
#endif
85
0
86
0
    ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
87
0
88
0
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
89
0
        goto err;
90
0
91
0
    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
92
0
        DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL);
93
0
err:
94
0
        DSA_free(ret);
95
0
        ret = NULL;
96
0
    }
97
0
98
0
    return ret;
99
0
}
100
101
void DSA_free(DSA *r)
102
0
{
103
0
    int i;
104
0
105
0
    if (r == NULL)
106
0
        return;
107
0
108
0
    CRYPTO_DOWN_REF(&r->references, &i, r->lock);
109
0
    REF_PRINT_COUNT("DSA", r);
110
0
    if (i > 0)
111
0
        return;
112
0
    REF_ASSERT_ISNT(i < 0);
113
0
114
0
    if (r->meth->finish)
115
0
        r->meth->finish(r);
116
0
#ifndef OPENSSL_NO_ENGINE
117
0
    ENGINE_finish(r->engine);
118
0
#endif
119
0
120
0
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
121
0
122
0
    CRYPTO_THREAD_lock_free(r->lock);
123
0
124
0
    BN_clear_free(r->p);
125
0
    BN_clear_free(r->q);
126
0
    BN_clear_free(r->g);
127
0
    BN_clear_free(r->pub_key);
128
0
    BN_clear_free(r->priv_key);
129
0
    OPENSSL_free(r);
130
0
}
131
132
int DSA_up_ref(DSA *r)
133
0
{
134
0
    int i;
135
0
136
0
    if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
137
0
        return 0;
138
0
139
0
    REF_PRINT_COUNT("DSA", r);
140
0
    REF_ASSERT_ISNT(i < 2);
141
0
    return ((i > 1) ? 1 : 0);
142
0
}
143
144
int DSA_size(const DSA *r)
145
0
{
146
0
    int ret, i;
147
0
    ASN1_INTEGER bs;
148
0
    unsigned char buf[4];       /* 4 bytes looks really small. However,
149
0
                                 * i2d_ASN1_INTEGER() will not look beyond
150
0
                                 * the first byte, as long as the second
151
0
                                 * parameter is NULL. */
152
0
153
0
    i = BN_num_bits(r->q);
154
0
    bs.length = (i + 7) / 8;
155
0
    bs.data = buf;
156
0
    bs.type = V_ASN1_INTEGER;
157
0
    /* If the top bit is set the asn1 encoding is 1 larger. */
158
0
    buf[0] = 0xff;
159
0
160
0
    i = i2d_ASN1_INTEGER(&bs, NULL);
161
0
    i += i;                     /* r and s */
162
0
    ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
163
0
    return ret;
164
0
}
165
166
int DSA_set_ex_data(DSA *d, int idx, void *arg)
167
0
{
168
0
    return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
169
0
}
170
171
void *DSA_get_ex_data(DSA *d, int idx)
172
0
{
173
0
    return CRYPTO_get_ex_data(&d->ex_data, idx);
174
0
}
175
176
int DSA_security_bits(const DSA *d)
177
0
{
178
0
    if (d->p && d->q)
179
0
        return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q));
180
0
    return -1;
181
0
}
182
183
#ifndef OPENSSL_NO_DH
184
DH *DSA_dup_DH(const DSA *r)
185
0
{
186
0
    /*
187
0
     * DSA has p, q, g, optional pub_key, optional priv_key. DH has p,
188
0
     * optional length, g, optional pub_key, optional priv_key, optional q.
189
0
     */
190
0
191
0
    DH *ret = NULL;
192
0
    BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL;
193
0
194
0
    if (r == NULL)
195
0
        goto err;
196
0
    ret = DH_new();
197
0
    if (ret == NULL)
198
0
        goto err;
199
0
    if (r->p != NULL || r->g != NULL || r->q != NULL) {
200
0
        if (r->p == NULL || r->g == NULL || r->q == NULL) {
201
0
            /* Shouldn't happen */
202
0
            goto err;
203
0
        }
204
0
        p = BN_dup(r->p);
205
0
        g = BN_dup(r->g);
206
0
        q = BN_dup(r->q);
207
0
        if (p == NULL || g == NULL || q == NULL || !DH_set0_pqg(ret, p, q, g))
208
0
            goto err;
209
0
        p = g = q = NULL;
210
0
    }
211
0
212
0
    if (r->pub_key != NULL) {
213
0
        pub_key = BN_dup(r->pub_key);
214
0
        if (pub_key == NULL)
215
0
            goto err;
216
0
        if (r->priv_key != NULL) {
217
0
            priv_key = BN_dup(r->priv_key);
218
0
            if (priv_key == NULL)
219
0
                goto err;
220
0
        }
221
0
        if (!DH_set0_key(ret, pub_key, priv_key))
222
0
            goto err;
223
0
    } else if (r->priv_key != NULL) {
224
0
        /* Shouldn't happen */
225
0
        goto err;
226
0
    }
227
0
228
0
    return ret;
229
0
230
0
 err:
231
0
    BN_free(p);
232
0
    BN_free(g);
233
0
    BN_free(q);
234
0
    BN_free(pub_key);
235
0
    BN_free(priv_key);
236
0
    DH_free(ret);
237
0
    return NULL;
238
0
}
239
#endif
240
241
void DSA_get0_pqg(const DSA *d,
242
                  const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
243
0
{
244
0
    if (p != NULL)
245
0
        *p = d->p;
246
0
    if (q != NULL)
247
0
        *q = d->q;
248
0
    if (g != NULL)
249
0
        *g = d->g;
250
0
}
251
252
int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
253
0
{
254
0
    /* If the fields p, q and g in d are NULL, the corresponding input
255
0
     * parameters MUST be non-NULL.
256
0
     */
257
0
    if ((d->p == NULL && p == NULL)
258
0
        || (d->q == NULL && q == NULL)
259
0
        || (d->g == NULL && g == NULL))
260
0
        return 0;
261
0
262
0
    if (p != NULL) {
263
0
        BN_free(d->p);
264
0
        d->p = p;
265
0
    }
266
0
    if (q != NULL) {
267
0
        BN_free(d->q);
268
0
        d->q = q;
269
0
    }
270
0
    if (g != NULL) {
271
0
        BN_free(d->g);
272
0
        d->g = g;
273
0
    }
274
0
275
0
    return 1;
276
0
}
277
278
void DSA_get0_key(const DSA *d,
279
                  const BIGNUM **pub_key, const BIGNUM **priv_key)
280
0
{
281
0
    if (pub_key != NULL)
282
0
        *pub_key = d->pub_key;
283
0
    if (priv_key != NULL)
284
0
        *priv_key = d->priv_key;
285
0
}
286
287
int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
288
0
{
289
0
    /* If the field pub_key in d is NULL, the corresponding input
290
0
     * parameters MUST be non-NULL.  The priv_key field may
291
0
     * be left NULL.
292
0
     */
293
0
    if (d->pub_key == NULL && pub_key == NULL)
294
0
        return 0;
295
0
296
0
    if (pub_key != NULL) {
297
0
        BN_free(d->pub_key);
298
0
        d->pub_key = pub_key;
299
0
    }
300
0
    if (priv_key != NULL) {
301
0
        BN_free(d->priv_key);
302
0
        d->priv_key = priv_key;
303
0
    }
304
0
305
0
    return 1;
306
0
}
307
308
const BIGNUM *DSA_get0_p(const DSA *d)
309
0
{
310
0
    return d->p;
311
0
}
312
313
const BIGNUM *DSA_get0_q(const DSA *d)
314
0
{
315
0
    return d->q;
316
0
}
317
318
const BIGNUM *DSA_get0_g(const DSA *d)
319
0
{
320
0
    return d->g;
321
0
}
322
323
const BIGNUM *DSA_get0_pub_key(const DSA *d)
324
0
{
325
0
    return d->pub_key;
326
0
}
327
328
const BIGNUM *DSA_get0_priv_key(const DSA *d)
329
0
{
330
0
    return d->priv_key;
331
0
}
332
333
void DSA_clear_flags(DSA *d, int flags)
334
0
{
335
0
    d->flags &= ~flags;
336
0
}
337
338
int DSA_test_flags(const DSA *d, int flags)
339
0
{
340
0
    return d->flags & flags;
341
0
}
342
343
void DSA_set_flags(DSA *d, int flags)
344
0
{
345
0
    d->flags |= flags;
346
0
}
347
348
ENGINE *DSA_get0_engine(DSA *d)
349
0
{
350
0
    return d->engine;
351
0
}
352
353
int DSA_bits(const DSA *dsa)
354
0
{
355
0
    return BN_num_bits(dsa->p);
356
0
}