GH715: ENGINE_finish can take NULL

Simplifies calling code.  Also fixed up any !ptr tests that were
nearby, turning them into NULL tests.

Reviewed-by: Richard Levitte <levitte@openssl.org>
This commit is contained in:
Rich Salz 2016-02-25 12:09:06 -05:00 committed by Rich Salz
parent 07b3ce8f80
commit 7c96dbcdab
18 changed files with 48 additions and 91 deletions

View File

@ -317,7 +317,6 @@ int init_gen_str(EVP_PKEY_CTX **pctx,
EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth); EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (tmpeng)
ENGINE_finish(tmpeng); ENGINE_finish(tmpeng);
#endif #endif
ctx = EVP_PKEY_CTX_new_id(pkey_id, e); ctx = EVP_PKEY_CTX_new_id(pkey_id, e);

View File

@ -1376,7 +1376,6 @@ static EVP_PKEY_CTX *set_keygen_ctx(const char *gstr,
EVP_PKEY_asn1_get0_info(NULL, pkey_type, NULL, NULL, NULL, ameth); EVP_PKEY_asn1_get0_info(NULL, pkey_type, NULL, NULL, NULL, ameth);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (tmpeng)
ENGINE_finish(tmpeng); ENGINE_finish(tmpeng);
#endif #endif
if (*pkey_type == EVP_PKEY_RSA) { if (*pkey_type == EVP_PKEY_RSA) {
@ -1434,7 +1433,6 @@ static EVP_PKEY_CTX *set_keygen_ctx(const char *gstr,
EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, &anam, ameth); EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, &anam, ameth);
*palgnam = OPENSSL_strdup(anam); *palgnam = OPENSSL_strdup(anam);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (tmpeng)
ENGINE_finish(tmpeng); ENGINE_finish(tmpeng);
#endif #endif
} }

View File

@ -82,10 +82,8 @@ EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
} else { } else {
ret = *a; ret = *a;
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (ret->engine) {
ENGINE_finish(ret->engine); ENGINE_finish(ret->engine);
ret->engine = NULL; ret->engine = NULL;
}
#endif #endif
} }

View File

