Skip to contentSkip to conversation

Load OpenSSL config on Windows from trusted location

PATCH Superseded Lev Stipakov 3 participants 16 messages Nov 18, 2021 – Nov 24, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Load OpenSSL config on Windows from trusted location

From: Lev Stipakov <[email protected]...>

Commit 7e33127d5 ("contrib/vcpkg-ports: remove openssl port") disabled OpenSSL config loading to prevent loading config from untrusted locations.

Config loading feature might be useful for some users. This brings it back, and sets OpenSSL enviroment variables

OPENSSL_CONF, OPENSSL_ENGINES, OPENSSL_MODULES

which are used to load config, engines and modules, to a trusted location. The location is built based on installation path, read from registry on startup. If installation path cannot be read, Windows\System32 is used as a fallback.

While on it, remove unused "bool impersonate_as_system();" declaration.

---
 .../vcpkg-triplets/arm64-windows-ovpn.cmake   |  2 -
 contrib/vcpkg-triplets/x64-windows-ovpn.cmake |  2 -
 contrib/vcpkg-triplets/x86-windows-ovpn.cmake |  2 -
 src/openvpn/buffer.c                          | 23 ------
 src/openvpn/win32.c                           | 76 +++++++++++++++++++
 src/openvpn/win32.h                           |  8 +-
 6 files changed, 83 insertions(+), 30 deletions(-)
contrib/vcpkg-triplets/arm64-windows-ovpn.cmake +0 -2
index 89f6a279..dd3c6c0a 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x64-windows-ovpn.cmake +0 -2
index d860eed6..7036ed2d 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x86-windows-ovpn.cmake +0 -2
index c1ea6ef3..7d3bf340 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
src/openvpn/buffer.c +0 -23
index c82d3d4d..54e758af 100644
@@ -310,29 +310,6 @@ openvpn_snprintf(char *str, size_t size, const char *format, ...)
     return (len >= 0 && len < size);
 }
 
-/*
- * openvpn_swprintf() is currently only used by Windows code paths
- * and when enabled for all platforms it will currently break older
- * OpenBSD versions lacking vswprintf(3) support in their libc.
- */
-
-#ifdef _WIN32
-bool
-openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...)
-{
-    va_list arglist;
-    int len = -1;
-    if (size > 0)
-    {
-        va_start(arglist, format);
-        len = vswprintf(str, size, format, arglist);
-        va_end(arglist);
-        str[size - 1] = L'\0';
-    }
-    return (len >= 0 && len < size);
-}
-#endif
-
 /*
  * write a string to the end of a buffer that was
  * truncated by buf_printf
src/openvpn/win32.c +76 -0

92 lines not on this page · raw message

src/openvpn/win32.h +7 -1
index 5d3371a0..4a992d91 100644
@@ -327,7 +327,13 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size,
 int
 openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
 
-bool impersonate_as_system();
+/*
+ * openvpn_swprintf() is currently only used by Windows code paths
+ * and when enabled for all platforms it will currently break older
+ * OpenBSD versions lacking vswprintf(3) support in their libc.
+ */
+bool
+openvpn_swprintf(wchar_t* const str, const size_t size, const wchar_t* const format, ...);
 
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */
Signature · 1 line
2.23.0.windows.1
#1 lore raw

PATCH v2 1/1 Load OpenSSL config on Windows from trusted location

From: Lev Stipakov <[email protected]...>

Commit 7e33127d5 ("contrib/vcpkg-ports: remove openssl port") disabled OpenSSL config loading to prevent loading config from untrusted locations.

Config loading feature might be useful for some users. This brings it back, and sets OpenSSL enviroment variables

OPENSSL_CONF, OPENSSL_ENGINES, OPENSSL_MODULES

which are used to load config, engines and modules, to a trusted location. The location is built based on installation path, read from registry on startup. If installation path cannot be read, Windows\System32 is used as a fallback.

While on it, remove unused "bool impersonate_as_system();" declaration.

---
v2: add missing "static" modifier to set_openssl_env_vars()
declaration noted by gcc

 .../vcpkg-triplets/arm64-windows-ovpn.cmake   |  2 -
 contrib/vcpkg-triplets/x64-windows-ovpn.cmake |  2 -
 contrib/vcpkg-triplets/x86-windows-ovpn.cmake |  2 -
 src/openvpn/buffer.c                          | 23 ------
 src/openvpn/win32.c                           | 76 +++++++++++++++++++
 src/openvpn/win32.h                           |  8 +-
 6 files changed, 83 insertions(+), 30 deletions(-)
contrib/vcpkg-triplets/arm64-windows-ovpn.cmake +0 -2
index 89f6a279..dd3c6c0a 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x64-windows-ovpn.cmake +0 -2
index d860eed6..7036ed2d 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x86-windows-ovpn.cmake +0 -2
index c1ea6ef3..7d3bf340 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
src/openvpn/buffer.c +0 -23
index c82d3d4d..54e758af 100644
@@ -310,29 +310,6 @@ openvpn_snprintf(char *str, size_t size, const char *format, ...)
     return (len >= 0 && len < size);
 }
 
-/*
- * openvpn_swprintf() is currently only used by Windows code paths
- * and when enabled for all platforms it will currently break older
- * OpenBSD versions lacking vswprintf(3) support in their libc.
- */
-
-#ifdef _WIN32
-bool
-openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...)
-{
-    va_list arglist;
-    int len = -1;
-    if (size > 0)
-    {
-        va_start(arglist, format);
-        len = vswprintf(str, size, format, arglist);
-        va_end(arglist);
-        str[size - 1] = L'\0';
-    }
-    return (len >= 0 && len < size);
-}
-#endif
-
 /*
  * write a string to the end of a buffer that was
  * truncated by buf_printf
src/openvpn/win32.c +76 -0

92 lines not on this page · raw message

src/openvpn/win32.h +7 -1
index 5d3371a0..4a992d91 100644
@@ -327,7 +327,13 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size,
 int
 openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
 
-bool impersonate_as_system();
+/*
+ * openvpn_swprintf() is currently only used by Windows code paths
+ * and when enabled for all platforms it will currently break older
+ * OpenBSD versions lacking vswprintf(3) support in their libc.
+ */
+bool
+openvpn_swprintf(wchar_t* const str, const size_t size, const wchar_t* const format, ...);
 
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */
Signature · 1 line
2.23.0.windows.1
#2 lore raw

Hi,

+1 for setting these env vars. I will test this but some quick comments

Quoted 155 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

Commit 7e33127d5 ("contrib/vcpkg-ports: remove openssl port") disabled OpenSSL config loading to prevent loading config from untrusted locations.

Config loading feature might be useful for some users. This brings it back, and sets OpenSSL enviroment variables

OPENSSL_CONF, OPENSSL_ENGINES, OPENSSL_MODULES

which are used to load config, engines and modules, to a trusted location. The location is built based on installation path, read from registry on startup.
If installation path cannot be read, Windows\System32 is used as a fallback.

