建立SSL\TLS连接(X509Chain.Build())需要太长时间

hku*_*bko 6 .net c# ssl system.net.httpwebrequest

我发现当我使用HttpWebRequest建立SSL\TLS连接时,调用时需要近30秒

  request.GetRequestStream()
Run Code Online (Sandbox Code Playgroud)

当我启用了跟踪启用堆栈跟踪时,我发现2s会找到poxy,所以我在app.config中禁用了它:

<system.net>
 <defaultProxy enabled="false" useDefaultCredentials="false">
  <proxy/>
  <bypasslist/>
  <module/>
 </defaultProxy>
</system.net>
Run Code Online (Sandbox Code Playgroud)

下一个接近28s的点是在

   at System.Net.Security.SecureChannel.AcquireClientCredentials(Byte[]& thumbPrint)
Run Code Online (Sandbox Code Playgroud)

在检查方法体后,我发现了对X509Chain.Build()的调用,并且花了将近25秒来构建证书链.

有趣的是,当你构造新的HttpWebReqest并再次尝试(没有app重启)时,执行请求需要几个ms.

谁能建议做什么?缓存请求不是一个选项,它应该从app运行快速.

更新:

我在X509Chain.BuildChain()中发现需要30秒的调用,它是:

if (!CAPISafe.CertGetCertificateChain(hChainEngine, pCertContext, ref pTime, invalidHandle, ref cert_chain_para, dwFlags, IntPtr.Zero, ref ppChainContext))
Run Code Online (Sandbox Code Playgroud)

在CAPISafe中声明的方法为:

[DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool CertGetCertificateChain([In] IntPtr hChainEngine, [In] SafeCertContextHandle pCertContext, [In] ref System.Runtime.InteropServices.ComTypes.FILETIME pTime, [In] SafeCertStoreHandle hAdditionalStore, [In] ref CAPIBase.CERT_CHAIN_PARA pChainPara, [In] uint dwFlags, [In] IntPtr pvReserved, [In, Out] ref SafeCertChainHandle ppChainContext);
Run Code Online (Sandbox Code Playgroud)

所以,它是加密API函数CertGetCertificateChain 还是不知道,下一步做什么...

更新:

我试图禁用CRL和OCSP检查,仍然没有效果:

  1. 添加到App.config

    <runtime>
      <generatePublisherEvidence enabled="false"/>
    </runtime>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 机器范围:控制面板 - > Internet选项 - >高级 - >在安全性下,取消选中检查发布者的证书吊销选项

  3. 在注册表中:

    [HKEY_USERS\S-1-5-20\Software\Microsoft\Windows\CurrentVersion\WinTrust\Trust Providers\Software Publishing]"State"= dword:00023e00

hku*_*bko 5

最后,我找到了问题的根源。尝试从以下位置下载证书信任列表时,我在事件日志中启用了CAPI2日志记录并发现了NetworkTimeoutException:

http://www.download.windowsupdate.com/msdownload/update/v3/static/trustedr/zh-CN/authrootstl.cab

因此,这就是防火墙问题。您可以阅读有关调查过程和所用技术的博客文章。