Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/dh/dh_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 "dh_locl.h"
15
#include <openssl/engine.h>
16
17
int DH_set_method(DH *dh, const DH_METHOD *meth)
18
0
{
19
0
    /*
20
0
     * NB: The caller is specifically setting a method, so it's not up to us
21
0
     * to deal with which ENGINE it comes from.
22
0
     */
23
0
    const DH_METHOD *mtmp;
24
0
    mtmp = dh->meth;
25
0
    if (mtmp->finish)
26
0
        mtmp->finish(dh);
27
0
#ifndef OPENSSL_NO_ENGINE
28
0
    ENGINE_finish(dh->engine);
29
0
    dh->engine = NULL;
30
0
#endif
31
0
    dh->meth = meth;
32
0
    if (meth->init)
33
0
        meth->init(dh);
34
0
    return 1;
35
0
}
36
37
DH *DH_new(void)
38
0
{
39
0
    return DH_new_method(NULL);
40
0
}
41
42
DH *DH_new_method(ENGINE *engine)
43
0
{
44
0
    DH *ret = OPENSSL_zalloc(sizeof(*ret));
45
0
46
0
    if (ret == NULL) {
47
0
        DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
48
0
        return NULL;
49
0
    }
50
0
51
0
    ret->references = 1;
52
0
    ret->lock = CRYPTO_THREAD_lock_new();
53
0
    if (ret->lock == NULL) {
54
0
        DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
55
0
        OPENSSL_free(ret);
56
0
        return NULL;
57
0
    }
58
0
59
0
    ret->meth = DH_get_default_method();
60
0
#ifndef OPENSSL_NO_ENGINE
61
0
    ret->flags = ret->meth->flags;  /* early default init */
62
0
    if (engine) {
63
0
        if (!ENGINE_init(engine)) {
64
0
            DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
65
0
            goto err;
66
0
        }
67
0
        ret->engine = engine;
68
0
    } else
69
0
        ret->engine = ENGINE_get_default_DH();
70
0
    if (ret->engine) {
71
0
        ret->meth = ENGINE_get_DH(ret->engine);
72
0
        if (ret->meth == NULL) {
73
0
            DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
74
0
            goto err;
75
0
        }
76
0
    }
77
0
#endif
78
0
79
0
    ret->flags = ret->meth->flags;
80
0
81
0
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
82
0
        goto err;
83
0
84
0
    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
85
0
        DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
86
0
err:
87
0
        DH_free(ret);
88
0
        ret = NULL;
89
0
    }
90
0
91
0
    return ret;
