2018-12-19 23:10:46 -05:00
|
|
|
/*
|
2019-06-01 17:53:40 -04:00
|
|
|
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
|
2018-12-19 23:10:46 -05:00
|
|
|
*
|
|
|
|
* 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 <cstdlib>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QString>
|
|
|
|
#include <QTextStream>
|
|
|
|
|
|
|
|
#include "Create.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
|
|
|
|
#include "core/Database.h"
|
|
|
|
|
|
|
|
#include "keys/CompositeKey.h"
|
|
|
|
#include "keys/Key.h"
|
|
|
|
|
2020-01-06 21:00:39 -05:00
|
|
|
const QCommandLineOption Create::DecryptionTimeOption =
|
|
|
|
QCommandLineOption(QStringList() << "t"
|
|
|
|
<< "decryption-time",
|
|
|
|
QObject::tr("Target decryption time in MS for the database."),
|
|
|
|
QObject::tr("time"));
|
|
|
|
|
2018-12-19 23:10:46 -05:00
|
|
|
Create::Create()
|
|
|
|
{
|
2020-01-29 12:18:48 -08:00
|
|
|
name = QString("db-create");
|
2018-12-19 23:10:46 -05:00
|
|
|
description = QObject::tr("Create a new database.");
|
2019-06-01 17:53:40 -04:00
|
|
|
positionalArguments.append({QString("database"), QObject::tr("Path of the database."), QString("")});
|
|
|
|
options.append(Command::KeyFileOption);
|
2020-01-06 21:00:39 -05:00
|
|
|
options.append(Create::DecryptionTimeOption);
|
2018-12-19 23:10:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a database file using the command line. A key file and/or
|
|
|
|
* password can be specified to encrypt the password. If none is
|
|
|
|
* specified the function will fail.
|
|
|
|
*
|
|
|
|
* If a key file is specified but it can't be loaded, the function will
|
|
|
|
* fail.
|
|
|
|
*
|
|
|
|
* If the database is being saved in a non existant directory, the
|
|
|
|
* function will fail.
|
|
|
|
*
|
|
|
|
* @return EXIT_SUCCESS on success, or EXIT_FAILURE on failure
|
|
|
|
*/
|
|
|
|
int Create::execute(const QStringList& arguments)
|
|
|
|
{
|
2019-06-01 17:53:40 -04:00
|
|
|
QSharedPointer<QCommandLineParser> parser = getCommandLineParser(arguments);
|
|
|
|
if (parser.isNull()) {
|
2018-12-19 23:10:46 -05:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-01-06 21:00:39 -05:00
|
|
|
bool quiet = parser->isSet(Command::QuietOption);
|
|
|
|
|
|
|
|
QTextStream out(quiet ? Utils::DEVNULL : Utils::STDOUT, QIODevice::WriteOnly);
|
|
|
|
QTextStream err(Utils::STDERR, QIODevice::WriteOnly);
|
|
|
|
|
2019-06-01 17:53:40 -04:00
|
|
|
const QStringList args = parser->positionalArguments();
|
|
|
|
|
2019-01-17 06:39:53 +01:00
|
|
|
const QString& databaseFilename = args.at(0);
|
2018-12-19 23:10:46 -05:00
|
|
|
if (QFileInfo::exists(databaseFilename)) {
|
|
|
|
err << QObject::tr("File %1 already exists.").arg(databaseFilename) << endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-01-06 21:00:39 -05:00
|
|
|
// Validate the decryption time before asking for a password.
|
|
|
|
QString decryptionTimeValue = parser->value(Create::DecryptionTimeOption);
|
|
|
|
int decryptionTime = 0;
|
|
|
|
if (decryptionTimeValue.length() != 0) {
|
|
|
|
decryptionTime = decryptionTimeValue.toInt();
|
|
|
|
if (decryptionTime <= 0) {
|
|
|
|
err << QObject::tr("Invalid decryption time %1.").arg(decryptionTimeValue) << endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if (decryptionTime < Kdf::MIN_ENCRYPTION_TIME || decryptionTime > Kdf::MAX_ENCRYPTION_TIME) {
|
|
|
|
err << QObject::tr("Target decryption time must be between %1 and %2.")
|
|
|
|
.arg(QString::number(Kdf::MIN_ENCRYPTION_TIME), QString::number(Kdf::MAX_ENCRYPTION_TIME))
|
|
|
|
<< endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 23:10:46 -05:00
|
|
|
auto key = QSharedPointer<CompositeKey>::create();
|
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
auto password = Utils::getPasswordFromStdin();
|
2018-12-19 23:10:46 -05:00
|
|
|
if (!password.isNull()) {
|
|
|
|
key->addKey(password);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSharedPointer<FileKey> fileKey;
|
2019-06-01 17:53:40 -04:00
|
|
|
if (parser->isSet(Command::KeyFileOption)) {
|
|
|
|
if (!loadFileKey(parser->value(Command::KeyFileOption), fileKey)) {
|
2018-12-19 23:10:46 -05:00
|
|
|
err << QObject::tr("Loading the key file failed") << endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fileKey.isNull()) {
|
|
|
|
key->addKey(fileKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key->isEmpty()) {
|
|
|
|
err << QObject::tr("No key is set. Aborting database creation.") << endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-09-13 09:49:03 -04:00
|
|
|
QSharedPointer<Database> db(new Database);
|
|
|
|
db->setKey(key);
|
2018-12-19 23:10:46 -05:00
|
|
|
|
2020-01-06 21:00:39 -05:00
|
|
|
if (decryptionTime != 0) {
|
|
|
|
auto kdf = db->kdf();
|
|
|
|
Q_ASSERT(kdf);
|
|
|
|
|
|
|
|
out << QObject::tr("Benchmarking key derivation function for %1ms delay.").arg(decryptionTimeValue) << endl;
|
|
|
|
int rounds = kdf->benchmark(decryptionTime);
|
|
|
|
out << QObject::tr("Setting %1 rounds for key derivation function.").arg(QString::number(rounds)) << endl;
|
|
|
|
kdf->setRounds(rounds);
|
|
|
|
|
|
|
|
bool ok = db->changeKdf(kdf);
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
err << QObject::tr("Error while setting database key derivation settings.") << endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 23:10:46 -05:00
|
|
|
QString errorMessage;
|
2019-10-20 18:38:52 -04:00
|
|
|
if (!db->saveAs(databaseFilename, &errorMessage, true, false)) {
|
2018-12-19 23:10:46 -05:00
|
|
|
err << QObject::tr("Failed to save the database: %1.").arg(errorMessage) << endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
out << QObject::tr("Successfully created new database.") << endl;
|
2019-09-13 09:49:03 -04:00
|
|
|
currentDatabase = db;
|
2018-12-19 23:10:46 -05:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a key file from disk. When the path specified does not exist a
|
|
|
|
* new file will be generated. No folders will be generated so the parent
|
|
|
|
* folder of the specified file nees to exist
|
|
|
|
*
|
|
|
|
* If the key file cannot be loaded or created the function will fail.
|
|
|
|
*
|
|
|
|
* @param path Path to the key file to be loaded
|
|
|
|
* @param fileKey Resulting fileKey
|
|
|
|
* @return true if the key file was loaded succesfully
|
|
|
|
*/
|
2019-01-17 06:39:53 +01:00
|
|
|
bool Create::loadFileKey(const QString& path, QSharedPointer<FileKey>& fileKey)
|
2018-12-19 23:10:46 -05:00
|
|
|
{
|
|
|
|
QTextStream err(Utils::STDERR, QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
QString error;
|
|
|
|
fileKey = QSharedPointer<FileKey>(new FileKey());
|
|
|
|
|
|
|
|
if (!QFileInfo::exists(path)) {
|
|
|
|
fileKey->create(path, &error);
|
|
|
|
|
|
|
|
if (!error.isEmpty()) {
|
|
|
|
err << QObject::tr("Creating KeyFile %1 failed: %2").arg(path, error) << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fileKey->load(path, &error)) {
|
|
|
|
err << QObject::tr("Loading KeyFile %1 failed: %2").arg(path, error) << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|