23
23
using Mono . Util ;
24
24
25
25
using Int8 = System . Byte ;
26
+ using size_t = System . IntPtr ;
26
27
27
28
namespace Mono . Unity
28
29
{
@@ -110,7 +111,7 @@ public UnityTlsContext (
110
111
else {
111
112
byte [ ] targetHostUtf8 = Encoding . UTF8 . GetBytes ( targetHost ) ;
112
113
fixed ( byte * targetHostUtf8Ptr = targetHostUtf8 ) {
113
- tlsContext = UnityTls . NativeInterface . unitytls_tlsctx_create_client ( protocolRange , callbacks , targetHostUtf8Ptr , targetHostUtf8 . Length , & errorState ) ;
114
+ tlsContext = UnityTls . NativeInterface . unitytls_tlsctx_create_client ( protocolRange , callbacks , targetHostUtf8Ptr , ( size_t ) targetHostUtf8 . Length , & errorState ) ;
114
115
}
115
116
116
117
certificateCallback = CertificateCallback ;
@@ -121,12 +122,14 @@ public UnityTlsContext (
121
122
UnityTls . NativeInterface . unitytls_tlsctx_set_x509verify_callback ( tlsContext , verifyCallback , ( void * ) ( IntPtr ) handle , & errorState ) ;
122
123
123
124
Mono . Unity . Debug . CheckAndThrow ( errorState , "Failed to create UnityTls context" ) ;
124
-
125
+
126
+ #pragma warning disable CS0162 // Disable unreachable code warning
125
127
if ( ActivateTracing ) {
126
128
traceCallback = TraceCallback ;
127
129
UnityTls . NativeInterface . unitytls_tlsctx_set_trace_callback ( tlsContext , traceCallback , null , & errorState ) ;
128
130
Mono . Unity . Debug . CheckAndThrow ( errorState , "Failed to set trace callback" ) ;
129
131
}
132
+ #pragma warning restore CS0162 // Reenable unreachable code warning.
130
133
131
134
hasContext = true ;
132
135
}
@@ -147,7 +150,7 @@ static private void ExtractNativeKeyAndChainFromManagedCertificate(X509Certifica
147
150
148
151
byte [ ] privateKeyDer = PKCS8 . PrivateKeyInfo . Encode ( cert2 . PrivateKey ) ;
149
152
fixed( byte * privateKeyDerPtr = privateKeyDer ) {
150
- nativeKey = UnityTls . NativeInterface . unitytls_key_parse_der ( privateKeyDerPtr , privateKeyDer . Length , null , 0 , errorState ) ;
153
+ nativeKey = UnityTls . NativeInterface . unitytls_key_parse_der ( privateKeyDerPtr , ( size_t ) privateKeyDer . Length , null , ( size_t ) 0 , errorState ) ;
151
154
}
152
155
} catch {
153
156
UnityTls . NativeInterface . unitytls_x509list_free ( nativeCertChain ) ;
@@ -190,7 +193,7 @@ public override (int ret, bool wantMore) Read (byte[] buffer, int offset, int co
190
193
lastException = null ;
191
194
var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
192
195
fixed ( byte * bufferPtr = buffer ) {
193
- numBytesRead = UnityTls . NativeInterface . unitytls_tlsctx_read ( tlsContext , bufferPtr + offset , count , & errorState ) ;
196
+ numBytesRead = ( int ) UnityTls . NativeInterface . unitytls_tlsctx_read ( tlsContext , bufferPtr + offset , ( size_t ) count , & errorState ) ;
194
197
}
195
198
if ( lastException != null )
196
199
throw lastException ;
@@ -223,7 +226,7 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
223
226
lastException = null ;
224
227
var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
225
228
fixed ( byte * bufferPtr = buffer ) {
226
- numBytesWritten = UnityTls . NativeInterface . unitytls_tlsctx_write ( tlsContext , bufferPtr + offset , count , & errorState ) ;
229
+ numBytesWritten = ( int ) UnityTls . NativeInterface . unitytls_tlsctx_write ( tlsContext , bufferPtr + offset , ( size_t ) count , & errorState ) ;
227
230
}
228
231
if ( lastException != null )
229
232
throw lastException ;
@@ -304,7 +307,7 @@ public override void StartHandshake ()
304
307
305
308
var errorState = UnityTls . NativeInterface . unitytls_errorstate_create ( ) ;
306
309
fixed ( UnityTls . unitytls_ciphersuite * ciphersPtr = ciphers )
307
- UnityTls . NativeInterface . unitytls_tlsctx_set_supported_ciphersuites ( tlsContext , ciphersPtr , ciphers . Length , & errorState ) ;
310
+ UnityTls . NativeInterface . unitytls_tlsctx_set_supported_ciphersuites ( tlsContext , ciphersPtr , ( size_t ) ciphers . Length , & errorState ) ;
308
311
Unity . Debug . CheckAndThrow ( errorState , "Failed to set list of supported ciphers" , AlertDescription . HandshakeFailure ) ;
309
312
}
310
313
}
@@ -368,21 +371,21 @@ static private size_t WriteCallback (void* userData, byte* data, size_t bufferLe
368
371
private size_t WriteCallback ( byte * data , size_t bufferLen , UnityTls . unitytls_errorstate * errorState )
369
372
{
370
373
try {
371
- if ( writeBuffer == null || writeBuffer . Length < bufferLen )
372
- writeBuffer = new byte [ bufferLen ] ;
373
- Marshal . Copy ( ( IntPtr ) data , writeBuffer , 0 , bufferLen ) ;
374
+ if ( writeBuffer == null || writeBuffer . Length < ( int ) bufferLen )
375
+ writeBuffer = new byte [ ( int ) bufferLen ] ;
376
+ Marshal . Copy ( ( IntPtr ) data , writeBuffer , 0 , ( int ) bufferLen ) ;
374
377
375
- if ( ! Parent . InternalWrite ( writeBuffer , 0 , bufferLen ) ) {
378
+ if ( ! Parent . InternalWrite ( writeBuffer , 0 , ( int ) bufferLen ) ) {
376
379
UnityTls . NativeInterface . unitytls_errorstate_raise_error ( errorState , UnityTls . unitytls_error_code . UNITYTLS_USER_WRITE_FAILED ) ;
377
- return 0 ;
380
+ return ( size_t ) 0 ;
378
381
}
379
382
380
383
return bufferLen ;
381
384
} catch ( Exception ex ) { // handle all exceptions and store them for later since we don't want to let them go through native code.
382
385
UnityTls . NativeInterface . unitytls_errorstate_raise_error ( errorState , UnityTls . unitytls_error_code . UNITYTLS_USER_UNKNOWN_ERROR ) ;
383
386
if ( lastException == null )
384
387
lastException = ex ;
385
- return 0 ;
388
+ return ( size_t ) 0 ;
386
389
}
387
390
}
388
391
@@ -397,17 +400,17 @@ static private size_t ReadCallback (void* userData, byte* buffer, size_t bufferL
397
400
private size_t ReadCallback ( byte * buffer , size_t bufferLen , UnityTls . unitytls_errorstate * errorState )
398
401
{
399
402
try {
400
- if ( readBuffer == null || readBuffer . Length < bufferLen )
401
- readBuffer = new byte [ bufferLen ] ;
403
+ if ( readBuffer == null || readBuffer . Length < ( int ) bufferLen )
404
+ readBuffer = new byte [ ( int ) bufferLen ] ;
402
405
403
406
bool wouldBlock ;
404
- int numBytesRead = Parent . InternalRead ( readBuffer , 0 , bufferLen , out wouldBlock ) ;
407
+ int numBytesRead = Parent . InternalRead ( readBuffer , 0 , ( int ) bufferLen , out wouldBlock ) ;
405
408
406
409
// Non graceful exit.
407
410
if ( numBytesRead < 0 ) {
408
411
UnityTls . NativeInterface . unitytls_errorstate_raise_error ( errorState , UnityTls . unitytls_error_code . UNITYTLS_USER_READ_FAILED ) ;
409
412
} else if ( numBytesRead > 0 ) {
410
- Marshal . Copy ( readBuffer , 0 , ( IntPtr ) buffer , bufferLen ) ;
413
+ Marshal . Copy ( readBuffer , 0 , ( IntPtr ) buffer , ( int ) bufferLen ) ;
411
414
} else { // numBytesRead == 0
412
415
// careful when rearranging this: wouldBlock might be true even if stream was closed abruptly.
413
416
if ( wouldBlock ) {
@@ -423,12 +426,12 @@ private size_t ReadCallback (byte* buffer, size_t bufferLen, UnityTls.unitytls_e
423
426
}
424
427
425
428
// Note that UnityTls ignores this number when raising an error.
426
- return numBytesRead ;
429
+ return ( size_t ) numBytesRead ;
427
430
} catch ( Exception ex ) { // handle all exceptions and store them for later since we don't want to let them go through native code.
428
431
UnityTls . NativeInterface . unitytls_errorstate_raise_error ( errorState , UnityTls . unitytls_error_code . UNITYTLS_USER_UNKNOWN_ERROR ) ;
429
432
if ( lastException == null )
430
433
lastException = ex ;
431
- return 0 ;
434
+ return ( size_t ) 0 ;
432
435
}
433
436
}
434
437
@@ -498,7 +501,7 @@ private void CertificateCallback (UnityTls.unitytls_tlsctx* ctx, Int8* cn, size_
498
501
[ MonoPInvokeCallback ( typeof ( UnityTls . unitytls_tlsctx_trace_callback ) ) ]
499
502
static private void TraceCallback ( void * userData , UnityTls . unitytls_tlsctx * ctx , byte * traceMessage , size_t traceMessageLen )
500
503
{
501
- string message = Encoding . UTF8 . GetString ( traceMessage , traceMessageLen ) ;
504
+ string message = Encoding . UTF8 . GetString ( traceMessage , ( int ) traceMessageLen ) ;
502
505
System . Console . Write ( message ) ;
503
506
}
504
507
}
0 commit comments