mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-27 13:19:31 -10:00
A set of HttpClient tests related to ClientCertificates were always failing on my machine: System.Net.Http.Functional.Tests.SocketsHttpHandler_HttpClientHandler_ClientCertificates_Test * method="Manual_SendClientCertificateWithClientAuthEKUToRemoteServer_OK" * method="Manual_SendClientCertificateWithNoEKUToRemoteServer_OK" * method="Manual_SendClientCertificateWithServerAuthEKUToRemoteServer_Forbidden" These tests would generate an exception: >System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.IO.IOException: The remote party requested renegotiation when AllowRenegotiation was set to false. Similar to the tests failing and fixed in PR dotnet/corefx#35225, the CI machines don't have the test root certificate installed. So, the tests weren't running in CI and the failures weren't noticed. The root cause of the test failures is due to using SocketsHttpHandler. It is always setting `SslStream.AllowRenegotiation` to false when first attempting to connect via HTTP/2. This is fine if the connection is truely HTTP/2 since HTTP/2 doesn't support TLS renegotiation. However, if the remote endpoint doesn't support HTTP/2 and thus connects with HTTP/1.1, then the SslStream should not have AllowRenegotiation set to false. Client certificate scenarios frequently have the server ask for the client certificate later in the connection process. So, TLS renegotiation is used. Opened new issue dotnet/corefx#35239 to fix this in SocketsHttpHandler (or SslStream). As part of fixing these tests, I refactored them into a single Theory test. But I had to workaround complications due to using RemoteInvoke. I added a new RemoteInvoke overload to handling passing an additional parameter. Commit migrated from https://github.com/dotnet/corefx/commit/0c2ccd172157e0ebf1957e1bbcd5f07786ad6e3d
313 lines
14 KiB
C#
313 lines
14 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Collections.Generic;
|
|
using System.Net.Security;
|
|
using System.Net.Sockets;
|
|
using System.Net.Test.Common;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Threading.Tasks;
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace System.Net.Http.Functional.Tests
|
|
{
|
|
using Configuration = System.Net.Test.Common.Configuration;
|
|
|
|
public abstract class HttpClientHandler_ClientCertificates_Test : HttpClientHandlerTestBase
|
|
{
|
|
public bool CanTestCertificates =>
|
|
Capability.IsTrustedRootCertificateInstalled() &&
|
|
(BackendSupportsCustomCertificateHandling || Capability.AreHostsFileNamesInstalled());
|
|
|
|
public bool CanTestClientCertificates =>
|
|
CanTestCertificates && BackendSupportsCustomCertificateHandling;
|
|
|
|
public HttpClientHandler_ClientCertificates_Test(ITestOutputHelper output)
|
|
{
|
|
_output = output;
|
|
}
|
|
|
|
private readonly ITestOutputHelper _output;
|
|
[Fact]
|
|
public void ClientCertificateOptions_Default()
|
|
{
|
|
using (HttpClientHandler handler = CreateHttpClientHandler())
|
|
{
|
|
Assert.Equal(ClientCertificateOption.Manual, handler.ClientCertificateOptions);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData((ClientCertificateOption)2)]
|
|
[InlineData((ClientCertificateOption)(-1))]
|
|
public void ClientCertificateOptions_InvalidArg_ThrowsException(ClientCertificateOption option)
|
|
{
|
|
using (HttpClientHandler handler = CreateHttpClientHandler())
|
|
{
|
|
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => handler.ClientCertificateOptions = option);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(ClientCertificateOption.Automatic)]
|
|
[InlineData(ClientCertificateOption.Manual)]
|
|
public void ClientCertificateOptions_ValueArg_Roundtrips(ClientCertificateOption option)
|
|
{
|
|
using (HttpClientHandler handler = CreateHttpClientHandler())
|
|
{
|
|
handler.ClientCertificateOptions = option;
|
|
Assert.Equal(option, handler.ClientCertificateOptions);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ClientCertificates_ClientCertificateOptionsAutomatic_ThrowsException()
|
|
{
|
|
using (HttpClientHandler handler = CreateHttpClientHandler())
|
|
{
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Automatic;
|
|
Assert.Throws<InvalidOperationException>(() => handler.ClientCertificates);
|
|
}
|
|
}
|
|
|
|
[OuterLoop("Uses external server")]
|
|
[Fact]
|
|
public async Task Automatic_SSLBackendNotSupported_ThrowsPlatformNotSupportedException()
|
|
{
|
|
if (BackendSupportsCustomCertificateHandling) // can't use [Conditional*] right now as it's evaluated at the wrong time for SocketsHttpHandler
|
|
{
|
|
return;
|
|
}
|
|
|
|
using (HttpClientHandler handler = CreateHttpClientHandler())
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Automatic;
|
|
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => client.GetAsync(Configuration.Http.SecureRemoteEchoServer));
|
|
}
|
|
}
|
|
|
|
[OuterLoop("Uses external server")]
|
|
[Fact]
|
|
public async Task Manual_SSLBackendNotSupported_ThrowsPlatformNotSupportedException()
|
|
{
|
|
if (BackendSupportsCustomCertificateHandling) // can't use [Conditional*] right now as it's evaluated at the wrong time for SocketsHttpHandler
|
|
{
|
|
return;
|
|
}
|
|
|
|
HttpClientHandler handler = CreateHttpClientHandler();
|
|
handler.ClientCertificates.Add(Configuration.Certificates.GetClientCertificate());
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => client.GetAsync(Configuration.Http.SecureRemoteEchoServer));
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<object[]> SelectClientCertificateForRemoteServer_MemberData()
|
|
{
|
|
yield return new object[] { 1, HttpStatusCode.OK };
|
|
yield return new object[] { 2, HttpStatusCode.OK };
|
|
yield return new object[] { 3, HttpStatusCode.Forbidden };
|
|
}
|
|
|
|
[OuterLoop("Uses external server")]
|
|
[Theory]
|
|
[MemberData(nameof(SelectClientCertificateForRemoteServer_MemberData))]
|
|
public void Manual_SelectClientCertificateForRemoteServer_ServerOnlyReceivesValidClientCert(
|
|
int certIndex, HttpStatusCode expectedStatusCode)
|
|
{
|
|
if (!CanTestClientCertificates) // can't use [Conditional*] right now as it's evaluated at the wrong time for SocketsHttpHandler
|
|
{
|
|
_output.WriteLine($"Skipping {nameof(Manual_SelectClientCertificateForRemoteServer_ServerOnlyReceivesValidClientCert)}()");
|
|
return;
|
|
}
|
|
|
|
// UAP HTTP stack caches connections per-process. This causes interference when these tests run in
|
|
// the same process as the other tests. Each test needs to be isolated to its own process.
|
|
// See dicussion: https://github.com/dotnet/corefx/issues/21945
|
|
RemoteInvoke(async (certIndexString, expectedStatusCodeString, useSocketsHttpHandlerString) =>
|
|
{
|
|
X509Certificate2 clientCert = null;
|
|
|
|
// Get client certificate. RemoteInvoke doesn't allow easy marshaling of complex types.
|
|
// So we have to select the certificate at this point in the test execution.
|
|
if (certIndexString == "1")
|
|
{
|
|
// This is a valid client cert since it has an EKU with a ClientAuthentication OID.
|
|
clientCert = Configuration.Certificates.GetClientCertificate();
|
|
}
|
|
else if (certIndexString == "2")
|
|
{
|
|
// This is a valid client cert since it has no EKU thus all usages are permitted.
|
|
clientCert = Configuration.Certificates.GetNoEKUCertificate();
|
|
}
|
|
else if (certIndexString == "3")
|
|
{
|
|
// This is an invalid client cert since it has an EKU but is missing ClientAuthentication OID.
|
|
clientCert = Configuration.Certificates.GetServerCertificate();
|
|
}
|
|
|
|
Assert.NotNull(clientCert);
|
|
|
|
var statusCode = (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), expectedStatusCodeString);
|
|
HttpClientHandler handler = CreateHttpClientHandler(useSocketsHttpHandlerString);
|
|
handler.ClientCertificates.Add(clientCert);
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
var request = new HttpRequestMessage();
|
|
request.RequestUri = new Uri(Configuration.Http.EchoClientCertificateRemoteServer);
|
|
|
|
// Issue #35239. Force HTTP/1.1.
|
|
request.Version = new Version(1,1);
|
|
HttpResponseMessage response = await client.SendAsync(request);
|
|
Assert.Equal(statusCode, response.StatusCode);
|
|
|
|
if (statusCode == HttpStatusCode.OK)
|
|
{
|
|
string body = await response.Content.ReadAsStringAsync();
|
|
byte[] bytes = Convert.FromBase64String(body);
|
|
var receivedCert = new X509Certificate2(bytes);
|
|
Assert.Equal(clientCert, receivedCert);
|
|
}
|
|
|
|
return SuccessExitCode;
|
|
}
|
|
}, certIndex.ToString(), expectedStatusCode.ToString(), UseSocketsHttpHandler.ToString()).Dispose();
|
|
}
|
|
|
|
[ActiveIssue(30056, TargetFrameworkMonikers.Uap)]
|
|
[OuterLoop("Uses GC and waits for finalizers")]
|
|
[Theory]
|
|
[InlineData(6, false)]
|
|
[InlineData(3, true)]
|
|
public async Task Manual_CertificateSentMatchesCertificateReceived_Success(
|
|
int numberOfRequests,
|
|
bool reuseClient) // validate behavior with and without connection pooling, which impacts client cert usage
|
|
{
|
|
if (!BackendSupportsCustomCertificateHandling) // can't use [Conditional*] right now as it's evaluated at the wrong time for SocketsHttpHandler
|
|
{
|
|
_output.WriteLine($"Skipping {nameof(Manual_CertificateSentMatchesCertificateReceived_Success)}()");
|
|
return;
|
|
}
|
|
|
|
var options = new LoopbackServer.Options { UseSsl = true };
|
|
|
|
Func<X509Certificate2, HttpClient> createClient = (cert) =>
|
|
{
|
|
HttpClientHandler handler = CreateHttpClientHandler();
|
|
handler.ServerCertificateCustomValidationCallback = delegate { return true; };
|
|
handler.ClientCertificates.Add(cert);
|
|
Assert.True(handler.ClientCertificates.Contains(cert));
|
|
return new HttpClient(handler);
|
|
};
|
|
|
|
Func<HttpClient, LoopbackServer, Uri, X509Certificate2, Task> makeAndValidateRequest = async (client, server, url, cert) =>
|
|
{
|
|
await TestHelper.WhenAllCompletedOrAnyFailed(
|
|
client.GetStringAsync(url),
|
|
server.AcceptConnectionAsync(async connection =>
|
|
{
|
|
SslStream sslStream = Assert.IsType<SslStream>(connection.Stream);
|
|
|
|
// We can't do Assert.Equal(cert, sslStream.RemoteCertificate) because
|
|
// on .NET Framework sslStream.RemoteCertificate is always an X509Certificate
|
|
// object which is not equal to the X509Certificate2 object we use in the tests.
|
|
// So, we'll just compare a few properties to make sure it's the right certificate.
|
|
Assert.Equal(cert.Subject, sslStream.RemoteCertificate.Subject);
|
|
Assert.Equal(cert.Issuer, sslStream.RemoteCertificate.Issuer);
|
|
|
|
await connection.ReadRequestHeaderAndSendResponseAsync(additionalHeaders: "Connection: close\r\n");
|
|
}));
|
|
};
|
|
|
|
await LoopbackServer.CreateServerAsync(async (server, url) =>
|
|
{
|
|
using (X509Certificate2 cert = Configuration.Certificates.GetClientCertificate())
|
|
{
|
|
if (reuseClient)
|
|
{
|
|
using (HttpClient client = createClient(cert))
|
|
{
|
|
for (int i = 0; i < numberOfRequests; i++)
|
|
{
|
|
await makeAndValidateRequest(client, server, url, cert);
|
|
|
|
GC.Collect();
|
|
GC.WaitForPendingFinalizers();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < numberOfRequests; i++)
|
|
{
|
|
using (HttpClient client = createClient(cert))
|
|
{
|
|
await makeAndValidateRequest(client, server, url, cert);
|
|
}
|
|
|
|
GC.Collect();
|
|
GC.WaitForPendingFinalizers();
|
|
}
|
|
}
|
|
}
|
|
}, options);
|
|
}
|
|
|
|
[ActiveIssue(30056, TargetFrameworkMonikers.Uap)]
|
|
[Theory]
|
|
[InlineData(ClientCertificateOption.Manual)]
|
|
[InlineData(ClientCertificateOption.Automatic)]
|
|
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Fails with \"Authentication failed\" error.")]
|
|
public async Task AutomaticOrManual_DoesntFailRegardlessOfWhetherClientCertsAreAvailable(ClientCertificateOption mode)
|
|
{
|
|
if (!BackendSupportsCustomCertificateHandling) // can't use [Conditional*] right now as it's evaluated at the wrong time for SocketsHttpHandler
|
|
{
|
|
_output.WriteLine($"Skipping {nameof(AutomaticOrManual_DoesntFailRegardlessOfWhetherClientCertsAreAvailable)}()");
|
|
return;
|
|
}
|
|
|
|
using (HttpClientHandler handler = CreateHttpClientHandler())
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
handler.ServerCertificateCustomValidationCallback = delegate { return true; };
|
|
handler.ClientCertificateOptions = mode;
|
|
|
|
await LoopbackServer.CreateServerAsync(async server =>
|
|
{
|
|
Task clientTask = client.GetStringAsync(server.Uri);
|
|
Task serverTask = server.AcceptConnectionAsync(async connection =>
|
|
{
|
|
SslStream sslStream = Assert.IsType<SslStream>(connection.Stream);
|
|
await connection.ReadRequestHeaderAndSendResponseAsync();
|
|
});
|
|
|
|
await new Task[] { clientTask, serverTask }.WhenAllOrAnyFailed();
|
|
}, new LoopbackServer.Options { UseSsl = true });
|
|
}
|
|
}
|
|
|
|
private bool BackendSupportsCustomCertificateHandling
|
|
{
|
|
get
|
|
{
|
|
#if TargetsWindows
|
|
return true;
|
|
#else
|
|
if (UseSocketsHttpHandler)
|
|
{
|
|
// Socket Handler is independent of platform curl.
|
|
return true;
|
|
}
|
|
|
|
return TestHelper.NativeHandlerSupportsSslConfiguration();
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
}
|