While on it, remove unused "bool impersonate_as_system();" declaration.

---
v2: add missing "static" modifier to set_openssl_env_vars()
declaration noted by gcc

 .../vcpkg-triplets/arm64-windows-ovpn.cmake   |  2 -
 contrib/vcpkg-triplets/x64-windows-ovpn.cmake |  2 -
 contrib/vcpkg-triplets/x86-windows-ovpn.cmake |  2 -
 src/openvpn/buffer.c                          | 23 ------
 src/openvpn/win32.c                           | 76 +++++++++++++++++++
 src/openvpn/win32.h                           |  8 +-
 6 files changed, 83 insertions(+), 30 deletions(-)
contrib/vcpkg-triplets/arm64-windows-ovpn.cmake +0 -2
index 89f6a279..dd3c6c0a 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x64-windows-ovpn.cmake +0 -2
index d860eed6..7036ed2d 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x86-windows-ovpn.cmake +0 -2
index c1ea6ef3..7d3bf340 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
src/openvpn/buffer.c +0 -23
index c82d3d4d..54e758af 100644
@@ -310,29 +310,6 @@ openvpn_snprintf(char *str, size_t size, const char
 *format, ...)
     return (len >= 0 && len < size);
 }
 
-/*
- * openvpn_swprintf() is currently only used by Windows code paths
- * and when enabled for all platforms it will currently break older
- * OpenBSD versions lacking vswprintf(3) support in their libc.
- */
-
-#ifdef _WIN32
-bool
-openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t
 *const format, ...)
-{
-    va_list arglist;
-    int len = -1;
-    if (size > 0)
-    {
-        va_start(arglist, format);
-        len = vswprintf(str, size, format, arglist);
-        va_end(arglist);
-        str[size - 1] = L'\0';
-    }
-    return (len >= 0 && len < size);
-}
-#endif
-
 /*

* write a string to the end of a buffer that was
* truncated by buf_printf

src/openvpn/win32.c +10 -0
index 6cff17b2..ebc7c833 100644
@@ -101,6 +101,12 @@ struct semaphore netcmd_semaphore; /* GLOBAL */
  */
 static char *win_sys_path = NULL; /* GLOBAL */
 
+/**
+ * Set OpenSSL environment variables to a safe directory
+ */
+static void
+set_openssl_env_vars();
+
 void
 init_win32(void)
 {
@@ -110,6 +116,8 @@ init_win32(void)
     }
     window_title_clear(&window_title);
     win32_signal_clear(&win32_signal);
+
+    set_openssl_env_vars();
 }
 
 void
@@ -1509,4 +1517,72 @@ send_msg_iservice(HANDLE pipe, const void *data,
 size_t size,
     return ret;
 }
 
+bool
+openvpn_swprintf(wchar_t* const str, const size_t size, const wchar_t*

const format, ...)

src/openvpn/win32.c +20 -0
+{
+    va_list arglist;
+    int len = -1;
+    if (size > 0)
+    {
+        va_start(arglist, format);
+        len = vswprintf(str, size, format, arglist);
+        va_end(arglist);
+        str[size - 1] = L'\0';
+    }
+    return (len >= 0 && len < size);
+}
+
+static BOOL
+get_install_path(WCHAR *path, DWORD size)
+{
+    WCHAR reg_path[256];
+    HKEY key;
+    BOOL res = FALSE;
+    openvpn_swprintf(reg_path, _countof(reg_path), L"SOFTWARE\\"
Quoted 2 lines

PACKAGE_NAME);

Does this string concatenation like L"foo" "bar" work correctly on MSVC? I know it works on mingw, but in the past with the GUI resources we had run into issues with such usage -- iirc, MSVC wanted L"foo" L"bar".

Quoted 30 lines
src/openvpn/win32.c +2 -0
+
+    LONG status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, reg_path, 0,

KEY_READ, &key);

src/openvpn/win32.c +7 -0
+    if (status != ERROR_SUCCESS)
+    {
+        return res;
+    }
+
+    /* The default value of REG_KEY is the install path */
+    status = RegGetValueW(key, NULL, NULL, RRF_RT_REG_SZ, NULL,

(LPBYTE)path, &size);

