Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main/openssl: cleanup #358

Merged
merged 3 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/main/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ int libre_init(void)
goto out;

out:
if (err) {
if (err)
net_sock_close();
#ifdef USE_OPENSSL
openssl_close();
#endif
}

return err;
}
Expand All @@ -50,7 +46,4 @@ void libre_close(void)
{
(void)fd_setsize(0);
net_sock_close();
#ifdef USE_OPENSSL
openssl_close();
#endif
}
1 change: 0 additions & 1 deletion src/main/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ extern "C" {

#ifdef USE_OPENSSL
int openssl_init(void);
void openssl_close(void);
#endif

#ifdef __cplusplus
Expand Down
128 changes: 0 additions & 128 deletions src/main/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,96 +6,10 @@
#ifdef HAVE_SIGNAL
#include <signal.h>
#endif
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#include <openssl/crypto.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <re_types.h>
#include <re_lock.h>
#include <re_mem.h>
#include "main.h"


#if defined (HAVE_PTHREAD) && (OPENSSL_VERSION_NUMBER < 0x10100000L)


static pthread_mutex_t *lockv;


static inline unsigned long threadid(void)
{
#if defined (DARWIN) || defined (FREEBSD) || defined (OPENBSD) || \
defined (NETBSD) || defined (DRAGONFLY)
return (unsigned long)(void *)pthread_self();
#else
return (unsigned long)pthread_self();
#endif
}


static void threadid_handler(CRYPTO_THREADID *id)
{
CRYPTO_THREADID_set_numeric(id, threadid());
}


static void locking_handler(int mode, int type, const char *file, int line)
{
(void)file;
(void)line;

if (mode & CRYPTO_LOCK)
(void)pthread_mutex_lock(&lockv[type]);
else
(void)pthread_mutex_unlock(&lockv[type]);
}


#endif


#if OPENSSL_VERSION_NUMBER < 0x10100000L
static struct CRYPTO_dynlock_value *dynlock_create_handler(const char *file,
int line)
{
struct lock *lock;
(void)file;
(void)line;

if (lock_alloc(&lock))
return NULL;

return (struct CRYPTO_dynlock_value *)lock;
}


static void dynlock_lock_handler(int mode, struct CRYPTO_dynlock_value *l,
const char *file, int line)
{
struct lock *lock = (struct lock *)l;
(void)file;
(void)line;

if (mode & CRYPTO_LOCK)
lock_write_get(lock);
else
lock_rel(lock);
}


static void dynlock_destroy_handler(struct CRYPTO_dynlock_value *l,
const char *file, int line)
{
(void)file;
(void)line;

mem_deref(l);
}
#endif


#ifdef SIGPIPE
static void sigpipe_handler(int x)
{
Expand All @@ -108,56 +22,14 @@ static void sigpipe_handler(int x)
int openssl_init(void)
{
int err;
#if defined (HAVE_PTHREAD) && (OPENSSL_VERSION_NUMBER < 0x10100000L)
int i;

lockv = mem_zalloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks(), NULL);
if (!lockv)
return ENOMEM;

for (i=0; i<CRYPTO_num_locks(); i++) {

err = pthread_mutex_init(&lockv[i], NULL);
if (err) {
lockv = mem_deref(lockv);
return err;
}
}

CRYPTO_THREADID_set_callback(threadid_handler);

CRYPTO_set_locking_callback(locking_handler);
#endif

#if OPENSSL_VERSION_NUMBER < 0x10100000L
CRYPTO_set_dynlock_create_callback(dynlock_create_handler);
CRYPTO_set_dynlock_lock_callback(dynlock_lock_handler);
CRYPTO_set_dynlock_destroy_callback(dynlock_destroy_handler);
#endif

#ifdef SIGPIPE
(void)signal(SIGPIPE, sigpipe_handler);
#endif

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
err = OPENSSL_init_ssl(OPENSSL_INIT_SSL_DEFAULT, NULL);
if (!err)
return !err;
#else
SSL_library_init();
SSL_load_error_strings();
#endif

return 0;
}


void openssl_close(void)
{
#if OPENSSL_VERSION_NUMBER < 0x10100000L
ERR_free_strings();
#endif
#if defined (HAVE_PTHREAD) && (OPENSSL_VERSION_NUMBER < 0x10100000L)
lockv = mem_deref(lockv);
#endif
}