TestSSHAgent.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2020 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 "TestSSHAgent.h"
  18. #include "config-keepassx-tests.h"
  19. #include "core/Config.h"
  20. #include "crypto/Crypto.h"
  21. #include "sshagent/KeeAgentSettings.h"
  22. #include "sshagent/OpenSSHKeyGen.h"
  23. #include "sshagent/SSHAgent.h"
  24. #include <QTest>
  25. QTEST_GUILESS_MAIN(TestSSHAgent)
  26. void TestSSHAgent::initTestCase()
  27. {
  28. QVERIFY(Crypto::init());
  29. Config::createTempFileInstance();
  30. m_agentSocketFile.setAutoRemove(true);
  31. QVERIFY(m_agentSocketFile.open());
  32. m_agentSocketFileName = m_agentSocketFile.fileName();
  33. QVERIFY(!m_agentSocketFileName.isEmpty());
  34. // let ssh-agent re-create it as a socket
  35. QVERIFY(m_agentSocketFile.remove());
  36. QStringList arguments;
  37. arguments << "-D" << "-a" << m_agentSocketFileName;
  38. QElapsedTimer timer;
  39. timer.start();
  40. qDebug() << "ssh-agent starting with arguments" << arguments;
  41. m_agentProcess.setProcessChannelMode(QProcess::ForwardedChannels);
  42. m_agentProcess.start("ssh-agent", arguments);
  43. m_agentProcess.closeWriteChannel();
  44. if (!m_agentProcess.waitForStarted()) {
  45. QSKIP("ssh-agent could not be started");
  46. }
  47. qDebug() << "ssh-agent started as pid" << m_agentProcess.processId();
  48. // we need to wait for the agent to open the socket before going into real tests
  49. QFileInfo socketFileInfo(m_agentSocketFileName);
  50. while (!timer.hasExpired(2000)) {
  51. if (socketFileInfo.exists()) {
  52. break;
  53. }
  54. QTest::qWait(10);
  55. }
  56. QVERIFY(socketFileInfo.exists());
  57. qDebug() << "ssh-agent initialized in" << timer.elapsed() << "ms";
  58. // initialize test key
  59. const QString keyString = QString("-----BEGIN OPENSSH PRIVATE KEY-----\n"
  60. "b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW\n"
  61. "QyNTUxOQAAACDdlO5F2kF2WzedrBAHBi9wBHeISzXZ0IuIqrp0EzeazAAAAKjgCfj94An4\n"
  62. "/QAAAAtzc2gtZWQyNTUxOQAAACDdlO5F2kF2WzedrBAHBi9wBHeISzXZ0IuIqrp0EzeazA\n"
  63. "AAAEBe1iilZFho8ZGAliiSj5URvFtGrgvmnEKdiLZow5hOR92U7kXaQXZbN52sEAcGL3AE\n"
  64. "d4hLNdnQi4iqunQTN5rMAAAAH29wZW5zc2hrZXktdGVzdC1wYXJzZUBrZWVwYXNzeGMBAg\n"
  65. "MEBQY=\n"
  66. "-----END OPENSSH PRIVATE KEY-----\n");
  67. const QByteArray keyData = keyString.toLatin1();
  68. QVERIFY(m_key.parsePKCS1PEM(keyData));
  69. }
  70. void TestSSHAgent::testConfiguration()
  71. {
  72. SSHAgent agent;
  73. // default config must not enable agent
  74. QVERIFY(!agent.isEnabled());
  75. agent.setEnabled(true);
  76. QVERIFY(agent.isEnabled());
  77. // this will either be an empty string or the real ssh-agent socket path, doesn't matter
  78. QString defaultSocketPath = agent.socketPath(false);
  79. // overridden path must match default before setting an override
  80. QCOMPARE(agent.socketPath(true), defaultSocketPath);
  81. agent.setAuthSockOverride(m_agentSocketFileName);
  82. // overridden path must match what we set
  83. QCOMPARE(agent.socketPath(true), m_agentSocketFileName);
  84. // non-overridden path must match the default
  85. QCOMPARE(agent.socketPath(false), defaultSocketPath);
  86. }
  87. void TestSSHAgent::testIdentity()
  88. {
  89. SSHAgent agent;
  90. agent.setEnabled(true);
  91. agent.setAuthSockOverride(m_agentSocketFileName);
  92. QVERIFY(agent.isAgentRunning());
  93. KeeAgentSettings settings;
  94. bool keyInAgent;
  95. // test adding a key works
  96. QVERIFY(agent.addIdentity(m_key, settings, m_uuid));
  97. QVERIFY(agent.checkIdentity(m_key, keyInAgent) && keyInAgent);
  98. // test non-conflicting key ownership doesn't throw an error
  99. QVERIFY(agent.addIdentity(m_key, settings, m_uuid));
  100. // test conflicting key ownership throws an error
  101. QUuid secondUuid("{11111111-1111-1111-1111-111111111111}");
  102. QVERIFY(!agent.addIdentity(m_key, settings, secondUuid));
  103. // test removing a key works
  104. QVERIFY(agent.removeIdentity(m_key));
  105. QVERIFY(agent.checkIdentity(m_key, keyInAgent) && !keyInAgent);
  106. }
  107. void TestSSHAgent::testRemoveOnClose()
  108. {
  109. SSHAgent agent;
  110. agent.setEnabled(true);
  111. agent.setAuthSockOverride(m_agentSocketFileName);
  112. QVERIFY(agent.isAgentRunning());
  113. KeeAgentSettings settings;
  114. bool keyInAgent;
  115. settings.setRemoveAtDatabaseClose(true);
  116. QVERIFY(agent.addIdentity(m_key, settings, m_uuid));
  117. QVERIFY(agent.checkIdentity(m_key, keyInAgent) && keyInAgent);
  118. agent.setEnabled(false);
  119. QVERIFY(agent.checkIdentity(m_key, keyInAgent) && !keyInAgent);
  120. }
  121. void TestSSHAgent::testLifetimeConstraint()
  122. {
  123. SSHAgent agent;
  124. agent.setEnabled(true);
  125. agent.setAuthSockOverride(m_agentSocketFileName);
  126. QVERIFY(agent.isAgentRunning());
  127. KeeAgentSettings settings;
  128. bool keyInAgent;
  129. settings.setUseLifetimeConstraintWhenAdding(true);
  130. settings.setLifetimeConstraintDuration(2); // two seconds
  131. // identity should be in agent immediately after adding
  132. QVERIFY(agent.addIdentity(m_key, settings, m_uuid));
  133. QVERIFY(agent.checkIdentity(m_key, keyInAgent) && keyInAgent);
  134. QElapsedTimer timer;
  135. timer.start();
  136. // wait for the identity to time out
  137. while (!timer.hasExpired(5000)) {
  138. QVERIFY(agent.checkIdentity(m_key, keyInAgent));
  139. if (!keyInAgent) {
  140. break;
  141. }
  142. QTest::qWait(100);
  143. }
  144. QVERIFY(!keyInAgent);
  145. }
  146. void TestSSHAgent::testConfirmConstraint()
  147. {
  148. SSHAgent agent;
  149. agent.setEnabled(true);
  150. agent.setAuthSockOverride(m_agentSocketFileName);
  151. QVERIFY(agent.isAgentRunning());
  152. KeeAgentSettings settings;
  153. bool keyInAgent;
  154. settings.setUseConfirmConstraintWhenAdding(true);
  155. QVERIFY(agent.addIdentity(m_key, settings, m_uuid));
  156. // we can't test confirmation itself is working but we can test the agent accepts the key
  157. QVERIFY(agent.checkIdentity(m_key, keyInAgent) && keyInAgent);
  158. QVERIFY(agent.removeIdentity(m_key));
  159. QVERIFY(agent.checkIdentity(m_key, keyInAgent) && !keyInAgent);
  160. }
  161. void TestSSHAgent::testToOpenSSHKey()
  162. {
  163. KeeAgentSettings settings;
  164. settings.setSelectedType("file");
  165. settings.setFileName(QString("%1/id_rsa-encrypted-asn1").arg(QString(KEEPASSX_TEST_DATA_DIR)));
  166. OpenSSHKey key;
  167. settings.toOpenSSHKey("username", "correctpassphrase", QString(), nullptr, key, false);
  168. QVERIFY(!key.publicKey().isEmpty());
  169. }
  170. void TestSSHAgent::testKeyGenRSA()
  171. {
  172. SSHAgent agent;
  173. agent.setEnabled(true);
  174. agent.setAuthSockOverride(m_agentSocketFileName);
  175. QVERIFY(agent.isAgentRunning());
  176. OpenSSHKey key;
  177. KeeAgentSettings settings;
  178. bool keyInAgent;
  179. QVERIFY(OpenSSHKeyGen::generateRSA(key, 2048));
  180. QVERIFY(agent.addIdentity(key, settings, m_uuid));
  181. QVERIFY(agent.checkIdentity(key, keyInAgent) && keyInAgent);
  182. QVERIFY(agent.removeIdentity(key));
  183. QVERIFY(agent.checkIdentity(key, keyInAgent) && !keyInAgent);
  184. }
  185. void TestSSHAgent::testKeyGenECDSA()
  186. {
  187. SSHAgent agent;
  188. agent.setEnabled(true);
  189. agent.setAuthSockOverride(m_agentSocketFileName);
  190. QVERIFY(agent.isAgentRunning());
  191. OpenSSHKey key;
  192. KeeAgentSettings settings;
  193. bool keyInAgent;
  194. QVERIFY(OpenSSHKeyGen::generateECDSA(key, 256));
  195. QVERIFY(agent.addIdentity(key, settings, m_uuid));
  196. QVERIFY(agent.checkIdentity(key, keyInAgent) && keyInAgent);
  197. QVERIFY(agent.removeIdentity(key));
  198. QVERIFY(agent.checkIdentity(key, keyInAgent) && !keyInAgent);
  199. }
  200. void TestSSHAgent::testKeyGenEd25519()
  201. {
  202. SSHAgent agent;
  203. agent.setEnabled(true);
  204. agent.setAuthSockOverride(m_agentSocketFileName);
  205. QVERIFY(agent.isAgentRunning());
  206. OpenSSHKey key;
  207. KeeAgentSettings settings;
  208. bool keyInAgent;
  209. QVERIFY(OpenSSHKeyGen::generateEd25519(key));
  210. QVERIFY(agent.addIdentity(key, settings, m_uuid));
  211. QVERIFY(agent.checkIdentity(key, keyInAgent) && keyInAgent);
  212. QVERIFY(agent.removeIdentity(key));
  213. QVERIFY(agent.checkIdentity(key, keyInAgent) && !keyInAgent);
  214. }
  215. void TestSSHAgent::cleanupTestCase()
  216. {
  217. if (m_agentProcess.state() != QProcess::NotRunning) {
  218. qDebug() << "Killing ssh-agent pid" << m_agentProcess.processId();
  219. m_agentProcess.terminate();
  220. m_agentProcess.waitForFinished();
  221. }
  222. m_agentSocketFile.remove();
  223. }