@@ -53,17 +53,26 @@ internal override bool ValidateCertificate (
53
53
X509CertificateCollection certificates , bool wantsChain , ref X509Chain chain ,
54
54
ref MonoSslPolicyErrors errors , ref int status11 )
55
55
{
56
- if ( certificates == null ) {
57
- errors |= MonoSslPolicyErrors . RemoteCertificateNotAvailable ;
58
- return false ;
59
- }
56
+ var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
60
57
61
- if ( wantsChain )
62
- chain = MNS . SystemCertificateValidator . CreateX509Chain ( certificates ) ;
58
+ var unityTlsChainImpl = chain . Impl as X509ChainImplUnityTls ;
59
+ if ( unityTlsChainImpl == null )
60
+ {
61
+ if ( certificates == null || certificates . Count == 0 ) {
62
+ errors |= MonoSslPolicyErrors . RemoteCertificateNotAvailable ;
63
+ return false ;
64
+ }
63
65
64
- if ( certificates == null || certificates . Count == 0 ) {
65
- errors |= MonoSslPolicyErrors . RemoteCertificateNotAvailable ;
66
- return false ;
66
+ if ( wantsChain )
67
+ chain = MNS . SystemCertificateValidator . CreateX509Chain ( certificates ) ;
68
+ }
69
+ else
70
+ {
71
+ var cert = UnityTls . NativeInterface . unitytls_x509list_get_x509 ( unityTlsChainImpl . NativeCertificateChain , ( size_t ) 0 , & errorState ) ;
72
+ if ( cert . handle == UnityTls . NativeInterface . UNITYTLS_INVALID_HANDLE ) {
73
+ errors |= MonoSslPolicyErrors . RemoteCertificateNotAvailable ;
74
+ return false ;
75
+ }
67
76
}
68
77
69
78
// fixup targetHost name by removing port
@@ -73,10 +82,9 @@ internal override bool ValidateCertificate (
73
82
targetHost = targetHost . Substring ( 0 , pos ) ;
74
83
}
75
84
76
- // convert cert to native
77
- var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
78
- var certificatesNative = UnityTls . NativeInterface . unitytls_x509list_create ( & errorState ) ;
85
+ // convert cert to native or extract from unityTlsChainImpl.
79
86
var result = UnityTls . unitytls_x509verify_result . UNITYTLS_X509VERIFY_NOT_DONE ;
87
+ UnityTls . unitytls_x509list * certificatesNative = null ;
80
88
try
81
89
{
82
90
// Things the validator provides that we might want to make use of here:
@@ -85,28 +93,40 @@ internal override bool ValidateCertificate (
85
93
//validator.Settings.CertificateValidationTime
86
94
//validator.Settings.CertificateSearchPaths // currently only used by MonoBtlsProvider
87
95
88
- CertHelper . AddCertificatesToNativeChain ( certificatesNative , certificates , & errorState ) ;
89
- var certificatesNativeRef = UnityTls . NativeInterface . unitytls_x509list_get_ref ( certificatesNative , & errorState ) ;
96
+ UnityTls . unitytls_x509list_ref certificatesNativeRef ;
97
+ if ( unityTlsChainImpl == null )
98
+ {
99
+ certificatesNative = UnityTls . NativeInterface . unitytls_x509list_create ( & errorState ) ;
100
+ CertHelper . AddCertificatesToNativeChain ( certificatesNative , certificates , & errorState ) ;
101
+ certificatesNativeRef = UnityTls . NativeInterface . unitytls_x509list_get_ref ( certificatesNative , & errorState ) ;
102
+ }
103
+ else
104
+ certificatesNativeRef = unityTlsChainImpl . NativeCertificateChain ;
105
+
90
106
var targetHostUtf8 = Encoding . UTF8 . GetBytes ( targetHost ) ;
91
107
92
108
if ( validator . Settings . TrustAnchors != null ) {
93
- var trustCAnative = UnityTls . NativeInterface . unitytls_x509list_create ( & errorState ) ;
94
- CertHelper . AddCertificatesToNativeChain ( trustCAnative , validator . Settings . TrustAnchors , & errorState ) ;
95
- var trustCAnativeRef = UnityTls . NativeInterface . unitytls_x509list_get_ref ( certificatesNative , & errorState ) ;
96
-
97
- fixed ( byte * targetHostUtf8Ptr = targetHostUtf8 ) {
98
- result = UnityTls . NativeInterface . unitytls_x509verify_explicit_ca ( certificatesNativeRef , trustCAnativeRef , targetHostUtf8Ptr , ( size_t ) targetHostUtf8 . Length , null , null , & errorState ) ;
109
+ UnityTls . unitytls_x509list * trustCAnative = null ;
110
+ try
111
+ {
112
+ trustCAnative = UnityTls . NativeInterface . unitytls_x509list_create ( & errorState ) ;
113
+ CertHelper . AddCertificatesToNativeChain ( trustCAnative , validator . Settings . TrustAnchors , & errorState ) ;
114
+ var trustCAnativeRef = UnityTls . NativeInterface . unitytls_x509list_get_ref ( trustCAnative , & errorState ) ;
115
+
116
+ fixed ( byte * targetHostUtf8Ptr = targetHostUtf8 ) {
117
+ result = UnityTls . NativeInterface . unitytls_x509verify_explicit_ca ( certificatesNativeRef , trustCAnativeRef , targetHostUtf8Ptr , ( size_t ) targetHostUtf8 . Length , null , null , & errorState ) ;
118
+ }
119
+ }
120
+ finally {
121
+ UnityTls . NativeInterface . unitytls_x509list_free ( trustCAnative ) ;
99
122
}
100
-
101
- UnityTls . NativeInterface . unitytls_x509list_free ( trustCAnative ) ;
102
123
} else {
103
124
fixed ( byte * targetHostUtf8Ptr = targetHostUtf8 ) {
104
125
result = UnityTls . NativeInterface . unitytls_x509verify_default_ca ( certificatesNativeRef , targetHostUtf8Ptr , ( size_t ) targetHostUtf8 . Length , null , null , & errorState ) ;
105
126
}
106
127
}
107
128
}
108
- finally
109
- {
129
+ finally {
110
130
UnityTls . NativeInterface . unitytls_x509list_free ( certificatesNative ) ;
111
131
}
112
132
0 commit comments