2011-11-13 08:52:43 -05:00
|
|
|
/*
|
2017-06-09 17:40:36 -04:00
|
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
2017-12-26 19:11:46 -05:00
|
|
|
* Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
|
2011-11-13 08:52:43 -05:00
|
|
|
*
|
|
|
|
* 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 "TestKeys.h"
|
2018-01-24 07:22:20 -05:00
|
|
|
#include "TestGlobal.h"
|
2011-11-13 08:52:43 -05:00
|
|
|
|
2013-10-03 09:18:16 -04:00
|
|
|
#include <QBuffer>
|
2011-11-13 08:52:43 -05:00
|
|
|
|
2011-12-21 17:22:07 -05:00
|
|
|
#include "config-keepassx-tests.h"
|
2017-12-26 19:11:46 -05:00
|
|
|
|
2011-12-21 17:22:07 -05:00
|
|
|
#include "core/Metadata.h"
|
2011-11-13 08:52:43 -05:00
|
|
|
#include "crypto/Crypto.h"
|
2017-12-26 19:11:46 -05:00
|
|
|
#include "crypto/CryptoHash.h"
|
2018-03-31 16:01:30 -04:00
|
|
|
#include "crypto/kdf/AesKdf.h"
|
2011-12-21 17:22:07 -05:00
|
|
|
#include "format/KeePass2Reader.h"
|
|
|
|
#include "format/KeePass2Writer.h"
|
|
|
|
#include "keys/FileKey.h"
|
2011-11-13 08:52:43 -05:00
|
|
|
#include "keys/PasswordKey.h"
|
2018-03-01 14:07:19 -05:00
|
|
|
#include "mock/MockChallengeResponseKey.h"
|
2011-11-13 08:52:43 -05:00
|
|
|
|
2014-05-16 06:32:52 -04:00
|
|
|
QTEST_GUILESS_MAIN(TestKeys)
|
2017-12-27 08:36:21 -05:00
|
|
|
Q_DECLARE_METATYPE(FileKey::Type);
|
2014-05-16 06:32:52 -04:00
|
|
|
|
2011-11-13 08:52:43 -05:00
|
|
|
void TestKeys::initTestCase()
|
|
|
|
{
|
2014-06-15 05:17:40 -04:00
|
|
|
QVERIFY(Crypto::init());
|
2011-11-13 08:52:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeys::testComposite()
|
|
|
|
{
|
2018-05-13 17:21:43 -04:00
|
|
|
auto compositeKey1 = QSharedPointer<CompositeKey>::create();
|
|
|
|
auto passwordKey1 = QSharedPointer<PasswordKey>::create();
|
|
|
|
auto passwordKey2 = QSharedPointer<PasswordKey>::create("test");
|
2011-11-13 08:52:43 -05:00
|
|
|
|
2012-04-25 12:35:30 -04:00
|
|
|
// make sure that addKey() creates a copy of the keys
|
2018-05-13 17:21:43 -04:00
|
|
|
compositeKey1->addKey(passwordKey1);
|
|
|
|
compositeKey1->addKey(passwordKey2);
|
2011-11-13 08:52:43 -05:00
|
|
|
|
2017-11-12 07:20:57 -05:00
|
|
|
AesKdf kdf;
|
|
|
|
kdf.setRounds(1);
|
|
|
|
QByteArray transformed1;
|
|
|
|
QVERIFY(compositeKey1->transform(kdf, transformed1));
|
|
|
|
QCOMPARE(transformed1.size(), 32);
|
2011-11-13 08:52:43 -05:00
|
|
|
|
2017-12-26 19:11:46 -05:00
|
|
|
QScopedPointer<CompositeKey> compositeKey3(new CompositeKey());
|
|
|
|
QScopedPointer<CompositeKey> compositeKey4(new CompositeKey());
|
2012-01-11 17:59:50 -05:00
|
|
|
|
2012-04-25 12:35:30 -04:00
|
|
|
// test clear()
|
2018-05-13 17:21:43 -04:00
|
|
|
compositeKey3->addKey(QSharedPointer<PasswordKey>::create("test"));
|
2012-01-11 17:59:50 -05:00
|
|
|
compositeKey3->clear();
|
2012-04-25 12:35:30 -04:00
|
|
|
QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey());
|
2011-11-13 08:52:43 -05:00
|
|
|
}
|
|
|
|
|
2011-12-21 17:22:07 -05:00
|
|
|
void TestKeys::testFileKey()
|
|
|
|
{
|
2017-12-27 08:36:21 -05:00
|
|
|
QFETCH(FileKey::Type, type);
|
2020-12-09 19:28:01 -05:00
|
|
|
QFETCH(QString, keyExt);
|
|
|
|
QFETCH(bool, fileKeyOk);
|
2011-12-21 17:22:07 -05:00
|
|
|
|
2020-12-09 19:28:01 -05:00
|
|
|
QString name = QString("FileKey").append(QTest::currentDataTag());
|
2011-12-21 17:22:07 -05:00
|
|
|
|
|
|
|
KeePass2Reader reader;
|
|
|
|
|
|
|
|
QString dbFilename = QString("%1/%2.kdbx").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
|
2020-12-09 19:28:01 -05:00
|
|
|
QString keyFilename = QString("%1/%2.%3").arg(QString(KEEPASSX_TEST_DATA_DIR), name, keyExt);
|
2011-12-21 17:22:07 -05:00
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
auto compositeKey = QSharedPointer<CompositeKey>::create();
|
|
|
|
auto fileKey = QSharedPointer<FileKey>::create();
|
2020-12-09 19:28:01 -05:00
|
|
|
QString error;
|
|
|
|
QVERIFY(fileKey->load(keyFilename, &error) == fileKeyOk);
|
|
|
|
QVERIFY(error.isEmpty() == fileKeyOk);
|
2018-05-13 17:21:43 -04:00
|
|
|
QCOMPARE(fileKey->type(), type);
|
2017-12-27 08:36:21 -05:00
|
|
|
|
2020-12-09 19:28:01 -05:00
|
|
|
// Test for same behaviour on code path without error parameter
|
|
|
|
auto fileKeyNoErrorParam = QSharedPointer<FileKey>::create();
|
|
|
|
QVERIFY(fileKeyNoErrorParam->load(keyFilename) == fileKeyOk);
|
|
|
|
QCOMPARE(fileKeyNoErrorParam->type(), type);
|
|
|
|
|
|
|
|
QCOMPARE(fileKey->rawKey(), fileKeyNoErrorParam->rawKey());
|
|
|
|
|
|
|
|
if (!fileKeyOk) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QCOMPARE(fileKey->rawKey().size(), 32);
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
compositeKey->addKey(fileKey);
|
2011-12-21 17:22:07 -05:00
|
|
|
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db = QSharedPointer<Database>::create();
|
|
|
|
QVERIFY(db->open(dbFilename, compositeKey, nullptr, false));
|
2012-01-06 14:03:13 -05:00
|
|
|
QVERIFY(!reader.hasError());
|
2011-12-21 17:22:07 -05:00
|
|
|
QCOMPARE(db->metadata()->name(), QString("%1 Database").arg(name));
|
|
|
|
}
|
|
|
|
|
2018-02-21 21:27:55 -05:00
|
|
|
// clang-format off
|
2011-12-21 17:22:07 -05:00
|
|
|
void TestKeys::testFileKey_data()
|
|
|
|
{
|
2017-12-27 08:36:21 -05:00
|
|
|
QTest::addColumn<FileKey::Type>("type");
|
2020-12-09 19:28:01 -05:00
|
|
|
QTest::addColumn<QString>("keyExt");
|
|
|
|
QTest::addColumn<bool>("fileKeyOk");
|
|
|
|
QTest::newRow("Xml") << FileKey::KeePass2XML << QString("key") << true;
|
|
|
|
QTest::newRow("XmlBrokenBase64") << FileKey::KeePass2XML << QString("key") << false;
|
|
|
|
QTest::newRow("XmlV2") << FileKey::KeePass2XMLv2 << QString("keyx") << true;
|
|
|
|
QTest::newRow("XmlV2HashFail") << FileKey::KeePass2XMLv2 << QString("keyx") << false;
|
|
|
|
QTest::newRow("XmlV2BrokenHex") << FileKey::KeePass2XMLv2 << QString("keyx") << false;
|
|
|
|
QTest::newRow("Binary") << FileKey::FixedBinary << QString("key") << true;
|
|
|
|
QTest::newRow("Hex") << FileKey::FixedBinaryHex << QString("key") << true;
|
|
|
|
QTest::newRow("Hashed") << FileKey::Hashed << QString("key") << true;
|
2011-12-21 17:22:07 -05:00
|
|
|
}
|
2018-02-21 21:27:55 -05:00
|
|
|
// clang-format on
|
2011-12-21 17:22:07 -05:00
|
|
|
|
|
|
|
void TestKeys::testCreateFileKey()
|
2017-12-26 19:11:46 -05:00
|
|
|
{
|
|
|
|
QBuffer keyBuffer1;
|
|
|
|
keyBuffer1.open(QBuffer::ReadWrite);
|
|
|
|
|
2020-12-09 19:28:01 -05:00
|
|
|
FileKey::createRandom(&keyBuffer1, 128);
|
2017-12-26 19:11:46 -05:00
|
|
|
QCOMPARE(keyBuffer1.size(), 128);
|
|
|
|
|
|
|
|
QBuffer keyBuffer2;
|
|
|
|
keyBuffer2.open(QBuffer::ReadWrite);
|
2020-12-09 19:28:01 -05:00
|
|
|
FileKey::createRandom(&keyBuffer2, 64);
|
2017-12-26 19:11:46 -05:00
|
|
|
QCOMPARE(keyBuffer2.size(), 64);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeys::testCreateAndOpenFileKey()
|
2011-12-21 17:22:07 -05:00
|
|
|
{
|
|
|
|
const QString dbName("testCreateFileKey database");
|
|
|
|
|
|
|
|
QBuffer keyBuffer;
|
|
|
|
keyBuffer.open(QBuffer::ReadWrite);
|
|
|
|
|
2020-12-09 19:28:01 -05:00
|
|
|
FileKey::createRandom(&keyBuffer);
|
2011-12-21 17:22:07 -05:00
|
|
|
keyBuffer.reset();
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
auto fileKey = QSharedPointer<FileKey>::create();
|
|
|
|
QVERIFY(fileKey->load(&keyBuffer));
|
|
|
|
auto compositeKey = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey->addKey(fileKey);
|
2011-12-21 17:22:07 -05:00
|
|
|
|
2017-12-26 19:11:46 -05:00
|
|
|
QScopedPointer<Database> dbOrg(new Database());
|
2015-05-09 13:47:53 -04:00
|
|
|
QVERIFY(dbOrg->setKey(compositeKey));
|
2011-12-21 17:22:07 -05:00
|
|
|
dbOrg->metadata()->setName(dbName);
|
|
|
|
|
|
|
|
QBuffer dbBuffer;
|
|
|
|
dbBuffer.open(QBuffer::ReadWrite);
|
|
|
|
|
|
|
|
KeePass2Writer writer;
|
2017-12-26 19:11:46 -05:00
|
|
|
writer.writeDatabase(&dbBuffer, dbOrg.data());
|
2017-11-12 14:20:37 -05:00
|
|
|
bool writeSuccess = writer.writeDatabase(&dbBuffer, dbOrg.data());
|
|
|
|
if (writer.hasError()) {
|
|
|
|
QFAIL(writer.errorString().toUtf8().constData());
|
|
|
|
}
|
|
|
|
QVERIFY(writeSuccess);
|
2011-12-21 17:22:07 -05:00
|
|
|
dbBuffer.reset();
|
|
|
|
|
|
|
|
KeePass2Reader reader;
|
2018-11-22 05:47:31 -05:00
|
|
|
auto dbRead = QSharedPointer<Database>::create();
|
|
|
|
reader.readDatabase(&dbBuffer, compositeKey, dbRead.data());
|
2017-11-12 14:20:37 -05:00
|
|
|
if (reader.hasError()) {
|
|
|
|
QFAIL(reader.errorString().toUtf8().constData());
|
|
|
|
}
|
2011-12-21 17:22:07 -05:00
|
|
|
QVERIFY(dbRead);
|
|
|
|
QCOMPARE(dbRead->metadata()->name(), dbName);
|
2017-12-26 19:11:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeys::testFileKeyHash()
|
|
|
|
{
|
|
|
|
QBuffer keyBuffer;
|
|
|
|
keyBuffer.open(QBuffer::ReadWrite);
|
|
|
|
|
2020-12-09 19:28:01 -05:00
|
|
|
FileKey::createRandom(&keyBuffer);
|
2017-12-26 19:11:46 -05:00
|
|
|
|
|
|
|
CryptoHash cryptoHash(CryptoHash::Sha256);
|
|
|
|
cryptoHash.addData(keyBuffer.data());
|
|
|
|
|
|
|
|
FileKey fileKey;
|
|
|
|
fileKey.load(&keyBuffer);
|
|
|
|
|
|
|
|
QCOMPARE(fileKey.rawKey(), cryptoHash.result());
|
2011-12-21 17:22:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeys::testFileKeyError()
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
QString errorMsg;
|
|
|
|
const QString fileName(QString(KEEPASSX_TEST_DATA_DIR).append("/does/not/exist"));
|
|
|
|
|
|
|
|
FileKey fileKey;
|
|
|
|
result = fileKey.load(fileName, &errorMsg);
|
|
|
|
QVERIFY(!result);
|
|
|
|
QVERIFY(!errorMsg.isEmpty());
|
|
|
|
errorMsg = "";
|
|
|
|
|
|
|
|
result = FileKey::create(fileName, &errorMsg);
|
|
|
|
QVERIFY(!result);
|
|
|
|
QVERIFY(!errorMsg.isEmpty());
|
|
|
|
errorMsg = "";
|
|
|
|
}
|
|
|
|
|
2014-01-19 09:26:32 -05:00
|
|
|
void TestKeys::benchmarkTransformKey()
|
|
|
|
{
|
|
|
|
QByteArray env = qgetenv("BENCHMARK");
|
|
|
|
|
|
|
|
if (env.isEmpty() || env == "0" || env == "no") {
|
2015-07-22 16:43:33 -04:00
|
|
|
QSKIP("Benchmark skipped. Set env variable BENCHMARK=1 to enable.");
|
2014-01-19 09:26:32 -05:00
|
|
|
}
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
auto pwKey = QSharedPointer<PasswordKey>::create();
|
|
|
|
pwKey->setPassword("password");
|
|
|
|
auto compositeKey = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKey->addKey(pwKey);
|
2014-01-19 09:26:32 -05:00
|
|
|
|
|
|
|
QByteArray seed(32, '\x4B');
|
|
|
|
|
2017-11-12 07:20:57 -05:00
|
|
|
QByteArray result;
|
|
|
|
AesKdf kdf;
|
|
|
|
kdf.setSeed(seed);
|
|
|
|
kdf.setRounds(1e6);
|
2015-05-09 13:47:53 -04:00
|
|
|
|
2018-03-31 16:01:30 -04:00
|
|
|
QBENCHMARK
|
|
|
|
{
|
2018-05-13 17:21:43 -04:00
|
|
|
Q_UNUSED(compositeKey->transform(kdf, result));
|
2017-11-12 07:20:57 -05:00
|
|
|
};
|
2014-01-19 09:26:32 -05:00
|
|
|
}
|
2018-03-01 14:07:19 -05:00
|
|
|
|
|
|
|
void TestKeys::testCompositeKeyComponents()
|
|
|
|
{
|
2018-05-13 17:21:43 -04:00
|
|
|
auto passwordKeyEnc = QSharedPointer<PasswordKey>::create("password");
|
|
|
|
auto fileKeyEnc = QSharedPointer<FileKey>::create();
|
2018-03-01 14:07:19 -05:00
|
|
|
QString error;
|
2018-05-13 17:21:43 -04:00
|
|
|
fileKeyEnc->load(QString("%1/%2").arg(QString(KEEPASSX_TEST_DATA_DIR), "FileKeyHashed.key"), &error);
|
2018-11-01 22:38:55 -04:00
|
|
|
if (!error.isEmpty()) {
|
2018-03-01 14:07:19 -05:00
|
|
|
QFAIL(qPrintable(error));
|
|
|
|
}
|
|
|
|
auto challengeResponseKeyEnc = QSharedPointer<MockChallengeResponseKey>::create(QByteArray(16, 0x10));
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
auto compositeKeyEnc = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKeyEnc->addKey(passwordKeyEnc);
|
|
|
|
compositeKeyEnc->addKey(fileKeyEnc);
|
|
|
|
compositeKeyEnc->addChallengeResponseKey(challengeResponseKeyEnc);
|
2018-03-01 14:07:19 -05:00
|
|
|
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db1 = QSharedPointer<Database>::create();
|
2018-03-01 14:07:19 -05:00
|
|
|
db1->setKey(compositeKeyEnc);
|
|
|
|
|
|
|
|
KeePass2Writer writer;
|
|
|
|
QBuffer buffer;
|
|
|
|
buffer.open(QBuffer::ReadWrite);
|
|
|
|
QVERIFY(writer.writeDatabase(&buffer, db1.data()));
|
|
|
|
|
|
|
|
buffer.seek(0);
|
2018-11-22 05:47:31 -05:00
|
|
|
auto db2 = QSharedPointer<Database>::create();
|
2018-03-01 14:07:19 -05:00
|
|
|
KeePass2Reader reader;
|
2018-05-13 17:21:43 -04:00
|
|
|
auto compositeKeyDec1 = QSharedPointer<CompositeKey>::create();
|
2018-03-01 14:07:19 -05:00
|
|
|
|
|
|
|
// try decryption and subsequently add key components until decryption is successful
|
2018-11-22 05:47:31 -05:00
|
|
|
QVERIFY(!reader.readDatabase(&buffer, compositeKeyDec1, db2.data()));
|
2018-03-01 14:07:19 -05:00
|
|
|
QVERIFY(reader.hasError());
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
compositeKeyDec1->addKey(passwordKeyEnc);
|
2018-03-01 14:07:19 -05:00
|
|
|
buffer.seek(0);
|
2018-11-22 05:47:31 -05:00
|
|
|
QVERIFY(!reader.readDatabase(&buffer, compositeKeyDec1, db2.data()));
|
2018-03-01 14:07:19 -05:00
|
|
|
QVERIFY(reader.hasError());
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
compositeKeyDec1->addKey(fileKeyEnc);
|
2018-03-01 14:07:19 -05:00
|
|
|
buffer.seek(0);
|
2018-11-22 05:47:31 -05:00
|
|
|
QVERIFY(!reader.readDatabase(&buffer, compositeKeyDec1, db2.data()));
|
2018-03-01 14:07:19 -05:00
|
|
|
QVERIFY(reader.hasError());
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
compositeKeyDec1->addChallengeResponseKey(challengeResponseKeyEnc);
|
2018-03-01 14:07:19 -05:00
|
|
|
buffer.seek(0);
|
2018-11-22 05:47:31 -05:00
|
|
|
QVERIFY(reader.readDatabase(&buffer, compositeKeyDec1, db2.data()));
|
2018-03-01 14:07:19 -05:00
|
|
|
// now we should be able to open the database
|
|
|
|
if (reader.hasError()) {
|
|
|
|
QFAIL(qPrintable(reader.errorString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// try the same again, but this time with one wrong key component each time
|
2018-05-13 17:21:43 -04:00
|
|
|
auto compositeKeyDec2 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKeyDec2->addKey(QSharedPointer<PasswordKey>::create("wrong password"));
|
|
|
|
compositeKeyDec2->addKey(fileKeyEnc);
|
|
|
|
compositeKeyDec2->addChallengeResponseKey(challengeResponseKeyEnc);
|
2018-03-01 14:07:19 -05:00
|
|
|
buffer.seek(0);
|
2018-11-22 05:47:31 -05:00
|
|
|
QVERIFY(!reader.readDatabase(&buffer, compositeKeyDec2, db2.data()));
|
2018-03-01 14:07:19 -05:00
|
|
|
QVERIFY(reader.hasError());
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
auto compositeKeyDec3 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKeyDec3->addKey(passwordKeyEnc);
|
|
|
|
auto fileKeyWrong = QSharedPointer<FileKey>::create();
|
|
|
|
fileKeyWrong->load(QString("%1/%2").arg(QString(KEEPASSX_TEST_DATA_DIR), "FileKeyHashed2.key"), &error);
|
2018-11-01 22:38:55 -04:00
|
|
|
if (!error.isEmpty()) {
|
2018-03-01 14:07:19 -05:00
|
|
|
QFAIL(qPrintable(error));
|
|
|
|
}
|
2018-05-13 17:21:43 -04:00
|
|
|
compositeKeyDec3->addKey(fileKeyWrong);
|
|
|
|
compositeKeyDec3->addChallengeResponseKey(challengeResponseKeyEnc);
|
2018-03-01 14:07:19 -05:00
|
|
|
buffer.seek(0);
|
2018-11-22 05:47:31 -05:00
|
|
|
QVERIFY(!reader.readDatabase(&buffer, compositeKeyDec3, db2.data()));
|
2018-03-01 14:07:19 -05:00
|
|
|
QVERIFY(reader.hasError());
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
auto compositeKeyDec4 = QSharedPointer<CompositeKey>::create();
|
|
|
|
compositeKeyDec4->addKey(passwordKeyEnc);
|
|
|
|
compositeKeyDec4->addKey(fileKeyEnc);
|
|
|
|
compositeKeyDec4->addChallengeResponseKey(QSharedPointer<MockChallengeResponseKey>::create(QByteArray(16, 0x20)));
|
2018-03-01 14:07:19 -05:00
|
|
|
buffer.seek(0);
|
2018-11-22 05:47:31 -05:00
|
|
|
QVERIFY(!reader.readDatabase(&buffer, compositeKeyDec4, db2.data()));
|
2018-03-01 14:07:19 -05:00
|
|
|
QVERIFY(reader.hasError());
|
|
|
|
}
|