Skip to content

Commit

Permalink
Result TAG can fetched after encrypt final (open-eid#1231)
Browse files Browse the repository at this point in the history
* Fix compatibility with Tar specification

IB-7877

Signed-off-by: Raul Metsma <[email protected]>

* Result TAG can fetched after encrypt final

IB-7880

Signed-off-by: Raul Metsma <[email protected]>

---------

Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma authored Nov 20, 2023
1 parent 7e1fd0d commit 700fa7d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
26 changes: 17 additions & 9 deletions client/CDoc2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<CDoc::File> files(bool &warning) const
{
std::vector<CDoc::File> 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;
}
Expand All @@ -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'))
Expand Down Expand Up @@ -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() {
Expand All @@ -352,6 +354,7 @@ namespace cdoc20 {
referenceChecksum == checkSum.second;
}

static const Header Empty;
static const int Size;
};

Expand Down Expand Up @@ -386,6 +389,7 @@ namespace cdoc20 {
}
};

const TAR::Header TAR::Header::Empty {};
const int TAR::Header::Size = int(sizeof(TAR::Header));
}

Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions client/Crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion client/Crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 700fa7d

Please sign in to comment.