Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't clean up engines after OpenSSL has already shut down
  • Loading branch information
dwmw2 committed May 4, 2020
1 parent 7f99bed commit f9c24e0
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/lib/crypto/OSSLCryptoFactory.cpp
Expand Up @@ -77,6 +77,7 @@ bool OSSLCryptoFactory::FipsSelfTestStatus = false;

static unsigned nlocks;
static Mutex** locks;
static bool ossl_shutdown;

// Mutex callback
void lock_callback(int mode, int n, const char* file, int line)
Expand All @@ -101,6 +102,13 @@ void lock_callback(int mode, int n, const char* file, int line)
}
}

#if OPENSSL_VERSION_NUMBER >= 0x10101000L
void ossl_factory_shutdown(void)
{
ossl_shutdown = true;
}
#endif

// Constructor
OSSLCryptoFactory::OSSLCryptoFactory()
{
Expand Down Expand Up @@ -176,6 +184,9 @@ OSSLCryptoFactory::OSSLCryptoFactory()
OPENSSL_INIT_ADD_ALL_CIPHERS |
OPENSSL_INIT_ADD_ALL_DIGESTS |
OPENSSL_INIT_LOAD_CONFIG, NULL);
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
OPENSSL_atexit(ossl_factory_shutdown);
#endif
#endif

// Initialise the GOST engine
Expand Down Expand Up @@ -226,31 +237,34 @@ OSSLCryptoFactory::OSSLCryptoFactory()
// Destructor
OSSLCryptoFactory::~OSSLCryptoFactory()
{
#ifdef WITH_GOST
// Finish the GOST engine
if (eg != NULL)
if (ossl_shutdown)
{
ENGINE_finish(eg);
ENGINE_free(eg);
eg = NULL;
}
#ifdef WITH_GOST
// Finish the GOST engine
if (eg != NULL)
{
ENGINE_finish(eg);
ENGINE_free(eg);
eg = NULL;
}
#endif

// Finish the rd_rand engine
ENGINE_finish(rdrand_engine);
ENGINE_free(rdrand_engine);
rdrand_engine = NULL;
// Finish the rd_rand engine
ENGINE_finish(rdrand_engine);
ENGINE_free(rdrand_engine);
rdrand_engine = NULL;

// Recycle locks
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
if (setLockingCallback)
{
CRYPTO_set_locking_callback(NULL);
}
#endif
}
// Destroy the one-and-only RNG
delete rng;

// Recycle locks
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
if (setLockingCallback)
{
CRYPTO_set_locking_callback(NULL);
}
#endif
for (unsigned i = 0; i < nlocks; i++)
{
MutexFactory::i()->recycleMutex(locks[i]);
Expand Down

0 comments on commit f9c24e0

Please sign in to comment.