diff options
author | Joel Sing <jsing@cvs.openbsd.org> | 2019-11-01 03:41:41 +0000 |
---|---|---|
committer | Joel Sing <jsing@cvs.openbsd.org> | 2019-11-01 03:41:41 +0000 |
commit | 3b6a51cf6def2d0a30b21f61217f2170447fd909 (patch) | |
tree | b3da9ea4cfccfb5be5be4c4b04887795b81fb021 | |
parent | ff9f81b16b742f9f8cf9b620105ecb87e10acccd (diff) |
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@
-rw-r--r-- | lib/libcrypto/rsa/rsa_lib.c | 64 |
1 files changed, 24 insertions, 40 deletions
diff --git a/lib/libcrypto/rsa/rsa_lib.c b/lib/libcrypto/rsa/rsa_lib.c index bf6865d2606..7cae5cb2eda 100644 --- a/lib/libcrypto/rsa/rsa_lib.c +++ b/lib/libcrypto/rsa/rsa_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_lib.c,v 1.38 2019/10/24 15:47:15 jsing Exp $ */ +/* $OpenBSD: rsa_lib.c,v 1.39 2019/11/01 03:41:40 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -131,68 +131,52 @@ RSA_new_method(ENGINE *engine) { RSA *ret; - ret = malloc(sizeof(RSA)); - if (ret == NULL) { + if ((ret = calloc(1, sizeof(RSA))) == NULL) { RSAerror(ERR_R_MALLOC_FAILURE); return NULL; } ret->meth = RSA_get_default_method(); + #ifndef OPENSSL_NO_ENGINE - if (engine) { + if (engine != NULL) { if (!ENGINE_init(engine)) { RSAerror(ERR_R_ENGINE_LIB); - free(ret); - return NULL; + goto err; } ret->engine = engine; - } else + } else { ret->engine = ENGINE_get_default_RSA(); - if (ret->engine) { - ret->meth = ENGINE_get_RSA(ret->engine); - if (ret->meth == NULL) { + } + + if (ret->engine != NULL) { + if ((ret->meth = ENGINE_get_RSA(ret->engine)) == NULL) { RSAerror(ERR_R_ENGINE_LIB); - ENGINE_finish(ret->engine); - free(ret); - return NULL; + goto err; } } #endif - ret->pad = 0; - ret->version = 0; - ret->n = NULL; - ret->e = NULL; - ret->d = NULL; - ret->p = NULL; - ret->q = NULL; - ret->dmp1 = NULL; - ret->dmq1 = NULL; - ret->iqmp = NULL; ret->references = 1; - ret->_method_mod_n = NULL; - ret->_method_mod_p = NULL; - ret->_method_mod_q = NULL; - ret->blinding = NULL; - ret->mt_blinding = NULL; ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; - if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) { -#ifndef OPENSSL_NO_ENGINE - ENGINE_finish(ret->engine); -#endif - free(ret); - return NULL; - } + + if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) + goto err; if (ret->meth->init != NULL && !ret->meth->init(ret)) { -#ifndef OPENSSL_NO_ENGINE - ENGINE_finish(ret->engine); -#endif CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data); - free(ret); - ret = NULL; + goto err; } + return ret; + + err: +#ifndef OPENSSL_NO_ENGINE + ENGINE_finish(ret->engine); +#endif + free(ret); + + return NULL; } void |