-
Notifications
You must be signed in to change notification settings - Fork 5.3k
add TLS 1.3 support to WinHttp #58590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -44,6 +44,7 @@ public class WinHttpHandler : HttpMessageHandler | |||||
|
|
||||||
| private static readonly StringWithQualityHeaderValue s_gzipHeaderValue = new StringWithQualityHeaderValue("gzip"); | ||||||
| private static readonly StringWithQualityHeaderValue s_deflateHeaderValue = new StringWithQualityHeaderValue("deflate"); | ||||||
| private static readonly Lazy<bool> s_supportsTls13 = new Lazy<bool>(CheckTls13Support()); | ||||||
|
|
||||||
| [ThreadStatic] | ||||||
| private static StringBuilder? t_requestHeadersBuilder; | ||||||
|
|
@@ -1156,6 +1157,7 @@ private void SetSessionHandleConnectionOptions(SafeWinHttpHandle sessionHandle) | |||||
|
|
||||||
| private void SetSessionHandleTlsOptions(SafeWinHttpHandle sessionHandle) | ||||||
| { | ||||||
| const SslProtocols Tls13 = (SslProtocols)12288; // enum is missing in .NET Standard | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took the value from documentation. But I don't really care. It seems like the SP_PROT is platform specific mapping that happens to be the same on Windows. |
||||||
| uint optionData = 0; | ||||||
| SslProtocols sslProtocols = | ||||||
| (_sslProtocols == SslProtocols.None) ? SecurityProtocol.DefaultSecurityProtocols : _sslProtocols; | ||||||
|
|
@@ -1187,10 +1189,13 @@ private void SetSessionHandleTlsOptions(SafeWinHttpHandle sessionHandle) | |||||
| optionData |= Interop.WinHttp.WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2; | ||||||
| } | ||||||
|
|
||||||
| // As of Win10RS5 there's no public constant for WinHTTP + TLS 1.3 | ||||||
| // This library builds against netstandard, which doesn't define the Tls13 enum field. | ||||||
| // Set this only if supported by WinHttp version. | ||||||
| if (s_supportsTls13.Value && (sslProtocols & Tls13) != 0) | ||||||
| { | ||||||
| optionData |= Interop.WinHttp.WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3; | ||||||
| } | ||||||
|
|
||||||
| // If only unknown values (e.g. TLS 1.3) were asked for, report ERROR_INVALID_PARAMETER. | ||||||
| // If only unknown values were asked for, report ERROR_INVALID_PARAMETER. | ||||||
| if (optionData == 0) | ||||||
| { | ||||||
| throw WinHttpException.CreateExceptionUsingError( | ||||||
|
|
@@ -1201,6 +1206,30 @@ private void SetSessionHandleTlsOptions(SafeWinHttpHandle sessionHandle) | |||||
| SetWinHttpOption(sessionHandle, Interop.WinHttp.WINHTTP_OPTION_SECURE_PROTOCOLS, ref optionData); | ||||||
| } | ||||||
|
|
||||||
| private static bool CheckTls13Support() | ||||||
| { | ||||||
| try | ||||||
| { | ||||||
| using (var handler = new WinHttpHandler()) | ||||||
| using (SafeWinHttpHandle sessionHandle = Interop.WinHttp.WinHttpOpen( | ||||||
| IntPtr.Zero, | ||||||
| Interop.WinHttp.WINHTTP_ACCESS_TYPE_NO_PROXY, | ||||||
| Interop.WinHttp.WINHTTP_NO_PROXY_NAME, | ||||||
| Interop.WinHttp.WINHTTP_NO_PROXY_BYPASS, | ||||||
| (int)Interop.WinHttp.WINHTTP_FLAG_ASYNC)) | ||||||
| { | ||||||
| uint optionData = Interop.WinHttp.WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3; | ||||||
|
|
||||||
| handler.SetWinHttpOption(sessionHandle, Interop.WinHttp.WINHTTP_OPTION_SECURE_PROTOCOLS, ref optionData); | ||||||
| return true; | ||||||
| } | ||||||
| } | ||||||
| catch | ||||||
| { | ||||||
| return false; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private void SetSessionHandleTimeoutOptions(SafeWinHttpHandle sessionHandle) | ||||||
| { | ||||||
| if (!Interop.WinHttp.WinHttpSetTimeouts( | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the reason for
12288is that's what we use in the definition:runtime/src/libraries/System.Net.Primitives/ref/System.Net.Primitives.cs
Line 524 in 9f6da55
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that is rather ugly. I wonder why? Perhaps it was auto-generated at some point?
I would still consider that hex values are more developer-friendly for reading ...
BTW: Definition is technically hex-defined:
runtime/src/libraries/System.Net.Primitives/src/System/Net/SecureProtocols/SslEnumTypes.cs
Line 18 in 57bfe47
runtime/src/libraries/Common/src/Interop/Windows/SChannel/Interop.SchProtocols.cs
Lines 31 to 33 in 57bfe47