@ -88,10 +88,8 @@ int DH_set_method(DH *dh, const DH_METHOD *meth)
if (mtmp->finish) if (mtmp->finish)
mtmp->finish(dh); mtmp->finish(dh);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (dh->engine) {
ENGINE_finish(dh->engine); ENGINE_finish(dh->engine);
dh->engine = NULL; dh->engine = NULL;
}
#endif #endif
dh->meth = meth; dh->meth = meth;
if (meth->init) if (meth->init)
@ -126,7 +124,7 @@ DH *DH_new_method(ENGINE *engine)
ret->engine = ENGINE_get_default_DH(); ret->engine = ENGINE_get_default_DH();
if (ret->engine) { if (ret->engine) {
ret->meth = ENGINE_get_DH(ret->engine); ret->meth = ENGINE_get_DH(ret->engine);
if (!ret->meth) { if (ret->meth == NULL) {
DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
ENGINE_finish(ret->engine); ENGINE_finish(ret->engine);
OPENSSL_free(ret); OPENSSL_free(ret);
@ -140,7 +138,6 @@ DH *DH_new_method(ENGINE *engine)
CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (ret->engine)
ENGINE_finish(ret->engine); ENGINE_finish(ret->engine);
#endif #endif
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
@ -165,7 +162,6 @@ void DH_free(DH *r)
if (r->meth->finish) if (r->meth->finish)
r->meth->finish(r); r->meth->finish(r);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (r->engine)
ENGINE_finish(r->engine); ENGINE_finish(r->engine);
#endif #endif

View File

@ -99,10 +99,8 @@ int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
if (mtmp->finish) if (mtmp->finish)
mtmp->finish(dsa); mtmp->finish(dsa);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (dsa->engine) {
ENGINE_finish(dsa->engine); ENGINE_finish(dsa->engine);
dsa->engine = NULL; dsa->engine = NULL;
}
#endif #endif
dsa->meth = meth; dsa->meth = meth;
if (meth->init) if (meth->init)
@ -132,7 +130,7 @@ DSA *DSA_new_method(ENGINE *engine)
ret->engine = ENGINE_get_default_DSA(); ret->engine = ENGINE_get_default_DSA();
if (ret->engine) { if (ret->engine) {
ret->meth = ENGINE_get_DSA(ret->engine); ret->meth = ENGINE_get_DSA(ret->engine);
if (!ret->meth) { if (ret->meth == NULL) {
DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
ENGINE_finish(ret->engine); ENGINE_finish(ret->engine);
OPENSSL_free(ret); OPENSSL_free(ret);

View File

@ -108,7 +108,6 @@ void EC_KEY_free(EC_KEY *r)
r->meth->finish(r); r->meth->finish(r);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (r->engine != NULL)
ENGINE_finish(r->engine); ENGINE_finish(r->engine);
#endif #endif
@ -130,7 +129,7 @@ EC_KEY *EC_KEY_copy(EC_KEY *dest, EC_KEY *src)
if (dest->meth->finish != NULL) if (dest->meth->finish != NULL)
dest->meth->finish(dest); dest->meth->finish(dest);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (dest->engine != NULL && ENGINE_finish(dest->engine) == 0) if (ENGINE_finish(dest->engine) == 0)
return 0; return 0;
dest->engine = NULL; dest->engine = NULL;
#endif #endif

View File

@ -105,10 +105,8 @@ int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth)
finish(key); finish(key);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (key->engine != NULL) {
ENGINE_finish(key->engine); ENGINE_finish(key->engine);
key->engine = NULL; key->engine = NULL;
}
#endif #endif
key->meth = meth; key->meth = meth;

View File

@ -227,6 +227,7 @@ static int int_engine_module_init(CONF_IMODULE *md, const CONF *cnf)
static void int_engine_module_finish(CONF_IMODULE *md) static void int_engine_module_finish(CONF_IMODULE *md)
{ {
ENGINE *e; ENGINE *e;
while ((e = sk_ENGINE_pop(initialized_engines))) while ((e = sk_ENGINE_pop(initialized_engines)))
ENGINE_finish(e); ENGINE_finish(e);
sk_ENGINE_free(initialized_engines); sk_ENGINE_free(initialized_engines);

View File

@ -136,10 +136,8 @@ int ENGINE_finish(ENGINE *e)
{ {
int to_return = 1; int to_return = 1;
if (e == NULL) { if (e == NULL)
ENGINEerr(ENGINE_F_ENGINE_FINISH, ERR_R_PASSED_NULL_PARAMETER); return 1;
return 0;
}
CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
to_return = engine_unlocked_finish(e, 1); to_return = engine_unlocked_finish(e, 1);
CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);

View File

@ -137,11 +137,6 @@ int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
} }
EVP_PKEY_CTX_free(ctx->pctx); EVP_PKEY_CTX_free(ctx->pctx);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (ctx->engine)
/*
* The EVP_MD we used belongs to an ENGINE, release the functional
* reference we held for this reason.
*/
ENGINE_finish(ctx->engine); ENGINE_finish(ctx->engine);
#endif #endif
memset(ctx, 0, sizeof(*ctx)); memset(ctx, 0, sizeof(*ctx));
@ -187,21 +182,21 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
* previous check attempted to avoid this if the same ENGINE and * previous check attempted to avoid this if the same ENGINE and
* EVP_MD could be used). * EVP_MD could be used).
*/ */
if (ctx->engine)
ENGINE_finish(ctx->engine); ENGINE_finish(ctx->engine);
if (impl) { if (impl != NULL) {
if (!ENGINE_init(impl)) { if (!ENGINE_init(impl)) {
EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR); EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
return 0; return 0;
} }
} else } else {
/* Ask if an ENGINE is reserved for this job */ /* Ask if an ENGINE is reserved for this job */
impl = ENGINE_get_digest_engine(type->type); impl = ENGINE_get_digest_engine(type->type);
if (impl) { }
if (impl != NULL) {
/* There's an ENGINE for this job ... (apparently) */ /* There's an ENGINE for this job ... (apparently) */
const EVP_MD *d = ENGINE_get_digest(impl, type->type); const EVP_MD *d = ENGINE_get_digest(impl, type->type);
if (!d) {
/* Same comment from evp_enc.c */ if (d == NULL) {
EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR); EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
ENGINE_finish(impl); ENGINE_finish(impl);
return 0; return 0;

View File

@ -79,11 +79,6 @@ int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c)
} }
OPENSSL_free(c->cipher_data); OPENSSL_free(c->cipher_data);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (c->engine)
/*
* The EVP_CIPHER we used belongs to an ENGINE, release the
* functional reference we held for this reason.
*/
ENGINE_finish(c->engine); ENGINE_finish(c->engine);
#endif #endif
memset(c, 0, sizeof(*c)); memset(c, 0, sizeof(*c));

View File

@ -224,10 +224,8 @@ static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
return 1; return 1;
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
/* If we have an ENGINE release it */ /* If we have an ENGINE release it */
if (pkey->engine) {
ENGINE_finish(pkey->engine); ENGINE_finish(pkey->engine);
pkey->engine = NULL; pkey->engine = NULL;
}
#endif #endif
} }
if (str) if (str)
@ -235,10 +233,10 @@ static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
else else
ameth = EVP_PKEY_asn1_find(&e, type); ameth = EVP_PKEY_asn1_find(&e, type);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (!pkey && e) if (pkey == NULL)
ENGINE_finish(e); ENGINE_finish(e);
#endif #endif
if (!ameth) { if (ameth == NULL) {
EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM); EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
return 0; return 0;
} }
@ -396,7 +394,6 @@ int EVP_PKEY_type(int type)
else else
ret = NID_undef; ret = NID_undef;
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (e)
ENGINE_finish(e); ENGINE_finish(e);
#endif #endif
return ret; return ret;
@ -437,10 +434,8 @@ static void EVP_PKEY_free_it(EVP_PKEY *x)
x->pkey.ptr = NULL; x->pkey.ptr = NULL;
} }
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (x->engine) {
ENGINE_finish(x->engine); ENGINE_finish(x->engine);
x->engine = NULL; x->engine = NULL;
}
#endif #endif
} }