src/openvpn/win32.c +19 -0
+    res = status == ERROR_SUCCESS;
+
+    RegCloseKey(key);
+
+    return res;
+}
+
+static void
+set_openssl_env_vars()
+{
+    const WCHAR* ssl_fallback_dir = L"C:\\Windows\\System32\\";
+
+    WCHAR install_path[MAX_PATH] = { 0 };
+    if (!get_install_path(install_path, _countof(install_path)))
+    {
+        /* if we cannot find installation path from the registry,
+         * use Windows directory as a fallback
+         */
+        openvpn_swprintf(install_path, _countof(install_path), L"%ls",
Quoted 3 lines

ssl_fallback_dir);
+ }

Is this registry value guaranteed to end with the directory separator "\\"? Should we check it? If so, it may be easier to strip it here and add in the format with no ending "\\" in the default above.

src/openvpn/win32.c send_msg_iservice(HANDLE pipe, const void *data, size_t size, · line 1574

Quoted 5 lines
src/openvpn/win32.c +4 -0
+
+    WCHAR openssl_cnf[MAX_PATH] = {0};
+    WCHAR openssl_engines[MAX_PATH] = {0};
+    WCHAR openssl_modules[MAX_PATH] = {0};

On linux one needs strings on the heap or const strings for putenv() as it does not make a copy unlike setenv (with some exceptions). Does _wputenv on Windows behave differently? Do local variables work?

src/openvpn/win32.c send_msg_iservice(HANDLE pipe, const void *data, size_t size, · line 1585

Quoted 12 lines
src/openvpn/win32.c +11 -0
+
+    openvpn_swprintf(openssl_cnf, _countof(install_path),
+        L"OPENSSL_CONF=%lsssl\\openssl.cnf", install_path);
+    openvpn_swprintf(openssl_engines, _countof(openssl_engines),
+        L"OPENSSL_ENGINES=%lsssl\\engines", install_path);
+    openvpn_swprintf(openssl_modules, _countof(openssl_modules),
+        L"OPENSSL_MODULES=%lsssl\\modules", install_path);
+
+    _wputenv(openssl_cnf);
+    _wputenv(openssl_engines);
+    _wputenv(openssl_modules);

I think we should set these only if not already set. Otherwise the user has no way of overriding these locations which is the original purpose of env vars.

Quoted 23 lines
src/openvpn/win32.c +2 -0
+}
+
 #endif /* ifdef _WIN32 */
src/openvpn/win32.h +7 -1
index 5d3371a0..4a992d91 100644
@@ -327,7 +327,13 @@ bool send_msg_iservice(HANDLE pipe, const void *data,
 size_t size,
 int
 openvpn_execve(const struct argv *a, const struct env_set *es, const
 unsigned int flags);
 
-bool impersonate_as_system();
+/*
+ * openvpn_swprintf() is currently only used by Windows code paths
+ * and when enabled for all platforms it will currently break older
+ * OpenBSD versions lacking vswprintf(3) support in their libc.
+ */
+bool
+openvpn_swprintf(wchar_t* const str, const size_t size, const wchar_t*
 const format, ...);
Quoted 3 lines

#endif /* ifndef OPENVPN_WIN32_H */
#endif /* ifdef _WIN32 */
--

Commit message says this brings back config loading but the config loading in crypto_openssl.c stays disabled for Windows. An oversight?

Selva

#3 lore raw

Hi,

Quoted 1 line

Does this string concatenation like L"foo" "bar" work correctly on MSVC? I know it works on mingw, but in the past with the GUI resources we had run into issues with such usage -- iirc, MSVC wanted L"foo" L"bar".

I think so, at least result of concatenation looks correct in procmon:

Quoted 3 lines

openvpn.exe 23312 CreateFile C:\Program Files\OpenVPN\ssl\openssl.cnf PATH NOT FOUND

Is this registry value guaranteed to end with the directory separator "\\"? Should we check it? If so, it may be easier to strip it here and add in the format with no ending "\\" in the default above.

Those are set by our MSI script. But let's be on the safe side.

Quoted 1 line

On linux one needs strings on the heap or const strings for putenv() as it does not make a copy unlike setenv (with some exceptions). Does _wputenv on Windows behave differently? Do local variables work?

At least they work in my tests. This
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-wputenv?view=msvc-170
doesn't mention anything about automatic variables cannot be used, contrary to "man putenv".

Quoted 6 lines

src/openvpn/win32.c send_msg_iservice(HANDLE pipe, const void *data, size_t size, · line 1585

Quoted 3 lines
src/openvpn/win32.c +3 -0
+    _wputenv(openssl_cnf);
+    _wputenv(openssl_engines);
+    _wputenv(openssl_modules);

I think we should set these only if not already set. Otherwise the user has no way of overriding these locations which is the original purpose of env vars.

Will do.

Quoted 1 line

Commit message says this brings back config loading but the config loading in crypto_openssl.c stays disabled for Windows. An oversight?

Good catch. Interesting that it works for me even with that code in crypto_openssl.c being disabled. It looks like our SSL context initialization does the right thing:

Quoted 1 line

libcrypto-1_1-x64.dll!OPENSSL_init_crypto(unsigned __int64 opts, const ossl_init_settings_st * settings) Line 699 C

libssl-1_1-x64.dll!OPENSSL_init_ssl(unsigned __int64 opts, const
ossl_init_settings_st * settings) Line 205 C
libssl-1_1-x64.dll!SSL_CTX_new(const ssl_method_st * meth) Line 3023 C openvpn.exe!tls_ctx_client_new(tls_root_ctx * ctx) Line 134 C openvpn.exe!init_ssl(const options * options, tls_root_ctx *
new_ctx, bool in_chroot) Line 622 C
openvpn.exe!do_init_crypto_tls_c1(context * c) Line 2741 C openvpn.exe!do_init_crypto_tls(context * c, const unsigned int
flags) Line 2821 C
openvpn.exe!do_init_crypto(context * c, const unsigned int flags) Line 3084 C openvpn.exe!init_instance(context * c, const env_set * env, const
unsigned int flags) Line 4311 C
openvpn.exe!init_instance_handle_signals(context * c, const env_set
* env, const unsigned int flags) Line 4124 C openvpn.exe!tunnel_point_to_point(context * c) Line 68 C

and opts got the correct bitflag:

#ifndef OPENSSL_NO_AUTOLOAD_CONFIG
    if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) == 0)
        opts |= OPENSSL_INIT_LOAD_CONFIG;

#endif

Does that code in crypto_openssl.c cover some unusual use case? Anyway I can bring it back.

Signature · 1 line
-Lev
#4 lore raw

PATCH v3 1/1 Load OpenSSL config on Windows from trusted location

From: Lev Stipakov <[email protected]...>

Commits

- 92535b6 ("contrib/vcpkg-ports: add openssl port with --no-autoload-config option set (CVE-2121-3606)")
- 447cfb4 ("crypto_openssl.c: disable explicit initialization on Windows (CVE-2121-3606)")

disabled OpenSSL config loading functionality, which could be exploited by loading config from untrusted locations.

However, ability to load config might be useful for some users.

This brings config loading back and sets OpenSSL enviroment variables

OPENSSL_CONF, OPENSSL_ENGINES, OPENSSL_MODULES

which are used to load config, engines and modules, to a trusted location. The location is constructed based on installation path, read from registry on startup. If installation path cannot be read, Windows\System32 is used as a fallback.

While on it, remove unused "bool impersonate_as_system();" declaration.

---

v3:
- do not assume that installation path ends with directory separator
- set enviroment variables only if they're not already set
- bring back explicit initialization on Windows (might be needed on some cases)
- slightly revamp commit message

v2:
- add missing "static" modifier to set_openssl_env_vars() declaration
spotted by gcc

 .../vcpkg-triplets/arm64-windows-ovpn.cmake   |  2 -
 contrib/vcpkg-triplets/x64-windows-ovpn.cmake |  2 -
 contrib/vcpkg-triplets/x86-windows-ovpn.cmake |  2 -
 src/openvpn/buffer.c                          | 23 -----
 src/openvpn/crypto_openssl.c                  |  2 -
 src/openvpn/win32.c                           | 92 +++++++++++++++++++
 src/openvpn/win32.h                           |  8 +-
 7 files changed, 99 insertions(+), 32 deletions(-)
contrib/vcpkg-triplets/arm64-windows-ovpn.cmake +0 -2
index 89f6a279..dd3c6c0a 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x64-windows-ovpn.cmake +0 -2
index d860eed6..7036ed2d 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x86-windows-ovpn.cmake +0 -2
index c1ea6ef3..7d3bf340 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
src/openvpn/buffer.c +0 -23
index c82d3d4d..54e758af 100644
@@ -310,29 +310,6 @@ openvpn_snprintf(char *str, size_t size, const char *format, ...)
     return (len >= 0 && len < size);
 }
 
-/*
- * openvpn_swprintf() is currently only used by Windows code paths
- * and when enabled for all platforms it will currently break older
- * OpenBSD versions lacking vswprintf(3) support in their libc.
- */
-
-#ifdef _WIN32
-bool
-openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...)
-{
-    va_list arglist;
-    int len = -1;
-    if (size > 0)
-    {
-        va_start(arglist, format);
-        len = vswprintf(str, size, format, arglist);
-        va_end(arglist);
-        str[size - 1] = L'\0';
-    }
-    return (len >= 0 && len < size);
-}
-#endif
-
 /*
  * write a string to the end of a buffer that was
  * truncated by buf_printf
src/openvpn/crypto_openssl.c +0 -2
index c9dc9d0a..ef520928 100644
@@ -154,13 +154,11 @@ crypto_init_lib_engine(const char *engine_name)
 void
 crypto_init_lib(void)
 {
-#ifndef _WIN32
 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
 #else
     OPENSSL_config(NULL);
 #endif
-#endif /* _WIN32 */
     /*
      * If you build the OpenSSL library and OpenVPN with
      * CRYPTO_MDEBUG, you will get a listing of OpenSSL
src/openvpn/win32.c +92 -0

108 lines not on this page · raw message

src/openvpn/win32.h +7 -1
index 5d3371a0..4a992d91 100644
@@ -327,7 +327,13 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size,
 int
 openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
 
-bool impersonate_as_system();
+/*
+ * openvpn_swprintf() is currently only used by Windows code paths
+ * and when enabled for all platforms it will currently break older
+ * OpenBSD versions lacking vswprintf(3) support in their libc.
+ */
+bool
+openvpn_swprintf(wchar_t* const str, const size_t size, const wchar_t* const format, ...);
 
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */
Signature · 1 line
2.23.0.windows.1
#5 lore raw

