-
Notifications
You must be signed in to change notification settings - Fork 5.1k
[QUIC] Certificate name validation #56175
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/libraries/Common/src/System/Net/Security/CertificateValidation.Windows.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.Win32.SafeHandles; | ||
using System.Diagnostics; | ||
using System.Net.Security; | ||
using System.Runtime.InteropServices; | ||
using System.Security.Cryptography; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Security.Principal; | ||
|
||
namespace System.Net | ||
{ | ||
internal static partial class CertificateValidation | ||
{ | ||
internal static SslPolicyErrors BuildChainAndVerifyProperties(X509Chain chain, X509Certificate2 remoteCertificate, bool checkCertName, bool isServer, string? hostName) | ||
{ | ||
SslPolicyErrors sslPolicyErrors = SslPolicyErrors.None; | ||
|
||
bool chainBuildResult = chain.Build(remoteCertificate); | ||
if (!chainBuildResult // Build failed on handle or on policy. | ||
&& chain.SafeHandle!.DangerousGetHandle() == IntPtr.Zero) // Build failed to generate a valid handle. | ||
{ | ||
throw new CryptographicException(Marshal.GetLastPInvokeError()); | ||
} | ||
|
||
if (checkCertName) | ||
{ | ||
unsafe | ||
{ | ||
uint status = 0; | ||
|
||
var eppStruct = new Interop.Crypt32.SSL_EXTRA_CERT_CHAIN_POLICY_PARA() | ||
{ | ||
cbSize = (uint)sizeof(Interop.Crypt32.SSL_EXTRA_CERT_CHAIN_POLICY_PARA), | ||
// Authenticate the remote party: (e.g. when operating in server mode, authenticate the client). | ||
dwAuthType = isServer ? Interop.Crypt32.AuthType.AUTHTYPE_CLIENT : Interop.Crypt32.AuthType.AUTHTYPE_SERVER, | ||
fdwChecks = 0, | ||
pwszServerName = null | ||
}; | ||
|
||
var cppStruct = new Interop.Crypt32.CERT_CHAIN_POLICY_PARA() | ||
{ | ||
cbSize = (uint)sizeof(Interop.Crypt32.CERT_CHAIN_POLICY_PARA), | ||
dwFlags = 0, | ||
pvExtraPolicyPara = &eppStruct | ||
}; | ||
|
||
fixed (char* namePtr = hostName) | ||
{ | ||
eppStruct.pwszServerName = namePtr; | ||
cppStruct.dwFlags |= | ||
(Interop.Crypt32.CertChainPolicyIgnoreFlags.CERT_CHAIN_POLICY_IGNORE_ALL & | ||
~Interop.Crypt32.CertChainPolicyIgnoreFlags.CERT_CHAIN_POLICY_IGNORE_INVALID_NAME_FLAG); | ||
|
||
SafeX509ChainHandle chainContext = chain.SafeHandle!; | ||
status = Verify(chainContext, ref cppStruct); | ||
if (status == Interop.Crypt32.CertChainPolicyErrors.CERT_E_CN_NO_MATCH) | ||
{ | ||
sslPolicyErrors |= SslPolicyErrors.RemoteCertificateNameMismatch; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!chainBuildResult) | ||
{ | ||
sslPolicyErrors |= SslPolicyErrors.RemoteCertificateChainErrors; | ||
} | ||
|
||
return sslPolicyErrors; | ||
} | ||
|
||
private static unsafe uint Verify(SafeX509ChainHandle chainContext, ref Interop.Crypt32.CERT_CHAIN_POLICY_PARA cpp) | ||
{ | ||
Interop.Crypt32.CERT_CHAIN_POLICY_STATUS status = default; | ||
status.cbSize = (uint)sizeof(Interop.Crypt32.CERT_CHAIN_POLICY_STATUS); | ||
|
||
bool errorCode = | ||
Interop.Crypt32.CertVerifyCertificateChainPolicy( | ||
(IntPtr)Interop.Crypt32.CertChainPolicy.CERT_CHAIN_POLICY_SSL, | ||
chainContext, | ||
ref cpp, | ||
ref status); | ||
|
||
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(chainContext, $"CertVerifyCertificateChainPolicy returned: {errorCode}. Status: {status.dwError}"); | ||
return status.dwError; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
do we need this? looks like extra.
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.
Yes we need this, it's used by one of the included interop source files:
runtime/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.SslCtx.cs
Line 43 in d148c34
https://github.com/dotnet/runtime/pull/56175/files#diff-1d4ce147edca0ce81f312b96fce76ac52eb02eb6d04e660817e3b47230edc794R64