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