From 6fab8599256f358a2828d876080a6037dafda231 Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Sat, 17 Feb 2018 23:29:37 +0000
Subject: [PATCH] Create and use is_crypt_initialized() - we can't call any
 cruptlib functions (e.g. to query the version) until/unless the library has
 been successfully initialized (and that could happen from the web server or
 the terminal/SSH server or a secure service that uses TLS).

---
 src/sbbs3/ssl.c   | 7 +++++++
 src/sbbs3/ssl.h   | 2 ++
 src/sbbs3/ver.cpp | 3 ++-
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/sbbs3/ssl.c b/src/sbbs3/ssl.c
index 7fe10af0ff..09177e6ffb 100644
--- a/src/sbbs3/ssl.c
+++ b/src/sbbs3/ssl.c
@@ -53,6 +53,7 @@ static bool get_error_string(int status, CRYPT_SESSION sess, char estr[SSL_ESTR_
 
 static pthread_once_t crypt_init_once = PTHREAD_ONCE_INIT;
 static pthread_mutex_t ssl_cert_mutex;
+static bool cryptlib_initialized;
 
 static void do_cryptEnd(void)
 {
@@ -66,6 +67,7 @@ static void internal_do_cryptInit(void)
 	if((ret=cryptInit())==CRYPT_OK) {
 		cryptAddRandom(NULL,CRYPT_RANDOM_SLOWPOLL);
 		atexit(do_cryptEnd);
+		cryptlib_initialized = true;
 	}
 	else {
 		lprintf(LOG_ERR,"cryptInit() returned %d", ret);
@@ -81,6 +83,11 @@ int DLLCALL do_cryptInit(void)
 	return 0;
 }
 
+bool DLLCALL is_crypt_initialized(void)
+{
+	return cryptlib_initialized;
+}
+
 #define DO(x)	get_error_string(x, ssl_context, estr, __FILE__, __LINE__)
 
 CRYPT_CONTEXT DLLCALL get_ssl_cert(scfg_t *cfg, char estr[SSL_ESTR_LEN])
diff --git a/src/sbbs3/ssl.h b/src/sbbs3/ssl.h
index 439c07c066..e1df40807e 100644
--- a/src/sbbs3/ssl.h
+++ b/src/sbbs3/ssl.h
@@ -4,6 +4,7 @@
 #include "sbbs.h"	// For DLLEXPORT
 #include <cryptlib.h>
 #include "scfgdefs.h"
+#include <stdbool.h>
 
 #ifdef DLLEXPORT
 #undef DLLEXPORT
@@ -38,6 +39,7 @@ DLLEXPORT char* DLLCALL get_crypt_attribute(CRYPT_SESSION sess, C_IN CRYPT_ATTRI
 DLLEXPORT char* DLLCALL get_crypt_error(CRYPT_SESSION sess);
 DLLEXPORT CRYPT_CONTEXT DLLCALL get_ssl_cert(scfg_t *cfg, char estr[SSL_ESTR_LEN]);
 DLLEXPORT int DLLCALL do_cryptInit(void);
+DLLEXPORT bool DLLCALL is_crypt_initialized(void);
 
 #if defined(__cplusplus)
 }
diff --git a/src/sbbs3/ver.cpp b/src/sbbs3/ver.cpp
index 2198b42743..3440e92376 100644
--- a/src/sbbs3/ver.cpp
+++ b/src/sbbs3/ver.cpp
@@ -37,6 +37,7 @@
  ****************************************************************************/
 
 #include "sbbs.h"
+#include "ssl.h"
 
 const char* beta_version = " "; /* Space if non-beta, " beta" otherwise */
 
@@ -109,7 +110,7 @@ void sbbs_t::ver()
 	socklib_version(str,SOCKLIB_DESC);
 
 #ifdef USE_CRYPTLIB
-	{
+	if(is_crypt_initialized()) {
 		int cl_major=0, cl_minor=0, cl_step=0;
 		int result;
 		result = cryptGetAttribute(CRYPT_UNUSED, CRYPT_OPTION_INFO_MAJORVERSION, &cl_major);
-- 
GitLab