92
0
}
93
94
void DH_free(DH *r)
95
0
{
96
0
    int i;
97
0
98
0
    if (r == NULL)
99
0
        return;
100
0
101
0
    CRYPTO_DOWN_REF(&r->references, &i, r->lock);
102
0
    REF_PRINT_COUNT("DH", r);
103
0
    if (i > 0)
104
0
        return;
105
0
    REF_ASSERT_ISNT(i < 0);
106
0
107
0
    if (r->meth->finish)
108
0
        r->meth->finish(r);
109
0
#ifndef OPENSSL_NO_ENGINE
110
0
    ENGINE_finish(r->engine);
111
0
#endif
112
0
113
0
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
114
0
115
0
    CRYPTO_THREAD_lock_free(r->lock);
116
0
117
0
    BN_clear_free(r->p);
118
0
    BN_clear_free(r->g);
119
0
    BN_clear_free(r->q);
120
0
    BN_clear_free(r->j);
121
0
    OPENSSL_free(r->seed);
122
0
    BN_clear_free(r->counter);
123
0
    BN_clear_free(r->pub_key);
124
0
    BN_clear_free(r->priv_key);
125
0
    OPENSSL_free(r);
126
0
}
127
128
int DH_up_ref(DH *r)
129
0
{
130
0
    int i;
131
0
132
0
    if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
133
0
        return 0;
134
0
135
0
    REF_PRINT_COUNT("DH", r);
136
0
    REF_ASSERT_ISNT(i < 2);
137
0
    return ((i > 1) ? 1 : 0);
138
0
}
139
140
int DH_set_ex_data(DH *d, int idx, void *arg)
141
0
{
142
0
    return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
143
0
}
144
145
void *DH_get_ex_data(DH *d, int idx)
146
0
{
147
0
    return CRYPTO_get_ex_data(&d->ex_data, idx);
148
0
}
149
150
int DH_bits(const DH *dh)
151
0
{
152
0
    return BN_num_bits(dh->p);
153
0
}
154
155
int DH_size(const DH *dh)
156
0
{
157
0
    return BN_num_bytes(dh->p);
158
0
}
159
160
int DH_security_bits(const DH *dh)
161
0
{
162
0
    int N;
163
0
    if (dh->q)
164
0
        N = BN_num_bits(dh->q);
165
0
    else if (dh->length)
166
0
        N = dh->length;
167
0
    else
168
0
        N = -1;
169
0
    return BN_security_bits(BN_num_bits(dh->p), N);
170
0
}
171
172
173
void DH_get0_pqg(const DH *dh,
174
                 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
175
0
{
176
0
    if (p != NULL)
177
0
        *p = dh->p;
178
0
    if (q != NULL)
179
0
        *q = dh->q;
180
0
    if (g != NULL)
181
0
        *g = dh->g;
182
0
}
183
184
int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
185
0
{
186
0
    /* If the fields p and g in d are NULL, the corresponding input
187
0
     * parameters MUST be non-NULL.  q may remain NULL.
188
0
     */
189
0
    if ((dh->p == NULL && p == NULL)
190
0
        || (dh->g == NULL && g == NULL))
191
0
        return 0;
192
0
193
0
    if (p != NULL) {
194
0
        BN_free(dh->p);
195
0
        dh->p = p;
196
0
    }
197
0
    if (q != NULL) {
198
0
        BN_free(dh->q);
199
0
        dh->q = q;
200
0
    }
201
0
    if (g != NULL) {
202
0
        BN_free(dh->g);
203
0
        dh->g = g;
204
0
    }
205
0
206
0
    if (q != NULL) {
207
0
        dh->length = BN_num_bits(q);
208
0
    }
209
0
210
0
    return 1;
211
0
}
212
213
long DH_get_length(const DH *dh)
214
0
{
215
0
    return dh->length;
216
0
}
217
218
int DH_set_length(DH *dh, long length)
219
0
{
220
0
    dh->length = length;
221
0
    return 1;
222
0
}
223
224
void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
225
0
{
226
0
    if (pub_key != NULL)
227
0
        *pub_key = dh->pub_key;
228
0
    if (priv_key != NULL)
229
0
        *priv_key = dh->priv_key;
230
0
}
231
232
int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
233
0
{
234
0
    if (pub_key != NULL) {
235
0
        BN_free(dh->pub_key);
236
0
        dh->pub_key = pub_key;
237
0
    }
238
0
    if (priv_key != NULL) {
239
0
        BN_free(dh->priv_key);
240
0
        dh->priv_key = priv_key;
241
0
    }
242
0
243
0
    return 1;
244
0
}
245
246
const BIGNUM *DH_get0_p(const DH *dh)
247
0
{
248
0
    return dh->p;
249
0
}
250
251
const BIGNUM *DH_get0_q(const DH *dh)
252
0
{
253
0
    return dh->q;
254
0
}
255
256
const BIGNUM *DH_get0_g(const DH *dh)
257
0
{
258
0
    return dh->g;
259
0
}
260
261
const BIGNUM *DH_get0_priv_key(const DH *dh)
262
0
{
263
0
    return dh->priv_key;
264
0
}
265
266
const BIGNUM *DH_get0_pub_key(const DH *dh)
267
0
{
268
0
    return dh->pub_key;
269
0
}
270
271
void DH_clear_flags(DH *dh, int flags)
272
0
{
273
0
    dh->flags &= ~flags;
274
0
}
275
276
int DH_test_flags(const DH *dh, int flags)
277
0
{
278
0
    return dh->flags & flags;
279
0
}
280
281
void DH_set_flags(DH *dh, int flags)
282
0
{
283
0
    dh->flags |= flags;
284
0
}
285
286
ENGINE *DH_get0_engine(DH *dh)
287
0
{
288
0
    return dh->engine;
289
0
}