Coverage Report

Created: 2026-02-22 06:11

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-2023 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
0
{
48
0
    return dh->meth;
49
0
}
50
#ifndef OPENSSL_NO_DEPRECATED_3_0
51
DH *DH_new(void)
52
0
{
53
0
    return dh_new_intern(NULL);
54
0
}
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
0
{
67
0
    return dh_new_intern(libctx);
68
0
}
69
70
static DH *dh_new_intern(OSSL_LIB_CTX *libctx)
71
0
{
72
0
    DH *ret = OPENSSL_zalloc(sizeof(*ret));
73
74
0
    if (ret == NULL)
75
0
        return NULL;
76
77
0
    ret->lock = CRYPTO_THREAD_lock_new();
78
0
    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
0
    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
0
    ret->libctx = libctx;
91
0
    ret->meth = DH_get_default_method();
92
0
    ret->flags = ret->meth->flags;
93
94
0
#ifndef FIPS_MODULE
95
0
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
96
0
        goto err;
97
0
#endif /* FIPS_MODULE */
98
99
0
    ossl_ffc_params_init(&ret->params);
100
101
0
    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
0
    return ret;
107
108
0
err:
109
0
    DH_free(ret);
110
0
    return NULL;
111
0
}
112
113
void DH_free(DH *r)
114
0
{
115
0
    int i;
116
117
0
    if (r == NULL)
118
0
        return;
119
120
0
    CRYPTO_DOWN_REF(&r->references, &i);
121
0
    REF_PRINT_COUNT("DH", i, r);
122
0
    if (i > 0)
123
0
        return;
124
0
    REF_ASSERT_ISNT(i < 0);
125
126
0
    if (r->meth != NULL && r->meth->finish != NULL)
127
0
        r->meth->finish(r);
128
0
#if !defined(FIPS_MODULE)
129
0
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
130
0
#endif
131
132
0
    CRYPTO_THREAD_lock_free(r->lock);
133
0
    CRYPTO_FREE_REF(&r->references);
134
135
0
    ossl_ffc_params_cleanup(&r->params);
136
0
    BN_clear_free(r->pub_key);
137
0
    BN_clear_free(r->priv_key);
138
0
    OPENSSL_free(r);
139
0
}
140
141
int DH_up_ref(DH *r)
142
0
{
143
0
    int i;
144
145
0
    if (CRYPTO_UP_REF(&r->references, &i) <= 0)
146
0
        return 0;
147
148
0
    REF_PRINT_COUNT("DH", i, r);
149
0
    REF_ASSERT_ISNT(i < 2);
150
0
    return ((i > 1) ? 1 : 0);
151
0
}
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
0
{
160
0
    d->libctx = libctx;
161
0
}
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
0
{
177
0
    if (dh->params.p != NULL)
178
0
        return BN_num_bits(dh->params.p);
179
0
    return -1;
180
0
}
181
182
int DH_size(const DH *dh)
183
0
{
184
0
    if (dh->params.p != NULL)
185
0
        return BN_num_bytes(dh->params.p);
186
0
    return -1;
187
0
}
188
189
int DH_security_bits(const DH *dh)
190
0
{
191
0
    int N;
192
193
0
    if (dh->params.q != NULL)
194
0
        N = BN_num_bits(dh->params.q);
195
0
    else if (dh->length)
196
0
        N = dh->length;
197
0
    else
198
0
        N = -1;
199
0
    if (dh->params.p != NULL)
200
0
        return BN_security_bits(BN_num_bits(dh->params.p), N);
201
0
    return -1;
202
0
}
203
204
void DH_get0_pqg(const DH *dh,
205
    const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
206
0
{
207
0
    ossl_ffc_params_get0_pqg(&dh->params, p, q, g);
208
0
}
209
210
int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
211
0
{
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
0
    if ((dh->params.p == NULL && p == NULL)
217
0
        || (dh->params.g == NULL && g == NULL))
218
0
        return 0;
219
220
0
    ossl_ffc_params_set0_pqg(&dh->params, p, q, g);
221
0
    ossl_dh_cache_named_group(dh);
222
0
    dh->dirty_cnt++;
223
0
    return 1;
224
0
}
225
226
long DH_get_length(const DH *dh)
227
0
{
228
0
    return dh->length;
229
0
}
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
0
{
240
0
    if (pub_key != NULL)
241
0
        *pub_key = dh->pub_key;
242
0
    if (priv_key != NULL)
243
0
        *priv_key = dh->priv_key;
244
0
}
245
246
int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
247
0
{
248
0
    if (pub_key != NULL) {
249
0
        BN_clear_free(dh->pub_key);
250
0
        dh->pub_key = pub_key;
251
0
    }
252
0
    if (priv_key != NULL) {
253
0
        BN_clear_free(dh->priv_key);
254
0
        dh->priv_key = priv_key;
255
0
    }
256
257
0
    dh->dirty_cnt++;
258
0
    return 1;
259
0
}
260
261
const BIGNUM *DH_get0_p(const DH *dh)
262
0
{
263
0
    return dh->params.p;
264
0
}
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
0
{
273
0
    return dh->params.g;
274
0
}
275
276
const BIGNUM *DH_get0_priv_key(const DH *dh)
277
0
{
278
0
    return dh->priv_key;
279
0
}
280
281
const BIGNUM *DH_get0_pub_key(const DH *dh)
282
0
{
283
0
    return dh->pub_key;
284
0
}
285
286
void DH_clear_flags(DH *dh, int flags)
287
0
{
288
0
    dh->flags &= ~flags;
289
0
}
290
291
int DH_test_flags(const DH *dh, int flags)
292
0
{
293
0
    return dh->flags & flags;
294
0
}
295
296
void DH_set_flags(DH *dh, int flags)
297
0
{
298
0
    dh->flags |= flags;
299
0
}
300
301
FFC_PARAMS *ossl_dh_get0_params(DH *dh)
302
0
{
303
0
    return &dh->params;
304
0
}
305
int ossl_dh_get0_nid(const DH *dh)
306
0
{
307
0
    return dh->params.nid;
308
0
}