Switch decrypt() to read the number of bytes from the frame size.

pull/15/head
Simon.Dean 2022-02-11 10:29:02 +00:00
parent 59d8d70b83
commit b8bbdf1288
4 changed files with 16 additions and 10 deletions

View File

@ -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;

View File

@ -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;

View File

@ -20,7 +20,7 @@
#include <functional>
#include <aasdk/Messenger/Cryptor.hpp>
#include <aasdk/Error/Error.hpp>
#include <aasdk/Common/Log.hpp>
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<decltype(mutex_)> 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;

View File

@ -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)
{