mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-27 13:19:31 -10:00
Commit migrated from https://github.com/dotnet/corefx/commit/9d4347b5bdca817937dea602f452df3fc2e9e2f3
224 lines
10 KiB
C#
224 lines
10 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.Test.Common;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Threading.Tasks;
|
|
using Xunit;
|
|
|
|
namespace System.Net.Http.Tests
|
|
{
|
|
public class HttpClientHandler_ServerCertificates_Test
|
|
{
|
|
[Fact]
|
|
public async Task NoCallback_ValidCertificate_CallbackNotCalled()
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
Assert.Null(handler.ServerCertificateValidationCallback);
|
|
Assert.False(handler.CheckCertificateRevocationList);
|
|
|
|
using (HttpResponseMessage response = await client.GetAsync(HttpTestServers.SecureRemoteEchoServer))
|
|
{
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
}
|
|
|
|
Assert.Throws<InvalidOperationException>(() => handler.ServerCertificateValidationCallback = null);
|
|
Assert.Throws<InvalidOperationException>(() => handler.CheckCertificateRevocationList = false);
|
|
}
|
|
}
|
|
|
|
[ConditionalFact(nameof(BackendSupportsCustomCertificateHandling))]
|
|
public async Task UseCallback_NotSecureConnection_CallbackNotCalled()
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
bool callbackCalled = false;
|
|
handler.ServerCertificateValidationCallback = delegate { callbackCalled = true; return true; };
|
|
|
|
using (HttpResponseMessage response = await client.GetAsync(HttpTestServers.RemoteEchoServer))
|
|
{
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
}
|
|
|
|
Assert.False(callbackCalled);
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<object[]> UseCallback_ValidCertificate_ExpectedValuesDuringCallback_Urls()
|
|
{
|
|
foreach (bool checkRevocation in new[] { true, false })
|
|
{
|
|
yield return new object[] { HttpTestServers.SecureRemoteEchoServer, checkRevocation };
|
|
yield return new object[] { HttpTestServers.RedirectUriForDestinationUri(true, HttpTestServers.SecureRemoteEchoServer, 1), checkRevocation };
|
|
}
|
|
}
|
|
|
|
[ConditionalTheory(nameof(BackendSupportsCustomCertificateHandling))]
|
|
[MemberData(nameof(UseCallback_ValidCertificate_ExpectedValuesDuringCallback_Urls))]
|
|
public async Task UseCallback_ValidCertificate_ExpectedValuesDuringCallback(Uri url, bool checkRevocation)
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
bool callbackCalled = false;
|
|
handler.CheckCertificateRevocationList = checkRevocation;
|
|
handler.ServerCertificateValidationCallback = (request, cert, chain, errors) => {
|
|
callbackCalled = true;
|
|
Assert.NotNull(request);
|
|
Assert.Equal(SslPolicyErrors.None, errors);
|
|
Assert.True(chain.ChainElements.Count > 0);
|
|
Assert.NotEmpty(cert.Subject);
|
|
Assert.Equal(checkRevocation ? X509RevocationMode.Online : X509RevocationMode.NoCheck, chain.ChainPolicy.RevocationMode);
|
|
return true;
|
|
};
|
|
|
|
using (HttpResponseMessage response = await client.GetAsync(url))
|
|
{
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
}
|
|
|
|
Assert.True(callbackCalled);
|
|
}
|
|
}
|
|
|
|
[ConditionalFact(nameof(BackendSupportsCustomCertificateHandling))]
|
|
public async Task UseCallback_CallbackReturnsFailure_ThrowsException()
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
handler.ServerCertificateValidationCallback = delegate { return false; };
|
|
await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync(HttpTestServers.SecureRemoteEchoServer));
|
|
}
|
|
}
|
|
|
|
[ConditionalFact(nameof(BackendSupportsCustomCertificateHandling))]
|
|
public async Task UseCallback_CallbackThrowsException_ExceptionPropagates()
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
var e = new DivideByZeroException();
|
|
handler.ServerCertificateValidationCallback = delegate { throw e; };
|
|
Assert.Same(e, await Assert.ThrowsAsync<DivideByZeroException>(() => client.GetAsync(HttpTestServers.SecureRemoteEchoServer)));
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(HttpTestServers.ExpiredCertRemoteServer)]
|
|
[InlineData(HttpTestServers.SelfSignedCertRemoteServer)]
|
|
[InlineData(HttpTestServers.WrongHostNameCertRemoteServer)]
|
|
public async Task NoCallback_BadCertificate_ThrowsException(string url)
|
|
{
|
|
using (var client = new HttpClient())
|
|
{
|
|
await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync(url));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task NoCallback_RevokedCertificate_NoRevocationChecking_Succeeds()
|
|
{
|
|
using (var client = new HttpClient())
|
|
using (HttpResponseMessage response = await client.GetAsync(HttpTestServers.RevokedCertRemoteServer))
|
|
{
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
}
|
|
}
|
|
|
|
[ConditionalFact(nameof(BackendSupportsCustomCertificateHandling))]
|
|
public async Task NoCallback_RevokedCertificate_RevocationChecking_Fails()
|
|
{
|
|
var handler = new HttpClientHandler() { CheckCertificateRevocationList = true };
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync(HttpTestServers.RevokedCertRemoteServer));
|
|
}
|
|
}
|
|
|
|
[ConditionalTheory(nameof(BackendSupportsCustomCertificateHandling))]
|
|
[InlineData(HttpTestServers.ExpiredCertRemoteServer, SslPolicyErrors.RemoteCertificateChainErrors)]
|
|
[InlineData(HttpTestServers.SelfSignedCertRemoteServer, SslPolicyErrors.RemoteCertificateChainErrors)]
|
|
[InlineData(HttpTestServers.WrongHostNameCertRemoteServer, SslPolicyErrors.RemoteCertificateNameMismatch)]
|
|
public async Task UseCallback_BadCertificate_ExpectedPolicyErrors(string url, SslPolicyErrors expectedErrors)
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
bool callbackCalled = false;
|
|
|
|
handler.ServerCertificateValidationCallback = (request, cert, chain, errors) =>
|
|
{
|
|
callbackCalled = true;
|
|
Assert.NotNull(request);
|
|
Assert.NotNull(cert);
|
|
Assert.NotNull(chain);
|
|
Assert.Equal(expectedErrors, errors);
|
|
return true;
|
|
};
|
|
|
|
using (HttpResponseMessage response = await client.GetAsync(url))
|
|
{
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
}
|
|
|
|
Assert.True(callbackCalled);
|
|
}
|
|
}
|
|
|
|
[ConditionalFact(nameof(BackendDoesNotSupportCustomCertificateHandling))]
|
|
public async Task SSLBackendNotSupported_Callback_ThrowsPlatformNotSupportedException()
|
|
{
|
|
using (var client = new HttpClient(new HttpClientHandler() { ServerCertificateValidationCallback = delegate { return true; } }))
|
|
{
|
|
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => client.GetAsync(HttpTestServers.SecureRemoteEchoServer));
|
|
}
|
|
}
|
|
|
|
[ConditionalFact(nameof(BackendDoesNotSupportCustomCertificateHandling))]
|
|
public async Task SSLBackendNotSupported_Revocation_ThrowsPlatformNotSupportedException()
|
|
{
|
|
using (var client = new HttpClient(new HttpClientHandler() { CheckCertificateRevocationList = true }))
|
|
{
|
|
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => client.GetAsync(HttpTestServers.SecureRemoteEchoServer));
|
|
}
|
|
}
|
|
|
|
[ConditionalFact(nameof(BackendDoesNotSupportCustomCertificateHandling))]
|
|
public async Task SSLBackendNotSupported_AutomaticClientCerts_ThrowsPlatformNotSupportedException()
|
|
{
|
|
using (var client = new HttpClient(new HttpClientHandler() { ClientCertificateOptions = ClientCertificateOption.Automatic }))
|
|
{
|
|
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => client.GetAsync(HttpTestServers.SecureRemoteEchoServer));
|
|
}
|
|
}
|
|
|
|
[ConditionalFact(nameof(BackendDoesNotSupportCustomCertificateHandling))]
|
|
public async Task SSLBackendNotSupported_ManualClientCerts_ThrowsPlatformNotSupportedException()
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
handler.ClientCertificates.Add(new X509Certificate2());
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => client.GetAsync(HttpTestServers.SecureRemoteEchoServer));
|
|
}
|
|
}
|
|
|
|
private static bool BackendSupportsCustomCertificateHandling =>
|
|
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
|
|
(CurlSslVersionDescription()?.StartsWith("OpenSSL") ?? false);
|
|
|
|
private static bool BackendDoesNotSupportCustomCertificateHandling => !BackendSupportsCustomCertificateHandling;
|
|
|
|
[DllImport("System.Net.Http.Native", EntryPoint = "HttpNative_GetSslVersionDescription")]
|
|
private static extern string CurlSslVersionDescription();
|
|
}
|
|
}
|