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