Hi,

src/openvpn/win32.c send_msg_iservice(HANDLE pipe, const void *data, size_t size, · line 1582

Quoted 11 lines · Lev Stipakov
src/openvpn/win32.c +11 -0
+    if ((install_path[wcslen(install_path) - 1]) == L'\\')
+    {
+        install_path[wcslen(install_path) - 1] = L'\0';
+    }
+
+    WCHAR openssl_cnf[MAX_PATH] = {0};
+    WCHAR openssl_engines[MAX_PATH] = {0};
+    WCHAR openssl_modules[MAX_PATH] = {0};
+
+    openvpn_swprintf(openssl_cnf, _countof(install_path),
+        L"OPENSSL_CONF=%ls\\ssl\\openssl.cnf", install_path);

This needs to be _countof(openssl_cnf) - even if they are the same today, they might not be tomorrow.

While at it, I wonder if it is more orderly to move the swprintf() calls in the "if NULL" clause now... like this:

   if (_wgetenv(L"OPENSSL_CONF") == NULL)
   {
       WCHAR openssl_cnf[MAX_PATH] = {0};
       openvpn_swprintf(openssl_cnf, _countof(openssl_cnf),
           L"OPENSSL_CONF=%ls\\ssl\\openssl.cnf", install_path);
       _wputenv(openssl_cnf);

}

(I would not have brought this up for a v4, but the _countof() needs to be fixed anyway)

Regarding Selva's comment on the scope of the memory passed to _wputenv(), I checked MS documentation, and it does not say anything.

OTOH, it points to _wputenv_s(varname,string) which might be worth considering...

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-s-wputenv-s?view=msvc-170

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#6 lore raw
Quoted 25 lines · Gert Doering

Hi,

src/openvpn/win32.c send_msg_iservice(HANDLE pipe, const void *data, size_t size, · line 1582

Quoted 11 lines · Lev Stipakov
src/openvpn/win32.c +11 -0
+    if ((install_path[wcslen(install_path) - 1]) == L'\\')
+    {
+        install_path[wcslen(install_path) - 1] = L'\0';
+    }
+
+    WCHAR openssl_cnf[MAX_PATH] = {0};
+    WCHAR openssl_engines[MAX_PATH] = {0};
+    WCHAR openssl_modules[MAX_PATH] = {0};
+
+    openvpn_swprintf(openssl_cnf, _countof(install_path),
+        L"OPENSSL_CONF=%ls\\ssl\\openssl.cnf", install_path);

This needs to be _countof(openssl_cnf) - even if they are the same today, they might not be tomorrow.

While at it, I wonder if it is more orderly to move the swprintf() calls in the "if NULL" clause now... like this:

   if (_wgetenv(L"OPENSSL_CONF") == NULL)
   {
       WCHAR openssl_cnf[MAX_PATH] = {0};
Quoted 8 lines
       openvpn_swprintf(openssl_cnf, _countof(openssl_cnf),
           L"OPENSSL_CONF=%ls\\ssl\\openssl.cnf", install_path);
       _wputenv(openssl_cnf);

}

(I would not have brought this up for a v4, but the _countof() needs to be fixed anyway)

If you are doing a v4 you may want to consider:

static struct {
wchar_t *name;
wchar_t *value;
} ossl_env[] = {{L"OPENSSL_CNF", L"openssl.cnf"},

                        {"OPENSSL_ENGINES", L"engines"},
                        {..}};

and use a loop. Less local arrays, easy to add more to the env zoo later etc..

Just saying --- I'm okay with the current style too.

Quoted 3 lines

Regarding Selva's comment on the scope of the memory passed to _wputenv(), I checked MS documentation, and it does not say anything.

MS docs say _putenv is their implementation of POSIX putenv but the latter does imply the pointer supplied by the user should be used as is. Anyway, if it works with automatics, good for us.

Quoted 4 lines

OTOH, it points to _wputenv_s(varname,string) which might be worth considering...

+1 to that especially if MSVC is planning to deprecate _wputenv as they have already done for a host of other such functions.

Selva

#7 lore raw

PATCH v4 1/1 Load OpenSSL config on Windows from trusted location

From: Lev Stipakov <[email protected]...>

Commits

- 92535b6 ("contrib/vcpkg-ports: add openssl port with --no-autoload-config option set (CVE-2121-3606)")
- 447cfb4 ("crypto_openssl.c: disable explicit initialization on Windows (CVE-2121-3606)")

disabled OpenSSL config loading functionality, which could be exploited by loading config from untrusted locations.

This feature might be useful for some users. This brings it back and sets OpenSSL enviroment variables

OPENSSL_CONF, OPENSSL_ENGINES, OPENSSL_MODULES

which are used to load config, engines and modules, to a trusted location. The location is constructed based on installation path, read from registry on startup. If installation path cannot be read, Windows\System32 is used as a fallback.

While on it, remove unused "bool impersonate_as_system();" declaration.

---
v4:
- make set_openssl_env_vars() code more succint
- use security-enhanced _wputenv_s/_wgetenv_s

v3:
- do not assume that installation path ends with directory separator
- set enviroment variables only if they're not already set
- bring back explicit initialization on Windows (might be needed on some cases)
- slightly revamp commit message

v2:
- add missing "static" modifier to set_openssl_env_vars() declaration
spotted by gcc

 .../vcpkg-triplets/arm64-windows-ovpn.cmake   |  2 -
 contrib/vcpkg-triplets/x64-windows-ovpn.cmake |  2 -
 contrib/vcpkg-triplets/x86-windows-ovpn.cmake |  2 -
 src/openvpn/buffer.c                          | 23 -----
 src/openvpn/crypto_openssl.c                  |  2 -
 src/openvpn/win32.c                           | 88 +++++++++++++++++++
 src/openvpn/win32.h                           |  8 +-
 7 files changed, 95 insertions(+), 32 deletions(-)
contrib/vcpkg-triplets/arm64-windows-ovpn.cmake +0 -2
index 89f6a279..dd3c6c0a 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x64-windows-ovpn.cmake +0 -2
index d860eed6..7036ed2d 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x86-windows-ovpn.cmake +0 -2
index c1ea6ef3..7d3bf340 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
src/openvpn/buffer.c +0 -23
index c82d3d4d..54e758af 100644
@@ -310,29 +310,6 @@ openvpn_snprintf(char *str, size_t size, const char *format, ...)
     return (len >= 0 && len < size);
 }
 
-/*
- * openvpn_swprintf() is currently only used by Windows code paths
- * and when enabled for all platforms it will currently break older
- * OpenBSD versions lacking vswprintf(3) support in their libc.
- */
-
-#ifdef _WIN32
-bool
-openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...)
-{
-    va_list arglist;
-    int len = -1;
-    if (size > 0)
-    {
-        va_start(arglist, format);
-        len = vswprintf(str, size, format, arglist);
-        va_end(arglist);
-        str[size - 1] = L'\0';
-    }
-    return (len >= 0 && len < size);
-}
-#endif
-
 /*
  * write a string to the end of a buffer that was
  * truncated by buf_printf
src/openvpn/crypto_openssl.c +0 -2
index c9dc9d0a..ef520928 100644
@@ -154,13 +154,11 @@ crypto_init_lib_engine(const char *engine_name)
 void
 crypto_init_lib(void)
 {
-#ifndef _WIN32
 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
 #else
     OPENSSL_config(NULL);
 #endif
-#endif /* _WIN32 */
     /*
      * If you build the OpenSSL library and OpenVPN with
      * CRYPTO_MDEBUG, you will get a listing of OpenSSL
src/openvpn/win32.c +88 -0

104 lines not on this page · raw message

src/openvpn/win32.h +7 -1
index 5d3371a0..4a992d91 100644
@@ -327,7 +327,13 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size,
 int
 openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
 
-bool impersonate_as_system();
+/*
+ * openvpn_swprintf() is currently only used by Windows code paths
+ * and when enabled for all platforms it will currently break older
+ * OpenBSD versions lacking vswprintf(3) support in their libc.
+ */
+bool
+openvpn_swprintf(wchar_t* const str, const size_t size, const wchar_t* const format, ...);
 
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */
Signature · 1 line
2.23.0.windows.1
Acked-by on v4 1/1
#8 lore raw

