From 9ab556d76be2afa047b14e3c42359f9d2fc15a83 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 23 Mar 2017 23:35:29 +1030 Subject: Reduce TLS memory usage --- crypto/src/crypto/tls/ByteQueue.cs | 20 ++++++++++++++++++++ crypto/src/crypto/tls/TlsClientProtocol.cs | 2 -- crypto/src/crypto/tls/TlsProtocol.cs | 7 ++++++- crypto/src/crypto/tls/TlsServerProtocol.cs | 1 - 4 files changed, 26 insertions(+), 4 deletions(-) (limited to 'crypto/src') 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; -- cgit 1.5.1