]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
83cf7abf | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
e38873f5 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
ada66e78 P |
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 | ||
d02b48c6 | 16 | #include <stdio.h> |
ec577822 | 17 | #include <openssl/bn.h> |
3c27208f | 18 | #include <openssl/engine.h> |
5a778ce5 DG |
19 | #include <openssl/obj_mac.h> |
20 | #include "internal/cryptlib.h" | |
21 | #include "internal/refcount.h" | |
dc8de3e6 | 22 | #include "crypto/dh.h" |
5a778ce5 | 23 | #include "dh_local.h" |
d02b48c6 | 24 | |
8083fd3a SL |
25 | static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx); |
26 | ||
62f49b90 | 27 | #ifndef FIPS_MODE |
cb78486d | 28 | int DH_set_method(DH *dh, const DH_METHOD *meth) |
0f113f3e MC |
29 | { |
30 | /* | |
31 | * NB: The caller is specifically setting a method, so it's not up to us | |
32 | * to deal with which ENGINE it comes from. | |
33 | */ | |
34 | const DH_METHOD *mtmp; | |
35 | mtmp = dh->meth; | |
36 | if (mtmp->finish) | |
37 | mtmp->finish(dh); | |
0b13e9f0 | 38 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd RS |
39 | ENGINE_finish(dh->engine); |
40 | dh->engine = NULL; | |
0b13e9f0 | 41 | #endif |
0f113f3e MC |
42 | dh->meth = meth; |
43 | if (meth->init) | |
44 | meth->init(dh); | |
45 | return 1; | |
46 | } | |
13066cee | 47 | |
c518117b RL |
48 | const DH_METHOD *dh_get_method(const DH *dh) |
49 | { | |
50 | return dh->meth; | |
51 | } | |
52 | ||
6b691a5c | 53 | DH *DH_new(void) |
0f113f3e | 54 | { |
8083fd3a | 55 | return dh_new_intern(NULL, NULL); |
0f113f3e | 56 | } |
13066cee | 57 | |
5270e702 | 58 | DH *DH_new_method(ENGINE *engine) |
8083fd3a SL |
59 | { |
60 | return dh_new_intern(engine, NULL); | |
61 | } | |
62 | #endif /* !FIPS_MODE */ | |
63 | ||
64 | DH *dh_new_with_ctx(OPENSSL_CTX *libctx) | |
65 | { | |
66 | return dh_new_intern(NULL, libctx); | |
67 | } | |
68 | ||
69 | static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx) | |
0f113f3e | 70 | { |
64b25758 | 71 | DH *ret = OPENSSL_zalloc(sizeof(*ret)); |
13066cee | 72 | |
0f113f3e | 73 | if (ret == NULL) { |
8083fd3a | 74 | DHerr(0, ERR_R_MALLOC_FAILURE); |
d188a536 | 75 | return NULL; |
0f113f3e | 76 | } |
0c9de428 | 77 | |
2bbf0baa F |
78 | ret->references = 1; |
79 | ret->lock = CRYPTO_THREAD_lock_new(); | |
80 | if (ret->lock == NULL) { | |
8083fd3a | 81 | DHerr(0, ERR_R_MALLOC_FAILURE); |
2bbf0baa F |
82 | OPENSSL_free(ret); |
83 | return NULL; | |
84 | } | |
85 | ||
8083fd3a | 86 | ret->libctx = libctx; |
0f113f3e | 87 | ret->meth = DH_get_default_method(); |
62f49b90 | 88 | #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE) |
2bbf0baa | 89 | ret->flags = ret->meth->flags; /* early default init */ |
0f113f3e MC |
90 | if (engine) { |
91 | if (!ENGINE_init(engine)) { | |
8083fd3a | 92 | DHerr(0, ERR_R_ENGINE_LIB); |
2bbf0baa | 93 | goto err; |
0f113f3e MC |
94 | } |
95 | ret->engine = engine; | |
96 | } else | |
97 | ret->engine = ENGINE_get_default_DH(); | |
98 | if (ret->engine) { | |
99 | ret->meth = ENGINE_get_DH(ret->engine); | |
7c96dbcd | 100 | if (ret->meth == NULL) { |
8083fd3a | 101 | DHerr(0, ERR_R_ENGINE_LIB); |
2bbf0baa | 102 | goto err; |
0f113f3e MC |
103 | } |
104 | } | |
0b13e9f0 | 105 | #endif |
cb78486d | 106 | |
0f113f3e | 107 | ret->flags = ret->meth->flags; |
d188a536 | 108 | |
a3327784 | 109 | #ifndef FIPS_MODE |
2bbf0baa F |
110 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data)) |
111 | goto err; | |
62f49b90 | 112 | #endif /* FIPS_MODE */ |
d188a536 AG |
113 | |
114 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { | |
8083fd3a | 115 | DHerr(0, ERR_R_INIT_FAIL); |
544648a8 | 116 | goto err; |
0f113f3e | 117 | } |
d188a536 AG |
118 | |
119 | return ret; | |
544648a8 NT |
120 | |
121 | err: | |
122 | DH_free(ret); | |
123 | return NULL; | |
0f113f3e | 124 | } |
d02b48c6 | 125 | |
6b691a5c | 126 | void DH_free(DH *r) |
0f113f3e MC |
127 | { |
128 | int i; | |
d6407083 | 129 | |
0f113f3e MC |
130 | if (r == NULL) |
131 | return; | |
d188a536 | 132 | |
2f545ae4 | 133 | CRYPTO_DOWN_REF(&r->references, &i, r->lock); |
f3f1cf84 | 134 | REF_PRINT_COUNT("DH", r); |
0f113f3e MC |
135 | if (i > 0) |
136 | return; | |
f3f1cf84 | 137 | REF_ASSERT_ISNT(i < 0); |
13066cee | 138 | |
0c5d725e | 139 | if (r->meth != NULL && r->meth->finish != NULL) |
0f113f3e | 140 | r->meth->finish(r); |
62f49b90 SL |
141 | #if !defined(FIPS_MODE) |
142 | # if !defined(OPENSSL_NO_ENGINE) | |
7c96dbcd | 143 | ENGINE_finish(r->engine); |
62f49b90 | 144 | # endif |
0f113f3e | 145 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); |
a3327784 | 146 | #endif |
d50f1bdf | 147 | |
d188a536 AG |
148 | CRYPTO_THREAD_lock_free(r->lock); |
149 | ||
dc8de3e6 | 150 | ffc_params_cleanup(&r->params); |
23a1d5e9 RS |
151 | BN_clear_free(r->pub_key); |
152 | BN_clear_free(r->priv_key); | |
0f113f3e MC |
153 | OPENSSL_free(r); |
154 | } | |
d02b48c6 | 155 | |
dc2a33d6 | 156 | int DH_up_ref(DH *r) |
0f113f3e | 157 | { |
d188a536 AG |
158 | int i; |
159 | ||
2f545ae4 | 160 | if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0) |
d188a536 | 161 | return 0; |
f3f1cf84 RS |
162 | |
163 | REF_PRINT_COUNT("DH", r); | |
164 | REF_ASSERT_ISNT(i < 2); | |
0f113f3e MC |
165 | return ((i > 1) ? 1 : 0); |
166 | } | |
5cbc2e8b | 167 | |
a3327784 | 168 | #ifndef FIPS_MODE |
dd9d233e | 169 | int DH_set_ex_data(DH *d, int idx, void *arg) |
0f113f3e | 170 | { |
26a7d938 | 171 | return CRYPTO_set_ex_data(&d->ex_data, idx, arg); |
0f113f3e | 172 | } |
13066cee | 173 | |
dd9d233e | 174 | void *DH_get_ex_data(DH *d, int idx) |
0f113f3e | 175 | { |
26a7d938 | 176 | return CRYPTO_get_ex_data(&d->ex_data, idx); |
0f113f3e | 177 | } |
a3327784 | 178 | #endif |
13066cee | 179 | |
26c79d56 KR |
180 | int DH_bits(const DH *dh) |
181 | { | |
dc8de3e6 | 182 | return BN_num_bits(dh->params.p); |
26c79d56 KR |
183 | } |
184 | ||
f971ccb2 | 185 | int DH_size(const DH *dh) |
0f113f3e | 186 | { |
dc8de3e6 | 187 | return BN_num_bytes(dh->params.p); |
0f113f3e | 188 | } |
2514fa79 DSH |
189 | |
190 | int DH_security_bits(const DH *dh) | |
0f113f3e MC |
191 | { |
192 | int N; | |
dc8de3e6 SL |
193 | if (dh->params.q != NULL) |
194 | N = BN_num_bits(dh->params.q); | |
0f113f3e MC |
195 | else if (dh->length) |
196 | N = dh->length; | |
197 | else | |
198 | N = -1; | |
dc8de3e6 | 199 | return BN_security_bits(BN_num_bits(dh->params.p), N); |
0f113f3e | 200 | } |
0aeddcfa | 201 | |
fd809cfd RL |
202 | void DH_get0_pqg(const DH *dh, |
203 | const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) | |
0aeddcfa | 204 | { |
dc8de3e6 | 205 | ffc_params_get0_pqg(&dh->params, p, q, g); |
0aeddcfa MC |
206 | } |
207 | ||
208 | int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) | |
209 | { | |
1da12e34 RL |
210 | /* If the fields p and g in d are NULL, the corresponding input |
211 | * parameters MUST be non-NULL. q may remain NULL. | |
1da12e34 | 212 | */ |
dc8de3e6 SL |
213 | if ((dh->params.p == NULL && p == NULL) |
214 | || (dh->params.g == NULL && g == NULL)) | |
0aeddcfa | 215 | return 0; |
1da12e34 | 216 | |
dc8de3e6 | 217 | ffc_params_set0_pqg(&dh->params, p, q, g); |
ca2bf555 | 218 | dh->params.nid = NID_undef; |
55f02cb6 SL |
219 | /* |
220 | * Check if this is a named group. If it finds a named group then the | |
221 | * 'q' and 'length' value are either already set or are set by the | |
222 | * call. | |
223 | */ | |
224 | if (DH_get_nid(dh) == NID_undef) { | |
225 | /* If its not a named group then set the 'length' if q is not NULL */ | |
226 | if (q != NULL) | |
227 | dh->length = BN_num_bits(q); | |
228 | } | |
8b84b075 | 229 | dh->dirty_cnt++; |
0aeddcfa MC |
230 | return 1; |
231 | } | |
232 | ||
233 | long DH_get_length(const DH *dh) | |
234 | { | |
235 | return dh->length; | |
236 | } | |
237 | ||
238 | int DH_set_length(DH *dh, long length) | |
239 | { | |
240 | dh->length = length; | |
241 | return 1; | |
242 | } | |
243 | ||
fd809cfd | 244 | void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key) |
0aeddcfa MC |
245 | { |
246 | if (pub_key != NULL) | |
247 | *pub_key = dh->pub_key; | |
248 | if (priv_key != NULL) | |
249 | *priv_key = dh->priv_key; | |
250 | } | |
251 | ||
252 | int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) | |
253 | { | |
1da12e34 | 254 | if (pub_key != NULL) { |
fa01370f | 255 | BN_clear_free(dh->pub_key); |
1da12e34 RL |
256 | dh->pub_key = pub_key; |
257 | } | |
258 | if (priv_key != NULL) { | |
fa01370f | 259 | BN_clear_free(dh->priv_key); |
1da12e34 RL |
260 | dh->priv_key = priv_key; |
261 | } | |
0aeddcfa | 262 | |
8b84b075 | 263 | dh->dirty_cnt++; |
0aeddcfa MC |
264 | return 1; |
265 | } | |
266 | ||
6db7fadf DMSP |
267 | const BIGNUM *DH_get0_p(const DH *dh) |
268 | { | |
dc8de3e6 | 269 | return dh->params.p; |
6db7fadf DMSP |
270 | } |
271 | ||
272 | const BIGNUM *DH_get0_q(const DH *dh) | |
273 | { | |
dc8de3e6 | 274 | return dh->params.q; |
6db7fadf DMSP |
275 | } |
276 | ||
277 | const BIGNUM *DH_get0_g(const DH *dh) | |
278 | { | |
dc8de3e6 | 279 | return dh->params.g; |
6db7fadf DMSP |
280 | } |
281 | ||
282 | const BIGNUM *DH_get0_priv_key(const DH *dh) | |
283 | { | |
284 | return dh->priv_key; | |
285 | } | |
286 | ||
287 | const BIGNUM *DH_get0_pub_key(const DH *dh) | |
288 | { | |
289 | return dh->pub_key; | |
290 | } | |
291 | ||
0aeddcfa MC |
292 | void DH_clear_flags(DH *dh, int flags) |
293 | { | |
294 | dh->flags &= ~flags; | |
295 | } | |
296 | ||
297 | int DH_test_flags(const DH *dh, int flags) | |
298 | { | |
299 | return dh->flags & flags; | |
300 | } | |
301 | ||
302 | void DH_set_flags(DH *dh, int flags) | |
303 | { | |
304 | dh->flags |= flags; | |
305 | } | |
306 | ||
62f49b90 | 307 | #ifndef FIPS_MODE |
0aeddcfa MC |
308 | ENGINE *DH_get0_engine(DH *dh) |
309 | { | |
310 | return dh->engine; | |
311 | } | |
62f49b90 | 312 | #endif /*FIPS_MODE */ |
dc8de3e6 SL |
313 | |
314 | FFC_PARAMS *dh_get0_params(DH *dh) | |
315 | { | |
316 | return &dh->params; | |
317 | } | |
ca2bf555 SL |
318 | int dh_get0_nid(const DH *dh) |
319 | { | |
320 | return dh->params.nid; | |
321 | } |