/src/openssl111/crypto/dh/dh_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include "internal/refcount.h" |
13 | | #include <openssl/bn.h> |
14 | | #include "dh_local.h" |
15 | | #include <openssl/engine.h> |
16 | | |
17 | | int DH_set_method(DH *dh, const DH_METHOD *meth) |
18 | 0 | { |
19 | | /* |
20 | | * NB: The caller is specifically setting a method, so it's not up to us |
21 | | * to deal with which ENGINE it comes from. |
22 | | */ |
23 | 0 | const DH_METHOD *mtmp; |
24 | 0 | mtmp = dh->meth; |
25 | 0 | if (mtmp->finish) |
26 | 0 | mtmp->finish(dh); |
27 | 0 | #ifndef OPENSSL_NO_ENGINE |
28 | 0 | ENGINE_finish(dh->engine); |
29 | 0 | dh->engine = NULL; |
30 | 0 | #endif |
31 | 0 | dh->meth = meth; |
32 | 0 | if (meth->init) |
33 | 0 | meth->init(dh); |
34 | 0 | return 1; |
35 | 0 | } |
36 | | |
37 | | DH *DH_new(void) |
38 | 799 | { |
39 | 799 | return DH_new_method(NULL); |
40 | 799 | } |
41 | | |
42 | | DH *DH_new_method(ENGINE *engine) |
43 | 799 | { |
44 | 799 | DH *ret = OPENSSL_zalloc(sizeof(*ret)); |
45 | | |
46 | 799 | if (ret == NULL) { |
47 | 0 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE); |
48 | 0 | return NULL; |
49 | 0 | } |
50 | | |
51 | 799 | ret->references = 1; |
52 | 799 | ret->lock = CRYPTO_THREAD_lock_new(); |
53 | 799 | if (ret->lock == NULL) { |
54 | 0 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE); |
55 | 0 | OPENSSL_free(ret); |
56 | 0 | return NULL; |
57 | 0 | } |
58 | | |
59 | 799 | ret->meth = DH_get_default_method(); |
60 | 799 | #ifndef OPENSSL_NO_ENGINE |
61 | 799 | ret->flags = ret->meth->flags; /* early default init */ |
62 | 799 | if (engine) { |
63 | 0 | if (!ENGINE_init(engine)) { |
64 | 0 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); |
65 | 0 | goto err; |
66 | 0 | } |
67 | 0 | ret->engine = engine; |
68 | 0 | } else |
69 | 799 | ret->engine = ENGINE_get_default_DH(); |
70 | 799 | if (ret->engine) { |
71 | 0 | ret->meth = ENGINE_get_DH(ret->engine); |
72 | 0 | if (ret->meth == NULL) { |
73 | 0 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); |
74 | 0 | goto err; |
75 | 0 | } |
76 | 0 | } |
77 | 799 | #endif |
78 | | |
79 | 799 | ret->flags = ret->meth->flags; |
80 | | |
81 | 799 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data)) |
82 | 0 | goto err; |
83 | | |
84 | 799 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { |
85 | 0 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL); |
86 | 0 | goto err; |
87 | 0 | } |
88 | | |
89 | 799 | return ret; |
90 | | |
91 | 0 | err: |
92 | 0 | DH_free(ret); |
93 | 0 | return NULL; |
94 | 799 | } |
95 | | |
96 | | void DH_free(DH *r) |
97 | 2.50k | { |
98 | 2.50k | int i; |
99 | | |
100 | 2.50k | if (r == NULL) |
101 | 1.70k | return; |
102 | | |
103 | 799 | CRYPTO_DOWN_REF(&r->references, &i, r->lock); |
104 | 799 | REF_PRINT_COUNT("DH", r); |
105 | 799 | if (i > 0) |
106 | 0 | return; |
107 | 799 | REF_ASSERT_ISNT(i < 0); |
108 | | |
109 | 799 | if (r->meth != NULL && r->meth->finish != NULL) |
110 | 799 | r->meth->finish(r); |
111 | 799 | #ifndef OPENSSL_NO_ENGINE |
112 | 799 | ENGINE_finish(r->engine); |
113 | 799 | #endif |
114 | | |
115 | 799 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); |
116 | | |
117 | 799 | CRYPTO_THREAD_lock_free(r->lock); |
118 | | |
119 | 799 | BN_clear_free(r->p); |
120 | 799 | BN_clear_free(r->g); |
121 | 799 | BN_clear_free(r->q); |
122 | 799 | BN_clear_free(r->j); |
123 | 799 | OPENSSL_free(r->seed); |
124 | 799 | BN_clear_free(r->counter); |
125 | 799 | BN_clear_free(r->pub_key); |
126 | 799 | BN_clear_free(r->priv_key); |
127 | 799 | OPENSSL_free(r); |
128 | 799 | } |
129 | | |
130 | | int DH_up_ref(DH *r) |
131 | 0 | { |
132 | 0 | int i; |
133 | |
|
134 | 0 | if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0) |
135 | 0 | return 0; |
136 | | |
137 | 0 | REF_PRINT_COUNT("DH", r); |
138 | 0 | REF_ASSERT_ISNT(i < 2); |
139 | 0 | return ((i > 1) ? 1 : 0); |
140 | 0 | } |
141 | | |
142 | | int DH_set_ex_data(DH *d, int idx, void *arg) |
143 | 0 | { |
144 | 0 | return CRYPTO_set_ex_data(&d->ex_data, idx, arg); |
145 | 0 | } |
146 | | |
147 | | void *DH_get_ex_data(DH *d, int idx) |
148 | 0 | { |
149 | 0 | return CRYPTO_get_ex_data(&d->ex_data, idx); |
150 | 0 | } |
151 | | |
152 | | int DH_bits(const DH *dh) |
153 | 0 | { |
154 | 0 | return BN_num_bits(dh->p); |
155 | 0 | } |
156 | | |
157 | | int DH_size(const DH *dh) |
158 | 0 | { |
159 | 0 | return BN_num_bytes(dh->p); |
160 | 0 | } |
161 | | |
162 | | int DH_security_bits(const DH *dh) |
163 | 0 | { |
164 | 0 | int N; |
165 | 0 | if (dh->q) |
166 | 0 | N = BN_num_bits(dh->q); |
167 | 0 | else if (dh->length) |
168 | 0 | N = dh->length; |
169 | 0 | else |
170 | 0 | N = -1; |
171 | 0 | return BN_security_bits(BN_num_bits(dh->p), N); |
172 | 0 | } |
173 | | |
174 | | |
175 | | void DH_get0_pqg(const DH *dh, |
176 | | const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) |
177 | 0 | { |
178 | 0 | if (p != NULL) |
179 | 0 | *p = dh->p; |
180 | 0 | if (q != NULL) |
181 | 0 | *q = dh->q; |
182 | 0 | if (g != NULL) |
183 | 0 | *g = dh->g; |
184 | 0 | } |
185 | | |
186 | | int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) |
187 | 0 | { |
188 | | /* If the fields p and g in d are NULL, the corresponding input |
189 | | * parameters MUST be non-NULL. q may remain NULL. |
190 | | */ |
191 | 0 | if ((dh->p == NULL && p == NULL) |
192 | 0 | || (dh->g == NULL && g == NULL)) |
193 | 0 | return 0; |
194 | | |
195 | 0 | if (p != NULL) { |
196 | 0 | BN_free(dh->p); |
197 | 0 | dh->p = p; |
198 | 0 | } |
199 | 0 | if (q != NULL) { |
200 | 0 | BN_free(dh->q); |
201 | 0 | dh->q = q; |
202 | 0 | } |
203 | 0 | if (g != NULL) { |
204 | 0 | BN_free(dh->g); |
205 | 0 | dh->g = g; |
206 | 0 | } |
207 | |
|
208 | 0 | if (q != NULL) { |
209 | 0 | dh->length = BN_num_bits(q); |
210 | 0 | } |
211 | |
|
212 | 0 | return 1; |
213 | 0 | } |
214 | | |
215 | | long DH_get_length(const DH *dh) |
216 | 0 | { |
217 | 0 | return dh->length; |
218 | 0 | } |
219 | | |
220 | | int DH_set_length(DH *dh, long length) |
221 | 0 | { |
222 | 0 | dh->length = length; |
223 | 0 | return 1; |
224 | 0 | } |
225 | | |
226 | | void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key) |
227 | 0 | { |
228 | 0 | if (pub_key != NULL) |
229 | 0 | *pub_key = dh->pub_key; |
230 | 0 | if (priv_key != NULL) |
231 | 0 | *priv_key = dh->priv_key; |
232 | 0 | } |
233 | | |
234 | | int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) |
235 | 0 | { |
236 | 0 | if (pub_key != NULL) { |
237 | 0 | BN_clear_free(dh->pub_key); |
238 | 0 | dh->pub_key = pub_key; |
239 | 0 | } |
240 | 0 | if (priv_key != NULL) { |
241 | 0 | BN_clear_free(dh->priv_key); |
242 | 0 | dh->priv_key = priv_key; |
243 | 0 | } |
244 | |
|
245 | 0 | return 1; |
246 | 0 | } |
247 | | |
248 | | const BIGNUM *DH_get0_p(const DH *dh) |
249 | 0 | { |
250 | 0 | return dh->p; |
251 | 0 | } |
252 | | |
253 | | const BIGNUM *DH_get0_q(const DH *dh) |
254 | 0 | { |
255 | 0 | return dh->q; |
256 | 0 | } |
257 | | |
258 | | const BIGNUM *DH_get0_g(const DH *dh) |
259 | 0 | { |
260 | 0 | return dh->g; |
261 | 0 | } |
262 | | |
263 | | const BIGNUM *DH_get0_priv_key(const DH *dh) |
264 | 0 | { |
265 | 0 | return dh->priv_key; |
266 | 0 | } |
267 | | |
268 | | const BIGNUM *DH_get0_pub_key(const DH *dh) |
269 | 0 | { |
270 | 0 | return dh->pub_key; |
271 | 0 | } |
272 | | |
273 | | void DH_clear_flags(DH *dh, int flags) |
274 | 0 | { |
275 | 0 | dh->flags &= ~flags; |
276 | 0 | } |
277 | | |
278 | | int DH_test_flags(const DH *dh, int flags) |
279 | 0 | { |
280 | 0 | return dh->flags & flags; |
281 | 0 | } |
282 | | |
283 | | void DH_set_flags(DH *dh, int flags) |
284 | 0 | { |
285 | 0 | dh->flags |= flags; |
286 | 0 | } |
287 | | |
288 | | ENGINE *DH_get0_engine(DH *dh) |
289 | 0 | { |
290 | 0 | return dh->engine; |
291 | 0 | } |