/src/openssl/crypto/dh/dh_lib.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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 |
8 | | */ |
9 | | |
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 | | |
16 | | #include <stdio.h> |
17 | | #include <openssl/bn.h> |
18 | | #include <openssl/obj_mac.h> |
19 | | #include <openssl/core_names.h> |
20 | | #include "internal/cryptlib.h" |
21 | | #include "internal/refcount.h" |
22 | | #include "internal/common.h" |
23 | | #include "crypto/evp.h" |
24 | | #include "crypto/dh.h" |
25 | | #include "dh_local.h" |
26 | | |
27 | | static DH *dh_new_intern(OSSL_LIB_CTX *libctx); |
28 | | |
29 | | #ifndef FIPS_MODULE |
30 | | int DH_set_method(DH *dh, const DH_METHOD *meth) |
31 | 0 | { |
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 | 0 | const DH_METHOD *mtmp; |
37 | 0 | mtmp = dh->meth; |
38 | 0 | if (mtmp->finish) |
39 | 0 | mtmp->finish(dh); |
40 | 0 | dh->meth = meth; |
41 | 0 | if (meth->init) |
42 | 0 | meth->init(dh); |
43 | 0 | return 1; |
44 | 0 | } |
45 | | |
46 | | const DH_METHOD *ossl_dh_get_method(const DH *dh) |
47 | 0 | { |
48 | 0 | return dh->meth; |
49 | 0 | } |
50 | | # ifndef OPENSSL_NO_DEPRECATED_3_0 |
51 | | DH *DH_new(void) |
52 | 0 | { |
53 | 0 | return dh_new_intern(NULL); |
54 | 0 | } |
55 | | # endif |
56 | | |
57 | | DH *DH_new_method(ENGINE *engine) |
58 | 0 | { |
59 | 0 | if (!ossl_assert(engine == NULL)) |
60 | 0 | return NULL; |
61 | 0 | return dh_new_intern(NULL); |
62 | 0 | } |
63 | | #endif /* !FIPS_MODULE */ |
64 | | |
65 | | DH *ossl_dh_new_ex(OSSL_LIB_CTX *libctx) |
66 | 0 | { |
67 | 0 | return dh_new_intern(libctx); |
68 | 0 | } |
69 | | |
70 | | static DH *dh_new_intern(OSSL_LIB_CTX *libctx) |
71 | 0 | { |
72 | 0 | DH *ret = OPENSSL_zalloc(sizeof(*ret)); |
73 | |
|
74 | 0 | if (ret == NULL) |
75 | 0 | return NULL; |
76 | | |
77 | 0 | ret->lock = CRYPTO_THREAD_lock_new(); |
78 | 0 | if (ret->lock == NULL) { |
79 | 0 | ERR_raise(ERR_LIB_DH, ERR_R_CRYPTO_LIB); |
80 | 0 | OPENSSL_free(ret); |
81 | 0 | return NULL; |
82 | 0 | } |
83 | | |
84 | 0 | if (!CRYPTO_NEW_REF(&ret->references, 1)) { |
85 | 0 | CRYPTO_THREAD_lock_free(ret->lock); |
86 | 0 | OPENSSL_free(ret); |
87 | 0 | return NULL; |
88 | 0 | } |
89 | | |
90 | 0 | ret->libctx = libctx; |
91 | 0 | ret->meth = DH_get_default_method(); |
92 | 0 | ret->flags = ret->meth->flags; |
93 | |
|
94 | 0 | #ifndef FIPS_MODULE |
95 | 0 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data)) |
96 | 0 | goto err; |
97 | 0 | #endif /* FIPS_MODULE */ |
98 | | |
99 | 0 | ossl_ffc_params_init(&ret->params); |
100 | |
|
101 | 0 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { |
102 | 0 | ERR_raise(ERR_LIB_DH, ERR_R_INIT_FAIL); |
103 | 0 | goto err; |
104 | 0 | } |
105 | | |
106 | 0 | return ret; |
107 | | |
108 | 0 | err: |
109 | 0 | DH_free(ret); |
110 | 0 | return NULL; |
111 | 0 | } |
112 | | |
113 | | void DH_free(DH *r) |
114 | 0 | { |
115 | 0 | int i; |
116 | |
|
117 | 0 | if (r == NULL) |
118 | 0 | return; |
119 | | |
120 | 0 | CRYPTO_DOWN_REF(&r->references, &i); |
121 | 0 | REF_PRINT_COUNT("DH", i, r); |
122 | 0 | if (i > 0) |
123 | 0 | return; |
124 | 0 | REF_ASSERT_ISNT(i < 0); |
125 | |
|
126 | 0 | if (r->meth != NULL && r->meth->finish != NULL) |
127 | 0 | r->meth->finish(r); |
128 | 0 | #if !defined(FIPS_MODULE) |
129 | 0 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); |
130 | 0 | #endif |
131 | |
|
132 | 0 | CRYPTO_THREAD_lock_free(r->lock); |
133 | 0 | CRYPTO_FREE_REF(&r->references); |
134 | |
|
135 | 0 | ossl_ffc_params_cleanup(&r->params); |
136 | 0 | BN_clear_free(r->pub_key); |
137 | 0 | BN_clear_free(r->priv_key); |
138 | 0 | OPENSSL_free(r); |
139 | 0 | } |
140 | | |
141 | | int DH_up_ref(DH *r) |
142 | 0 | { |
143 | 0 | int i; |
144 | |
|
145 | 0 | if (CRYPTO_UP_REF(&r->references, &i) <= 0) |
146 | 0 | return 0; |
147 | | |
148 | 0 | REF_PRINT_COUNT("DH", i, r); |
149 | 0 | REF_ASSERT_ISNT(i < 2); |
150 | 0 | return ((i > 1) ? 1 : 0); |
151 | 0 | } |
152 | | |
153 | | void ossl_dh_set0_libctx(DH *d, OSSL_LIB_CTX *libctx) |
154 | 0 | { |
155 | 0 | d->libctx = libctx; |
156 | 0 | } |
157 | | |
158 | | #ifndef FIPS_MODULE |
159 | | int DH_set_ex_data(DH *d, int idx, void *arg) |
160 | 0 | { |
161 | 0 | return CRYPTO_set_ex_data(&d->ex_data, idx, arg); |
162 | 0 | } |
163 | | |
164 | | void *DH_get_ex_data(const DH *d, int idx) |
165 | 0 | { |
166 | 0 | return CRYPTO_get_ex_data(&d->ex_data, idx); |
167 | 0 | } |
168 | | #endif |
169 | | |
170 | | int DH_bits(const DH *dh) |
171 | 0 | { |
172 | 0 | if (dh->params.p != NULL) |
173 | 0 | return BN_num_bits(dh->params.p); |
174 | 0 | return -1; |
175 | 0 | } |
176 | | |
177 | | int DH_size(const DH *dh) |
178 | 0 | { |
179 | 0 | if (dh->params.p != NULL) |
180 | 0 | return BN_num_bytes(dh->params.p); |
181 | 0 | return -1; |
182 | 0 | } |
183 | | |
184 | | int DH_security_bits(const DH *dh) |
185 | 0 | { |
186 | 0 | int N; |
187 | |
|
188 | 0 | if (dh->params.q != NULL) |
189 | 0 | N = BN_num_bits(dh->params.q); |
190 | 0 | else if (dh->length) |
191 | 0 | N = dh->length; |
192 | 0 | else |
193 | 0 | N = -1; |
194 | 0 | if (dh->params.p != NULL) |
195 | 0 | return BN_security_bits(BN_num_bits(dh->params.p), N); |
196 | 0 | return -1; |
197 | 0 | } |
198 | | |
199 | | void DH_get0_pqg(const DH *dh, |
200 | | const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) |
201 | 0 | { |
202 | 0 | ossl_ffc_params_get0_pqg(&dh->params, p, q, g); |
203 | 0 | } |
204 | | |
205 | | int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) |
206 | 0 | { |
207 | | /* |
208 | | * If the fields p and g in dh are NULL, the corresponding input |
209 | | * parameters MUST be non-NULL. q may remain NULL. |
210 | | */ |
211 | 0 | if ((dh->params.p == NULL && p == NULL) |
212 | 0 | || (dh->params.g == NULL && g == NULL)) |
213 | 0 | return 0; |
214 | | |
215 | 0 | ossl_ffc_params_set0_pqg(&dh->params, p, q, g); |
216 | 0 | ossl_dh_cache_named_group(dh); |
217 | 0 | dh->dirty_cnt++; |
218 | 0 | return 1; |
219 | 0 | } |
220 | | |
221 | | long DH_get_length(const DH *dh) |
222 | 0 | { |
223 | 0 | return dh->length; |
224 | 0 | } |
225 | | |
226 | | int DH_set_length(DH *dh, long length) |
227 | 0 | { |
228 | 0 | dh->length = length; |
229 | 0 | dh->dirty_cnt++; |
230 | 0 | return 1; |
231 | 0 | } |
232 | | |
233 | | void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key) |
234 | 0 | { |
235 | 0 | if (pub_key != NULL) |
236 | 0 | *pub_key = dh->pub_key; |
237 | 0 | if (priv_key != NULL) |
238 | 0 | *priv_key = dh->priv_key; |
239 | 0 | } |
240 | | |
241 | | int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) |
242 | 0 | { |
243 | 0 | if (pub_key != NULL) { |
244 | 0 | BN_clear_free(dh->pub_key); |
245 | 0 | dh->pub_key = pub_key; |
246 | 0 | } |
247 | 0 | if (priv_key != NULL) { |
248 | 0 | BN_clear_free(dh->priv_key); |
249 | 0 | dh->priv_key = priv_key; |
250 | 0 | } |
251 | |
|
252 | 0 | dh->dirty_cnt++; |
253 | 0 | return 1; |
254 | 0 | } |
255 | | |
256 | | const BIGNUM *DH_get0_p(const DH *dh) |
257 | 0 | { |
258 | 0 | return dh->params.p; |
259 | 0 | } |
260 | | |
261 | | const BIGNUM *DH_get0_q(const DH *dh) |
262 | 0 | { |
263 | 0 | return dh->params.q; |
264 | 0 | } |
265 | | |
266 | | const BIGNUM *DH_get0_g(const DH *dh) |
267 | 0 | { |
268 | 0 | return dh->params.g; |
269 | 0 | } |
270 | | |
271 | | const BIGNUM *DH_get0_priv_key(const DH *dh) |
272 | 0 | { |
273 | 0 | return dh->priv_key; |
274 | 0 | } |
275 | | |
276 | | const BIGNUM *DH_get0_pub_key(const DH *dh) |
277 | 0 | { |
278 | 0 | return dh->pub_key; |
279 | 0 | } |
280 | | |
281 | | void DH_clear_flags(DH *dh, int flags) |
282 | 0 | { |
283 | 0 | dh->flags &= ~flags; |
284 | 0 | } |
285 | | |
286 | | int DH_test_flags(const DH *dh, int flags) |
287 | 0 | { |
288 | 0 | return dh->flags & flags; |
289 | 0 | } |
290 | | |
291 | | void DH_set_flags(DH *dh, int flags) |
292 | 0 | { |
293 | 0 | dh->flags |= flags; |
294 | 0 | } |
295 | | |
296 | | FFC_PARAMS *ossl_dh_get0_params(DH *dh) |
297 | 0 | { |
298 | 0 | return &dh->params; |
299 | 0 | } |
300 | | int ossl_dh_get0_nid(const DH *dh) |
301 | 0 | { |
302 | 0 | return dh->params.nid; |
303 | 0 | } |