From b97e4df0caa165874f84be9458b3b424cf3b6c4b Mon Sep 17 00:00:00 2001 From: wfurt Date: Wed, 24 Nov 2021 14:02:09 -0800 Subject: [PATCH] make sure OpenSSL is initialized before Tls13Supported code runs --- .../Interop.Initialization.cs | 33 +++++++++++++++---- .../Interop.Ssl.cs | 10 ++++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.Initialization.cs b/src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.Initialization.cs index 39a0d67f2021cb..d54b50c66ce945 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.Initialization.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.Initialization.cs @@ -13,23 +13,42 @@ static Ssl() { SslInitializer.Initialize(); } + + // access for static properties that may be called before static constructor. + internal static void EsureInitialized() + { + SslInitializer.Initialize(); + } } internal static class SslInitializer { #if !SYSNETSECURITY_NO_OPENSSL + private static readonly object _initLock = new object(); + private static bool _initialized; + +#pragma warning disable CA1810 static SslInitializer() { - CryptoInitializer.Initialize(); - - //Call ssl specific initializer - Ssl.EnsureLibSslInitialized(); - if (Interop.Crypto.ErrPeekLastError() != 0) + lock (_initLock) { - // It is going to be wrapped in a type load exception but will have the error information - throw Interop.Crypto.CreateOpenSslCryptographicException(); + if (!_initialized) + { + CryptoInitializer.Initialize(); + + //Call ssl specific initializer + Ssl.EnsureLibSslInitialized(); + if (Interop.Crypto.ErrPeekLastError() != 0) + { + // It is going to be wrapped in a type load exception but will have the error information + throw Interop.Crypto.CreateOpenSslCryptographicException(); + } + + _initialized = true; + } } } +#pragma warning restore CA1810 #endif internal static void Initialize() diff --git a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Ssl.cs b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Ssl.cs index 1db80064b2c80c..d5c746045161f3 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Ssl.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Ssl.cs @@ -191,8 +191,14 @@ internal static byte[] ConvertAlpnProtocolListToByteArray(List