TestKeys.cpp 11 KB

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