Coverage Report

Created: 2023-09-25 06:41

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