@@ -31,22 +31,22 @@ unsafe internal class UnityTlsContext : MobileTlsContext
31
31
private const bool ActivateTracing = false ;
32
32
33
33
// Native UnityTls objects
34
- private UnityTls . unitytls_tlsctx * m_TlsContext = null ;
35
- private UnityTls . unitytls_x509list * m_RequestedClientCertChain = null ;
36
- private UnityTls . unitytls_key * m_RequestedClientKey = null ;
34
+ UnityTls . unitytls_tlsctx * tlsContext = null ;
35
+ UnityTls . unitytls_x509list * requestedClientCertChain = null ;
36
+ UnityTls . unitytls_key * requestedClientKey = null ;
37
37
38
38
// States and certificates
39
- X509Certificate m_LocalClientCertificate ;
40
- X509Certificate m_RemoteCertificate ;
41
- MonoTlsConnectionInfo m_Connectioninfo ;
42
- bool m_IsAuthenticated = false ;
43
- bool m_HasContext = false ;
39
+ X509Certificate localClientCertificate ;
40
+ X509Certificate remoteCertificate ;
41
+ MonoTlsConnectionInfo connectioninfo ;
42
+ bool isAuthenticated = false ;
43
+ bool hasContext = false ;
44
44
45
45
// Memory-buffer
46
- byte [ ] m_WriteBuffer ;
47
- byte [ ] m_ReadBuffer ;
46
+ byte [ ] writeBuffer ;
47
+ byte [ ] readBuffer ;
48
48
49
- GCHandle m_handle ;
49
+ GCHandle handle ;
50
50
Exception lastException ;
51
51
52
52
public UnityTlsContext (
@@ -57,7 +57,7 @@ public UnityTlsContext (
57
57
: base ( parent , serverMode , targetHost , enabledProtocols , serverCertificate , clientCertificates , askForClientCert )
58
58
{
59
59
// Need GCHandle to get a consistent pointer to this instance
60
- m_handle = GCHandle . Alloc ( this ) ;
60
+ handle = GCHandle . Alloc ( this ) ;
61
61
62
62
var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
63
63
@@ -70,7 +70,7 @@ public UnityTlsContext (
70
70
UnityTls . unitytls_tlsctx_callbacks callbacks = new UnityTls . unitytls_tlsctx_callbacks {
71
71
write = WriteCallback ,
72
72
read = ReadCallback ,
73
- data = ( void * ) ( IntPtr ) m_handle ,
73
+ data = ( void * ) ( IntPtr ) handle ,
74
74
} ;
75
75
76
76
if ( serverMode ) {
@@ -80,14 +80,14 @@ public UnityTlsContext (
80
80
var serverKeyRef = UnityTls . NativeInterface . unitytls_key_get_ref ( serverPrivateKey , & errorState ) ;
81
81
Mono . Unity . Debug . CheckAndThrow ( errorState , "Failed to parse server key/certificate" ) ;
82
82
83
- m_TlsContext = UnityTls . NativeInterface . unitytls_tlsctx_create_server ( protocolRange , callbacks , serverCertsRef , serverKeyRef , & errorState ) ;
83
+ tlsContext = UnityTls . NativeInterface . unitytls_tlsctx_create_server ( protocolRange , callbacks , serverCertsRef , serverKeyRef , & errorState ) ;
84
84
85
85
if ( askForClientCert ) {
86
86
UnityTls . unitytls_x509list * clientAuthCAList = null ;
87
87
try {
88
88
clientAuthCAList = UnityTls . NativeInterface . unitytls_x509list_create ( & errorState ) ;
89
89
var clientAuthCAListRef = UnityTls . NativeInterface . unitytls_x509list_get_ref ( clientAuthCAList , & errorState ) ;
90
- UnityTls . NativeInterface . unitytls_tlsctx_server_require_client_authentication ( m_TlsContext , clientAuthCAListRef , & errorState ) ;
90
+ UnityTls . NativeInterface . unitytls_tlsctx_server_require_client_authentication ( tlsContext , clientAuthCAListRef , & errorState ) ;
91
91
} finally {
92
92
UnityTls . NativeInterface . unitytls_x509list_free ( clientAuthCAList ) ;
93
93
}
@@ -100,22 +100,22 @@ public UnityTlsContext (
100
100
else {
101
101
byte [ ] targetHostUtf8 = Encoding . UTF8 . GetBytes ( targetHost ) ;
102
102
fixed ( byte * targetHostUtf8Ptr = targetHostUtf8 ) {
103
- m_TlsContext = UnityTls . NativeInterface . unitytls_tlsctx_create_client ( protocolRange , callbacks , targetHostUtf8Ptr , targetHostUtf8 . Length , & errorState ) ;
103
+ tlsContext = UnityTls . NativeInterface . unitytls_tlsctx_create_client ( protocolRange , callbacks , targetHostUtf8Ptr , targetHostUtf8 . Length , & errorState ) ;
104
104
}
105
105
106
- UnityTls . NativeInterface . unitytls_tlsctx_set_certificate_callback ( m_TlsContext , CertificateCallback , ( void * ) ( IntPtr ) m_handle , & errorState ) ;
106
+ UnityTls . NativeInterface . unitytls_tlsctx_set_certificate_callback ( tlsContext , CertificateCallback , ( void * ) ( IntPtr ) handle , & errorState ) ;
107
107
}
108
108
109
- UnityTls . NativeInterface . unitytls_tlsctx_set_x509verify_callback ( m_TlsContext , VerifyCallback , ( void * ) ( IntPtr ) m_handle , & errorState ) ;
109
+ UnityTls . NativeInterface . unitytls_tlsctx_set_x509verify_callback ( tlsContext , VerifyCallback , ( void * ) ( IntPtr ) handle , & errorState ) ;
110
110
111
111
Mono . Unity . Debug . CheckAndThrow ( errorState , "Failed to create UnityTls context" ) ;
112
112
113
113
if ( ActivateTracing ) {
114
- UnityTls . NativeInterface . unitytls_tlsctx_set_trace_callback ( m_TlsContext , TraceCallback , null , & errorState ) ;
114
+ UnityTls . NativeInterface . unitytls_tlsctx_set_trace_callback ( tlsContext , TraceCallback , null , & errorState ) ;
115
115
Mono . Unity . Debug . CheckAndThrow ( errorState , "Failed to set trace callback" ) ;
116
116
}
117
117
118
- m_HasContext = true ;
118
+ hasContext = true ;
119
119
}
120
120
121
121
static private void ExtractNativeKeyAndChainFromManagedCertificate ( X509Certificate cert , UnityTls . unitytls_errorstate * errorState , out UnityTls . unitytls_x509list * nativeCertChain , out UnityTls . unitytls_key * nativeKey )
@@ -144,22 +144,22 @@ static private void ExtractNativeKeyAndChainFromManagedCertificate(X509Certifica
144
144
}
145
145
146
146
public override bool HasContext {
147
- get { return m_HasContext ; }
147
+ get { return hasContext ; }
148
148
}
149
149
public override bool IsAuthenticated {
150
- get { return m_IsAuthenticated ; }
150
+ get { return isAuthenticated ; }
151
151
}
152
152
public override MonoTlsConnectionInfo ConnectionInfo {
153
- get { return m_Connectioninfo ; }
153
+ get { return connectioninfo ; }
154
154
}
155
155
internal override bool IsRemoteCertificateAvailable {
156
- get { return m_RemoteCertificate != null ; }
156
+ get { return remoteCertificate != null ; }
157
157
}
158
158
internal override X509Certificate LocalClientCertificate {
159
- get { return m_LocalClientCertificate ; }
159
+ get { return localClientCertificate ; }
160
160
}
161
161
public override X509Certificate RemoteCertificate {
162
- get { return m_RemoteCertificate ; }
162
+ get { return remoteCertificate ; }
163
163
}
164
164
public override TlsProtocols NegotiatedProtocol {
165
165
get { return ConnectionInfo . ProtocolVersion ; }
@@ -178,7 +178,7 @@ public override (int ret, bool wantMore) Read (byte[] buffer, int offset, int co
178
178
lastException = null ;
179
179
var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
180
180
fixed ( byte * bufferPtr = buffer ) {
181
- numBytesRead = UnityTls . NativeInterface . unitytls_tlsctx_read ( m_TlsContext , bufferPtr + offset , count , & errorState ) ;
181
+ numBytesRead = UnityTls . NativeInterface . unitytls_tlsctx_read ( tlsContext , bufferPtr + offset , count , & errorState ) ;
182
182
}
183
183
if ( lastException != null )
184
184
throw lastException ;
@@ -199,7 +199,7 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
199
199
lastException = null ;
200
200
var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
201
201
fixed ( byte * bufferPtr = buffer ) {
202
- numBytesWritten = UnityTls . NativeInterface . unitytls_tlsctx_write ( m_TlsContext , bufferPtr + offset , count , & errorState ) ;
202
+ numBytesWritten = UnityTls . NativeInterface . unitytls_tlsctx_write ( tlsContext , bufferPtr + offset , count , & errorState ) ;
203
203
}
204
204
if ( lastException != null )
205
205
throw lastException ;
@@ -215,12 +215,12 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
215
215
public override void Shutdown ( )
216
216
{
217
217
// Destroy native UnityTls objects
218
- UnityTls . NativeInterface . unitytls_x509list_free ( m_RequestedClientCertChain ) ;
219
- UnityTls . NativeInterface . unitytls_key_free ( m_RequestedClientKey ) ;
220
- UnityTls . NativeInterface . unitytls_tlsctx_free ( m_TlsContext ) ;
221
- m_TlsContext = null ;
218
+ UnityTls . NativeInterface . unitytls_x509list_free ( requestedClientCertChain ) ;
219
+ UnityTls . NativeInterface . unitytls_key_free ( requestedClientKey ) ;
220
+ UnityTls . NativeInterface . unitytls_tlsctx_free ( tlsContext ) ;
221
+ tlsContext = null ;
222
222
223
- m_HasContext = false ;
223
+ hasContext = false ;
224
224
}
225
225
226
226
protected override void Dispose ( bool disposing )
@@ -231,24 +231,24 @@ protected override void Dispose (bool disposing)
231
231
Shutdown ( ) ;
232
232
233
233
// reset states
234
- m_LocalClientCertificate = null ;
235
- m_RemoteCertificate = null ;
234
+ localClientCertificate = null ;
235
+ remoteCertificate = null ;
236
236
237
- if ( m_LocalClientCertificate != null ) {
238
- m_LocalClientCertificate . Dispose ( ) ;
239
- m_LocalClientCertificate = null ;
237
+ if ( localClientCertificate != null ) {
238
+ localClientCertificate . Dispose ( ) ;
239
+ localClientCertificate = null ;
240
240
}
241
- if ( m_RemoteCertificate != null ) {
242
- m_RemoteCertificate . Dispose ( ) ;
243
- m_RemoteCertificate = null ;
241
+ if ( remoteCertificate != null ) {
242
+ remoteCertificate . Dispose ( ) ;
243
+ remoteCertificate = null ;
244
244
}
245
245
246
- m_Connectioninfo = null ;
247
- m_IsAuthenticated = false ;
248
- m_HasContext = false ;
246
+ connectioninfo = null ;
247
+ isAuthenticated = false ;
248
+ hasContext = false ;
249
249
}
250
250
251
- m_handle . Free ( ) ;
251
+ handle . Free ( ) ;
252
252
253
253
} finally {
254
254
base . Dispose ( disposing ) ;
@@ -264,7 +264,7 @@ public override void StartHandshake ()
264
264
265
265
var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
266
266
fixed ( UnityTls . unitytls_ciphersuite * ciphersPtr = ciphers )
267
- UnityTls . NativeInterface . unitytls_tlsctx_set_supported_ciphersuites ( m_TlsContext , ciphersPtr , ciphers . Length , & errorState ) ;
267
+ UnityTls . NativeInterface . unitytls_tlsctx_set_supported_ciphersuites ( tlsContext , ciphersPtr , ciphers . Length , & errorState ) ;
268
268
Unity . Debug . CheckAndThrow ( errorState , "Failed to set list of supported ciphers" , AlertDescription . HandshakeFailure ) ;
269
269
}
270
270
}
@@ -273,7 +273,7 @@ public override bool ProcessHandshake ()
273
273
{
274
274
lastException = null ;
275
275
var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
276
- var result = UnityTls . NativeInterface . unitytls_tlsctx_process_handshake ( m_TlsContext , & errorState ) ;
276
+ var result = UnityTls . NativeInterface . unitytls_tlsctx_process_handshake ( tlsContext , & errorState ) ;
277
277
if ( errorState . code == UnityTls . unitytls_error_code . UNITYTLS_USER_WOULD_BLOCK )
278
278
return false ;
279
279
if ( lastException != null )
@@ -299,10 +299,10 @@ public override void FinishHandshake ()
299
299
{
300
300
// Query some data. Ignore errors on the way since failure is not crucial.
301
301
var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
302
- var cipherSuite = UnityTls . NativeInterface . unitytls_tlsctx_get_ciphersuite ( m_TlsContext , & errorState ) ;
303
- var protocolVersion = UnityTls . NativeInterface . unitytls_tlsctx_get_protocol ( m_TlsContext , & errorState ) ;
302
+ var cipherSuite = UnityTls . NativeInterface . unitytls_tlsctx_get_ciphersuite ( tlsContext , & errorState ) ;
303
+ var protocolVersion = UnityTls . NativeInterface . unitytls_tlsctx_get_protocol ( tlsContext , & errorState ) ;
304
304
305
- m_Connectioninfo = new MonoTlsConnectionInfo ( ) {
305
+ connectioninfo = new MonoTlsConnectionInfo ( ) {
306
306
CipherSuiteCode = ( CipherSuiteCode ) cipherSuite ,
307
307
ProtocolVersion = UnityTlsConversions . ConvertProtocolVersion ( protocolVersion ) ,
308
308
PeerDomainName = ServerName
@@ -315,7 +315,7 @@ public override void FinishHandshake ()
315
315
//HashAlgorithmType
316
316
//ExchangeAlgorithmType
317
317
} ;
318
- m_IsAuthenticated = true ;
318
+ isAuthenticated = true ;
319
319
}
320
320
321
321
[ MonoPInvokeCallback ( typeof ( UnityTls . unitytls_tlsctx_write_callback ) ) ]
@@ -329,11 +329,11 @@ static private size_t WriteCallback (void* userData, byte* data, size_t bufferLe
329
329
private size_t WriteCallback ( byte * data , size_t bufferLen , UnityTls . unitytls_errorstate * errorState )
330
330
{
331
331
try {
332
- if ( m_WriteBuffer == null || m_WriteBuffer . Length < bufferLen )
333
- m_WriteBuffer = new byte [ bufferLen ] ;
334
- Marshal . Copy ( ( IntPtr ) data , m_WriteBuffer , 0 , bufferLen ) ;
332
+ if ( writeBuffer == null || writeBuffer . Length < bufferLen )
333
+ writeBuffer = new byte [ bufferLen ] ;
334
+ Marshal . Copy ( ( IntPtr ) data , writeBuffer , 0 , bufferLen ) ;
335
335
336
- if ( ! Parent . InternalWrite ( m_WriteBuffer , 0 , bufferLen ) ) {
336
+ if ( ! Parent . InternalWrite ( writeBuffer , 0 , bufferLen ) ) {
337
337
UnityTls . NativeInterface . unitytls_errorstate_raise_error ( errorState , UnityTls . unitytls_error_code . UNITYTLS_USER_WRITE_FAILED ) ;
338
338
return 0 ;
339
339
}
@@ -358,11 +358,11 @@ static private size_t ReadCallback (void* userData, byte* buffer, size_t bufferL
358
358
private size_t ReadCallback ( byte * buffer , size_t bufferLen , UnityTls . unitytls_errorstate * errorState )
359
359
{
360
360
try {
361
- if ( m_ReadBuffer == null || m_ReadBuffer . Length < bufferLen )
362
- m_ReadBuffer = new byte [ bufferLen ] ;
361
+ if ( readBuffer == null || readBuffer . Length < bufferLen )
362
+ readBuffer = new byte [ bufferLen ] ;
363
363
364
364
bool wouldBlock ;
365
- int numBytesRead = Parent . InternalRead ( m_ReadBuffer , 0 , bufferLen , out wouldBlock ) ;
365
+ int numBytesRead = Parent . InternalRead ( readBuffer , 0 , bufferLen , out wouldBlock ) ;
366
366
if ( wouldBlock ) {
367
367
UnityTls . NativeInterface . unitytls_errorstate_raise_error ( errorState , UnityTls . unitytls_error_code . UNITYTLS_USER_WOULD_BLOCK ) ;
368
368
return 0 ;
@@ -372,7 +372,7 @@ private size_t ReadCallback (byte* buffer, size_t bufferLen, UnityTls.unitytls_e
372
372
return 0 ;
373
373
}
374
374
375
- Marshal . Copy ( m_ReadBuffer , 0 , ( IntPtr ) buffer , bufferLen ) ;
375
+ Marshal . Copy ( readBuffer , 0 , ( IntPtr ) buffer , bufferLen ) ;
376
376
return numBytesRead ;
377
377
} catch ( Exception ex ) { // handle all exceptions and store them for later since we don't want to let them go through native code.
378
378
UnityTls . NativeInterface . unitytls_errorstate_raise_error ( errorState , UnityTls . unitytls_error_code . UNITYTLS_USER_UNKNOWN_ERROR ) ;
@@ -394,7 +394,7 @@ private UnityTls.unitytls_x509verify_result VerifyCallback (UnityTls.unitytls_x5
394
394
{
395
395
try {
396
396
X509CertificateCollection certificates = CertHelper . NativeChainToManagedCollection ( chain , errorState ) ;
397
- m_RemoteCertificate = new X509Certificate ( certificates [ 0 ] ) ;
397
+ remoteCertificate = new X509Certificate ( certificates [ 0 ] ) ;
398
398
399
399
if ( ValidateCertificate ( certificates ) )
400
400
return UnityTls . unitytls_x509verify_result . UNITYTLS_X509VERIFY_SUCCESS ;
@@ -419,23 +419,23 @@ static private void CertificateCallback (void* userData, UnityTls.unitytls_tlsct
419
419
private void CertificateCallback ( UnityTls . unitytls_tlsctx * ctx , Int8 * cn , size_t cnLen , UnityTls . unitytls_x509name * caList , size_t caListLen , UnityTls . unitytls_x509list_ref * chain , UnityTls . unitytls_key_ref * key , UnityTls . unitytls_errorstate * errorState )
420
420
{
421
421
try {
422
- if ( m_RemoteCertificate == null )
422
+ if ( remoteCertificate == null )
423
423
throw new TlsException ( AlertDescription . InternalError , "Cannot request client certificate before receiving one from the server." ) ;
424
424
425
- m_LocalClientCertificate = SelectClientCertificate ( m_RemoteCertificate , null ) ;
425
+ localClientCertificate = SelectClientCertificate ( remoteCertificate , null ) ;
426
426
427
- if ( m_LocalClientCertificate == null ) {
427
+ if ( localClientCertificate == null ) {
428
428
* chain = new UnityTls . unitytls_x509list_ref { handle = UnityTls . NativeInterface . UNITYTLS_INVALID_HANDLE } ;
429
429
* key = new UnityTls . unitytls_key_ref { handle = UnityTls . NativeInterface . UNITYTLS_INVALID_HANDLE } ;
430
430
} else {
431
431
// Need to create native objects for client chain/key. Need to keep them cached.
432
432
// Make sure we don't have old native objects still around.
433
- UnityTls . NativeInterface . unitytls_x509list_free ( m_RequestedClientCertChain ) ;
434
- UnityTls . NativeInterface . unitytls_key_free ( m_RequestedClientKey ) ;
433
+ UnityTls . NativeInterface . unitytls_x509list_free ( requestedClientCertChain ) ;
434
+ UnityTls . NativeInterface . unitytls_key_free ( requestedClientKey ) ;
435
435
436
- ExtractNativeKeyAndChainFromManagedCertificate ( m_LocalClientCertificate , errorState , out m_RequestedClientCertChain , out m_RequestedClientKey ) ;
437
- * chain = UnityTls . NativeInterface . unitytls_x509list_get_ref ( m_RequestedClientCertChain , errorState ) ;
438
- * key = UnityTls . NativeInterface . unitytls_key_get_ref ( m_RequestedClientKey , errorState ) ;
436
+ ExtractNativeKeyAndChainFromManagedCertificate ( localClientCertificate , errorState , out requestedClientCertChain , out requestedClientKey ) ;
437
+ * chain = UnityTls . NativeInterface . unitytls_x509list_get_ref ( requestedClientCertChain , errorState ) ;
438
+ * key = UnityTls . NativeInterface . unitytls_key_get_ref ( requestedClientKey , errorState ) ;
439
439
}
440
440
441
441
Unity . Debug . CheckAndThrow ( * errorState , "Failed to retrieve certificates on request." , AlertDescription . HandshakeFailure ) ;
0 commit comments