@@ -13,8 +13,9 @@ internal static class CertificateValidation
13
13
{
14
14
private static readonly IdnMapping s_idnMapping = new IdnMapping ( ) ;
15
15
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
18
19
{
19
20
SslPolicyErrors errors = chain . Build ( remoteCertificate ) ?
20
21
SslPolicyErrors . None :
@@ -30,53 +31,19 @@ internal static SslPolicyErrors BuildChainAndVerifyProperties(X509Chain chain, X
30
31
return errors | SslPolicyErrors . RemoteCertificateNameMismatch ;
31
32
}
32
33
33
- SafeX509Handle certHandle ;
34
- unsafe
34
+ bool match ;
35
+
36
+ if ( IPAddress . TryParse ( hostName , out _ ) )
35
37
{
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 ) ;
52
39
}
53
-
54
- int hostNameMatch ;
55
- using ( certHandle )
40
+ else
56
41
{
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 ) ;
76
44
}
77
45
78
- Debug . Assert ( hostNameMatch == 0 || hostNameMatch == 1 , $ "Expected 0 or 1 from CheckX509Hostname, got { hostNameMatch } ") ;
79
- return hostNameMatch == 1 ?
46
+ return match ?
80
47
errors :
81
48
errors | SslPolicyErrors . RemoteCertificateNameMismatch ;
82
49
}
0 commit comments