summary refs log tree commit diff
path: root/crypto/src/tls/TlsProtocol.cs
diff options
context:
space:
mode:
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;
         }