diff --git a/include/aasdk/Messenger/Cryptor.hpp b/include/aasdk/Messenger/Cryptor.hpp index c74a135..d8b178a 100644 --- a/include/aasdk/Messenger/Cryptor.hpp +++ b/include/aasdk/Messenger/Cryptor.hpp @@ -37,7 +37,7 @@ public: void deinit() override; bool doHandshake() override; size_t encrypt(common::Data& output, const common::DataConstBuffer& buffer) override; - size_t decrypt(common::Data& output, const common::DataConstBuffer& buffer) override; + size_t decrypt(common::Data& output, const common::DataConstBuffer& buffer, int length) override; common::Data readHandshakeBuffer() override; void writeHandshakeBuffer(const common::DataConstBuffer& buffer) override; diff --git a/include/aasdk/Messenger/ICryptor.hpp b/include/aasdk/Messenger/ICryptor.hpp index eb52f79..bc2ba1f 100644 --- a/include/aasdk/Messenger/ICryptor.hpp +++ b/include/aasdk/Messenger/ICryptor.hpp @@ -39,7 +39,7 @@ public: virtual void deinit() = 0; virtual bool doHandshake() = 0; virtual size_t encrypt(common::Data& output, const common::DataConstBuffer& buffer) = 0; - virtual size_t decrypt(common::Data& output, const common::DataConstBuffer& buffer) = 0; + virtual size_t decrypt(common::Data& output, const common::DataConstBuffer& buffer, int length) = 0; virtual common::Data readHandshakeBuffer() = 0; virtual void writeHandshakeBuffer(const common::DataConstBuffer& buffer) = 0; virtual bool isActive() const = 0; diff --git a/src/Messenger/Cryptor.cpp b/src/Messenger/Cryptor.cpp index 154e37c..fca0a25 100644 --- a/src/Messenger/Cryptor.cpp +++ b/src/Messenger/Cryptor.cpp @@ -20,7 +20,7 @@ #include #include #include - +#include namespace aasdk { @@ -178,18 +178,23 @@ size_t Cryptor::encrypt(common::Data& output, const common::DataConstBuffer& buf return this->read(output); } -size_t Cryptor::decrypt(common::Data& output, const common::DataConstBuffer& buffer) +size_t Cryptor::decrypt(common::Data& output, const common::DataConstBuffer& buffer, int frameLength) { + int overhead = 29; + int length = frameLength - overhead; std::lock_guard lock(mutex_); this->write(buffer); const size_t beginOffset = output.size(); - output.resize(beginOffset + 1); - size_t availableBytes = 1; - size_t totalReadSize = 0; + size_t totalReadSize = 0; // Initialise + size_t availableBytes = length; + size_t readBytes = (length - totalReadSize) > 2048 ? 2048 : length - totalReadSize; // Calculate How many Bytes to Read + output.resize(output.size() + readBytes); // Resize Output to match the bytes we want to read - while(availableBytes > 0) + // We try to be a bit more explicit here, using the frame length from the frame itself rather than just blindly reading from the SSL buffer. + + while(readBytes > 0) { const auto& currentBuffer = common::DataBuffer(output, totalReadSize + beginOffset); auto readSize = sslWrapper_->sslRead(ssl_, currentBuffer.data, currentBuffer.size); @@ -201,7 +206,8 @@ size_t Cryptor::decrypt(common::Data& output, const common::DataConstBuffer& buf totalReadSize += readSize; availableBytes = sslWrapper_->getAvailableBytes(ssl_); - output.resize(output.size() + availableBytes); + readBytes = (length - totalReadSize) > 2048 ? 2048 : length - totalReadSize; + output.resize(output.size() + readBytes); } return totalReadSize; diff --git a/src/Messenger/MessageInStream.cpp b/src/Messenger/MessageInStream.cpp index 9c63801..c39e0a6 100644 --- a/src/Messenger/MessageInStream.cpp +++ b/src/Messenger/MessageInStream.cpp @@ -129,7 +129,7 @@ void MessageInStream::receiveFramePayloadHandler(const common::DataConstBuffer& { try { - cryptor_->decrypt(message_->getPayload(), buffer); + cryptor_->decrypt(message_->getPayload(), buffer, frameSize_); } catch(const error::Error& e) {