Coverage Report

Created: 2023-09-25 06:45

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