keepassxc

Форк
0
/
KeyFileEditWidget.cpp 
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

28
KeyFileEditWidget::KeyFileEditWidget(DatabaseSettingsWidget* parent)
29
    : KeyComponentWidget(parent)
30
    , m_compUi(new Ui::KeyFileEditWidget())
31
    , m_parent(parent)
32
{
33
    initComponent();
34
}
35

36
KeyFileEditWidget::~KeyFileEditWidget() = default;
37

38
bool KeyFileEditWidget::addToCompositeKey(QSharedPointer<CompositeKey> key)
39
{
40
    auto fileKey = QSharedPointer<FileKey>::create();
41
    QString fileKeyName = m_compUi->keyFileLineEdit->text();
42
    if (!fileKey->load(fileKeyName, nullptr)) {
43
        return false;
44
    }
45

46
    if (fileKey->type() != FileKey::KeePass2XMLv2 && fileKey->type() != FileKey::Hashed) {
47
        QMessageBox::warning(getMainWindow(),
48
                             tr("Old key file format"),
49
                             tr("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."),
52
                             QMessageBox::Ok);
53
    }
54

55
    key->addKey(fileKey);
56
    return true;
57
}
58

59
bool KeyFileEditWidget::validate(QString& errorMessage) const
60
{
61
    FileKey fileKey;
62
    QString fileKeyError;
63
    QString fileKeyName = m_compUi->keyFileLineEdit->text();
64
    if (!fileKey.load(fileKeyName, &fileKeyError)) {
65
        errorMessage = tr("Error loading the key file '%1'\nMessage: %2").arg(fileKeyName, fileKeyError);
66
        return false;
67
    }
68
    return true;
69
}
70

71
QWidget* KeyFileEditWidget::componentEditWidget()
72
{
73
    m_compEditWidget = new QWidget();
74
    m_compUi->setupUi(m_compEditWidget);
75

76
    connect(m_compUi->createKeyFileButton, SIGNAL(clicked()), SLOT(createKeyFile()));
77
    connect(m_compUi->browseKeyFileButton, SIGNAL(clicked()), SLOT(browseKeyFile()));
78

79
    return m_compEditWidget;
80
}
81

82
void KeyFileEditWidget::initComponentEditWidget(QWidget* widget)
83
{
84
    Q_UNUSED(widget);
85
    Q_ASSERT(m_compEditWidget);
86
    m_compUi->keyFileLineEdit->setFocus();
87
}
88

89
void KeyFileEditWidget::initComponent()
90
{
91
    // These need to be set in total for each credential type for translation purposes
92
    m_ui->groupBox->setTitle(tr("Key File"));
93
    m_ui->addButton->setText(tr("Add Key File"));
94
    m_ui->changeButton->setText(tr("Change Key File"));
95
    m_ui->removeButton->setText(tr("Remove Key File"));
96
    m_ui->changeOrRemoveLabel->setText(tr("Key File set, click to change or remove"));
97

98
    m_ui->componentDescription->setText(
99
        tr("<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

103
void KeyFileEditWidget::createKeyFile()
104
{
105
    Q_ASSERT(m_compEditWidget);
106
    if (!m_compEditWidget) {
107
        return;
108
    }
109
    QString filters = QString("%1 (*.keyx *.key);;%2 (*)").arg(tr("Key files"), tr("All files"));
110
    QString fileName = fileDialog()->getSaveFileName(this, tr("Create Key File…"), QString(), filters);
111

112
    if (!fileName.isEmpty()) {
113
        QString errorMsg;
114
        bool created = FileKey::create(fileName, &errorMsg);
115
        if (!created) {
116
            MessageBox::critical(getMainWindow(),
117
                                 tr("Error creating key file"),
118
                                 tr("Unable to create key file: %1").arg(errorMsg),
119
                                 QMessageBox::Button::Ok);
120
        } else {
121
            m_compUi->keyFileLineEdit->setText(fileName);
122
        }
123
    }
124
}
125

126
void KeyFileEditWidget::browseKeyFile()
127
{
128
    Q_ASSERT(m_compEditWidget);
129
    if (!m_compEditWidget) {
130
        return;
131
    }
132
    QString filters = QString("%1 (*.keyx *.key);;%2 (*)").arg(tr("Key files"), tr("All files"));
133
    QString fileName = fileDialog()->getOpenFileName(this, tr("Select a key file"), QString(), filters);
134

135
    if (QFileInfo(fileName).canonicalFilePath() == m_parent->getDatabase()->canonicalFilePath()) {
136
        MessageBox::critical(getMainWindow(),
137
                             tr("Invalid Key File"),
138
                             tr("You cannot use the current database as its own keyfile. Please choose a different "
139
                                "file or generate a new key file."));
140
        return;
141
    } else if (fileName.endsWith(".kdbx", Qt::CaseInsensitive)) {
142
        auto response =
143
            MessageBox::warning(getMainWindow(),
144
                                tr("Suspicious Key File"),
145
                                tr("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?"),
148
                                MessageBox::Continue | MessageBox::Cancel,
149
                                MessageBox::Cancel);
150
        if (response != MessageBox::Continue) {
151
            return;
152
        }
153
    }
154

155
    if (!fileName.isEmpty()) {
156
        m_compUi->keyFileLineEdit->setText(fileName);
157
    }
158
}
159

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.