Skip to content

Commit a47260d

Browse files
committed
[unitytls] Removed calls to UnityTls.GetInterface
1 parent b990700 commit a47260d

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ unsafe internal static partial class UnityTls
2929
private const string DLLNAME = "MacStandalonePlayer_TLSModule_Dynamic.dylib";
3030
private const CallingConvention CALLCONV = CallingConvention.Cdecl;
3131

32-
// TODO
33-
//extern const UInt64 UNITYTLS_INVALID_HANDLE;
34-
3532
// ------------------------------------
3633
// Error Handling
3734
// ------------------------------------

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

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ unsafe internal class UnityTlsContext : MobileTlsContext
2828
{
2929
private const bool ActivateTracing = false;
3030

31+
private UnityTls.mono_unity_unitytls_interface unityTlsNative;
32+
3133
// Native UnityTls objects
3234
private UnityTls.unitytls_tlsctx* m_TlsContext = null;
3335

@@ -51,10 +53,12 @@ public UnityTlsContext (
5153
X509CertificateCollection clientCertificates, bool askForClientCert)
5254
: base (parent, serverMode, targetHost, enabledProtocols, serverCertificate, clientCertificates, askForClientCert)
5355
{
56+
unityTlsNative = UnityTls.GetInterface();
57+
5458
// Need GCHandle to get a consistent pointer to this instance
5559
m_handle = GCHandle.Alloc (this);
5660

57-
var errorState = UnityTls.GetInterface().unitytls_errorstate_create ();
61+
var errorState = unityTlsNative.unitytls_errorstate_create ();
5862

5963
// Map selected protocols as best as we can.
6064
UnityTls.unitytls_tlsctx_protocolrange protocolRange = new UnityTls.unitytls_tlsctx_protocolrange {
@@ -75,40 +79,41 @@ public UnityTlsContext (
7579
if (serverCertificate2 == null || serverCertificate2.PrivateKey == null)
7680
throw new ArgumentException ("serverCertificate does not have a private key", "serverCertificate");
7781

82+
7883
UnityTls.unitytls_x509list* serverCerts = null;
7984
UnityTls.unitytls_key* serverPrivateKey = null;
8085
try {
81-
serverCerts = UnityTls.GetInterface().unitytls_x509list_create (&errorState);
86+
serverCerts = unityTlsNative.unitytls_x509list_create (&errorState);
8287
CertHelper.AddCertificateToNativeChain (serverCerts, serverCertificate, &errorState);
83-
var serverCertsRef = UnityTls.GetInterface().unitytls_x509list_get_ref (serverCerts, &errorState);
88+
var serverCertsRef = unityTlsNative.unitytls_x509list_get_ref (serverCerts, &errorState);
8489

8590
byte[] privateKeyDer = PKCS8.PrivateKeyInfo.Encode (serverCertificate2.PrivateKey);
8691
fixed(byte* privateKeyDerPtr = privateKeyDer) {
87-
serverPrivateKey = UnityTls.GetInterface().unitytls_key_parse_der (privateKeyDerPtr, privateKeyDer.Length, null, 0, &errorState);
92+
serverPrivateKey = unityTlsNative.unitytls_key_parse_der (privateKeyDerPtr, privateKeyDer.Length, null, 0, &errorState);
8893
}
89-
var serverKeyRef = UnityTls.GetInterface().unitytls_key_get_ref (serverPrivateKey, &errorState);
94+
var serverKeyRef = unityTlsNative.unitytls_key_get_ref (serverPrivateKey, &errorState);
9095

9196
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to parse server key/certificate");
9297

93-
m_TlsContext = UnityTls.GetInterface().unitytls_tlsctx_create_server (protocolRange, callbacks, serverCertsRef, serverKeyRef, &errorState);
98+
m_TlsContext = unityTlsNative.unitytls_tlsctx_create_server (protocolRange, callbacks, serverCertsRef, serverKeyRef, &errorState);
9499
} finally {
95-
UnityTls.GetInterface().unitytls_x509list_free (serverCerts);
96-
UnityTls.GetInterface().unitytls_key_free (serverPrivateKey);
100+
unityTlsNative.unitytls_x509list_free (serverCerts);
101+
unityTlsNative.unitytls_key_free (serverPrivateKey);
97102
}
98103
}
99104
else {
100105
byte [] targetHostUtf8 = Encoding.UTF8.GetBytes (targetHost);
101106
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
102-
m_TlsContext = UnityTls.GetInterface().unitytls_tlsctx_create_client (protocolRange, callbacks, targetHostUtf8Ptr, targetHostUtf8.Length, &errorState);
107+
m_TlsContext = unityTlsNative.unitytls_tlsctx_create_client (protocolRange, callbacks, targetHostUtf8Ptr, targetHostUtf8.Length, &errorState);
103108
}
104109
}
105110

106-
UnityTls.GetInterface().unitytls_tlsctx_set_x509verify_callback (m_TlsContext, VerifyCallback, (void*)(IntPtr)m_handle, &errorState);
111+
unityTlsNative.unitytls_tlsctx_set_x509verify_callback (m_TlsContext, VerifyCallback, (void*)(IntPtr)m_handle, &errorState);
107112

108113
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to create UnityTls context");
109114

110115
if (ActivateTracing) {
111-
UnityTls.GetInterface().unitytls_tlsctx_set_trace_callback (m_TlsContext, TraceCallback, null, &errorState);
116+
unityTlsNative.unitytls_tlsctx_set_trace_callback (m_TlsContext, TraceCallback, null, &errorState);
112117
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to set trace callback");
113118
}
114119

@@ -149,9 +154,9 @@ public override (int ret, bool wantMore) Read (byte[] buffer, int offset, int co
149154
bool wouldBlock = false;
150155
int numBytesRead = 0;
151156

152-
var errorState = UnityTls.GetInterface().unitytls_errorstate_create ();
157+
var errorState = unityTlsNative.unitytls_errorstate_create ();
153158
fixed (byte* bufferPtr = buffer) {
154-
numBytesRead = UnityTls.GetInterface().unitytls_tlsctx_read (m_TlsContext, bufferPtr + offset, count, &errorState);
159+
numBytesRead = unityTlsNative.unitytls_tlsctx_read (m_TlsContext, bufferPtr + offset, count, &errorState);
155160
}
156161

157162
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
@@ -167,9 +172,9 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
167172
bool wouldBlock = false;
168173
int numBytesWritten = 0;
169174

170-
var errorState = UnityTls.GetInterface().unitytls_errorstate_create ();
175+
var errorState = unityTlsNative.unitytls_errorstate_create ();
171176
fixed (byte* bufferPtr = buffer) {
172-
numBytesWritten = UnityTls.GetInterface().unitytls_tlsctx_write (m_TlsContext, bufferPtr + offset, count, &errorState);
177+
numBytesWritten = unityTlsNative.unitytls_tlsctx_write (m_TlsContext, bufferPtr + offset, count, &errorState);
173178
}
174179

175180
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
@@ -183,7 +188,7 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
183188
public override void Shutdown ()
184189
{
185190
// Destroy native UnityTls objects
186-
UnityTls.GetInterface().unitytls_tlsctx_free (m_TlsContext);
191+
unityTlsNative.unitytls_tlsctx_free (m_TlsContext);
187192
m_TlsContext = null;
188193

189194
m_HasContext = false;
@@ -248,8 +253,8 @@ public override bool ProcessHandshake ()
248253
public override void FinishHandshake ()
249254
{
250255
// Query some data. Ignore errors on the way since failure is not crucial.
251-
var cipherSuite = UnityTls.GetInterface().unitytls_tlsctx_get_ciphersuite(m_TlsContext, null);
252-
var protocolVersion = UnityTls.GetInterface().unitytls_tlsctx_get_protocol(m_TlsContext, null);
256+
var cipherSuite = unityTlsNative.unitytls_tlsctx_get_ciphersuite(m_TlsContext, null);
257+
var protocolVersion = unityTlsNative.unitytls_tlsctx_get_protocol(m_TlsContext, null);
253258

254259
m_Connectioninfo = new MonoTlsConnectionInfo () {
255260
CipherSuiteCode = (CipherSuiteCode)cipherSuite,
@@ -283,13 +288,13 @@ private size_t WriteCallback (byte* data, size_t bufferLen, UnityTls.unitytls_er
283288
Marshal.Copy ((IntPtr)data, m_WriteBuffer, 0, bufferLen);
284289

285290
if (!Parent.InternalWrite (m_WriteBuffer, 0, bufferLen)) {
286-
UnityTls.GetInterface().unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WRITE_FAILED);
291+
unityTlsNative.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WRITE_FAILED);
287292
return 0;
288293
}
289294

290295
return bufferLen;
291296
} catch { // handle all exceptions since we don't want to let them go through native code.
292-
UnityTls.GetInterface().unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
297+
unityTlsNative.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
293298
return 0;
294299
}
295300
}
@@ -311,18 +316,18 @@ private size_t ReadCallback (byte* buffer, size_t bufferLen, UnityTls.unitytls_e
311316
bool wouldBlock;
312317
int numBytesRead = Parent.InternalRead (m_ReadBuffer, 0, bufferLen, out wouldBlock);
313318
if (wouldBlock) {
314-
UnityTls.GetInterface().unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK);
319+
unityTlsNative.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK);
315320
return 0;
316321
}
317322
if (numBytesRead < 0) {
318-
UnityTls.GetInterface().unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_READ_FAILED);
323+
unityTlsNative.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_READ_FAILED);
319324
return 0;
320325
}
321326

