Skip to content

Commit 16b7255

Browse files
authored
improve Tls12 detection on Windows7 (#67935)
* improve Tls12 detection on Windows7 * fix Tls11 * feedback from review
1 parent f216e77 commit 16b7255

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ private static bool GetIsInContainer()
361361
return (IsLinux && File.Exists("/.dockerenv"));
362362
}
363363

364-
private static bool GetProtocolSupportFromWindowsRegistry(SslProtocols protocol, bool defaultProtocolSupport)
364+
private static bool GetProtocolSupportFromWindowsRegistry(SslProtocols protocol, bool defaultProtocolSupport, bool disabledByDefault = false)
365365
{
366366
string registryProtocolName = protocol switch
367367
{
@@ -381,13 +381,18 @@ private static bool GetProtocolSupportFromWindowsRegistry(SslProtocols protocol,
381381
string serverKey = @$"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\{registryProtocolName}\Server";
382382

383383
object client, server;
384+
object clientDefault, serverDefault;
384385
try
385386
{
386387
client = Registry.GetValue(clientKey, "Enabled", defaultProtocolSupport ? 1 : 0);
387388
server = Registry.GetValue(serverKey, "Enabled", defaultProtocolSupport ? 1 : 0);
388-
if (client is int c && server is int s)
389+
390+
clientDefault = Registry.GetValue(clientKey, "DisabledByDefault", 1);
391+
serverDefault = Registry.GetValue(serverKey, "DisabledByDefault", 1);
392+
393+
if (client is int c && server is int s && clientDefault is int cd && serverDefault is int sd)
389394
{
390-
return c == 1 && s == 1;
395+
return (c == 1 && s == 1) && (!disabledByDefault || (cd == 0 && sd == 0));
391396
}
392397
}
393398
catch (SecurityException)
@@ -436,28 +441,35 @@ private static bool AndroidGetSslProtocolSupport(SslProtocols protocol)
436441

437442
private static bool GetTls10Support()
438443
{
439-
// on Windows, macOS, and Android TLS1.0/1.1 are supported.
444+
// on macOS and Android TLS 1.0 is supported.
440445
if (IsOSXLike || IsAndroid)
441446
{
442447
return true;
443448
}
449+
450+
// Windows depend on registry, enabled by default on all supported versions.
444451
if (IsWindows)
445452
{
446-
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls, true);
453+
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls, defaultProtocolSupport: true);
447454
}
448455

449456
return OpenSslGetTlsSupport(SslProtocols.Tls);
450457
}
451458

452459
private static bool GetTls11Support()
453460
{
454-
// on Windows, macOS, and Android TLS1.0/1.1 are supported.
455461
if (IsWindows)
456462
{
457-
// TLS 1.1 and 1.2 can work on Windows7 but it is not enabled by default.
458-
bool defaultProtocolSupport = !IsWindows7;
459-
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls11, defaultProtocolSupport);
463+
// TLS 1.1 can work on Windows 7 but it is disabled by default.
464+
if (IsWindows7)
465+
{
466+
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls11, defaultProtocolSupport: false, disabledByDefault: true);
467+
}
468+
469+
// It is enabled on other versions unless explicitly disabled.
470+
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls11, defaultProtocolSupport: true);
460471
}
472+
// on macOS and Android TLS 1.1 is supported.
461473
else if (IsOSXLike || IsAndroid)
462474
{
463475
return true;
@@ -468,9 +480,19 @@ private static bool GetTls11Support()
468480

469481
private static bool GetTls12Support()
470482
{
471-
// TLS 1.1 and 1.2 can work on Windows7 but it is not enabled by default.
472-
bool defaultProtocolSupport = !IsWindows7;
473-
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls12, defaultProtocolSupport);
483+
if (IsWindows)
484+
{
485+
// TLS 1.2 can work on Windows 7 but it is disabled by default.
486+
if (IsWindows7)
487+
{
488+
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls12, defaultProtocolSupport: false, disabledByDefault: true);
489+
}
490+
491+
// It is enabled on other versions unless explicitly disabled.
492+
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls12, defaultProtocolSupport: true);
493+
}
494+
495+
return true;
474496
}
475497

476498
private static bool GetTls13Support()

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public static IEnumerable<object[]> OneOrBothUseDefaulData()
7474
}
7575
}
7676

77-
[ActiveIssue("https://github.com/dotnet/runtime/issues/67712")]
7877
[ConditionalTheory]
7978
[MemberData(nameof(OneOrBothUseDefaulData))]
8079
public async Task ClientAndServer_OneOrBothUseDefault_Ok(SslProtocols? clientProtocols, SslProtocols? serverProtocols)

0 commit comments

Comments
 (0)