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