summary refs log tree commit diff
path: root/crypto/src/tls
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-05-23 22:44:57 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-05-23 22:44:57 +0700
commit98b3ce6512812cd1d91c60ca0e69d69c32838d8e (patch)
tree8af69c097b389fe494e97401f7808590314a5b6b /crypto/src/tls
parentAdd ValidateBufferArguments (diff)
downloadBouncyCastle.NET-ed25519-98b3ce6512812cd1d91c60ca0e69d69c32838d8e.tar.xz
Work on Stream classes
- consistent naming, ordering
- CanRead/Seek/Write fixes
- argument validation
- some cases of blocking zero-byte Read calls
Diffstat (limited to 'crypto/src/tls')
-rw-r--r--crypto/src/tls/ByteQueueInputStream.cs16
-rw-r--r--crypto/src/tls/ByteQueueOutputStream.cs10
-rw-r--r--crypto/src/tls/TlsStream.cs10
-rw-r--r--crypto/src/tls/crypto/TlsHashSink.cs16
-rw-r--r--crypto/src/tls/crypto/TlsMacSink.cs16
5 files changed, 38 insertions, 30 deletions
diff --git a/crypto/src/tls/ByteQueueInputStream.cs b/crypto/src/tls/ByteQueueInputStream.cs
index b59b5d1e7..0b15071ad 100644
--- a/crypto/src/tls/ByteQueueInputStream.cs
+++ b/crypto/src/tls/ByteQueueInputStream.cs
@@ -31,6 +31,15 @@ namespace Org.BouncyCastle.Tls
             return bytesToRead;
         }
 
+        public override int Read(byte[] buffer, int offset, int count)
+        {
+            Streams.ValidateBufferArguments(buffer, offset, count);
+
+            int bytesToRead = System.Math.Min(m_buffer.Available, count);
+            m_buffer.RemoveData(buffer, offset, bytesToRead, 0);
+            return bytesToRead;
+        }
+
         public override int ReadByte()
         {
             if (m_buffer.Available == 0)
@@ -39,13 +48,6 @@ namespace Org.BouncyCastle.Tls
             return m_buffer.RemoveData(1, 0)[0];
         }
 
-        public override int Read(byte[] buf, int off, int len)
-        {
-            int bytesToRead = System.Math.Min(m_buffer.Available, len);
-            m_buffer.RemoveData(buf, off, bytesToRead, 0);
-            return bytesToRead;
-        }
-
         public long Skip(long n)
         {
             int bytesToRemove = System.Math.Min((int)n, m_buffer.Available);
diff --git a/crypto/src/tls/ByteQueueOutputStream.cs b/crypto/src/tls/ByteQueueOutputStream.cs
index 76f04916f..441a3773a 100644
--- a/crypto/src/tls/ByteQueueOutputStream.cs
+++ b/crypto/src/tls/ByteQueueOutputStream.cs
@@ -20,14 +20,16 @@ namespace Org.BouncyCastle.Tls
             get { return m_buffer; }
         }
 
-        public override void WriteByte(byte b)
+        public override void Write(byte[] buffer, int offset, int count)
         {
-            m_buffer.AddData(new byte[]{ b }, 0, 1);
+            Streams.ValidateBufferArguments(buffer, offset, count);
+
+            m_buffer.AddData(buffer, offset, count);
         }
 
-        public override void Write(byte[] buf, int off, int len)
+        public override void WriteByte(byte value)
         {
-            m_buffer.AddData(buf, off, len);
+            m_buffer.AddData(new byte[]{ value }, 0, 1);
         }
     }
 }
diff --git a/crypto/src/tls/TlsStream.cs b/crypto/src/tls/TlsStream.cs
index 02f8b733e..f3dea1574 100644
--- a/crypto/src/tls/TlsStream.cs
+++ b/crypto/src/tls/TlsStream.cs
@@ -10,12 +10,12 @@ namespace Org.BouncyCastle.Tls
 
         internal TlsStream(TlsProtocol handler)
         {
-            this.m_handler = handler;
+            m_handler = handler;
         }
 
         public override bool CanRead
         {
-            get { return !m_handler.IsClosed; }
+            get { return true; }
         }
 
         public override bool CanSeek
@@ -25,7 +25,7 @@ namespace Org.BouncyCastle.Tls
 
         public override bool CanWrite
         {
-            get { return !m_handler.IsClosed; }
+            get { return true; }
         }
 
 #if PORTABLE
@@ -88,9 +88,9 @@ namespace Org.BouncyCastle.Tls
             m_handler.WriteApplicationData(buffer, offset, count);
         }
 
-        public override void WriteByte(byte b)
+        public override void WriteByte(byte value)
         {
-            Write(new byte[]{ b }, 0, 1);
+            Write(new byte[]{ value }, 0, 1);
         }
     }
 }
diff --git a/crypto/src/tls/crypto/TlsHashSink.cs b/crypto/src/tls/crypto/TlsHashSink.cs
index 64496744d..a1681b0c8 100644
--- a/crypto/src/tls/crypto/TlsHashSink.cs
+++ b/crypto/src/tls/crypto/TlsHashSink.cs
@@ -19,17 +19,19 @@ namespace Org.BouncyCastle.Tls.Crypto
             get { return m_hash; }
         }
 
-        public override void WriteByte(byte b)
+        public override void Write(byte[] buffer, int offset, int count)
         {
-            m_hash.Update(new byte[] { b }, 0, 1);
-        }
+            Streams.ValidateBufferArguments(buffer, offset, count);
 
-        public override void Write(byte[] buf, int off, int len)
-        {
-            if (len > 0)
+            if (count > 0)
             {
-                m_hash.Update(buf, off, len);
+                m_hash.Update(buffer, offset, count);
             }
         }
+
+        public override void WriteByte(byte value)
+        {
+            m_hash.Update(new byte[]{ value }, 0, 1);
+        }
     }
 }
diff --git a/crypto/src/tls/crypto/TlsMacSink.cs b/crypto/src/tls/crypto/TlsMacSink.cs
index 58e65c731..e7d5c70d7 100644
--- a/crypto/src/tls/crypto/TlsMacSink.cs
+++ b/crypto/src/tls/crypto/TlsMacSink.cs
@@ -19,17 +19,19 @@ namespace Org.BouncyCastle.Tls.Crypto
             get { return m_mac; }
         }
 
-        public override void WriteByte(byte b)
+        public override void Write(byte[] buffer, int offset, int count)
         {
-            m_mac.Update(new byte[]{ b }, 0, 1);
-        }
+            Streams.ValidateBufferArguments(buffer, offset, count);
 
-        public override void Write(byte[] buf, int off, int len)
-        {
-            if (len > 0)
+            if (count > 0)
             {
-                m_mac.Update(buf, off, len);
+                m_mac.Update(buffer, offset, count);
             }
         }
+
+        public override void WriteByte(byte value)
+        {
+            m_mac.Update(new byte[]{ value }, 0, 1);
+        }
     }
 }