TestKdbx4.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "TestKdbx4.h"
  18. #include "config-keepassx-tests.h"
  19. #include "core/Metadata.h"
  20. #include "format/KdbxXmlReader.h"
  21. #include "format/KdbxXmlWriter.h"
  22. #include "format/KeePass2.h"
  23. #include "format/KeePass2Reader.h"
  24. #include "format/KeePass2Writer.h"
  25. #ifdef WITH_XC_KEESHARE
  26. #include "keeshare/KeeShare.h"
  27. #include "keeshare/KeeShareSettings.h"
  28. #endif
  29. #include "keys/FileKey.h"
  30. #include "keys/PasswordKey.h"
  31. #include "mock/MockChallengeResponseKey.h"
  32. #include "mock/MockClock.h"
  33. #include <QTest>
  34. int main(int argc, char* argv[])
  35. {
  36. QCoreApplication app(argc, argv);
  37. QCoreApplication::setAttribute(Qt::AA_Use96Dpi, true);
  38. QTEST_SET_MAIN_SOURCE_PATH
  39. TestKdbx4Argon2 argon2Test;
  40. TestKdbx4AesKdf aesKdfTest;
  41. TestKdbx4Format kdbx4Test;
  42. return QTest::qExec(&argon2Test, argc, argv) || QTest::qExec(&aesKdfTest, argc, argv)
  43. || QTest::qExec(&kdbx4Test, argc, argv);
  44. }
  45. void TestKdbx4Argon2::initTestCaseImpl()
  46. {
  47. m_xmlDb->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_ARGON2D)));
  48. m_kdbxSourceDb->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_ARGON2D)));
  49. }
  50. QSharedPointer<Database>
  51. TestKdbx4Argon2::readXml(const QString& path, bool strictMode, bool& hasError, QString& errorString)
  52. {
  53. KdbxXmlReader reader(KeePass2::FILE_VERSION_4);
  54. reader.setStrictMode(strictMode);
  55. auto db = reader.readDatabase(path);
  56. hasError = reader.hasError();
  57. errorString = reader.errorString();
  58. return db;
  59. }
  60. QSharedPointer<Database> TestKdbx4Argon2::readXml(QBuffer* buf, bool strictMode, bool& hasError, QString& errorString)
  61. {
  62. KdbxXmlReader reader(KeePass2::FILE_VERSION_4);
  63. reader.setStrictMode(strictMode);
  64. auto db = reader.readDatabase(buf);
  65. hasError = reader.hasError();
  66. errorString = reader.errorString();
  67. return db;
  68. }
  69. void TestKdbx4Argon2::writeXml(QBuffer* buf, Database* db, bool& hasError, QString& errorString)
  70. {
  71. KdbxXmlWriter writer(KeePass2::FILE_VERSION_4, {});
  72. writer.writeDatabase(buf, db);
  73. hasError = writer.hasError();
  74. errorString = writer.errorString();
  75. }
  76. void TestKdbx4Argon2::readKdbx(QIODevice* device,
  77. QSharedPointer<const CompositeKey> key,
  78. QSharedPointer<Database> db,
  79. bool& hasError,
  80. QString& errorString)
  81. {
  82. KeePass2Reader reader;
  83. reader.readDatabase(device, key, db.data());
  84. hasError = reader.hasError();
  85. if (hasError) {
  86. errorString = reader.errorString();
  87. }
  88. QCOMPARE(reader.version(), KeePass2::FILE_VERSION_4);
  89. }
  90. void TestKdbx4Argon2::writeKdbx(QIODevice* device, Database* db, bool& hasError, QString& errorString)
  91. {
  92. if (db->kdf()->uuid() == KeePass2::KDF_AES_KDBX3) {
  93. db->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_ARGON2D)));
  94. }
  95. KeePass2Writer writer;
  96. hasError = writer.writeDatabase(device, db);
  97. hasError = writer.hasError();
  98. if (hasError) {
  99. errorString = writer.errorString();
  100. }
  101. QCOMPARE(writer.version(), KeePass2::FILE_VERSION_4);
  102. }
  103. void TestKdbx4AesKdf::initTestCaseImpl()
  104. {
  105. m_xmlDb->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_AES_KDBX4)));
  106. m_kdbxSourceDb->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_AES_KDBX4)));
  107. }
  108. Q_DECLARE_METATYPE(QUuid)
  109. void TestKdbx4Format::init()
  110. {
  111. MockClock::setup(new MockClock());
  112. }
  113. void TestKdbx4Format::cleanup()
  114. {
  115. MockClock::teardown();
  116. }
  117. void TestKdbx4Format::testFormat400()
  118. {
  119. QString filename = QString(KEEPASSX_TEST_DATA_DIR).append("/Format400.kdbx");
  120. auto key = QSharedPointer<CompositeKey>::create();
  121. key->addKey(QSharedPointer<PasswordKey>::create("t"));
  122. KeePass2Reader reader;
  123. auto db = QSharedPointer<Database>::create();
  124. reader.readDatabase(filename, key, db.data());
  125. QCOMPARE(reader.version(), KeePass2::FILE_VERSION_4);
  126. QVERIFY(db.data());
  127. QVERIFY(!reader.hasError());
  128. QCOMPARE(db->rootGroup()->name(), QString("Format400"));
  129. QCOMPARE(db->metadata()->name(), QString("Format400"));
  130. QCOMPARE(db->rootGroup()->entries().size(), 1);
  131. auto entry = db->rootGroup()->entries().at(0);
  132. QCOMPARE(entry->title(), QString("Format400"));
  133. QCOMPARE(entry->username(), QString("Format400"));
  134. QCOMPARE(entry->attributes()->keys().size(), 6);
  135. QCOMPARE(entry->attributes()->value("Format400"), QString("Format400"));
  136. QCOMPARE(entry->attachments()->keys().size(), 1);
  137. QCOMPARE(entry->attachments()->value("Format400"), QByteArray("Format400\n"));
  138. }
  139. void TestKdbx4Format::testFormat400Upgrade()
  140. {
  141. QFETCH(QUuid, kdfUuid);
  142. QFETCH(QUuid, cipherUuid);
  143. QFETCH(bool, addCustomData);
  144. QFETCH(quint32, expectedVersion);
  145. QScopedPointer<Database> sourceDb(new Database());
  146. sourceDb->changeKdf(fastKdf(sourceDb->kdf()));
  147. sourceDb->metadata()->setName("Wubba lubba dub dub");
  148. QCOMPARE(sourceDb->kdf()->uuid(), KeePass2::KDF_AES_KDBX3); // default is legacy AES-KDF
  149. auto key = QSharedPointer<CompositeKey>::create();
  150. key->addKey(QSharedPointer<PasswordKey>::create("I am in great pain, please help me!"));
  151. sourceDb->setKey(key, true, true);
  152. QBuffer buffer;
  153. buffer.open(QBuffer::ReadWrite);
  154. // upgrade to KDBX 4 by changing KDF and Cipher
  155. sourceDb->changeKdf(fastKdf(KeePass2::uuidToKdf(kdfUuid)));
  156. sourceDb->setCipher(cipherUuid);
  157. // CustomData in meta should not cause any version change
  158. sourceDb->metadata()->customData()->set("CustomPublicData", "Hey look, I turned myself into a pickle!");
  159. if (addCustomData) {
  160. // this, however, should
  161. sourceDb->rootGroup()->customData()->set("CustomGroupData",
  162. "I just killed my family! I don't care who they were!");
  163. }
  164. KeePass2Writer writer;
  165. writer.writeDatabase(&buffer, sourceDb.data());
  166. if (writer.hasError()) {
  167. QFAIL(qPrintable(QString("Error while writing database: %1").arg(writer.errorString())));
  168. }
  169. // read buffer back
  170. buffer.seek(0);
  171. KeePass2Reader reader;
  172. auto targetDb = QSharedPointer<Database>::create();
  173. reader.readDatabase(&buffer, key, targetDb.data());
  174. if (reader.hasError()) {
  175. QFAIL(qPrintable(QString("Error while reading database: %1").arg(reader.errorString())));
  176. }
  177. QVERIFY(targetDb->rootGroup());
  178. QCOMPARE(targetDb->metadata()->name(), sourceDb->metadata()->name());
  179. QCOMPARE(reader.version(), expectedVersion);
  180. QCOMPARE(targetDb->cipher(), cipherUuid);
  181. QCOMPARE(targetDb->metadata()->customData()->value("CustomPublicData"),
  182. sourceDb->metadata()->customData()->value("CustomPublicData"));
  183. QCOMPARE(targetDb->rootGroup()->customData()->value("CustomGroupData"),
  184. sourceDb->rootGroup()->customData()->value("CustomGroupData"));
  185. }
  186. // clang-format off
  187. void TestKdbx4Format::testFormat400Upgrade_data()
  188. {
  189. QTest::addColumn<QUuid>("kdfUuid");
  190. QTest::addColumn<QUuid>("cipherUuid");
  191. QTest::addColumn<bool>("addCustomData");
  192. QTest::addColumn<quint32>("expectedVersion");
  193. auto constexpr kdbx3 = KeePass2::FILE_VERSION_3_1;
  194. auto constexpr kdbx4 = KeePass2::FILE_VERSION_4;
  195. QTest::newRow("Argon2d + AES") << KeePass2::KDF_ARGON2D << KeePass2::CIPHER_AES256 << false << kdbx4;
  196. QTest::newRow("Argon2id + AES") << KeePass2::KDF_ARGON2ID << KeePass2::CIPHER_AES256 << false << kdbx4;
  197. QTest::newRow("AES-KDF + AES") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_AES256 << false << kdbx4;
  198. QTest::newRow("AES-KDF (legacy) + AES") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_AES256 << false << kdbx3;
  199. QTest::newRow("Argon2d + AES + CustomData") << KeePass2::KDF_ARGON2D << KeePass2::CIPHER_AES256 << true << kdbx4;
  200. QTest::newRow("Argon2id + AES + CustomData") << KeePass2::KDF_ARGON2ID << KeePass2::CIPHER_AES256 << true << kdbx4;
  201. QTest::newRow("AES-KDF + AES + CustomData") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_AES256 << true << kdbx4;
  202. QTest::newRow("AES-KDF (legacy) + AES + CustomData") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_AES256 << true << kdbx4;
  203. QTest::newRow("Argon2d + ChaCha20") << KeePass2::KDF_ARGON2D << KeePass2::CIPHER_CHACHA20 << false << kdbx4;
  204. QTest::newRow("Argon2id + ChaCha20") << KeePass2::KDF_ARGON2ID << KeePass2::CIPHER_CHACHA20 << false << kdbx4;
  205. QTest::newRow("AES-KDF + ChaCha20") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_CHACHA20 << false << kdbx4;
  206. QTest::newRow("AES-KDF (legacy) + ChaCha20") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_CHACHA20 << false << kdbx3;
  207. QTest::newRow("Argon2d + ChaCha20 + CustomData") << KeePass2::KDF_ARGON2D << KeePass2::CIPHER_CHACHA20 << true << kdbx4;
  208. QTest::newRow("Argon2id + ChaCha20 + CustomData") << KeePass2::KDF_ARGON2ID << KeePass2::CIPHER_CHACHA20 << true << kdbx4;
  209. QTest::newRow("AES-KDF + ChaCha20 + CustomData") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_CHACHA20 << true << kdbx4;
  210. QTest::newRow("AES-KDF (legacy) + ChaCha20 + CustomData") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_CHACHA20 << true << kdbx4;
  211. QTest::newRow("Argon2d + Twofish") << KeePass2::KDF_ARGON2D << KeePass2::CIPHER_TWOFISH << false << kdbx4;
  212. QTest::newRow("Argon2id + Twofish") << KeePass2::KDF_ARGON2ID << KeePass2::CIPHER_TWOFISH << false << kdbx4;
  213. QTest::newRow("AES-KDF + Twofish") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_TWOFISH << false << kdbx4;
  214. QTest::newRow("AES-KDF (legacy) + Twofish") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_TWOFISH << false << kdbx3;
  215. QTest::newRow("Argon2d + Twofish + CustomData") << KeePass2::KDF_ARGON2D << KeePass2::CIPHER_TWOFISH << true << kdbx4;
  216. QTest::newRow("Argon2id + Twofish + CustomData") << KeePass2::KDF_ARGON2ID << KeePass2::CIPHER_TWOFISH << true << kdbx4;
  217. QTest::newRow("AES-KDF + Twofish + CustomData") << KeePass2::KDF_AES_KDBX4 << KeePass2::CIPHER_TWOFISH << true << kdbx4;
  218. QTest::newRow("AES-KDF (legacy) + Twofish + CustomData") << KeePass2::KDF_AES_KDBX3 << KeePass2::CIPHER_TWOFISH << true << kdbx4;
  219. }
  220. // clang-format on
  221. void TestKdbx4Format::testFormat410Upgrade()
  222. {
  223. Database db;
  224. db.changeKdf(fastKdf(db.kdf()));
  225. QCOMPARE(db.kdf()->uuid(), KeePass2::KDF_AES_KDBX3);
  226. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
  227. auto group1 = new Group();
  228. group1->setUuid(QUuid::createUuid());
  229. group1->setParent(db.rootGroup());
  230. auto group2 = new Group();
  231. group2->setUuid(QUuid::createUuid());
  232. group2->setParent(db.rootGroup());
  233. auto entry = new Entry();
  234. entry->setUuid(QUuid::createUuid());
  235. entry->setGroup(group1);
  236. // Groups with tags
  237. group1->setTags("tag");
  238. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_4_1);
  239. group1->setTags("");
  240. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
  241. // PasswordQuality flag set
  242. entry->setExcludeFromReports(true);
  243. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_4_1);
  244. entry->setExcludeFromReports(false);
  245. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
  246. // Previous parent group set on group
  247. group1->setPreviousParentGroup(group2);
  248. QCOMPARE(group1->previousParentGroup(), group2);
  249. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_4_1);
  250. group1->setPreviousParentGroup(nullptr);
  251. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
  252. // Previous parent group set on entry
  253. entry->setPreviousParentGroup(group2);
  254. QCOMPARE(entry->previousParentGroup(), group2);
  255. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_4_1);
  256. entry->setPreviousParentGroup(nullptr);
  257. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
  258. // Custom icons with name or modification date
  259. Metadata::CustomIconData customIcon;
  260. auto iconUuid = QUuid::createUuid();
  261. db.metadata()->addCustomIcon(iconUuid, customIcon);
  262. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
  263. customIcon.name = "abc";
  264. db.metadata()->removeCustomIcon(iconUuid);
  265. db.metadata()->addCustomIcon(iconUuid, customIcon);
  266. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_4_1);
  267. customIcon.name.clear();
  268. customIcon.lastModified = Clock::currentDateTimeUtc();
  269. db.metadata()->removeCustomIcon(iconUuid);
  270. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_3_1);
  271. db.metadata()->addCustomIcon(iconUuid, customIcon);
  272. QCOMPARE(KeePass2Writer::kdbxVersionRequired(&db), KeePass2::FILE_VERSION_4_1);
  273. }
  274. void TestKdbx4Format::testUpgradeMasterKeyIntegrity()
  275. {
  276. QFETCH(QString, upgradeAction);
  277. QFETCH(quint32, expectedVersion);
  278. // prepare composite key
  279. auto passwordKey = QSharedPointer<PasswordKey>::create("turXpGMQiUE6CkPvWacydAKsnp4cxz");
  280. QByteArray fileKeyBytes("Ma6hHov98FbPeyAL22XhcgmpJk8xjQ");
  281. QBuffer fileKeyBuffer(&fileKeyBytes);
  282. fileKeyBuffer.open(QBuffer::ReadOnly);
  283. auto fileKey = QSharedPointer<FileKey>::create();
  284. fileKey->load(&fileKeyBuffer);
  285. auto crKey = QSharedPointer<MockChallengeResponseKey>::create(QByteArray("azdJnbVCFE76vV6t9RJ2DS6xvSS93k"));
  286. auto compositeKey = QSharedPointer<CompositeKey>::create();
  287. compositeKey->addKey(passwordKey);
  288. compositeKey->addKey(fileKey);
  289. compositeKey->addChallengeResponseKey(crKey);
  290. QScopedPointer<Database> db(new Database());
  291. db->changeKdf(fastKdf(db->kdf()));
  292. QCOMPARE(db->kdf()->uuid(), KeePass2::KDF_AES_KDBX3); // default is legacy AES-KDF
  293. db->setKey(compositeKey);
  294. // upgrade the database by a specific method
  295. if (upgradeAction == "none") {
  296. // do nothing
  297. } else if (upgradeAction == "meta-customdata") {
  298. db->metadata()->customData()->set("abc", "def");
  299. } else if (upgradeAction == "kdf-aes-kdbx3") {
  300. db->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_AES_KDBX3)));
  301. } else if (upgradeAction == "kdf-argon2") {
  302. db->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_ARGON2D)));
  303. } else if (upgradeAction == "kdf-aes-kdbx4") {
  304. db->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_AES_KDBX4)));
  305. } else if (upgradeAction == "public-customdata") {
  306. db->publicCustomData().insert("abc", "def");
  307. } else if (upgradeAction == "rootgroup-customdata") {
  308. db->rootGroup()->customData()->set("abc", "def");
  309. } else if (upgradeAction == "group-customdata") {
  310. auto group = new Group();
  311. group->setParent(db->rootGroup());
  312. group->setUuid(QUuid::createUuid());
  313. group->customData()->set("abc", "def");
  314. } else if (upgradeAction == "rootentry-customdata") {
  315. auto entry = new Entry();
  316. entry->setGroup(db->rootGroup());
  317. entry->setUuid(QUuid::createUuid());
  318. entry->customData()->set("abc", "def");
  319. } else if (upgradeAction == "entry-customdata") {
  320. auto group = new Group();
  321. group->setParent(db->rootGroup());
  322. group->setUuid(QUuid::createUuid());
  323. auto entry = new Entry();
  324. entry->setGroup(group);
  325. entry->setUuid(QUuid::createUuid());
  326. entry->customData()->set("abc", "def");
  327. } else {
  328. QFAIL(qPrintable(QString("Unknown action: %s").arg(upgradeAction)));
  329. }
  330. QBuffer buffer;
  331. buffer.open(QBuffer::ReadWrite);
  332. KeePass2Writer writer;
  333. QVERIFY(writer.writeDatabase(&buffer, db.data()));
  334. // paranoid check that we cannot decrypt the database without a key
  335. buffer.seek(0);
  336. KeePass2Reader reader;
  337. auto db2 = QSharedPointer<Database>::create();
  338. reader.readDatabase(&buffer, QSharedPointer<CompositeKey>::create(), db2.data());
  339. QVERIFY(reader.hasError());
  340. // check that we can read back the database with the original composite key,
  341. // i.e., no components have been lost on the way
  342. buffer.seek(0);
  343. db2 = QSharedPointer<Database>::create();
  344. reader.readDatabase(&buffer, compositeKey, db2.data());
  345. if (reader.hasError()) {
  346. QFAIL(qPrintable(reader.errorString()));
  347. }
  348. QCOMPARE(reader.version(), expectedVersion);
  349. if (expectedVersion >= KeePass2::FILE_VERSION_4) {
  350. QVERIFY(db2->kdf()->uuid() != KeePass2::KDF_AES_KDBX3);
  351. }
  352. }
  353. void TestKdbx4Format::testUpgradeMasterKeyIntegrity_data()
  354. {
  355. QTest::addColumn<QString>("upgradeAction");
  356. QTest::addColumn<quint32>("expectedVersion");
  357. QTest::newRow("Upgrade: none") << QString("none") << KeePass2::FILE_VERSION_3_1;
  358. QTest::newRow("Upgrade: none (meta-customdata)") << QString("meta-customdata") << KeePass2::FILE_VERSION_3_1;
  359. QTest::newRow("Upgrade: none (explicit kdf-aes-kdbx3)") << QString("kdf-aes-kdbx3") << KeePass2::FILE_VERSION_3_1;
  360. QTest::newRow("Upgrade (explicit): kdf-argon2") << QString("kdf-argon2") << KeePass2::FILE_VERSION_4;
  361. QTest::newRow("Upgrade (explicit): kdf-aes-kdbx4") << QString("kdf-aes-kdbx4") << KeePass2::FILE_VERSION_4;
  362. QTest::newRow("Upgrade (implicit): public-customdata") << QString("public-customdata") << KeePass2::FILE_VERSION_4;
  363. QTest::newRow("Upgrade (implicit): rootgroup-customdata")
  364. << QString("rootgroup-customdata") << KeePass2::FILE_VERSION_4;
  365. QTest::newRow("Upgrade (implicit): group-customdata") << QString("group-customdata") << KeePass2::FILE_VERSION_4;
  366. QTest::newRow("Upgrade (implicit): rootentry-customdata")
  367. << QString("rootentry-customdata") << KeePass2::FILE_VERSION_4;
  368. QTest::newRow("Upgrade (implicit): entry-customdata") << QString("entry-customdata") << KeePass2::FILE_VERSION_4;
  369. }
  370. void TestKdbx4Format::testAttachmentIndexStability()
  371. {
  372. QScopedPointer<Database> db(new Database());
  373. db->changeKdf(fastKdf(KeePass2::uuidToKdf(KeePass2::KDF_ARGON2ID)));
  374. auto compositeKey = QSharedPointer<CompositeKey>::create();
  375. db->setKey(compositeKey);
  376. QVERIFY(!db->uuid().isNull());
  377. auto root = db->rootGroup();
  378. auto group1 = new Group();
  379. group1->setUuid(QUuid::createUuid());
  380. QVERIFY(!group1->uuid().isNull());
  381. group1->setParent(root);
  382. // Simulate KeeShare group, which uses its own attachment namespace
  383. auto group2 = new Group();
  384. group2->setUuid(QUuid::createUuid());
  385. QVERIFY(!group2->uuid().isNull());
  386. group2->setParent(group1);
  387. #ifdef WITH_XC_KEESHARE
  388. KeeShareSettings::Reference ref;
  389. ref.type = KeeShareSettings::SynchronizeWith;
  390. ref.path = "123";
  391. KeeShare::setReferenceTo(group2, ref);
  392. QVERIFY(KeeShare::isShared(group2));
  393. #endif
  394. auto attachment1 = QByteArray("qwerty");
  395. auto attachment2 = QByteArray("asdf");
  396. auto attachment3 = QByteArray("zxcv");
  397. auto entry1 = new Entry();
  398. entry1->setUuid(QUuid::createUuid());
  399. QVERIFY(!entry1->uuid().isNull());
  400. auto uuid1 = entry1->uuid();
  401. entry1->attachments()->set("a", attachment1);
  402. QCOMPARE(entry1->attachments()->keys().size(), 1);
  403. QCOMPARE(entry1->attachments()->values().size(), 1);
  404. entry1->setGroup(root);
  405. auto entry2 = new Entry();
  406. entry2->setUuid(QUuid::createUuid());
  407. QVERIFY(!entry2->uuid().isNull());
  408. auto uuid2 = entry2->uuid();
  409. entry2->attachments()->set("a", attachment1);
  410. entry2->attachments()->set("b", attachment2);
  411. QCOMPARE(entry2->attachments()->keys().size(), 2);
  412. QCOMPARE(entry2->attachments()->values().size(), 2);
  413. entry2->setGroup(group1);
  414. auto entry3 = new Entry();
  415. entry3->setUuid(QUuid::createUuid());
  416. QVERIFY(!entry3->uuid().isNull());
  417. auto uuid3 = entry3->uuid();
  418. entry3->attachments()->set("a", attachment1);
  419. entry3->attachments()->set("b", attachment2);
  420. entry3->attachments()->set("x", attachment3);
  421. entry3->attachments()->set("y", attachment3);
  422. QCOMPARE(entry3->attachments()->keys().size(), 4);
  423. QCOMPARE(entry3->attachments()->values().size(), 3);
  424. entry3->setGroup(group2);
  425. QBuffer buffer;
  426. buffer.open(QBuffer::ReadWrite);
  427. KeePass2Writer writer;
  428. QVERIFY(writer.writeDatabase(&buffer, db.data()));
  429. QVERIFY(writer.version() >= KeePass2::FILE_VERSION_4);
  430. buffer.seek(0);
  431. KeePass2Reader reader;
  432. // Re-read database and check that all attachments are still correctly assigned
  433. auto db2 = QSharedPointer<Database>::create();
  434. reader.readDatabase(&buffer, QSharedPointer<CompositeKey>::create(), db2.data());
  435. QVERIFY(!reader.hasError());
  436. QVERIFY(!db->uuid().isNull());
  437. auto a1 = db2->rootGroup()->findEntryByUuid(uuid1)->attachments();
  438. QCOMPARE(a1->keys().size(), 1);
  439. QCOMPARE(a1->values().size(), 1);
  440. QCOMPARE(a1->value("a"), attachment1);
  441. auto a2 = db2->rootGroup()->findEntryByUuid(uuid2)->attachments();
  442. QCOMPARE(a2->keys().size(), 2);
  443. QCOMPARE(a2->values().size(), 2);
  444. QCOMPARE(a2->value("a"), attachment1);
  445. QCOMPARE(a2->value("b"), attachment2);
  446. #ifdef WITH_XC_KEESHARE
  447. QVERIFY(KeeShare::isShared(db2->rootGroup()->findEntryByUuid(uuid3)->group()));
  448. #endif
  449. auto a3 = db2->rootGroup()->findEntryByUuid(uuid3)->attachments();
  450. QCOMPARE(a3->keys().size(), 4);
  451. QCOMPARE(a3->values().size(), 3);
  452. QCOMPARE(a3->value("a"), attachment1);
  453. QCOMPARE(a3->value("b"), attachment2);
  454. QCOMPARE(a3->value("x"), attachment3);
  455. QCOMPARE(a3->value("y"), attachment3);
  456. }
  457. void TestKdbx4Format::testCustomData()
  458. {
  459. Database db;
  460. // test public custom data
  461. QVariantMap publicCustomData;
  462. publicCustomData.insert("CD1", 123);
  463. publicCustomData.insert("CD2", true);
  464. publicCustomData.insert("CD3", "abcäöü");
  465. db.setPublicCustomData(publicCustomData);
  466. publicCustomData.insert("CD4", QByteArray::fromHex("ababa123ff"));
  467. db.publicCustomData().insert("CD4", publicCustomData.value("CD4"));
  468. QCOMPARE(db.publicCustomData(), publicCustomData);
  469. const QString customDataKey1 = "CD1";
  470. const QString customDataKey2 = "CD2";
  471. const QString customData1 = "abcäöü";
  472. const QString customData2 = "Hello World";
  473. // test custom database data
  474. db.metadata()->customData()->set(customDataKey1, customData1);
  475. db.metadata()->customData()->set(customDataKey2, customData2);
  476. auto lastModified = db.metadata()->customData()->value(CustomData::LastModified);
  477. const int dataSize = customDataKey1.toUtf8().size() + customDataKey2.toUtf8().size() + customData1.toUtf8().size()
  478. + customData2.toUtf8().size() + lastModified.toUtf8().size()
  479. + CustomData::LastModified.toUtf8().size();
  480. QCOMPARE(db.metadata()->customData()->size(), 3);
  481. QCOMPARE(db.metadata()->customData()->dataSize(), dataSize);
  482. // test custom root group data
  483. Group* root = db.rootGroup();
  484. root->customData()->set(customDataKey1, customData1);
  485. root->customData()->set(customDataKey2, customData2);
  486. QCOMPARE(root->customData()->size(), 3);
  487. QCOMPARE(root->customData()->dataSize(), dataSize);
  488. // test copied custom group data
  489. auto* group = new Group();
  490. group->setParent(root);
  491. group->setUuid(QUuid::createUuid());
  492. group->customData()->copyDataFrom(root->customData());
  493. QCOMPARE(*group->customData(), *root->customData());
  494. // test copied custom entry data
  495. auto* entry = new Entry();
  496. entry->setGroup(group);
  497. entry->setUuid(QUuid::createUuid());
  498. entry->customData()->copyDataFrom(group->customData());
  499. QCOMPARE(*entry->customData(), *root->customData());
  500. // test custom data deletion
  501. entry->customData()->set("additional item", "foobar");
  502. QCOMPARE(entry->customData()->size(), 4);
  503. entry->customData()->remove("additional item");
  504. QCOMPARE(entry->customData()->size(), 3);
  505. QCOMPARE(entry->customData()->dataSize(), dataSize);
  506. // test custom data on cloned groups
  507. QScopedPointer<Group> clonedGroup(group->clone());
  508. QCOMPARE(*clonedGroup->customData(), *group->customData());
  509. // test custom data on cloned entries
  510. QScopedPointer<Entry> clonedEntry(entry->clone(Entry::CloneNoFlags));
  511. QCOMPARE(*clonedEntry->customData(), *entry->customData());
  512. QBuffer buffer;
  513. buffer.open(QBuffer::ReadWrite);
  514. KeePass2Writer writer;
  515. writer.writeDatabase(&buffer, &db);
  516. // read buffer back
  517. buffer.seek(0);
  518. KeePass2Reader reader;
  519. auto newDb = QSharedPointer<Database>::create();
  520. reader.readDatabase(&buffer, QSharedPointer<CompositeKey>::create(), newDb.data());
  521. // test all custom data are read back successfully from KDBX
  522. QCOMPARE(newDb->publicCustomData(), publicCustomData);
  523. QCOMPARE(newDb->metadata()->customData()->value(customDataKey1), customData1);
  524. QCOMPARE(newDb->metadata()->customData()->value(customDataKey2), customData2);
  525. QCOMPARE(newDb->rootGroup()->customData()->value(customDataKey1), customData1);
  526. QCOMPARE(newDb->rootGroup()->customData()->value(customDataKey2), customData2);
  527. auto* newGroup = newDb->rootGroup()->children()[0];
  528. QCOMPARE(newGroup->customData()->value(customDataKey1), customData1);
  529. QCOMPARE(newGroup->customData()->value(customDataKey2), customData2);
  530. auto* newEntry = newDb->rootGroup()->children()[0]->entries()[0];
  531. QCOMPARE(newEntry->customData()->value(customDataKey1), customData1);
  532. QCOMPARE(newEntry->customData()->value(customDataKey2), customData2);
  533. }