TestSharing.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "TestSharing.h"
  18. #include "TestGlobal.h"
  19. #include <QBuffer>
  20. #include <QSignalSpy>
  21. #include <QTemporaryFile>
  22. #include <QXmlStreamReader>
  23. #include <QXmlStreamWriter>
  24. #include "config-keepassx-tests.h"
  25. #include "core/Metadata.h"
  26. #include "crypto/Crypto.h"
  27. #include "crypto/Random.h"
  28. #include "format/KeePass2Writer.h"
  29. #include "keeshare/KeeShareSettings.h"
  30. #include "keys/PasswordKey.h"
  31. #include <botan/pkcs8.h>
  32. #include <botan/rsa.h>
  33. #include <botan/x509_key.h>
  34. #include <format/KeePass2Reader.h>
  35. QTEST_GUILESS_MAIN(TestSharing)
  36. Q_DECLARE_METATYPE(KeeShareSettings::Type)
  37. Q_DECLARE_METATYPE(KeeShareSettings::Key)
  38. Q_DECLARE_METATYPE(KeeShareSettings::Certificate)
  39. Q_DECLARE_METATYPE(KeeShareSettings::Trust)
  40. Q_DECLARE_METATYPE(KeeShareSettings::ScopedCertificate)
  41. Q_DECLARE_METATYPE(QList<KeeShareSettings::ScopedCertificate>)
  42. void TestSharing::initTestCase()
  43. {
  44. QVERIFY(Crypto::init());
  45. }
  46. void TestSharing::testNullObjects()
  47. {
  48. const QString empty;
  49. QXmlStreamReader reader(empty);
  50. const auto nullKey = KeeShareSettings::Key();
  51. QVERIFY(nullKey.isNull());
  52. const auto xmlKey = KeeShareSettings::Key::deserialize(reader);
  53. QVERIFY(xmlKey.isNull());
  54. const auto certificate = KeeShareSettings::Certificate();
  55. QVERIFY(certificate.isNull());
  56. const auto xmlCertificate = KeeShareSettings::Certificate::deserialize(reader);
  57. QVERIFY(xmlCertificate.isNull());
  58. const auto own = KeeShareSettings::Own();
  59. QVERIFY(own.isNull());
  60. const auto xmlOwn = KeeShareSettings::Own::deserialize(empty);
  61. QVERIFY(xmlOwn.isNull());
  62. const auto active = KeeShareSettings::Active();
  63. QVERIFY(active.isNull());
  64. const auto xmlActive = KeeShareSettings::Active::deserialize(empty);
  65. QVERIFY(xmlActive.isNull());
  66. const auto foreign = KeeShareSettings::Foreign();
  67. QVERIFY(foreign.certificates.isEmpty());
  68. const auto xmlForeign = KeeShareSettings::Foreign::deserialize(empty);
  69. QVERIFY(xmlForeign.certificates.isEmpty());
  70. const auto reference = KeeShareSettings::Reference();
  71. QVERIFY(reference.isNull());
  72. const auto xmlReference = KeeShareSettings::Reference::deserialize(empty);
  73. QVERIFY(xmlReference.isNull());
  74. }
  75. void TestSharing::testCertificateSerialization()
  76. {
  77. QFETCH(KeeShareSettings::Trust, trusted);
  78. auto key = stubkey();
  79. KeeShareSettings::ScopedCertificate original;
  80. original.path = "/path";
  81. original.certificate = KeeShareSettings::Certificate{key, "Some <!> &#_\"\" weird string"};
  82. original.trust = trusted;
  83. QString buffer;
  84. QXmlStreamWriter writer(&buffer);
  85. writer.writeStartDocument();
  86. writer.writeStartElement("Certificate");
  87. KeeShareSettings::ScopedCertificate::serialize(writer, original);
  88. writer.writeEndElement();
  89. writer.writeEndDocument();
  90. QXmlStreamReader reader(buffer);
  91. reader.readNextStartElement();
  92. QVERIFY(reader.name() == "Certificate");
  93. KeeShareSettings::ScopedCertificate restored = KeeShareSettings::ScopedCertificate::deserialize(reader);
  94. QCOMPARE(restored.certificate.key->private_key_bits(), original.certificate.key->private_key_bits());
  95. QCOMPARE(restored.certificate.signer, original.certificate.signer);
  96. QCOMPARE(restored.trust, original.trust);
  97. QCOMPARE(restored.path, original.path);
  98. QCOMPARE(restored.certificate.key->public_key_bits(), key->public_key_bits());
  99. }
  100. void TestSharing::testCertificateSerialization_data()
  101. {
  102. QTest::addColumn<KeeShareSettings::Trust>("trusted");
  103. QTest::newRow("Ask") << KeeShareSettings::Trust::Ask;
  104. QTest::newRow("Trusted") << KeeShareSettings::Trust::Trusted;
  105. QTest::newRow("Untrusted") << KeeShareSettings::Trust::Untrusted;
  106. }
  107. void TestSharing::testKeySerialization()
  108. {
  109. auto key = stubkey();
  110. KeeShareSettings::Key original;
  111. original.key = key;
  112. QString buffer;
  113. QXmlStreamWriter writer(&buffer);
  114. writer.writeStartDocument();
  115. writer.writeStartElement("Key");
  116. KeeShareSettings::Key::serialize(writer, original);
  117. writer.writeEndElement();
  118. writer.writeEndDocument();
  119. QXmlStreamReader reader(buffer);
  120. reader.readNextStartElement();
  121. QVERIFY(reader.name() == "Key");
  122. KeeShareSettings::Key restored = KeeShareSettings::Key::deserialize(reader);
  123. QCOMPARE(restored.key->private_key_bits(), original.key->private_key_bits());
  124. QCOMPARE(restored.key->algo_name(), original.key->algo_name());
  125. }
  126. void TestSharing::testReferenceSerialization()
  127. {
  128. QFETCH(QString, password);
  129. QFETCH(QString, path);
  130. QFETCH(QUuid, uuid);
  131. QFETCH(int, type);
  132. KeeShareSettings::Reference original;
  133. original.password = password;
  134. original.path = path;
  135. original.uuid = uuid;
  136. original.type = static_cast<KeeShareSettings::Type>(type);
  137. const QString serialized = KeeShareSettings::Reference::serialize(original);
  138. const KeeShareSettings::Reference restored = KeeShareSettings::Reference::deserialize(serialized);
  139. QCOMPARE(restored.password, original.password);
  140. QCOMPARE(restored.path, original.path);
  141. QCOMPARE(restored.uuid, original.uuid);
  142. QCOMPARE(int(restored.type), int(original.type));
  143. }
  144. void TestSharing::testReferenceSerialization_data()
  145. {
  146. QTest::addColumn<QString>("password");
  147. QTest::addColumn<QString>("path");
  148. QTest::addColumn<QUuid>("uuid");
  149. QTest::addColumn<int>("type");
  150. QTest::newRow("1") << "Password"
  151. << "/some/path" << QUuid::createUuid() << int(KeeShareSettings::Inactive);
  152. QTest::newRow("2") << ""
  153. << "" << QUuid() << int(KeeShareSettings::SynchronizeWith);
  154. QTest::newRow("3") << ""
  155. << "/some/path" << QUuid() << int(KeeShareSettings::ExportTo);
  156. }
  157. void TestSharing::testSettingsSerialization()
  158. {
  159. QFETCH(bool, importing);
  160. QFETCH(bool, exporting);
  161. QFETCH(KeeShareSettings::Certificate, ownCertificate);
  162. QFETCH(KeeShareSettings::Key, ownKey);
  163. QFETCH(QList<KeeShareSettings::ScopedCertificate>, foreignCertificates);
  164. KeeShareSettings::Own originalOwn;
  165. KeeShareSettings::Foreign originalForeign;
  166. KeeShareSettings::Active originalActive;
  167. originalActive.in = importing;
  168. originalActive.out = exporting;
  169. originalOwn.certificate = ownCertificate;
  170. originalOwn.key = ownKey;
  171. originalForeign.certificates = foreignCertificates;
  172. const QString serializedActive = KeeShareSettings::Active::serialize(originalActive);
  173. KeeShareSettings::Active restoredActive = KeeShareSettings::Active::deserialize(serializedActive);
  174. const QString serializedOwn = KeeShareSettings::Own::serialize(originalOwn);
  175. KeeShareSettings::Own restoredOwn = KeeShareSettings::Own::deserialize(serializedOwn);
  176. const QString serializedForeign = KeeShareSettings::Foreign::serialize(originalForeign);
  177. KeeShareSettings::Foreign restoredForeign = KeeShareSettings::Foreign::deserialize(serializedForeign);
  178. QCOMPARE(restoredActive.in, importing);
  179. QCOMPARE(restoredActive.out, exporting);
  180. if (ownCertificate.key) {
  181. QCOMPARE(restoredOwn.certificate, ownCertificate);
  182. }
  183. if (ownKey.key) {
  184. QCOMPARE(restoredOwn.key, ownKey);
  185. }
  186. QCOMPARE(restoredForeign.certificates.count(), foreignCertificates.count());
  187. for (int i = 0; i < foreignCertificates.count(); ++i) {
  188. QCOMPARE(restoredForeign.certificates[i].certificate, foreignCertificates[i].certificate);
  189. }
  190. }
  191. void TestSharing::testSettingsSerialization_data()
  192. {
  193. auto sshKey0 = stubkey(0);
  194. KeeShareSettings::ScopedCertificate certificate0;
  195. certificate0.path = "/path/0";
  196. certificate0.certificate = KeeShareSettings::Certificate{sshKey0, "Some <!> &#_\"\" weird string"};
  197. certificate0.trust = KeeShareSettings::Trust::Trusted;
  198. KeeShareSettings::Key key0;
  199. key0.key = sshKey0;
  200. auto sshKey1 = stubkey(1);
  201. KeeShareSettings::ScopedCertificate certificate1;
  202. certificate1.path = "/path/1";
  203. certificate1.certificate = KeeShareSettings::Certificate{sshKey1, "Another "};
  204. certificate1.trust = KeeShareSettings::Trust::Untrusted;
  205. QTest::addColumn<bool>("importing");
  206. QTest::addColumn<bool>("exporting");
  207. QTest::addColumn<KeeShareSettings::Certificate>("ownCertificate");
  208. QTest::addColumn<KeeShareSettings::Key>("ownKey");
  209. QTest::addColumn<QList<KeeShareSettings::ScopedCertificate>>("foreignCertificates");
  210. QTest::newRow("1") << false << false << KeeShareSettings::Certificate() << KeeShareSettings::Key()
  211. << QList<KeeShareSettings::ScopedCertificate>();
  212. QTest::newRow("2") << true << false << KeeShareSettings::Certificate() << KeeShareSettings::Key()
  213. << QList<KeeShareSettings::ScopedCertificate>();
  214. QTest::newRow("3") << true << true << KeeShareSettings::Certificate() << KeeShareSettings::Key()
  215. << QList<KeeShareSettings::ScopedCertificate>({certificate0, certificate1});
  216. QTest::newRow("4") << false << true << certificate0.certificate << key0
  217. << QList<KeeShareSettings::ScopedCertificate>();
  218. QTest::newRow("5") << false << false << certificate0.certificate << key0
  219. << QList<KeeShareSettings::ScopedCertificate>({certificate1});
  220. }
  221. const QSharedPointer<Botan::RSA_PrivateKey> TestSharing::stubkey(int index)
  222. {
  223. static QMap<int, QSharedPointer<Botan::RSA_PrivateKey>> keys;
  224. if (!keys.contains(index)) {
  225. keys.insert(index,
  226. QSharedPointer<Botan::RSA_PrivateKey>(new Botan::RSA_PrivateKey(*randomGen()->getRng(), 2048)));
  227. }
  228. return keys[index];
  229. }