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