c# - 安装 .net Framework 4.7.2 时,4.5.1 应用程序中的 TLS1.2 协商失败
问题描述
我们目前在使用基于 Web 的应用程序时遇到问题。它可以在任何操作系统 win2k12 R2 和 IIS 上运行良好。该应用程序是针对 .net Framework 4.5.1 编译的。虽然我们知道较新的 .NET Framework 版本,但由于一些不方便的 3rd 方依赖项,我们在更新方面遇到了困难。
最近,微软发布了隐式安装 .NET Framework 4.7.2 的 KB。安装了这些更新的客户现在面临着一个重大问题。
以下代码虽然过于简单,但包含所有必要信息。
private HttpWebResponse GetResponse(HttpWebRequest httpWebRequest)
{
lock (Statics._lockObject)
{
HttpWebResponse httpResponse = null;
SecurityProtocolType tempProtocol = ServicePointManager.SecurityProtocol;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // Magic Number equals SecurityProtocolType.Tls1.2
ServicePointManager.ServerCertificateValidationCallback = CheckSSL;
try
{
httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
}
finally
{
ServicePointManager.SecurityProtocol = tempProtocol;
ServicePointManager.ServerCertificateValidationCallback = null;
}
return httpResponse;
}
}
在安装了 .NET Framework 4.7.2 的系统上执行此操作时,我们在GetResponse()
调用中收到以下错误:
“底层连接已关闭:发送时发生意外错误。”
at System.Net.HttpWebRequest.GetResponse()
和内部异常:
“身份验证失败,因为远程方已关闭传输流。”
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.WriteHeaders(Boolean async)
卸载上述 KB 并因此卸载 .NET Framework 4.7.2 确实有帮助,但对于我们的客户群来说是没有选择的。
到目前为止,我们尝试的其他选项是:
设置注册表项如下:
[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\.NETFramework\v4.0.30319]
"SystemDefaultTlsVersions"=dword:00000001
"SchUseStrongCrypto"=dword:00000001
同时在 ServicePointManager.SecurityProtocol 属性中注释掉 tls 1.2 的显式设置(来源:https ://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls#schusestrongcrypto )
设置 appcontextswitches:
Switch.System.ServiceModel.DontEnableSystemDefaultTlsVersions Switch.System.ServiceModel.DisableUsingServicePointManagerSecurityProtocols
(来源:https ://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/appcontextswitchoverrides-element )
到目前为止,我们无法对抛出的异常进行任何更改。谁能指出我们正确的方向?
非常感谢您!
更新:
由于评论中的建议,我们做了一些wireshark跟踪。
在没有安装 Framework 4.7.2 的情况下,我们的应用程序与服务器应用程序协商成功 - 正如我们的代码示例所建议的那样 - tls 1.2 成功:
之后,我们将代码更改为专门设置 tls 1.1 只是为了确认设置 ServicePointManager.SecurityProtocol 会产生影响,我们看到,毫不奇怪,应用程序无法协商 tls 1.1:
现在有趣的部分,我们再次安装了.NET Framework 4.7.2,重新设置了代码来具体说
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
并再次追踪电话。现在,令我们惊讶的是,该应用程序尝试协商 tls 1.0,这当然失败了:
之后,我们通过以下方式专门禁用了操作系统上的 tls 1.0:
`HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Client
"Enabled"=dword:0000000
"DisabledByDefault"=dword:00000001 `
再次查看 Wireshark,应用程序尝试协商 ssl3.0。回到注册表并专门禁用该注册表,Wireshark 中不再显示 ClientHello 并且异常说,找不到相互算法来连接到服务器。
解决方案
尝试设置这些注册表设置:
Key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319
Value: SchUseStrongCrypto
Data: 1
Key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Server
Value: Enabled
Data: 0
Value: DisabledByDefault
Data: 1
之后,在进行 HTTPWebRequest 之前,包括 ServicePointManager 更改:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
这应该强制您的应用程序使用 TLS 1.2。我没有看到任何说您一次尝试了上述所有方法,并且这样做对我们有用。
推荐阅读
- docker - Docker compose 错误:nginx:在 /etc/nginx/conf.d/default.conf:21 的上游“app”中找不到 [emerg] 主机
- sass - 在 Grav CMS 的自定义主题中设置 SCSS 编译
- json - Terraform 可选的 jsonencode 属性
- python - 为什么 html 看不到来自烧瓶路线的新图像?
- c++ - 创建传递值的数组大小会产生垃圾值
- django - 如何修复(字典更新序列元素 #0 的长度为 211;需要 2)错误?
- activerecord - Yii1中如何在活动记录查询中使用列名
- python - http.client.InvalidURL 问题
- javascript - 不明白这段代码的执行流程是怎样的
- javascript - 是否可以使用 Vue-router 将屏幕加载到选项卡中?