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