From 21a0219fe6a0860c6cf4ccb56787f02a701552ff Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Thu, 8 Dec 2016 22:05:07 +0100 Subject: [PATCH] Moved uninitialize_ssl to own file --- Makefile | 1 + src/httplib_uninitialize_ssl.c | 67 ++++++++++++++++++++++++++++++++++ src/libhttp-private.h | 2 + src/libhttp.c | 44 ++++------------------ 4 files changed, 78 insertions(+), 36 deletions(-) create mode 100644 src/httplib_uninitialize_ssl.c diff --git a/Makefile b/Makefile index b7881eb9..42776d7d 100644 --- a/Makefile +++ b/Makefile @@ -67,6 +67,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_set_tcp_nodelay.c \ src/httplib_start.c \ src/httplib_stop.c \ + src/httplib_uninitialize_ssl.c \ src/httplib_version.c \ src/httplib_websocket_client_thread.c \ src/httplib_worker_thread.c diff --git a/src/httplib_uninitialize_ssl.c b/src/httplib_uninitialize_ssl.c new file mode 100644 index 00000000..20dad561 --- /dev/null +++ b/src/httplib_uninitialize_ssl.c @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2016 Lammert Bies + * Copyright (c) 2013-2016 the Civetweb developers + * Copyright (c) 2004-2013 Sergey Lyubka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + + +#include "libhttp-private.h" + + + +/* + * void XX_httplib_unitialize_ssl( struct mg_context *ctx ); + * + * The function XX_httplib_unititialize_ssl() is used to properly stop the SSL + * subsystem. + */ + +#if !defined(NO_SSL) +void XX_httplib_uninitialize_ssl( struct mg_context *ctx ) { + + int i; + (void)ctx; + + if (XX_httplib_atomic_dec(&XX_httplib_cryptolib_users) == 0) { + + /* Shutdown according to + * https://wiki.openssl.org/index.php/Library_Initialization#Cleanup + * http://stackoverflow.com/questions/29845527/how-to-properly-uninitialize-openssl + */ + CRYPTO_set_locking_callback(NULL); + CRYPTO_set_id_callback(NULL); + ENGINE_cleanup(); + CONF_modules_unload(1); + ERR_free_strings(); + EVP_cleanup(); + CRYPTO_cleanup_all_ex_data(); + ERR_remove_state(0); + + for (i = 0; i < CRYPTO_num_locks(); i++) { + pthread_mutex_destroy(&XX_httplib_ssl_mutexes[i]); + } + XX_httplib_free(XX_httplib_ssl_mutexes); + XX_httplib_ssl_mutexes = NULL; + } + +} /* XX_httplib_unitialize_ssl */ +#endif /* !NO_SSL */ diff --git a/src/libhttp-private.h b/src/libhttp-private.h index b8807a5c..7d2a597e 100644 --- a/src/libhttp-private.h +++ b/src/libhttp-private.h @@ -906,7 +906,9 @@ void * XX_httplib_malloc( size_t a ); extern const struct uriprot_tp XX_httplib_abs_uri_protocols[]; extern struct mg_option XX_httplib_config_options[]; +extern int XX_httplib_cryptolib_users; extern struct ssl_func XX_httplib_crypto_sw[]; +extern pthread_mutex_t * XX_httplib_ssl_mutexes; extern struct ssl_func XX_httplib_ssl_sw[]; extern int XX_httplib_sTlsInit; extern pthread_key_t XX_httplib_sTlsKey; diff --git a/src/libhttp.c b/src/libhttp.c index a6275326..4973283d 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -8474,7 +8474,7 @@ static int refresh_trust( struct mg_connection *conn ) { } -static pthread_mutex_t *ssl_mutexes; +pthread_mutex_t *XX_httplib_ssl_mutexes; int XX_httplib_sslize( struct mg_connection *conn, SSL_CTX *s, int (*func)(SSL *) ) { @@ -8639,9 +8639,9 @@ static void ssl_locking_callback(int mode, int mutex_num, const char *file, int if (mode & 1) { /* 1 is CRYPTO_LOCK */ - (void)pthread_mutex_lock(&ssl_mutexes[mutex_num]); + (void)pthread_mutex_lock(&XX_httplib_ssl_mutexes[mutex_num]); } else { - (void)pthread_mutex_unlock(&ssl_mutexes[mutex_num]); + (void)pthread_mutex_unlock(&XX_httplib_ssl_mutexes[mutex_num]); } } @@ -8689,9 +8689,9 @@ static void *cryptolib_dll_handle; /* Store the crypto library handle. */ #if defined(SSL_ALREADY_INITIALIZED) -static int cryptolib_users = 1; /* Reference counter for crypto library. */ +int XX_httplib_cryptolib_users = 1; /* Reference counter for crypto library. */ #else -static int cryptolib_users = 0; /* Reference counter for crypto library. */ +int XX_httplib_cryptolib_users = 0; /* Reference counter for crypto library. */ #endif @@ -8707,7 +8707,7 @@ static int initialize_ssl(struct mg_context *ctx) { } #endif /* NO_SSL_DL */ - if (XX_httplib_atomic_inc(&cryptolib_users) > 1) return 1; + if (XX_httplib_atomic_inc(&XX_httplib_cryptolib_users) > 1) return 1; /* Initialize locking callbacks, needed for thread safety. * http://www.openssl.org/support/faq.html#PROG1 @@ -8715,13 +8715,13 @@ static int initialize_ssl(struct mg_context *ctx) { i = CRYPTO_num_locks(); if (i < 0) i = 0; size = sizeof(pthread_mutex_t) * ((size_t)(i)); - if ((ssl_mutexes = (pthread_mutex_t *)XX_httplib_malloc(size)) == NULL) { + if ((XX_httplib_ssl_mutexes = (pthread_mutex_t *)XX_httplib_malloc(size)) == NULL) { mg_cry( XX_httplib_fc(ctx), "%s: cannot allocate mutexes: %s", __func__, ssl_error()); return 0; } for (i = 0; i < CRYPTO_num_locks(); i++) { - pthread_mutex_init(&ssl_mutexes[i], &XX_httplib_pthread_mutex_attr); + pthread_mutex_init(&XX_httplib_ssl_mutexes[i], &XX_httplib_pthread_mutex_attr); } CRYPTO_set_locking_callback(&ssl_locking_callback); @@ -8905,32 +8905,4 @@ int XX_httplib_set_ssl_option( struct mg_context *ctx ) { } /* XX_httplib_set_ssl_option */ - -void XX_httplib_uninitialize_ssl( struct mg_context *ctx ) { - - int i; - (void)ctx; - - if (XX_httplib_atomic_dec(&cryptolib_users) == 0) { - - /* Shutdown according to - * https://wiki.openssl.org/index.php/Library_Initialization#Cleanup - * http://stackoverflow.com/questions/29845527/how-to-properly-uninitialize-openssl - */ - CRYPTO_set_locking_callback(NULL); - CRYPTO_set_id_callback(NULL); - ENGINE_cleanup(); - CONF_modules_unload(1); - ERR_free_strings(); - EVP_cleanup(); - CRYPTO_cleanup_all_ex_data(); - ERR_remove_state(0); - - for (i = 0; i < CRYPTO_num_locks(); i++) { - pthread_mutex_destroy(&ssl_mutexes[i]); - } - XX_httplib_free(ssl_mutexes); - ssl_mutexes = NULL; - } -} #endif /* !NO_SSL */