322327
Marshal.Copy (m_ReadBuffer, 0, (IntPtr)buffer, bufferLen);
323328
return numBytesRead;
324329
} catch { // handle all exceptions since we don't want to let them go through native code.
325-
UnityTls.GetInterface().unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
330+
unityTlsNative.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
326331
return 0;
327332
}
328333
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ internal override bool ValidateCertificate (
7171
targetHost = targetHost.Substring (0, pos);
7272
}
7373

74+
var unityTlsNative = UnityTls.GetInterface();
75+
7476
// convert cert to native
75-
var errorState = UnityTls.GetInterface().unitytls_errorstate_create ();
76-
var certificatesNative = UnityTls.GetInterface().unitytls_x509list_create (&errorState);
77+
var errorState = unityTlsNative.unitytls_errorstate_create ();
78+
var certificatesNative = unityTlsNative.unitytls_x509list_create (&errorState);
7779
var result = UnityTls.unitytls_x509verify_result.UNITYTLS_X509VERIFY_NOT_DONE;
7880
try
7981
{
@@ -85,28 +87,28 @@ internal override bool ValidateCertificate (
8587
//validator.Settings.SendCloseNotify // UnityTls always sends a close notify if the underlying impl supports it. Currently only used by MonoBtlsProvider
8688

8789
CertHelper.AddCertificatesToNativeChain (certificatesNative, certificates, &errorState);
88-
var certificatesNativeRef = UnityTls.GetInterface().unitytls_x509list_get_ref (certificatesNative, &errorState);
90+
var certificatesNativeRef = unityTlsNative.unitytls_x509list_get_ref (certificatesNative, &errorState);
8991
var targetHostUtf8 = Encoding.UTF8.GetBytes (targetHost);
9092

9193
if (validator.Settings.TrustAnchors != null) {
92-
var trustCAnative = UnityTls.GetInterface().unitytls_x509list_create (&errorState);
94+
var trustCAnative = unityTlsNative.unitytls_x509list_create (&errorState);
9395
CertHelper.AddCertificatesToNativeChain (trustCAnative, validator.Settings.TrustAnchors, &errorState);
94-
var trustCAnativeRef = UnityTls.GetInterface().unitytls_x509list_get_ref (certificatesNative, &errorState);
96+
var trustCAnativeRef = unityTlsNative.unitytls_x509list_get_ref (certificatesNative, &errorState);
9597

9698
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
97-
result = UnityTls.GetInterface().unitytls_x509verify_explicit_ca (certificatesNativeRef, trustCAnativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
99+
result = unityTlsNative.unitytls_x509verify_explicit_ca (certificatesNativeRef, trustCAnativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
98100
}
99101

100-
UnityTls.GetInterface().unitytls_x509list_free (trustCAnative);
102+
unityTlsNative.unitytls_x509list_free (trustCAnative);
101103
} else {
102104
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
103-
result = UnityTls.GetInterface().unitytls_x509verify_default_ca (certificatesNativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
105+
result = unityTlsNative.unitytls_x509verify_default_ca (certificatesNativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
104106
}
105107
}
106108
}
107109
finally
108110
{
109-
UnityTls.GetInterface().unitytls_x509list_free (certificatesNative);
111+
unityTlsNative.unitytls_x509list_free (certificatesNative);
110112
}
111113

112114
errors = UnityTlsConversions.VerifyResultToPolicyErrror(result);

0 commit comments

Comments
 (0)