summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dsa/dsa_lib.c
diff options
context:
space:
mode:
authortb <>2023-08-12 06:14:36 +0000
committertb <>2023-08-12 06:14:36 +0000
commit7dd9dcbc7a655f702d9a2f3f641a158d5f2d2566 (patch)
tree8a0516229988a4b4d44324162099ad13ebef80fa /src/lib/libcrypto/dsa/dsa_lib.c
parentdc545d973cd674616909f2370a243a5ca42d85d1 (diff)
downloadopenbsd-7dd9dcbc7a655f702d9a2f3f641a158d5f2d2566.tar.gz
openbsd-7dd9dcbc7a655f702d9a2f3f641a158d5f2d2566.tar.bz2
openbsd-7dd9dcbc7a655f702d9a2f3f641a158d5f2d2566.zip
Convert {DH,DSA}_new_method() to using calloc()
Due to OPENSSL_NO_ENGINE the engine member of dh and dsa is currently uninitialized. As a consequence, {DH,DSA}_get0_engine() will return a garbage pointer, which is particularly bad because the only reason we kept them in the first place is that they are used by some software... A side effect of freeing with {DH,DSA}_free() instead of a hand-rolled version is that we may call ->meth->finish() before ->meth->init() was called. We need a NULL check for ->meth to be on the safe side in case we should need to bring ENGINE back. with nits from djm ok deraadt djm
Diffstat (limited to 'src/lib/libcrypto/dsa/dsa_lib.c')
-rw-r--r--src/lib/libcrypto/dsa/dsa_lib.c65
1 files changed, 25 insertions, 40 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_lib.c b/src/lib/libcrypto/dsa/dsa_lib.c
index 46a7dbcfbe..a9d2179aeb 100644
--- a/src/lib/libcrypto/dsa/dsa_lib.c
+++ b/src/lib/libcrypto/dsa/dsa_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: dsa_lib.c,v 1.43 2023/07/08 14:28:15 beck Exp $ */ 1/* $OpenBSD: dsa_lib.c,v 1.44 2023/08/12 06:14:36 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -127,61 +127,46 @@ LCRYPTO_ALIAS(DSA_set_method);
127DSA * 127DSA *
128DSA_new_method(ENGINE *engine) 128DSA_new_method(ENGINE *engine)
129{ 129{
130 DSA *ret; 130 DSA *dsa;
131 131
132 ret = malloc(sizeof(DSA)); 132 if ((dsa = calloc(1, sizeof(DSA))) == NULL) {
133 if (ret == NULL) {
134 DSAerror(ERR_R_MALLOC_FAILURE); 133 DSAerror(ERR_R_MALLOC_FAILURE);
135 return NULL; 134 goto err;
136 } 135 }
137 ret->meth = DSA_get_default_method(); 136
137 dsa->meth = DSA_get_default_method();
138 dsa->flags = dsa->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
139 dsa->references = 1;
140
138#ifndef OPENSSL_NO_ENGINE 141#ifndef OPENSSL_NO_ENGINE
139 if (engine) { 142 if (engine) {
140 if (!ENGINE_init(engine)) { 143 if (!ENGINE_init(engine)) {
141 DSAerror(ERR_R_ENGINE_LIB); 144 DSAerror(ERR_R_ENGINE_LIB);
142 free(ret); 145 goto err;
143 return NULL;
144 } 146 }
145 ret->engine = engine; 147 dsa->engine = engine;
146 } else 148 } else
147 ret->engine = ENGINE_get_default_DSA(); 149 dsa->engine = ENGINE_get_default_DSA();
148 if (ret->engine) { 150 if (dsa->engine != NULL) {
149 ret->meth = ENGINE_get_DSA(ret->engine); 151 if ((dsa->meth = ENGINE_get_DSA(dsa->engine)) == NULL) {
150 if (ret->meth == NULL) {
151 DSAerror(ERR_R_ENGINE_LIB); 152 DSAerror(ERR_R_ENGINE_LIB);
152 ENGINE_finish(ret->engine); 153 goto err;
153 free(ret);
154 return NULL;
155 } 154 }
155 dsa->flags = dsa->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
156 } 156 }
157#endif 157#endif
158 158
159 ret->pad = 0; 159 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data))
160 ret->version = 0; 160 goto err;
161 ret->p = NULL; 161 if (dsa->meth->init != NULL && !dsa->meth->init(dsa))
162 ret->q = NULL; 162 goto err;
163 ret->g = NULL;
164
165 ret->pub_key = NULL;
166 ret->priv_key = NULL;
167 163
168 ret->kinv = NULL; 164 return dsa;
169 ret->r = NULL;
170 ret->method_mont_p = NULL;
171 165
172 ret->references = 1; 166 err:
173 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; 167 DSA_free(dsa);
174 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
175 if (ret->meth->init != NULL && !ret->meth->init(ret)) {
176#ifndef OPENSSL_NO_ENGINE
177 ENGINE_finish(ret->engine);
178#endif
179 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
180 free(ret);
181 ret = NULL;
182 }
183 168
184 return ret; 169 return NULL;
185} 170}
186LCRYPTO_ALIAS(DSA_new_method); 171LCRYPTO_ALIAS(DSA_new_method);
187 172
@@ -197,7 +182,7 @@ DSA_free(DSA *r)
197 if (i > 0) 182 if (i > 0)
198 return; 183 return;
199 184
200 if (r->meth->finish) 185 if (r->meth != NULL && r->meth->finish != NULL)
201 r->meth->finish(r); 186 r->meth->finish(r);
202#ifndef OPENSSL_NO_ENGINE 187#ifndef OPENSSL_NO_ENGINE
203 ENGINE_finish(r->engine); 188 ENGINE_finish(r->engine);