Skip to content

Commit ca2058f

Browse files
committed
[unitytls] Removed local caching of unitytls native interface again.
Adds code complexity with little gain. With native interface being a property this also feels more natural now.
1 parent 589f130 commit ca2058f

File tree

3 files changed

+41
-48
lines changed

3 files changed

+41
-48
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,17 @@ public static void AddCertificateToNativeChain (UnityTls.unitytls_x509list* nati
3333
public static X509CertificateCollection NativeChainToManagedCollection (UnityTls.unitytls_x509list_ref nativeCertificateChain, UnityTls.unitytls_errorstate* errorState)
3434
{
3535
X509CertificateCollection certificates = new X509CertificateCollection ();
36-
var unityTls = UnityTls.NativeInterface;
3736

38-
var cert = unityTls.unitytls_x509list_get_x509 (nativeCertificateChain, 0, errorState);
39-
for (int i = 0; cert.handle != unityTls.UNITYTLS_INVALID_HANDLE; ++i) {
40-
size_t certBufferSize = unityTls.unitytls_x509_export_der (cert, null, 0, errorState);
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);
4140
var certBuffer = new byte[certBufferSize]; // Need to reallocate every time since X509Certificate constructor takes no length but only a byte array.
4241
fixed(byte* certBufferPtr = certBuffer) {
43-
unityTls.unitytls_x509_export_der (cert, certBufferPtr, certBufferSize, errorState);
42+
UnityTls.NativeInterface.unitytls_x509_export_der (cert, certBufferPtr, certBufferSize, errorState);
4443
}
4544
certificates.Add (new X509Certificate (certBuffer));
4645

47-
cert = unityTls.unitytls_x509list_get_x509 (nativeCertificateChain, i, errorState);
46+
cert = UnityTls.NativeInterface.unitytls_x509list_get_x509 (nativeCertificateChain, i, errorState);
4847
}
4948

5049
return certificates;

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

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

31-
private UnityTls.mono_unity_unitytls_interface unityTlsNative;
32-
3331
// Native UnityTls objects
3432
private UnityTls.unitytls_tlsctx* m_TlsContext = null;
3533

@@ -53,12 +51,10 @@ public UnityTlsContext (
5351
X509CertificateCollection clientCertificates, bool askForClientCert)
5452
: base (parent, serverMode, targetHost, enabledProtocols, serverCertificate, clientCertificates, askForClientCert)
5553
{
56-
unityTlsNative = UnityTls.NativeInterface;
57-
5854
// Need GCHandle to get a consistent pointer to this instance
5955
m_handle = GCHandle.Alloc (this);
6056

61-
var errorState = unityTlsNative.unitytls_errorstate_create ();
57+
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
6258

6359
// Map selected protocols as best as we can.
6460
UnityTls.unitytls_tlsctx_protocolrange protocolRange = new UnityTls.unitytls_tlsctx_protocolrange {
@@ -83,37 +79,37 @@ public UnityTlsContext (
8379
UnityTls.unitytls_x509list* serverCerts = null;
8480
UnityTls.unitytls_key* serverPrivateKey = null;
8581
try {
86-
serverCerts = unityTlsNative.unitytls_x509list_create (&errorState);
82+
serverCerts = UnityTls.NativeInterface.unitytls_x509list_create (&errorState);
8783
CertHelper.AddCertificateToNativeChain (serverCerts, serverCertificate, &errorState);
88-
var serverCertsRef = unityTlsNative.unitytls_x509list_get_ref (serverCerts, &errorState);
84+
var serverCertsRef = UnityTls.NativeInterface.unitytls_x509list_get_ref (serverCerts, &errorState);
8985

9086
byte[] privateKeyDer = PKCS8.PrivateKeyInfo.Encode (serverCertificate2.PrivateKey);
9187
fixed(byte* privateKeyDerPtr = privateKeyDer) {
92-
serverPrivateKey = unityTlsNative.unitytls_key_parse_der (privateKeyDerPtr, privateKeyDer.Length, null, 0, &errorState);
88+
serverPrivateKey = UnityTls.NativeInterface.unitytls_key_parse_der (privateKeyDerPtr, privateKeyDer.Length, null, 0, &errorState);
9389
}
94-
var serverKeyRef = unityTlsNative.unitytls_key_get_ref (serverPrivateKey, &errorState);
90+
var serverKeyRef = UnityTls.NativeInterface.unitytls_key_get_ref (serverPrivateKey, &errorState);
9591

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

98-
m_TlsContext = unityTlsNative.unitytls_tlsctx_create_server (protocolRange, callbacks, serverCertsRef, serverKeyRef, &errorState);
94+
m_TlsContext = UnityTls.NativeInterface.unitytls_tlsctx_create_server (protocolRange, callbacks, serverCertsRef, serverKeyRef, &errorState);
9995
} finally {
100-
unityTlsNative.unitytls_x509list_free (serverCerts);
101-
unityTlsNative.unitytls_key_free (serverPrivateKey);
96+
UnityTls.NativeInterface.unitytls_x509list_free (serverCerts);
97+
UnityTls.NativeInterface.unitytls_key_free (serverPrivateKey);
10298
}
10399
}
104100
else {
105101
byte [] targetHostUtf8 = Encoding.UTF8.GetBytes (targetHost);
106102
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
107-
m_TlsContext = unityTlsNative.unitytls_tlsctx_create_client (protocolRange, callbacks, targetHostUtf8Ptr, targetHostUtf8.Length, &errorState);
103+
m_TlsContext = UnityTls.NativeInterface.unitytls_tlsctx_create_client (protocolRange, callbacks, targetHostUtf8Ptr, targetHostUtf8.Length, &errorState);
108104
}
109105
}
110106

111-
unityTlsNative.unitytls_tlsctx_set_x509verify_callback (m_TlsContext, VerifyCallback, (void*)(IntPtr)m_handle, &errorState);
107+
UnityTls.NativeInterface.unitytls_tlsctx_set_x509verify_callback (m_TlsContext, VerifyCallback, (void*)(IntPtr)m_handle, &errorState);
112108

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

115111
if (ActivateTracing) {
116-
unityTlsNative.unitytls_tlsctx_set_trace_callback (m_TlsContext, TraceCallback, null, &errorState);
112+
UnityTls.NativeInterface.unitytls_tlsctx_set_trace_callback (m_TlsContext, TraceCallback, null, &errorState);
117113
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to set trace callback");
118114
}
119115

@@ -154,9 +150,9 @@ public override (int ret, bool wantMore) Read (byte[] buffer, int offset, int co
154150
bool wouldBlock = false;
155151
int numBytesRead = 0;
156152

157-
var errorState = unityTlsNative.unitytls_errorstate_create ();
153+
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
158154
fixed (byte* bufferPtr = buffer) {
159-
numBytesRead = unityTlsNative.unitytls_tlsctx_read (m_TlsContext, bufferPtr + offset, count, &errorState);
155+
numBytesRead = UnityTls.NativeInterface.unitytls_tlsctx_read (m_TlsContext, bufferPtr + offset, count, &errorState);
160156
}
161157

162158
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
@@ -172,9 +168,9 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
172168
bool wouldBlock = false;
173169
int numBytesWritten = 0;
174170

175-
var errorState = unityTlsNative.unitytls_errorstate_create ();
171+
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
176172
fixed (byte* bufferPtr = buffer) {
177-
numBytesWritten = unityTlsNative.unitytls_tlsctx_write (m_TlsContext, bufferPtr + offset, count, &errorState);
173+
numBytesWritten = UnityTls.NativeInterface.unitytls_tlsctx_write (m_TlsContext, bufferPtr + offset, count, &errorState);
178174
}
179175

180176
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
@@ -188,7 +184,7 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
188184
public override void Shutdown ()
189185
{
190186
// Destroy native UnityTls objects
191-
unityTlsNative.unitytls_tlsctx_free (m_TlsContext);
187+
UnityTls.NativeInterface.unitytls_tlsctx_free (m_TlsContext);
192188
m_TlsContext = null;
193189

194190
m_HasContext = false;
@@ -228,17 +224,17 @@ public override void StartHandshake ()
228224
for (int i = 0; i < ciphers.Length; i++)
229225
ciphers [i] = (UnityTls.unitytls_ciphersuite)Settings.EnabledCiphers [i];
230226

231-
var errorState = unityTlsNative.unitytls_errorstate_create ();
227+
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
232228
fixed (UnityTls.unitytls_ciphersuite* ciphersPtr = ciphers)
233-
unityTlsNative.unitytls_tlsctx_set_supported_ciphersuites (m_TlsContext, ciphersPtr, ciphers.Length, &errorState);
229+
UnityTls.NativeInterface.unitytls_tlsctx_set_supported_ciphersuites (m_TlsContext, ciphersPtr, ciphers.Length, &errorState);
234230
Unity.Debug.CheckAndThrow (errorState, "Failed to set list of supported ciphers", AlertDescription.HandshakeFailure);
235231
}
236232
}
237233

238234
public override bool ProcessHandshake ()
239235
{
240-
var errorState = unityTlsNative.unitytls_errorstate_create ();
241-
var result = unityTlsNative.unitytls_tlsctx_process_handshake (m_TlsContext, &errorState);
236+
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
237+
var result = UnityTls.NativeInterface.unitytls_tlsctx_process_handshake (m_TlsContext, &errorState);
242238
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
243239
return false;
244240

@@ -261,8 +257,8 @@ public override bool ProcessHandshake ()
261257
public override void FinishHandshake ()
262258
{
263259
// Query some data. Ignore errors on the way since failure is not crucial.
264-
var cipherSuite = unityTlsNative.unitytls_tlsctx_get_ciphersuite(m_TlsContext, null);
265-
var protocolVersion = unityTlsNative.unitytls_tlsctx_get_protocol(m_TlsContext, null);
260+
var cipherSuite = UnityTls.NativeInterface.unitytls_tlsctx_get_ciphersuite(m_TlsContext, null);
261+
var protocolVersion = UnityTls.NativeInterface.unitytls_tlsctx_get_protocol(m_TlsContext, null);
266262

267263
m_Connectioninfo = new MonoTlsConnectionInfo () {
268264
CipherSuiteCode = (CipherSuiteCode)cipherSuite,
@@ -296,13 +292,13 @@ private size_t WriteCallback (byte* data, size_t bufferLen, UnityTls.unitytls_er
296292
Marshal.Copy ((IntPtr)data, m_WriteBuffer, 0, bufferLen);
297293

298294
if (!Parent.InternalWrite (m_WriteBuffer, 0, bufferLen)) {
299-
unityTlsNative.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WRITE_FAILED);
295+
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WRITE_FAILED);
300296
return 0;
301297
}
302298

303299
return bufferLen;
304300
} catch { // handle all exceptions since we don't want to let them go through native code.
305-
unityTlsNative.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
301+
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
306302
return 0;
307303
}
308304
}
@@ -324,18 +320,18 @@ private size_t ReadCallback (byte* buffer, size_t bufferLen, UnityTls.unitytls_e
324320
bool wouldBlock;
325321
int numBytesRead = Parent.InternalRead (m_ReadBuffer, 0, bufferLen, out wouldBlock);
326322
if (wouldBlock) {
327-
unityTlsNative.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK);
323+
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK);
328324
return 0;
329325
}
330326
if (numBytesRead < 0) {
331-
unityTlsNative.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_READ_FAILED);
327+
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_READ_FAILED);
332328
return 0;
333329
}
334330

