Merge pull request #160 from louib/feature/updateKdbxExtract

Fixes #156 : update kdbx-extract
This commit is contained in:
Janek Bevendorff 2017-01-14 22:26:23 +02:00 committed by GitHub
commit 9dadafe20a
7 changed files with 66 additions and 47 deletions

View file

@ -18,12 +18,15 @@
#include "CompositeKey.h" #include "CompositeKey.h"
#include "CompositeKey_p.h" #include "CompositeKey_p.h"
#include <QtConcurrent>
#include <QElapsedTimer> #include <QElapsedTimer>
#include <QFile>
#include <QtConcurrent>
#include "core/Global.h" #include "core/Global.h"
#include "crypto/CryptoHash.h" #include "crypto/CryptoHash.h"
#include "crypto/SymmetricCipher.h" #include "crypto/SymmetricCipher.h"
#include "keys/FileKey.h"
#include "keys/PasswordKey.h"
CompositeKey::CompositeKey() CompositeKey::CompositeKey()
{ {
@ -71,6 +74,29 @@ CompositeKey& CompositeKey::operator=(const CompositeKey& key)
return *this; return *this;
} }
/*
* Read a key from a line of input.
* If the line references a valid file
* path, the key is loaded from file.
*/
CompositeKey CompositeKey::readFromLine(QString line)
{
CompositeKey key;
if (QFile::exists(line)) {
FileKey fileKey;
fileKey.load(line);
key.addKey(fileKey);
}
else {
PasswordKey password;
password.setPassword(line);
key.addKey(password);
}
return key;
}
QByteArray CompositeKey::rawKey() const QByteArray CompositeKey::rawKey() const
{ {
CryptoHash cryptoHash(CryptoHash::Sha256); CryptoHash cryptoHash(CryptoHash::Sha256);

View file

@ -19,6 +19,7 @@
#define KEEPASSX_COMPOSITEKEY_H #define KEEPASSX_COMPOSITEKEY_H
#include <QList> #include <QList>
#include <QString>
#include "keys/Key.h" #include "keys/Key.h"
@ -39,6 +40,7 @@ public:
void addKey(const Key& key); void addKey(const Key& key);
static int transformKeyBenchmark(int msec); static int transformKeyBenchmark(int msec);
static CompositeKey readFromLine(QString line);
private: private:
static QByteArray transformKeyRaw(const QByteArray& key, const QByteArray& seed, static QByteArray transformKeyRaw(const QByteArray& key, const QByteArray& seed,

View file

@ -83,6 +83,22 @@ void TestKeys::testComposite()
delete compositeKey4; delete compositeKey4;
} }
void TestKeys::testCompositeKeyReadFromLine()
{
QString keyFilename = QString("%1/FileKeyXml.key").arg(QString(KEEPASSX_TEST_DATA_DIR));
CompositeKey compositeFileKey = CompositeKey::readFromLine(keyFilename);
FileKey fileKey;
fileKey.load(keyFilename);
QCOMPARE(compositeFileKey.rawKey().size(), fileKey.rawKey().size());
CompositeKey compositePasswordKey = CompositeKey::readFromLine(QString("password"));
PasswordKey passwordKey(QString("password"));
QCOMPARE(compositePasswordKey.rawKey().size(), passwordKey.rawKey().size());
}
void TestKeys::testFileKey() void TestKeys::testFileKey()
{ {
QFETCH(QString, type); QFETCH(QString, type);

View file

@ -27,6 +27,7 @@ class TestKeys : public QObject
private Q_SLOTS: private Q_SLOTS:
void initTestCase(); void initTestCase();
void testComposite(); void testComposite();
void testCompositeKeyReadFromLine();
void testFileKey(); void testFileKey();
void testFileKey_data(); void testFileKey_data();
void testCreateFileKey(); void testCreateFileKey();

View file

@ -18,10 +18,7 @@ include_directories(../src)
add_executable(kdbx-extract kdbx-extract.cpp) add_executable(kdbx-extract kdbx-extract.cpp)
target_link_libraries(kdbx-extract target_link_libraries(kdbx-extract
keepassx_core keepassx_core
${MHD_LIBRARIES}
Qt5::Core Qt5::Core
Qt5::Concurrent
Qt5::Widgets
${GCRYPT_LIBRARIES} ${GCRYPT_LIBRARIES}
${ZLIB_LIBRARIES}) ${ZLIB_LIBRARIES})

View file

@ -17,6 +17,7 @@
#include <stdio.h> #include <stdio.h>
#include <QCommandLineParser>
#include <QCoreApplication> #include <QCoreApplication>
#include <QFile> #include <QFile>
#include <QStringList> #include <QStringList>
@ -33,8 +34,16 @@ int main(int argc, char **argv)
{ {
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
if (app.arguments().size() != 3) { QCommandLineParser parser;
qCritical("Usage: kdbx-extract <password/key file> <kdbx file>"); parser.setApplicationDescription(QCoreApplication::translate("main",
"Extract and print a KeePassXC database file."));
parser.addPositionalArgument("database", QCoreApplication::translate("main", "path of the database to extract."));
parser.addHelpOption();
parser.process(app);
const QStringList args = parser.positionalArguments();
if (args.size() != 1) {
parser.showHelp();
return 1; return 1;
} }
@ -42,25 +51,18 @@ int main(int argc, char **argv)
qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString())); qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString()));
} }
CompositeKey key; static QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
if (QFile::exists(app.arguments().at(1))) { QString line = inputTextStream.readLine();
FileKey fileKey; CompositeKey key = CompositeKey::readFromLine(line);
fileKey.load(app.arguments().at(1));
key.addKey(fileKey);
}
else {
PasswordKey password;
password.setPassword(app.arguments().at(1));
key.addKey(password);
}
QFile dbFile(app.arguments().at(2)); QString databaseFilename = args.at(0);
QFile dbFile(databaseFilename);
if (!dbFile.exists()) { if (!dbFile.exists()) {
qCritical("File does not exist."); qCritical("File %s does not exist.", qPrintable(databaseFilename));
return 1; return 1;
} }
if (!dbFile.open(QIODevice::ReadOnly)) { if (!dbFile.open(QIODevice::ReadOnly)) {
qCritical("Unable to open file."); qCritical("Unable to open file %s.", qPrintable(databaseFilename));
return 1; return 1;
} }

