[rtmpdump] [PATCH] OPENSSL crypto: SSL_load_error_strings and SSL_library_init are not reentrant

Rafaël Carré funman at videolan.org
Thu Sep 4 15:25:58 CEST 2014


---
 librtmp/Makefile |  5 ++++-
 librtmp/rtmp.c   | 35 +++++++++++++++++++++++++++++------
 2 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/librtmp/Makefile b/librtmp/Makefile
index 2c1c790..ebbf863 100644
--- a/librtmp/Makefile
+++ b/librtmp/Makefile
@@ -30,7 +30,10 @@ LIBS_posix=
 LIBS_darwin=
 LIBS_mingw=-lws2_32 -lwinmm -lgdi32
 LIB_GNUTLS=-lgnutls -lhogweed -lnettle -lgmp $(LIBZ)
-LIB_OPENSSL=-lssl -lcrypto $(LIBZ)
+LIB_OPENSSL_posix=-lpthread
+LIB_OPENSSL_darwin=-lpthread
+LIB_OPENSSL_mingw=
+LIB_OPENSSL=-lssl -lcrypto $(LIBZ) $(LIB_OPENSSL_$(SYS))
 LIB_POLARSSL=-lpolarssl $(LIBZ)
 PRIVATE_LIBS=$(LIBS_$(SYS))
 CRYPTO_LIB=$(LIB_$(CRYPTO)) $(PRIVATE_LIBS)
diff --git a/librtmp/rtmp.c b/librtmp/rtmp.c
index 60f251c..8fd5b88 100644
--- a/librtmp/rtmp.c
+++ b/librtmp/rtmp.c
@@ -28,6 +28,9 @@
 #include <string.h>
 #include <assert.h>
 #include <time.h>
+#if defined(USE_OPENSSL) && !defined(_WIN32)
+#include <pthread.h>
+#endif
 
 #include "rtmp_sys.h"
 #include "log.h"
@@ -221,6 +224,19 @@ RTMP_LibVersion()
   return RTMP_LIB_VERSION;
 }
 
+#ifdef USE_OPENSSL
+static void
+OpenSSL_Setup()
+{
+  SSL_load_error_strings();
+  SSL_library_init();
+  OpenSSL_add_all_digests();
+  RTMP_TLS_ctx = SSL_CTX_new(SSLv23_method());
+  SSL_CTX_set_options(RTMP_TLS_ctx, SSL_OP_ALL);
+  SSL_CTX_set_default_verify_paths(RTMP_TLS_ctx);
+}
+#endif
+
 void
 RTMP_TLS_Init()
 {
@@ -242,12 +258,19 @@ RTMP_TLS_Init()
   	"ca.pem", GNUTLS_X509_FMT_PEM);
 #elif !defined(NO_SSL) /* USE_OPENSSL */
   /* libcrypto doesn't need anything special */
-  SSL_load_error_strings();
-  SSL_library_init();
-  OpenSSL_add_all_digests();
-  RTMP_TLS_ctx = SSL_CTX_new(SSLv23_method());
-  SSL_CTX_set_options(RTMP_TLS_ctx, SSL_OP_ALL);
-  SSL_CTX_set_default_verify_paths(RTMP_TLS_ctx);
+#ifdef _WIN32
+    #warning FIXME: OpenSSL init is not reentrant
+    OpenSSL_Setup();
+#else
+  static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+  static int initialized = 0;
+  pthread_mutex_lock(&lock);
+  if (!initialized) {
+    OpenSSL_Setup();
+    initialized = 1;
+  }
+  pthread_mutex_unlock(&lock);
+#endif
 #endif
 #endif
 }
-- 
2.1.0



More information about the rtmpdump mailing list