summary refs log tree commit diff
path: root/crypto/src/tls
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-30 11:15:39 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-30 11:15:39 +0700
commit633b1a5da2e9ad5323397616dbee05b3d7de5739 (patch)
tree5048f252d70c5fceea0ffbde04afbd3ad9ec7539 /crypto/src/tls
parentNull tmpBuffers when disposing (diff)
downloadBouncyCastle.NET-ed25519-633b1a5da2e9ad5323397616dbee05b3d7de5739.tar.xz
Refactoring around MemoryStream
Diffstat (limited to 'crypto/src/tls')
-rw-r--r--crypto/src/tls/CertificateUrl.cs4
-rw-r--r--crypto/src/tls/ClientHello.cs2
-rw-r--r--crypto/src/tls/DigestInputBuffer.cs9
-rw-r--r--crypto/src/tls/DtlsReliableHandshake.cs9
-rw-r--r--crypto/src/tls/HandshakeMessageInput.cs27
-rw-r--r--crypto/src/tls/HandshakeMessageOutput.cs28
-rw-r--r--crypto/src/tls/OcspStatusRequest.cs4
-rw-r--r--crypto/src/tls/ServerNameList.cs4
-rw-r--r--crypto/src/tls/TlsExtensionsUtilities.cs2
9 files changed, 24 insertions, 65 deletions
diff --git a/crypto/src/tls/CertificateUrl.cs b/crypto/src/tls/CertificateUrl.cs
index 6629e67f9..e14446d6f 100644
--- a/crypto/src/tls/CertificateUrl.cs
+++ b/crypto/src/tls/CertificateUrl.cs
@@ -101,13 +101,13 @@ namespace Org.BouncyCastle.Tls
             internal void EncodeTo(Stream output)
             {
                 // Patch actual length back in
-                int length = (int)Length - 2;
+                int length = Convert.ToInt32(Length) - 2;
                 TlsUtilities.CheckUint16(length);
 
                 Seek(0L, SeekOrigin.Begin);
                 TlsUtilities.WriteUint16(length, this);
 
-                Streams.WriteBufTo(this, output);
+                WriteTo(output);
 
                 Platform.Dispose(this);
             }
diff --git a/crypto/src/tls/ClientHello.cs b/crypto/src/tls/ClientHello.cs
index 14e8b4cde..574264491 100644
--- a/crypto/src/tls/ClientHello.cs
+++ b/crypto/src/tls/ClientHello.cs
@@ -142,7 +142,7 @@ namespace Org.BouncyCastle.Tls
 
             int cipher_suites_length = TlsUtilities.ReadUint16(input);
             if (cipher_suites_length < 2 || (cipher_suites_length & 1) != 0
-                || (int)(messageInput.Length - messageInput.Position) < cipher_suites_length)
+                || Convert.ToInt32(messageInput.Length - messageInput.Position) < cipher_suites_length)
             {
                 throw new TlsFatalAlert(AlertDescription.decode_error);
             }
diff --git a/crypto/src/tls/DigestInputBuffer.cs b/crypto/src/tls/DigestInputBuffer.cs
index 9b4ea4b06..a5d53a02f 100644
--- a/crypto/src/tls/DigestInputBuffer.cs
+++ b/crypto/src/tls/DigestInputBuffer.cs
@@ -2,7 +2,6 @@
 using System.IO;
 
 using Org.BouncyCastle.Tls.Crypto;
-using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Tls
 {
@@ -11,16 +10,14 @@ namespace Org.BouncyCastle.Tls
     {
         internal void UpdateDigest(TlsHash hash)
         {
-            Streams.WriteBufTo(this, new TlsHashSink(hash));
+            WriteTo(new TlsHashSink(hash));
         }
 
         /// <exception cref="IOException"/>
         internal void CopyInputTo(Stream output)
         {
-            // TODO[tls-port]
-            // NOTE: Copy data since the output here may be under control of external code.
-            //Streams.PipeAll(new MemoryStream(buf, 0, count), output);
-            Streams.WriteBufTo(this, output);
+            // TODO[tls] Consider defensive copy if 'output' might be external code
+            WriteTo(output);
         }
     }
 }
