Skip to content

Commit

Permalink
improve Tls12 detection on Windows7 (#67935)
Browse files Browse the repository at this point in the history
* improve Tls12 detection on Windows7

* fix Tls11

* feedback from review
  • Loading branch information
wfurt authored Apr 13, 2022
1 parent f216e77 commit 16b7255
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ private static bool GetIsInContainer()
return (IsLinux && File.Exists("/.dockerenv"));
}

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

object client, server;
object clientDefault, serverDefault;
try
{
client = Registry.GetValue(clientKey, "Enabled", defaultProtocolSupport ? 1 : 0);
server = Registry.GetValue(serverKey, "Enabled", defaultProtocolSupport ? 1 : 0);
if (client is int c && server is int s)

clientDefault = Registry.GetValue(clientKey, "DisabledByDefault", 1);
serverDefault = Registry.GetValue(serverKey, "DisabledByDefault", 1);

if (client is int c && server is int s && clientDefault is int cd && serverDefault is int sd)
{
return c == 1 && s == 1;
return (c == 1 && s == 1) && (!disabledByDefault || (cd == 0 && sd == 0));
}
}
catch (SecurityException)
Expand Down Expand Up @@ -436,28 +441,35 @@ private static bool AndroidGetSslProtocolSupport(SslProtocols protocol)

private static bool GetTls10Support()
{
// on Windows, macOS, and Android TLS1.0/1.1 are supported.
// on macOS and Android TLS 1.0 is supported.
if (IsOSXLike || IsAndroid)
{
return true;
}

// Windows depend on registry, enabled by default on all supported versions.
if (IsWindows)
{
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls, true);
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls, defaultProtocolSupport: true);
}

return OpenSslGetTlsSupport(SslProtocols.Tls);
}

private static bool GetTls11Support()
{
// on Windows, macOS, and Android TLS1.0/1.1 are supported.
if (IsWindows)
{
// TLS 1.1 and 1.2 can work on Windows7 but it is not enabled by default.
bool defaultProtocolSupport = !IsWindows7;
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls11, defaultProtocolSupport);
// TLS 1.1 can work on Windows 7 but it is disabled by default.
if (IsWindows7)
{
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls11, defaultProtocolSupport: false, disabledByDefault: true);
}

// It is enabled on other versions unless explicitly disabled.
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls11, defaultProtocolSupport: true);
}
// on macOS and Android TLS 1.1 is supported.
else if (IsOSXLike || IsAndroid)
{
return true;
Expand All @@ -468,9 +480,19 @@ private static bool GetTls11Support()

private static bool GetTls12Support()
{
// TLS 1.1 and 1.2 can work on Windows7 but it is not enabled by default.
bool defaultProtocolSupport = !IsWindows7;
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls12, defaultProtocolSupport);
if (IsWindows)
{
// TLS 1.2 can work on Windows 7 but it is disabled by default.
if (IsWindows7)
{
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls12, defaultProtocolSupport: false, disabledByDefault: true);
}

// It is enabled on other versions unless explicitly disabled.
return GetProtocolSupportFromWindowsRegistry(SslProtocols.Tls12, defaultProtocolSupport: true);
}

return true;
}

private static bool GetTls13Support()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public static IEnumerable<object[]> OneOrBothUseDefaulData()
}
}

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

0 comments on commit 16b7255

Please sign in to comment.