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