|
|
105ad9 |
From dfa0c242cd9b329554971cf80c094e8f58d756e3 Mon Sep 17 00:00:00 2001
|
|
|
105ad9 |
From: Jeremy Barton <jbarton@microsoft.com>
|
|
|
105ad9 |
Date: Wed, 26 Sep 2018 15:35:35 -0700
|
|
|
105ad9 |
Subject: [PATCH 6/7] Check for the specific in-use version of OpenSSL when
|
|
|
105ad9 |
working with libcurl
|
|
|
105ad9 |
|
|
|
105ad9 |
Rather than check a generic 1.0/1.1, test for the specific library version that
|
|
|
105ad9 |
the crypto shim has loaded. This makes things work when both libcurl
|
|
|
105ad9 |
and the crypto shim are using OpenSSL 1.1 and also prevents a state where two
|
|
|
105ad9 |
different copies of the library (at different patch versions) are utilized.
|
|
|
105ad9 |
---
|
|
|
105ad9 |
.../Interop.Initialization.cs | 8 +--
|
|
|
105ad9 |
.../Interop.VersionInfo.cs | 44 +++++++++++-
|
|
|
105ad9 |
.../Interop.OpenSslVersion.cs | 2 +-
|
|
|
105ad9 |
.../src/System.Net.Http.csproj | 3 +
|
|
|
105ad9 |
.../CurlHandler.SslProvider.Linux.cs | 8 +--
|
|
|
105ad9 |
.../FunctionalTests/HttpClientEKUTest.cs | 2 +-
|
|
|
105ad9 |
...ttpClientHandlerTest.ClientCertificates.cs | 14 +---
|
|
|
105ad9 |
...ientHandlerTest.ServerCertificates.Unix.cs | 9 +--
|
|
|
105ad9 |
...HttpClientHandlerTest.SslProtocols.Unix.cs | 3 +-
|
|
|
105ad9 |
.../System.Net.Http.Functional.Tests.csproj | 3 +-
|
|
|
105ad9 |
.../tests/FunctionalTests/TestHelper.cs | 69 +++++++++++++++++++
|
|
|
105ad9 |
11 files changed, 130 insertions(+), 35 deletions(-)
|
|
|
105ad9 |
|
|
|
105ad9 |
diff --git a/src/Common/src/Interop/Unix/System.Net.Http.Native/Interop.Initialization.cs b/src/Common/src/Interop/Unix/System.Net.Http.Native/Interop.Initialization.cs
|
|
|
105ad9 |
index eef56ec0b3..d6bcc8df02 100644
|
|
|
105ad9 |
--- a/src/Common/src/Interop/Unix/System.Net.Http.Native/Interop.Initialization.cs
|
|
|
105ad9 |
+++ b/src/Common/src/Interop/Unix/System.Net.Http.Native/Interop.Initialization.cs
|
|
|
105ad9 |
@@ -26,11 +26,11 @@ internal static partial class Interop
|
|
|
105ad9 |
#if !SYSNETHTTP_NO_OPENSSL
|
|
|
105ad9 |
string opensslVersion = Interop.Http.GetSslVersionDescription();
|
|
|
105ad9 |
if (string.IsNullOrEmpty(opensslVersion) ||
|
|
|
105ad9 |
- opensslVersion.IndexOf(Interop.Http.OpenSsl10Description, StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
105ad9 |
+ opensslVersion.IndexOf(Interop.Http.OpenSslDescriptionPrefix, StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
105ad9 |
{
|
|
|
105ad9 |
- // CURL uses OpenSSL which we must initialize first to guarantee thread-safety
|
|
|
105ad9 |
- // Only initialize for OpenSSL/1.0, any newer versions may have mismatched
|
|
|
105ad9 |
- // pointers, resulting in segfaults.
|
|
|
105ad9 |
+ // CURL uses OpenSSL which we must initialize first to guarantee thread-safety.
|
|
|
105ad9 |
+ // We'll wake up whatever OpenSSL we're going to run against, but might later determine that
|
|
|
105ad9 |
+ // they aren't compatible.
|
|
|
105ad9 |
CryptoInitializer.Initialize();
|
|
|
105ad9 |
}
|
|
|
105ad9 |
#endif
|
|
|
105ad9 |
diff --git a/src/Common/src/Interop/Unix/System.Net.Http.Native/Interop.VersionInfo.cs b/src/Common/src/Interop/Unix/System.Net.Http.Native/Interop.VersionInfo.cs
|
|
|
105ad9 |
index 1899fd0af3..8175159b6f 100644
|
|
|
105ad9 |
--- a/src/Common/src/Interop/Unix/System.Net.Http.Native/Interop.VersionInfo.cs
|
|
|
105ad9 |
+++ b/src/Common/src/Interop/Unix/System.Net.Http.Native/Interop.VersionInfo.cs
|
|
|
105ad9 |
@@ -3,6 +3,7 @@
|
|
|
105ad9 |
// See the LICENSE file in the project root for more information.
|
|
|
105ad9 |
|
|
|
105ad9 |
using System;
|
|
|
105ad9 |
+using System.Diagnostics;
|
|
|
105ad9 |
using System.Runtime.InteropServices;
|
|
|
105ad9 |
|
|
|
105ad9 |
internal static partial class Interop
|
|
|
105ad9 |
@@ -47,8 +48,49 @@ internal static partial class Interop
|
|
|
105ad9 |
[DllImport(Libraries.HttpNative, EntryPoint = "HttpNative_GetSslVersionDescription")]
|
|
|
105ad9 |
internal static extern string GetSslVersionDescription();
|
|
|
105ad9 |
|
|
|
105ad9 |
- internal const string OpenSsl10Description = "openssl/1.0";
|
|
|
105ad9 |
+ internal const string OpenSslDescriptionPrefix = "OpenSSL/";
|
|
|
105ad9 |
internal const string SecureTransportDescription = "SecureTransport";
|
|
|
105ad9 |
internal const string LibreSslDescription = "LibreSSL";
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+#if !SYSNETHTTP_NO_OPENSSL
|
|
|
105ad9 |
+ private static readonly Lazy<string> s_requiredOpenSslDescription =
|
|
|
105ad9 |
+ new Lazy<string>(() => DetermineRequiredOpenSslDescription());
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ private static readonly Lazy<bool> s_hasMatchingOpenSsl =
|
|
|
105ad9 |
+ new Lazy<bool>(() => RequiredOpenSslDescription == GetSslVersionDescription());
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ internal static string RequiredOpenSslDescription => s_requiredOpenSslDescription.Value;
|
|
|
105ad9 |
+ internal static bool HasMatchingOpenSslVersion => s_hasMatchingOpenSsl.Value;
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ private static string DetermineRequiredOpenSslDescription()
|
|
|
105ad9 |
+ {
|
|
|
105ad9 |
+ string versionDescription = Interop.OpenSsl.OpenSslVersionDescription();
|
|
|
105ad9 |
+ var version = versionDescription.AsSpan();
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ // OpenSSL version description looks like this:
|
|
|
105ad9 |
+ //
|
|
|
105ad9 |
+ // OpenSSL 1.1.1 FIPS 11 Sep 2018
|
|
|
105ad9 |
+ //
|
|
|
105ad9 |
+ // libcurl's OpenSSL vtls backend ignores status in the version string.
|
|
|
105ad9 |
+ // Major, minor, and fix are encoded (by libcurl) as unpadded hex
|
|
|
105ad9 |
+ // (0 => "0", 15 => "f", 16 => "10").
|
|
|
105ad9 |
+ //
|
|
|
105ad9 |
+ // Patch is encoded as in the way OpenSSL would do it.
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ string prefix = "OpenSSL ";
|
|
|
105ad9 |
+ if (version.StartsWith(prefix))
|
|
|
105ad9 |
+ {
|
|
|
105ad9 |
+ version = version.Slice(prefix.Length).Trim();
|
|
|
105ad9 |
+ }
|
|
|
105ad9 |
+ int end = version.IndexOf(" ");
|
|
|
105ad9 |
+ if (end != -1)
|
|
|
105ad9 |
+ {
|
|
|
105ad9 |
+ version = version.Slice(0, end);
|
|
|
105ad9 |
+ }
|
|
|
105ad9 |
+ version = version.Trim();
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ return $"{OpenSslDescriptionPrefix}{version.ToString()}";
|
|
|
105ad9 |
+ }
|
|
|
105ad9 |
+#endif
|
|
|
105ad9 |
}
|
|
|
105ad9 |
}
|
|
|
105ad9 |
diff --git a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSslVersion.cs b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSslVersion.cs
|
|
|
105ad9 |
index 70805706ef..13c2339ce6 100644
|
|
|
105ad9 |
--- a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSslVersion.cs
|
|
|
105ad9 |
+++ b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSslVersion.cs
|
|
|
105ad9 |
@@ -12,7 +12,7 @@ internal static partial class Interop
|
|
|
105ad9 |
private static Version s_opensslVersion;
|
|
|
105ad9 |
|
|
|
105ad9 |
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_SSLEayVersion")]
|
|
|
105ad9 |
- private static extern string OpenSslVersionDescription();
|
|
|
105ad9 |
+ internal static extern string OpenSslVersionDescription();
|
|
|
105ad9 |
|
|
|
105ad9 |
internal static Version OpenSslVersion
|
|
|
105ad9 |
{
|
|
|
105ad9 |
diff --git a/src/System.Net.Http/src/System.Net.Http.csproj b/src/System.Net.Http/src/System.Net.Http.csproj
|
|
|
105ad9 |
index 66e5b8d5f8..34b4d22e15 100644
|
|
|
105ad9 |
--- a/src/System.Net.Http/src/System.Net.Http.csproj
|
|
|
105ad9 |
+++ b/src/System.Net.Http/src/System.Net.Http.csproj
|
|
|
105ad9 |
@@ -500,6 +500,9 @@
|
|
|
105ad9 |
<Compile Include="$(CommonPath)\Interop\Unix\System.Security.Cryptography.Native\Interop.Crypto.cs">
|
|
|
105ad9 |
<Link>Common\Interop\Unix\System.Security.Cryptography.Native\Interop.Crypto.cs</Link>
|
|
|
105ad9 |
</Compile>
|
|
|
105ad9 |
+ <Compile Include="$(CommonPath)\Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslVersion.cs">
|
|
|
105ad9 |
+ <Link>Common\Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslVersion.cs</Link>
|
|
|
105ad9 |
+ </Compile>
|
|
|
105ad9 |
<Compile Include="$(CommonPath)\Interop\Unix\System.Security.Cryptography.Native\Interop.Ssl.cs">
|
|
|
105ad9 |
<Link>Common\Interop\Unix\System.Security.Cryptography.Native\Interop.Ssl.cs</Link>
|
|
|
105ad9 |
</Compile>
|
|
|
105ad9 |
diff --git a/src/System.Net.Http/src/System/Net/Http/CurlHandler/CurlHandler.SslProvider.Linux.cs b/src/System.Net.Http/src/System/Net/Http/CurlHandler/CurlHandler.SslProvider.Linux.cs
|
|
|
105ad9 |
index 55e583e137..2fdcde686d 100644
|
|
|
105ad9 |
--- a/src/System.Net.Http/src/System/Net/Http/CurlHandler/CurlHandler.SslProvider.Linux.cs
|
|
|
105ad9 |
+++ b/src/System.Net.Http/src/System/Net/Http/CurlHandler/CurlHandler.SslProvider.Linux.cs
|
|
|
105ad9 |
@@ -55,7 +55,7 @@ namespace System.Net.Http
|
|
|
105ad9 |
|
|
|
105ad9 |
// Configure the options. Our best support is when targeting OpenSSL/1.0. For other backends,
|
|
|
105ad9 |
// we fall back to a minimal amount of support, and may throw a PNSE based on the options requested.
|
|
|
105ad9 |
- if (CurlSslVersionDescription.IndexOf(Interop.Http.OpenSsl10Description, StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
105ad9 |
+ if (Interop.Http.HasMatchingOpenSslVersion)
|
|
|
105ad9 |
{
|
|
|
105ad9 |
// Register the callback with libcurl. We need to register even if there's no user-provided
|
|
|
105ad9 |
// server callback and even if there are no client certificates, because we support verifying
|
|
|
105ad9 |
@@ -169,12 +169,12 @@ namespace System.Net.Http
|
|
|
105ad9 |
{
|
|
|
105ad9 |
if (certProvider != null)
|
|
|
105ad9 |
{
|
|
|
105ad9 |
- throw new PlatformNotSupportedException(SR.Format(SR.net_http_libcurl_clientcerts_notsupported_sslbackend, CurlVersionDescription, CurlSslVersionDescription, Interop.Http.OpenSsl10Description));
|
|
|
105ad9 |
+ throw new PlatformNotSupportedException(SR.Format(SR.net_http_libcurl_clientcerts_notsupported_sslbackend, CurlVersionDescription, CurlSslVersionDescription, Interop.Http.RequiredOpenSslDescription));
|
|
|
105ad9 |
}
|
|
|
105ad9 |
|
|
|
105ad9 |
if (easy._handler.CheckCertificateRevocationList)
|
|
|
105ad9 |
{
|
|
|
105ad9 |
- throw new PlatformNotSupportedException(SR.Format(SR.net_http_libcurl_revocation_notsupported_sslbackend, CurlVersionDescription, CurlSslVersionDescription, Interop.Http.OpenSsl10Description));
|
|
|
105ad9 |
+ throw new PlatformNotSupportedException(SR.Format(SR.net_http_libcurl_revocation_notsupported_sslbackend, CurlVersionDescription, CurlSslVersionDescription, Interop.Http.RequiredOpenSslDescription));
|
|
|
105ad9 |
}
|
|
|
105ad9 |
|
|
|
105ad9 |
if (easy._handler.ServerCertificateCustomValidationCallback != null)
|
|
|
105ad9 |
@@ -187,7 +187,7 @@ namespace System.Net.Http
|
|
|
105ad9 |
}
|
|
|
105ad9 |
else
|
|
|
105ad9 |
{
|
|
|
105ad9 |
- throw new PlatformNotSupportedException(SR.Format(SR.net_http_libcurl_callback_notsupported_sslbackend, CurlVersionDescription, CurlSslVersionDescription, Interop.Http.OpenSsl10Description));
|
|
|
105ad9 |
+ throw new PlatformNotSupportedException(SR.Format(SR.net_http_libcurl_callback_notsupported_sslbackend, CurlVersionDescription, CurlSslVersionDescription, Interop.Http.RequiredOpenSslDescription));
|
|
|
105ad9 |
}
|
|
|
105ad9 |
}
|
|
|
105ad9 |
else
|
|
|
105ad9 |
diff --git a/src/System.Net.Http/tests/FunctionalTests/HttpClientEKUTest.cs b/src/System.Net.Http/tests/FunctionalTests/HttpClientEKUTest.cs
|
|
|
105ad9 |
index c6badc770e..9ac90390e3 100644
|
|
|
105ad9 |
--- a/src/System.Net.Http/tests/FunctionalTests/HttpClientEKUTest.cs
|
|
|
105ad9 |
+++ b/src/System.Net.Http/tests/FunctionalTests/HttpClientEKUTest.cs
|
|
|
105ad9 |
@@ -22,7 +22,7 @@ namespace System.Net.Http.Functional.Tests
|
|
|
105ad9 |
#if TargetsWindows
|
|
|
105ad9 |
true;
|
|
|
105ad9 |
#else
|
|
|
105ad9 |
- Interop.Http.GetSslVersionDescription()?.StartsWith(Interop.Http.OpenSsl10Description, StringComparison.OrdinalIgnoreCase) ?? false;
|
|
|
105ad9 |
+ TestHelper.NativeHandlerSupportsSslConfiguration();
|
|
|
105ad9 |
#endif
|
|
|
105ad9 |
|
|
|
105ad9 |
private static bool CanTestCertificates =>
|
|
|
105ad9 |
diff --git a/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ClientCertificates.cs b/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ClientCertificates.cs
|
|
|
105ad9 |
index 78d0fdb09f..217726db64 100644
|
|
|
105ad9 |
--- a/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ClientCertificates.cs
|
|
|
105ad9 |
+++ b/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ClientCertificates.cs
|
|
|
105ad9 |
@@ -319,19 +319,7 @@ namespace System.Net.Http.Functional.Tests
|
|
|
105ad9 |
#if TargetsWindows
|
|
|
105ad9 |
return true;
|
|
|
105ad9 |
#else
|
|
|
105ad9 |
- if (UseSocketsHttpHandler)
|
|
|
105ad9 |
- {
|
|
|
105ad9 |
- return true;
|
|
|
105ad9 |
- }
|
|
|
105ad9 |
-
|
|
|
105ad9 |
- if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
|
|
105ad9 |
- {
|
|
|
105ad9 |
- return false;
|
|
|
105ad9 |
- }
|
|
|
105ad9 |
-
|
|
|
105ad9 |
- // For other Unix-based systems it's true if (and only if) the openssl backend
|
|
|
105ad9 |
- // is used with libcurl.
|
|
|
105ad9 |
- return (Interop.Http.GetSslVersionDescription()?.StartsWith(Interop.Http.OpenSsl10Description, StringComparison.OrdinalIgnoreCase) ?? false);
|
|
|
105ad9 |
+ return TestHelper.NativeHandlerSupportsSslConfiguration();
|
|
|
105ad9 |
#endif
|
|
|
105ad9 |
}
|
|
|
105ad9 |
}
|
|
|
105ad9 |
diff --git a/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ServerCertificates.Unix.cs b/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ServerCertificates.Unix.cs
|
|
|
105ad9 |
index d19a63f598..eb38f93615 100644
|
|
|
105ad9 |
--- a/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ServerCertificates.Unix.cs
|
|
|
105ad9 |
+++ b/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ServerCertificates.Unix.cs
|
|
|
105ad9 |
@@ -58,14 +58,7 @@ namespace System.Net.Http.Functional.Tests
|
|
|
105ad9 |
return true;
|
|
|
105ad9 |
}
|
|
|
105ad9 |
|
|
|
105ad9 |
- if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
|
|
105ad9 |
- {
|
|
|
105ad9 |
- return false;
|
|
|
105ad9 |
- }
|
|
|
105ad9 |
-
|
|
|
105ad9 |
- // For other Unix-based systems it's true if (and only if) the openssl backend
|
|
|
105ad9 |
- // is used with libcurl.
|
|
|
105ad9 |
- return (Interop.Http.GetSslVersionDescription()?.StartsWith(Interop.Http.OpenSsl10Description, StringComparison.OrdinalIgnoreCase) ?? false);
|
|
|
105ad9 |
+ return TestHelper.NativeHandlerSupportsSslConfiguration();
|
|
|
105ad9 |
}
|
|
|
105ad9 |
}
|
|
|
105ad9 |
|
|
|
105ad9 |
diff --git a/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.SslProtocols.Unix.cs b/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.SslProtocols.Unix.cs
|
|
|
105ad9 |
index 615f2cb4fa..e7631e3940 100644
|
|
|
105ad9 |
--- a/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.SslProtocols.Unix.cs
|
|
|
105ad9 |
+++ b/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.SslProtocols.Unix.cs
|
|
|
105ad9 |
@@ -17,7 +17,6 @@ namespace System.Net.Http.Functional.Tests
|
|
|
105ad9 |
public abstract partial class HttpClientHandler_SslProtocols_Test
|
|
|
105ad9 |
{
|
|
|
105ad9 |
private bool BackendSupportsSslConfiguration =>
|
|
|
105ad9 |
- UseSocketsHttpHandler ||
|
|
|
105ad9 |
- (Interop.Http.GetSslVersionDescription()?.StartsWith(Interop.Http.OpenSsl10Description, StringComparison.OrdinalIgnoreCase) ?? false);
|
|
|
105ad9 |
+ UseSocketsHttpHandler || TestHelper.NativeHandlerSupportsSslConfiguration();
|
|
|
105ad9 |
}
|
|
|
105ad9 |
}
|
|
|
105ad9 |
diff --git a/src/System.Net.Http/tests/FunctionalTests/System.Net.Http.Functional.Tests.csproj b/src/System.Net.Http/tests/FunctionalTests/System.Net.Http.Functional.Tests.csproj
|
|
|
105ad9 |
index 68c87c2b6e..b3b9d2437d 100644
|
|
|
105ad9 |
--- a/src/System.Net.Http/tests/FunctionalTests/System.Net.Http.Functional.Tests.csproj
|
|
|
105ad9 |
+++ b/src/System.Net.Http/tests/FunctionalTests/System.Net.Http.Functional.Tests.csproj
|
|
|
105ad9 |
@@ -5,6 +5,7 @@
|
|
|
105ad9 |
<ProjectGuid>{C85CF035-7804-41FF-9557-48B7C948B58D}</ProjectGuid>
|
|
|
105ad9 |
<DefineConstants Condition="'$(TargetGroup)'=='netcoreapp'">$(DefineConstants);netcoreapp</DefineConstants>
|
|
|
105ad9 |
<DefineConstants Condition="'$(TargetsWindows)'=='true'">$(DefineConstants);TargetsWindows</DefineConstants>
|
|
|
105ad9 |
+ <DefineConstants>$(DefineConstants);SYSNETHTTP_NO_OPENSSL</DefineConstants>
|
|
|
105ad9 |
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
|
|
105ad9 |
</PropertyGroup>
|
|
|
105ad9 |
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Unix-Debug|AnyCPU'" />
|
|
|
105ad9 |
@@ -169,4 +170,4 @@
|
|
|
105ad9 |
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
|
|
105ad9 |
</ItemGroup>
|
|
|
105ad9 |
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
|
|
|
105ad9 |
-</Project>
|
|
|
105ad9 |
\ No newline at end of file
|
|
|
105ad9 |
+</Project>
|
|
|
105ad9 |
diff --git a/src/System.Net.Http/tests/FunctionalTests/TestHelper.cs b/src/System.Net.Http/tests/FunctionalTests/TestHelper.cs
|
|
|
105ad9 |
index 9bf6f216d1..2e29128a92 100644
|
|
|
105ad9 |
--- a/src/System.Net.Http/tests/FunctionalTests/TestHelper.cs
|
|
|
105ad9 |
+++ b/src/System.Net.Http/tests/FunctionalTests/TestHelper.cs
|
|
|
105ad9 |
@@ -6,6 +6,8 @@ using System.Collections.Generic;
|
|
|
105ad9 |
using System.Linq;
|
|
|
105ad9 |
using System.Net.NetworkInformation;
|
|
|
105ad9 |
using System.Net.Security;
|
|
|
105ad9 |
+using System.Reflection;
|
|
|
105ad9 |
+using System.Runtime.InteropServices;
|
|
|
105ad9 |
using System.Security.Cryptography;
|
|
|
105ad9 |
using System.Security.Cryptography.X509Certificates;
|
|
|
105ad9 |
using System.Text;
|
|
|
105ad9 |
@@ -107,5 +109,72 @@ namespace System.Net.Http.Functional.Tests
|
|
|
105ad9 |
.Select(a => a.Address)
|
|
|
105ad9 |
.Where(a => a.IsIPv6LinkLocal)
|
|
|
105ad9 |
.FirstOrDefault();
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ public static void EnsureHttp2Feature(HttpClientHandler handler)
|
|
|
105ad9 |
+ {
|
|
|
105ad9 |
+ // All .NET Core implementations of HttpClientHandler have HTTP/2 enabled by default except when using
|
|
|
105ad9 |
+ // SocketsHttpHandler. Right now, the HTTP/2 feature is disabled on SocketsHttpHandler unless certain
|
|
|
105ad9 |
+ // AppContext switches or environment variables are set. To help with testing, we can enable the HTTP/2
|
|
|
105ad9 |
+ // feature for a specific handler instance by using reflection.
|
|
|
105ad9 |
+ FieldInfo field_socketsHttpHandler = typeof(HttpClientHandler).GetField(
|
|
|
105ad9 |
+ "_socketsHttpHandler",
|
|
|
105ad9 |
+ BindingFlags.NonPublic | BindingFlags.Instance);
|
|
|
105ad9 |
+ if (field_socketsHttpHandler == null)
|
|
|
105ad9 |
+ {
|
|
|
105ad9 |
+ // Not using .NET Core implementation, i.e. could be .NET Framework or UAP.
|
|
|
105ad9 |
+ return;
|
|
|
105ad9 |
+ }
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ object _socketsHttpHandler = field_socketsHttpHandler.GetValue(handler);
|
|
|
105ad9 |
+ if (_socketsHttpHandler == null)
|
|
|
105ad9 |
+ {
|
|
|
105ad9 |
+ // Not using SocketsHttpHandler, i.e. using WinHttpHandler or CurlHandler.
|
|
|
105ad9 |
+ return;
|
|
|
105ad9 |
+ }
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ // Get HttpConnectionSettings object from SocketsHttpHandler.
|
|
|
105ad9 |
+ Type type_SocketsHttpHandler = typeof(HttpClientHandler).Assembly.GetType("System.Net.Http.SocketsHttpHandler");
|
|
|
105ad9 |
+ FieldInfo field_settings = type_SocketsHttpHandler.GetField(
|
|
|
105ad9 |
+ "_settings",
|
|
|
105ad9 |
+ BindingFlags.NonPublic | BindingFlags.Instance);
|
|
|
105ad9 |
+ Assert.NotNull(field_settings);
|
|
|
105ad9 |
+ object _settings = field_settings.GetValue(_socketsHttpHandler);
|
|
|
105ad9 |
+ Assert.NotNull(_settings);
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ // Set _maxHttpVersion field to HTTP/2.0.
|
|
|
105ad9 |
+ Type type_HttpConnectionSettings = typeof(HttpClientHandler).Assembly.GetType("System.Net.Http.HttpConnectionSettings");
|
|
|
105ad9 |
+ FieldInfo field_maxHttpVersion = type_HttpConnectionSettings.GetField(
|
|
|
105ad9 |
+ "_maxHttpVersion",
|
|
|
105ad9 |
+ BindingFlags.NonPublic | BindingFlags.Instance);
|
|
|
105ad9 |
+ field_maxHttpVersion.SetValue(_settings, new Version(2, 0));
|
|
|
105ad9 |
+ }
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ public static bool NativeHandlerSupportsSslConfiguration()
|
|
|
105ad9 |
+ {
|
|
|
105ad9 |
+#if TargetsWindows
|
|
|
105ad9 |
+ return true;
|
|
|
105ad9 |
+#else
|
|
|
105ad9 |
+ if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
|
|
105ad9 |
+ {
|
|
|
105ad9 |
+ return false;
|
|
|
105ad9 |
+ }
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ // For other Unix-based systems it's true if (and only if) the currect openssl backend
|
|
|
105ad9 |
+ // is used with libcurl.
|
|
|
105ad9 |
+ bool hasAnyOpenSsl =
|
|
|
105ad9 |
+ Interop.Http.GetSslVersionDescription()?.StartsWith(Interop.Http.OpenSslDescriptionPrefix, StringComparison.OrdinalIgnoreCase) ?? false;
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ if (!hasAnyOpenSsl)
|
|
|
105ad9 |
+ {
|
|
|
105ad9 |
+ return false;
|
|
|
105ad9 |
+ }
|
|
|
105ad9 |
+
|
|
|
105ad9 |
+ // We're on an OpenSSL-based system, with an OpenSSL backend.
|
|
|
105ad9 |
+ // Ask the product how it feels about this.
|
|
|
105ad9 |
+ Type interopHttp = typeof(HttpClient).Assembly.GetType("Interop+Http");
|
|
|
105ad9 |
+ PropertyInfo hasMatchingOpenSslVersion = interopHttp.GetProperty("HasMatchingOpenSslVersion", BindingFlags.Static | BindingFlags.NonPublic);
|
|
|
105ad9 |
+ return (bool)hasMatchingOpenSslVersion.GetValue(null);
|
|
|
105ad9 |
+#endif
|
|
|
105ad9 |
+ }
|
|
|
105ad9 |
}
|
|
|
105ad9 |
}
|
|
|
105ad9 |
--
|
|
|
105ad9 |
2.20.1
|
|
|
105ad9 |
|