]>
Commit | Line | Data |
---|---|---|
aa6bb135 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
aa6bb135 RS |
4 | * Licensed under the OpenSSL license (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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
ec577822 | 12 | #include <openssl/bn.h> |
0aeddcfa | 13 | #include "dh_locl.h" |
3c27208f | 14 | #include <openssl/engine.h> |
d02b48c6 | 15 | |
3a644582 | 16 | static const DH_METHOD *default_DH_method = NULL; |
13066cee | 17 | |
cb78486d | 18 | void DH_set_default_method(const DH_METHOD *meth) |
0f113f3e MC |
19 | { |
20 | default_DH_method = meth; | |
21 | } | |
13066cee | 22 | |
cb78486d | 23 | const DH_METHOD *DH_get_default_method(void) |
0f113f3e MC |
24 | { |
25 | if (!default_DH_method) | |
26 | default_DH_method = DH_OpenSSL(); | |
27 | return default_DH_method; | |
28 | } | |
13066cee | 29 | |
cb78486d | 30 | int DH_set_method(DH *dh, const DH_METHOD *meth) |
0f113f3e MC |
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); | |
0b13e9f0 | 40 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd RS |
41 | ENGINE_finish(dh->engine); |
42 | dh->engine = NULL; | |
0b13e9f0 | 43 | #endif |
0f113f3e MC |
44 | dh->meth = meth; |
45 | if (meth->init) | |
46 | meth->init(dh); | |
47 | return 1; | |
48 | } | |
13066cee | 49 | |
6b691a5c | 50 | DH *DH_new(void) |
0f113f3e MC |
51 | { |
52 | return DH_new_method(NULL); | |
53 | } | |
13066cee | 54 | |
5270e702 | 55 | DH *DH_new_method(ENGINE *engine) |
0f113f3e | 56 | { |
64b25758 | 57 | DH *ret = OPENSSL_zalloc(sizeof(*ret)); |
13066cee | 58 | |
0f113f3e MC |
59 | if (ret == NULL) { |
60 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE); | |
d188a536 | 61 | return NULL; |
0f113f3e | 62 | } |
0c9de428 | 63 | |
2bbf0baa F |
64 | ret->references = 1; |
65 | ret->lock = CRYPTO_THREAD_lock_new(); | |
66 | if (ret->lock == NULL) { | |
67 | OPENSSL_free(ret); | |
68 | return NULL; | |
69 | } | |
70 | ||
0f113f3e | 71 | ret->meth = DH_get_default_method(); |
0b13e9f0 | 72 | #ifndef OPENSSL_NO_ENGINE |
2bbf0baa | 73 | ret->flags = ret->meth->flags; /* early default init */ |
0f113f3e MC |
74 | if (engine) { |
75 | if (!ENGINE_init(engine)) { | |
76 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); | |
2bbf0baa | 77 | goto err; |
0f113f3e MC |
78 | } |
79 | ret->engine = engine; | |
80 | } else | |
81 | ret->engine = ENGINE_get_default_DH(); | |
82 | if (ret->engine) { | |
83 | ret->meth = ENGINE_get_DH(ret->engine); | |
7c96dbcd | 84 | if (ret->meth == NULL) { |
0f113f3e | 85 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); |
2bbf0baa | 86 | goto err; |
0f113f3e MC |
87 | } |
88 | } | |
0b13e9f0 | 89 | #endif |
cb78486d | 90 | |
0f113f3e | 91 | ret->flags = ret->meth->flags; |
d188a536 | 92 | |
2bbf0baa F |
93 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data)) |
94 | goto err; | |
d188a536 AG |
95 | |
96 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { | |
2bbf0baa F |
97 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL); |
98 | err: | |
d188a536 | 99 | DH_free(ret); |
0f113f3e MC |
100 | ret = NULL; |
101 | } | |
d188a536 AG |
102 | |
103 | return ret; | |
0f113f3e | 104 | } |
d02b48c6 | 105 | |
6b691a5c | 106 | void DH_free(DH *r) |
0f113f3e MC |
107 | { |
108 | int i; | |
d6407083 | 109 | |
0f113f3e MC |
110 | if (r == NULL) |
111 | return; | |
d188a536 AG |
112 | |
113 | CRYPTO_atomic_add(&r->references, -1, &i, r->lock); | |
f3f1cf84 | 114 | REF_PRINT_COUNT("DH", r); |
0f113f3e MC |
115 | if (i > 0) |
116 | return; | |
f3f1cf84 | 117 | REF_ASSERT_ISNT(i < 0); |
13066cee | 118 | |
0f113f3e MC |
119 | if (r->meth->finish) |
120 | r->meth->finish(r); | |
0b13e9f0 | 121 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 122 | ENGINE_finish(r->engine); |
0b13e9f0 | 123 | #endif |
ac8b4ee0 | 124 | |
0f113f3e | 125 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); |
d50f1bdf | 126 | |
d188a536 AG |
127 | CRYPTO_THREAD_lock_free(r->lock); |
128 | ||
23a1d5e9 RS |
129 | BN_clear_free(r->p); |
130 | BN_clear_free(r->g); | |
131 | BN_clear_free(r->q); | |
132 | BN_clear_free(r->j); | |
b548a1f1 | 133 | OPENSSL_free(r->seed); |
23a1d5e9 RS |
134 | BN_clear_free(r->counter); |
135 | BN_clear_free(r->pub_key); | |
136 | BN_clear_free(r->priv_key); | |
0f113f3e MC |
137 | OPENSSL_free(r); |
138 | } | |
d02b48c6 | 139 | |
dc2a33d6 | 140 | int DH_up_ref(DH *r) |
0f113f3e | 141 | { |
d188a536 AG |
142 | int i; |
143 | ||
144 | if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0) | |
145 | return 0; | |
f3f1cf84 RS |
146 | |
147 | REF_PRINT_COUNT("DH", r); | |
148 | REF_ASSERT_ISNT(i < 2); | |
0f113f3e MC |
149 | return ((i > 1) ? 1 : 0); |
150 | } | |
5cbc2e8b | 151 | |
dd9d233e | 152 | int DH_set_ex_data(DH *d, int idx, void *arg) |
0f113f3e MC |
153 | { |
154 | return (CRYPTO_set_ex_data(&d->ex_data, idx, arg)); | |
155 | } | |
13066cee | 156 | |
dd9d233e | 157 | void *DH_get_ex_data(DH *d, int idx) |
0f113f3e MC |
158 | { |
159 | return (CRYPTO_get_ex_data(&d->ex_data, idx)); | |
160 | } | |
13066cee | 161 | |
26c79d56 KR |
162 | int DH_bits(const DH *dh) |
163 | { | |
164 | return BN_num_bits(dh->p); | |
165 | } | |
166 | ||
f971ccb2 | 167 | int DH_size(const DH *dh) |
0f113f3e MC |
168 | { |
169 | return (BN_num_bytes(dh->p)); | |
170 | } | |
2514fa79 DSH |
171 | |
172 | int DH_security_bits(const DH *dh) | |
0f113f3e MC |
173 | { |
174 | int N; | |
175 | if (dh->q) | |
176 | N = BN_num_bits(dh->q); | |
177 | else if (dh->length) | |
178 | N = dh->length; | |
179 | else | |
180 | N = -1; | |
181 | return BN_security_bits(BN_num_bits(dh->p), N); | |
182 | } | |
0aeddcfa MC |
183 | |
184 | ||
185 | void DH_get0_pqg(const DH *dh, BIGNUM **p, BIGNUM **q, BIGNUM **g) | |
186 | { | |
187 | if (p != NULL) | |
188 | *p = dh->p; | |
189 | if (q != NULL) | |
190 | *q = dh->q; | |
191 | if (g != NULL) | |
192 | *g = dh->g; | |
193 | } | |
194 | ||
195 | int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) | |
196 | { | |
1da12e34 RL |
197 | /* If the fields p and g in d are NULL, the corresponding input |
198 | * parameters MUST be non-NULL. q may remain NULL. | |
199 | * | |
200 | * It is an error to give the results from get0 on d | |
201 | * as input parameters. | |
202 | */ | |
203 | if (p == dh->p || (dh->q != NULL && q == dh->q) || g == dh->g) | |
0aeddcfa | 204 | return 0; |
1da12e34 RL |
205 | |
206 | if (p != NULL) { | |
207 | BN_free(dh->p); | |
208 | dh->p = p; | |
209 | } | |
210 | if (q != NULL) { | |
211 | BN_free(dh->q); | |
212 | dh->q = q; | |
213 | } | |
214 | if (g != NULL) { | |
215 | BN_free(dh->g); | |
216 | dh->g = g; | |
217 | } | |
0aeddcfa MC |
218 | |
219 | if (q != NULL) { | |
220 | dh->length = BN_num_bits(q); | |
221 | } | |
222 | ||
223 | return 1; | |
224 | } | |
225 | ||
226 | long DH_get_length(const DH *dh) | |
227 | { | |
228 | return dh->length; | |
229 | } | |
230 | ||
231 | int DH_set_length(DH *dh, long length) | |
232 | { | |
233 | dh->length = length; | |
234 | return 1; | |
235 | } | |
236 | ||
237 | void DH_get0_key(const DH *dh, BIGNUM **pub_key, BIGNUM **priv_key) | |
238 | { | |
239 | if (pub_key != NULL) | |
240 | *pub_key = dh->pub_key; | |
241 | if (priv_key != NULL) | |
242 | *priv_key = dh->priv_key; | |
243 | } | |
244 | ||
245 | int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) | |
246 | { | |
1da12e34 RL |
247 | /* If the pub_key in dh is NULL, the corresponding input |
248 | * parameters MUST be non-NULL. The priv_key field may | |
249 | * be left NULL. | |
250 | * | |
251 | * It is an error to give the results from get0 on dh | |
252 | * as input parameters. | |
253 | */ | |
254 | if (dh->pub_key == pub_key | |
4a397f51 | 255 | || (dh->priv_key != NULL && priv_key == dh->priv_key)) |
0aeddcfa MC |
256 | return 0; |
257 | ||
1da12e34 RL |
258 | if (pub_key != NULL) { |
259 | BN_free(dh->pub_key); | |
260 | dh->pub_key = pub_key; | |
261 | } | |
262 | if (priv_key != NULL) { | |
263 | BN_free(dh->priv_key); | |
264 | dh->priv_key = priv_key; | |
265 | } | |
0aeddcfa MC |
266 | |
267 | return 1; | |
268 | } | |
269 | ||
270 | void DH_clear_flags(DH *dh, int flags) | |
271 | { | |
272 | dh->flags &= ~flags; | |
273 | } | |
274 | ||
275 | int DH_test_flags(const DH *dh, int flags) | |
276 | { | |
277 | return dh->flags & flags; | |
278 | } | |
279 | ||
280 | void DH_set_flags(DH *dh, int flags) | |
281 | { | |
282 | dh->flags |= flags; | |
283 | } | |
284 | ||
285 | ENGINE *DH_get0_engine(DH *dh) | |
286 | { | |
287 | return dh->engine; | |
288 | } |