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