TestKeys.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
  3. * Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 or (at your option)
  8. * version 3 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "TestKeys.h"
  19. #include "TestGlobal.h"
  20. #include <QBuffer>
  21. #include "config-keepassx-tests.h"
  22. #include "core/Metadata.h"
  23. #include "crypto/Crypto.h"
  24. #include "crypto/CryptoHash.h"
  25. #include "crypto/kdf/AesKdf.h"
  26. #include "format/KeePass2Reader.h"
  27. #include "format/KeePass2Writer.h"
  28. #include "keys/FileKey.h"
  29. #include "keys/PasswordKey.h"
  30. #include "mock/MockChallengeResponseKey.h"
  31. QTEST_GUILESS_MAIN(TestKeys)
  32. Q_DECLARE_METATYPE(FileKey::Type);
  33. void TestKeys::initTestCase()
  34. {
  35. QVERIFY(Crypto::init());
  36. }
  37. void TestKeys::testComposite()
  38. {
  39. auto compositeKey1 = QSharedPointer<CompositeKey>::create();
  40. auto passwordKey1 = QSharedPointer<PasswordKey>::create();
  41. auto passwordKey2 = QSharedPointer<PasswordKey>::create("test");
  42. // make sure that addKey() creates a copy of the keys
  43. compositeKey1->addKey(passwordKey1);
  44. compositeKey1->addKey(passwordKey2);
  45. AesKdf kdf;
  46. kdf.setRounds(1);
  47. QByteArray transformed1;
  48. QVERIFY(compositeKey1->transform(kdf, transformed1));
  49. QCOMPARE(transformed1.size(), 32);
  50. QScopedPointer<CompositeKey> compositeKey3(new CompositeKey());
  51. QScopedPointer<CompositeKey> compositeKey4(new CompositeKey());
  52. // test clear()
  53. compositeKey3->addKey(QSharedPointer<PasswordKey>::create("test"));
  54. compositeKey3->clear();
  55. QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey());
  56. }
  57. void TestKeys::testFileKey()
  58. {
  59. QFETCH(FileKey::Type, type);
  60. QFETCH(QString, keyExt);
  61. QFETCH(bool, fileKeyOk);
  62. QString name = QString("FileKey").append(QTest::currentDataTag());
  63. KeePass2Reader reader;
  64. QString dbFilename = QString("%1/%2.kdbx").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
  65. QString keyFilename = QString("%1/%2.%3").arg(QString(KEEPASSX_TEST_DATA_DIR), name, keyExt);
  66. auto compositeKey = QSharedPointer<CompositeKey>::create();
  67. auto fileKey = QSharedPointer<FileKey>::create();
  68. QString error;
  69. QVERIFY(fileKey->load(keyFilename, &error) == fileKeyOk);
  70. QVERIFY(error.isEmpty() == fileKeyOk);
  71. QCOMPARE(fileKey->type(), type);
  72. // Test for same behaviour on code path without error parameter
  73. auto fileKeyNoErrorParam = QSharedPointer<FileKey>::create();
  74. QVERIFY(fileKeyNoErrorParam->load(keyFilename) == fileKeyOk);
  75. QCOMPARE(fileKeyNoErrorParam->type(), type);
  76. QCOMPARE(fileKey->rawKey(), fileKeyNoErrorParam->rawKey());
  77. if (!fileKeyOk) {
  78. return;
  79. }
  80. QCOMPARE(fileKey->rawKey().size(), 32);
  81. compositeKey->addKey(fileKey);
  82. auto db = QSharedPointer<Database>::create();
  83. QVERIFY(db->open(dbFilename, compositeKey, nullptr, false));
  84. QVERIFY(!reader.hasError());
  85. QCOMPARE(db->metadata()->name(), QString("%1 Database").arg(name));
  86. }
  87. // clang-format off
  88. void TestKeys::testFileKey_data()
  89. {
  90. QTest::addColumn<FileKey::Type>("type");
  91. QTest::addColumn<QString>("keyExt");
  92. QTest::addColumn<bool>("fileKeyOk");
  93. QTest::newRow("Xml") << FileKey::KeePass2XML << QString("key") << true;
  94. QTest::newRow("XmlBrokenBase64") << FileKey::KeePass2XML << QString("key") << false;
  95. QTest::newRow("XmlV2") << FileKey::KeePass2XMLv2 << QString("keyx") << true;
  96. QTest::newRow("XmlV2HashFail") << FileKey::KeePass2XMLv2 << QString("keyx") << false;
  97. QTest::newRow("XmlV2BrokenHex") << FileKey::KeePass2XMLv2 << QString("keyx") << false;
  98. QTest::newRow("Binary") << FileKey::FixedBinary << QString("key") << true;
  99. QTest::newRow("Hex") << FileKey::FixedBinaryHex << QString("key") << true;
  100. QTest::newRow("Hashed") << FileKey::Hashed << QString("key") << true;
  101. }
  102. // clang-format on
  103. void TestKeys::testCreateFileKey()
  104. {
  105. QBuffer keyBuffer1;
  106. keyBuffer1.open(QBuffer::ReadWrite);
  107. FileKey::createRandom(&keyBuffer1, 128);
  108. QCOMPARE(keyBuffer1.size(), 128);
  109. QBuffer keyBuffer2;
  110. keyBuffer2.open(QBuffer::ReadWrite);
  111. FileKey::createRandom(&keyBuffer2, 64);
  112. QCOMPARE(keyBuffer2.size(), 64);
  113. }
  114. void TestKeys::testCreateAndOpenFileKey()
  115. {
  116. const QString dbName("testCreateFileKey database");
  117. QBuffer keyBuffer;
  118. keyBuffer.open(QBuffer::ReadWrite);
  119. FileKey::createRandom(&keyBuffer);
  120. keyBuffer.reset();
  121. auto fileKey = QSharedPointer<FileKey>::create();
  122. QVERIFY(fileKey->load(&keyBuffer));
  123. auto compositeKey = QSharedPointer<CompositeKey>::create();
  124. compositeKey->addKey(fileKey);
  125. QScopedPointer<Database> dbOrg(new Database());
  126. QVERIFY(dbOrg->setKey(compositeKey));
  127. dbOrg->metadata()->setName(dbName);
  128. QBuffer dbBuffer;
  129. dbBuffer.open(QBuffer::ReadWrite);
  130. KeePass2Writer writer;
  131. writer.writeDatabase(&dbBuffer, dbOrg.data());
  132. bool writeSuccess = writer.writeDatabase(&dbBuffer, dbOrg.data());
  133. if (writer.hasError()) {
  134. QFAIL(writer.errorString().toUtf8().constData());
  135. }
  136. QVERIFY(writeSuccess);
  137. dbBuffer.reset();
  138. KeePass2Reader reader;
  139. auto dbRead = QSharedPointer<Database>::create();
  140. reader.readDatabase(&dbBuffer, compositeKey, dbRead.data());
  141. if (reader.hasError()) {
  142. QFAIL(reader.errorString().toUtf8().constData());
  143. }
  144. QVERIFY(dbRead);
  145. QCOMPARE(dbRead->metadata()->name(), dbName);
  146. }
  147. void TestKeys::testFileKeyHash()
  148. {
  149. QBuffer keyBuffer;
  150. keyBuffer.open(QBuffer::ReadWrite);
  151. FileKey::createRandom(&keyBuffer);
  152. CryptoHash cryptoHash(CryptoHash::Sha256);
  153. cryptoHash.addData(keyBuffer.data());
  154. FileKey fileKey;
  155. fileKey.load(&keyBuffer);
  156. QCOMPARE(fileKey.rawKey(), cryptoHash.result());
  157. }
  158. void TestKeys::testFileKeyError()
  159. {
  160. bool result;
  161. QString errorMsg;
  162. const QString fileName(QString(KEEPASSX_TEST_DATA_DIR).append("/does/not/exist"));
  163. FileKey fileKey;
  164. result = fileKey.load(fileName, &errorMsg);
  165. QVERIFY(!result);
  166. QVERIFY(!errorMsg.isEmpty());
  167. errorMsg = "";
  168. result = FileKey::create(fileName, &errorMsg);
  169. QVERIFY(!result);
  170. QVERIFY(!errorMsg.isEmpty());
  171. errorMsg = "";
  172. }
  173. void TestKeys::benchmarkTransformKey()
  174. {
  175. QByteArray env = qgetenv("BENCHMARK");
  176. if (env.isEmpty() || env == "0" || env == "no") {
  177. QSKIP("Benchmark skipped. Set env variable BENCHMARK=1 to enable.");
  178. }
  179. auto pwKey = QSharedPointer<PasswordKey>::create();
  180. pwKey->setPassword("password");
  181. auto compositeKey = QSharedPointer<CompositeKey>::create();
  182. compositeKey->addKey(pwKey);
  183. QByteArray seed(32, '\x4B');
  184. QByteArray result;
  185. AesKdf kdf;
  186. kdf.setSeed(seed);
  187. kdf.setRounds(1e6);
  188. QBENCHMARK
  189. {
  190. Q_UNUSED(compositeKey->transform(kdf, result));
  191. };
  192. }
  193. void TestKeys::testCompositeKeyComponents()
  194. {
  195. auto passwordKeyEnc = QSharedPointer<PasswordKey>::create("password");
  196. auto fileKeyEnc = QSharedPointer<FileKey>::create();
  197. QString error;
  198. fileKeyEnc->load(QString("%1/%2").arg(QString(KEEPASSX_TEST_DATA_DIR), "FileKeyHashed.key"), &error);
  199. if (!error.isEmpty()) {
  200. QFAIL(qPrintable(error));
  201. }
  202. auto challengeResponseKeyEnc = QSharedPointer<MockChallengeResponseKey>::create(QByteArray(16, 0x10));
  203. auto compositeKeyEnc = QSharedPointer<CompositeKey>::create();
  204. compositeKeyEnc->addKey(passwordKeyEnc);
  205. compositeKeyEnc->addKey(fileKeyEnc);
  206. compositeKeyEnc->addChallengeResponseKey(challengeResponseKeyEnc);
  207. auto db1 = QSharedPointer<Database>::create();
  208. db1->setKey(compositeKeyEnc);
  209. KeePass2Writer writer;
  210. QBuffer buffer;
  211. buffer.open(QBuffer::ReadWrite);
  212. QVERIFY(writer.writeDatabase(&buffer, db1.data()));
  213. buffer.seek(0);
  214. auto db2 = QSharedPointer<Database>::create();
  215. KeePass2Reader reader;
  216. auto compositeKeyDec1 = QSharedPointer<CompositeKey>::create();
  217. // try decryption and subsequently add key components until decryption is successful
  218. QVERIFY(!reader.readDatabase(&buffer, compositeKeyDec1, db2.data()));
  219. QVERIFY(reader.hasError());
  220. compositeKeyDec1->addKey(passwordKeyEnc);
  221. buffer.seek(0);
  222. QVERIFY(!reader.readDatabase(&buffer, compositeKeyDec1, db2.data()));
  223. QVERIFY(reader.hasError());
  224. compositeKeyDec1->addKey(fileKeyEnc);
  225. buffer.seek(0);
  226. QVERIFY(!reader.readDatabase(&buffer, compositeKeyDec1, db2.data()));
  227. QVERIFY(reader.hasError());
  228. compositeKeyDec1->addChallengeResponseKey(challengeResponseKeyEnc);
  229. buffer.seek(0);
  230. QVERIFY(reader.readDatabase(&buffer, compositeKeyDec1, db2.data()));
  231. // now we should be able to open the database
  232. if (reader.hasError()) {
  233. QFAIL(qPrintable(reader.errorString()));
  234. }
  235. // try the same again, but this time with one wrong key component each time
  236. auto compositeKeyDec2 = QSharedPointer<CompositeKey>::create();
  237. compositeKeyDec2->addKey(QSharedPointer<PasswordKey>::create("wrong password"));
  238. compositeKeyDec2->addKey(fileKeyEnc);
  239. compositeKeyDec2->addChallengeResponseKey(challengeResponseKeyEnc);
  240. buffer.seek(0);
  241. QVERIFY(!reader.readDatabase(&buffer, compositeKeyDec2, db2.data()));
  242. QVERIFY(reader.hasError());
  243. auto compositeKeyDec3 = QSharedPointer<CompositeKey>::create();
  244. compositeKeyDec3->addKey(passwordKeyEnc);
  245. auto fileKeyWrong = QSharedPointer<FileKey>::create();
  246. fileKeyWrong->load(QString("%1/%2").arg(QString(KEEPASSX_TEST_DATA_DIR), "FileKeyHashed2.key"), &error);
  247. if (!error.isEmpty()) {
  248. QFAIL(qPrintable(error));
  249. }
  250. compositeKeyDec3->addKey(fileKeyWrong);
  251. compositeKeyDec3->addChallengeResponseKey(challengeResponseKeyEnc);
  252. buffer.seek(0);
  253. QVERIFY(!reader.readDatabase(&buffer, compositeKeyDec3, db2.data()));
  254. QVERIFY(reader.hasError());
  255. auto compositeKeyDec4 = QSharedPointer<CompositeKey>::create();
  256. compositeKeyDec4->addKey(passwordKeyEnc);
  257. compositeKeyDec4->addKey(fileKeyEnc);
  258. compositeKeyDec4->addChallengeResponseKey(QSharedPointer<MockChallengeResponseKey>::create(QByteArray(16, 0x20)));
  259. buffer.seek(0);
  260. QVERIFY(!reader.readDatabase(&buffer, compositeKeyDec4, db2.data()));
  261. QVERIFY(reader.hasError());
  262. }