summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2019-11-01 03:41:40 +0000
committerjsing <>2019-11-01 03:41:40 +0000
commit207d5072457060a56e0ae65ce9e652e88178d798 (patch)
treed88de625441e4ba372a575c1b1eb494e21ed0046 /src/lib
parentb4d73e8ad801a43d9f7b4819e067284405812833 (diff)
downloadopenbsd-207d5072457060a56e0ae65ce9e652e88178d798.tar.gz
openbsd-207d5072457060a56e0ae65ce9e652e88178d798.tar.bz2
openbsd-207d5072457060a56e0ae65ce9e652e88178d798.zip
Clean up RSA_new_method().
Use calloc() instead of malloc() for initialisation and remove explicit zero initialisation of members. This ensures that new members always get initialised. Also use a single error return path, simplifying code. ok tb@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/rsa/rsa_lib.c64
1 files changed, 24 insertions, 40 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_lib.c b/src/lib/libcrypto/rsa/rsa_lib.c
index bf6865d260..7cae5cb2ed 100644
--- a/src/lib/libcrypto/rsa/rsa_lib.c
+++ b/src/lib/libcrypto/rsa/rsa_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: rsa_lib.c,v 1.38 2019/10/24 15:47:15 jsing Exp $ */ 1/* $OpenBSD: rsa_lib.c,v 1.39 2019/11/01 03:41:40 jsing 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 *
@@ -131,68 +131,52 @@ RSA_new_method(ENGINE *engine)
131{ 131{
132 RSA *ret; 132 RSA *ret;
133 133
134 ret = malloc(sizeof(RSA)); 134 if ((ret = calloc(1, sizeof(RSA))) == NULL) {
135 if (ret == NULL) {
136 RSAerror(ERR_R_MALLOC_FAILURE); 135 RSAerror(ERR_R_MALLOC_FAILURE);
137 return NULL; 136 return NULL;
138 } 137 }
139 138
140 ret->meth = RSA_get_default_method(); 139 ret->meth = RSA_get_default_method();
140
141#ifndef OPENSSL_NO_ENGINE 141#ifndef OPENSSL_NO_ENGINE
142 if (engine) { 142 if (engine != NULL) {
143 if (!ENGINE_init(engine)) { 143 if (!ENGINE_init(engine)) {
144 RSAerror(ERR_R_ENGINE_LIB); 144 RSAerror(ERR_R_ENGINE_LIB);
145 free(ret); 145 goto err;
146 return NULL;
147 } 146 }
148 ret->engine = engine; 147 ret->engine = engine;
149 } else 148 } else {
150 ret->engine = ENGINE_get_default_RSA(); 149 ret->engine = ENGINE_get_default_RSA();
151 if (ret->engine) { 150 }
152 ret->meth = ENGINE_get_RSA(ret->engine); 151
153 if (ret->meth == NULL) { 152 if (ret->engine != NULL) {
153 if ((ret->meth = ENGINE_get_RSA(ret->engine)) == NULL) {
154 RSAerror(ERR_R_ENGINE_LIB); 154 RSAerror(ERR_R_ENGINE_LIB);
155 ENGINE_finish(ret->engine); 155 goto err;
156 free(ret);
157 return NULL;
158 } 156 }
159 } 157 }
160#endif 158#endif
161 159
162 ret->pad = 0;
163 ret->version = 0;
164 ret->n = NULL;
165 ret->e = NULL;
166 ret->d = NULL;
167 ret->p = NULL;
168 ret->q = NULL;
169 ret->dmp1 = NULL;
170 ret->dmq1 = NULL;
171 ret->iqmp = NULL;
172 ret->references = 1; 160 ret->references = 1;
173 ret->_method_mod_n = NULL;
174 ret->_method_mod_p = NULL;
175 ret->_method_mod_q = NULL;
176 ret->blinding = NULL;
177 ret->mt_blinding = NULL;
178 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; 161 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
179 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) { 162
180#ifndef OPENSSL_NO_ENGINE 163 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data))
181 ENGINE_finish(ret->engine); 164 goto err;
182#endif
183 free(ret);
184 return NULL;
185 }
186 165
187 if (ret->meth->init != NULL && !ret->meth->init(ret)) { 166 if (ret->meth->init != NULL && !ret->meth->init(ret)) {
188#ifndef OPENSSL_NO_ENGINE
189 ENGINE_finish(ret->engine);
190#endif
191 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data); 167 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
192 free(ret); 168 goto err;
193 ret = NULL;
194 } 169 }
170
195 return ret; 171 return ret;
172
173 err:
174#ifndef OPENSSL_NO_ENGINE
175 ENGINE_finish(ret->engine);
176#endif
177 free(ret);
178
179 return NULL;
196} 180}
197 181
198void 182void