Skip to content

Commit 1165e17

Browse files
committed
[unitytls] User exceptions from read/write/handshake are now passed on properly.
1 parent fe8bf68 commit 1165e17

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ unsafe internal class UnityTlsContext : MobileTlsContext
4343
byte [] m_ReadBuffer;
4444

4545
GCHandle m_handle;
46+
Exception lastException;
4647

4748
public UnityTlsContext (
4849
MobileAuthenticatedStream parent,
@@ -150,10 +151,13 @@ public override (int ret, bool wantMore) Read (byte[] buffer, int offset, int co
150151
bool wouldBlock = false;
151152
int numBytesRead = 0;
152153

154+
lastException = null;
153155
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
154156
fixed (byte* bufferPtr = buffer) {
155157
numBytesRead = UnityTls.NativeInterface.unitytls_tlsctx_read (m_TlsContext, bufferPtr + offset, count, &errorState);
156158
}
159+
if (lastException != null)
160+
throw lastException;
157161

158162
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
159163
wouldBlock = true;
@@ -168,10 +172,13 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
168172
bool wouldBlock = false;
169173
int numBytesWritten = 0;
170174

175+
lastException = null;
171176
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
172177
fixed (byte* bufferPtr = buffer) {
173178
numBytesWritten = UnityTls.NativeInterface.unitytls_tlsctx_write (m_TlsContext, bufferPtr + offset, count, &errorState);
174179
}
180+
if (lastException != null)
181+
throw lastException;
175182

176183
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
177184
wouldBlock = true;
@@ -233,10 +240,13 @@ public override void StartHandshake ()
233240

234241
public override bool ProcessHandshake ()
235242
{
243+
lastException = null;
236244
var errorState = UnityTls.NativeInterface.unitytls_errorstate_create ();
237245
var result = UnityTls.NativeInterface.unitytls_tlsctx_process_handshake (m_TlsContext, &errorState);
238246
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK)
239247
return false;
248+
if (lastException != null)
249+
throw lastException;
240250

241251
// Not done is not an error if we are server and don't ask for ClientCertificate
242252
if (result == UnityTls.unitytls_x509verify_result.UNITYTLS_X509VERIFY_NOT_DONE && IsServer && !AskForClientCertificate)
@@ -298,8 +308,10 @@ private size_t WriteCallback (byte* data, size_t bufferLen, UnityTls.unitytls_er
298308
}
299309

300310
return bufferLen;
301-
} catch { // handle all exceptions since we don't want to let them go through native code.
311+
} catch (Exception ex) { // handle all exceptions and store them for later since we don't want to let them go through native code.
302312
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
313+
if (lastException == null)
314+
lastException = ex;
303315
return 0;
304316
}
305317
}
@@ -331,8 +343,10 @@ private size_t ReadCallback (byte* buffer, size_t bufferLen, UnityTls.unitytls_e
331343

332344
Marshal.Copy (m_ReadBuffer, 0, (IntPtr)buffer, bufferLen);
333345
return numBytesRead;
334-
} catch { // handle all exceptions since we don't want to let them go through native code.
346+
} catch (Exception ex) { // handle all exceptions and store them for later since we don't want to let them go through native code.
335347
UnityTls.NativeInterface.unitytls_errorstate_raise_error (errorState, UnityTls.unitytls_error_code.UNITYTLS_USER_UNKNOWN_ERROR);
348+
if (lastException == null)
349+
lastException = ex;
336350
return 0;
337351
}
338352
}
@@ -354,7 +368,9 @@ private UnityTls.unitytls_x509verify_result VerifyCallback (UnityTls.unitytls_x5
354368
return UnityTls.unitytls_x509verify_result.UNITYTLS_X509VERIFY_SUCCESS;
355369
else
356370
return UnityTls.unitytls_x509verify_result.UNITYTLS_X509VERIFY_FLAG_NOT_TRUSTED;
357-
} catch { // handle all exceptions since we don't want to let them go through native code.
371+
} catch (Exception ex) { // handle all exceptions and store them for later since we don't want to let them go through native code.
372+
if (lastException == null)
373+
lastException = ex;
358374
return UnityTls.unitytls_x509verify_result.UNITYTLS_X509VERIFY_FATAL_ERROR;
359375
}
360376
}

0 commit comments

Comments
 (0)