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