Coverage Report

Created: 2026-07-12 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/dh/dh_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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
 * DH low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include <openssl/bn.h>
18
#include <openssl/obj_mac.h>
19
#include <openssl/core_names.h>
20
#include "internal/cryptlib.h"
21
#include "internal/refcount.h"
22
#include "internal/common.h"
23
#include "crypto/evp.h"
24
#include "crypto/dh.h"
25
#include "dh_local.h"
26
27
static DH *dh_new_intern(OSSL_LIB_CTX *libctx);
28
29
#ifndef FIPS_MODULE
30
int DH_set_method(DH *dh, const DH_METHOD *meth)
31
0
{
32
    /*
33
     * NB: The caller is specifically setting a method, so it's not up to us
34
     * to deal with which ENGINE it comes from.
35
     */
36
0
    const DH_METHOD *mtmp;
37
0
    mtmp = dh->meth;
38
0
    if (mtmp->finish)
39
0
        mtmp->finish(dh);
40
0
    dh->meth = meth;
41
0
    if (meth->init)
42
0
        meth->init(dh);
43
0
    return 1;
44
0
}
45
46
const DH_METHOD *ossl_dh_get_method(const DH *dh)
47
13.9k
{
48
13.9k
    return dh->meth;
49
13.9k
}
50
#ifndef OPENSSL_NO_DEPRECATED_3_0
51
DH *DH_new(void)
52
306k
{
53
306k
    return dh_new_intern(NULL);
54
306k
}
55
#endif
56
57
DH *DH_new_method(ENGINE *engine)
58
0
{
59
0
    if (!ossl_assert(engine == NULL))
60
0
        return NULL;
61
0
    return dh_new_intern(NULL);
62
0
}
63
#endif /* !FIPS_MODULE */
64
65
DH *ossl_dh_new_ex(OSSL_LIB_CTX *libctx)
66
23.9k
{
67
23.9k
    return dh_new_intern(libctx);
68
23.9k
}
69
70
static DH *dh_new_intern(OSSL_LIB_CTX *libctx)
71
31.0k
{
72
31.0k
    DH *ret = OPENSSL_zalloc(sizeof(*ret));
73
74
31.0k
    if (ret == NULL)
75
0
        return NULL;
76
77
31.0k
    ret->lock = CRYPTO_THREAD_lock_new();
78
31.0k
    if (ret->lock == NULL) {
79
0
        ERR_raise(ERR_LIB_DH, ERR_R_CRYPTO_LIB);
80
0
        OPENSSL_free(ret);
81
0
        return NULL;
82
0
    }
83
84
31.0k
    if (!CRYPTO_NEW_REF(&ret->references, 1)) {
85
0
        CRYPTO_THREAD_lock_free(ret->lock);
86
0
        OPENSSL_free(ret);
87
0
        return NULL;
88
0
    }
89
90
31.0k
    ret->libctx = libctx;
91
31.0k
    ret->meth = DH_get_default_method();
92
31.0k
    ret->flags = ret->meth->flags;
93
94
31.0k
#ifndef FIPS_MODULE
95
31.0k
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
96
0
        goto err;
97
31.0k
#endif /* FIPS_MODULE */
98
99
31.0k
    ossl_ffc_params_init(&ret->params);
100
101
31.0k
    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
102
0
        ERR_raise(ERR_LIB_DH, ERR_R_INIT_FAIL);
103
0
        goto err;
104
0
    }
105
106
31.0k
    return ret;
107
108
0
err:
109
0
    DH_free(ret);
110
0
    return NULL;
