TestYkChallengeResponseKey.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2014 Kyle Manna <kyle@kylemanna.com>
  3. * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
  4. *
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 or (at your option)
  9. * version 3 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "TestYkChallengeResponseKey.h"
  20. #include "TestGlobal.h"
  21. #include "core/Tools.h"
  22. #include "crypto/Crypto.h"
  23. #include "keys/ChallengeResponseKey.h"
  24. #include <QCryptographicHash>
  25. #include <QScopedPointer>
  26. #include <QSignalSpy>
  27. QTEST_GUILESS_MAIN(TestYubiKeyChallengeResponse)
  28. void TestYubiKeyChallengeResponse::initTestCase()
  29. {
  30. // crypto subsystem needs to be initialized for YubiKey testing
  31. QVERIFY(Crypto::init());
  32. if (!YubiKey::instance()->isInitialized()) {
  33. QSKIP("Unable to initialize YubiKey interface.");
  34. }
  35. }
  36. void TestYubiKeyChallengeResponse::testDetectDevices()
  37. {
  38. YubiKey::instance()->findValidKeys();
  39. // Wait for the hardware to respond
  40. QSignalSpy detected(YubiKey::instance(), SIGNAL(detectComplete(bool)));
  41. QTRY_VERIFY_WITH_TIMEOUT(detected.count() > 0, 2000);
  42. // Look at the information retrieved from the key(s)
  43. for (auto key : YubiKey::instance()->foundKeys()) {
  44. auto displayName = YubiKey::instance()->getDisplayName(key);
  45. QVERIFY(displayName.contains("Challenge-Response - Slot") || displayName.contains("Configured Slot -"));
  46. QVERIFY(displayName.contains(QString::number(key.first)));
  47. QVERIFY(displayName.contains(QString::number(key.second)));
  48. }
  49. }
  50. /**
  51. * Secret key for the YubiKey slot used by the unit test is
  52. * 1c e3 0f d7 8d 20 dc fa 40 b5 0c 18 77 9a fb 0f 02 28 8d b7
  53. * This secret can be on either slot but must be passive.
  54. */
  55. void TestYubiKeyChallengeResponse::testKeyChallenge()
  56. {
  57. auto keys = YubiKey::instance()->foundKeys();
  58. if (keys.isEmpty()) {
  59. QSKIP("No YubiKey devices were detected.");
  60. }
  61. // Find a key that is configured in passive mode
  62. bool wouldBlock = false;
  63. YubiKeySlot pKey(0, 0);
  64. for (auto key : keys) {
  65. if (YubiKey::instance()->testChallenge(key, &wouldBlock) && !wouldBlock) {
  66. pKey = key;
  67. break;
  68. }
  69. Tools::wait(100);
  70. }
  71. if (pKey.first == 0) {
  72. /* Testing active mode in unit tests is unreasonable */
  73. QSKIP("No YubiKey contains a slot in passive mode.");
  74. }
  75. QScopedPointer<ChallengeResponseKey> key(new ChallengeResponseKey(pKey));
  76. QByteArray ba("UnitTest");
  77. QVERIFY(key->challenge(ba));
  78. QCOMPARE(key->rawKey().size(), 20);
  79. auto hash = QString(QCryptographicHash::hash(key->rawKey(), QCryptographicHash::Sha256).toHex());
  80. QCOMPARE(hash, QString("2f7802c7112c301303526e7737b54d546c905076dca6e9538edf761a2264cd70"));
  81. }