summary refs log tree commit diff
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
parentNull tmpBuffers when disposing (diff)
downloadBouncyCastle.NET-ed25519-633b1a5da2e9ad5323397616dbee05b3d7de5739.tar.xz
Refactoring around MemoryStream
-rw-r--r--crypto/src/asn1/Asn1InputStream.cs2
-rw-r--r--crypto/src/crypto/BufferedIesCipher.cs2
-rw-r--r--crypto/src/crypto/digests/NullDigest.cs2
-rw-r--r--crypto/src/crypto/modes/CcmBlockCipher.cs22
-rw-r--r--crypto/src/crypto/modes/GcmSivBlockCipher.cs27
-rw-r--r--crypto/src/crypto/modes/KCcmBlockCipher.cs16
-rw-r--r--crypto/src/crypto/signers/Ed25519Signer.cs29
-rw-r--r--crypto/src/crypto/signers/Ed25519ctxSigner.cs28
-rw-r--r--crypto/src/crypto/signers/Ed448Signer.cs28
-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
-rw-r--r--crypto/src/util/io/Streams.cs21
-rw-r--r--crypto/test/src/tls/test/PipedStream.cs4
20 files changed, 69 insertions, 201 deletions
diff --git a/crypto/src/asn1/Asn1InputStream.cs b/crypto/src/asn1/Asn1InputStream.cs
index da6175fcc..aa91cdf62 100644
--- a/crypto/src/asn1/Asn1InputStream.cs
+++ b/crypto/src/asn1/Asn1InputStream.cs
@@ -28,7 +28,7 @@ namespace Org.BouncyCastle.Asn1
                 return asn1.Limit;
 
             if (input is MemoryStream memory)
-                return (int)(memory.Length - memory.Position);
+                return Convert.ToInt32(memory.Length - memory.Position);
 
             return int.MaxValue;
         }
diff --git a/crypto/src/crypto/BufferedIesCipher.cs b/crypto/src/crypto/BufferedIesCipher.cs
index 293c15c92..b0330e416 100644
--- a/crypto/src/crypto/BufferedIesCipher.cs
+++ b/crypto/src/crypto/BufferedIesCipher.cs
@@ -50,7 +50,7 @@ namespace Org.BouncyCastle.Crypto
 			if (engine == null)
 				throw new InvalidOperationException("cipher not initialised");
 
-			int baseLen = inputLen + (int) buffer.Length;
+			int baseLen = inputLen + Convert.ToInt32(buffer.Length);
 			return forEncryption
 				?	baseLen + 20
 				:	baseLen - 20;
diff --git a/crypto/src/crypto/digests/NullDigest.cs b/crypto/src/crypto/digests/NullDigest.cs
index 76b69afbf..d14dd5c9f 100644
--- a/crypto/src/crypto/digests/NullDigest.cs
+++ b/crypto/src/crypto/digests/NullDigest.cs
@@ -22,7 +22,7 @@ namespace Org.BouncyCastle.Crypto.Digests
 
 		public int GetDigestSize()
 		{
-			return (int)bOut.Length;
+			return Convert.ToInt32(bOut.Length);
 		}
 
 		public void Update(byte b)
diff --git a/crypto/src/crypto/modes/CcmBlockCipher.cs b/crypto/src/crypto/modes/CcmBlockCipher.cs
index 2981fdcb2..abd7dbb8d 100644
--- a/crypto/src/crypto/modes/CcmBlockCipher.cs
+++ b/crypto/src/crypto/modes/CcmBlockCipher.cs
@@ -1,7 +1,6 @@
 using System;
 using System.IO;
 
-using Org.BouncyCastle.Crypto;
 using Org.BouncyCastle.Crypto.Macs;
 using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Utilities;
