@@ -28,6 +28,8 @@ unsafe internal class UnityTlsContext : MobileTlsContext
28
28
{
29
29
private const bool ActivateTracing = false ;
30
30
31
+ private UnityTls . mono_unity_unitytls_interface unityTlsNative ;
32
+
31
33
// Native UnityTls objects
32
34
private UnityTls . unitytls_tlsctx * m_TlsContext = null ;
33
35
@@ -51,10 +53,12 @@ public UnityTlsContext (
51
53
X509CertificateCollection clientCertificates , bool askForClientCert )
52
54
: base ( parent , serverMode , targetHost , enabledProtocols , serverCertificate , clientCertificates , askForClientCert )
53
55
{
56
+ unityTlsNative = UnityTls . GetInterface ( ) ;
57
+
54
58
// Need GCHandle to get a consistent pointer to this instance
55
59
m_handle = GCHandle . Alloc ( this ) ;
56
60
57
- var errorState = UnityTls . GetInterface ( ) . unitytls_errorstate_create ( ) ;
61
+ var errorState = unityTlsNative . unitytls_errorstate_create ( ) ;
58
62
59
63
// Map selected protocols as best as we can.
60
64
UnityTls . unitytls_tlsctx_protocolrange protocolRange = new UnityTls . unitytls_tlsctx_protocolrange {
@@ -75,40 +79,41 @@ public UnityTlsContext (
75
79
if ( serverCertificate2 == null || serverCertificate2 . PrivateKey == null )
76
80
throw new ArgumentException ( "serverCertificate does not have a private key" , "serverCertificate" ) ;
77
81
82
+
78
83
UnityTls . unitytls_x509list * serverCerts = null ;
79
84
UnityTls . unitytls_key * serverPrivateKey = null ;
80
85
try {
81
- serverCerts = UnityTls . GetInterface ( ) . unitytls_x509list_create ( & errorState ) ;
86
+ serverCerts = unityTlsNative . unitytls_x509list_create ( & errorState ) ;
82
87
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 ) ;
84
89
85
90
byte [ ] privateKeyDer = PKCS8 . PrivateKeyInfo . Encode ( serverCertificate2 . PrivateKey ) ;
86
91
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 ) ;
88
93
}
89
- var serverKeyRef = UnityTls . GetInterface ( ) . unitytls_key_get_ref ( serverPrivateKey , & errorState ) ;
94
+ var serverKeyRef = unityTlsNative . unitytls_key_get_ref ( serverPrivateKey , & errorState ) ;
90
95
91
96
Mono . Unity . Debug . CheckAndThrow ( errorState , "Failed to parse server key/certificate" ) ;
92
97
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 ) ;
94
99
} 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 ) ;
97
102
}
98
103
}
99
104
else {
100
105
byte [ ] targetHostUtf8 = Encoding . UTF8 . GetBytes ( targetHost ) ;
101
106
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 ) ;
103
108
}
104
109
}
105
110
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 ) ;
107
112
108
113
Mono . Unity . Debug . CheckAndThrow ( errorState , "Failed to create UnityTls context" ) ;
109
114
110
115
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 ) ;
112
117
Mono . Unity . Debug . CheckAndThrow ( errorState , "Failed to set trace callback" ) ;
113
118
}
114
119
@@ -149,9 +154,9 @@ public override (int ret, bool wantMore) Read (byte[] buffer, int offset, int co
149
154
bool wouldBlock = false ;
150
155
int numBytesRead = 0 ;
151
156
152
- var errorState = UnityTls . GetInterface ( ) . unitytls_errorstate_create ( ) ;
157
+ var errorState = unityTlsNative . unitytls_errorstate_create ( ) ;
153
158
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 ) ;
155
160
}
156
161
157
162
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
167
172
bool wouldBlock = false ;
168
173
int numBytesWritten = 0 ;
169
174
170
- var errorState = UnityTls . GetInterface ( ) . unitytls_errorstate_create ( ) ;
175
+ var errorState = unityTlsNative . unitytls_errorstate_create ( ) ;
171
176
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 ) ;
173
178
}
174
179
175
180
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
183
188
public override void Shutdown ( )
184
189
{
185
190
// Destroy native UnityTls objects
186
- UnityTls . GetInterface ( ) . unitytls_tlsctx_free ( m_TlsContext ) ;
191
+ unityTlsNative . unitytls_tlsctx_free ( m_TlsContext ) ;
187
192
m_TlsContext = null ;
188
193
189
194
m_HasContext = false ;
@@ -248,8 +253,8 @@ public override bool ProcessHandshake ()
248
253
public override void FinishHandshake ( )
249
254
{
250
255
// 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 ) ;
253
258
254
259
m_Connectioninfo = new MonoTlsConnectionInfo ( ) {
255
260
CipherSuiteCode = ( CipherSuiteCode ) cipherSuite ,
@@ -283,13 +288,13 @@ private size_t WriteCallback (byte* data, size_t bufferLen, UnityTls.unitytls_er
283
288
Marshal . Copy ( ( IntPtr ) data , m_WriteBuffer , 0 , bufferLen ) ;
284
289
285
290
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 ) ;
287
292
return 0 ;
288
293
}
289
294
290
295
return bufferLen ;
291
296
} 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 ) ;
293
298
return 0 ;
294
299
}
295
300
}
@@ -311,18 +316,18 @@ private size_t ReadCallback (byte* buffer, size_t bufferLen, UnityTls.unitytls_e
311
316
bool wouldBlock ;
312
317
int numBytesRead = Parent . InternalRead ( m_ReadBuffer , 0 , bufferLen , out wouldBlock ) ;
313
318
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 ) ;
315
320
return 0 ;
316
321
}
317
322
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 ) ;
319
324
return 0 ;
320
325
}
321
326
322
327
Marshal . Copy ( m_ReadBuffer , 0 , ( IntPtr ) buffer , bufferLen ) ;
323
328
return numBytesRead ;
324
329
} 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 ) ;
326
331
return 0 ;
327
332
}
328
333
}
0 commit comments