From 2a0ba21824861f76f35077088f7cea350023f090 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 13 May 2022 18:59:03 +0700 Subject: Blocking zero-byte Read for TlsStream --- crypto/src/tls/TlsProtocol.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/crypto/src/tls/TlsProtocol.cs b/crypto/src/tls/TlsProtocol.cs index 8fe6dc225..57b452f60 100644 --- a/crypto/src/tls/TlsProtocol.cs +++ b/crypto/src/tls/TlsProtocol.cs @@ -707,10 +707,17 @@ namespace Org.BouncyCastle.Tls /// If something goes wrong during reading data. public virtual int ReadApplicationData(byte[] buf, int off, int len) { - if (len < 1) - return 0; + if (buf == null) + throw new ArgumentNullException("buf"); + if (off < 0) + throw new ArgumentOutOfRangeException("off"); + if (len < 0 || len > buf.Length - off) + throw new ArgumentOutOfRangeException("len"); + + if (!m_appDataReady) + throw new InvalidOperationException("Cannot read application data until initial handshake completed."); - while (m_applicationDataQueue.Available == 0) + while (m_applicationDataQueue.Available < 1) { if (this.m_closed) { @@ -719,8 +726,6 @@ namespace Org.BouncyCastle.Tls return -1; } - if (!m_appDataReady) - throw new InvalidOperationException("Cannot read application data until initial handshake completed."); /* * NOTE: Only called more than once when empty records are received, so no special @@ -729,8 +734,11 @@ namespace Org.BouncyCastle.Tls SafeReadRecord(); } - len = System.Math.Min(len, m_applicationDataQueue.Available); - m_applicationDataQueue.RemoveData(buf, off, len, 0); + if (len > 0) + { + len = System.Math.Min(len, m_applicationDataQueue.Available); + m_applicationDataQueue.RemoveData(buf, off, len, 0); + } return len; } -- cgit 1.4.1