@@ -146,13 +145,8 @@ namespace Org.BouncyCastle.Crypto.Modes
             byte[]	outBytes,
             int		outOff)
         {
-#if PORTABLE
-            byte[] input = data.ToArray();
-            int inLen = input.Length;
-#else
             byte[] input = data.GetBuffer();
-            int inLen = (int)data.Position;
-#endif
+            int inLen = Convert.ToInt32(data.Length);
 
             int len = ProcessPacket(input, 0, inLen, outBytes, outOff);
 
@@ -188,7 +182,7 @@ namespace Org.BouncyCastle.Crypto.Modes
         public virtual int GetOutputSize(
             int len)
         {
-            int totalData = (int)data.Length + len;
+            int totalData = Convert.ToInt32(data.Length) + len;
 
             if (forEncryption)
             {
@@ -403,15 +397,10 @@ namespace Org.BouncyCastle.Crypto.Modes
                 {
                     cMac.BlockUpdate(initialAssociatedText, 0, initialAssociatedText.Length);
                 }
-                if (associatedText.Position > 0)
+                if (associatedText.Length > 0)
                 {
-#if PORTABLE
-                    byte[] input = associatedText.ToArray();
-                    int len = input.Length;
-#else
                     byte[] input = associatedText.GetBuffer();
-                    int len = (int)associatedText.Position;
-#endif
+                    int len = Convert.ToInt32(associatedText.Length);
 
                     cMac.BlockUpdate(input, 0, len);
                 }
@@ -444,7 +433,8 @@ namespace Org.BouncyCastle.Crypto.Modes
 
         private int GetAssociatedTextLength()
         {
-            return (int)associatedText.Length + ((initialAssociatedText == null) ? 0 : initialAssociatedText.Length);
+            return Convert.ToInt32(associatedText.Length) +
+                (initialAssociatedText == null ? 0 : initialAssociatedText.Length);
         }
 
         private bool HasAssociatedText()
diff --git a/crypto/src/crypto/modes/GcmSivBlockCipher.cs b/crypto/src/crypto/modes/GcmSivBlockCipher.cs
index a6c729eae..eb070bf0c 100644
--- a/crypto/src/crypto/modes/GcmSivBlockCipher.cs
+++ b/crypto/src/crypto/modes/GcmSivBlockCipher.cs
@@ -367,7 +367,7 @@ namespace Org.BouncyCastle.Crypto.Modes
                 int myDataLen = BUFLEN + encryptPlain(myTag, pOutput, pOffset);
 
                 /* Add the tag to the output */
-                Array.Copy(myTag, 0, pOutput, pOffset + (int)thePlain.Length, BUFLEN);
+                Array.Copy(myTag, 0, pOutput, pOffset + Convert.ToInt32(thePlain.Length), BUFLEN);
 
                 /* Reset the streams */
                 ResetStreams();
@@ -403,9 +403,9 @@ namespace Org.BouncyCastle.Crypto.Modes
         {
             if (forEncryption)
             {
-                return (int)(pLen + thePlain.Length + BUFLEN);
+                return pLen + Convert.ToInt32(thePlain.Length) + BUFLEN;
             }
-            int myCurr = (int)(pLen + theEncData.Length);
+            int myCurr = pLen + Convert.ToInt32(theEncData.Length);
             return myCurr > BUFLEN ? myCurr - BUFLEN : 0;
         }
 
@@ -422,8 +422,9 @@ namespace Org.BouncyCastle.Crypto.Modes
             /* Clear the plainText buffer */
             if (thePlain != null)
             {
-                thePlain.Position = 0L;
-                Streams.WriteZeroes(thePlain, thePlain.Capacity);
+                int count = Convert.ToInt32(thePlain.Length);
+                Array.Clear(thePlain.GetBuffer(), 0, count);
+                thePlain.SetLength(0);
             }
 
             /* Reset hashers */
@@ -485,14 +486,8 @@ namespace Org.BouncyCastle.Crypto.Modes
         */
         private int encryptPlain(byte[] pCounter, byte[] pTarget, int pOffset)
         {
-            /* Access buffer and length */
-#if PORTABLE
-            byte[] thePlainBuf = thePlain.ToArray();
-            int thePlainLen = thePlainBuf.Length;
-#else
             byte[] thePlainBuf = thePlain.GetBuffer();
-            int thePlainLen = (int)thePlain.Length;
-#endif
+            int thePlainLen = Convert.ToInt32(thePlain.Length);
 
             byte[] mySrc = thePlainBuf;
             byte[] myCounter = Arrays.Clone(pCounter);
@@ -530,14 +525,8 @@ namespace Org.BouncyCastle.Crypto.Modes
         */
         private void decryptPlain()
         {
-            /* Access buffer and length */
-#if PORTABLE
-            byte[] theEncDataBuf = theEncData.ToArray();
-            int theEncDataLen = theEncDataBuf.Length;
-#else
             byte[] theEncDataBuf = theEncData.GetBuffer();
-            int theEncDataLen = (int)theEncData.Length;
-#endif
+            int theEncDataLen = Convert.ToInt32(theEncData.Length);
 
             byte[] mySrc = theEncDataBuf;
             int myRemaining = theEncDataLen - BUFLEN;
diff --git a/crypto/src/crypto/modes/KCcmBlockCipher.cs b/crypto/src/crypto/modes/KCcmBlockCipher.cs
index e56b8897d..afa7063a3 100644
--- a/crypto/src/crypto/modes/KCcmBlockCipher.cs
+++ b/crypto/src/crypto/modes/KCcmBlockCipher.cs
@@ -243,15 +243,10 @@ namespace Org.BouncyCastle.Crypto.Modes
 
             if (associatedText.Length > 0)
             {
-#if PORTABLE
-                byte[] aad = associatedText.ToArray();
-                int aadLen = aad.Length;
-#else
                 byte[] aad = associatedText.GetBuffer();
-                int aadLen = (int)associatedText.Length;
-#endif
+                int aadLen = Convert.ToInt32(associatedText.Length);
 
-                int dataLen = forEncryption ? (int)data.Length : ((int)data.Length - macSize);
+                int dataLen = Convert.ToInt32(data.Length) - (forEncryption ? 0 : macSize);
 
                 ProcessAAD(aad, 0, aadLen, dataLen);
             }
@@ -385,13 +380,8 @@ namespace Org.BouncyCastle.Crypto.Modes
 
         public virtual int DoFinal(byte[] output, int outOff)
         {
-#if PORTABLE
-            byte[] buf = data.ToArray();
-            int bufLen = buf.Length;
-#else
             byte[] buf = data.GetBuffer();
-            int bufLen = (int)data.Length;
-#endif
+            int bufLen = Convert.ToInt32(data.Length);
 
             int len = ProcessPacket(buf, 0, bufLen, output, outOff);
 
diff --git a/crypto/src/crypto/signers/Ed25519Signer.cs b/crypto/src/crypto/signers/Ed25519Signer.cs
index eb3d25398..4646ce1a5 100644
--- a/crypto/src/crypto/signers/Ed25519Signer.cs
+++ b/crypto/src/crypto/signers/Ed25519Signer.cs
@@ -3,8 +3,6 @@ using System.IO;
 
 using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Math.EC.Rfc8032;
-using Org.BouncyCastle.Utilities;
-using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Crypto.Signers
 {
@@ -81,13 +79,9 @@ namespace Org.BouncyCastle.Crypto.Signers
             {
                 lock (this)
                 {
-#if PORTABLE
-                    byte[] buf = ToArray();
-                    int count = buf.Length;
-#else
                     byte[] buf = GetBuffer();
-                    int count = (int)Position;
-#endif
+                    int count = Convert.ToInt32(Length);
+
                     byte[] signature = new byte[Ed25519PrivateKeyParameters.SignatureSize];
                     privateKey.Sign(Ed25519.Algorithm.Ed25519, null, buf, 0, count, signature, 0);
                     Reset();
@@ -105,13 +99,9 @@ namespace Org.BouncyCastle.Crypto.Signers
 
                 lock (this)
                 {
-#if PORTABLE
-                    byte[] buf = ToArray();
-                    int count = buf.Length;
-#else
                     byte[] buf = GetBuffer();
-                    int count = (int)Position;
-#endif
+                    int count = Convert.ToInt32(Length);
+
                     byte[] pk = publicKey.GetEncoded();
                     bool result = Ed25519.Verify(signature, 0, pk, 0, buf, 0, count);
                     Reset();
@@ -123,14 +113,9 @@ namespace Org.BouncyCastle.Crypto.Signers
             {
                 lock (this)
                 {
-                    long count = Position;
-#if PORTABLE
-                    this.Position = 0L;
-                    Streams.WriteZeroes(this, count);
-#else
-                    Array.Clear(GetBuffer(), 0, (int)count);
-#endif
-                    this.Position = 0L;
+                    int count = Convert.ToInt32(Length);
+                    Array.Clear(GetBuffer(), 0, count);
+                    SetLength(0);
                 }
             }
         }
diff --git a/crypto/src/crypto/signers/Ed25519ctxSigner.cs b/crypto/src/crypto/signers/Ed25519ctxSigner.cs
index 3610e25de..293afedf5 100644
--- a/crypto/src/crypto/signers/Ed25519ctxSigner.cs
+++ b/crypto/src/crypto/signers/Ed25519ctxSigner.cs
@@ -4,7 +4,6 @@ using System.IO;
 using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Math.EC.Rfc8032;
 using Org.BouncyCastle.Utilities;
-using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Crypto.Signers
 {
@@ -83,13 +82,9 @@ namespace Org.BouncyCastle.Crypto.Signers
             {
                 lock (this)
                 {
-#if PORTABLE
-                    byte[] buf = ToArray();
-                    int count = buf.Length;
-#else
                     byte[] buf = GetBuffer();
-                    int count = (int)Position;
-#endif
+                    int count = Convert.ToInt32(Length);
+
                     byte[] signature = new byte[Ed25519PrivateKeyParameters.SignatureSize];
                     privateKey.Sign(Ed25519.Algorithm.Ed25519ctx, ctx, buf, 0, count, signature, 0);
                     Reset();
@@ -107,13 +102,9 @@ namespace Org.BouncyCastle.Crypto.Signers
 
                 lock (this)
                 {
-#if PORTABLE
-                    byte[] buf = ToArray();
-                    int count = buf.Length;
-#else
                     byte[] buf = GetBuffer();
-                    int count = (int)Position;
-#endif
+                    int count = Convert.ToInt32(Length);
+
                     byte[] pk = publicKey.GetEncoded();
                     bool result = Ed25519.Verify(signature, 0, pk, 0, ctx, buf, 0, count);
                     Reset();
@@ -125,14 +116,9 @@ namespace Org.BouncyCastle.Crypto.Signers
             {
                 lock (this)
                 {
-                    long count = Position;
-#if PORTABLE
-                    this.Position = 0L;
-                    Streams.WriteZeroes(this, count);
-#else
-                    Array.Clear(GetBuffer(), 0, (int)count);
-#endif
-                    this.Position = 0L;
+                    int count = Convert.ToInt32(Length);
+                    Array.Clear(GetBuffer(), 0, count);
+                    SetLength(0);
                 }
             }
         }
diff --git a/crypto/src/crypto/signers/Ed448Signer.cs b/crypto/src/crypto/signers/Ed448Signer.cs
index 746029834..723ee7e33 100644
--- a/crypto/src/crypto/signers/Ed448Signer.cs
+++ b/crypto/src/crypto/signers/Ed448Signer.cs
@@ -4,7 +4,6 @@ using System.IO;
 using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Math.EC.Rfc8032;
 using Org.BouncyCastle.Utilities;
-using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Crypto.Signers
 {
@@ -83,13 +82,9 @@ namespace Org.BouncyCastle.Crypto.Signers
             {
                 lock (this)
                 {
-#if PORTABLE
-                byte[] buf = ToArray();
-                int count = buf.Length;
-#else
                     byte[] buf = GetBuffer();
-                    int count = (int)Position;
-#endif
+                    int count = Convert.ToInt32(Length);
+
                     byte[] signature = new byte[Ed448PrivateKeyParameters.SignatureSize];
                     privateKey.Sign(Ed448.Algorithm.Ed448, ctx, buf, 0, count, signature, 0);
                     Reset();
@@ -107,13 +102,9 @@ namespace Org.BouncyCastle.Crypto.Signers
 
                 lock (this)
                 {
-#if PORTABLE
-                    byte[] buf = ToArray();
-                    int count = buf.Length;
-#else
                     byte[] buf = GetBuffer();
-                    int count = (int)Position;
-#endif
+                    int count = Convert.ToInt32(Length);
+
                     byte[] pk = publicKey.GetEncoded();
                     bool result = Ed448.Verify(signature, 0, pk, 0, ctx, buf, 0, count);
                     Reset();
@@ -125,14 +116,9 @@ namespace Org.BouncyCastle.Crypto.Signers
             {
                 lock (this)
                 {
-                    long count = Position;
-#if PORTABLE
-                    this.Position = 0L;
-                    Streams.WriteZeroes(this, count);
-#else
-                    Array.Clear(GetBuffer(), 0, (int)count);
-#endif
-                    this.Position = 0L;
+                    int count = Convert.ToInt32(Length);
+                    Array.Clear(GetBuffer(), 0, count);
+                    SetLength(0);
                 }
             }
         }
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);
diff --git a/crypto/src/util/io/Streams.cs b/crypto/src/util/io/Streams.cs
index 3623dfe39..e1da47fcd 100644
--- a/crypto/src/util/io/Streams.cs
+++ b/crypto/src/util/io/Streams.cs
@@ -117,29 +117,12 @@ namespace Org.BouncyCastle.Utilities.IO
 				throw new ArgumentOutOfRangeException("count");
 		}
 
-		/// <exception cref="IOException"></exception>
-		public static void WriteBufTo(MemoryStream buf, Stream output)
-        {
-            buf.WriteTo(output);
-        }
-
         /// <exception cref="IOException"></exception>
         public static int WriteBufTo(MemoryStream buf, byte[] output, int offset)
         {
-            int size = (int)buf.Length;
-            WriteBufTo(buf, new MemoryStream(output, offset, size));
+			int size = Convert.ToInt32(buf.Length);
+            buf.WriteTo(new MemoryStream(output, offset, size));
             return size;
         }
-
-        public static void WriteZeroes(Stream outStr, long count)
-        {
-            byte[] zeroes = new byte[BufferSize];
-            while (count > BufferSize)
-            {
-                outStr.Write(zeroes, 0, BufferSize);
-                count -= BufferSize;
-            }
-            outStr.Write(zeroes, 0, (int)count);
-        }
     }
 }
diff --git a/crypto/test/src/tls/test/PipedStream.cs b/crypto/test/src/tls/test/PipedStream.cs
index 6de5703e1..dfca35b81 100644
--- a/crypto/test/src/tls/test/PipedStream.cs
+++ b/crypto/test/src/tls/test/PipedStream.cs
@@ -80,7 +80,7 @@ namespace Org.BouncyCastle.Tls.Tests
             lock (m_other)
             {
                 WaitForData();
-                int len = (int)System.Math.Min(count, m_other.m_buf.Position - m_readPos);
+                int len = System.Math.Min(count, Convert.ToInt32(m_other.m_buf.Position - m_readPos));
                 Array.Copy(m_other.m_buf.GetBuffer(), m_readPos, buffer, offset, len);
                 m_readPos += len;
                 return len;
@@ -92,7 +92,7 @@ namespace Org.BouncyCastle.Tls.Tests
             lock (m_other)
             {
                 WaitForData();
-                bool eof = (m_readPos >= m_other.m_buf.Position);
+                bool eof = m_readPos >= m_other.m_buf.Position;
                 return eof ? -1 : m_other.m_buf.GetBuffer()[m_readPos++];
             }
         }