diff --git a/crypto/src/tls/DtlsReliableHandshake.cs b/crypto/src/tls/DtlsReliableHandshake.cs
index 1d35cf2ce..32c5c7851 100644
--- a/crypto/src/tls/DtlsReliableHandshake.cs
+++ b/crypto/src/tls/DtlsReliableHandshake.cs
@@ -539,16 +539,11 @@ namespace Org.BouncyCastle.Tls
 
             internal void SendToRecordLayer(DtlsRecordLayer recordLayer)
             {
-#if PORTABLE
-                byte[] buf = ToArray();
-                int bufLen = buf.Length;
-#else
                 byte[] buf = GetBuffer();
-                int bufLen = (int)Length;
-#endif
+                int bufLen = Convert.ToInt32(Length);
 
                 recordLayer.Send(buf, 0, bufLen);
-                Platform.Dispose(this);
+                Dispose();
             }
         }
 
diff --git a/crypto/src/tls/HandshakeMessageInput.cs b/crypto/src/tls/HandshakeMessageInput.cs
index 8d9a291d7..9224e0db7 100644
--- a/crypto/src/tls/HandshakeMessageInput.cs
+++ b/crypto/src/tls/HandshakeMessageInput.cs
@@ -2,7 +2,6 @@
 using System.IO;
 
 using Org.BouncyCastle.Tls.Crypto;
