summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2017-03-23 23:35:29 +1030
committerPeter Dettman <peter.dettman@bouncycastle.org>2017-03-23 23:35:29 +1030
commit9ab556d76be2afa047b14e3c42359f9d2fc15a83 (patch)
treee2df2e69e5689882c7be8371c598b09cf530b496
parentImprovements to WriteHandshakeMessage method (diff)
downloadBouncyCastle.NET-ed25519-9ab556d76be2afa047b14e3c42359f9d2fc15a83.tar.xz
Reduce TLS memory usage
-rw-r--r--crypto/src/crypto/tls/ByteQueue.cs20
-rw-r--r--crypto/src/crypto/tls/TlsClientProtocol.cs2
-rw-r--r--crypto/src/crypto/tls/TlsProtocol.cs7
-rw-r--r--crypto/src/crypto/tls/TlsServerProtocol.cs1
4 files changed, 26 insertions, 4 deletions
diff --git a/crypto/src/crypto/tls/ByteQueue.cs b/crypto/src/crypto/tls/ByteQueue.cs
index c23ec2fbf..f9d4ee606 100644
--- a/crypto/src/crypto/tls/ByteQueue.cs
+++ b/crypto/src/crypto/tls/ByteQueue.cs
@@ -174,5 +174,25 @@ namespace Org.BouncyCastle.Crypto.Tls
             RemoveData(buf, 0, len, skip);
             return buf;
         }
+
+        public void Shrink()
+        {
+            if (available == 0)
+            {
+                databuf = TlsUtilities.EmptyBytes;
+                skipped = 0;
+            }
+            else
+            {
+                int desiredSize = ByteQueue.NextTwoPow(available);
+                if (desiredSize < databuf.Length)
+                {
+                    byte[] tmp = new byte[desiredSize];
+                    Array.Copy(databuf, skipped, tmp, 0, available);
+                    databuf = tmp;
+                    skipped = 0;
+                }
+            }
+        }
     }
 }
diff --git a/crypto/src/crypto/tls/TlsClientProtocol.cs b/crypto/src/crypto/tls/TlsClientProtocol.cs
index d24d13bb5..24fb3f63d 100644
--- a/crypto/src/crypto/tls/TlsClientProtocol.cs
+++ b/crypto/src/crypto/tls/TlsClientProtocol.cs
@@ -147,7 +147,6 @@ namespace Org.BouncyCastle.Crypto.Tls
 
                 SendFinishedMessage();
                 this.mConnectionState = CS_CLIENT_FINISHED;
-                this.mConnectionState = CS_END;
 
                 CompleteHandshake();
                 return;
@@ -241,7 +240,6 @@ namespace Org.BouncyCastle.Crypto.Tls
 
                     ProcessFinishedMessage(buf);
                     this.mConnectionState = CS_SERVER_FINISHED;
-                    this.mConnectionState = CS_END;
 
                     CompleteHandshake();
                     break;
diff --git a/crypto/src/crypto/tls/TlsProtocol.cs b/crypto/src/crypto/tls/TlsProtocol.cs
index 667b3eb33..69361bf6f 100644
--- a/crypto/src/crypto/tls/TlsProtocol.cs
+++ b/crypto/src/crypto/tls/TlsProtocol.cs
@@ -43,7 +43,7 @@ namespace Org.BouncyCastle.Crypto.Tls
         /*
          * Queues for data from some protocols.
          */
-        private ByteQueue mApplicationDataQueue = new ByteQueue();
+        private ByteQueue mApplicationDataQueue = new ByteQueue(0);
         private ByteQueue mAlertQueue = new ByteQueue(2);
         private ByteQueue mHandshakeQueue = new ByteQueue();
     //    private ByteQueue mHeartbeatQueue = new ByteQueue();
@@ -182,6 +182,11 @@ namespace Org.BouncyCastle.Crypto.Tls
         {
             try
             {
+                this.mConnectionState = CS_END;
+
+                this.mAlertQueue.Shrink();
+                this.mHandshakeQueue.Shrink();
+
                 this.mRecordStream.FinaliseHandshake();
 
                 this.mAppDataSplitEnabled = !TlsUtilities.IsTlsV11(Context);
diff --git a/crypto/src/crypto/tls/TlsServerProtocol.cs b/crypto/src/crypto/tls/TlsServerProtocol.cs
index 6642f43f4..db823539a 100644
--- a/crypto/src/crypto/tls/TlsServerProtocol.cs
+++ b/crypto/src/crypto/tls/TlsServerProtocol.cs
@@ -365,7 +365,6 @@ namespace Org.BouncyCastle.Crypto.Tls
 
                     SendFinishedMessage();
                     this.mConnectionState = CS_SERVER_FINISHED;
-                    this.mConnectionState = CS_END;
 
                     CompleteHandshake();
                     break;