diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.SslProtocols.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.SslProtocols.cs index ed0563692771c1..1d521de2f10165 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.SslProtocols.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.SslProtocols.cs @@ -127,7 +127,7 @@ public async Task GetAsync_AllowedSSLVersion_Succeeds(SslProtocols acceptedProto } // Use a different SNI for each connection to prevent TLS 1.3 renegotiation issue: https://github.com/dotnet/runtime/issues/47378 - client.DefaultRequestHeaders.Host = getTestSNIName(); + client.DefaultRequestHeaders.Host = GetTestSNIName(); var options = new LoopbackServer.Options { UseSsl = true, SslProtocols = acceptedProtocol }; await LoopbackServer.CreateServerAsync(async (server, url) => @@ -140,7 +140,7 @@ await TestHelper.WhenAllCompletedOrAnyFailed( Assert.Equal(1, count); } - string getTestSNIName() + string GetTestSNIName() { return $"{nameof(GetAsync_AllowedSSLVersion_Succeeds)}_{acceptedProtocol}_{requestOnlyThisProtocol}"; } diff --git a/src/libraries/System.Net.Security/tests/FunctionalTests/ClientAsyncAuthenticateTest.cs b/src/libraries/System.Net.Security/tests/FunctionalTests/ClientAsyncAuthenticateTest.cs index b5770ef49de181..8ef2e170b69e81 100644 --- a/src/libraries/System.Net.Security/tests/FunctionalTests/ClientAsyncAuthenticateTest.cs +++ b/src/libraries/System.Net.Security/tests/FunctionalTests/ClientAsyncAuthenticateTest.cs @@ -145,10 +145,13 @@ private async Task ClientAsyncSslHelper( using (var client = new TcpClient()) { server.SslProtocols = serverSslProtocols; + // Use a different SNI for each connection to prevent TLS 1.3 renegotiation issue: https://github.com/dotnet/runtime/issues/47378 + string serverName = TestHelper.GetTestSNIName(nameof(ClientAsyncSslHelper), clientSslProtocols, serverSslProtocols); + await client.ConnectAsync(server.RemoteEndPoint.Address, server.RemoteEndPoint.Port); using (SslStream sslStream = new SslStream(client.GetStream(), false, certificateCallback != null ? certificateCallback : AllowAnyServerCertificate, null)) { - Task clientAuthTask = sslStream.AuthenticateAsClientAsync("localhost", null, clientSslProtocols, false); + Task clientAuthTask = sslStream.AuthenticateAsClientAsync(serverName, null, clientSslProtocols, false); await clientAuthTask.TimeoutAfter(TestConfiguration.PassingTestTimeoutMilliseconds); _log.WriteLine("Client authenticated to server({0}) with encryption cipher: {1} {2}-bit strength", diff --git a/src/libraries/System.Net.Security/tests/FunctionalTests/ServerAsyncAuthenticateTest.cs b/src/libraries/System.Net.Security/tests/FunctionalTests/ServerAsyncAuthenticateTest.cs index 5deb8334361863..4307b3df9d2751 100644 --- a/src/libraries/System.Net.Security/tests/FunctionalTests/ServerAsyncAuthenticateTest.cs +++ b/src/libraries/System.Net.Security/tests/FunctionalTests/ServerAsyncAuthenticateTest.cs @@ -362,7 +362,8 @@ private async Task ServerAsyncSslHelper( return true; })) { - string serverName = _serverCertificate.GetNameInfo(X509NameType.SimpleName, false); + // Use a different SNI for each connection to prevent TLS 1.3 renegotiation issue: https://github.com/dotnet/runtime/issues/47378 + string serverName = TestHelper.GetTestSNIName(nameof(ServerAsyncSslHelper), clientSslProtocols, serverSslProtocols); _log.WriteLine("Connected on {0} {1} ({2} {3})", clientStream.Socket.LocalEndPoint, clientStream.Socket.RemoteEndPoint, clientStream.Socket.Handle, serverStream.Socket.Handle); _log.WriteLine("client SslStream#{0} server SslStream#{1}", sslClientStream.GetHashCode(), sslServerStream.GetHashCode()); diff --git a/src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamSystemDefaultsTest.cs b/src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamSystemDefaultsTest.cs index 0eebb02de11815..5380b4027a6f76 100644 --- a/src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamSystemDefaultsTest.cs +++ b/src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamSystemDefaultsTest.cs @@ -80,7 +80,7 @@ public async Task ClientAndServer_OneOrBothUseDefault_Ok(SslProtocols? clientPro using (X509Certificate2 clientCertificate = Configuration.Certificates.GetClientCertificate()) { // Use a different SNI for each connection to prevent TLS 1.3 renegotiation issue: https://github.com/dotnet/runtime/issues/47378 - string serverHost = getTestSNIName(); + string serverHost = TestHelper.GetTestSNIName(nameof(ClientAndServer_OneOrBothUseDefault_Ok), clientProtocols, serverProtocols); var clientCertificates = new X509CertificateCollection() { clientCertificate }; await TestConfiguration.WhenAllOrAnyFailedWithTimeout( @@ -100,16 +100,6 @@ await TestConfiguration.WhenAllOrAnyFailedWithTimeout( _clientStream.SslProtocol + " " + _clientStream.HashAlgorithm); } } - - string getTestSNIName() - { - static string ProtocolToString(SslProtocols? protocol) - { - return (protocol?.ToString() ?? "null").Replace(", ", "_"); - } - - return $"{nameof(ClientAndServer_OneOrBothUseDefault_Ok)}_{ProtocolToString(clientProtocols)}_{ProtocolToString(serverProtocols)}"; - } } [ConditionalTheory(nameof(IsNotWindows7))] diff --git a/src/libraries/System.Net.Security/tests/FunctionalTests/TestHelper.cs b/src/libraries/System.Net.Security/tests/FunctionalTests/TestHelper.cs index ecdfeb47829a45..d9752147435b1f 100644 --- a/src/libraries/System.Net.Security/tests/FunctionalTests/TestHelper.cs +++ b/src/libraries/System.Net.Security/tests/FunctionalTests/TestHelper.cs @@ -2,8 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.IO; +using System.Linq; using System.Net.Sockets; using System.Net.Test.Common; +using System.Security.Authentication; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates.Tests.Common; @@ -157,7 +159,7 @@ internal static (X509Certificate2 certificate, X509Certificate2Collection) Gener intermedPub3.Dispose(); CertificateAuthority intermediateAuthority3 = new CertificateAuthority(intermedCert3, null, null, null); - RSA eeKey = (RSA)endEntity.PrivateKey; + RSA eeKey = (RSA)endEntity.PrivateKey; endEntity = intermediateAuthority3.CreateEndEntity( $"CN=\"A SSL Test\", O=\"testName\"", eeKey, @@ -212,5 +214,18 @@ internal static async Task PingPong(SslStream client, SslStream server) Assert.Equal(s_pong, buffer); await t; } + + internal static string GetTestSNIName(string testMethodName, params SslProtocols?[] protocols) + { + static string ProtocolToString(SslProtocols? protocol) + { + return (protocol?.ToString() ?? "null").Replace(", ", "-"); + } + + var args = string.Join(".", protocols.Select(p => ProtocolToString(p))); + var name = testMethodName.Length > 63 ? testMethodName.Substring(0, 63) : testMethodName; + + return $"{name}.{args}"; + } } }