summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2017-03-23 22:44:22 +1030
committerPeter Dettman <peter.dettman@bouncycastle.org>2017-03-23 22:44:22 +1030
commit549033c625177468f1fe876a7d1deed27df3d27b (patch)
treef21ea5ff52b9a8673a71207eaa4b8ea6d5fb038d
parentAvoid extra copy when processing TLS handshake messages (diff)
downloadBouncyCastle.NET-ed25519-549033c625177468f1fe876a7d1deed27df3d27b.tar.xz
Improvements to WriteHandshakeMessage method
-rw-r--r--crypto/src/crypto/tls/TlsProtocol.cs20
1 files changed, 14 insertions, 6 deletions
diff --git a/crypto/src/crypto/tls/TlsProtocol.cs b/crypto/src/crypto/tls/TlsProtocol.cs
index eb0ad1101..667b3eb33 100644
--- a/crypto/src/crypto/tls/TlsProtocol.cs
+++ b/crypto/src/crypto/tls/TlsProtocol.cs
@@ -628,16 +628,24 @@ namespace Org.BouncyCastle.Crypto.Tls
 
         protected virtual void WriteHandshakeMessage(byte[] buf, int off, int len)
         {
-            mRecordStream.HandshakeHashUpdater.Write(buf, off, len);
+            if (len < 4)
+                throw new TlsFatalAlert(AlertDescription.internal_error);
 
-            while (len > 0)
+            byte type = TlsUtilities.ReadUint8(buf, off);
+            if (type != HandshakeType.hello_request)
+            {
+                mRecordStream.HandshakeHashUpdater.Write(buf, off, len);
+            }
+
+            int total = 0;
+            do
             {
                 // Fragment data according to the current fragment limit.
-                int toWrite = System.Math.Min(len, mRecordStream.GetPlaintextLimit());
-                SafeWriteRecord(ContentType.handshake, buf, off, toWrite);
-                off += toWrite;
-                len -= toWrite;
+                int toWrite = System.Math.Min(len - total, mRecordStream.GetPlaintextLimit());
+                SafeWriteRecord(ContentType.handshake, buf, off + total, toWrite);
+                total += toWrite;
             }
+            while (total < len);
         }
 
         /// <summary>The secure bidirectional stream for this connection</summary>