From 700fa7d96d411346ff5c27d404485eb976bd5d92 Mon Sep 17 00:00:00 2001 From: Raul Metsma Date: Mon, 20 Nov 2023 08:36:47 +0200 Subject: [PATCH] Result TAG can fetched after encrypt final (#1231) * Fix compatibility with Tar specification IB-7877 Signed-off-by: Raul Metsma * Result TAG can fetched after encrypt final IB-7880 Signed-off-by: Raul Metsma --------- Signed-off-by: Raul Metsma --- client/CDoc2.cpp | 26 +++++++++++++++++--------- client/Crypto.cpp | 10 +++++----- client/Crypto.h | 3 ++- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/client/CDoc2.cpp b/client/CDoc2.cpp index cb891da16..f0c8bb81e 100644 --- a/client/CDoc2.cpp +++ b/client/CDoc2.cpp @@ -59,7 +59,7 @@ namespace cdoc20 { return true; return dispatchToMain([] { auto *notification = new FadeInNotification(Application::mainWindow(), - ria::qdigidoc4::colors::WHITE, ria::qdigidoc4::colors::MANTIS, 110); + ria::qdigidoc4::colors::WHITE, ria::qdigidoc4::colors::MANTIS, 110); notification->start(QCoreApplication::translate("MainWindow", "Check internet connection"), 750, 3000, 1200); return false; }); @@ -206,7 +206,7 @@ namespace cdoc20 { return io->write(pad) == pad.size(); }; auto toPaxRecord = [](const QByteArray &keyword, const QByteArray &value) { - QByteArray record = " " + keyword + "=" + value + "\n"; + QByteArray record = ' ' + keyword + '=' + value + '\n'; QByteArray result; for(auto len = record.size(); result.size() != len; ++len) result = QByteArray::number(len + 1) + record; @@ -241,20 +241,23 @@ namespace cdoc20 { if(auto size = copyIODevice(file.data.get(), io.get()); size < 0 || !writePadding(size)) return false; } - Header eof{}; - return io->write((const char*)&eof, Header::Size) == Header::Size; + return io->write((const char*)&Header::Empty, Header::Size) == Header::Size && + io->write((const char*)&Header::Empty, Header::Size) == Header::Size; } std::vector files(bool &warning) const { std::vector result; Header h {}; + auto readHeader = [&h, this] { return io->read((char*)&h, Header::Size) == Header::Size; }; while(io->bytesAvailable() > 0) { - if(io->read((char*)&h, Header::Size) != Header::Size) + if(!readHeader()) return {}; if(h.isNull()) { + if(!readHeader() && !h.isNull()) + return {}; warning = io->bytesAvailable() > 0; return result; } @@ -270,7 +273,7 @@ namespace cdoc20 { if(paxData.size() != f.size) return {}; io->skip(padding(f.size)); - if(io->read((char*)&h, Header::Size) != Header::Size || h.isNull() || !h.verify()) + if(!readHeader() || h.isNull() || !h.verify()) return {}; f.size = fromOctal(h.size); for(const QByteArray &data: paxData.split('\n')) @@ -338,8 +341,7 @@ namespace cdoc20 { } bool isNull() { - static const Header zeroBlock {}; - return memcmp(this, &zeroBlock, sizeof(Header)) == 0; + return memcmp(this, &Empty, sizeof(Header)) == 0; } bool verify() { @@ -352,6 +354,7 @@ namespace cdoc20 { referenceChecksum == checkSum.second; } + static const Header Empty; static const int Size; }; @@ -386,6 +389,7 @@ namespace cdoc20 { } }; + const TAR::Header TAR::Header::Empty {}; const int TAR::Header::Size = int(sizeof(TAR::Header)); } @@ -688,12 +692,16 @@ bool CDoc2::save(const QString &path) file.remove(); return false; } - file.write(enc.resultTAG()); if(!enc.result()) { file.remove(); return false; } + QByteArray tag = enc.tag(); +#ifndef NDEBUG + qDebug() << "tag" << tag.toHex(); +#endif + file.write(tag); return true; } diff --git a/client/Crypto.cpp b/client/Crypto.cpp index c8c484b53..ee671b046 100644 --- a/client/Crypto.cpp +++ b/client/Crypto.cpp @@ -77,12 +77,12 @@ bool Crypto::Cipher::result() const return true; } -QByteArray Crypto::Cipher::resultTAG() const +QByteArray Crypto::Cipher::tag() const { - QByteArray result(EVP_CIPHER_CTX_block_size(ctx.get()), 0); - if(isError(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_GET_TAG, int(result.size()), result.data()))) - result.clear(); - return result; + if(QByteArray result(tagLen(), 0); + !isError(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_GET_TAG, int(result.size()), result.data()))) + return result; + return {}; } QByteArray Crypto::aes_wrap(const QByteArray &key, const QByteArray &data, bool encrypt) diff --git a/client/Crypto.h b/client/Crypto.h index 8b9999f67..3c91235ca 100644 --- a/client/Crypto.h +++ b/client/Crypto.h @@ -45,7 +45,8 @@ class Crypto QByteArray update(const QByteArray &data) const; bool update(char *data, int size) const; bool result() const; - QByteArray resultTAG() const; + QByteArray tag() const; + static constexpr int tagLen() { return 16; } }; static QByteArray aes_wrap(const QByteArray &key, const QByteArray &data, bool encrypt);