Skip to content

Commit 0541fdf

Browse files
authored
Merge pull request #838 from Unity-Technologies/unity-master-unitytls
UnityTls Interface update + handling of connection closing
2 parents 0897c51 + 0768508 commit 0541fdf

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ public enum unitytls_error_code : UInt32
4444
UNITYTLS_INTERNAL_ERROR, // Internal implementation error.
4545
UNITYTLS_NOT_SUPPORTED, // The requested action is not supported on the current platform/implementation.
4646
UNITYTLS_ENTROPY_SOURCE_FAILED, // Failed to generate requested amount of entropy data.
47+
UNITYTLS_STREAM_CLOSED, // The operation is not possible because the stream between the peers was closed.
4748

49+
UNITYTLS_USER_CUSTOM_ERROR_START = 0x100000,
4850
UNITYTLS_USER_WOULD_BLOCK, // Can be set by the user to signal that a call (e.g. read/write callback) would block and needs to be called again.
4951
// Some implementations may set this if not all bytes have been read/written.
50-
UNITYTLS_USER_STREAM_CLOSED, // Can be set by the user to cancel a read/write operation.
5152
UNITYTLS_USER_READ_FAILED, // Can be set by the user to indicate a failed read operation.
5253
UNITYTLS_USER_WRITE_FAILED, // Can be set by the user to indicate a failed write operation.
5354
UNITYTLS_USER_UNKNOWN_ERROR, // Can be set by the user to indicate a generic error.
55+
UNITYTLS_USER_CUSTOM_ERROR_END = 0x200000,
5456
}
5557

5658
[StructLayout (LayoutKind.Sequential)]
@@ -177,10 +179,10 @@ public class unitytls_interface_struct
177179

178180
public delegate unitytls_key_ref unitytls_key_get_ref_t(unitytls_key* key, unitytls_errorstate* errorState);
179181
public unitytls_key_get_ref_t unitytls_key_get_ref;
180-
public delegate unitytls_key* unitytls_key_parse_pem_t(Int8* buffer, size_t bufferLen, Int8* password, size_t passwordLen, unitytls_errorstate* errorState);
181-
public unitytls_key_parse_der_t unitytls_key_parse_pem;
182182
public delegate unitytls_key* unitytls_key_parse_der_t(UInt8* buffer, size_t bufferLen, Int8* password, size_t passwordLen, unitytls_errorstate* errorState);
183183
public unitytls_key_parse_der_t unitytls_key_parse_der;
184+
public delegate unitytls_key* unitytls_key_parse_pem_t(Int8* buffer, size_t bufferLen, Int8* password, size_t passwordLen, unitytls_errorstate* errorState);
185+
public unitytls_key_parse_pem_t unitytls_key_parse_pem;
184186
public delegate void unitytls_key_free_t(unitytls_key* key);
185187
public unitytls_key_free_t unitytls_key_free;
186188

@@ -231,8 +233,13 @@ public class unitytls_interface_struct
231233
public unitytls_tlsctx_read_t unitytls_tlsctx_read;
232234
public delegate size_t unitytls_tlsctx_write_t(unitytls_tlsctx* ctx, UInt8* data, size_t bufferLen, unitytls_errorstate* errorState);
233235
public unitytls_tlsctx_write_t unitytls_tlsctx_write;
236+
public delegate void unitytls_tlsctx_notify_close_t(unitytls_tlsctx* ctx, unitytls_errorstate* errorState);
237+
public unitytls_tlsctx_notify_close_t unitytls_tlsctx_notify_close;
234238
public delegate void unitytls_tlsctx_free_t(unitytls_tlsctx* ctx);
235239
public unitytls_tlsctx_free_t unitytls_tlsctx_free;
240+
241+
public delegate void unitytls_random_generate_bytes_t(UInt8 * buffer, size_t bufferLen, unitytls_errorstate * errorState);
242+
public unitytls_random_generate_bytes_t unitytls_random_generate_bytes;
236243
}
237244

238245
[MethodImplAttribute (MethodImplOptions.InternalCall)]

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ public override (int ret, bool wantMore) Read (byte[] buffer, int offset, int co
185185

186186
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
187187
wouldBlock = true;
188+
else if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_STREAM_CLOSED)
189+
return (0, false); // According to Apple and Btls implementation this is how we should handle gracefully closed connections.
188190
else
189191
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to read data from TLS context");
190192

@@ -206,6 +208,8 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
206208

207209
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
208210
wouldBlock = true;
211+
else if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_STREAM_CLOSED)
212+
return (0, false); // According to Apple and Btls implementation this is how we should handle gracefully closed connections.
209213
else
210214
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to write data to TLS context");
211215

@@ -214,6 +218,11 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
214218

215219
public override void Shutdown ()
216220
{
221+
if(Settings != null && Settings.SendCloseNotify) {
222+
var err = UnityTls.NativeInterface.unitytls_errorstate_create ();
223+
UnityTls.NativeInterface.unitytls_tlsctx_notify_close (tlsContext, &err);
224+
}
225+
217226
// Destroy native UnityTls objects
218227
UnityTls.NativeInterface.unitytls_x509list_free (requestedClientCertChain);
219228
UnityTls.NativeInterface.unitytls_key_free (requestedClientKey);
@@ -407,7 +416,6 @@ private UnityTls.unitytls_x509verify_result VerifyCallback (UnityTls.unitytls_x5
407416
}
408417
}
409418

410-
411419
[MonoPInvokeCallback (typeof (UnityTls.unitytls_tlsctx_certificate_callback))]
412420
static private void CertificateCallback (void* userData, 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)
413421
{

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ internal override bool ValidateCertificate (
8282
//validator.Settings.CheckCertificateRevocationStatus // not used by mono?
8383
//validator.Settings.CertificateValidationTime
8484
//validator.Settings.CertificateSearchPaths // currently only used by MonoBtlsProvider
85-
//validator.Settings.SendCloseNotify // UnityTls always sends a close notify if the underlying impl supports it. Currently only used by MonoBtlsProvider
8685

8786
CertHelper.AddCertificatesToNativeChain (certificatesNative, certificates, &errorState);
8887
var certificatesNativeRef = UnityTls.NativeInterface.unitytls_x509list_get_ref (certificatesNative, &errorState);

0 commit comments

Comments
 (0)