avformat/tls_openssl: #if ff_openssl_init/deinit() away if possible

These functions do nothing useful when used with a non-ancient
version of openssl (namely 1.1.0 or above).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2024-05-17 15:09:00 +02:00
parent 8b48b0adab
commit 583c3d45fa
2 changed files with 27 additions and 16 deletions

View File

@ -18,8 +18,13 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "config_components.h"
#if CONFIG_TLS_PROTOCOL && CONFIG_OPENSSL
#include <openssl/opensslv.h>
#endif
#include <fcntl.h>
#include "network.h"
#include "tls.h"
@ -31,7 +36,7 @@
int ff_tls_init(void)
{
#if CONFIG_TLS_PROTOCOL
#if CONFIG_OPENSSL
#if CONFIG_OPENSSL && OPENSSL_VERSION_NUMBER < 0x10100000L
int ret;
if ((ret = ff_openssl_init()) < 0)
return ret;
@ -46,7 +51,7 @@ int ff_tls_init(void)
void ff_tls_deinit(void)
{
#if CONFIG_TLS_PROTOCOL
#if CONFIG_OPENSSL
#if CONFIG_OPENSSL && OPENSSL_VERSION_NUMBER < 0x10100000L
ff_openssl_deinit();
#endif
#if CONFIG_GNUTLS

View File

@ -23,16 +23,12 @@
#include "os_support.h"
#include "url.h"
#include "tls.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/thread.h"
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
static int openssl_init;
typedef struct TLSContext {
const AVClass *class;
TLSShared tls_shared;
@ -44,10 +40,22 @@ typedef struct TLSContext {
int io_err;
} TLSContext;
/* OpenSSL 1.0.2 or below, then you would use SSL_library_init. If you are
* using OpenSSL 1.1.0 or above, then the library will initialize
* itself automatically.
* https://wiki.openssl.org/index.php/Library_Initialization
*/
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#include "libavutil/thread.h"
static AVMutex openssl_mutex = AV_MUTEX_INITIALIZER;
#if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
static int openssl_init;
#if HAVE_THREADS
#include <openssl/crypto.h>
#include "libavutil/mem.h"
pthread_mutex_t *openssl_mutexes;
static void openssl_lock(int mode, int type, const char *file, int line)
{
@ -68,16 +76,9 @@ int ff_openssl_init(void)
{
ff_mutex_lock(&openssl_mutex);
if (!openssl_init) {
/* OpenSSL 1.0.2 or below, then you would use SSL_library_init. If you are
* using OpenSSL 1.1.0 or above, then the library will initialize
* itself automatically.
* https://wiki.openssl.org/index.php/Library_Initialization
*/
#if OPENSSL_VERSION_NUMBER < 0x10100000L
SSL_library_init();
SSL_load_error_strings();
#endif
#if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
#if HAVE_THREADS
if (!CRYPTO_get_locking_callback()) {
int i;
openssl_mutexes = av_malloc_array(sizeof(pthread_mutex_t), CRYPTO_num_locks());
@ -106,7 +107,7 @@ void ff_openssl_deinit(void)
ff_mutex_lock(&openssl_mutex);
openssl_init--;
if (!openssl_init) {
#if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
#if HAVE_THREADS
if (CRYPTO_get_locking_callback() == openssl_lock) {
int i;
CRYPTO_set_locking_callback(NULL);
@ -118,6 +119,7 @@ void ff_openssl_deinit(void)
}
ff_mutex_unlock(&openssl_mutex);
}
#endif
static int print_tls_error(URLContext *h, int ret)
{
@ -157,7 +159,9 @@ static int tls_close(URLContext *h)
if (c->url_bio_method)
BIO_meth_free(c->url_bio_method);
#endif
#if OPENSSL_VERSION_NUMBER < 0x10100000L
ff_openssl_deinit();
#endif
return 0;
}
@ -253,8 +257,10 @@ static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **op
BIO *bio;
int ret;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
if ((ret = ff_openssl_init()) < 0)
return ret;
#endif
if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
goto fail;