View file

@ -29,31 +29,6 @@
#include "format/KeePass2Reader.h" #include "format/KeePass2Reader.h"
#include "format/KeePass2Writer.h" #include "format/KeePass2Writer.h"
#include "keys/CompositeKey.h" #include "keys/CompositeKey.h"
#include "keys/FileKey.h"
#include "keys/PasswordKey.h"
/*
* Read a key from a line of input.
* If the line references a valid file
* path, the key is loaded from file.
*/
CompositeKey readKeyFromLine(QString line)
{
CompositeKey key;
if (QFile::exists(line)) {
FileKey fileKey;
fileKey.load(line);
key.addKey(fileKey);
}
else {
PasswordKey password;
password.setPassword(line);
key.addKey(password);
}
return key;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -85,7 +60,7 @@ int main(int argc, char **argv)
static QTextStream inputTextStream(stdin, QIODevice::ReadOnly); static QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
QString line1 = inputTextStream.readLine(); QString line1 = inputTextStream.readLine();
CompositeKey key1 = readKeyFromLine(line1); CompositeKey key1 = CompositeKey::readFromLine(line1);
CompositeKey key2; CompositeKey key2;
if (parser.isSet("same-password")) { if (parser.isSet("same-password")) {
@ -93,7 +68,7 @@ int main(int argc, char **argv)
} }
else { else {
QString line2 = inputTextStream.readLine(); QString line2 = inputTextStream.readLine();
key2 = readKeyFromLine(line2); key2 = CompositeKey::readFromLine(line2);
} }