Hi,

Looks good in my tests using the msvc artifacts from
https://github.com/lstipakov/openvpn/actions/runs/1496339867#artifacts.

Loads config from <install-root>\ssl\openssl.cnf and engines specified with relative paths load from <install-root>\ssl\engines. So the env vars are being seen by OpenSSL and being used as expected.

I wasted quite some time using the msvc-built executable with mingw compiled OpenSSL libraries (I was being lazy copying only openvpn.exe). For some reason OpenSSL doesn't detect the env vars in this case --- process explorer shows its all set right, but the library doesn't load openssl.cnf. Not worth exploring further, I guess.

Testing OPENSSL_MODULES path is left for later when a 3.0 build with MSVC is available.

Some nits below which may be ignored.

Quoted 192 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

Commits

- 92535b6 ("contrib/vcpkg-ports: add openssl port with
--no-autoload-config option set (CVE-2121-3606)")
- 447cfb4 ("crypto_openssl.c: disable explicit initialization on Windows
(CVE-2121-3606)")

disabled OpenSSL config loading functionality, which could be exploited by loading config from untrusted locations.

This feature might be useful for some users. This brings it back and sets OpenSSL enviroment variables

OPENSSL_CONF, OPENSSL_ENGINES, OPENSSL_MODULES

which are used to load config, engines and modules, to a trusted location. The location is constructed based on installation path, read from registry on startup.
If installation path cannot be read, Windows\System32 is used as a fallback.

While on it, remove unused "bool impersonate_as_system();" declaration.

---
v4:
- make set_openssl_env_vars() code more succint
- use security-enhanced _wputenv_s/_wgetenv_s

v3:
- do not assume that installation path ends with directory separator
- set enviroment variables only if they're not already set
- bring back explicit initialization on Windows (might be needed on some cases)
- slightly revamp commit message

v2:
- add missing "static" modifier to set_openssl_env_vars() declaration
spotted by gcc

 .../vcpkg-triplets/arm64-windows-ovpn.cmake   |  2 -
 contrib/vcpkg-triplets/x64-windows-ovpn.cmake |  2 -
 contrib/vcpkg-triplets/x86-windows-ovpn.cmake |  2 -
 src/openvpn/buffer.c                          | 23 -----
 src/openvpn/crypto_openssl.c                  |  2 -
 src/openvpn/win32.c                           | 88 +++++++++++++++++++
 src/openvpn/win32.h                           |  8 +-
 7 files changed, 95 insertions(+), 32 deletions(-)
contrib/vcpkg-triplets/arm64-windows-ovpn.cmake +0 -2
index 89f6a279..dd3c6c0a 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x64-windows-ovpn.cmake +0 -2
index d860eed6..7036ed2d 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x86-windows-ovpn.cmake +0 -2
index c1ea6ef3..7d3bf340 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
src/openvpn/buffer.c +0 -23
index c82d3d4d..54e758af 100644
@@ -310,29 +310,6 @@ openvpn_snprintf(char *str, size_t size, const char
 *format, ...)
     return (len >= 0 && len < size);
 }
 
-/*
- * openvpn_swprintf() is currently only used by Windows code paths
- * and when enabled for all platforms it will currently break older
- * OpenBSD versions lacking vswprintf(3) support in their libc.
- */
-
-#ifdef _WIN32
-bool
-openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t
 *const format, ...)
-{
-    va_list arglist;
-    int len = -1;
-    if (size > 0)
-    {
-        va_start(arglist, format);
-        len = vswprintf(str, size, format, arglist);
-        va_end(arglist);
-        str[size - 1] = L'\0';
-    }
-    return (len >= 0 && len < size);
-}
-#endif
-
 /*

* write a string to the end of a buffer that was
* truncated by buf_printf

src/openvpn/crypto_openssl.c +0 -2
index c9dc9d0a..ef520928 100644
@@ -154,13 +154,11 @@ crypto_init_lib_engine(const char *engine_name)
 void
 crypto_init_lib(void)
 {
-#ifndef _WIN32
 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
 #else
     OPENSSL_config(NULL);
 #endif
-#endif /* _WIN32 */
     /*
      * If you build the OpenSSL library and OpenVPN with
      * CRYPTO_MDEBUG, you will get a listing of OpenSSL
src/openvpn/win32.c +10 -0
index 6cff17b2..ee4d3f66 100644
@@ -101,6 +101,12 @@ struct semaphore netcmd_semaphore; /* GLOBAL */
  */
 static char *win_sys_path = NULL; /* GLOBAL */
 
+/**
+ * Set OpenSSL environment variables to a safe directory
+ */
+static void
+set_openssl_env_vars();
+
 void
 init_win32(void)
 {
@@ -110,6 +116,8 @@ init_win32(void)
     }
     window_title_clear(&window_title);
     win32_signal_clear(&win32_signal);
