選單
GSS 技術部落格
在這個園地裡我們將從技術、專案管理、客戶對談面和大家分享我們多年的經驗,希望大家不管是喜歡或是有意見,都可以回饋給我們,讓我們有機會和大家對話並一起成長!
若有任何問題請來信:gss_crm@gss.com.tw
4 分鐘閱讀時間 (866 個字)

AuthenticationException: Authentication failed, see inner exception. Win32Exception: 接收到的訊息超出預期或格式不正確。WebException: 要求已經中止: 無法建立 SSL/TLS 的安全通道。

markus-spiske-bMvuh0YQQ68-unsplash-2

前言

今天同事詢問透過 .NET Core HttpClient 去 Call Https 網頁時,會噴 The SSL connection could not be established Win32Exception (0x80090326): 接收到的訊息超出預期或格式不正確 的錯誤,詳細如下,

System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
—> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
—> System.ComponentModel.Win32Exception (0x80090326): 接收到的訊息超出預期或格式不正確。
— End of inner exception stack trace —
at System.Net.Security.SslStream.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, ExceptionDispatchInfo exception)
at System.Net.Security.SslStream.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.PartialFrameCallback(AsyncProtocolRequest asyncRequest)

研究

在網路上有找到TLS handshake failures are not clear with SChannel這篇有提到似乎是因為 Cipher 的問題。
於是使用以下的 Console 程式在本機上測試卻沒有任何問題,

static async Task Main(string[] args)
{
  var client = new HttpClient();
  var response =
  await client.GetAsync("https://store.gsscloud.com/");
  response.EnsureSuccessStatusCode();
  string responseBody = await response.Content.ReadAsStringAsync();
  Console.WriteLine(responseBody);
}

拿到正式機去執行就真的發生一樣錯誤。
於是到SSL Server Test測試一下該網站 Support 幾種 Cipher ,結果只 Support 3 種 384 的。
於是透過IIS Crypto Tool 將本機 384 Bits 的 Cipher 都取消,重開機再執行測試程式真的發生一樣的錯誤。

.NET CORE 的程式會噴以下的錯誤,

NET Core Call Https Error

System.Net.Http.HttpRequestException
HResult=0x80131501
Message=The SSL connection could not be established, see inner exception.
Source=System.Net.Http
StackTrace:
at System.Net.Http.ConnectHelper.d__4.MoveNext()

 

內部例外狀況 1:

AuthenticationException: Authentication failed, see inner exception.

 

內部例外狀況 2:

Win32Exception: 接收到的訊息超出預期或格式不正確。

 

.NET FRAMEWORK 的程式,錯誤訊息如下,

NET Framework Call Https Error

內部例外狀況 1:
WebException: 要求已經中止: 無法建立 SSL/TLS 的安全通道。

 

而透過 Wireshark 來看,錯誤是Handshake Failure
Wireshark Https Handshake Failure

即然猜到了真正原因,於是就將 Server 需要的 Cipher 再加進去。
Add Cipher

重開機後,再執行程式就沒有問題了,透過 Wireshark 來看,在 Client Hello 送上去的 Cipher 就有包含 Server 允許的 Cipher ,也正常出現 Server Hello
Https Client Hello And Server Hello

後來同事使用IIS Crypto Tool在正式機上檢視,果然沒有那些 384 的 Cipher。

WEBEXCEPTION: 基礎連接已關閉: 傳送時發生未預期的錯誤。 遠端主機已強制關閉一個現存的連線

有時如果使用不允許的通訊協定,例如 Server 只允許 TLSv1.2 ,Client 給 TLSv1 就會產生以下的錯誤,

System.Net.Http.HttpRequestException
HResult=0x80131500
Message=傳送要求時發生錯誤。

 

內部例外狀況 1:

WebException: 基礎連接已關閉: 傳送時發生未預期的錯誤。

 

內部例外狀況 2:

IOException: 無法從傳輸連接讀取資料: 遠端主機已強制關閉一個現存的連線。

 

內部例外狀況 3:
SocketException: 遠端主機已強制關閉一個現存的連線。
遠端主機已強制關閉一個現存的連線

而透過Wireshark 來看,就可以看到 GG 在 Reset,因為它的 Client Hello 走的是 TLSV1
https Server Reset

這錯大部份出現在 .NET Framework 身上,因為 .NET Framework 預設是 TLSV1.0 。
所以程式要加上

 

System.Net.ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

 


WIRESHARK 使用 FILTER

透過 Wireshark 來錄封包時,有時資料太多不好找,這時可以使用 Filter 的功能,例如我本機是 10.211.55.3 ,而 Server 是 54.238.55.188
就可以下以下的 Filter來看這2邊的資訊,

(ip.src == 54.238.55.188 or ip.src == 10.211.55.3 ) and (ip.dst == 54.238.55.188 or ip.dst == 10.211.55.3)

Wireshark Filter

 

以上是最近遇到程式呼叫 https 遇到問題的整理,希望對大家有幫助。
如果是在開發機上使用自簽憑證則可以參考Allowing Untrusted SSL Certificates with HttpClient

參考資訊

TLS handshake failures are not clear with SChannel
SSL Server Test
IIS Crypto Tool
Allowing Untrusted SSL Certificates with HttpClient

透過 Aspose.Words 讓設定 SectionBreakOddPage(自下個奇數頁起) 的...
Uncaught DOMException: Blocked a frame with origin

相關文章

 

評論

尚無評論
已經注冊了? 這裡登入
Guest
2024/05/07, 週二

Captcha 圖像