mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-12-10 22:25:44 -05:00
CLI: Add Yubikey unlock support
This commit is contained in:
parent
77fcde875e
commit
964478e78f
19 changed files with 816 additions and 529 deletions
|
|
@ -160,6 +160,7 @@ set(keepassx_SOURCES
|
|||
keys/FileKey.cpp
|
||||
keys/PasswordKey.cpp
|
||||
keys/YkChallengeResponseKey.cpp
|
||||
keys/YkChallengeResponseKeyCLI.cpp
|
||||
streams/HashedBlockStream.cpp
|
||||
streams/HmacBlockStream.cpp
|
||||
streams/LayeredStream.cpp
|
||||
|
|
|
|||
|
|
@ -52,6 +52,12 @@ const QCommandLineOption Command::KeyFileOption = QCommandLineOption(QStringList
|
|||
const QCommandLineOption Command::NoPasswordOption =
|
||||
QCommandLineOption(QStringList() << "no-password", QObject::tr("Deactivate password key for the database."));
|
||||
|
||||
const QCommandLineOption Command::YubiKeyOption =
|
||||
QCommandLineOption(QStringList() << "y"
|
||||
<< "yubikey",
|
||||
QObject::tr("Yubikey slot used to encrypt the database."),
|
||||
QObject::tr("slot"));
|
||||
|
||||
QMap<QString, Command*> commands;
|
||||
|
||||
Command::Command()
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ public:
|
|||
static const QCommandLineOption QuietOption;
|
||||
static const QCommandLineOption KeyFileOption;
|
||||
static const QCommandLineOption NoPasswordOption;
|
||||
static const QCommandLineOption YubiKeyOption;
|
||||
};
|
||||
|
||||
#endif // KEEPASSXC_COMMAND_H
|
||||
|
|
|
|||
|
|
@ -24,6 +24,9 @@ DatabaseCommand::DatabaseCommand()
|
|||
positionalArguments.append({QString("database"), QObject::tr("Path of the database."), QString("")});
|
||||
options.append(Command::KeyFileOption);
|
||||
options.append(Command::NoPasswordOption);
|
||||
#ifdef WITH_XC_YUBIKEY
|
||||
options.append(Command::YubiKeyOption);
|
||||
#endif
|
||||
}
|
||||
|
||||
int DatabaseCommand::execute(const QStringList& arguments)
|
||||
|
|
@ -37,6 +40,7 @@ int DatabaseCommand::execute(const QStringList& arguments)
|
|||
auto db = Utils::unlockDatabase(args.at(0),
|
||||
!parser->isSet(Command::NoPasswordOption),
|
||||
parser->value(Command::KeyFileOption),
|
||||
parser->value(Command::YubiKeyOption),
|
||||
parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
|
||||
Utils::STDERR);
|
||||
if (!db) {
|
||||
|
|
|
|||
|
|
@ -43,6 +43,9 @@ const QCommandLineOption Merge::DryRunOption =
|
|||
QCommandLineOption(QStringList() << "dry-run",
|
||||
QObject::tr("Only print the changes detected by the merge operation."));
|
||||
|
||||
const QCommandLineOption Merge::YubiKeyFromOption(QStringList() << "yubikey-from",
|
||||
QObject::tr("Yubikey slot for the second database."),
|
||||
QObject::tr("slot"));
|
||||
Merge::Merge()
|
||||
{
|
||||
name = QString("merge");
|
||||
|
|
@ -51,6 +54,9 @@ Merge::Merge()
|
|||
options.append(Merge::KeyFileFromOption);
|
||||
options.append(Merge::NoPasswordFromOption);
|
||||
options.append(Merge::DryRunOption);
|
||||
#ifdef WITH_XC_YUBIKEY
|
||||
options.append(Merge::YubiKeyFromOption);
|
||||
#endif
|
||||
positionalArguments.append({QString("database2"), QObject::tr("Path of the database to merge from."), QString("")});
|
||||
}
|
||||
|
||||
|
|
@ -70,6 +76,7 @@ int Merge::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer
|
|||
db2 = Utils::unlockDatabase(fromDatabasePath,
|
||||
!parser->isSet(Merge::NoPasswordFromOption),
|
||||
parser->value(Merge::KeyFileFromOption),
|
||||
parser->value(Merge::YubiKeyFromOption),
|
||||
parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
|
||||
Utils::STDERR);
|
||||
if (!db2) {
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ public:
|
|||
static const QCommandLineOption SameCredentialsOption;
|
||||
static const QCommandLineOption KeyFileFromOption;
|
||||
static const QCommandLineOption NoPasswordFromOption;
|
||||
static const QCommandLineOption YubiKeyFromOption;
|
||||
static const QCommandLineOption DryRunOption;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -102,6 +102,7 @@ namespace Utils
|
|||
QSharedPointer<Database> unlockDatabase(const QString& databaseFilename,
|
||||
const bool isPasswordProtected,
|
||||
const QString& keyFilename,
|
||||
const QString& yubiKeySlot,
|
||||
FILE* outputDescriptor,
|
||||
FILE* errorDescriptor)
|
||||
{
|
||||
|
|
@ -153,6 +154,31 @@ namespace Utils
|
|||
compositeKey->addKey(fileKey);
|
||||
}
|
||||
|
||||
#ifdef WITH_XC_YUBIKEY
|
||||
if (!yubiKeySlot.isEmpty()) {
|
||||
bool ok = false;
|
||||
int slot = yubiKeySlot.toInt(&ok, 10);
|
||||
if (!ok || (slot != 1 && slot != 2)) {
|
||||
err << QObject::tr("Invalid YubiKey slot %1").arg(yubiKeySlot) << endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
QString errorMessage;
|
||||
bool blocking = YubiKey::instance()->checkSlotIsBlocking(slot, errorMessage);
|
||||
if (!errorMessage.isEmpty()) {
|
||||
err << errorMessage << endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
auto key = QSharedPointer<YkChallengeResponseKeyCLI>(new YkChallengeResponseKeyCLI(
|
||||
slot,
|
||||
blocking,
|
||||
QObject::tr("Please touch the button on your YubiKey to unlock %1").arg(databaseFilename),
|
||||
outputDescriptor));
|
||||
compositeKey->addChallengeResponseKey(key);
|
||||
}
|
||||
#endif
|
||||
|
||||
auto db = QSharedPointer<Database>::create();
|
||||
QString error;
|
||||
if (db->open(databaseFilename, compositeKey, &error, false)) {
|
||||
|
|
|
|||
|
|
@ -25,6 +25,12 @@
|
|||
#include "keys/PasswordKey.h"
|
||||
#include <QtCore/qglobal.h>
|
||||
|
||||
#ifdef WITH_XC_YUBIKEY
|
||||
#include "keys/YkChallengeResponseKey.h"
|
||||
#include "keys/YkChallengeResponseKeyCLI.h"
|
||||
#include "keys/drivers/YubiKey.h"
|
||||
#endif
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
extern FILE* STDOUT;
|
||||
|
|
@ -38,6 +44,7 @@ namespace Utils
|
|||
QSharedPointer<Database> unlockDatabase(const QString& databaseFilename,
|
||||
const bool isPasswordProtected = true,
|
||||
const QString& keyFilename = {},
|
||||
const QString& yubiKeySlot = {},
|
||||
FILE* outputDescriptor = STDOUT,
|
||||
FILE* errorDescriptor = STDERR);
|
||||
|
||||
|
|
|
|||
|
|
@ -70,6 +70,9 @@ Specifies a path to a key file for unlocking the database. In a merge operation
|
|||
.IP "--no-password"
|
||||
Deactivate password key for the database.
|
||||
|
||||
.IP "-y, --yubikey <slot>"
|
||||
Specifies a yubikey slot for unlocking the database. In a merge operation this option is used to specify the yubikey slot for the first database.
|
||||
|
||||
.IP "-q, --quiet <path>"
|
||||
Silence password prompt and other secondary outputs.
|
||||
|
||||
|
|
@ -91,6 +94,9 @@ Path of the key file for the second database.
|
|||
.IP "--no-password-from"
|
||||
Deactivate password key for the database to merge from.
|
||||
|
||||
.IP "--yubikey-from <slot>"
|
||||
Yubikey slot for the second database.
|
||||
|
||||
.IP "-s, --same-credentials"
|
||||
Use the same credentials for unlocking both database.
|
||||
|
||||
|
|
|
|||
|
|
@ -66,9 +66,9 @@ QByteArray YkChallengeResponseKey::rawKey() const
|
|||
/**
|
||||
* Assumes yubikey()->init() was called
|
||||
*/
|
||||
bool YkChallengeResponseKey::challenge(const QByteArray& challenge)
|
||||
bool YkChallengeResponseKey::challenge(const QByteArray& c)
|
||||
{
|
||||
return this->challenge(challenge, 2);
|
||||
return challenge(c, 2);
|
||||
}
|
||||
|
||||
bool YkChallengeResponseKey::challenge(const QByteArray& challenge, unsigned int retries)
|
||||
|
|
|
|||
71
src/keys/YkChallengeResponseKeyCLI.cpp
Normal file
71
src/keys/YkChallengeResponseKeyCLI.cpp
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Copyright (C) 2019 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 "keys/YkChallengeResponseKeyCLI.h"
|
||||
#include "keys/drivers/YubiKey.h"
|
||||
|
||||
#include "core/Tools.h"
|
||||
#include "crypto/CryptoHash.h"
|
||||
#include "crypto/Random.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QtCore/qglobal.h>
|
||||
|
||||
QUuid YkChallengeResponseKeyCLI::UUID("e2be77c0-c810-417a-8437-32f41d00bd1d");
|
||||
|
||||
YkChallengeResponseKeyCLI::YkChallengeResponseKeyCLI(int slot,
|
||||
bool blocking,
|
||||
QString messageInteraction,
|
||||
FILE* outputDescriptor)
|
||||
: ChallengeResponseKey(UUID)
|
||||
, m_slot(slot)
|
||||
, m_blocking(blocking)
|
||||
, m_messageInteraction(messageInteraction)
|
||||
, m_out(outputDescriptor)
|
||||
{
|
||||
}
|
||||
|
||||
QByteArray YkChallengeResponseKeyCLI::rawKey() const
|
||||
{
|
||||
return m_key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assumes yubikey()->init() was called
|
||||
*/
|
||||
bool YkChallengeResponseKeyCLI::challenge(const QByteArray& c)
|
||||
{
|
||||
return challenge(c, 2);
|
||||
}
|
||||
|
||||
bool YkChallengeResponseKeyCLI::challenge(const QByteArray& challenge, unsigned int retries)
|
||||
{
|
||||
QTextStream out(m_out, QIODevice::WriteOnly);
|
||||
do {
|
||||
--retries;
|
||||
|
||||
if (m_blocking) {
|
||||
out << m_messageInteraction << endl;
|
||||
}
|
||||
YubiKey::ChallengeResult result = YubiKey::instance()->challenge(m_slot, m_blocking, challenge, m_key);
|
||||
if (result == YubiKey::SUCCESS) {
|
||||
return true;
|
||||
}
|
||||
} while (retries > 0);
|
||||
|
||||
return false;
|
||||
}
|
||||
52
src/keys/YkChallengeResponseKeyCLI.h
Normal file
52
src/keys/YkChallengeResponseKeyCLI.h
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (C) 2019 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/>.
|
||||
*/
|
||||
|
||||
#ifndef KEEPASSX_YK_CHALLENGERESPONSEKEYCLI_H
|
||||
#define KEEPASSX_YK_CHALLENGERESPONSEKEYCLI_H
|
||||
|
||||
#include "core/Global.h"
|
||||
#include "keys/ChallengeResponseKey.h"
|
||||
#include "keys/drivers/YubiKey.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QTextStream>
|
||||
|
||||
class YkChallengeResponseKeyCLI : public QObject, public ChallengeResponseKey
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
static QUuid UUID;
|
||||
|
||||
explicit YkChallengeResponseKeyCLI(int slot,
|
||||
bool blocking,
|
||||
QString messageInteraction,
|
||||
FILE* outputDescriptor);
|
||||
|
||||
QByteArray rawKey() const override;
|
||||
bool challenge(const QByteArray& challenge) override;
|
||||
bool challenge(const QByteArray& challenge, unsigned int retries);
|
||||
|
||||
private:
|
||||
QByteArray m_key;
|
||||
int m_slot;
|
||||
bool m_blocking;
|
||||
QString m_messageInteraction;
|
||||
FILE* m_out;
|
||||
};
|
||||
|
||||
#endif // KEEPASSX_YK_CHALLENGERESPONSEKEYCLI_H
|
||||
|
|
@ -132,27 +132,17 @@ void YubiKey::detect()
|
|||
{
|
||||
bool found = false;
|
||||
|
||||
if (init()) {
|
||||
YubiKey::ChallengeResult result;
|
||||
QByteArray rand = randomGen()->randomArray(1);
|
||||
QByteArray resp;
|
||||
|
||||
// Check slot 1 and 2 for Challenge-Response HMAC capability
|
||||
for (int i = 1; i <= 2; ++i) {
|
||||
result = challenge(i, false, rand, resp);
|
||||
if (result == ALREADY_RUNNING) {
|
||||
// Try this slot again after waiting
|
||||
Tools::sleep(300);
|
||||
result = challenge(i, false, rand, resp);
|
||||
}
|
||||
|
||||
if (result != ALREADY_RUNNING && result != ERROR) {
|
||||
emit detected(i, result == WOULDBLOCK);
|
||||
found = true;
|
||||
}
|
||||
// Wait between slots to let the yubikey settle
|
||||
Tools::sleep(150);
|
||||
// Check slot 1 and 2 for Challenge-Response HMAC capability
|
||||
for (int i = 1; i <= 2; ++i) {
|
||||
QString errorMsg;
|
||||
bool isBlocking = checkSlotIsBlocking(i, errorMsg);
|
||||
if (errorMsg.isEmpty()) {
|
||||
found = true;
|
||||
emit detected(i, isBlocking);
|
||||
}
|
||||
|
||||
// Wait between slots to let the yubikey settle.
|
||||
Tools::sleep(150);
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
|
|
@ -162,6 +152,38 @@ void YubiKey::detect()
|
|||
}
|
||||
}
|
||||
|
||||
bool YubiKey::checkSlotIsBlocking(int slot, QString& errorMessage)
|
||||
{
|
||||
if (!init()) {
|
||||
errorMessage = QString("Could not initialize YubiKey.");
|
||||
return false;
|
||||
}
|
||||
|
||||
YubiKey::ChallengeResult result;
|
||||
QByteArray rand = randomGen()->randomArray(1);
|
||||
QByteArray resp;
|
||||
|
||||
result = challenge(slot, false, rand, resp);
|
||||
if (result == ALREADY_RUNNING) {
|
||||
// Try this slot again after waiting
|
||||
Tools::sleep(300);
|
||||
result = challenge(slot, false, rand, resp);
|
||||
}
|
||||
|
||||
if (result == SUCCESS || result == WOULDBLOCK) {
|
||||
return result == WOULDBLOCK;
|
||||
} else if (result == ALREADY_RUNNING) {
|
||||
errorMessage = QString("YubiKey busy");
|
||||
return false;
|
||||
} else if (result == ERROR) {
|
||||
errorMessage = QString("YubiKey error");
|
||||
return false;
|
||||
}
|
||||
|
||||
errorMessage = QString("Error while polling YubiKey");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool YubiKey::getSerial(unsigned int& serial)
|
||||
{
|
||||
m_mutex.lock();
|
||||
|
|
@ -190,14 +212,14 @@ YubiKey::ChallengeResult YubiKey::challenge(int slot, bool mayBlock, const QByte
|
|||
int yk_cmd = (slot == 1) ? SLOT_CHAL_HMAC1 : SLOT_CHAL_HMAC2;
|
||||
QByteArray paddedChallenge = challenge;
|
||||
|
||||
// yk_challenge_response() insists on 64 byte response buffer */
|
||||
// yk_challenge_response() insists on 64 bytes response buffer */
|
||||
response.clear();
|
||||
response.resize(64);
|
||||
|
||||
/* The challenge sent to the yubikey should always be 64 bytes for
|
||||
* compatibility with all configurations. Follow PKCS7 padding.
|
||||
*
|
||||
* There is some question whether or not 64 byte fixed length
|
||||
* There is some question whether or not 64 bytes fixed length
|
||||
* configurations even work, some docs say avoid it.
|
||||
*/
|
||||
const int padLen = 64 - paddedChallenge.size();
|
||||
|
|
|
|||
|
|
@ -90,6 +90,13 @@ public:
|
|||
*/
|
||||
void detect();
|
||||
|
||||
/**
|
||||
* @param slot the yubikey slot.
|
||||
* @param errorMessage populated if an error occured.
|
||||
*
|
||||
* @return whether the key is blocking or not.
|
||||
*/
|
||||
bool checkSlotIsBlocking(int slot, QString& errorMessage);
|
||||
signals:
|
||||
/** Emitted in response to detect() when a device is found
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue