Help please.
I have a Win32 application (service) that loads several dlls that make OpenSSL
calls. Also there is a separate dll that takes care of the OpenSSL
initialization (thread setup, SSL_library_init(), SSL_CTX_new() ).
I've followed the samples online on how to do the thread setup, so I have the
following:
#define MUTEX_TYPE HANDLE
#define MUTEX_SETUP(x) (x) = CreateMutex( NULL, FALSE, NULL )
#define MUTEX_CLEANUP(x) CloseHandle(x)
#define MUTEX_LOCK(x) WaitForSingleObject( (x), INFINITE )
#define MUTEX_UNLOCK(x) ReleaseMutex(x)
#define THREAD_ID GetCurrentThreadId()
// This array will store all of the mutexes available to OpenSSL.
static MUTEX_TYPE* mutex_buf = NULL;
static unsigned long id_function()
{
return ((unsigned long)THREAD_ID);
}
static void locking_function(int mode, int n, const char* file, int line)
{
if ( mode & CRYPTO_LOCK )
MUTEX_LOCK( mutex_buf[n] );
else
MUTEX_UNLOCK( mutex_buf[n] );
}
int THREAD_setup()
{
mutex_buf = (MUTEX_TYPE*)malloc( CRYPTO_num_locks() * sizeof(MUTEX_TYPE) );
if ( !mutex_buf )
return 0;
for(int i=0; i<CRYPTO_num_locks(); ++i)
MUTEX_SETUP( mutex_buf[i] );
// id_function(void) is a function that returns a thread ID. It is not
needed on Windows?
CRYPTO_set_id_callback( id_function );
CRYPTO_set_locking_callback( locking_function );
return 1;
}
int THREAD_cleanup()
{
if ( !mutex_buf )
return 0;
CRYPTO_set_id_callback( NULL );
CRYPTO_set_locking_callback( NULL );
for(int i=0; i<CRYPTO_num_locks(); ++i)
MUTEX_CLEANUP( mutex_buf[i] );
free( mutex_buf );
mutex_buf = NULL;
return 1;
}
Also the logic makes sure that there is only one SSL_CTX object for the process.
What happens is that when a second thread tries to access the OpenSSL the
system crashes in:
static unsigned long WINAPI _threadstart (void * ptd)
Can somebody please help me identify the problem?
Yana