[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - common/Util.hpp Makefile.am net/ssl.cpp net/ssl.hpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Fri Feb 17 06:51:00 UTC 2017


 Makefile.am     |    4 ++-
 common/Util.hpp |    3 ++
 net/ssl.cpp     |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/ssl.hpp     |   21 +++++++++++++++++++
 4 files changed, 87 insertions(+), 1 deletion(-)

New commits:
commit 1cf7ea54a877f10d19890923755e8e53d0425667
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Fri Feb 17 01:15:42 2017 -0500

    nb: more SSL initialization
    
    Multi-threading support.
    
    Change-Id: I7233ca61f9a0dce5601b4de03a7ad3273acc0f3c
    Reviewed-on: https://gerrit.libreoffice.org/34355
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/Makefile.am b/Makefile.am
index 24032e5..d538593 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -91,7 +91,9 @@ loolwsd_fuzzer_SOURCES = $(loolwsd_sources) \
                          kit/DummyLibreOfficeKit.cpp
 
 loolnb_SOURCES = net/loolnb.cpp \
-                 net/ssl.cpp
+                 net/ssl.cpp \
+                 common/Log.cpp \
+                 common/Util.cpp
 
 clientnb_SOURCES = net/clientnb.cpp
 
diff --git a/common/Util.hpp b/common/Util.hpp
index 9fae42b..57dc623 100644
--- a/common/Util.hpp
+++ b/common/Util.hpp
@@ -34,6 +34,9 @@ namespace Util
         void reseed();
         unsigned getNext();
 
+        /// Generate an array of random characters.
+        std::vector<char> getBytes(const size_t length);
+
         /// Generates a random string suitable for
         /// file/directory names.
         std::string getFilename(const size_t length);
diff --git a/net/ssl.cpp b/net/ssl.cpp
index 111fbe3..35599d1 100644
--- a/net/ssl.cpp
+++ b/net/ssl.cpp
@@ -10,8 +10,13 @@
 #include "ssl.hpp"
 #include "config.h"
 
+#include <sys/syscall.h>
+
+#include "Util.hpp"
+
 std::atomic<int> SslContext::RefCount(0);
 std::unique_ptr<SslContext> SslContext::Instance;
+std::vector<std::unique_ptr<std::mutex>> SslContext::Mutexes;
 
 SslContext::SslContext(const std::string& certFilePath,
                        const std::string& keyFilePath,
@@ -29,6 +34,21 @@ SslContext::SslContext(const std::string& certFilePath,
     SSL_library_init();
     SSL_load_error_strings();
     OpenSSL_add_all_algorithms();
+
+    const std::vector<char> rand = Util::rng::getBytes(512);
+    RAND_seed(&rand[0], rand.size());
+
+    // Initialize multi-threading support.
+    for (int x = 0; x < CRYPTO_num_locks(); ++x)
+    {
+        Mutexes.emplace_back(new std::mutex);
+    }
+
+    CRYPTO_set_locking_callback(&SslContext::lock);
+    CRYPTO_set_id_callback(&SslContext::id);
+    CRYPTO_set_dynlock_create_callback(&SslContext::dynlockCreate);
+    CRYPTO_set_dynlock_lock_callback(&SslContext::dynlock);
+    CRYPTO_set_dynlock_destroy_callback(&SslContext::dynlockDestroy);
 }
 
 SslContext::~SslContext()
@@ -41,4 +61,44 @@ SslContext::~SslContext()
     CONF_modules_free();
 }
 
+void SslContext::lock(int mode, int n, const char* /*file*/, int /*line*/)
+{
+    if (mode & CRYPTO_LOCK)
+    {
+        Mutexes[n]->lock();
+    }
+    else
+    {
+        Mutexes[n]->unlock();
+    }
+}
+
+unsigned long SslContext::id()
+{
+    return syscall(SYS_gettid);
+}
+
+CRYPTO_dynlock_value* SslContext::dynlockCreate(const char* /*file*/, int /*line*/)
+{
+    return new CRYPTO_dynlock_value;
+}
+
+
+void SslContext::dynlock(int mode, struct CRYPTO_dynlock_value* lock, const char* /*file*/, int /*line*/)
+{
+    if (mode & CRYPTO_LOCK)
+    {
+        lock->Mutex.lock();
+    }
+    else
+    {
+        lock->Mutex.unlock();
+    }
+}
+
+void SslContext::dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* /*file*/, int /*line*/)
+{
+    delete lock;
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/net/ssl.hpp b/net/ssl.hpp
index 458d2cf..90098a4 100644
--- a/net/ssl.hpp
+++ b/net/ssl.hpp
@@ -12,7 +12,9 @@
 
 #include <atomic>
 #include <memory>
+#include <mutex>
 #include <string>
+#include <vector>
 
 #include <openssl/ssl.h>
 #include <openssl/rand.h>
@@ -22,6 +24,16 @@
 #include <openssl/conf.h>
 #endif
 
+extern "C"
+{
+    // Multithreading support for OpenSSL.
+    // Not needed in recent (1.x?) versions.
+    struct CRYPTO_dynlock_value
+    {
+        std::mutex Mutex;
+    };
+}
+
 class SslContext
 {
 public:
@@ -55,9 +67,18 @@ private:
                const std::string& keyFilePath,
                const std::string& caFilePath);
 
+    // Multithreading support for OpenSSL.
+    // Not needed in recent (1.x?) versions.
+    static void lock(int mode, int n, const char* file, int line);
+    static unsigned long id();
+    static struct CRYPTO_dynlock_value* dynlockCreate(const char* file, int line);
+    static void dynlock(int mode, struct CRYPTO_dynlock_value* lock, const char* file, int line);
+    static void dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* file, int line);
+
 private:
     static std::atomic<int> RefCount;
     static std::unique_ptr<SslContext> Instance;
+    static std::vector<std::unique_ptr<std::mutex>> Mutexes;
 
     SSL_CTX* _ctx;
 };


More information about the Libreoffice-commits mailing list