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"
|
|
|
|
|
2013-10-03 09:18:16 -04:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QTest>
|
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/Database.h"
|
|
|
|
#include "core/Metadata.h"
|
2017-12-26 19:11:46 -05:00
|
|
|
#include "core/Tools.h"
|
2011-11-13 08:52:43 -05:00
|
|
|
#include "crypto/Crypto.h"
|
2017-11-12 07:20:57 -05:00
|
|
|
#include "crypto/kdf/AesKdf.h"
|
2017-12-26 19:11:46 -05:00
|
|
|
#include "crypto/CryptoHash.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"
|
|
|
|
|
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()
|
|
|
|
{
|
2017-12-26 19:11:46 -05:00
|
|
|
QScopedPointer<CompositeKey> compositeKey1(new CompositeKey());
|
|
|
|
QScopedPointer<PasswordKey> passwordKey1(new PasswordKey());
|
|
|
|
QScopedPointer<PasswordKey> passwordKey2(new PasswordKey("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
|
2011-11-13 08:52:43 -05:00
|
|
|
compositeKey1->addKey(*passwordKey1);
|
|
|
|
compositeKey1->addKey(*passwordKey2);
|
|
|
|
|
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
|
|
|
|
|
|
|
// make sure the subkeys are copied
|
2017-12-26 19:11:46 -05:00
|
|
|
QScopedPointer<CompositeKey> compositeKey2(compositeKey1->clone());
|
2017-11-12 07:20:57 -05:00
|
|
|
QByteArray transformed2;
|
|
|
|
QVERIFY(compositeKey2->transform(kdf, transformed2));
|
|
|
|
QCOMPARE(transformed2.size(), 32);
|
|
|
|
QCOMPARE(transformed1, transformed2);
|
2012-01-11 17:59:50 -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()
|
2012-01-11 17:59:50 -05:00
|
|
|
compositeKey3->addKey(PasswordKey("test"));
|
|
|
|
compositeKey3->clear();
|
2012-04-25 12:35:30 -04:00
|
|
|
QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey());
|
2012-01-11 17:59:50 -05:00
|
|
|
|
2012-04-25 12:35:30 -04:00
|
|
|
// test assignment operator
|
|
|
|
compositeKey3->addKey(PasswordKey("123"));
|
|
|
|
*compositeKey4 = *compositeKey3;
|
2012-01-11 17:59:50 -05:00
|
|
|
QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey());
|
|
|
|
|
2012-07-17 04:47:56 -04:00
|
|
|
// test self-assignment
|
|
|
|
*compositeKey3 = *compositeKey3;
|
|
|
|
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);
|
|
|
|
QFETCH(QString, typeString);
|
2011-12-21 17:22:07 -05:00
|
|
|
|
2017-12-27 08:36:21 -05:00
|
|
|
QString name = QString("FileKey").append(typeString);
|
2011-12-21 17:22:07 -05:00
|
|
|
|
|
|
|
KeePass2Reader reader;
|
|
|
|
|
|
|
|
QString dbFilename = QString("%1/%2.kdbx").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
|
|
|
|
QString keyFilename = QString("%1/%2.key").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
|
|
|
|
|
|
|
|
CompositeKey compositeKey;
|
|
|
|
FileKey fileKey;
|
|
|
|
QVERIFY(fileKey.load(keyFilename));
|
|
|
|
QCOMPARE(fileKey.rawKey().size(), 32);
|
2017-12-27 08:36:21 -05:00
|
|
|
|
|
|
|
QCOMPARE(fileKey.type(), type);
|
|
|
|
|
2011-12-21 17:22:07 -05:00
|
|
|
compositeKey.addKey(fileKey);
|
|
|
|
|
2017-12-26 19:11:46 -05:00
|
|
|
QScopedPointer<Database> db(reader.readDatabase(dbFilename, compositeKey));
|
2011-12-21 17:22:07 -05:00
|
|
|
QVERIFY(db);
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeys::testFileKey_data()
|
|
|
|
{
|
2017-12-27 08:36:21 -05:00
|
|
|
QTest::addColumn<FileKey::Type>("type");
|
|
|
|
QTest::addColumn<QString>("typeString");
|
|
|
|
QTest::newRow("Xml") << FileKey::KeePass2XML << QString("Xml");
|
|
|
|
QTest::newRow("XmlBrokenBase64") << FileKey::Hashed << QString("XmlBrokenBase64");
|
|
|
|
QTest::newRow("Binary") << FileKey::FixedBinary << QString("Binary");
|
|
|
|
QTest::newRow("Hex") << FileKey::FixedBinaryHex << QString("Hex");
|
|
|
|
QTest::newRow("Hashed") << FileKey::Hashed << QString("Hashed");
|
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);
|
|
|
|
|
|
|
|
FileKey::create(&keyBuffer1, 128);
|
|
|
|
QCOMPARE(keyBuffer1.size(), 128);
|
|
|
|
|
|
|
|
QBuffer keyBuffer2;
|
|
|
|
keyBuffer2.open(QBuffer::ReadWrite);
|
|
|
|
FileKey::create(&keyBuffer2, 64);
|
|
|
|
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);
|
|
|
|
|
|
|
|
FileKey::create(&keyBuffer);
|
|
|
|
keyBuffer.reset();
|
|
|
|
|
|
|
|
FileKey fileKey;
|
|
|
|
QVERIFY(fileKey.load(&keyBuffer));
|
|
|
|
CompositeKey compositeKey;
|
|
|
|
compositeKey.addKey(fileKey);
|
|
|
|
|
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;
|
2017-12-26 19:11:46 -05:00
|
|
|
QScopedPointer<Database> dbRead(reader.readDatabase(&dbBuffer, compositeKey));
|
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);
|
|
|
|
|
|
|
|
FileKey::create(&keyBuffer);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
PasswordKey pwKey;
|
|
|
|
pwKey.setPassword("password");
|
|
|
|
CompositeKey compositeKey;
|
|
|
|
compositeKey.addKey(pwKey);
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-01-19 09:26:32 -05:00
|
|
|
QBENCHMARK {
|
2017-11-12 07:20:57 -05:00
|
|
|
Q_UNUSED(compositeKey.transform(kdf, result));
|
|
|
|
};
|
2014-01-19 09:26:32 -05:00
|
|
|
}
|