提交 9d07185d 编写于 作者: A andreasr

[unitytls] UnityTls interface is now installed over a struct of callbacks

上级 f9c524ff
......@@ -228,10 +228,18 @@ namespace Mono.Net.Security
providerCache = new Dictionary<Guid,MSI.MonoTlsProvider> ();
#if UNITY
var unityTlsEntry = new Tuple<Guid,String> (UnityTlsId, "Mono.Unity.UnityTlsProvider");
providerRegistration.Add ("default", unityTlsEntry);
providerRegistration.Add ("unitytls", unityTlsEntry);
#else
if (Mono.Unity.UnityTls.IsSupported())
{
var unityTlsEntry = new Tuple<Guid,String> (UnityTlsId, "Mono.Unity.UnityTlsProvider");
providerRegistration.Add ("default", unityTlsEntry);
providerRegistration.Add ("unitytls", unityTlsEntry);
//Console.Out.WriteLine("UnityTls activated");
}
else
{
//Console.Out.WriteLine("UnityTls not available, using Mono's default implementations");
#endif
var appleTlsEntry = new Tuple<Guid,String> (AppleTlsId, "Mono.AppleTls.AppleTlsProvider");
......@@ -258,6 +266,8 @@ namespace Mono.Net.Security
providerRegistration.Add ("default", legacyEntry);
providerRegistration.Add ("apple", appleTlsEntry);
#if UNITY
}
#endif
}
}
......
......@@ -16,7 +16,7 @@ namespace Mono.Unity
{
byte[] certDer = certificate.GetRawCertData ();
fixed(byte* certDerPtr = certDer) {
UnityTls.unitytls_x509list_append_der (nativeCertificateChain, certDerPtr, certDer.Length, errorState);
UnityTls.GetInterface().unitytls_x509list_append_der (nativeCertificateChain, certDerPtr, certDer.Length, errorState);
}
var certificateImpl2 = certificate.Impl as X509Certificate2Impl;
......@@ -35,14 +35,14 @@ namespace Mono.Unity
X509CertificateCollection certificates = new X509CertificateCollection ();
// TODO: Should iterate until we get an invalid handle instead. Can't acces INVALID_HANDLE yet though due to limitations of UnityTls interface
size_t numCerts = UnityTls.unitytls_x509list_get_size (nativeCertificateChain, errorState);
size_t numCerts = UnityTls.GetInterface().unitytls_x509list_get_size (nativeCertificateChain, errorState);
for (int i = 0; i < numCerts; ++i) {
UnityTls.unitytls_x509_ref cert = UnityTls.unitytls_x509list_get_x509 (nativeCertificateChain, i, errorState);
size_t certBufferSize = UnityTls.unitytls_x509_export_der (cert, null, 0, errorState);
UnityTls.unitytls_x509_ref cert = UnityTls.GetInterface().unitytls_x509list_get_x509 (nativeCertificateChain, i, errorState);
size_t certBufferSize = UnityTls.GetInterface().unitytls_x509_export_der (cert, null, 0, errorState);
var certBuffer = new byte[certBufferSize]; // Need to reallocate every time since X509Certificate constructor takes no length but only a byte array.
fixed(byte* certBufferPtr = certBuffer) {
UnityTls.unitytls_x509_export_der (cert, certBufferPtr, certBufferSize, errorState);
UnityTls.GetInterface().unitytls_x509_export_der (cert, certBufferPtr, certBufferSize, errorState);
}
certificates.Add (new X509Certificate (certBuffer));
......
......@@ -63,12 +63,6 @@ namespace Mono.Unity
private UInt64 reserved; // Implementation specific error code/handle.
}
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_errorstate unitytls_errorstate_create();
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static void unitytls_errorstate_raise_error(unitytls_errorstate* errorState, unitytls_error_code errorCode);
// ------------------------------------
// Private Key
// ------------------------------------
......@@ -77,13 +71,6 @@ namespace Mono.Unity
[StructLayout (LayoutKind.Sequential)]
public struct unitytls_key_ref { UInt64 handle; }
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_key_ref unitytls_key_get_ref(unitytls_key* key, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_key* unitytls_key_parse_der(UInt8* buffer, size_t bufferLen, UInt8* password, size_t passwordLen, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static void unitytls_key_free(unitytls_key* key);
// ------------------------------------
// X.509 Certificate
// -----------------------------------
......@@ -91,9 +78,6 @@ namespace Mono.Unity
[StructLayout (LayoutKind.Sequential)]
public struct unitytls_x509_ref { UInt64 handle; }
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static size_t unitytls_x509_export_der(unitytls_x509_ref cert, UInt8* buffer, size_t bufferLen, unitytls_errorstate* errorState);
// ------------------------------------
// X.509 Certificate List
// ------------------------------------
......@@ -101,21 +85,6 @@ namespace Mono.Unity
[StructLayout (LayoutKind.Sequential)]
public struct unitytls_x509list_ref { UInt64 handle; }
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_x509list_ref unitytls_x509list_get_ref(unitytls_x509list* list, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static size_t unitytls_x509list_get_size(unitytls_x509list_ref list, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_x509_ref unitytls_x509list_get_x509(unitytls_x509list_ref list, size_t index, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_x509list* unitytls_x509list_create(unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static void unitytls_x509list_append(unitytls_x509list* list, unitytls_x509_ref cert, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static void unitytls_x509list_append_der(unitytls_x509list* list, UInt8* buffer, size_t bufferLen, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static void unitytls_x509list_free(unitytls_x509list* list);
// ------------------------------------
// X.509 Certificate Verification
// ------------------------------------
......@@ -127,7 +96,7 @@ namespace Mono.Unity
UNITYTLS_X509VERIFY_FATAL_ERROR = 0xFFFFFFFF,
UNITYTLS_X509VERIFY_FLAG_EXPIRED = 0x00000001,
UNITYTLS_X509VERIFY_FLAG_REVOKED = 0x00000002, // requires CRL backend
UNITYTLS_X509VERIFY_FLAG_REVOKED = 0x00000002,
UNITYTLS_X509VERIFY_FLAG_CN_MISMATCH = 0x00000004,
UNITYTLS_X509VERIFY_FLAG_NOT_TRUSTED = 0x00000008,
......@@ -145,12 +114,6 @@ namespace Mono.Unity
public delegate unitytls_x509verify_result unitytls_x509verify_callback(void* userData, unitytls_x509_ref cert, unitytls_x509verify_result result, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_x509verify_result unitytls_x509verify_default_ca(unitytls_x509list_ref chain, Int8* cn, size_t cnLen, unitytls_x509verify_callback cb, void* userData, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_x509verify_result unitytls_x509verify_explicit_ca(unitytls_x509list_ref chain, unitytls_x509list_ref trustCA, Int8* cn, size_t cnLen, unitytls_x509verify_callback cb, void* userData, unitytls_errorstate* errorState);
// ------------------------------------
// TLS Context
// ------------------------------------
......@@ -192,30 +155,92 @@ namespace Mono.Unity
public void* data;
};
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_tlsctx* unitytls_tlsctx_create_server(unitytls_tlsctx_protocolrange supportedProtocols, unitytls_tlsctx_callbacks callbacks, unitytls_x509list_ref certChain, unitytls_key_ref leafCertificateKey, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_tlsctx* unitytls_tlsctx_create_client(unitytls_tlsctx_protocolrange supportedProtocols, unitytls_tlsctx_callbacks callbacks, Int8* cn, size_t cnLen, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static void unitytls_tlsctx_set_trace_callback(unitytls_tlsctx* ctx, unitytls_tlsctx_trace_callback cb, void* userData, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static void unitytls_tlsctx_set_x509verify_callback(unitytls_tlsctx* ctx, unitytls_tlsctx_x509verify_callback cb, void* userData, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static void unitytls_tlsctx_set_supported_ciphersuites(unitytls_tlsctx* ctx, unitytls_ciphersuite* supportedCiphersuites, size_t supportedCiphersuitesLen, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_ciphersuite unitytls_tlsctx_get_ciphersuite(unitytls_tlsctx* ctx, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_protocol unitytls_tlsctx_get_protocol(unitytls_tlsctx* ctx, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static unitytls_x509verify_result unitytls_tlsctx_process_handshake(unitytls_tlsctx* ctx, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static size_t unitytls_tlsctx_read(unitytls_tlsctx* ctx, UInt8* buffer, size_t bufferLen, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static size_t unitytls_tlsctx_write(unitytls_tlsctx* ctx, UInt8* data, size_t bufferLen, unitytls_errorstate* errorState);
[DllImport (DLLNAME, CallingConvention=CALLCONV)]
extern public static void unitytls_tlsctx_free(unitytls_tlsctx* ctx);
// ------------------------------------------------------------------------
// unitytls interface defintion
// ------------------------------------------------------------------------
[StructLayout (LayoutKind.Sequential)]
public class mono_unity_unitytls_interface
{
public delegate unitytls_errorstate unitytls_errorstate_create_t();
public unitytls_errorstate_create_t unitytls_errorstate_create;
public delegate void unitytls_errorstate_raise_error_t(unitytls_errorstate* errorState, unitytls_error_code errorCode);
public unitytls_errorstate_raise_error_t unitytls_errorstate_raise_error;
public delegate unitytls_key_ref unitytls_key_get_ref_t(unitytls_key* key, unitytls_errorstate* errorState);
public unitytls_key_get_ref_t unitytls_key_get_ref;
public delegate unitytls_key* unitytls_key_parse_der_t(UInt8* buffer, size_t bufferLen, UInt8* password, size_t passwordLen, unitytls_errorstate* errorState);
public unitytls_key_parse_der_t unitytls_key_parse_der;
public delegate void unitytls_key_free_t(unitytls_key* key);
public unitytls_key_free_t unitytls_key_free;
public delegate size_t unitytls_x509_export_der_t(unitytls_x509_ref cert, UInt8* buffer, size_t bufferLen, unitytls_errorstate* errorState);
public unitytls_x509_export_der_t unitytls_x509_export_der;
public delegate unitytls_x509list_ref unitytls_x509list_get_ref_t(unitytls_x509list* list, unitytls_errorstate* errorState);
public unitytls_x509list_get_ref_t unitytls_x509list_get_ref;
public delegate size_t unitytls_x509list_get_size_t(unitytls_x509list_ref list, unitytls_errorstate* errorState);
public unitytls_x509list_get_size_t unitytls_x509list_get_size;
public delegate unitytls_x509_ref unitytls_x509list_get_x509_t(unitytls_x509list_ref list, size_t index, unitytls_errorstate* errorState);
public unitytls_x509list_get_x509_t unitytls_x509list_get_x509;
public delegate unitytls_x509list* unitytls_x509list_create_t(unitytls_errorstate* errorState);
public unitytls_x509list_create_t unitytls_x509list_create;
public delegate void unitytls_x509list_append_t(unitytls_x509list* list, unitytls_x509_ref cert, unitytls_errorstate* errorState);
public unitytls_x509list_append_t unitytls_x509list_append;
public delegate void unitytls_x509list_append_der_t(unitytls_x509list* list, UInt8* buffer, size_t bufferLen, unitytls_errorstate* errorState);
public unitytls_x509list_append_der_t unitytls_x509list_append_der;
public delegate void unitytls_x509list_free_t(unitytls_x509list* list);
public unitytls_x509list_free_t unitytls_x509list_free;
public delegate unitytls_x509verify_result unitytls_x509verify_default_ca_t(unitytls_x509list_ref chain, Int8* cn, size_t cnLen, unitytls_x509verify_callback cb, void* userData, unitytls_errorstate* errorState);
public unitytls_x509verify_default_ca_t unitytls_x509verify_default_ca;
public delegate unitytls_x509verify_result unitytls_x509verify_explicit_ca_t(unitytls_x509list_ref chain, unitytls_x509list_ref trustCA, Int8* cn, size_t cnLen, unitytls_x509verify_callback cb, void* userData, unitytls_errorstate* errorState);
public unitytls_x509verify_explicit_ca_t unitytls_x509verify_explicit_ca;
public delegate unitytls_tlsctx* unitytls_tlsctx_create_server_t(unitytls_tlsctx_protocolrange supportedProtocols, unitytls_tlsctx_callbacks callbacks, unitytls_x509list_ref certChain, unitytls_key_ref leafCertificateKey, unitytls_errorstate* errorState);
public unitytls_tlsctx_create_server_t unitytls_tlsctx_create_server;
public delegate unitytls_tlsctx* unitytls_tlsctx_create_client_t(unitytls_tlsctx_protocolrange supportedProtocols, unitytls_tlsctx_callbacks callbacks, Int8* cn, size_t cnLen, unitytls_errorstate* errorState);
public unitytls_tlsctx_create_client_t unitytls_tlsctx_create_client;
public delegate void unitytls_tlsctx_set_trace_callback_t(unitytls_tlsctx* ctx, unitytls_tlsctx_trace_callback cb, void* userData, unitytls_errorstate* errorState);
public unitytls_tlsctx_set_trace_callback_t unitytls_tlsctx_set_trace_callback;
public delegate void unitytls_tlsctx_set_x509verify_callback_t(unitytls_tlsctx* ctx, unitytls_tlsctx_x509verify_callback cb, void* userData, unitytls_errorstate* errorState);
public unitytls_tlsctx_set_x509verify_callback_t unitytls_tlsctx_set_x509verify_callback;
public delegate void unitytls_tlsctx_set_supported_ciphersuites_t(unitytls_tlsctx* ctx, unitytls_ciphersuite* supportedCiphersuites, size_t supportedCiphersuitesLen, unitytls_errorstate* errorState);
public unitytls_tlsctx_set_supported_ciphersuites_t unitytls_tlsctx_set_supported_ciphersuites;
public delegate unitytls_ciphersuite unitytls_tlsctx_get_ciphersuite_t(unitytls_tlsctx* ctx, unitytls_errorstate* errorState);
public unitytls_tlsctx_get_ciphersuite_t unitytls_tlsctx_get_ciphersuite;
public delegate unitytls_protocol unitytls_tlsctx_get_protocol_t(unitytls_tlsctx* ctx, unitytls_errorstate* errorState);
public unitytls_tlsctx_get_protocol_t unitytls_tlsctx_get_protocol;
public delegate unitytls_x509verify_result unitytls_tlsctx_process_handshake_t(unitytls_tlsctx* ctx, unitytls_errorstate* errorState);
public unitytls_tlsctx_process_handshake_t unitytls_tlsctx_process_handshake;
public delegate size_t unitytls_tlsctx_read_t(unitytls_tlsctx* ctx, UInt8* buffer, size_t bufferLen, unitytls_errorstate* errorState);
public unitytls_tlsctx_read_t unitytls_tlsctx_read;
public delegate size_t unitytls_tlsctx_write_t(unitytls_tlsctx* ctx, UInt8* data, size_t bufferLen, unitytls_errorstate* errorState);
public unitytls_tlsctx_write_t unitytls_tlsctx_write;
public delegate void unitytls_tlsctx_free_t(unitytls_tlsctx* ctx);
public unitytls_tlsctx_free_t unitytls_tlsctx_free;
}
[DllImport("__Internal")]
private static extern IntPtr mono_unity_get_unitytls_interface();
private static mono_unity_unitytls_interface marshalledInterface = null;
public static bool IsSupported()
{
try {
return GetInterface() != null;
} catch (System.Exception) {
return false;
}
}
public static mono_unity_unitytls_interface GetInterface()
{
if (marshalledInterface == null)
marshalledInterface = Marshal.PtrToStructure<mono_unity_unitytls_interface>(mono_unity_get_unitytls_interface());
return marshalledInterface;
}
}
}
\ No newline at end of file
......@@ -54,7 +54,7 @@ namespace Mono.Unity
// Need GCHandle to get a consistent pointer to this instance
m_handle = GCHandle.Alloc (this);
UnityTls.unitytls_errorstate errorState = UnityTls.unitytls_errorstate_create ();
var errorState = UnityTls.GetInterface().unitytls_errorstate_create ();
// Map selected protocols as best as we can.
UnityTls.unitytls_tlsctx_protocolrange protocolRange = new UnityTls.unitytls_tlsctx_protocolrange {
......@@ -78,37 +78,37 @@ namespace Mono.Unity
UnityTls.unitytls_x509list* serverCerts = null;
UnityTls.unitytls_key* serverPrivateKey = null;
try {
serverCerts = UnityTls.unitytls_x509list_create (&errorState);
serverCerts = UnityTls.GetInterface().unitytls_x509list_create (&errorState);
CertHelper.AddCertificateToNativeChain (serverCerts, serverCertificate, &errorState);
UnityTls.unitytls_x509list_ref serverCertsRef = UnityTls.unitytls_x509list_get_ref (serverCerts, &errorState);
var serverCertsRef = UnityTls.GetInterface().unitytls_x509list_get_ref (serverCerts, &errorState);
byte[] privateKeyDer = PKCS8.PrivateKeyInfo.Encode (serverCertificate2.PrivateKey);
fixed(byte* privateKeyDerPtr = privateKeyDer) {
serverPrivateKey = UnityTls.unitytls_key_parse_der (privateKeyDerPtr, privateKeyDer.Length, null, 0, &errorState);
serverPrivateKey = UnityTls.GetInterface().unitytls_key_parse_der (privateKeyDerPtr, privateKeyDer.Length, null, 0, &errorState);
}
UnityTls.unitytls_key_ref serverKeyRef = UnityTls.unitytls_key_get_ref (serverPrivateKey, &errorState);
var serverKeyRef = UnityTls.GetInterface().unitytls_key_get_ref (serverPrivateKey, &errorState);
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to parse server key/certificate");
m_TlsContext = UnityTls.unitytls_tlsctx_create_server (protocolRange, callbacks, serverCertsRef, serverKeyRef, &errorState);
m_TlsContext = UnityTls.GetInterface().unitytls_tlsctx_create_server (protocolRange, callbacks, serverCertsRef, serverKeyRef, &errorState);
} finally {
UnityTls.unitytls_x509list_free (serverCerts);
UnityTls.unitytls_key_free (serverPrivateKey);
UnityTls.GetInterface().unitytls_x509list_free (serverCerts);
UnityTls.GetInterface().unitytls_key_free (serverPrivateKey);
}
}
else {
byte [] targetHostUtf8 = Encoding.UTF8.GetBytes (targetHost);
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
m_TlsContext = UnityTls.unitytls_tlsctx_create_client (protocolRange, callbacks, targetHostUtf8Ptr, targetHostUtf8.Length, &errorState);
m_TlsContext = UnityTls.GetInterface().unitytls_tlsctx_create_client (protocolRange, callbacks, targetHostUtf8Ptr, targetHostUtf8.Length, &errorState);
}
}
UnityTls.unitytls_tlsctx_set_x509verify_callback (m_TlsContext, VerifyCallback, (void*)(IntPtr)m_handle, &errorState);
UnityTls.GetInterface().unitytls_tlsctx_set_x509verify_callback (m_TlsContext, VerifyCallback, (void*)(IntPtr)m_handle, &errorState);
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to create UnityTls context");
if (ActivateTracing) {
UnityTls.unitytls_tlsctx_set_trace_callback (m_TlsContext, TraceCallback, null, &errorState);
UnityTls.GetInterface().unitytls_tlsctx_set_trace_callback (m_TlsContext, TraceCallback, null, &errorState);
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to set trace callback");
}
......@@ -149,9 +149,9 @@ namespace Mono.Unity
bool wouldBlock = false;
int numBytesRead = 0;
UnityTls.unitytls_errorstate errorState = UnityTls.unitytls_errorstate_create ();
var errorState = UnityTls.GetInterface().unitytls_errorstate_create ();
fixed (byte* bufferPtr = buffer) {
numBytesRead = UnityTls.unitytls_tlsctx_read (m_TlsContext, bufferPtr + offset, count, &errorState);
numBytesRead = UnityTls.GetInterface().unitytls_tlsctx_read (m_TlsContext, bufferPtr + offset, count, &errorState);
}
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
......@@ -167,9 +167,9 @@ namespace Mono.Unity
bool wouldBlock = false;
int numBytesWritten = 0;
UnityTls.unitytls_errorstate errorState = UnityTls.unitytls_errorstate_create ();
var errorState = UnityTls.GetInterface().unitytls_errorstate_create ();
fixed (byte* bufferPtr = buffer) {
numBytesWritten = UnityTls.unitytls_tlsctx_write (m_TlsContext, bufferPtr + offset, count, &errorState);
numBytesWritten = UnityTls.GetInterface().unitytls_tlsctx_write (m_TlsContext, bufferPtr + offset, count, &errorState);
}
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
......@@ -183,7 +183,7 @@ namespace Mono.Unity
public override void Shutdown ()
{
// Destroy native UnityTls objects
UnityTls.unitytls_tlsctx_free (m_TlsContext);
UnityTls.GetInterface().unitytls_tlsctx_free (m_TlsContext);
m_TlsContext = null;
m_HasContext = false;
......@@ -224,8 +224,8 @@ namespace Mono.Unity
public override bool ProcessHandshake ()
{
UnityTls.unitytls_errorstate errorState = UnityTls.unitytls_errorstate_create ();
UnityTls.unitytls_x509verify_result result = UnityTls.unitytls_tlsctx_process_handshake (m_TlsContext, &errorState);
var errorState = UnityTls.GetInterface().unitytls_errorstate_create ();
var result = UnityTls.GetInterface().unitytls_tlsctx_process_handshake (m_TlsContext, &errorState);
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
return false;
......@@ -248,8 +248,8 @@ namespace Mono.Unity
public override void FinishHandshake ()
{
// Query some data. Ignore errors on the way since failure is not crucial.
UnityTls.unitytls_ciphersuite cipherSuite = UnityTls.unitytls_tlsctx_get_ciphersuite(m_TlsContext, null);
UnityTls.unitytls_protocol protocolVersion = UnityTls.unitytls_tlsctx_get_protocol(m_TlsContext, null);
var cipherSuite = UnityTls.GetInterface().unitytls_tlsctx_get_ciphersuite(m_TlsContext, null);
var protocolVersion = UnityTls.GetInterface().unitytls_tlsctx_get_protocol(m_TlsContext, null);
m_Connectioninfo = new MonoTlsConnectionInfo () {
CipherSuiteCode = (CipherSuiteCode)cipherSuite,
......@@ -283,13 +283,13 @@ namespace Mono.Unity
Marshal.Copy ((IntPtr)data, m_WriteBuffer, 0, bufferLen);
if (!Parent.InternalWrite (m_WriteBuffer, 0, bufferLen)) {
UnityTls.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WRITE_FAILED);
UnityTls.GetInterface().unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WRITE_FAILED);
return 0;
}
return bufferLen;
} catch { // handle all exceptions since we don't want to let them go through native code.
UnityTls.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
UnityTls.GetInterface().unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
return 0;
}
}
......@@ -311,18 +311,18 @@ namespace Mono.Unity
bool wouldBlock;
int numBytesRead = Parent.InternalRead (m_ReadBuffer, 0, bufferLen, out wouldBlock);
if (wouldBlock) {
UnityTls.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK);
UnityTls.GetInterface().unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK);
return 0;
}
if (numBytesRead < 0) {
UnityTls.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_READ_FAILED);
UnityTls.GetInterface().unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_READ_FAILED);
return 0;
}
Marshal.Copy (m_ReadBuffer, 0, (IntPtr)buffer, bufferLen);
return numBytesRead;
} catch { // handle all exceptions since we don't want to let them go through native code.
UnityTls.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
UnityTls.GetInterface().unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
return 0;
}
}
......
......@@ -72,9 +72,9 @@ namespace Mono.Unity
}
// convert cert to native
UnityTls.unitytls_errorstate errorState = UnityTls.unitytls_errorstate_create ();
UnityTls.unitytls_x509list* certificatesNative = UnityTls.unitytls_x509list_create (&errorState);
UnityTls.unitytls_x509verify_result result = UnityTls.unitytls_x509verify_result.UNITYTLS_X509VERIFY_NOT_DONE;
var errorState = UnityTls.GetInterface().unitytls_errorstate_create ();
var certificatesNative = UnityTls.GetInterface().unitytls_x509list_create (&errorState);
var result = UnityTls.unitytls_x509verify_result.UNITYTLS_X509VERIFY_NOT_DONE;
try
{
// Things the validator provides that we might want to make use of here:
......@@ -85,28 +85,28 @@ namespace Mono.Unity
//validator.Settings.SendCloseNotify // UnityTls always sends a close notify if the underlying impl supports it. Currently only used by MonoBtlsProvider
CertHelper.AddCertificatesToNativeChain (certificatesNative, certificates, &errorState);
UnityTls.unitytls_x509list_ref certificatesNativeRef = UnityTls.unitytls_x509list_get_ref (certificatesNative, &errorState);
byte[] targetHostUtf8 = Encoding.UTF8.GetBytes (targetHost);
var certificatesNativeRef = UnityTls.GetInterface().unitytls_x509list_get_ref (certificatesNative, &errorState);
var targetHostUtf8 = Encoding.UTF8.GetBytes (targetHost);
if (validator.Settings.TrustAnchors != null) {
UnityTls.unitytls_x509list* trustCAnative = UnityTls.unitytls_x509list_create (&errorState);
var trustCAnative = UnityTls.GetInterface().unitytls_x509list_create (&errorState);
CertHelper.AddCertificatesToNativeChain (trustCAnative, validator.Settings.TrustAnchors, &errorState);
UnityTls.unitytls_x509list_ref trustCAnativeRef = UnityTls.unitytls_x509list_get_ref (certificatesNative, &errorState);
var trustCAnativeRef = UnityTls.GetInterface().unitytls_x509list_get_ref (certificatesNative, &errorState);
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
result = UnityTls.unitytls_x509verify_explicit_ca (certificatesNativeRef, trustCAnativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
result = UnityTls.GetInterface().unitytls_x509verify_explicit_ca (certificatesNativeRef, trustCAnativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
}
UnityTls.unitytls_x509list_free (trustCAnative);
UnityTls.GetInterface().unitytls_x509list_free (trustCAnative);
} else {
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
result = UnityTls.unitytls_x509verify_default_ca (certificatesNativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
result = UnityTls.GetInterface().unitytls_x509verify_default_ca (certificatesNativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
}
}
}
finally
{
UnityTls.unitytls_x509list_free (certificatesNative);
UnityTls.GetInterface().unitytls_x509list_free (certificatesNative);
}
errors = UnityTlsConversions.VerifyResultToPolicyErrror(result);
......
......@@ -885,6 +885,21 @@ MonoClass* mono_unity_defaults_get_byte_class()
return mono_defaults.byte_class;
}
//unitytls
static mono_unity_unitytls_interface* gUnitytlsInterface = NULL;
mono_unity_unitytls_interface* mono_unity_get_unitytls_interface()
{
return gUnitytlsInterface;
}
MONO_API void
mono_unity_install_unitytls_interface(mono_unity_unitytls_interface* callbacks)
{
gUnitytlsInterface = callbacks;
}
//misc
MonoAssembly* mono_unity_assembly_get_mscorlib()
......
......@@ -144,6 +144,10 @@ MonoClass* mono_unity_defaults_get_char_class();
MonoClass* mono_unity_defaults_get_delegate_class();
MonoClass* mono_unity_defaults_get_byte_class();
//unitytls
typedef struct mono_unity_unitytls_interface mono_unity_unitytls_interface;
mono_unity_unitytls_interface* mono_unity_get_unitytls_interface();
//misc
MonoAssembly* mono_unity_assembly_get_mscorlib();
MonoImage* mono_unity_image_get_mscorlib();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册