Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 45 additions & 24 deletions src/libraries/System.Net.Quic/tests/FunctionalTests/MsQuicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public async Task UntrustedClientCertificateFails()

await using QuicListener listener = await CreateQuicListener(listenerOptions);
QuicClientConnectionOptions clientOptions = CreateQuicClientOptions(listener.LocalEndPoint);
clientOptions.ClientAuthenticationOptions.ClientCertificates = new X509CertificateCollection() { ClientCertificate };
clientOptions.ClientAuthenticationOptions.ClientCertificateContext = QuicTestCollection.ClientCertificateContext;
Task<QuicConnection> clientTask = CreateQuicConnection(clientOptions).AsTask();

// This will propagate the AuthenticationException since the client certificate is not trusted.
Expand Down Expand Up @@ -402,6 +402,7 @@ public async Task ConnectWithServerCertificateCallback()
ConnectionOptionsCallback = (_, _, _) =>
{
var serverOptions = CreateQuicServerOptions();
serverOptions.ServerAuthenticationOptions.ServerCertificateContext = null;
serverOptions.ServerAuthenticationOptions.ServerCertificate = null;
serverOptions.ServerAuthenticationOptions.ServerCertificateSelectionCallback = (sender, hostName) =>
{
Expand Down Expand Up @@ -475,6 +476,7 @@ public async Task ConnectWithIpSetsSni(string destination)
ConnectionOptionsCallback = (_, _, _) =>
{
var serverOptions = CreateQuicServerOptions();
serverOptions.ServerAuthenticationOptions.ServerCertificateContext = null;
serverOptions.ServerAuthenticationOptions.ServerCertificate = null;
serverOptions.ServerAuthenticationOptions.ServerCertificateSelectionCallback = (sender, hostName) =>
{
Expand Down Expand Up @@ -508,6 +510,7 @@ public async Task ConnectWithCertificateForDifferentName_Throws()
ConnectionOptionsCallback = (_, _, _) =>
{
var serverOptions = CreateQuicServerOptions();
serverOptions.ServerAuthenticationOptions.ServerCertificateContext = null;
serverOptions.ServerAuthenticationOptions.ServerCertificate = certificate;
return ValueTask.FromResult(serverOptions);
}
Expand Down Expand Up @@ -541,6 +544,7 @@ public async Task ConnectWithCertificate_MissingTargetHost_Succeeds()
ConnectionOptionsCallback = (_, _, _) =>
{
var serverOptions = CreateQuicServerOptions();
serverOptions.ServerAuthenticationOptions.ServerCertificateContext = null;
serverOptions.ServerAuthenticationOptions.ServerCertificate = certificate;
return ValueTask.FromResult(serverOptions);
}
Expand Down Expand Up @@ -578,32 +582,49 @@ public async Task ConnectWithCertificateForLoopbackIP_IndicatesExpectedError(str
// [ActiveIssue("https://github.com/dotnet/runtime/issues/119641")]
forceRsaCertificate: !PlatformDetection.IsWindows);
X509Certificate2 certificate = pkiHolder.EndEntity;

var listenerOptions = new QuicListenerOptions()
SslStreamCertificateContext serverContext = SslStreamCertificateContext.Create(certificate, pkiHolder.IssuerChain, offline: true);
try
{
ListenEndPoint = new IPEndPoint(ipAddress, 0),
ApplicationProtocols = new List<SslApplicationProtocol>() { ApplicationProtocol },
ConnectionOptionsCallback = (_, _, _) =>
var listenerOptions = new QuicListenerOptions()
{
var serverOptions = CreateQuicServerOptions();
serverOptions.ServerAuthenticationOptions.ServerCertificate = certificate;
return ValueTask.FromResult(serverOptions);
}
};
ListenEndPoint = new IPEndPoint(ipAddress, 0),
ApplicationProtocols = new List<SslApplicationProtocol>() { ApplicationProtocol },
ConnectionOptionsCallback = (_, _, _) =>
{
var serverOptions = CreateQuicServerOptions();
serverOptions.ServerAuthenticationOptions.ServerCertificate = null;
serverOptions.ServerAuthenticationOptions.ServerCertificateContext = serverContext;
return ValueTask.FromResult(serverOptions);
}
};

// Use whatever endpoint, it'll get overwritten in CreateConnectedQuicConnection.
QuicClientConnectionOptions clientOptions = CreateQuicClientOptions(listenerOptions.ListenEndPoint);
clientOptions.ClientAuthenticationOptions.RemoteCertificateValidationCallback = (sender, cert, chain, errors) =>
{
Assert.Equal(certificate.Subject, cert.Subject);
Assert.Equal(certificate.Issuer, cert.Issuer);
Assert.Equal(expectsError ? SslPolicyErrors.RemoteCertificateNameMismatch : SslPolicyErrors.None, errors & SslPolicyErrors.RemoteCertificateNameMismatch);
return true;
};
// Use whatever endpoint, it'll get overwritten in CreateConnectedQuicConnection.
QuicClientConnectionOptions clientOptions = CreateQuicClientOptions(listenerOptions.ListenEndPoint);
clientOptions.ClientAuthenticationOptions.RemoteCertificateValidationCallback = (sender, cert, chain, errors) =>
{
Assert.Equal(certificate.Subject, cert.Subject);
Assert.Equal(certificate.Issuer, cert.Issuer);
Assert.Equal(expectsError ? SslPolicyErrors.RemoteCertificateNameMismatch : SslPolicyErrors.None, errors & SslPolicyErrors.RemoteCertificateNameMismatch);
return true;
};

(QuicConnection clientConnection, QuicConnection serverConnection) = await CreateConnectedQuicConnection(clientOptions, listenerOptions);
await clientConnection.DisposeAsync();
await serverConnection.DisposeAsync();
(QuicConnection clientConnection, QuicConnection serverConnection) = await CreateConnectedQuicConnection(clientOptions, listenerOptions);
try
{
await clientConnection.DisposeAsync();
}
finally
{
await serverConnection.DisposeAsync();
}
}
finally
{
foreach (X509Certificate2 intermediate in serverContext.IntermediateCertificates)
{
intermediate.Dispose();
}
}
}

public enum ClientCertSource
Expand Down Expand Up @@ -667,7 +688,7 @@ public async Task ConnectWithClientCertificate(bool sendCertificate, ClientCertS
break;

case ClientCertSource.CertificateContext:
clientOptions.ClientAuthenticationOptions.ClientCertificateContext = SslStreamCertificateContext.Create(ClientCertificate, null);
clientOptions.ClientAuthenticationOptions.ClientCertificateContext = QuicTestCollection.ClientCertificateContext;
break;
}
(QuicConnection clientConnection, QuicConnection serverConnection) = await CreateConnectedQuicConnection(clientOptions, listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed class QuicStreamConformanceTests : ConnectedStreamConformanceTests
protected override bool BlocksOnZeroByteReads => true;
protected override bool CanTimeout => true;

public readonly X509Certificate2 ServerCertificate = System.Net.Test.Common.Configuration.Certificates.GetServerCertificate();
public readonly X509Certificate2 ServerCertificate = QuicTestCollection.GetServerCertificate();
public ITestOutputHelper _output;

protected override void Dispose(bool disposing)
Expand All @@ -45,7 +45,7 @@ public SslServerAuthenticationOptions GetSslServerAuthenticationOptions()
return new SslServerAuthenticationOptions()
{
ApplicationProtocols = new List<SslApplicationProtocol>() { new SslApplicationProtocol("quictest") },
ServerCertificate = ServerCertificate
ServerCertificateContext = QuicTestCollection.ServerCertificateContext
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public abstract class QuicTestBase : IDisposable

public static SslApplicationProtocol ApplicationProtocol { get; } = new SslApplicationProtocol("quictest");

public readonly X509Certificate2 ServerCertificate = System.Net.Test.Common.Configuration.Certificates.GetServerCertificate();
public readonly X509Certificate2 ClientCertificate = System.Net.Test.Common.Configuration.Certificates.GetClientCertificate();
public readonly X509Certificate2 ServerCertificate = QuicTestCollection.GetServerCertificate();
public readonly X509Certificate2 ClientCertificate = QuicTestCollection.GetClientCertificate();

public ITestOutputHelper _output;
public const int PassingTestTimeoutMilliseconds = 4 * 60 * 1000;
Expand Down Expand Up @@ -96,7 +96,7 @@ public SslServerAuthenticationOptions GetSslServerAuthenticationOptions()
return new SslServerAuthenticationOptions()
{
ApplicationProtocols = new List<SslApplicationProtocol>() { ApplicationProtocol },
ServerCertificate = ServerCertificate
ServerCertificateContext = QuicTestCollection.ServerCertificateContext
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

using Microsoft.Quic;
using System.Diagnostics.CodeAnalysis;
using System.Net.Security;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -24,6 +26,18 @@ public unsafe class QuicTestCollection : ICollectionFixture<QuicTestCollection>,
public static Version MsQuicVersion { get; } = GetMsQuicVersion();

private static readonly Dictionary<string, int> s_unobservedExceptions = new Dictionary<string, int>();
private static readonly X509Certificate2 s_clientCertificate = System.Net.Test.Common.Configuration.Certificates.GetClientCertificate();
private static readonly X509Certificate2 s_serverCertificate = System.Net.Test.Common.Configuration.Certificates.GetServerCertificate();

internal static SslStreamCertificateContext ClientCertificateContext { get; } =
SslStreamCertificateContext.Create(s_clientCertificate, additionalCertificates: null, offline: true);

// Avoid rebuilding the default certificate chain, including issuer discovery, for every test connection.
internal static SslStreamCertificateContext ServerCertificateContext { get; } =
SslStreamCertificateContext.Create(s_serverCertificate, additionalCertificates: null, offline: true);

internal static X509Certificate2 GetClientCertificate() => new X509Certificate2(s_clientCertificate);
internal static X509Certificate2 GetServerCertificate() => new X509Certificate2(s_serverCertificate);

public QuicTestCollection()
{
Expand Down Expand Up @@ -56,6 +70,19 @@ public QuicTestCollection()

public unsafe void Dispose()
{
foreach (X509Certificate2 certificate in ClientCertificateContext.IntermediateCertificates)
{
certificate.Dispose();
}

foreach (X509Certificate2 certificate in ServerCertificateContext.IntermediateCertificates)
{
certificate.Dispose();
}

s_clientCertificate.Dispose();
s_serverCertificate.Dispose();

if (!IsSupported)
{
return;
Expand Down
Loading