keepassxc
158 строк · 5.8 Кб
1/*
2* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
3*
4* This program is free software: you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation, either version 2 or (at your option)
7* version 3 of the License.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "KeyFileEditWidget.h"19#include "ui_KeyComponentWidget.h"20#include "ui_KeyFileEditWidget.h"21
22#include "gui/FileDialog.h"23#include "gui/MainWindow.h"24#include "gui/MessageBox.h"25#include "gui/dbsettings/DatabaseSettingsWidget.h"26#include "keys/FileKey.h"27
28KeyFileEditWidget::KeyFileEditWidget(DatabaseSettingsWidget* parent)29: KeyComponentWidget(parent)30, m_compUi(new Ui::KeyFileEditWidget())31, m_parent(parent)32{
33initComponent();34}
35
36KeyFileEditWidget::~KeyFileEditWidget() = default;37
38bool KeyFileEditWidget::addToCompositeKey(QSharedPointer<CompositeKey> key)39{
40auto fileKey = QSharedPointer<FileKey>::create();41QString fileKeyName = m_compUi->keyFileLineEdit->text();42if (!fileKey->load(fileKeyName, nullptr)) {43return false;44}45
46if (fileKey->type() != FileKey::KeePass2XMLv2 && fileKey->type() != FileKey::Hashed) {47QMessageBox::warning(getMainWindow(),48tr("Old key file format"),49tr("You selected a key file in an old format which KeePassXC<br>"50"may stop supporting in the future.<br><br>"51"Please consider generating a new key file instead."),52QMessageBox::Ok);53}54
55key->addKey(fileKey);56return true;57}
58
59bool KeyFileEditWidget::validate(QString& errorMessage) const60{
61FileKey fileKey;62QString fileKeyError;63QString fileKeyName = m_compUi->keyFileLineEdit->text();64if (!fileKey.load(fileKeyName, &fileKeyError)) {65errorMessage = tr("Error loading the key file '%1'\nMessage: %2").arg(fileKeyName, fileKeyError);66return false;67}68return true;69}
70
71QWidget* KeyFileEditWidget::componentEditWidget()72{
73m_compEditWidget = new QWidget();74m_compUi->setupUi(m_compEditWidget);75
76connect(m_compUi->createKeyFileButton, SIGNAL(clicked()), SLOT(createKeyFile()));77connect(m_compUi->browseKeyFileButton, SIGNAL(clicked()), SLOT(browseKeyFile()));78
79return m_compEditWidget;80}
81
82void KeyFileEditWidget::initComponentEditWidget(QWidget* widget)83{
84Q_UNUSED(widget);85Q_ASSERT(m_compEditWidget);86m_compUi->keyFileLineEdit->setFocus();87}
88
89void KeyFileEditWidget::initComponent()90{
91// These need to be set in total for each credential type for translation purposes92m_ui->groupBox->setTitle(tr("Key File"));93m_ui->addButton->setText(tr("Add Key File"));94m_ui->changeButton->setText(tr("Change Key File"));95m_ui->removeButton->setText(tr("Remove Key File"));96m_ui->changeOrRemoveLabel->setText(tr("Key File set, click to change or remove"));97
98m_ui->componentDescription->setText(99tr("<p>You can add a key file containing random bytes for additional security.</p>"100"<p>You must keep it secret and never lose it or you will be locked out.</p>"));101}
102
103void KeyFileEditWidget::createKeyFile()104{
105Q_ASSERT(m_compEditWidget);106if (!m_compEditWidget) {107return;108}109QString filters = QString("%1 (*.keyx *.key);;%2 (*)").arg(tr("Key files"), tr("All files"));110QString fileName = fileDialog()->getSaveFileName(this, tr("Create Key File…"), QString(), filters);111
112if (!fileName.isEmpty()) {113QString errorMsg;114bool created = FileKey::create(fileName, &errorMsg);115if (!created) {116MessageBox::critical(getMainWindow(),117tr("Error creating key file"),118tr("Unable to create key file: %1").arg(errorMsg),119QMessageBox::Button::Ok);120} else {121m_compUi->keyFileLineEdit->setText(fileName);122}123}124}
125
126void KeyFileEditWidget::browseKeyFile()127{
128Q_ASSERT(m_compEditWidget);129if (!m_compEditWidget) {130return;131}132QString filters = QString("%1 (*.keyx *.key);;%2 (*)").arg(tr("Key files"), tr("All files"));133QString fileName = fileDialog()->getOpenFileName(this, tr("Select a key file"), QString(), filters);134
135if (QFileInfo(fileName).canonicalFilePath() == m_parent->getDatabase()->canonicalFilePath()) {136MessageBox::critical(getMainWindow(),137tr("Invalid Key File"),138tr("You cannot use the current database as its own keyfile. Please choose a different "139"file or generate a new key file."));140return;141} else if (fileName.endsWith(".kdbx", Qt::CaseInsensitive)) {142auto response =143MessageBox::warning(getMainWindow(),144tr("Suspicious Key File"),145tr("The chosen key file looks like a password database file. A key file must be a "146"static file that never changes or you will lose access to your database "147"forever.\nAre you sure you want to continue with this file?"),148MessageBox::Continue | MessageBox::Cancel,149MessageBox::Cancel);150if (response != MessageBox::Continue) {151return;152}153}154
155if (!fileName.isEmpty()) {156m_compUi->keyFileLineEdit->setText(fileName);157}158}
159