335331
Marshal.Copy (m_ReadBuffer, 0, (IntPtr)buffer, bufferLen);
336332
return numBytesRead;
337333
} catch { // handle all exceptions since we don't want to let them go through native code.
338-
unityTlsNative.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
334+
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
339335
return 0;
340336
}
341337
}

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

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

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

8987
CertHelper.AddCertificatesToNativeChain (certificatesNative, certificates, &errorState);
90-
var certificatesNativeRef = unityTlsNative.unitytls_x509list_get_ref (certificatesNative, &errorState);
88+
var certificatesNativeRef = UnityTls.NativeInterface.unitytls_x509list_get_ref (certificatesNative, &errorState);
9189
var targetHostUtf8 = Encoding.UTF8.GetBytes (targetHost);
9290

9391
if (validator.Settings.TrustAnchors != null) {
94-
var trustCAnative = unityTlsNative.unitytls_x509list_create (&errorState);
92+
var trustCAnative = UnityTls.NativeInterface.unitytls_x509list_create (&errorState);
9593
CertHelper.AddCertificatesToNativeChain (trustCAnative, validator.Settings.TrustAnchors, &errorState);
96-
var trustCAnativeRef = unityTlsNative.unitytls_x509list_get_ref (certificatesNative, &errorState);
94+
var trustCAnativeRef = UnityTls.NativeInterface.unitytls_x509list_get_ref (certificatesNative, &errorState);
9795

9896
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
99-
result = unityTlsNative.unitytls_x509verify_explicit_ca (certificatesNativeRef, trustCAnativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
97+
result = UnityTls.NativeInterface.unitytls_x509verify_explicit_ca (certificatesNativeRef, trustCAnativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
10098
}
10199

102-
unityTlsNative.unitytls_x509list_free (trustCAnative);
100+
UnityTls.NativeInterface.unitytls_x509list_free (trustCAnative);
103101
} else {
104102
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
105-
result = unityTlsNative.unitytls_x509verify_default_ca (certificatesNativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
103+
result = UnityTls.NativeInterface.unitytls_x509verify_default_ca (certificatesNativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
106104
}
107105
}
108106
}
109107
finally
110108
{
111-
unityTlsNative.unitytls_x509list_free (certificatesNative);
109+
UnityTls.NativeInterface.unitytls_x509list_free (certificatesNative);
112110
}
113111

114112
errors = UnityTlsConversions.VerifyResultToPolicyErrror(result);

0 commit comments

Comments
 (0)