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

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


 Makefile.am    |    4 ++-
 net/loolnb.cpp |    7 +++++
 net/ssl.cpp    |   44 +++++++++++++++++++++++++++++++++++++
 net/ssl.hpp    |   67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 121 insertions(+), 1 deletion(-)

New commits:
commit d75d0abd02373ced07bcb6ea6a03e17ec70a6729
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Fri Feb 17 00:30:29 2017 -0500

    nb: add OpenSSL dependency and initialization
    
    Change-Id: I0d369acbc95db5d2b678032632ac4edfa561cbad
    Reviewed-on: https://gerrit.libreoffice.org/34354
    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 064d54e..24032e5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -31,6 +31,7 @@ AM_CPPFLAGS = -pthread -DLOOLWSD_DATADIR='"@LOOLWSD_DATADIR@"' -DLOOLWSD_CONFIGD
 AM_LDFLAGS = -pthread -Wl,-E
 loolforkit_LDFLAGS = -pthread -Wl,-E,-rpath,/snap/loolwsd/current/usr/lib
 loolmount_LDFLAGS = -pthread -Wl,-E,-rpath,/snap/loolwsd/current/usr/lib
+loolnb_LDFLAGS = -pthread -Wl,-E,-rpath,/snap/loolwsd/current/usr/lib -lssl -lcrypto
 
 loolwsd_fuzzer_CPPFLAGS = -DKIT_IN_PROCESS=1 -DFUZZER=1 -DTDOC=\"$(abs_top_srcdir)/test/data\" $(AM_CPPFLAGS)
 
@@ -89,7 +90,8 @@ loolwsd_fuzzer_SOURCES = $(loolwsd_sources) \
                          $(shared_sources) \
                          kit/DummyLibreOfficeKit.cpp
 
-loolnb_SOURCES = net/loolnb.cpp
+loolnb_SOURCES = net/loolnb.cpp \
+                 net/ssl.cpp
 
 clientnb_SOURCES = net/clientnb.cpp
 
diff --git a/net/loolnb.cpp b/net/loolnb.cpp
index 742c883..17909cb 100644
--- a/net/loolnb.cpp
+++ b/net/loolnb.cpp
@@ -28,6 +28,7 @@
 using Poco::MemoryInputStream;
 using Poco::StringTokenizer;
 
+#include "ssl.hpp"
 #include "socket.hpp"
 
 constexpr int PortNumber = 9191;
@@ -373,6 +374,11 @@ void server(SocketPoll& clientPoller)
 
 int main(int, const char**)
 {
+    // TODO: These would normally come from config.
+    SslContext::initialize("/etc/loolwsd/cert.pem",
+                           "/etc/loolwsd/key.pem",
+                           "/etc/loolwsd/ca-chain.cert.pem");
+
     // Used to poll client sockets.
     SocketPoll poller;
 
@@ -392,6 +398,7 @@ int main(int, const char**)
 
     threadPoll.stop();
 
+    SslContext::uninitialize();
     return 0;
 }
 
diff --git a/net/ssl.cpp b/net/ssl.cpp
new file mode 100644
index 0000000..111fbe3
--- /dev/null
+++ b/net/ssl.cpp
@@ -0,0 +1,44 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include "ssl.hpp"
+#include "config.h"
+
+std::atomic<int> SslContext::RefCount(0);
+std::unique_ptr<SslContext> SslContext::Instance;
+
+SslContext::SslContext(const std::string& certFilePath,
+                       const std::string& keyFilePath,
+                       const std::string& caFilePath) :
+    _ctx(nullptr)
+{
+    (void)certFilePath;
+    (void)keyFilePath;
+    (void)caFilePath;
+
+#if OPENSSL_VERSION_NUMBER >= 0x0907000L
+    OPENSSL_config(nullptr);
+#endif
+
+    SSL_library_init();
+    SSL_load_error_strings();
+    OpenSSL_add_all_algorithms();
+}
+
+SslContext::~SslContext()
+{
+    EVP_cleanup();
+    ERR_free_strings();
+    CRYPTO_set_locking_callback(0);
+    CRYPTO_set_id_callback(0);
+
+    CONF_modules_free();
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/net/ssl.hpp b/net/ssl.hpp
new file mode 100644
index 0000000..458d2cf
--- /dev/null
+++ b/net/ssl.hpp
@@ -0,0 +1,67 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef INCLUDED_SSL_HPP
+#define INCLUDED_SSL_HPP
+
+#include <atomic>
+#include <memory>
+#include <string>
+
+#include <openssl/ssl.h>
+#include <openssl/rand.h>
+#include <openssl/crypto.h>
+#include <openssl/err.h>
+#if OPENSSL_VERSION_NUMBER >= 0x0907000L
+#include <openssl/conf.h>
+#endif
+
+class SslContext
+{
+public:
+    static void initialize(const std::string& certFilePath,
+                           const std::string& keyFilePath,
+                           const std::string& caFilePath)
+    {
+        if (++RefCount == 1)
+        {
+            Instance.reset(new SslContext(certFilePath, keyFilePath, caFilePath));
+        }
+    }
+
+    static void uninitialize()
+    {
+        if (--RefCount == 0)
+        {
+            Instance.reset();
+        }
+    }
+
+    static SSL* newSsl()
+    {
+        return SSL_new(Instance->_ctx);
+    }
+
+    ~SslContext();
+
+private:
+    SslContext(const std::string& certFilePath,
+               const std::string& keyFilePath,
+               const std::string& caFilePath);
+
+private:
+    static std::atomic<int> RefCount;
+    static std::unique_ptr<SslContext> Instance;
+
+    SSL_CTX* _ctx;
+};
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list