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