111
31.0k
}
112
113
void DH_free(DH *r)
114
917k
{
115
917k
    int i;
116
117
917k
    if (r == NULL)
118
529k
        return;
119
120
387k
    CRYPTO_DOWN_REF(&r->references, &i);
121
387k
    REF_PRINT_COUNT("DH", i, r);
122
387k
    if (i > 0)
123
56.7k
        return;
124
330k
    REF_ASSERT_ISNT(i < 0);
125
126
330k
    if (r->meth != NULL && r->meth->finish != NULL)
127
330k
        r->meth->finish(r);
128
330k
#if !defined(FIPS_MODULE)
129
330k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
130
330k
#endif
131
132
330k
    CRYPTO_THREAD_lock_free(r->lock);
133
330k
    CRYPTO_FREE_REF(&r->references);
134
135
330k
    ossl_ffc_params_cleanup(&r->params);
136
330k
    BN_clear_free(r->pub_key);
137
330k
    BN_clear_free(r->priv_key);
138
330k
    OPENSSL_free(r);
139
330k
}
140
141
int DH_up_ref(DH *r)
142
56.7k
{
143
56.7k
    int i;
144
145
56.7k
    if (CRYPTO_UP_REF(&r->references, &i) <= 0)
146
0
        return 0;
147
148
56.7k
    REF_PRINT_COUNT("DH", i, r);
149
56.7k
    REF_ASSERT_ISNT(i < 2);
150
56.7k
    return ((i > 1) ? 1 : 0);
151
56.7k
}
152
153
OSSL_LIB_CTX *ossl_dh_get0_libctx(const DH *dh)
154
0
{
155
0
    return dh->libctx;
156
0
}
157
158
void ossl_dh_set0_libctx(DH *d, OSSL_LIB_CTX *libctx)
159
55.2k
{
160
55.2k
    d->libctx = libctx;
161
55.2k
}
162
163
#ifndef FIPS_MODULE
164
int DH_set_ex_data(DH *d, int idx, void *arg)
165
0
{
166
0
    return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
167
0
}
168
169
void *DH_get_ex_data(const DH *d, int idx)
170
0
{
171
0
    return CRYPTO_get_ex_data(&d->ex_data, idx);
172
0
}
173
#endif
174
175
int DH_bits(const DH *dh)
176
79.2k
{
177
79.2k
    if (dh->params.p != NULL)
178
79.2k
        return BN_num_bits(dh->params.p);
179
0
    return -1;
180
79.2k
}
181
182
int DH_size(const DH *dh)
183
88.1k
{
184
88.1k
    if (dh->params.p != NULL)
185
88.1k
        return BN_num_bytes(dh->params.p);
186
0
    return -1;
187
88.1k
}
188
189
int DH_security_bits(const DH *dh)
190
79.2k
{
191
79.2k
    int N;
192
193
79.2k
    if (dh->params.q != NULL)
194
55.6k
        N = BN_num_bits(dh->params.q);
195
23.5k
    else if (dh->length)
196
3.76k
        N = dh->length;
197
19.7k
    else
198
19.7k
        N = -1;
199
79.2k
    if (dh->params.p != NULL)
200
79.2k
        return BN_security_bits(BN_num_bits(dh->params.p), N);
201
0
    return -1;
202
79.2k
}
203
204
void DH_get0_pqg(const DH *dh,
205
    const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
206
10.7k
{
207
10.7k
    ossl_ffc_params_get0_pqg(&dh->params, p, q, g);
208
10.7k
}
209
210
int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
211
54.0k
{
212
    /*
213
     * If the fields p and g in dh are NULL, the corresponding input
214
     * parameters MUST be non-NULL.  q may remain NULL.
215
     */
216
54.0k
    if ((dh->params.p == NULL && p == NULL)
217
54.0k
        || (dh->params.g == NULL && g == NULL))
218
0
        return 0;
219
220
54.0k
    ossl_ffc_params_set0_pqg(&dh->params, p, q, g);
221
54.0k
    ossl_dh_cache_named_group(dh);
222
54.0k
    dh->dirty_cnt++;
223
54.0k
    return 1;
224
54.0k
}
225
226
long DH_get_length(const DH *dh)
227
106k
{
228
106k
    return dh->length;
229
106k
}
230
231
int DH_set_length(DH *dh, long length)
232
0
{
233
0
    dh->length = length;
234
0
    dh->dirty_cnt++;
235
0
    return 1;
236
0
}
237
238
void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
239
138k
{
240
138k
    if (pub_key != NULL)
241
130k
        *pub_key = dh->pub_key;
242
138k
    if (priv_key != NULL)
243
96.7k
        *priv_key = dh->priv_key;
244
138k
}
245
246
int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
247
11.1k
{
248
11.1k
    if (pub_key != NULL) {
249
10.8k
        BN_clear_free(dh->pub_key);
250
10.8k
        dh->pub_key = pub_key;
251
10.8k
    }
252
11.1k
    if (priv_key != NULL) {
253
245
        BN_clear_free(dh->priv_key);
254
245
        dh->priv_key = priv_key;
255
245
    }
256
257
11.1k
    dh->dirty_cnt++;
258
11.1k
    return 1;
259
11.1k
}
260
261
const BIGNUM *DH_get0_p(const DH *dh)
262
9.62k
{
263
9.62k
    return dh->params.p;
264
9.62k
}
265
266
const BIGNUM *DH_get0_q(const DH *dh)
267
0
{
268
0
    return dh->params.q;
269
0
}
270
271
const BIGNUM *DH_get0_g(const DH *dh)
272
135
{
273
135
    return dh->params.g;
274
135
}
275
276
const BIGNUM *DH_get0_priv_key(const DH *dh)
277
23.4k
{
278
23.4k
    return dh->priv_key;
279
23.4k
}
280
281
const BIGNUM *DH_get0_pub_key(const DH *dh)
282
33.5k
{
283
33.5k
    return dh->pub_key;
284
33.5k
}
285
286
void DH_clear_flags(DH *dh, int flags)
287
79.3k
{
288
79.3k
    dh->flags &= ~flags;
289
79.3k
}
290
291
int DH_test_flags(const DH *dh, int flags)
292
88
{
293
88
    return dh->flags & flags;
294
88
}
295
296
void DH_set_flags(DH *dh, int flags)
297
79.3k
{
298
79.3k
    dh->flags |= flags;
299
79.3k
}
300
301
FFC_PARAMS *ossl_dh_get0_params(DH *dh)
302
143k
{
303
143k
    return &dh->params;
304
143k
}
305
int ossl_dh_get0_nid(const DH *dh)
306
0
{
307
0
    return dh->params.nid;
308
0
}