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