Skip to content

Commit 822ed79

Browse files
authored
Read tag from end of stream (#1232)
IB-7882 Signed-off-by: Raul Metsma <[email protected]>
1 parent 9100625 commit 822ed79

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
container: ${{ matrix.container }}
5656
strategy:
5757
matrix:
58-
container: ['ubuntu:20.04', 'ubuntu:22.04', 'ubuntu:23.04', 'ubuntu:23.10']
58+
container: ['ubuntu:20.04', 'ubuntu:22.04', 'ubuntu:23.10']
5959
env:
6060
DEBIAN_FRONTEND: noninteractive
6161
DEBFULLNAME: github-actions

client/CDoc2.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ namespace cdoc20 {
127127

128128
qint64 bytesAvailable() const final
129129
{
130-
return io->bytesAvailable() + buf.size() + QIODevice::bytesAvailable();
130+
return (io->bytesAvailable() - Crypto::Cipher::tagLen()) + buf.size() + QIODevice::bytesAvailable();
131131
}
132132

133133
qint64 readData(char *data, qint64 maxlen) final
@@ -137,7 +137,8 @@ namespace cdoc20 {
137137
std::array<char,CHUNK> in{};
138138
for(int res = Z_OK; s.avail_out > 0 && res == Z_OK;)
139139
{
140-
if(auto size = io->read(in.data(), in.size()); size > 0)
140+
if(auto insize = io->bytesAvailable() - Crypto::Cipher::tagLen(),
141+
size = io->read(in.data(), qMin<qint64>(insize, in.size())); size > 0)
141142
{
142143
if(!cipher->update(in.data(), int(size)))
143144
return -1;
@@ -532,6 +533,11 @@ bool CDoc2::decryptPayload(const QByteArray &fmk)
532533
files = cdoc20::TAR(std::unique_ptr<QIODevice>(new cdoc20::stream(this, &dec))).files(warning);
533534
if(warning)
534535
setLastError(tr("CDoc contains additional payload data that is not part of content"));
536+
QByteArray tag = read(16);
537+
#ifndef NDEBUG
538+
qDebug() << "tag" << tag.toHex();
539+
#endif
540+
dec.setTag(tag);
535541
if(!dec.result())
536542
files.clear();
537543
return !files.empty();

client/Crypto.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ QByteArray Crypto::Cipher::tag() const
8585
return {};
8686
}
8787

88+
bool Crypto::Cipher::setTag(const QByteArray &data) const
89+
{
90+
return !isError(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_TAG, int(data.size()), const_cast<char*>(data.data())));
91+
}
92+
8893
QByteArray Crypto::aes_wrap(const QByteArray &key, const QByteArray &data, bool encrypt)
8994
{
9095
Cipher c(key.size() == 32 ? EVP_aes_256_wrap() : EVP_aes_128_wrap(), key, {}, encrypt);

client/Crypto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Crypto
4747
bool result() const;
4848
QByteArray tag() const;
4949
static constexpr int tagLen() { return 16; }
50+
bool setTag(const QByteArray &data) const;
5051
};
5152

5253
static QByteArray aes_wrap(const QByteArray &key, const QByteArray &data, bool encrypt);

0 commit comments

Comments
 (0)