summary refs log tree commit diff
path: root/crypto/src/tls/TlsProtocol.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-05-13 18:59:03 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-05-13 18:59:03 +0700
commit2a0ba21824861f76f35077088f7cea350023f090 (patch)
tree2e06166d013be230a1de1a2b62c3dcc3736aba00 /crypto/src/tls/TlsProtocol.cs
parentMerge branch 'alpn-id' (diff)
downloadBouncyCastle.NET-ed25519-2a0ba21824861f76f35077088f7cea350023f090.tar.xz
Blocking zero-byte Read for TlsStream
Diffstat (limited to 'crypto/src/tls/TlsProtocol.cs')
-rw-r--r--crypto/src/tls/TlsProtocol.cs22
1 files 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
         /// <exception cref="IOException">If something goes wrong during reading data.</exception>
         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;
         }