+
+    set_openssl_env_vars();
 }
 
 void
@@ -1509,4 +1517,84 @@ send_msg_iservice(HANDLE pipe, const void *data,
 size_t size,
     return ret;
 }
 
+bool
+openvpn_swprintf(wchar_t* const str, const size_t size, const wchar_t*

const format, ...)

src/openvpn/win32.c +20 -0
+{
+    va_list arglist;
+    int len = -1;
+    if (size > 0)
+    {
+        va_start(arglist, format);
+        len = vswprintf(str, size, format, arglist);
+        va_end(arglist);
+        str[size - 1] = L'\0';
+    }
+    return (len >= 0 && len < size);
+}
+
+static BOOL
+get_install_path(WCHAR *path, DWORD size)
+{
+    WCHAR reg_path[256];
+    HKEY key;
+    BOOL res = FALSE;
+    openvpn_swprintf(reg_path, _countof(reg_path), L"SOFTWARE\\"
Quoted 1 line

PACKAGE_NAME);

+

Quoted 36 lines

+ LONG status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, reg_path, 0,
KEY_READ, &key);

src/openvpn/win32.c +7 -0
+    if (status != ERROR_SUCCESS)
+    {
+        return res;
+    }
+
+    /* The default value of REG_KEY is the install path */
+    status = RegGetValueW(key, NULL, NULL, RRF_RT_REG_SZ, NULL,

(LPBYTE)path, &size);

src/openvpn/win32.c +19 -0
+    res = status == ERROR_SUCCESS;
+
+    RegCloseKey(key);
+
+    return res;
+}
+
+static void
+set_openssl_env_vars()
+{
+    const WCHAR* ssl_fallback_dir = L"C:\\Windows\\System32";
+
+    WCHAR install_path[MAX_PATH] = { 0 };
+    if (!get_install_path(install_path, _countof(install_path)))
+    {
+        /* if we cannot find installation path from the registry,
+         * use Windows directory as a fallback
+         */
+        openvpn_swprintf(install_path, _countof(install_path), L"%ls",

ssl_fallback_dir);

src/openvpn/win32.c +5 -0
+    }
+
+    if ((install_path[wcslen(install_path) - 1]) == L'\\')
+    {
+        install_path[wcslen(install_path) - 1] = L'\0';

src/openvpn/win32.c send_msg_iservice(HANDLE pipe, const void *data, size_t size, · line 1584

Quoted 10 lines
src/openvpn/win32.c +10 -0
+    }
+
+    static struct {
+        WCHAR* name;
+        WCHAR* value;
+    } ossl_env[] = {
+        {L"OPENSSL_CONF", L"openssl.cnf"},
+        {L"OPENSSL_ENGINES", L"engines"},
+        {L"OPENSSL_MODULES", L"modules"}
+    };

As you defined this inside the function, it doesn't have to be static. Also, name and value could be const. Our preferred style is "TYPE *val", not the "TYPE* val" used here and elsewhere in the patch. Neither are worth a v5, IMO.

Quoted 26 lines
src/openvpn/win32.c +10 -0
+
+    for (size_t i = 0; i < SIZE(ossl_env); ++i)
+    {
+        size_t size = 0;
+
+        _wgetenv_s(&size, NULL, 0, ossl_env[i].name);
+        if (size == 0)
+        {
+            WCHAR val[MAX_PATH] = {0};
+            openvpn_swprintf(val, _countof(val), L"%ls\\ssl\\%ls",

install_path, ossl_env[i].value);

src/openvpn/win32.c +5 -0
+            _wputenv_s(ossl_env[i].name, val);
+        }
+    }
+}
+
 #endif /* ifdef _WIN32 */
src/openvpn/win32.h +0 -0
index 5d3371a0..4a992d91 100644
@@ -327,7 +327,13 @@ bool send_msg_iservice(HANDLE pipe, const void *data,
 size_t size,
 int
 openvpn_execve(const struct argv *a, const struct env_set *es, const
 unsigned int flags);

src/openvpn/win32.h bool send_msg_iservice(HANDLE pipe, const void *data, · line 339

Quoted 10 lines
src/openvpn/win32.h +7 -1
 
-bool impersonate_as_system();
+/*
+ * openvpn_swprintf() is currently only used by Windows code paths
+ * and when enabled for all platforms it will currently break older
+ * OpenBSD versions lacking vswprintf(3) support in their libc.
+ */
+bool
+openvpn_swprintf(wchar_t* const str, const size_t size, const wchar_t*
 const format, ...);

Now there is a duplicate declaration in buffer.h which could be removed. Multiple declarations is not an error, so let it be...

Quoted 3 lines

#endif /* ifndef OPENVPN_WIN32_H */
#endif /* ifdef _WIN32 */

#9 lore raw
In reply to Lev Stipakov

Your patch has been applied to the master and release/2.5 branch (I consider this a bugfix since the "do not load config!" CVE patch unintendedly broke functionality for people)

As instructed I have changed "* " to " *" according to style :-), and removed the double declaration in buffer.h - the latter is something on the edge of "can I do that at commit time?" but in this case it's "the very same declaration", "tun.c and win32.c do include win32.h", so easy enough.

I did not change the "static" bit.

I have not tested this "for real", just did a test compile of master and release/2.5 on MinGW. But the code change looks good, and it does not break compilation, even with my changes :-)

Added the trac reference to #1296 (thanks Lev for digging it up).

commit 23e6aaef149bd31a7e80af28ee1e3658d2810d4f (master) commit f911b3f69b0a8296918a06d02eb5144bb4cd8a06 (release/2.5)

Author: Lev Stipakov
Date:   Fri Nov 19 03:55:48 2021 +0200
     Load OpenSSL config on Windows from trusted location

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Selva Nair <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg23248.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#10 lore raw

Do we need this fix in openvpn-gui? It only (?) uses openssl to change private key password, could this functionality be affected by config?

ke 24. marrask. 2021 klo 12.06 Gert Doering ([email protected]...) kirjoitti:

Quoted 36 lines

Your patch has been applied to the master and release/2.5 branch (I consider this a bugfix since the "do not load config!" CVE patch unintendedly broke functionality for people)

As instructed I have changed "* " to " *" according to style :-), and removed the double declaration in buffer.h - the latter is something on the edge of "can I do that at commit time?" but in this case it's "the very same declaration", "tun.c and win32.c do include win32.h", so easy enough.

I did not change the "static" bit.

I have not tested this "for real", just did a test compile of master and release/2.5 on MinGW. But the code change looks good, and it does not break compilation, even with my changes :-)

Added the trac reference to #1296 (thanks Lev for digging it up).

commit 23e6aaef149bd31a7e80af28ee1e3658d2810d4f (master) commit f911b3f69b0a8296918a06d02eb5144bb4cd8a06 (release/2.5)

Author: Lev Stipakov
Date:   Fri Nov 19 03:55:48 2021 +0200
     Load OpenSSL config on Windows from trusted location

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Selva Nair <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg23248.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Quoted 8 lines

