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

View File

@ -23,16 +23,12 @@
#include "os_support.h" #include "os_support.h"
#include "url.h" #include "url.h"
#include "tls.h" #include "tls.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h" #include "libavutil/opt.h"
#include "libavutil/thread.h"
#include <openssl/bio.h> #include <openssl/bio.h>
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <openssl/err.h> #include <openssl/err.h>
static int openssl_init;
typedef struct TLSContext { typedef struct TLSContext {
const AVClass *class; const AVClass *class;
TLSShared tls_shared; TLSShared tls_shared;
@ -44,10 +40,22 @@ typedef struct TLSContext {
int io_err; int io_err;
} TLSContext; } 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; 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 <openssl/crypto.h>
#include "libavutil/mem.h"
pthread_mutex_t *openssl_mutexes; pthread_mutex_t *openssl_mutexes;
static void openssl_lock(int mode, int type, const char *file, int line) 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); ff_mutex_lock(&openssl_mutex);
if (!openssl_init) { 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_library_init();
SSL_load_error_strings(); SSL_load_error_strings();
#endif #if HAVE_THREADS
#if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
if (!CRYPTO_get_locking_callback()) { if (!CRYPTO_get_locking_callback()) {
int i; int i;
openssl_mutexes = av_malloc_array(sizeof(pthread_mutex_t), CRYPTO_num_locks()); 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); ff_mutex_lock(&openssl_mutex);
openssl_init--; openssl_init--;
if (!openssl_init) { if (!openssl_init) {
#if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L #if HAVE_THREADS
if (CRYPTO_get_locking_callback() == openssl_lock) { if (CRYPTO_get_locking_callback() == openssl_lock) {
int i; int i;
CRYPTO_set_locking_callback(NULL); CRYPTO_set_locking_callback(NULL);
@ -118,6 +119,7 @@ void ff_openssl_deinit(void)
} }
ff_mutex_unlock(&openssl_mutex); ff_mutex_unlock(&openssl_mutex);
} }
#endif
static int print_tls_error(URLContext *h, int ret) static int print_tls_error(URLContext *h, int ret)
{ {
@ -157,7 +159,9 @@ static int tls_close(URLContext *h)
if (c->url_bio_method) if (c->url_bio_method)
BIO_meth_free(c->url_bio_method); BIO_meth_free(c->url_bio_method);
#endif #endif
#if OPENSSL_VERSION_NUMBER < 0x10100000L
ff_openssl_deinit(); ff_openssl_deinit();
#endif
return 0; return 0;
} }
@ -253,8 +257,10 @@ static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **op
BIO *bio; BIO *bio;
int ret; int ret;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
if ((ret = ff_openssl_init()) < 0) if ((ret = ff_openssl_init()) < 0)
return ret; return ret;
#endif
if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0) if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
goto fail; goto fail;