Skip to content

Commit 1dafe55

Browse files
committed
Replaced size_t class in UnityTls and replaced with simple aliases to IntPtr
Since compiler handle structs differently on function calls this may have caused issues when passing arguments between managed and native code. Even if not, we're clearly safer with the explicit casts now.
1 parent 04ae5cc commit 1dafe55

File tree

4 files changed

+34
-43
lines changed

4 files changed

+34
-43
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#if SECURITY_DEP
22
using System.Security.Cryptography.X509Certificates;
33

4+
using size_t = System.IntPtr;
5+
46
namespace Mono.Unity
57
{
68
internal unsafe static class CertHelper
@@ -16,7 +18,7 @@ public static void AddCertificateToNativeChain (UnityTls.unitytls_x509list* nati
1618
{
1719
byte[] certDer = certificate.GetRawCertData ();
1820
fixed(byte* certDerPtr = certDer) {
19-
UnityTls.NativeInterface.unitytls_x509list_append_der (nativeCertificateChain, certDerPtr, certDer.Length, errorState);
21+
UnityTls.NativeInterface.unitytls_x509list_append_der (nativeCertificateChain, certDerPtr, (size_t)certDer.Length, errorState);
2022
}
2123

2224
var certificateImpl2 = certificate.Impl as X509Certificate2Impl;
@@ -34,16 +36,16 @@ public static X509CertificateCollection NativeChainToManagedCollection (UnityTls
3436
{
3537
X509CertificateCollection certificates = new X509CertificateCollection ();
3638

37-
var cert = UnityTls.NativeInterface.unitytls_x509list_get_x509 (nativeCertificateChain, 0, errorState);
39+
var cert = UnityTls.NativeInterface.unitytls_x509list_get_x509 (nativeCertificateChain, (size_t)0, errorState);
3840
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);
40-
var certBuffer = new byte[certBufferSize]; // Need to reallocate every time since X509Certificate constructor takes no length but only a byte array.
41+
size_t certBufferSize = UnityTls.NativeInterface.unitytls_x509_export_der (cert, null, (size_t)0, errorState);
42+
var certBuffer = new byte[(int)certBufferSize]; // Need to reallocate every time since X509Certificate constructor takes no length but only a byte array.
4143
fixed(byte* certBufferPtr = certBuffer) {
4244
UnityTls.NativeInterface.unitytls_x509_export_der (cert, certBufferPtr, certBufferSize, errorState);
4345
}
4446
certificates.Add (new X509Certificate (certBuffer));
4547

46-
cert = UnityTls.NativeInterface.unitytls_x509list_get_x509 (nativeCertificateChain, i, errorState);
48+
cert = UnityTls.NativeInterface.unitytls_x509list_get_x509 (nativeCertificateChain, (size_t)i, errorState);
4749
}
4850

4951
return certificates;

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,7 @@ namespace Mono.Unity
99
// The aliases here are just there to keep the semantic in the interface and make it more similar to the c original.
1010
using UInt8 = Byte;
1111
using Int8 = Byte;
12-
13-
[StructLayout (LayoutKind.Sequential)]
14-
internal struct size_t
15-
{
16-
public size_t(uint i) {
17-
value = new IntPtr(i);
18-
}
19-
20-
public static implicit operator size_t(int d) {
21-
return new size_t((uint)d);
22-
}
23-
public static implicit operator int(size_t s) {
24-
return s.value.ToInt32();
25-
}
26-
27-
public IntPtr value;
28-
}
12+
using size_t = IntPtr;
2913

3014
unsafe internal static partial class UnityTls
3115
{

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using Mono.Util;
2424

2525
using Int8 = System.Byte;
26+
using size_t = System.IntPtr;
2627

2728
namespace Mono.Unity
2829
{
@@ -110,7 +111,7 @@ public UnityTlsContext (
110111
else {
111112
byte [] targetHostUtf8 = Encoding.UTF8.GetBytes (targetHost);
112113
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);
114115
}
115116

116117
certificateCallback = CertificateCallback;
@@ -121,12 +122,14 @@ public UnityTlsContext (
121122
UnityTls.NativeInterface.unitytls_tlsctx_set_x509verify_callback (tlsContext, verifyCallback, (void*)(IntPtr)handle, &errorState);
122123

123124
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to create UnityTls context");
124-
125+
126+
#pragma warning disable CS0162 // Disable unreachable code warning
125127
if (ActivateTracing) {
126128
traceCallback = TraceCallback;
127129
UnityTls.NativeInterface.unitytls_tlsctx_set_trace_callback (tlsContext, traceCallback, null, &errorState);
128130
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to set trace callback");
129131
}
132+
#pragma warning restore CS0162 // Reenable unreachable code warning.
130133

131134
hasContext = true;
132135
}
@@ -147,7 +150,7 @@ static private void ExtractNativeKeyAndChainFromManagedCertificate(X509Certifica
147150

148151
byte[] privateKeyDer = PKCS8.PrivateKeyInfo.Encode (cert2.PrivateKey);
149152
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);
151154
}
152155
} catch {
153156
UnityTls.NativeInterface.unitytls_x509list_free (nativeCertChain);
@@ -190,7 +193,7 @@ public override (int ret, bool wantMore) Read (byte[] buffer, int offset, int co
190193
lastException = null;
191194
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
192195
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);
194197
}
195198
if (lastException != null)
196199
throw lastException;
@@ -223,7 +226,7 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
223226
lastException = null;
224227
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
225228
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);
227230
}
228231
if (lastException != null)
229232
throw lastException;
@@ -304,7 +307,7 @@ public override void StartHandshake ()
304307

305308
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
306309
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);
308311
Unity.Debug.CheckAndThrow (errorState, "Failed to set list of supported ciphers", AlertDescription.HandshakeFailure);
309312
}
310313
}
@@ -368,21 +371,21 @@ static private size_t WriteCallback (void* userData, byte* data, size_t bufferLe
368371
private size_t WriteCallback (byte* data, size_t bufferLen, UnityTls.unitytls_errorstate* errorState)
369372
{
370373
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);
374377

375-
if (!Parent.InternalWrite (writeBuffer, 0, bufferLen)) {
378+
if (!Parent.InternalWrite (writeBuffer, 0, (int)bufferLen)) {
376379
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_WRITE_FAILED);
377-
return 0;
380+
return (size_t)0;
378381
}
379382

380383
return bufferLen;
381384
} catch (Exception ex) { // handle all exceptions and store them for later since we don't want to let them go through native code.
382385
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
383386
if (lastException == null)
384387
lastException = ex;
385-
return 0;
388+
return (size_t)0;
386389
}
387390
}
388391