Gert Doering

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

--
-Lev

#11 lore raw

Hi,

Quoted 3 lines · Lev Stipakov

Do we need this fix in openvpn-gui? It only (?) uses openssl to change private key password, could this functionality be affected by config?

I do not know.. We do not call any functions that would lead to a config loading, so probably not required. Automatic crypto initialization does not load the config, nor does an explicit call to OPENSSL_init_crypto() unless instructed to. OPENSSL_init_ssl() loads the config unless explicitly disabled, but we do not use it in the GUI.

However, to be on the safe side, we could set these env vars if not already set by the user.

Selva

#12 lore raw
In reply to Gert Doering

Hi

Quoted 4 lines · Gert Doering

Your patch has been applied to the master and release/2.5 branch (I consider this a bugfix since the "do not load config!" CVE patch unintendedly broke functionality for people)

What would be a good location in the man page where we can document this. These are not env vars we natively support so putting it under a section named "ENVIRONMENT VARIABLES" does not seem right. Also we already have a section with that name which refer to env vars we export to scripts.

Selva

#13 lore raw
In reply to Lev Stipakov

I don't have a setup to properly test it, like actually loading the config - I only checked that the openvpn.exe attempted to access openssl.cnf at the correct location.

If someone wants to test - binary artifacts could be found here:
https://github.com/lstipakov/openvpn/actions/runs/1496114596

I could also do testing if someone educates me how :)

ti 23. marrask. 2021 klo 20.27 Lev Stipakov ([email protected]...) kirjoitti:

Quoted 265 lines

From: Lev Stipakov <[email protected]...>

Commits

- 92535b6 ("contrib/vcpkg-ports: add openssl port with --no-autoload-config option set (CVE-2121-3606)")
- 447cfb4 ("crypto_openssl.c: disable explicit initialization on Windows (CVE-2121-3606)")

disabled OpenSSL config loading functionality, which could be exploited by loading config from untrusted locations.

However, ability to load config might be useful for some users.

This brings config loading back and sets OpenSSL enviroment variables

OPENSSL_CONF, OPENSSL_ENGINES, OPENSSL_MODULES

which are used to load config, engines and modules, to a trusted location. The location is constructed based on installation path, read from registry on startup. If installation path cannot be read, Windows\System32 is used as a fallback.

While on it, remove unused "bool impersonate_as_system();" declaration.

---

v3:
- do not assume that installation path ends with directory separator
- set enviroment variables only if they're not already set
- bring back explicit initialization on Windows (might be needed on some cases)
- slightly revamp commit message

v2:
- add missing "static" modifier to set_openssl_env_vars() declaration
spotted by gcc

 .../vcpkg-triplets/arm64-windows-ovpn.cmake   |  2 -
 contrib/vcpkg-triplets/x64-windows-ovpn.cmake |  2 -
 contrib/vcpkg-triplets/x86-windows-ovpn.cmake |  2 -
 src/openvpn/buffer.c                          | 23 -----
 src/openvpn/crypto_openssl.c                  |  2 -
 src/openvpn/win32.c                           | 92 +++++++++++++++++++
 src/openvpn/win32.h                           |  8 +-
 7 files changed, 99 insertions(+), 32 deletions(-)
contrib/vcpkg-triplets/arm64-windows-ovpn.cmake +0 -2
index 89f6a279..dd3c6c0a 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x64-windows-ovpn.cmake +0 -2
index d860eed6..7036ed2d 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
contrib/vcpkg-triplets/x86-windows-ovpn.cmake +0 -2
index c1ea6ef3..7d3bf340 100644
@@ -5,5 +5,3 @@ set(VCPKG_LIBRARY_LINKAGE dynamic)
 if(PORT STREQUAL "lz4")
     set(VCPKG_LIBRARY_LINKAGE static)
 endif()
-
-set(OPENSSL_NO_AUTOLOAD_CONFIG ON)
src/openvpn/buffer.c +0 -23
index c82d3d4d..54e758af 100644
@@ -310,29 +310,6 @@ openvpn_snprintf(char *str, size_t size, const char *format, ...)
     return (len >= 0 && len < size);
 }
 
-/*
- * openvpn_swprintf() is currently only used by Windows code paths
- * and when enabled for all platforms it will currently break older
- * OpenBSD versions lacking vswprintf(3) support in their libc.
- */
-
-#ifdef _WIN32
-bool
-openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...)
-{
-    va_list arglist;
-    int len = -1;
-    if (size > 0)
-    {
-        va_start(arglist, format);
-        len = vswprintf(str, size, format, arglist);
-        va_end(arglist);
-        str[size - 1] = L'\0';
-    }
-    return (len >= 0 && len < size);
-}
-#endif
-
 /*
  * write a string to the end of a buffer that was
  * truncated by buf_printf
src/openvpn/crypto_openssl.c +0 -2
index c9dc9d0a..ef520928 100644
@@ -154,13 +154,11 @@ crypto_init_lib_engine(const char *engine_name)
 void
 crypto_init_lib(void)
 {
-#ifndef _WIN32
 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
 #else
     OPENSSL_config(NULL);
 #endif
-#endif /* _WIN32 */
     /*
      * If you build the OpenSSL library and OpenVPN with
      * CRYPTO_MDEBUG, you will get a listing of OpenSSL
src/openvpn/win32.c +92 -0
index 6cff17b2..ac7775e4 100644
@@ -101,6 +101,12 @@ struct semaphore netcmd_semaphore; /* GLOBAL */
  */
 static char *win_sys_path = NULL; /* GLOBAL */
 
+/**
+ * Set OpenSSL environment variables to a safe directory
+ */
+static void
+set_openssl_env_vars();
+
 void
 init_win32(void)
 {
@@ -110,6 +116,8 @@ init_win32(void)
     }
     window_title_clear(&window_title);
     win32_signal_clear(&win32_signal);
+
+    set_openssl_env_vars();
 }
 
 void
@@ -1509,4 +1517,88 @@ send_msg_iservice(HANDLE pipe, const void *data, size_t size,
     return ret;
 }
 
