mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-04-04 21:17:43 +03:00
Improve error reporing of layered streams.
This commit is contained in:
parent
f6243675c9
commit
cfffdae573
4 changed files with 22 additions and 15 deletions
|
@ -150,6 +150,7 @@ bool HashedBlockStream::readHashedBlock()
|
||||||
if (m_blockSize == 0) {
|
if (m_blockSize == 0) {
|
||||||
if (hash.count('\0') != 32) {
|
if (hash.count('\0') != 32) {
|
||||||
m_error = true;
|
m_error = true;
|
||||||
|
setErrorString("Invalid hash of final block.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,6 +167,7 @@ bool HashedBlockStream::readHashedBlock()
|
||||||
|
|
||||||
if (hash != CryptoHash::hash(m_buffer, CryptoHash::Sha256)) {
|
if (hash != CryptoHash::hash(m_buffer, CryptoHash::Sha256)) {
|
||||||
m_error = true;
|
m_error = true;
|
||||||
|
setErrorString("Mismatch between hash and data.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,6 +215,7 @@ bool HashedBlockStream::writeHashedBlock()
|
||||||
{
|
{
|
||||||
if (!Endian::writeInt32(m_blockIndex, m_baseDevice, ByteOrder)) {
|
if (!Endian::writeInt32(m_blockIndex, m_baseDevice, ByteOrder)) {
|
||||||
m_error = true;
|
m_error = true;
|
||||||
|
setErrorString(m_baseDevice->errorString());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
m_blockIndex++;
|
m_blockIndex++;
|
||||||
|
@ -227,17 +230,20 @@ bool HashedBlockStream::writeHashedBlock()
|
||||||
|
|
||||||
if (m_baseDevice->write(hash) != hash.size()) {
|
if (m_baseDevice->write(hash) != hash.size()) {
|
||||||
m_error = true;
|
m_error = true;
|
||||||
|
setErrorString(m_baseDevice->errorString());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Endian::writeInt32(m_buffer.size(), m_baseDevice, ByteOrder)) {
|
if (!Endian::writeInt32(m_buffer.size(), m_baseDevice, ByteOrder)) {
|
||||||
m_error = true;
|
m_error = true;
|
||||||
|
setErrorString(m_baseDevice->errorString());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_buffer.isEmpty()) {
|
if (!m_buffer.isEmpty()) {
|
||||||
if (m_baseDevice->write(m_buffer) != m_buffer.size()) {
|
if (m_baseDevice->write(m_buffer) != m_buffer.size()) {
|
||||||
m_error = true;
|
m_error = true;
|
||||||
|
setErrorString(m_baseDevice->errorString());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -147,6 +147,7 @@ bool SymmetricCipherStream::readBlock()
|
||||||
else if (padLength > m_cipher->blockSize()) {
|
else if (padLength > m_cipher->blockSize()) {
|
||||||
// invalid padding
|
// invalid padding
|
||||||
m_error = true;
|
m_error = true;
|
||||||
|
setErrorString("Invalid padding.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -217,7 +218,7 @@ bool SymmetricCipherStream::writeBlock(bool lastBlock)
|
||||||
|
|
||||||
if (m_baseDevice->write(m_buffer) != m_buffer.size()) {
|
if (m_baseDevice->write(m_buffer) != m_buffer.size()) {
|
||||||
m_error = true;
|
m_error = true;
|
||||||
// TODO: copy error string
|
setErrorString(m_cipher->errorString());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -36,15 +36,15 @@ void TestHashedBlockStream::testWriteRead()
|
||||||
QByteArray data = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
|
QByteArray data = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
|
||||||
|
|
||||||
QBuffer buffer;
|
QBuffer buffer;
|
||||||
buffer.open(QIODevice::ReadWrite);
|
QVERIFY(buffer.open(QIODevice::ReadWrite));
|
||||||
|
|
||||||
HashedBlockStream writer(&buffer, 16);
|
HashedBlockStream writer(&buffer, 16);
|
||||||
writer.open(QIODevice::WriteOnly);
|
QVERIFY(writer.open(QIODevice::WriteOnly));
|
||||||
|
|
||||||
HashedBlockStream reader(&buffer);
|
HashedBlockStream reader(&buffer);
|
||||||
reader.open(QIODevice::ReadOnly);
|
QVERIFY(reader.open(QIODevice::ReadOnly));
|
||||||
|
|
||||||
writer.write(data.left(16));
|
QCOMPARE(writer.write(data.left(16)), qint64(16));
|
||||||
QVERIFY(writer.reset());
|
QVERIFY(writer.reset());
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
QCOMPARE(reader.read(17), data.left(16));
|
QCOMPARE(reader.read(17), data.left(16));
|
||||||
|
@ -52,7 +52,7 @@ void TestHashedBlockStream::testWriteRead()
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
buffer.buffer().clear();
|
buffer.buffer().clear();
|
||||||
|
|
||||||
writer.write(data.left(10));
|
QCOMPARE(writer.write(data.left(10)), qint64(10));
|
||||||
QVERIFY(writer.reset());
|
QVERIFY(writer.reset());
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
QCOMPARE(reader.read(5), data.left(5));
|
QCOMPARE(reader.read(5), data.left(5));
|
||||||
|
@ -62,7 +62,7 @@ void TestHashedBlockStream::testWriteRead()
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
buffer.buffer().clear();
|
buffer.buffer().clear();
|
||||||
|
|
||||||
writer.write(data.left(20));
|
QCOMPARE(writer.write(data.left(20)), qint64(20));
|
||||||
QVERIFY(writer.reset());
|
QVERIFY(writer.reset());
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
QCOMPARE(reader.read(20), data.left(20));
|
QCOMPARE(reader.read(20), data.left(20));
|
||||||
|
|
|
@ -57,12 +57,12 @@ void TestSymmetricCipher::testAes256CbcEncryption()
|
||||||
SymmetricCipher::Encrypt);
|
SymmetricCipher::Encrypt);
|
||||||
QVERIFY(stream.init(key, iv));
|
QVERIFY(stream.init(key, iv));
|
||||||
buffer.open(QIODevice::WriteOnly);
|
buffer.open(QIODevice::WriteOnly);
|
||||||
stream.open(QIODevice::WriteOnly);
|
QVERIFY(stream.open(QIODevice::WriteOnly));
|
||||||
QVERIFY(stream.reset());
|
QVERIFY(stream.reset());
|
||||||
|
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
buffer.buffer().clear();
|
buffer.buffer().clear();
|
||||||
stream.write(plainText.left(16));
|
QCOMPARE(stream.write(plainText.left(16)), qint64(16));
|
||||||
QCOMPARE(buffer.data(), cipherText.left(16));
|
QCOMPARE(buffer.data(), cipherText.left(16));
|
||||||
QVERIFY(stream.reset());
|
QVERIFY(stream.reset());
|
||||||
// make sure padding is written
|
// make sure padding is written
|
||||||
|
@ -70,13 +70,13 @@ void TestSymmetricCipher::testAes256CbcEncryption()
|
||||||
|
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
buffer.buffer().clear();
|
buffer.buffer().clear();
|
||||||
stream.write(plainText.left(10));
|
QCOMPARE(stream.write(plainText.left(10)), qint64(10));
|
||||||
QVERIFY(buffer.data().isEmpty());
|
QVERIFY(buffer.data().isEmpty());
|
||||||
|
|
||||||
QVERIFY(stream.reset());
|
QVERIFY(stream.reset());
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
buffer.buffer().clear();
|
buffer.buffer().clear();
|
||||||
stream.write(plainText.left(10));
|
QCOMPARE(stream.write(plainText.left(10)), qint64(10));
|
||||||
stream.close();
|
stream.close();
|
||||||
QCOMPARE(buffer.data().size(), 16);
|
QCOMPARE(buffer.data().size(), 16);
|
||||||
}
|
}
|
||||||
|
@ -106,20 +106,20 @@ void TestSymmetricCipher::testAes256CbcDecryption()
|
||||||
SymmetricCipher::Decrypt);
|
SymmetricCipher::Decrypt);
|
||||||
QVERIFY(stream.init(key, iv));
|
QVERIFY(stream.init(key, iv));
|
||||||
buffer.open(QIODevice::ReadOnly);
|
buffer.open(QIODevice::ReadOnly);
|
||||||
stream.open(QIODevice::ReadOnly);
|
QVERIFY(stream.open(QIODevice::ReadOnly));
|
||||||
|
|
||||||
QCOMPARE(stream.read(10),
|
QCOMPARE(stream.read(10),
|
||||||
plainText.left(10));
|
plainText.left(10));
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
stream.reset();
|
QVERIFY(stream.reset());
|
||||||
QCOMPARE(stream.read(20),
|
QCOMPARE(stream.read(20),
|
||||||
plainText.left(20));
|
plainText.left(20));
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
stream.reset();
|
QVERIFY(stream.reset());
|
||||||
QCOMPARE(stream.read(16),
|
QCOMPARE(stream.read(16),
|
||||||
plainText.left(16));
|
plainText.left(16));
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
stream.reset();
|
QVERIFY(stream.reset());
|
||||||
QCOMPARE(stream.read(100),
|
QCOMPARE(stream.read(100),
|
||||||
plainText);
|
plainText);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue