Skip to content

Commit 994793a

Browse files
AndreasReichUnityAlex
authored andcommitted
[unitytls] User facing certificate callbacks contain now certificates from system store
1 parent c1ee661 commit 994793a

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

mcs/class/System/Mono.UnityTls/UnityTlsProvider.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ internal override MNS.MobileAuthenticatedStream CreateSslStream (
4141
return new UnityTlsStream (innerStream, leaveInnerStreamOpen, sslStream, settings, this);
4242
}
4343

44+
static UnityTls.unitytls_x509verify_result x509verify_callback(void* userData, UnityTls.unitytls_x509_ref cert, UnityTls.unitytls_x509verify_result result, UnityTls.unitytls_errorstate* errorState)
45+
{
46+
if (userData != null)
47+
UnityTls.NativeInterface.unitytls_x509list_append ((UnityTls.unitytls_x509list*)userData, cert, errorState);
48+
return result;
49+
}
50+
4451
internal override bool ValidateCertificate (
4552
MNS.ChainValidationHelper validator, string targetHost, bool serverMode,
4653
X509CertificateCollection certificates, bool wantsChain, ref X509Chain chain,
@@ -82,6 +89,8 @@ internal override bool ValidateCertificate (
8289
// convert cert to native or extract from unityTlsChainImpl.
8390
var result = UnityTls.unitytls_x509verify_result.UNITYTLS_X509VERIFY_NOT_DONE;
8491
UnityTls.unitytls_x509list* certificatesNative = null;
92+
UnityTls.unitytls_x509list* finalCertificateChainNative =
93+
chain == null ? null : UnityTls.NativeInterface.unitytls_x509list_create (&errorState);
8594
try
8695
{
8796
// Things the validator provides that we might want to make use of here:
@@ -111,22 +120,36 @@ internal override bool ValidateCertificate (
111120
var trustCAnativeRef = UnityTls.NativeInterface.unitytls_x509list_get_ref (trustCAnative, &errorState);
112121

113122
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
114-
result = UnityTls.NativeInterface.unitytls_x509verify_explicit_ca (certificatesNativeRef, trustCAnativeRef, targetHostUtf8Ptr, (size_t)targetHostUtf8.Length, null, null, &errorState);
123+
result = UnityTls.NativeInterface.unitytls_x509verify_explicit_ca (
124+
certificatesNativeRef, trustCAnativeRef, targetHostUtf8Ptr, (size_t)targetHostUtf8.Length, x509verify_callback, finalCertificateChainNative, &errorState);
115125
}
116126
}
117127
finally {
118128
UnityTls.NativeInterface.unitytls_x509list_free (trustCAnative);
119129
}
120130
} else {
121131
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
122-
result = UnityTls.NativeInterface.unitytls_x509verify_default_ca (certificatesNativeRef, targetHostUtf8Ptr, (size_t)targetHostUtf8.Length, null, null, &errorState);
132+
result = UnityTls.NativeInterface.unitytls_x509verify_default_ca (
133+
certificatesNativeRef, targetHostUtf8Ptr, (size_t)targetHostUtf8.Length, x509verify_callback, finalCertificateChainNative, &errorState);
123134
}
124135
}
125136
}
137+
catch {
138+
UnityTls.NativeInterface.unitytls_x509list_free (finalCertificateChainNative);
139+
throw;
140+
}
126141
finally {
127142
UnityTls.NativeInterface.unitytls_x509list_free (certificatesNative);
128143
}
129144

145+
if (finalCertificateChainNative != null) {
146+
chain?.Dispose();
147+
chain = new X509Chain(new X509ChainImplUnityTls(
148+
UnityTls.NativeInterface.unitytls_x509list_get_ref (finalCertificateChainNative, &errorState),
149+
reverseOrder: true // the verify callback starts with the root and ends with the leaf. That's the opposite of chain ordering.
150+
));
151+
}
152+
130153
errors = UnityTlsConversions.VerifyResultToPolicyErrror(result);
131154
// There should be a status per certificate, but once again we're following closely the BTLS implementation
132155
// https://github.com/mono/mono/blob/1553889bc54f87060158febca7e6b8b9910975f8/mcs/class/System/Mono.Btls/MonoBtlsProvider.cs#L180

mcs/class/System/Mono.UnityTls/X509ChainImplUnityTls.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ class X509ChainImplUnityTls : X509ChainImpl
1717
private UnityTls.unitytls_x509list_ref nativeCertificateChain;
1818
private X509ChainPolicy policy = new X509ChainPolicy ();
1919
private List<X509ChainStatus> chainStatusList;
20+
private bool reverseOrder;
2021

21-
internal X509ChainImplUnityTls (UnityTls.unitytls_x509list_ref nativeCertificateChain)
22+
internal X509ChainImplUnityTls (UnityTls.unitytls_x509list_ref nativeCertificateChain, bool reverseOrder = false)
2223
{
2324
this.elements = null;
2425
this.nativeCertificateChain = nativeCertificateChain;
26+
this.reverseOrder = reverseOrder;
2527
}
2628

2729
public override bool IsValid {
@@ -57,6 +59,13 @@ public override X509ChainElementCollection ChainElements {
5759
}
5860
}
5961

62+
if (reverseOrder) {
63+
var reversed = new X509ChainElementCollection ();
64+
for (int i=elements.Count - 1; i>=0; --i)
65+
reversed.Add(elements[i].Certificate);
66+
elements = reversed;
67+
}
68+
6069
return elements;
6170
}
6271
}

0 commit comments

Comments
 (0)