View File

@ -162,7 +162,6 @@ static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
ret = OPENSSL_zalloc(sizeof(*ret)); ret = OPENSSL_zalloc(sizeof(*ret));
if (ret == NULL) { if (ret == NULL) {
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (e)
ENGINE_finish(e); ENGINE_finish(e);
#endif #endif
EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE); EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
@ -329,11 +328,6 @@ void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
EVP_PKEY_free(ctx->pkey); EVP_PKEY_free(ctx->pkey);
EVP_PKEY_free(ctx->peerkey); EVP_PKEY_free(ctx->peerkey);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (ctx->engine)
/*
* The EVP_PKEY_CTX we used belongs to an ENGINE, release the
* functional reference we held for this reason.
*/
ENGINE_finish(ctx->engine); ENGINE_finish(ctx->engine);
#endif #endif
OPENSSL_free(ctx); OPENSSL_free(ctx);

View File

@ -218,7 +218,6 @@ static int check_pem(const char *nm, const char *name)
else else
r = 0; r = 0;
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (e)
ENGINE_finish(e); ENGINE_finish(e);
#endif #endif
return r; return r;

View File

@ -79,10 +79,8 @@ static const RAND_METHOD *default_RAND_meth = NULL;
int RAND_set_rand_method(const RAND_METHOD *meth) int RAND_set_rand_method(const RAND_METHOD *meth)
{ {
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (funct_ref) {
ENGINE_finish(funct_ref); ENGINE_finish(funct_ref);
funct_ref = NULL; funct_ref = NULL;
}
#endif #endif
default_RAND_meth = meth; default_RAND_meth = meth;
return 1; return 1;
@ -95,7 +93,7 @@ const RAND_METHOD *RAND_get_rand_method(void)
ENGINE *e = ENGINE_get_default_RAND(); ENGINE *e = ENGINE_get_default_RAND();
if (e) { if (e) {
default_RAND_meth = ENGINE_get_RAND(e); default_RAND_meth = ENGINE_get_RAND(e);
if (!default_RAND_meth) { if (default_RAND_meth == NULL) {
ENGINE_finish(e); ENGINE_finish(e);
e = NULL; e = NULL;
} }
@ -117,7 +115,7 @@ int RAND_set_rand_engine(ENGINE *engine)
if (!ENGINE_init(engine)) if (!ENGINE_init(engine))
return 0; return 0;
tmp_meth = ENGINE_get_RAND(engine); tmp_meth = ENGINE_get_RAND(engine);
if (!tmp_meth) { if (tmp_meth == NULL) {
ENGINE_finish(engine); ENGINE_finish(engine);
return 0; return 0;
} }

View File

@ -109,10 +109,8 @@ int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
if (mtmp->finish) if (mtmp->finish)
mtmp->finish(rsa); mtmp->finish(rsa);
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (rsa->engine) {
ENGINE_finish(rsa->engine); ENGINE_finish(rsa->engine);
rsa->engine = NULL; rsa->engine = NULL;
}
#endif #endif
rsa->meth = meth; rsa->meth = meth;
if (meth->init) if (meth->init)
@ -143,7 +141,7 @@ RSA *RSA_new_method(ENGINE *engine)
ret->engine = ENGINE_get_default_RSA(); ret->engine = ENGINE_get_default_RSA();
if (ret->engine) { if (ret->engine) {
ret->meth = ENGINE_get_RSA(ret->engine); ret->meth = ENGINE_get_RSA(ret->engine);
if (!ret->meth) { 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); ENGINE_finish(ret->engine);
OPENSSL_free(ret); OPENSSL_free(ret);

View File

@ -439,7 +439,6 @@ static int get_optional_pkey_id(const char *pkey_name)
ameth) <= 0) ameth) <= 0)
pkey_id = 0; pkey_id = 0;
} }
if (tmpeng)
ENGINE_finish(tmpeng); ENGINE_finish(tmpeng);
return pkey_id; return pkey_id;
} }

View File

@ -2433,7 +2433,6 @@ void SSL_CTX_free(SSL_CTX *a)
SSL_CTX_SRP_CTX_free(a); SSL_CTX_SRP_CTX_free(a);
#endif #endif
#ifndef OPENSSL_NO_ENGINE #ifndef OPENSSL_NO_ENGINE
if (a->client_cert_engine)
ENGINE_finish(a->client_cert_engine); ENGINE_finish(a->client_cert_engine);
#endif #endif