2018-05-13 17:21:43 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 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 "KeyFileEditWidget.h"
|
2021-02-14 16:59:22 -05:00
|
|
|
#include "ui_KeyComponentWidget.h"
|
2018-05-13 17:21:43 -04:00
|
|
|
#include "ui_KeyFileEditWidget.h"
|
2018-10-31 23:27:38 -04:00
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
#include "gui/FileDialog.h"
|
2018-10-31 23:27:38 -04:00
|
|
|
#include "gui/MainWindow.h"
|
2018-05-13 17:21:43 -04:00
|
|
|
#include "gui/MessageBox.h"
|
2021-02-14 16:59:22 -05:00
|
|
|
#include "gui/dbsettings/DatabaseSettingsWidget.h"
|
2018-05-13 17:21:43 -04:00
|
|
|
#include "keys/FileKey.h"
|
|
|
|
|
2019-10-16 09:11:47 -04:00
|
|
|
KeyFileEditWidget::KeyFileEditWidget(DatabaseSettingsWidget* parent)
|
2018-05-13 17:21:43 -04:00
|
|
|
: KeyComponentWidget(parent)
|
|
|
|
, m_compUi(new Ui::KeyFileEditWidget())
|
2019-10-16 09:11:47 -04:00
|
|
|
, m_parent(parent)
|
2018-05-13 17:21:43 -04:00
|
|
|
{
|
2021-02-14 16:59:22 -05:00
|
|
|
initComponent();
|
2018-05-13 17:21:43 -04:00
|
|
|
}
|
|
|
|
|
2023-01-29 15:47:13 -05:00
|
|
|
KeyFileEditWidget::~KeyFileEditWidget() = default;
|
2018-05-13 17:21:43 -04:00
|
|
|
|
|
|
|
bool KeyFileEditWidget::addToCompositeKey(QSharedPointer<CompositeKey> key)
|
|
|
|
{
|
|
|
|
auto fileKey = QSharedPointer<FileKey>::create();
|
2021-04-11 16:48:35 -04:00
|
|
|
QString fileKeyName = m_compUi->keyFileLineEdit->text();
|
2018-05-13 17:21:43 -04:00
|
|
|
if (!fileKey->load(fileKeyName, nullptr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-09 19:28:01 -05:00
|
|
|
if (fileKey->type() != FileKey::KeePass2XMLv2 && fileKey->type() != FileKey::Hashed) {
|
2018-11-01 21:33:27 -04:00
|
|
|
QMessageBox::warning(getMainWindow(),
|
2020-12-09 19:28:01 -05:00
|
|
|
tr("Old key file format"),
|
|
|
|
tr("You selected a key file in an old format which KeePassXC<br>"
|
|
|
|
"may stop supporting in the future.<br><br>"
|
|
|
|
"Please consider generating a new key file instead."),
|
2018-05-13 17:21:43 -04:00
|
|
|
QMessageBox::Ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
key->addKey(fileKey);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KeyFileEditWidget::validate(QString& errorMessage) const
|
|
|
|
{
|
|
|
|
FileKey fileKey;
|
|
|
|
QString fileKeyError;
|
2021-04-11 16:48:35 -04:00
|
|
|
QString fileKeyName = m_compUi->keyFileLineEdit->text();
|
2018-05-13 17:21:43 -04:00
|
|
|
if (!fileKey.load(fileKeyName, &fileKeyError)) {
|
|
|
|
errorMessage = tr("Error loading the key file '%1'\nMessage: %2").arg(fileKeyName, fileKeyError);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget* KeyFileEditWidget::componentEditWidget()
|
|
|
|
{
|
|
|
|
m_compEditWidget = new QWidget();
|
|
|
|
m_compUi->setupUi(m_compEditWidget);
|
|
|
|
|
|
|
|
connect(m_compUi->createKeyFileButton, SIGNAL(clicked()), SLOT(createKeyFile()));
|
|
|
|
connect(m_compUi->browseKeyFileButton, SIGNAL(clicked()), SLOT(browseKeyFile()));
|
|
|
|
|
|
|
|
return m_compEditWidget;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyFileEditWidget::initComponentEditWidget(QWidget* widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(widget);
|
|
|
|
Q_ASSERT(m_compEditWidget);
|
2021-04-11 16:48:35 -04:00
|
|
|
m_compUi->keyFileLineEdit->setFocus();
|
2018-05-13 17:21:43 -04:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:59:22 -05:00
|
|
|
void KeyFileEditWidget::initComponent()
|
|
|
|
{
|
|
|
|
// These need to be set in total for each credential type for translation purposes
|
|
|
|
m_ui->groupBox->setTitle(tr("Key File"));
|
|
|
|
m_ui->addButton->setText(tr("Add Key File"));
|
|
|
|
m_ui->changeButton->setText(tr("Change Key File"));
|
|
|
|
m_ui->removeButton->setText(tr("Remove Key File"));
|
|
|
|
m_ui->changeOrRemoveLabel->setText(tr("Key File set, click to change or remove"));
|
|
|
|
|
|
|
|
m_ui->componentDescription->setText(
|
|
|
|
tr("<p>You can add a key file containing random bytes for additional security.</p>"
|
|
|
|
"<p>You must keep it secret and never lose it or you will be locked out.</p>"));
|
|
|
|
}
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
void KeyFileEditWidget::createKeyFile()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_compEditWidget);
|
|
|
|
if (!m_compEditWidget) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-02 18:41:39 -04:00
|
|
|
QString filters = QString("%1 (*.keyx *.key);;%2 (*)").arg(tr("Key files"), tr("All files"));
|
2021-02-14 19:15:10 -05:00
|
|
|
QString fileName = fileDialog()->getSaveFileName(this, tr("Create Key File…"), QString(), filters);
|
2018-05-13 17:21:43 -04:00
|
|
|
|
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
QString errorMsg;
|
|
|
|
bool created = FileKey::create(fileName, &errorMsg);
|
|
|
|
if (!created) {
|
2018-10-31 23:27:38 -04:00
|
|
|
MessageBox::critical(getMainWindow(),
|
|
|
|
tr("Error creating key file"),
|
|
|
|
tr("Unable to create key file: %1").arg(errorMsg),
|
|
|
|
QMessageBox::Button::Ok);
|
2018-05-13 17:21:43 -04:00
|
|
|
} else {
|
2021-04-11 16:48:35 -04:00
|
|
|
m_compUi->keyFileLineEdit->setText(fileName);
|
2018-05-13 17:21:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyFileEditWidget::browseKeyFile()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_compEditWidget);
|
|
|
|
if (!m_compEditWidget) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-02 18:41:39 -04:00
|
|
|
QString filters = QString("%1 (*.keyx *.key);;%2 (*)").arg(tr("Key files"), tr("All files"));
|
2018-05-13 17:21:43 -04:00
|
|
|
QString fileName = fileDialog()->getOpenFileName(this, tr("Select a key file"), QString(), filters);
|
|
|
|
|
2019-10-16 09:11:47 -04:00
|
|
|
if (QFileInfo(fileName).canonicalFilePath() == m_parent->getDatabase()->canonicalFilePath()) {
|
|
|
|
MessageBox::critical(getMainWindow(),
|
|
|
|
tr("Invalid Key File"),
|
|
|
|
tr("You cannot use the current database as its own keyfile. Please choose a different "
|
|
|
|
"file or generate a new key file."));
|
|
|
|
return;
|
|
|
|
} else if (fileName.endsWith(".kdbx", Qt::CaseInsensitive)) {
|
|
|
|
auto response =
|
|
|
|
MessageBox::warning(getMainWindow(),
|
|
|
|
tr("Suspicious Key File"),
|
|
|
|
tr("The chosen key file looks like a password database file. A key file must be a "
|
|
|
|
"static file that never changes or you will lose access to your database "
|
|
|
|
"forever.\nAre you sure you want to continue with this file?"),
|
|
|
|
MessageBox::Continue | MessageBox::Cancel,
|
|
|
|
MessageBox::Cancel);
|
|
|
|
if (response != MessageBox::Continue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 17:21:43 -04:00
|
|
|
if (!fileName.isEmpty()) {
|
2021-04-11 16:48:35 -04:00
|
|
|
m_compUi->keyFileLineEdit->setText(fileName);
|
2018-05-13 17:21:43 -04:00
|
|
|
}
|
|
|
|
}
|