Skip to content

Commit 0768508

Browse files
committed
[unitytls] Handling gracefully closed connections properly now. Sending close notify if requested.
1 parent d476b39 commit 0768508

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

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)