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