Pass FrameSize to Cryptor during decryption

pull/14/head
Simon.Dean 2022-01-31 13:46:17 +00:00
parent af3611c296
commit 1d46df4883
5 changed files with 19 additions and 9 deletions

View File

@ -38,7 +38,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

@ -40,7 +40,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

@ -51,6 +51,8 @@ private:
FrameType recentFrameType_;
ReceivePromise::Pointer promise_;
Message::Pointer message_;
int frameSize_;
std::map<messenger::ChannelId, Message::Pointer> channel_assembly_buffers;
};

View File

@ -20,6 +20,7 @@
#include <functional>
#include <f1x/aasdk/Messenger/Cryptor.hpp>
#include <f1x/aasdk/Error/Error.hpp>
#include <f1x/aasdk/Common/Log.hpp>
namespace f1x
{
@ -179,18 +180,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);
@ -202,7 +208,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

@ -122,6 +122,7 @@ void MessageInStream::receiveFrameSizeHandler(const common::DataConstBuffer& buf
});
FrameSize frameSize(buffer);
frameSize_ = (int) frameSize.getSize();
transport_->receive(frameSize.getSize(), std::move(transportPromise));
}
@ -131,7 +132,7 @@ void MessageInStream::receiveFramePayloadHandler(const common::DataConstBuffer&
{
try
{
cryptor_->decrypt(message_->getPayload(), buffer);
cryptor_->decrypt(message_->getPayload(), buffer, frameSize_);
}
catch(const error::Error& e)
{