提交 11ed851d 编写于 作者: F FdaSilvaYY 提交者: Matt Caswell

Fix and simplify error handling in (RSA/EC_kmeth)_new_method()

Inspired from PR #873.
Nearly same as 2bbf0baa.
Reviewed-by: NRich Salz <rsalz@openssl.org>
Reviewed-by: NMatt Caswell <matt@openssl.org>
上级 aca6dae9
...@@ -78,15 +78,11 @@ EC_KEY *EC_KEY_new_method(ENGINE *engine) ...@@ -78,15 +78,11 @@ EC_KEY *EC_KEY_new_method(ENGINE *engine)
ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE); ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE);
return NULL; return NULL;
} }
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
OPENSSL_free(ret);
return NULL;
}
ret->references = 1;
ret->lock = CRYPTO_THREAD_lock_new(); ret->lock = CRYPTO_THREAD_lock_new();
if (ret->lock == NULL) { if (ret->lock == NULL) {
ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE); ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE);
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data);
OPENSSL_free(ret); OPENSSL_free(ret);
return NULL; return NULL;
} }
...@@ -96,10 +92,7 @@ EC_KEY *EC_KEY_new_method(ENGINE *engine) ...@@ -96,10 +92,7 @@ EC_KEY *EC_KEY_new_method(ENGINE *engine)
if (engine != NULL) { if (engine != NULL) {
if (!ENGINE_init(engine)) { if (!ENGINE_init(engine)) {
ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB); ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data); goto err;
CRYPTO_THREAD_lock_free(ret->lock);
OPENSSL_free(ret);
return NULL;
} }
ret->engine = engine; ret->engine = engine;
} else } else
...@@ -108,25 +101,27 @@ EC_KEY *EC_KEY_new_method(ENGINE *engine) ...@@ -108,25 +101,27 @@ EC_KEY *EC_KEY_new_method(ENGINE *engine)
ret->meth = ENGINE_get_EC(ret->engine); ret->meth = ENGINE_get_EC(ret->engine);
if (ret->meth == NULL) { if (ret->meth == NULL) {
ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB); ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
ENGINE_finish(ret->engine); goto err;
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data);
CRYPTO_THREAD_lock_free(ret->lock);
OPENSSL_free(ret);
return NULL;
} }
} }
#endif #endif
ret->version = 1; ret->version = 1;
ret->conv_form = POINT_CONVERSION_UNCOMPRESSED; ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
ret->references = 1;
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
goto err;
}
if (ret->meth->init != NULL && ret->meth->init(ret) == 0) { if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_INIT_FAIL); ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_INIT_FAIL);
EC_KEY_free(ret); goto err;
return NULL;
} }
return ret; return ret;
err:
EC_KEY_free(ret);
return NULL;
} }
int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
......
...@@ -70,21 +70,28 @@ int RSA_set_method(RSA *rsa, const RSA_METHOD *meth) ...@@ -70,21 +70,28 @@ int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
RSA *RSA_new_method(ENGINE *engine) RSA *RSA_new_method(ENGINE *engine)
{ {
RSA *ret; RSA *ret = OPENSSL_zalloc(sizeof(*ret));
ret = OPENSSL_zalloc(sizeof(*ret));
if (ret == NULL) { if (ret == NULL) {
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
return NULL; return NULL;
} }
ret->references = 1;
ret->lock = CRYPTO_THREAD_lock_new();
if (ret->lock == NULL) {
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
OPENSSL_free(ret);
return NULL;
}
ret->meth = RSA_get_default_method(); ret->meth = RSA_get_default_method();
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
if (engine) { if (engine) {
if (!ENGINE_init(engine)) { if (!ENGINE_init(engine)) {
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
OPENSSL_free(ret); goto err;
return NULL;
} }
ret->engine = engine; ret->engine = engine;
} else } else
...@@ -93,39 +100,26 @@ RSA *RSA_new_method(ENGINE *engine) ...@@ -93,39 +100,26 @@ RSA *RSA_new_method(ENGINE *engine)
ret->meth = ENGINE_get_RSA(ret->engine); ret->meth = ENGINE_get_RSA(ret->engine);
if (ret->meth == NULL) { if (ret->meth == NULL) {
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
ENGINE_finish(ret->engine); goto err;
OPENSSL_free(ret);
return NULL;
} }
} }
#endif #endif
ret->references = 1;
ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) { if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
#ifndef OPENSSL_NO_ENGINE goto err;
ENGINE_finish(ret->engine);
#endif
OPENSSL_free(ret);
return NULL;
}
ret->lock = CRYPTO_THREAD_lock_new();
if (ret->lock == NULL) {
#ifndef OPENSSL_NO_ENGINE
ENGINE_finish(ret->engine);
#endif
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
OPENSSL_free(ret);
return NULL;
} }
if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
RSA_free(ret); RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
ret = NULL; goto err;
} }
return ret; return ret;
err:
RSA_free(ret);
return NULL;
} }
void RSA_free(RSA *r) void RSA_free(RSA *r)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册