2020-02-06 03:15:50 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "TestSSHAgent.h"
|
2021-08-08 09:36:06 -04:00
|
|
|
#include "config-keepassx-tests.h"
|
2020-02-06 03:15:50 -05:00
|
|
|
#include "core/Config.h"
|
|
|
|
#include "crypto/Crypto.h"
|
2021-07-11 22:10:29 -04:00
|
|
|
#include "sshagent/KeeAgentSettings.h"
|
2021-12-10 13:57:22 -05:00
|
|
|
#include "sshagent/OpenSSHKeyGen.h"
|
2020-02-06 03:15:50 -05:00
|
|
|
#include "sshagent/SSHAgent.h"
|
|
|
|
|
2021-07-11 22:10:29 -04:00
|
|
|
#include <QTest>
|
|
|
|
|
2020-02-06 03:15:50 -05:00
|
|
|
QTEST_GUILESS_MAIN(TestSSHAgent)
|
|
|
|
|
|
|
|
void TestSSHAgent::initTestCase()
|
|
|
|
{
|
|
|
|
QVERIFY(Crypto::init());
|
|
|
|
Config::createTempFileInstance();
|
|
|
|
|
|
|
|
m_agentSocketFile.setAutoRemove(true);
|
|
|
|
QVERIFY(m_agentSocketFile.open());
|
|
|
|
|
|
|
|
m_agentSocketFileName = m_agentSocketFile.fileName();
|
|
|
|
QVERIFY(!m_agentSocketFileName.isEmpty());
|
|
|
|
|
|
|
|
// let ssh-agent re-create it as a socket
|
|
|
|
QVERIFY(m_agentSocketFile.remove());
|
|
|
|
|
|
|
|
QStringList arguments;
|
2024-07-24 04:03:25 -04:00
|
|
|
arguments << "-D" << "-a" << m_agentSocketFileName;
|
2020-02-06 03:15:50 -05:00
|
|
|
|
|
|
|
QElapsedTimer timer;
|
|
|
|
timer.start();
|
|
|
|
|
|
|
|
qDebug() << "ssh-agent starting with arguments" << arguments;
|
|
|
|
m_agentProcess.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
|
|
m_agentProcess.start("ssh-agent", arguments);
|
|
|
|
m_agentProcess.closeWriteChannel();
|
|
|
|
|
|
|
|
if (!m_agentProcess.waitForStarted()) {
|
|
|
|
QSKIP("ssh-agent could not be started");
|
|
|
|
}
|
|
|
|
|
2024-06-22 07:22:44 -04:00
|
|
|
qDebug() << "ssh-agent started as pid" << m_agentProcess.processId();
|
2020-02-06 03:15:50 -05:00
|
|
|
|
|
|
|
// we need to wait for the agent to open the socket before going into real tests
|
|
|
|
QFileInfo socketFileInfo(m_agentSocketFileName);
|
|
|
|
while (!timer.hasExpired(2000)) {
|
|
|
|
if (socketFileInfo.exists()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
QTest::qWait(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVERIFY(socketFileInfo.exists());
|
|
|
|
qDebug() << "ssh-agent initialized in" << timer.elapsed() << "ms";
|
|
|
|
|
|
|
|
// initialize test key
|
|
|
|
const QString keyString = QString("-----BEGIN OPENSSH PRIVATE KEY-----\n"
|
|
|
|
"b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW\n"
|
|
|
|
"QyNTUxOQAAACDdlO5F2kF2WzedrBAHBi9wBHeISzXZ0IuIqrp0EzeazAAAAKjgCfj94An4\n"
|
|
|
|
"/QAAAAtzc2gtZWQyNTUxOQAAACDdlO5F2kF2WzedrBAHBi9wBHeISzXZ0IuIqrp0EzeazA\n"
|
|
|
|
"AAAEBe1iilZFho8ZGAliiSj5URvFtGrgvmnEKdiLZow5hOR92U7kXaQXZbN52sEAcGL3AE\n"
|
|
|
|
"d4hLNdnQi4iqunQTN5rMAAAAH29wZW5zc2hrZXktdGVzdC1wYXJzZUBrZWVwYXNzeGMBAg\n"
|
|
|
|
"MEBQY=\n"
|
|
|
|
"-----END OPENSSH PRIVATE KEY-----\n");
|
|
|
|
|
|
|
|
const QByteArray keyData = keyString.toLatin1();
|
|
|
|
|
|
|
|
QVERIFY(m_key.parsePKCS1PEM(keyData));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSSHAgent::testConfiguration()
|
|
|
|
{
|
|
|
|
SSHAgent agent;
|
|
|
|
|
|
|
|
// default config must not enable agent
|
|
|
|
QVERIFY(!agent.isEnabled());
|
|
|
|
|
|
|
|
agent.setEnabled(true);
|
|
|
|
QVERIFY(agent.isEnabled());
|
|
|
|
|
|
|
|
// this will either be an empty string or the real ssh-agent socket path, doesn't matter
|
|
|
|
QString defaultSocketPath = agent.socketPath(false);
|
|
|
|
|
|
|
|
// overridden path must match default before setting an override
|
|
|
|
QCOMPARE(agent.socketPath(true), defaultSocketPath);
|
|
|
|
|
|
|
|
agent.setAuthSockOverride(m_agentSocketFileName);
|
|
|
|
|
|
|
|
// overridden path must match what we set
|
|
|
|
QCOMPARE(agent.socketPath(true), m_agentSocketFileName);
|
|
|
|
|
|
|
|
// non-overridden path must match the default
|
|
|
|
QCOMPARE(agent.socketPath(false), defaultSocketPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSSHAgent::testIdentity()
|
|
|
|
{
|
|
|
|
SSHAgent agent;
|
|
|
|
agent.setEnabled(true);
|
|
|
|
agent.setAuthSockOverride(m_agentSocketFileName);
|
|
|
|
|
|
|
|
QVERIFY(agent.isAgentRunning());
|
|
|
|
|
|
|
|
KeeAgentSettings settings;
|
|
|
|
bool keyInAgent;
|
|
|
|
|
|
|
|
// test adding a key works
|
2020-04-04 02:00:49 -04:00
|
|
|
QVERIFY(agent.addIdentity(m_key, settings, m_uuid));
|
2020-02-06 03:15:50 -05:00
|
|
|
QVERIFY(agent.checkIdentity(m_key, keyInAgent) && keyInAgent);
|
|
|
|
|
2020-04-04 02:00:49 -04:00
|
|
|
// test non-conflicting key ownership doesn't throw an error
|
|
|
|
QVERIFY(agent.addIdentity(m_key, settings, m_uuid));
|
|
|
|
|
|
|
|
// test conflicting key ownership throws an error
|
|
|
|
QUuid secondUuid("{11111111-1111-1111-1111-111111111111}");
|
|
|
|
QVERIFY(!agent.addIdentity(m_key, settings, secondUuid));
|
|
|
|
|
2020-02-06 03:15:50 -05:00
|
|
|
// test removing a key works
|
|
|
|
QVERIFY(agent.removeIdentity(m_key));
|
|
|
|
QVERIFY(agent.checkIdentity(m_key, keyInAgent) && !keyInAgent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSSHAgent::testRemoveOnClose()
|
|
|
|
{
|
|
|
|
SSHAgent agent;
|
|
|
|
agent.setEnabled(true);
|
|
|
|
agent.setAuthSockOverride(m_agentSocketFileName);
|
|
|
|
|
|
|
|
QVERIFY(agent.isAgentRunning());
|
|
|
|
|
|
|
|
KeeAgentSettings settings;
|
|
|
|
bool keyInAgent;
|
|
|
|
|
|
|
|
settings.setRemoveAtDatabaseClose(true);
|
2020-04-04 02:00:49 -04:00
|
|
|
QVERIFY(agent.addIdentity(m_key, settings, m_uuid));
|
2020-02-06 03:15:50 -05:00
|
|
|
QVERIFY(agent.checkIdentity(m_key, keyInAgent) && keyInAgent);
|
|
|
|
agent.setEnabled(false);
|
|
|
|
QVERIFY(agent.checkIdentity(m_key, keyInAgent) && !keyInAgent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSSHAgent::testLifetimeConstraint()
|
|
|
|
{
|
|
|
|
SSHAgent agent;
|
|
|
|
agent.setEnabled(true);
|
|
|
|
agent.setAuthSockOverride(m_agentSocketFileName);
|
|
|
|
|
|
|
|
QVERIFY(agent.isAgentRunning());
|
|
|
|
|
|
|
|
KeeAgentSettings settings;
|
|
|
|
bool keyInAgent;
|
|
|
|
|
|
|
|
settings.setUseLifetimeConstraintWhenAdding(true);
|
|
|
|
settings.setLifetimeConstraintDuration(2); // two seconds
|
|
|
|
|
|
|
|
// identity should be in agent immediately after adding
|
2020-04-04 02:00:49 -04:00
|
|
|
QVERIFY(agent.addIdentity(m_key, settings, m_uuid));
|
2020-02-06 03:15:50 -05:00
|
|
|
QVERIFY(agent.checkIdentity(m_key, keyInAgent) && keyInAgent);
|
|
|
|
|
|
|
|
QElapsedTimer timer;
|
|
|
|
timer.start();
|
|
|
|
|
|
|
|
// wait for the identity to time out
|
|
|
|
while (!timer.hasExpired(5000)) {
|
|
|
|
QVERIFY(agent.checkIdentity(m_key, keyInAgent));
|
|
|
|
|
|
|
|
if (!keyInAgent) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTest::qWait(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVERIFY(!keyInAgent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSSHAgent::testConfirmConstraint()
|
|
|
|
{
|
|
|
|
SSHAgent agent;
|
|
|
|
agent.setEnabled(true);
|
|
|
|
agent.setAuthSockOverride(m_agentSocketFileName);
|
|
|
|
|
|
|
|
QVERIFY(agent.isAgentRunning());
|
|
|
|
|
|
|
|
KeeAgentSettings settings;
|
|
|
|
bool keyInAgent;
|
|
|
|
|
|
|
|
settings.setUseConfirmConstraintWhenAdding(true);
|
|
|
|
|
2020-04-04 02:00:49 -04:00
|
|
|
QVERIFY(agent.addIdentity(m_key, settings, m_uuid));
|
2020-02-06 03:15:50 -05:00
|
|
|
|
|
|
|
// we can't test confirmation itself is working but we can test the agent accepts the key
|
|
|
|
QVERIFY(agent.checkIdentity(m_key, keyInAgent) && keyInAgent);
|
|
|
|
|
|
|
|
QVERIFY(agent.removeIdentity(m_key));
|
|
|
|
QVERIFY(agent.checkIdentity(m_key, keyInAgent) && !keyInAgent);
|
|
|
|
}
|
|
|
|
|
2021-08-08 09:36:06 -04:00
|
|
|
void TestSSHAgent::testToOpenSSHKey()
|
|
|
|
{
|
|
|
|
KeeAgentSettings settings;
|
|
|
|
settings.setSelectedType("file");
|
|
|
|
settings.setFileName(QString("%1/id_rsa-encrypted-asn1").arg(QString(KEEPASSX_TEST_DATA_DIR)));
|
|
|
|
|
|
|
|
OpenSSHKey key;
|
|
|
|
settings.toOpenSSHKey("username", "correctpassphrase", QString(), nullptr, key, false);
|
|
|
|
|
|
|
|
QVERIFY(!key.publicKey().isEmpty());
|
|
|
|
}
|
|
|
|
|
2021-12-10 13:57:22 -05:00
|
|
|
void TestSSHAgent::testKeyGenRSA()
|
|
|
|
{
|
|
|
|
SSHAgent agent;
|
|
|
|
agent.setEnabled(true);
|
|
|
|
agent.setAuthSockOverride(m_agentSocketFileName);
|
|
|
|
|
|
|
|
QVERIFY(agent.isAgentRunning());
|
|
|
|
|
|
|
|
OpenSSHKey key;
|
|
|
|
KeeAgentSettings settings;
|
|
|
|
bool keyInAgent;
|
|
|
|
|
|
|
|
QVERIFY(OpenSSHKeyGen::generateRSA(key, 2048));
|
|
|
|
|
|
|
|
QVERIFY(agent.addIdentity(key, settings, m_uuid));
|
|
|
|
QVERIFY(agent.checkIdentity(key, keyInAgent) && keyInAgent);
|
|
|
|
QVERIFY(agent.removeIdentity(key));
|
|
|
|
QVERIFY(agent.checkIdentity(key, keyInAgent) && !keyInAgent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSSHAgent::testKeyGenECDSA()
|
|
|
|
{
|
|
|
|
SSHAgent agent;
|
|
|
|
agent.setEnabled(true);
|
|
|
|
agent.setAuthSockOverride(m_agentSocketFileName);
|
|
|
|
|
|
|
|
QVERIFY(agent.isAgentRunning());
|
|
|
|
|
|
|
|
OpenSSHKey key;
|
|
|
|
KeeAgentSettings settings;
|
|
|
|
bool keyInAgent;
|
|
|
|
|
|
|
|
QVERIFY(OpenSSHKeyGen::generateECDSA(key, 256));
|
|
|
|
|
|
|
|
QVERIFY(agent.addIdentity(key, settings, m_uuid));
|
|
|
|
QVERIFY(agent.checkIdentity(key, keyInAgent) && keyInAgent);
|
|
|
|
QVERIFY(agent.removeIdentity(key));
|
|
|
|
QVERIFY(agent.checkIdentity(key, keyInAgent) && !keyInAgent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSSHAgent::testKeyGenEd25519()
|
|
|
|
{
|
|
|
|
SSHAgent agent;
|
|
|
|
agent.setEnabled(true);
|
|
|
|
agent.setAuthSockOverride(m_agentSocketFileName);
|
|
|
|
|
|
|
|
QVERIFY(agent.isAgentRunning());
|
|
|
|
|
|
|
|
OpenSSHKey key;
|
|
|
|
KeeAgentSettings settings;
|
|
|
|
bool keyInAgent;
|
|
|
|
|
|
|
|
QVERIFY(OpenSSHKeyGen::generateEd25519(key));
|
|
|
|
|
|
|
|
QVERIFY(agent.addIdentity(key, settings, m_uuid));
|
|
|
|
QVERIFY(agent.checkIdentity(key, keyInAgent) && keyInAgent);
|
|
|
|
QVERIFY(agent.removeIdentity(key));
|
|
|
|
QVERIFY(agent.checkIdentity(key, keyInAgent) && !keyInAgent);
|
|
|
|
}
|
|
|
|
|
2020-02-06 03:15:50 -05:00
|
|
|
void TestSSHAgent::cleanupTestCase()
|
|
|
|
{
|
|
|
|
if (m_agentProcess.state() != QProcess::NotRunning) {
|
2024-06-22 07:22:44 -04:00
|
|
|
qDebug() << "Killing ssh-agent pid" << m_agentProcess.processId();
|
2020-02-06 03:15:50 -05:00
|
|
|
m_agentProcess.terminate();
|
|
|
|
m_agentProcess.waitForFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_agentSocketFile.remove();
|
|
|
|
}
|