2018-01-17 12:15:37 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "TestKdbx4.h"
|
2018-01-24 07:22:20 -05:00
|
|
|
|
2018-03-31 16:01:30 -04:00
|
|
|
#include "config-keepassx-tests.h"
|
2018-01-17 12:15:37 -05:00
|
|
|
#include "core/Metadata.h"
|
2018-03-31 16:01:30 -04:00
|
|
|
#include "format/KdbxXmlReader.h"
|
|
|
|
#include "format/KdbxXmlWriter.h"
|
2018-01-17 12:15:37 -05:00
|
|
|
#include "format/KeePass2.h"
|
2018-01-17 14:52:29 -05:00
|
|
|
#include "format/KeePass2Reader.h"
|
|
|
|
#include "format/KeePass2Writer.h"
|
2018-03-31 16:01:30 -04:00
|
|
|
#include "keys/FileKey.h"
|
|
|
|
#include "keys/PasswordKey.h"
|
|
|
|
#include "mock/MockChallengeResponseKey.h"
|
2022-04-18 13:40:10 -04:00
|
|
|
#include "mock/MockClock.h"
|
2021-07-11 22:10:29 -04:00
|
|
|
#include <QTest>
|
2018-01-17 12:15:37 -05:00
|
|
|
|
2020-01-09 20:11:43 -05:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
QCoreApplication::setAttribute(Qt::AA_Use96Dpi, true);
|
|
|
|
QTEST_SET_MAIN_SOURCE_PATH
|
|
|
|
|
|
|
|
TestKdbx4Argon2 argon2Test;
|
|
|
|
TestKdbx4AesKdf aesKdfTest;
|
2021-11-13 12:46:05 -05:00
|
|
|
TestKdbx4Format kdbx4Test;
|
|
|
|
return QTest::qExec(&argon2Test, argc, argv) || QTest::qExec(&aesKdfTest, argc, argv)
|
|
|
|
|| QTest::qExec(&kdbx4Test, argc, argv);
|
2020-01-09 20:11:43 -05:00
|
|
|
}
|
2018-01-17 12:15:37 -05:00
|
|
|
|
2020-01-09 20:11:43 -05:00
|
|
|
void TestKdbx4Argon2::initTestCaseImpl()
|
2018-01-17 12:15:37 -05:00
|
|
|
{
|
2020-11-20 15:49:56 -05:00
|
|
|
m_xmlDb->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_ARGON2D)));
|
|
|
|
m_kdbxSourceDb->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_ARGON2D)));
|
2018-01-17 12:15:37 -05:00
|
|
|
}
|
|
|
|
|
2020-01-09 20:11:43 -05:00
|
|
|
QSharedPointer<Database>
|
|
|
|
TestKdbx4Argon2::readXml(const QString& path, bool strictMode, bool& hasError, QString& errorString)
|
2018-01-17 12:15:37 -05:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
KdbxXmlReader reader(KeePass2::FILE_VERSION_4);
|
2018-01-17 12:15:37 -05:00
|
|
|
reader.setStrictMode(strictMode);
|
|
|
|
auto db = reader.readDatabase(path);
|
|
|
|
hasError = reader.hasError();
|
|
|
|
errorString = reader.errorString();
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:11:43 -05:00
|
|
|
QSharedPointer<Database> TestKdbx4Argon2::readXml(QBuffer* buf, bool strictMode, bool& hasError, QString& errorString)
|
2018-01-17 12:15:37 -05:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
KdbxXmlReader reader(KeePass2::FILE_VERSION_4);
|
2018-01-17 12:15:37 -05:00
|
|
|
reader.setStrictMode(strictMode);
|
|
|
|
auto db = reader.readDatabase(buf);
|
|
|
|
hasError = reader.hasError();
|
|
|
|
errorString = reader.errorString();
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:11:43 -05:00
|
|
|
void TestKdbx4Argon2::writeXml(QBuffer* buf, Database* db, bool& hasError, QString& errorString)
|
2018-01-17 12:15:37 -05:00
|
|
|
{
|
2018-01-17 14:52:29 -05:00
|
|
|
KdbxXmlWriter writer(KeePass2::FILE_VERSION_4);
|
2018-01-17 12:15:37 -05:00
|
|
|
writer.writeDatabase(buf, db);
|
|
|
|
hasError = writer.hasError();
|
|
|
|
errorString = writer.errorString();
|
|
|
|
}
|
2018-01-17 14:52:29 -05:00
|
|
|
|
2020-01-09 20:11:43 -05:00
|
|
|
void TestKdbx4Argon2::readKdbx(QIODevice* device,
|
|
|
|
QSharedPointer<const CompositeKey> key,
|
|
|
|
QSharedPointer<Database> db,
|
|
|
|
bool& hasError,
|
|
|
|
QString& errorString)
|
2018-01-17 14:52:29 -05:00
|
|
|
{
|
|
|
|
KeePass2Reader reader;
|
2018-11-22 05:47:31 -05:00
|
|
|
reader.readDatabase(device, key, db.data());
|
2018-01-17 14:52:29 -05:00
|
|
|
hasError = reader.hasError();
|
|
|
|
if (hasError) {
|
|
|
|
errorString = reader.errorString();
|
|
|
|
}
|
|
|
|
QCOMPARE(reader.version(), KeePass2::FILE_VERSION_4);
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:11:43 -05:00
|
|
|
void TestKdbx4Argon2::writeKdbx(QIODevice* device, Database* db, bool& hasError, QString& errorString)
|
2018-01-17 14:52:29 -05:00
|
|
|
{
|
|
|
|
if (db->kdf()->uuid() == KeePass2::KDF_AES_KDBX3) {
|
2020-11-20 15:49:56 -05:00
|
|
|
db->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_ARGON2D)));
|
2018-01-17 14:52:29 -05:00
|
|
|
}
|
|
|
|
KeePass2Writer writer;
|
|
|
|
hasError = writer.writeDatabase(device, db);
|
|
|
|
hasError = writer.hasError();
|
|
|
|
if (hasError) {
|
|
|
|
errorString = writer.errorString();
|
|
|
|
}
|
|
|
|
QCOMPARE(writer.version(), KeePass2::FILE_VERSION_4);
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:46:05 -05:00
|
|
|
void TestKdbx4AesKdf::initTestCaseImpl()
|
|
|
|
{
|
|
|
|
m_xmlDb->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_AES_KDBX4)));
|
|
|
|
m_kdbxSourceDb->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_AES_KDBX4)));
|
|
|
|
}
|
|
|
|
|
2018-03-22 17:56:05 -04:00
|
|
|
Q_DECLARE_METATYPE(QUuid)
|
2022-04-18 13:40:10 -04:00
|
|
|
|
|
|
|
void TestKdbx4Format::init()
|
|
|
|
{
|
|
|
|
MockClock::setup(new MockClock());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKdbx4Format::cleanup()
|
|
|
|
{
|
|
|
|
MockClock::teardown();
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:46:05 -05:00
|
|
|
void TestKdbx4Format::testFormat400()
|
2018-01-17 14:52:29 -05:00
|
|
|
{
|
|
|
|
QString filename = QString(KEEPASSX_TEST_DATA_DIR).append("/Format400.kdbx");
|
2018-05-13 17:21:43 -04:00
|
|
|
auto key = QSharedPointer<CompositeKey>::create();
|
|
|
|
key->addKey(QSharedPointer<PasswordKey>::create("t"));
|
2018-01-17 14:52:29 -05:00
|
|
|
KeePass2Reader reader;
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db = QSharedPointer<Database>::create();
|
|
|
|
reader.readDatabase(filename, key, db.data());
|
2018-01-17 14:52:29 -05:00
|
|
|
QCOMPARE(reader.version(), KeePass2::FILE_VERSION_4);
|
|
|
|
QVERIFY(db.data());
|
|
|
|
QVERIFY(!reader.hasError());
|
|
|
|
|
|
|
|
QCOMPARE(db->rootGroup()->name(), QString("Format400"));
|
|
|
|
QCOMPARE(db->metadata()->name(), QString("Format400"));
|
|
|
|
QCOMPARE(db->rootGroup()->entries().size(), 1);
|
|
|
|
auto entry = db->rootGroup()->entries().at(0);
|
|
|
|
|
|
|
|
QCOMPARE(entry->title(), QString("Format400"));
|
|
|
|
QCOMPARE(entry->username(), QString("Format400"));
|
|
|
|
QCOMPARE(entry->attributes()->keys().size(), 6);
|
|
|
|
QCOMPARE(entry->attributes()->value("Format400"), QString("Format400"));
|
|
|
|
QCOMPARE(entry->attachments()->keys().size(), 1);
|
|
|
|
QCOMPARE(entry->attachments()->value("Format400"), QByteArray("Format400\n"));
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:46:05 -05:00
|
|
|
void TestKdbx4Format::testFormat400Upgrade()
|
2018-01-17 14:52:29 -05:00
|
|
|
{
|
2018-03-22 17:56:05 -04:00
|
|
|
QFETCH(QUuid, kdfUuid);
|
|
|
|
QFETCH(QUuid, cipherUuid);
|
2018-02-18 15:01:22 -05:00
|
|
|
QFETCH(bool, addCustomData);
|
2018-01-17 14:52:29 -05:00
|
|
|
QFETCH(quint32, expectedVersion);
|
|
|
|
|
|
|
|
QScopedPointer<Database> sourceDb(new Database());
|
2018-03-08 04:20:25 -05:00
|
|
|
sourceDb->changeKdf(fastKdf(sourceDb->kdf()));
|
2018-01-17 14:52:29 -05:00
|
|
|
sourceDb->metadata()->setName("Wubba lubba dub dub");
|
2018-03-31 16:01:30 -04:00
|
|
|
QCOMPARE(sourceDb->kdf()->uuid(), KeePass2::KDF_AES_KDBX3); // default is legacy AES-KDF
|
2018-01-17 14:52:29 -05:00
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
auto key = QSharedPointer<CompositeKey>::create();
|
|
|
|
key->addKey(QSharedPointer<PasswordKey>::create("I am in great pain, please help me!"));
|
2018-01-17 14:52:29 -05:00
|
|
|
sourceDb->setKey(key, true, true);
|
|
|
|
|
|
|
|
QBuffer buffer;
|
|
|
|
buffer.open(QBuffer::ReadWrite);
|
|
|
|
|
|
|
|
// upgrade to KDBX 4 by changing KDF and Cipher
|
2018-03-08 04:20:25 -05:00
|
|
|
sourceDb->changeKdf(fastKdf(KeePass2::uuidToKdf(kdfUuid)));
|
2018-01-17 14:52:29 -05:00
|
|
|
sourceDb->setCipher(cipherUuid);
|
2018-02-18 15:01:22 -05:00
|
|
|
|
2018-02-28 17:02:45 -05:00
|
|
|
// CustomData in meta should not cause any version change
|
|
|
|
sourceDb->metadata()->customData()->set("CustomPublicData", "Hey look, I turned myself into a pickle!");
|
2018-02-18 15:01:22 -05:00
|
|
|
if (addCustomData) {
|
2018-02-28 17:02:45 -05:00
|
|
|
// this, however, should
|
2018-03-31 16:01:30 -04:00
|
|
|
sourceDb->rootGroup()->customData()->set("CustomGroupData",
|
|
|
|
"I just killed my family! I don't care who they were!");
|
2018-02-18 15:01:22 -05:00
|
|
|
}
|
|
|
|
|
2018-01-17 14:52:29 -05:00
|
|
|
KeePass2Writer writer;
|
|
|
|
writer.writeDatabase(&buffer, sourceDb.data());
|
|
|
|
if (writer.hasError()) {
|
|
|
|
QFAIL(qPrintable(QString("Error while writing database: %1").arg(writer.errorString())));
|
|
|
|
}
|
|
|
|
|
|
|
|
// read buffer back
|
|
|
|
buffer.seek(0);
|
|
|
|
KeePass2Reader reader;
|
2018-11-22 05:47:31 -05:00
|
|
|
auto targetDb = QSharedPointer<Database>::create();
|
|
|
|
reader.readDatabase(&buffer, key, targetDb.data());
|
2018-01-17 14:52:29 -05:00
|
|
|
if (reader.hasError()) {
|
|
|
|
QFAIL(qPrintable(QString("Error while reading database: %1").arg(reader.errorString())));
|
|
|
|
}
|
|
|
|
|
|
|
|
QVERIFY(targetDb->rootGroup());
|
|
|
|
QCOMPARE(targetDb->metadata()->name(), sourceDb->metadata()->name());
|
|
|
|
|
|
|
|
QCOMPARE(reader.version(), expectedVersion);
|
|
|
|
QCOMPARE(targetDb->cipher(), cipherUuid);
|
2019-05-01 18:35:08 -04:00
|
|
|
QCOMPARE(targetDb->metadata()->customData()->value("CustomPublicData"),
|
|
|
|
sourceDb->metadata()->customData()->value("CustomPublicData"));
|
|
|
|
QCOMPARE(targetDb->rootGroup()->customData()->value("CustomGroupData"),
|
|
|
|
sourceDb->rootGroup()->customData()->value("CustomGroupData"));
|
2018-01-17 14:52:29 -05:00
|
|
|
}
|
|
|
|
|
2018-02-21 21:27:55 -05:00
|
|
|
// clang-format off
|
2021-11-13 12:46:05 -05:00
|
|
|
void TestKdbx4Format::testFormat400Upgrade_data()
|
2018-01-17 14:52:29 -05:00
|
|
|
{
|
2018-03-22 17:56:05 -04:00
|
|
|
QTest::addColumn<QUuid>("kdfUuid");
|
|
|
|
QTest::addColumn<QUuid>("cipherUuid");
|
2018-02-18 15:01:22 -05:00
|
|
|
QTest::addColumn<bool>("addCustomData");
|
2018-01-17 14:52:29 -05:00
|
|
|
QTest::addColumn<quint32>("expectedVersion");
|
|
|
|
|
2021-11-19 18:32:09 -05:00
|
|
|
auto constexpr kdbx3 = KeePass2::FILE_VERSION_3_1;
|
|
|
|
auto constexpr kdbx4 = KeePass2::FILE_VERSION_4;
|
2018-01-17 14:52:29 -05:00
|
|
|
|
2020-11-20 15:49:56 -05:00
|
|
|
QTest::newRow("Argon2d + AES") << KeePass2::KDF_ARGON2D << KeePass2::CIPHER_AES256 << false << kdbx4;
|
|
|
|
QTest::newRow("Argon2id + AES") << KeePass2::KDF_ARGON2ID << KeePass2::CIPHER_AES256 << false << kdbx4;
|
|
|
|
QTest::newRow("AES-KDF + AES") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_AES256 << false << kdbx4;
|
|
|
|
QTest::newRow("AES-KDF (legacy) + AES") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_AES256 << false << kdbx3;
|
|
|
|
QTest::newRow("Argon2d + AES + CustomData") << KeePass2::KDF_ARGON2D << KeePass2::CIPHER_AES256 << true << kdbx4;
|
|
|
|
QTest::newRow("Argon2id + AES + CustomData") << KeePass2::KDF_ARGON2ID << KeePass2::CIPHER_AES256 << true << kdbx4;
|
|
|
|
QTest::newRow("AES-KDF + AES + CustomData") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_AES256 << true << kdbx4;
|
|
|
|
QTest::newRow("AES-KDF (legacy) + AES + CustomData") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_AES256 << true << kdbx4;
|
|
|
|
|
|
|
|
QTest::newRow("Argon2d + ChaCha20") << KeePass2::KDF_ARGON2D << KeePass2::CIPHER_CHACHA20 << false << kdbx4;
|
|
|
|
QTest::newRow("Argon2id + ChaCha20") << KeePass2::KDF_ARGON2ID << KeePass2::CIPHER_CHACHA20 << false << kdbx4;
|
|
|
|
QTest::newRow("AES-KDF + ChaCha20") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_CHACHA20 << false << kdbx4;
|
|
|
|
QTest::newRow("AES-KDF (legacy) + ChaCha20") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_CHACHA20 << false << kdbx3;
|
|
|
|
QTest::newRow("Argon2d + ChaCha20 + CustomData") << KeePass2::KDF_ARGON2D << KeePass2::CIPHER_CHACHA20 << true << kdbx4;
|
|
|
|
QTest::newRow("Argon2id + ChaCha20 + CustomData") << KeePass2::KDF_ARGON2ID << KeePass2::CIPHER_CHACHA20 << true << kdbx4;
|
|
|
|
QTest::newRow("AES-KDF + ChaCha20 + CustomData") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_CHACHA20 << true << kdbx4;
|
|
|
|
QTest::newRow("AES-KDF (legacy) + ChaCha20 + CustomData") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_CHACHA20 << true << kdbx4;
|
|
|
|
|
|
|
|
QTest::newRow("Argon2d + Twofish") << KeePass2::KDF_ARGON2D << KeePass2::CIPHER_TWOFISH << false << kdbx4;
|
|
|
|
QTest::newRow("Argon2id + Twofish") << KeePass2::KDF_ARGON2ID << KeePass2::CIPHER_TWOFISH << false << kdbx4;
|
|
|
|
QTest::newRow("AES-KDF + Twofish") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_TWOFISH << false << kdbx4;
|
|
|
|
QTest::newRow("AES-KDF (legacy) + Twofish") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_TWOFISH << false << kdbx3;
|
|
|
|
QTest::newRow("Argon2d + Twofish + CustomData") << KeePass2::KDF_ARGON2D << KeePass2::CIPHER_TWOFISH << true << kdbx4;
|
|
|
|
QTest::newRow("Argon2id + Twofish + CustomData") << KeePass2::KDF_ARGON2ID << KeePass2::CIPHER_TWOFISH << true << kdbx4;
|
|
|
|
QTest::newRow("AES-KDF + Twofish + CustomData") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_TWOFISH << true << kdbx4;
|
|
|
|
QTest::newRow("AES-KDF (legacy) + Twofish + CustomData") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_TWOFISH << true << kdbx4;
|
2018-01-17 14:52:29 -05:00
|
|
|
}
|
2018-02-21 21:27:55 -05:00
|
|
|
// clang-format on
|
2018-02-18 13:54:07 -05:00
|
|
|
|
2021-11-13 12:46:05 -05:00
|
|
|
void TestKdbx4Format::testFormat410Upgrade()
|
|
|
|
{
|
|
|
|
Database db;
|
|
|
|
db.changeKdf(fastKdf(db.kdf()));
|
|
|
|
QCOMPARE(db.kdf()->uuid(), KeePass2::KDF_AES_KDBX3);
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
|
|
|
|
auto group1 = new Group();
|
|
|
|
group1->setUuid(QUuid::createUuid());
|
|
|
|
group1->setParent(db.rootGroup());
|
|
|
|
|
|
|
|
auto group2 = new Group();
|
|
|
|
group2->setUuid(QUuid::createUuid());
|
|
|
|
group2->setParent(db.rootGroup());
|
|
|
|
|
|
|
|
auto entry = new Entry();
|
|
|
|
entry->setUuid(QUuid::createUuid());
|
|
|
|
entry->setGroup(group1);
|
|
|
|
|
|
|
|
// Groups with tags
|
|
|
|
group1->setTags("tag");
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_4_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
group1->setTags("");
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
|
|
|
|
// PasswordQuality flag set
|
|
|
|
entry->setExcludeFromReports(true);
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_4_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
entry->setExcludeFromReports(false);
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
|
|
|
|
// Previous parent group set on group
|
|
|
|
group1->setPreviousParentGroup(group2);
|
|
|
|
QCOMPARE(group1->previousParentGroup(), group2);
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_4_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
group1->setPreviousParentGroup(nullptr);
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
|
|
|
|
// Previous parent group set on entry
|
|
|
|
entry->setPreviousParentGroup(group2);
|
|
|
|
QCOMPARE(entry->previousParentGroup(), group2);
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_4_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
entry->setPreviousParentGroup(nullptr);
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
|
|
|
|
// Custom icons with name or modification date
|
|
|
|
Metadata::CustomIconData customIcon;
|
|
|
|
auto iconUuid = QUuid::createUuid();
|
|
|
|
db.metadata()->addCustomIcon(iconUuid, customIcon);
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
customIcon.name = "abc";
|
|
|
|
db.metadata()->removeCustomIcon(iconUuid);
|
|
|
|
db.metadata()->addCustomIcon(iconUuid, customIcon);
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_4_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
customIcon.name.clear();
|
|
|
|
customIcon.lastModified = Clock::currentDateTimeUtc();
|
|
|
|
db.metadata()->removeCustomIcon(iconUuid);
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
db.metadata()->addCustomIcon(iconUuid, customIcon);
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_4_1);
|
2021-11-13 12:46:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestKdbx4Format::testUpgradeMasterKeyIntegrity()
|
2018-03-01 15:25:29 -05:00
|
|
|
{
|
|
|
|
QFETCH(QString, upgradeAction);
|
|
|
|
QFETCH(quint32, expectedVersion);
|
|
|
|
|
|
|
|
// prepare composite key
|
2018-05-13 17:21:43 -04:00
|
|
|
auto passwordKey = QSharedPointer<PasswordKey>::create("turXpGMQiUE6CkPvWacydAKsnp4cxz");
|
2018-03-01 15:25:29 -05:00
|
|
|
|
|
|
|
QByteArray fileKeyBytes("Ma6hHov98FbPeyAL22XhcgmpJk8xjQ");
|
|
|
|
QBuffer fileKeyBuffer(&fileKeyBytes);
|
|
|
|
fileKeyBuffer.open(QBuffer::ReadOnly);
|
2018-05-13 17:21:43 -04:00
|
|
|
auto fileKey = QSharedPointer<FileKey>::create();
|
|
|
|
fileKey->load(&fileKeyBuffer);
|
2018-03-01 15:25:29 -05:00
|
|
|
|
|
|
|
auto crKey = QSharedPointer<MockChallengeResponseKey>::create(QByteArray("azdJnbVCFE76vV6t9RJ2DS6xvSS93k"));
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
auto compositeKey = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey->addKey(passwordKey);
|
|
|
|
compositeKey->addKey(fileKey);
|
|
|
|
compositeKey->addChallengeResponseKey(crKey);
|
2018-03-01 15:25:29 -05:00
|
|
|
|
|
|
|
QScopedPointer<Database> db(new Database());
|
2018-03-08 04:20:25 -05:00
|
|
|
db->changeKdf(fastKdf(db->kdf()));
|
2020-01-09 20:11:43 -05:00
|
|
|
QCOMPARE(db->kdf()->uuid(), KeePass2::KDF_AES_KDBX3); // default is legacy AES-KDF
|
2018-03-01 15:25:29 -05:00
|
|
|
db->setKey(compositeKey);
|
|
|
|
|
|
|
|
// upgrade the database by a specific method
|
|
|
|
if (upgradeAction == "none") {
|
|
|
|
// do nothing
|
|
|
|
} else if (upgradeAction == "meta-customdata") {
|
|
|
|
db->metadata()->customData()->set("abc", "def");
|
|
|
|
} else if (upgradeAction == "kdf-aes-kdbx3") {
|
2018-03-08 04:20:25 -05:00
|
|
|
db->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_AES_KDBX3)));
|
2018-03-01 15:25:29 -05:00
|
|
|
} else if (upgradeAction == "kdf-argon2") {
|
2020-11-20 15:49:56 -05:00
|
|
|
db->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_ARGON2D)));
|
2018-03-01 15:25:29 -05:00
|
|
|
} else if (upgradeAction == "kdf-aes-kdbx4") {
|
2018-03-08 04:20:25 -05:00
|
|
|
db->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_AES_KDBX4)));
|
2018-03-01 15:25:29 -05:00
|
|
|
} else if (upgradeAction == "public-customdata") {
|
|
|
|
db->publicCustomData().insert("abc", "def");
|
|
|
|
} else if (upgradeAction == "rootgroup-customdata") {
|
|
|
|
db->rootGroup()->customData()->set("abc", "def");
|
|
|
|
} else if (upgradeAction == "group-customdata") {
|
|
|
|
auto group = new Group();
|
|
|
|
group->setParent(db->rootGroup());
|
2018-03-22 17:56:05 -04:00
|
|
|
group->setUuid(QUuid::createUuid());
|
2018-03-01 15:25:29 -05:00
|
|
|
group->customData()->set("abc", "def");
|
|
|
|
} else if (upgradeAction == "rootentry-customdata") {
|
|
|
|
auto entry = new Entry();
|
|
|
|
entry->setGroup(db->rootGroup());
|
2018-03-22 17:56:05 -04:00
|
|
|
entry->setUuid(QUuid::createUuid());
|
2018-03-01 15:25:29 -05:00
|
|
|
entry->customData()->set("abc", "def");
|
|
|
|
} else if (upgradeAction == "entry-customdata") {
|
|
|
|
auto group = new Group();
|
|
|
|
group->setParent(db->rootGroup());
|
2018-03-22 17:56:05 -04:00
|
|
|
group->setUuid(QUuid::createUuid());
|
2018-03-01 15:25:29 -05:00
|
|
|
auto entry = new Entry();
|
|
|
|
entry->setGroup(group);
|
2018-03-22 17:56:05 -04:00
|
|
|
entry->setUuid(QUuid::createUuid());
|
2018-03-01 15:25:29 -05:00
|
|
|
entry->customData()->set("abc", "def");
|
|
|
|
} else {
|
|
|
|
QFAIL(qPrintable(QString("Unknown action: %s").arg(upgradeAction)));
|
|
|
|
}
|
|
|
|
|
|
|
|
QBuffer buffer;
|
|
|
|
buffer.open(QBuffer::ReadWrite);
|
|
|
|
KeePass2Writer writer;
|
|
|
|
QVERIFY(writer.writeDatabase(&buffer, db.data()));
|
|
|
|
|
|
|
|
// paranoid check that we cannot decrypt the database without a key
|
|
|
|
buffer.seek(0);
|
|
|
|
KeePass2Reader reader;
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db2 = QSharedPointer<Database>::create();
|
|
|
|
reader.readDatabase(&buffer, QSharedPointer<CompositeKey>::create(), db2.data());
|
2018-03-01 15:25:29 -05:00
|
|
|
QVERIFY(reader.hasError());
|
|
|
|
|
|
|
|
// check that we can read back the database with the original composite key,
|
|
|
|
// i.e., no components have been lost on the way
|
|
|
|
buffer.seek(0);
|
2018-11-22 05:47:31 -05:00
|
|
|
db2 = QSharedPointer<Database>::create();
|
|
|
|
reader.readDatabase(&buffer, compositeKey, db2.data());
|
2018-03-01 15:25:29 -05:00
|
|
|
if (reader.hasError()) {
|
|
|
|
QFAIL(qPrintable(reader.errorString()));
|
|
|
|
}
|
2021-11-19 18:32:09 -05:00
|
|
|
QCOMPARE(reader.version(), expectedVersion);
|
|
|
|
if (expectedVersion >= KeePass2::FILE_VERSION_4) {
|
2020-01-09 20:11:43 -05:00
|
|
|
QVERIFY(db2->kdf()->uuid() != KeePass2::KDF_AES_KDBX3);
|
|
|
|
}
|
2018-03-01 15:25:29 -05:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:46:05 -05:00
|
|
|
void TestKdbx4Format::testUpgradeMasterKeyIntegrity_data()
|
2018-03-01 15:25:29 -05:00
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("upgradeAction");
|
|
|
|
QTest::addColumn<quint32>("expectedVersion");
|
|
|
|
|
2021-11-19 18:32:09 -05:00
|
|
|
QTest::newRow("Upgrade: none") << QString("none") << KeePass2::FILE_VERSION_3_1;
|
|
|
|
QTest::newRow("Upgrade: none (meta-customdata)") << QString("meta-customdata") << KeePass2::FILE_VERSION_3_1;
|
|
|
|
QTest::newRow("Upgrade: none (explicit kdf-aes-kdbx3)") << QString("kdf-aes-kdbx3") << KeePass2::FILE_VERSION_3_1;
|
2018-03-31 16:01:30 -04:00
|
|
|
QTest::newRow("Upgrade (explicit): kdf-argon2") << QString("kdf-argon2") << KeePass2::FILE_VERSION_4;
|
|
|
|
QTest::newRow("Upgrade (explicit): kdf-aes-kdbx4") << QString("kdf-aes-kdbx4") << KeePass2::FILE_VERSION_4;
|
|
|
|
QTest::newRow("Upgrade (implicit): public-customdata") << QString("public-customdata") << KeePass2::FILE_VERSION_4;
|
2018-10-31 23:27:38 -04:00
|
|
|
QTest::newRow("Upgrade (implicit): rootgroup-customdata")
|
|
|
|
<< QString("rootgroup-customdata") << KeePass2::FILE_VERSION_4;
|
2018-03-31 16:01:30 -04:00
|
|
|
QTest::newRow("Upgrade (implicit): group-customdata") << QString("group-customdata") << KeePass2::FILE_VERSION_4;
|
2018-10-31 23:27:38 -04:00
|
|
|
QTest::newRow("Upgrade (implicit): rootentry-customdata")
|
|
|
|
<< QString("rootentry-customdata") << KeePass2::FILE_VERSION_4;
|
2018-03-31 16:01:30 -04:00
|
|
|
QTest::newRow("Upgrade (implicit): entry-customdata") << QString("entry-customdata") << KeePass2::FILE_VERSION_4;
|
2018-03-01 15:25:29 -05:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:46:05 -05:00
|
|
|
void TestKdbx4Format::testCustomData()
|
2018-02-18 13:54:07 -05:00
|
|
|
{
|
|
|
|
Database db;
|
|
|
|
|
|
|
|
// test public custom data
|
|
|
|
QVariantMap publicCustomData;
|
|
|
|
publicCustomData.insert("CD1", 123);
|
|
|
|
publicCustomData.insert("CD2", true);
|
|
|
|
publicCustomData.insert("CD3", "abcäöü");
|
|
|
|
db.setPublicCustomData(publicCustomData);
|
2018-03-01 15:25:29 -05:00
|
|
|
publicCustomData.insert("CD4", QByteArray::fromHex("ababa123ff"));
|
|
|
|
db.publicCustomData().insert("CD4", publicCustomData.value("CD4"));
|
2018-02-18 13:54:07 -05:00
|
|
|
QCOMPARE(db.publicCustomData(), publicCustomData);
|
|
|
|
|
|
|
|
const QString customDataKey1 = "CD1";
|
|
|
|
const QString customDataKey2 = "CD2";
|
|
|
|
const QString customData1 = "abcäöü";
|
|
|
|
const QString customData2 = "Hello World";
|
|
|
|
|
|
|
|
// test custom database data
|
|
|
|
db.metadata()->customData()->set(customDataKey1, customData1);
|
|
|
|
db.metadata()->customData()->set(customDataKey2, customData2);
|
2019-05-01 18:35:08 -04:00
|
|
|
auto lastModified = db.metadata()->customData()->value(CustomData::LastModified);
|
2021-11-10 17:11:03 -05:00
|
|
|
const int dataSize = customDataKey1.toUtf8().size() + customDataKey2.toUtf8().size() + customData1.toUtf8().size()
|
2019-05-01 18:35:08 -04:00
|
|
|
+ customData2.toUtf8().size() + lastModified.toUtf8().size()
|
|
|
|
+ CustomData::LastModified.toUtf8().size();
|
|
|
|
QCOMPARE(db.metadata()->customData()->size(), 3);
|
2018-02-18 13:54:07 -05:00
|
|
|
QCOMPARE(db.metadata()->customData()->dataSize(), dataSize);
|
|
|
|
|
|
|
|
// test custom root group data
|
|
|
|
Group* root = db.rootGroup();
|
|
|
|
root->customData()->set(customDataKey1, customData1);
|
|
|
|
root->customData()->set(customDataKey2, customData2);
|
2019-05-01 18:35:08 -04:00
|
|
|
QCOMPARE(root->customData()->size(), 3);
|
2018-02-18 13:54:07 -05:00
|
|
|
QCOMPARE(root->customData()->dataSize(), dataSize);
|
|
|
|
|
|
|
|
// test copied custom group data
|
|
|
|
auto* group = new Group();
|
|
|
|
group->setParent(root);
|
2018-03-22 17:56:05 -04:00
|
|
|
group->setUuid(QUuid::createUuid());
|
2018-02-18 13:54:07 -05:00
|
|
|
group->customData()->copyDataFrom(root->customData());
|
|
|
|
QCOMPARE(*group->customData(), *root->customData());
|
|
|
|
|
|
|
|
// test copied custom entry data
|
|
|
|
auto* entry = new Entry();
|
|
|
|
entry->setGroup(group);
|
2018-03-22 17:56:05 -04:00
|
|
|
entry->setUuid(QUuid::createUuid());
|
2018-02-18 13:54:07 -05:00
|
|
|
entry->customData()->copyDataFrom(group->customData());
|
|
|
|
QCOMPARE(*entry->customData(), *root->customData());
|
|
|
|
|
|
|
|
// test custom data deletion
|
|
|
|
entry->customData()->set("additional item", "foobar");
|
2019-05-01 18:35:08 -04:00
|
|
|
QCOMPARE(entry->customData()->size(), 4);
|
2018-02-18 13:54:07 -05:00
|
|
|
entry->customData()->remove("additional item");
|
2019-05-01 18:35:08 -04:00
|
|
|
QCOMPARE(entry->customData()->size(), 3);
|
2018-02-18 13:54:07 -05:00
|
|
|
QCOMPARE(entry->customData()->dataSize(), dataSize);
|
|
|
|
|
|
|
|
// test custom data on cloned groups
|
|
|
|
QScopedPointer<Group> clonedGroup(group->clone());
|
|
|
|
QCOMPARE(*clonedGroup->customData(), *group->customData());
|
|
|
|
|
|
|
|
// test custom data on cloned entries
|
|
|
|
QScopedPointer<Entry> clonedEntry(entry->clone(Entry::CloneNoFlags));
|
|
|
|
QCOMPARE(*clonedEntry->customData(), *entry->customData());
|
|
|
|
|
|
|
|
QBuffer buffer;
|
|
|
|
buffer.open(QBuffer::ReadWrite);
|
|
|
|
KeePass2Writer writer;
|
|
|
|
writer.writeDatabase(&buffer, &db);
|
|
|
|
|
|
|
|
// read buffer back
|
|
|
|
buffer.seek(0);
|
|
|
|
KeePass2Reader reader;
|
2018-11-22 05:47:31 -05:00
|
|
|
auto newDb = QSharedPointer<Database>::create();
|
|
|
|
reader.readDatabase(&buffer, QSharedPointer<CompositeKey>::create(), newDb.data());
|
2018-02-18 13:54:07 -05:00
|
|
|
|
|
|
|
// test all custom data are read back successfully from KDBX
|
|
|
|
QCOMPARE(newDb->publicCustomData(), publicCustomData);
|
|
|
|
|
|
|
|
QCOMPARE(newDb->metadata()->customData()->value(customDataKey1), customData1);
|
|
|
|
QCOMPARE(newDb->metadata()->customData()->value(customDataKey2), customData2);
|
|
|
|
|
|
|
|
QCOMPARE(newDb->rootGroup()->customData()->value(customDataKey1), customData1);
|
|
|
|
QCOMPARE(newDb->rootGroup()->customData()->value(customDataKey2), customData2);
|
|
|
|
|
|
|
|
auto* newGroup = newDb->rootGroup()->children()[0];
|
|
|
|
QCOMPARE(newGroup->customData()->value(customDataKey1), customData1);
|
|
|
|
QCOMPARE(newGroup->customData()->value(customDataKey2), customData2);
|
|
|
|
|
|
|
|
auto* newEntry = newDb->rootGroup()->children()[0]->entries()[0];
|
|
|
|
QCOMPARE(newEntry->customData()->value(customDataKey1), customData1);
|
|
|
|
QCOMPARE(newEntry->customData()->value(customDataKey2), customData2);
|
|
|
|
}
|