-using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Tls
 {
@@ -13,46 +12,28 @@ namespace Org.BouncyCastle.Tls
         private readonly int m_offset;
 
         internal HandshakeMessageInput(byte[] buf, int offset, int length)
-#if PORTABLE
-            : base(buf, offset, length, false)
-#else
             : base(buf, offset, length, false, true)
-#endif
         {
-#if PORTABLE
-            this.m_offset = 0;
-#else
-            this.m_offset = offset;
-#endif
+            m_offset = offset;
         }
 
         public void UpdateHash(TlsHash hash)
         {
-            Streams.WriteBufTo(this, new TlsHashSink(hash));
+            WriteTo(new TlsHashSink(hash));
         }
 
         internal void UpdateHashPrefix(TlsHash hash, int bindersSize)
         {
-#if PORTABLE
-            byte[] buf = ToArray();
-            int count = buf.Length;
-#else
             byte[] buf = GetBuffer();
-            int count = (int)Length;
-#endif
+            int count = Convert.ToInt32(Length);
 
             hash.Update(buf, m_offset, count - bindersSize);
         }
 
         internal void UpdateHashSuffix(TlsHash hash, int bindersSize)
         {
-#if PORTABLE
-            byte[] buf = ToArray();
-            int count = buf.Length;
-#else
             byte[] buf = GetBuffer();
-            int count = (int)Length;
-#endif
+            int count = Convert.ToInt32(Length);
 
             hash.Update(buf, m_offset + count - bindersSize, bindersSize);
         }
diff --git a/crypto/src/tls/HandshakeMessageOutput.cs b/crypto/src/tls/HandshakeMessageOutput.cs
index ff45ce6f3..0d8f15018 100644
--- a/crypto/src/tls/HandshakeMessageOutput.cs
+++ b/crypto/src/tls/HandshakeMessageOutput.cs
@@ -41,40 +41,31 @@ namespace Org.BouncyCastle.Tls
         internal void Send(TlsProtocol protocol)
         {
             // Patch actual length back in
-            int bodyLength = (int)Length - 4;
+            int bodyLength = Convert.ToInt32(Length) - 4;
             TlsUtilities.CheckUint24(bodyLength);
 
             Seek(1L, SeekOrigin.Begin);
             TlsUtilities.WriteUint24(bodyLength, this);
 
-#if PORTABLE
-            byte[] buf = ToArray();
-            int count = buf.Length;
-#else
             byte[] buf = GetBuffer();
-            int count = (int)Length;
-#endif
+            int count = Convert.ToInt32(Length);
+
             protocol.WriteHandshakeMessage(buf, 0, count);
 
-            Platform.Dispose(this);
+            Dispose();
         }
 
         internal void PrepareClientHello(TlsHandshakeHash handshakeHash, int bindersSize)
         {
             // Patch actual length back in
-            int bodyLength = (int)Length - 4 + bindersSize;
+            int bodyLength = Convert.ToInt32(Length) - 4 + bindersSize;
             TlsUtilities.CheckUint24(bodyLength);
 
             Seek(1L, SeekOrigin.Begin);
             TlsUtilities.WriteUint24(bodyLength, this);
 
-#if PORTABLE
-            byte[] buf = ToArray();
-            int count = buf.Length;
-#else
             byte[] buf = GetBuffer();
-            int count = (int)Length;
-#endif
+            int count = Convert.ToInt32(Length);
 
             handshakeHash.Update(buf, 0, count);
 
@@ -83,13 +74,8 @@ namespace Org.BouncyCastle.Tls
 
         internal void SendClientHello(TlsClientProtocol clientProtocol, TlsHandshakeHash handshakeHash, int bindersSize)
         {
-#if PORTABLE
-            byte[] buf = ToArray();
-            int count = buf.Length;
-#else
             byte[] buf = GetBuffer();
-            int count = (int)Length;
-#endif
+            int count = Convert.ToInt32(Length);
 
             if (bindersSize > 0)
             {
diff --git a/crypto/src/tls/OcspStatusRequest.cs b/crypto/src/tls/OcspStatusRequest.cs
index 00728f64e..a1688efcb 100644
--- a/crypto/src/tls/OcspStatusRequest.cs
+++ b/crypto/src/tls/OcspStatusRequest.cs
@@ -56,8 +56,8 @@ namespace Org.BouncyCastle.Tls
                     TlsUtilities.WriteOpaque16(derEncoding, buf);
                 }
                 TlsUtilities.CheckUint16(buf.Length);
-                TlsUtilities.WriteUint16((int)buf.Length, output);
-                Streams.WriteBufTo(buf, output);
+                TlsUtilities.WriteUint16(Convert.ToInt32(buf.Length), output);
+                buf.WriteTo(output);
             }
 
             if (m_requestExtensions == null)
diff --git a/crypto/src/tls/ServerNameList.cs b/crypto/src/tls/ServerNameList.cs
index 358e82a67..248cf2f7f 100644
--- a/crypto/src/tls/ServerNameList.cs
+++ b/crypto/src/tls/ServerNameList.cs
@@ -43,10 +43,10 @@ namespace Org.BouncyCastle.Tls
                 entry.Encode(buf);
             }
 
-            int length = (int)buf.Length;
+            int length = Convert.ToInt32(buf.Length);
             TlsUtilities.CheckUint16(length);
             TlsUtilities.WriteUint16(length, output);
-            Streams.WriteBufTo(buf, output);
+            buf.WriteTo(output);
         }
 
         /// <summary>Parse a <see cref="ServerNameList"/> from a <see cref="Stream"/>.</summary>
diff --git a/crypto/src/tls/TlsExtensionsUtilities.cs b/crypto/src/tls/TlsExtensionsUtilities.cs
index 9ffdcfe40..6ad22f1e4 100644
--- a/crypto/src/tls/TlsExtensionsUtilities.cs
+++ b/crypto/src/tls/TlsExtensionsUtilities.cs
@@ -1433,7 +1433,7 @@ namespace Org.BouncyCastle.Tls
         /// <exception cref="IOException"/>
         private static byte[] PatchOpaque16(MemoryStream buf)
         {
-            int length = (int)buf.Length - 2;
+            int length = Convert.ToInt32(buf.Length) - 2;
             TlsUtilities.CheckUint16(length);
             byte[] extensionData = buf.ToArray();
             TlsUtilities.WriteUint16(length, extensionData, 0);