+bool
+openvpn_swprintf(wchar_t* const str, const size_t size, const wchar_t* const format, ...)
+{
+    va_list arglist;
+    int len = -1;
+    if (size > 0)
+    {
+        va_start(arglist, format);
+        len = vswprintf(str, size, format, arglist);
+        va_end(arglist);
+        str[size - 1] = L'\0';
+    }
+    return (len >= 0 && len < size);
+}
+
+static BOOL
+get_install_path(WCHAR *path, DWORD size)
+{
+    WCHAR reg_path[256];
+    HKEY key;
+    BOOL res = FALSE;
+    openvpn_swprintf(reg_path, _countof(reg_path), L"SOFTWARE\\" PACKAGE_NAME);
+
+    LONG status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, reg_path, 0, KEY_READ, &key);
+    if (status != ERROR_SUCCESS)
+    {
+        return res;
+    }
+
+    /* The default value of REG_KEY is the install path */
+    status = RegGetValueW(key, NULL, NULL, RRF_RT_REG_SZ, NULL, (LPBYTE)path, &size);
+    res = status == ERROR_SUCCESS;
+
+    RegCloseKey(key);
+
+    return res;
+}
+
+static void
+set_openssl_env_vars()
+{
+    const WCHAR* ssl_fallback_dir = L"C:\\Windows\\System32";
+
+    WCHAR install_path[MAX_PATH] = { 0 };
+    if (!get_install_path(install_path, _countof(install_path)))
+    {
+        /* if we cannot find installation path from the registry,
+         * use Windows directory as a fallback
+         */
+        openvpn_swprintf(install_path, _countof(install_path), L"%ls", ssl_fallback_dir);
+    }
+
+    if ((install_path[wcslen(install_path) - 1]) == L'\\')
+    {
+        install_path[wcslen(install_path) - 1] = L'\0';
+    }
+
+    WCHAR openssl_cnf[MAX_PATH] = {0};
+    WCHAR openssl_engines[MAX_PATH] = {0};
+    WCHAR openssl_modules[MAX_PATH] = {0};
+
+    openvpn_swprintf(openssl_cnf, _countof(install_path),
+        L"OPENSSL_CONF=%ls\\ssl\\openssl.cnf", install_path);
+    openvpn_swprintf(openssl_engines, _countof(openssl_engines),
+        L"OPENSSL_ENGINES=%ls\\ssl\\engines", install_path);
+    openvpn_swprintf(openssl_modules, _countof(openssl_modules),
+        L"OPENSSL_MODULES=%ls\\ssl\\modules", install_path);
+
+    if (_wgetenv(L"OPENSSL_CONF") == NULL)
+    {
+        _wputenv(openssl_cnf);
+    }
+
+    if (_wgetenv(L"OPENSSL_ENGINES") == NULL)
+    {
+        _wputenv(openssl_engines);
+    }
+
+    if (_wgetenv(L"OPENSSL_MODULES") == NULL)
+    {
+        _wputenv(openssl_modules);
+    }
+}
+
 #endif /* ifdef _WIN32 */
src/openvpn/win32.h +7 -2
index 5d3371a0..4a992d91 100644
@@ -327,7 +327,13 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size,
 int
 openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
 
-bool impersonate_as_system();
+/*
+ * openvpn_swprintf() is currently only used by Windows code paths
+ * and when enabled for all platforms it will currently break older
+ * OpenBSD versions lacking vswprintf(3) support in their libc.
+ */
+bool
+openvpn_swprintf(wchar_t* const str, const size_t size, const wchar_t* const format, ...);
 
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */
--

2.23.0.windows.1

Signature · 1 line
-Lev
#14 lore raw

Hi,

Quoted 11 lines · Lev Stipakov

I don't have a setup to properly test it, like actually loading the config - I only checked that the openvpn.exe attempted to access openssl.cnf at the correct location.

If someone wants to test - binary artifacts could be found here:
https://github.com/lstipakov/openvpn/actions/runs/1496114596

I could also do testing if someone educates me how :)

Try using an openssl.cnf like the one below which restricts signature algorithms to a some non-PSS schemes. Change that line to restrict them further or comment out to use defaults: not including PSS will force non-PSS signature with TLS 1.2 even with OpenSSL 1.1.1 server. And will break TLS 1.3 negotiation. Removing ECC signatures and using an EC key certificate will break the connection etc..

#
# OpenSSL configuration file to restrict siglags during handshake #
openssl_conf = default_conf

[default_conf]
ssl_conf = ssl_sect

[ssl_sect]
system_default = system_default_sect

[system_default_sect]
#MinProtocol = TLSv1.2
#CipherString = [email protected]...=0
SignatureAlgorithms = RSA+SHA256:RSA+SHA384:RSA+SHA512:ECDSA+SHA256:ECDSA+SHA384:ECDSA+SHA512:RSA+SHA1:ECDSA+SHA1

# possible values
# PKCS1:  rsa_pkcs1_sha256:rsa_pkcs1_sha384:rsa_pkcs1_sha512
# ECDSA:
ecdsa_secp256r1_sha256:ecdsa_secp384r1_sha384:ecdsa_secp521r1_sha512
# PSS with rsa encryption public key
rsa_pss_rsae_sha256:rsa_pss_rsae_sha384:rsa_pss_rsae_sha512
# EdDSA :ed25519:ed448
# PSS with PSS public key:
rsa_pss_pss_sha256:rsa_pss_pss_sha384:rsa_pss_pss_sha512
# Legacy rsa_pkcs1_sha1:ecdsa_sha1
#15 lore raw

Thanks,

I tried this one and client wasn't able to connect:

OpenSSL: error:14201076:SSL routines:tls_choose_sigalg:no suitable
signature algorithm

So it looks like config loading works.

The binaries for V4 could be found here:
https://github.com/lstipakov/openvpn/actions/runs/1496339867

ti 23. marrask. 2021 klo 20.58 Selva Nair ([email protected]...) kirjoitti:

Quoted 41 lines

Hi,

Quoted 11 lines · Lev Stipakov

I don't have a setup to properly test it, like actually loading the config - I only checked that the openvpn.exe attempted to access openssl.cnf at the correct location.

If someone wants to test - binary artifacts could be found here:
https://github.com/lstipakov/openvpn/actions/runs/1496114596

I could also do testing if someone educates me how :)

Try using an openssl.cnf like the one below which restricts signature algorithms to a some non-PSS schemes. Change that line to restrict them further or comment out to use defaults: not including PSS will force non-PSS signature with TLS 1.2 even with OpenSSL 1.1.1 server. And will break TLS 1.3 negotiation. Removing ECC signatures and using an EC key certificate will break the connection etc..

#
# OpenSSL configuration file to restrict siglags during handshake #
openssl_conf = default_conf

[default_conf]
ssl_conf = ssl_sect

[ssl_sect]
system_default = system_default_sect

[system_default_sect]
#MinProtocol = TLSv1.2
#CipherString = [email protected]...=0
SignatureAlgorithms = RSA+SHA256:RSA+SHA384:RSA+SHA512:ECDSA+SHA256:ECDSA+SHA384:ECDSA+SHA512:RSA+SHA1:ECDSA+SHA1

# possible values
# PKCS1:  rsa_pkcs1_sha256:rsa_pkcs1_sha384:rsa_pkcs1_sha512
# ECDSA: ecdsa_secp256r1_sha256:ecdsa_secp384r1_sha384:ecdsa_secp521r1_sha512
# PSS with rsa encryption public key rsa_pss_rsae_sha256:rsa_pss_rsae_sha384:rsa_pss_rsae_sha512
# EdDSA :ed25519:ed448
# PSS with PSS public key: rsa_pss_pss_sha256:rsa_pss_pss_sha384:rsa_pss_pss_sha512
# Legacy rsa_pkcs1_sha1:ecdsa_sha1
Signature · 1 line
-Lev