@@ -13,8 +13,9 @@ internal static class CertificateValidation
1313 {
1414 private static readonly IdnMapping s_idnMapping = new IdnMapping ( ) ;
1515
16- // WARNING: This function will do the verification using OpenSSL. If the intention is to use OS function, caller should use CertificatePal interface.
17- internal static SslPolicyErrors BuildChainAndVerifyProperties ( X509Chain chain , X509Certificate2 remoteCertificate , bool checkCertName , bool _ /*isServer*/ , string ? hostName , Span < byte > certificateBuffer )
16+ #pragma warning disable IDE0060
17+ internal static SslPolicyErrors BuildChainAndVerifyProperties ( X509Chain chain , X509Certificate2 remoteCertificate , bool checkCertName , bool isServer , string ? hostName , Span < byte > certificateBuffer )
18+ #pragma warning restore IDE0060
1819 {
1920 SslPolicyErrors errors = chain . Build ( remoteCertificate ) ?
2021 SslPolicyErrors . None :
@@ -30,53 +31,19 @@ internal static SslPolicyErrors BuildChainAndVerifyProperties(X509Chain chain, X
3031 return errors | SslPolicyErrors . RemoteCertificateNameMismatch ;
3132 }
3233
33- SafeX509Handle certHandle ;
34- unsafe
34+ bool match ;
35+
36+ if ( IPAddress . TryParse ( hostName , out _ ) )
3537 {
36- if ( certificateBuffer . Length > 0 )
37- {
38- fixed ( byte * pCert = certificateBuffer )
39- {
40- certHandle = Interop . Crypto . DecodeX509 ( ( IntPtr ) pCert , certificateBuffer . Length ) ;
41- }
42- }
43- else
44- {
45- // We dont't have DER encoded buffer.
46- byte [ ] der = remoteCertificate . Export ( X509ContentType . Cert ) ;
47- fixed ( byte * pDer = der )
48- {
49- certHandle = Interop . Crypto . DecodeX509 ( ( IntPtr ) pDer , der . Length ) ;
50- }
51- }
38+ match = remoteCertificate . MatchesHostname ( hostName ) ;
5239 }
53-
54- int hostNameMatch ;
55- using ( certHandle )
40+ else
5641 {
57- IPAddress ? hostnameAsIp ;
58- if ( IPAddress . TryParse ( hostName , out hostnameAsIp ) )
59- {
60- byte [ ] addressBytes = hostnameAsIp . GetAddressBytes ( ) ;
61- hostNameMatch = Interop . Crypto . CheckX509IpAddress ( certHandle , addressBytes , addressBytes . Length , hostName , hostName . Length ) ;
62- }
63- else
64- {
65- // The IdnMapping converts Unicode input into the IDNA punycode sequence.
66- // It also does host case normalization. The bypass logic would be something
67- // like "all characters being within [a-z0-9.-]+"
68- string matchName = s_idnMapping . GetAscii ( hostName ) ;
69- hostNameMatch = Interop . Crypto . CheckX509Hostname ( certHandle , matchName , matchName . Length ) ;
70-
71- if ( hostNameMatch < 0 )
72- {
73- throw Interop . Crypto . CreateOpenSslCryptographicException ( ) ;
74- }
75- }
42+ string matchName = s_idnMapping . GetAscii ( hostName ) ;
43+ match = remoteCertificate . MatchesHostname ( matchName ) ;
7644 }
7745
78- Debug . Assert ( hostNameMatch == 0 || hostNameMatch == 1 , $ "Expected 0 or 1 from CheckX509Hostname, got { hostNameMatch } ") ;
79- return hostNameMatch == 1 ?
46+ return match ?
8047 errors :
8148 errors | SslPolicyErrors . RemoteCertificateNameMismatch ;
8249 }
0 commit comments