@@ -397,17 +400,17 @@ static private size_t ReadCallback (void* userData, byte* buffer, size_t bufferL
397400
private size_t ReadCallback (byte* buffer, size_t bufferLen, UnityTls.unitytls_errorstate* errorState)
398401
{
399402
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];
402405

403406
bool wouldBlock;
404-
int numBytesRead = Parent.InternalRead (readBuffer, 0, bufferLen, out wouldBlock);
407+
int numBytesRead = Parent.InternalRead (readBuffer, 0, (int)bufferLen, out wouldBlock);
405408

406409
// Non graceful exit.
407410
if (numBytesRead < 0) {
408411
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_READ_FAILED);
409412
} else if (numBytesRead > 0) {
410-
Marshal.Copy (readBuffer, 0, (IntPtr)buffer, bufferLen);
413+
Marshal.Copy (readBuffer, 0, (IntPtr)buffer, (int)bufferLen);
411414
} else { // numBytesRead == 0
412415
// careful when rearranging this: wouldBlock might be true even if stream was closed abruptly.
413416
if (wouldBlock) {
@@ -423,12 +426,12 @@ private size_t ReadCallback (byte* buffer, size_t bufferLen, UnityTls.unitytls_e
423426
}
424427

425428
// Note that UnityTls ignores this number when raising an error.
426-
return numBytesRead;
429+
return (size_t)numBytesRead;
427430
} catch (Exception ex) { // handle all exceptions and store them for later since we don't want to let them go through native code.
428431
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
429432
if (lastException == null)
430433
lastException = ex;
431-
return 0;
434+
return (size_t)0;
432435
}
433436
}
434437

@@ -498,7 +501,7 @@ private void CertificateCallback (UnityTls.unitytls_tlsctx* ctx, Int8* cn, size_
498501
[MonoPInvokeCallback (typeof (UnityTls.unitytls_tlsctx_trace_callback))]
499502
static private void TraceCallback (void* userData, UnityTls.unitytls_tlsctx* ctx, byte* traceMessage, size_t traceMessageLen)
500503
{
501-
string message = Encoding.UTF8.GetString (traceMessage, traceMessageLen);
504+
string message = Encoding.UTF8.GetString (traceMessage, (int)traceMessageLen);
502505
System.Console.Write (message);
503506
}
504507
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using Mono.Security.Interface;
1818
#endif
1919

20+
using size_t = System.IntPtr;
21+
2022
namespace Mono.Unity
2123
{
2224
unsafe internal class UnityTlsProvider : MonoTlsProvider
@@ -93,13 +95,13 @@ internal override bool ValidateCertificate (
9395
var trustCAnativeRef = UnityTls.NativeInterface.unitytls_x509list_get_ref (certificatesNative, &errorState);
9496

9597
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
96-
result = UnityTls.NativeInterface.unitytls_x509verify_explicit_ca (certificatesNativeRef, trustCAnativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
98+
result = UnityTls.NativeInterface.unitytls_x509verify_explicit_ca (certificatesNativeRef, trustCAnativeRef, targetHostUtf8Ptr, (size_t)targetHostUtf8.Length, null, null, &errorState);
9799
}
98100

99101
UnityTls.NativeInterface.unitytls_x509list_free (trustCAnative);
100102
} else {
101103
fixed (byte* targetHostUtf8Ptr = targetHostUtf8) {
102-
result = UnityTls.NativeInterface.unitytls_x509verify_default_ca (certificatesNativeRef, targetHostUtf8Ptr, targetHostUtf8.Length, null, null, &errorState);
104+
result = UnityTls.NativeInterface.unitytls_x509verify_default_ca (certificatesNativeRef, targetHostUtf8Ptr, (size_t)targetHostUtf8.Length, null, null, &errorState);
103105
}
104106
}
105107
}

0 commit comments

Comments
 (0)