Coverage Report

Created: 2023-06-08 06:40

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