Skip to content

Commit cdaebd8

Browse files
authored
Merge pull request #784 from Unity-Technologies/unity-master-unitytls
Unitytls integration
2 parents 1e12465 + 5a28379 commit cdaebd8

File tree

12 files changed

+1112
-0
lines changed

12 files changed

+1112
-0
lines changed

mcs/class/System/Mono.Net.Security/MonoTlsProviderFactory.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ internal static void Debug (string message, params object[] args)
211211

212212
#endregion
213213

214+
internal static readonly Guid UnityTlsId = new Guid("06414A97-74F6-488F-877B-A6CA9BBEB82E");
214215
internal static readonly Guid AppleTlsId = new Guid ("981af8af-a3a3-419a-9f01-a518e3a17c1c");
215216
internal static readonly Guid BtlsId = new Guid ("432d18c9-9348-4b90-bfbf-9f2a10e1f15b");
216217
internal static readonly Guid LegacyId = new Guid ("809e77d5-56cc-4da8-b9f0-45e65ba9cceb");
@@ -226,6 +227,16 @@ static void InitializeProviderRegistration ()
226227
providerRegistration = new Dictionary<string,Tuple<Guid,string>> ();
227228
providerCache = new Dictionary<Guid,MSI.MonoTlsProvider> ();
228229

230+
#if UNITY
231+
if (Mono.Unity.UnityTls.IsSupported)
232+
{
233+
var unityTlsEntry = new Tuple<Guid,String> (UnityTlsId, "Mono.Unity.UnityTlsProvider");
234+
providerRegistration.Add ("default", unityTlsEntry);
235+
providerRegistration.Add ("unitytls", unityTlsEntry);
236+
return;
237+
}
238+
#endif
239+
229240
var appleTlsEntry = new Tuple<Guid,String> (AppleTlsId, "Mono.AppleTls.AppleTlsProvider");
230241

231242
#if ONLY_APPLETLS || MONOTOUCH || XAMMAC
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#if SECURITY_DEP
2+
using System.Security.Cryptography.X509Certificates;
3+
4+
namespace Mono.Unity
5+
{
6+
internal unsafe static class CertHelper
7+
{
8+
public static void AddCertificatesToNativeChain (UnityTls.unitytls_x509list* nativeCertificateChain, X509CertificateCollection certificates, UnityTls.unitytls_errorstate* errorState)
9+
{
10+
foreach (var certificate in certificates) {
11+
AddCertificateToNativeChain (nativeCertificateChain, certificate, errorState);
12+
}
13+
}
14+
15+
public static void AddCertificateToNativeChain (UnityTls.unitytls_x509list* nativeCertificateChain, X509Certificate certificate, UnityTls.unitytls_errorstate* errorState)
16+
{
17+
byte[] certDer = certificate.GetRawCertData ();
18+
fixed(byte* certDerPtr = certDer) {
19+
UnityTls.NativeInterface.unitytls_x509list_append_der (nativeCertificateChain, certDerPtr, certDer.Length, errorState);
20+
}
21+
22+
var certificateImpl2 = certificate.Impl as X509Certificate2Impl;
23+
if (certificateImpl2 != null) {
24+
var intermediates = certificateImpl2.IntermediateCertificates;
25+
if (intermediates != null && intermediates.Count > 0) {
26+
for (int i=0; i<intermediates.Count; ++i) {
27+
AddCertificateToNativeChain (nativeCertificateChain, new X509Certificate (intermediates[i]), errorState);
28+
}
29+
}
30+
}
31+
}
32+
33+
public static X509CertificateCollection NativeChainToManagedCollection (UnityTls.unitytls_x509list_ref nativeCertificateChain, UnityTls.unitytls_errorstate* errorState)
34+
{
35+
X509CertificateCollection certificates = new X509CertificateCollection ();
36+
37+
var cert = UnityTls.NativeInterface.unitytls_x509list_get_x509 (nativeCertificateChain, 0, errorState);
38+
for (int i = 0; cert.handle != UnityTls.NativeInterface.UNITYTLS_INVALID_HANDLE; ++i) {
39+
size_t certBufferSize = UnityTls.NativeInterface.unitytls_x509_export_der (cert, null, 0, errorState);
40+
var certBuffer = new byte[certBufferSize]; // Need to reallocate every time since X509Certificate constructor takes no length but only a byte array.
41+
fixed(byte* certBufferPtr = certBuffer) {
42+
UnityTls.NativeInterface.unitytls_x509_export_der (cert, certBufferPtr, certBufferSize, errorState);
43+
}
44+
certificates.Add (new X509Certificate (certBuffer));
45+
46+
cert = UnityTls.NativeInterface.unitytls_x509list_get_x509 (nativeCertificateChain, i, errorState);
47+
}
48+
49+
return certificates;
50+
}
51+
}
52+
}
53+
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#if SECURITY_DEP
2+
#if MONO_SECURITY_ALIAS
3+
extern alias MonoSecurity;
4+
#endif
5+
6+
#if MONO_SECURITY_ALIAS
7+
using MonoSecurity::Mono.Security.Interface;
8+
#else
9+
using Mono.Security.Interface;
10+
#endif
11+
12+
namespace Mono.Unity
13+
{
14+
internal static class Debug
15+
{
16+
public static void CheckAndThrow (UnityTls.unitytls_errorstate errorState, string context, AlertDescription defaultAlert = AlertDescription.InternalError)
17+
{
18+
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_SUCCESS)
19+
return;
20+
21+
string message = string.Format ("{0} - error code: {1}", context, errorState.code);
22+
throw new TlsException (defaultAlert, message);
23+
}
24+
25+
public static void CheckAndThrow(UnityTls.unitytls_errorstate errorState, UnityTls.unitytls_x509verify_result verifyResult, string context, AlertDescription defaultAlert = AlertDescription.InternalError)
26+
{
27+
// Ignore verify result if verification is not the issue.
28+
if (verifyResult == UnityTls.unitytls_x509verify_result.UNITYTLS_X509VERIFY_SUCCESS) {
29+
CheckAndThrow (errorState, context, defaultAlert);
30+
return;
31+
}
32+
33+
AlertDescription alert = UnityTlsConversions.VerifyResultToAlertDescription (verifyResult, defaultAlert);
34+
string message = string.Format ("{0} - error code: {1}, verify result: {2}", context, errorState.code, verifyResult);
35+
throw new TlsException (alert, message);
36+
}
37+
}
38+
}
39+
#endif

0 commit comments

Comments
 (0)