Skip to content

Commit 7698a9a

Browse files
authored
Fix performance regression in SSL handshake (#66077)
1 parent c125826 commit 7698a9a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/libraries/System.Net.Security/src/System/Net/CertificateValidationPal.Windows.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,20 @@ internal static SslPolicyErrors VerifyCertificateProperties(
4848
SafeFreeCertContext? remoteContext = null;
4949
try
5050
{
51-
// SECPKG_ATTR_REMOTE_CERT_CHAIN can be used even before the TLS handshake completes, which is necessary
52-
// in order to supply the certificate to the client cert selection callback. However, it is not available on
53-
// windows 7, so use the SECPKG_ATTR_REMOTE_CERT_CONTEXT as a fallback option.
54-
if (!SSPIWrapper.QueryContextAttributes_SECPKG_ATTR_REMOTE_CERT_CHAIN(GlobalSSPI.SSPISecureChannel, securityContext, out remoteContext))
51+
// SECPKG_ATTR_REMOTE_CERT_CONTEXT will not succeed before TLS handshake completes. Inside the handshake,
52+
// we need to use (more expensive) SECPKG_ATTR_REMOTE_CERT_CHAIN. That one may be unsupported on older
53+
// versions of windows. In that case, we have no option than to return null.
54+
//
55+
// We can use retrieveCollection to distinguish between in-handshake and after-handshake calls, because
56+
// the collection is retrieved for cert validation purposes after the handshake completes.
57+
if (retrieveCollection) // handshake completed
5558
{
5659
SSPIWrapper.QueryContextAttributes_SECPKG_ATTR_REMOTE_CERT_CONTEXT(GlobalSSPI.SSPISecureChannel, securityContext, out remoteContext);
5760
}
61+
else // in handshake
62+
{
63+
SSPIWrapper.QueryContextAttributes_SECPKG_ATTR_REMOTE_CERT_CHAIN(GlobalSSPI.SSPISecureChannel, securityContext, out remoteContext);
64+
}
5865

5966
if (remoteContext != null && !remoteContext.IsInvalid)
6067
{

0 commit comments

Comments
 (0)