From f9739160ac187b9485c3aa55d1cf60f745d280b6 Mon Sep 17 00:00:00 2001
From: Vladimir Davydov <vdavydov@tarantool.org>
Date: Tue, 31 May 2022 15:05:40 +0300
Subject: [PATCH] ssl: move OpenSSL library initialization code to separate
 file

We redefine ssl_init and ssl_free in the EE build, because we need to do
some extra work there. Currently, it's fine to duplicate the bulk of the
OpenSSL library initialization code between EE and CE repositories, but
with the introduction of OpenSSL 3.0 it's going to become more
complicated so duplicating would look bad. Let's move the common code to
ssl_init_impl() and ssl_free_impl() helper functions.

Needed for #6477

NO_DOC=refactoring
NO_TEST=refactoring
NO_CHANGELOG=refactoring
---
 src/lib/core/CMakeLists.txt |  1 +
 src/lib/core/ssl.c          | 18 +++---------------
 src/lib/core/ssl_init.c     | 33 +++++++++++++++++++++++++++++++++
 src/lib/core/ssl_init.h     | 18 ++++++++++++++++++
 4 files changed, 55 insertions(+), 15 deletions(-)
 create mode 100644 src/lib/core/ssl_init.c
 create mode 100644 src/lib/core/ssl_init.h

diff --git a/src/lib/core/CMakeLists.txt b/src/lib/core/CMakeLists.txt
index f17ae163d8..d6646fe23e 100644
--- a/src/lib/core/CMakeLists.txt
+++ b/src/lib/core/CMakeLists.txt
@@ -39,6 +39,7 @@ set(core_sources
     mp_interval.c
     prbuf.c
     clock_lowres.c
+    ssl_init.c
 )
 
 if(ENABLE_TUPLE_COMPRESSION)
diff --git a/src/lib/core/ssl.c b/src/lib/core/ssl.c
index 57989ea99d..e8eb667f13 100644
--- a/src/lib/core/ssl.c
+++ b/src/lib/core/ssl.c
@@ -5,14 +5,11 @@
  */
 #include "ssl.h"
 
-#include <openssl/crypto.h>
-#include <openssl/err.h>
-#include <openssl/evp.h>
-#include <openssl/ssl.h>
 #include <stddef.h>
 
 #include "diag.h"
 #include "iostream.h"
+#include "ssl_init.h"
 #include "trivia/config.h"
 
 #if defined(ENABLE_SSL)
@@ -22,22 +19,13 @@
 void
 ssl_init(void)
 {
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
-	OpenSSL_add_all_digests();
-	OpenSSL_add_all_ciphers();
-	ERR_load_crypto_strings();
-#else
-	OPENSSL_init_crypto(0, NULL);
-	OPENSSL_init_ssl(0, NULL);
-#endif
+	ssl_init_impl();
 }
 
 void
 ssl_free(void)
 {
-#ifdef OPENSSL_cleanup
-	OPENSSL_cleanup();
-#endif
+	ssl_free_impl();
 }
 
 struct ssl_iostream_ctx *
diff --git a/src/lib/core/ssl_init.c b/src/lib/core/ssl_init.c
new file mode 100644
index 0000000000..dbfcec07ba
--- /dev/null
+++ b/src/lib/core/ssl_init.c
@@ -0,0 +1,33 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2010-2022, Tarantool AUTHORS, please see AUTHORS file.
+ */
+#include "ssl_init.h"
+
+#include <openssl/crypto.h>
+#include <openssl/err.h>
+#include <openssl/evp.h>
+#include <openssl/ssl.h>
+#include <stddef.h>
+
+void
+ssl_init_impl(void)
+{
+#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
+	OpenSSL_add_all_digests();
+	OpenSSL_add_all_ciphers();
+	ERR_load_crypto_strings();
+#else
+	OPENSSL_init_crypto(0, NULL);
+	OPENSSL_init_ssl(0, NULL);
+#endif
+}
+
+void
+ssl_free_impl(void)
+{
+#ifdef OPENSSL_cleanup
+	OPENSSL_cleanup();
+#endif
+}
diff --git a/src/lib/core/ssl_init.h b/src/lib/core/ssl_init.h
new file mode 100644
index 0000000000..006b66419f
--- /dev/null
+++ b/src/lib/core/ssl_init.h
@@ -0,0 +1,18 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2010-2022, Tarantool AUTHORS, please see AUTHORS file.
+ */
+#pragma once
+
+/**
+ * Initializes OpenSSL library. Internal method. Use ssl_init() instead.
+ */
+void
+ssl_init_impl(void);
+
+/**
+ * Frees OpenSSL library. Internal method. Use ssl_free() instead.
+ */
+void
+ssl_free_impl(void);
-- 
GitLab