@@ -28,8 +28,6 @@ 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
-
33
31
// Native UnityTls objects
34
32
private UnityTls . unitytls_tlsctx * m_TlsContext = null ;
35
33
@@ -53,12 +51,10 @@ public UnityTlsContext (
53
51
X509CertificateCollection clientCertificates , bool askForClientCert )
54
52
: base ( parent , serverMode , targetHost , enabledProtocols , serverCertificate , clientCertificates , askForClientCert )
55
53
{
56
- unityTlsNative = UnityTls . NativeInterface ;
57
-
58
54
// Need GCHandle to get a consistent pointer to this instance
59
55
m_handle = GCHandle . Alloc ( this ) ;
60
56
61
- var errorState = unityTlsNative . unitytls_errorstate_create ( ) ;
57
+ var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
62
58
63
59
// Map selected protocols as best as we can.
64
60
UnityTls . unitytls_tlsctx_protocolrange protocolRange = new UnityTls . unitytls_tlsctx_protocolrange {
@@ -83,37 +79,37 @@ public UnityTlsContext (
83
79
UnityTls . unitytls_x509list * serverCerts = null ;
84
80
UnityTls . unitytls_key * serverPrivateKey = null ;
85
81
try {
86
- serverCerts = unityTlsNative . unitytls_x509list_create ( & errorState ) ;
82
+ serverCerts = UnityTls . NativeInterface . unitytls_x509list_create ( & errorState ) ;
87
83
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 ) ;
89
85
90
86
byte [ ] privateKeyDer = PKCS8 . PrivateKeyInfo . Encode ( serverCertificate2 . PrivateKey ) ;
91
87
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 ) ;
93
89
}
94
- var serverKeyRef = unityTlsNative . unitytls_key_get_ref ( serverPrivateKey , & errorState ) ;
90
+ var serverKeyRef = UnityTls . NativeInterface . unitytls_key_get_ref ( serverPrivateKey , & errorState ) ;
95
91
96
92
Mono . Unity . Debug . CheckAndThrow ( errorState , "Failed to parse server key/certificate" ) ;
97
93
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 ) ;
99
95
} 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 ) ;
102
98
}
103
99
}
104
100
else {
105
101
byte [ ] targetHostUtf8 = Encoding . UTF8 . GetBytes ( targetHost ) ;
106
102
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 ) ;
108
104
}
109
105
}
110
106
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 ) ;
112
108
113
109
Mono . Unity . Debug . CheckAndThrow ( errorState , "Failed to create UnityTls context" ) ;
114
110
115
111
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 ) ;
117
113
Mono . Unity . Debug . CheckAndThrow ( errorState , "Failed to set trace callback" ) ;
118
114
}
119
115
@@ -154,9 +150,9 @@ public override (int ret, bool wantMore) Read (byte[] buffer, int offset, int co
154
150
bool wouldBlock = false ;
155
151
int numBytesRead = 0 ;
156
152
157
- var errorState = unityTlsNative . unitytls_errorstate_create ( ) ;
153
+ var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
158
154
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 ) ;
160
156
}
161
157
162
158
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
172
168
bool wouldBlock = false ;
173
169
int numBytesWritten = 0 ;
174
170
175
- var errorState = unityTlsNative . unitytls_errorstate_create ( ) ;
171
+ var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
176
172
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 ) ;
178
174
}
179
175
180
176
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
188
184
public override void Shutdown ( )
189
185
{
190
186
// Destroy native UnityTls objects
191
- unityTlsNative . unitytls_tlsctx_free ( m_TlsContext ) ;
187
+ UnityTls . NativeInterface . unitytls_tlsctx_free ( m_TlsContext ) ;
192
188
m_TlsContext = null ;
193
189
194
190
m_HasContext = false ;
@@ -228,17 +224,17 @@ public override void StartHandshake ()
228
224
for ( int i = 0 ; i < ciphers . Length ; i ++ )
229
225
ciphers [ i ] = ( UnityTls . unitytls_ciphersuite ) Settings . EnabledCiphers [ i ] ;
230
226
231
- var errorState = unityTlsNative . unitytls_errorstate_create ( ) ;
227
+ var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
232
228
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 ) ;
234
230
Unity . Debug . CheckAndThrow ( errorState , "Failed to set list of supported ciphers" , AlertDescription . HandshakeFailure ) ;
235
231
}
236
232
}
237
233
238
234
public override bool ProcessHandshake ( )
239
235
{
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 ) ;
242
238
if ( errorState . code == UnityTls . unitytls_error_code . UNITYTLS_USER_WOULD_BLOCK )
243
239
return false ;
244
240
@@ -261,8 +257,8 @@ public override bool ProcessHandshake ()
261
257
public override void FinishHandshake ( )
262
258
{
263
259
// 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 ) ;
266
262
267
263
m_Connectioninfo = new MonoTlsConnectionInfo ( ) {
268
264
CipherSuiteCode = ( CipherSuiteCode ) cipherSuite ,
@@ -296,13 +292,13 @@ private size_t WriteCallback (byte* data, size_t bufferLen, UnityTls.unitytls_er
296
292
Marshal . Copy ( ( IntPtr ) data , m_WriteBuffer , 0 , bufferLen ) ;
297
293
298
294
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 ) ;
300
296
return 0 ;
301
297
}
302
298
303
299
return bufferLen ;
304
300
} 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 ) ;
306
302
return 0 ;
307
303
}
308
304
}
@@ -324,18 +320,18 @@ private size_t ReadCallback (byte* buffer, size_t bufferLen, UnityTls.unitytls_e
324
320
bool wouldBlock ;
325
321
int numBytesRead = Parent . InternalRead ( m_ReadBuffer , 0 , bufferLen , out wouldBlock ) ;
326
322
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 ) ;
328
324
return 0 ;
329
325
}
330
326
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 ) ;
332
328
return 0 ;
333
329
}
334
330
335
331
Marshal . Copy ( m_ReadBuffer , 0 , ( IntPtr ) buffer , bufferLen ) ;
336
332
return numBytesRead ;
337
333
} 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 ) ;
339
335
return 0 ;
340
336
}
341
337
}
0 commit comments