Skip to content

Commit 25c222b

Browse files
authored
use empty server name is client did not specify one (#39671)
* use empty server name * fix merge * feedback from review * add missing file
1 parent 065bf96 commit 25c222b

File tree

4 files changed

+13
-38
lines changed

4 files changed

+13
-38
lines changed

src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Ssl.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public static bool SslCheckHostnameMatch(SafeSslHandle handle, string hostName,
384384
// this code could be removed.
385385
//
386386
// It was verified as supporting case invariant match as of 10.12.1 (Sierra).
387-
string matchName = s_idnMapping.GetAscii(hostName);
387+
string matchName = string.IsNullOrEmpty(hostName) ? string.Empty : s_idnMapping.GetAscii(hostName);
388388

389389
using (SafeCFDateHandle cfNotBefore = CoreFoundation.CFDateCreate(notBefore))
390390
using (SafeCreateHandle cfHostname = CoreFoundation.CFStringCreateWithCString(matchName))

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ internal static SafeSslHandle AllocateSslContext(SslProtocols protocols, SafeX50
189189
if (!sslAuthenticationOptions.IsServer)
190190
{
191191
// The IdnMapping converts unicode input into the IDNA punycode sequence.
192-
string punyCode = s_idnMapping.GetAscii(sslAuthenticationOptions.TargetHost!);
192+
string punyCode = string.IsNullOrEmpty(sslAuthenticationOptions.TargetHost) ? string.Empty : s_idnMapping.GetAscii(sslAuthenticationOptions.TargetHost!);
193193

194194
// Similar to windows behavior, set SNI on openssl by default for client context, ignore errors.
195195
if (!Ssl.SslSetTlsExtHostName(context, punyCode))

src/libraries/System.Net.Security/src/System/Net/Security/SslStream.Implementation.cs

+5-8
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ namespace System.Net.Security
1515
{
1616
public partial class SslStream
1717
{
18-
private static int s_uniqueNameInteger = 123;
19-
2018
private SslAuthenticationOptions? _sslAuthenticationOptions;
2119

2220
private int _nestedAuth;
@@ -66,10 +64,6 @@ private void ValidateCreateContext(SslClientAuthenticationOptions sslClientAuthe
6664
try
6765
{
6866
_sslAuthenticationOptions = new SslAuthenticationOptions(sslClientAuthenticationOptions, remoteCallback, localCallback);
69-
if (_sslAuthenticationOptions.TargetHost!.Length == 0)
70-
{
71-
_sslAuthenticationOptions.TargetHost = "?" + Interlocked.Increment(ref s_uniqueNameInteger).ToString(NumberFormatInfo.InvariantInfo);
72-
}
7367
_context = new SecureChannel(_sslAuthenticationOptions, this);
7468
}
7569
catch (Win32Exception e)
@@ -420,12 +414,15 @@ private async ValueTask<ProtocolToken> ReceiveBlobAsync<TIOAdapter>(TIOAdapter a
420414
if (_lastFrame.HandshakeType == TlsHandshakeType.ClientHello)
421415
{
422416
// SNI if it exist. Even if we could not parse the hello, we can fall-back to default certificate.
423-
_sslAuthenticationOptions!.TargetHost = _lastFrame.TargetName;
417+
if (_lastFrame.TargetName != null)
418+
{
419+
_sslAuthenticationOptions!.TargetHost = _lastFrame.TargetName;
420+
}
424421

425422
if (_sslAuthenticationOptions.ServerOptionDelegate != null)
426423
{
427424
SslServerAuthenticationOptions userOptions =
428-
await _sslAuthenticationOptions.ServerOptionDelegate(this, new SslClientHelloInfo(_lastFrame.TargetName, _lastFrame.SupportedVersions),
425+
await _sslAuthenticationOptions.ServerOptionDelegate(this, new SslClientHelloInfo(_sslAuthenticationOptions.TargetHost, _lastFrame.SupportedVersions),
429426
_sslAuthenticationOptions.UserState, adapter.CancellationToken).ConfigureAwait(false);
430427
_sslAuthenticationOptions.UpdateOptions(userOptions);
431428
}

src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamNetworkStreamTest.cs

+6-28
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public async Task SslStream_NestedAuth_Throws()
205205
[InlineData(true)]
206206
public async Task SslStream_TargetHostName_Succeeds(bool useEmptyName)
207207
{
208-
string tagetName = useEmptyName ? string.Empty : Guid.NewGuid().ToString("N");
208+
string targetName = useEmptyName ? string.Empty : Guid.NewGuid().ToString("N");
209209

210210
(Stream clientStream, Stream serverStream) = TestHelper.GetConnectedStreams();
211211
using (clientStream)
@@ -218,19 +218,12 @@ public async Task SslStream_TargetHostName_Succeeds(bool useEmptyName)
218218
Assert.Equal(string.Empty, client.TargetHostName);
219219
Assert.Equal(string.Empty, server.TargetHostName);
220220

221-
SslClientAuthenticationOptions clientOptions = new SslClientAuthenticationOptions() { TargetHost = tagetName };
221+
SslClientAuthenticationOptions clientOptions = new SslClientAuthenticationOptions() { TargetHost = targetName };
222222
clientOptions.RemoteCertificateValidationCallback =
223223
(sender, certificate, chain, sslPolicyErrors) =>
224224
{
225225
SslStream stream = (SslStream)sender;
226-
if (useEmptyName)
227-
{
228-
Assert.Equal('?', stream.TargetHostName[0]);
229-
}
230-
else
231-
{
232-
Assert.Equal(tagetName, stream.TargetHostName);
233-
}
226+
Assert.Equal(targetName, stream.TargetHostName);
234227

235228
return true;
236229
};
@@ -240,31 +233,16 @@ public async Task SslStream_TargetHostName_Succeeds(bool useEmptyName)
240233
(sender, name) =>
241234
{
242235
SslStream stream = (SslStream)sender;
243-
if (useEmptyName)
244-
{
245-
Assert.Equal('?', stream.TargetHostName[0]);
246-
}
247-
else
248-
{
249-
Assert.Equal(tagetName, stream.TargetHostName);
250-
}
236+
Assert.Equal(targetName, stream.TargetHostName);
251237

252238
return certificate;
253239
};
254240

255241
await TestConfiguration.WhenAllOrAnyFailedWithTimeout(
256242
client.AuthenticateAsClientAsync(clientOptions),
257243
server.AuthenticateAsServerAsync(serverOptions));
258-
if (useEmptyName)
259-
{
260-
Assert.Equal('?', client.TargetHostName[0]);
261-
Assert.Equal('?', server.TargetHostName[0]);
262-
}
263-
else
264-
{
265-
Assert.Equal(tagetName, client.TargetHostName);
266-
Assert.Equal(tagetName, server.TargetHostName);
267-
}
244+
Assert.Equal(targetName, client.TargetHostName);
245+
Assert.Equal(targetName, server.TargetHostName);
268246
}
269247
}
270248

0 commit comments

Comments
 (0)