2012-01-13 17:52:37 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* 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 "ChangeMasterKeyWidget.h"
|
|
|
|
#include "ui_ChangeMasterKeyWidget.h"
|
|
|
|
|
2014-01-12 17:08:36 +01:00
|
|
|
#include "core/FilePath.h"
|
2012-01-13 17:52:37 +01:00
|
|
|
#include "keys/FileKey.h"
|
|
|
|
#include "keys/PasswordKey.h"
|
2014-05-26 00:49:28 -07:00
|
|
|
#include "keys/YkChallengeResponseKey.h"
|
2012-06-10 17:26:35 +01:00
|
|
|
#include "gui/FileDialog.h"
|
2013-10-08 22:09:20 +02:00
|
|
|
#include "gui/MessageBox.h"
|
2014-05-26 00:49:28 -07:00
|
|
|
#include "crypto/Random.h"
|
2017-02-24 03:25:08 +01:00
|
|
|
#include "MainWindow.h"
|
2012-01-13 17:52:37 +01:00
|
|
|
|
2017-02-21 01:06:32 +01:00
|
|
|
#include "config-keepassx.h"
|
|
|
|
|
2017-02-23 23:52:36 +01:00
|
|
|
#include <QtConcurrentRun>
|
|
|
|
#include <QSharedPointer>
|
|
|
|
|
2012-01-13 17:52:37 +01:00
|
|
|
ChangeMasterKeyWidget::ChangeMasterKeyWidget(QWidget* parent)
|
2012-04-24 13:23:09 +02:00
|
|
|
: DialogyWidget(parent)
|
2012-01-13 17:52:37 +01:00
|
|
|
, m_ui(new Ui::ChangeMasterKeyWidget())
|
|
|
|
{
|
|
|
|
m_ui->setupUi(this);
|
|
|
|
|
2015-01-19 23:27:57 +00:00
|
|
|
m_ui->messageWidget->setHidden(true);
|
|
|
|
|
2017-02-20 23:20:32 +01:00
|
|
|
m_ui->togglePasswordButton->setIcon(filePath()->onOffIcon("actions", "password-show"));
|
|
|
|
m_ui->repeatPasswordEdit->enableVerifyMode(m_ui->enterPasswordEdit);
|
|
|
|
|
2017-02-21 01:28:50 +01:00
|
|
|
connect(m_ui->passwordGroup, SIGNAL(clicked(bool)), SLOT(setOkEnabled()));
|
2014-01-12 17:08:36 +01:00
|
|
|
connect(m_ui->togglePasswordButton, SIGNAL(toggled(bool)), m_ui->enterPasswordEdit, SLOT(setShowPassword(bool)));
|
2017-02-21 01:28:50 +01:00
|
|
|
|
|
|
|
connect(m_ui->keyFileGroup, SIGNAL(clicked(bool)), SLOT(setOkEnabled()));
|
2012-06-10 17:26:35 +01:00
|
|
|
connect(m_ui->createKeyFileButton, SIGNAL(clicked()), SLOT(createKeyFile()));
|
2012-06-10 17:40:05 +01:00
|
|
|
connect(m_ui->browseKeyFileButton, SIGNAL(clicked()), SLOT(browseKeyFile()));
|
2017-02-21 01:28:50 +01:00
|
|
|
connect(m_ui->keyFileCombo, SIGNAL(editTextChanged(QString)), SLOT(setOkEnabled()));
|
|
|
|
|
|
|
|
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(generateKey()));
|
|
|
|
connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(reject()));
|
2017-01-14 17:36:14 -08:00
|
|
|
|
2017-02-21 01:06:32 +01:00
|
|
|
#ifdef WITH_XC_YUBIKEY
|
2017-02-21 01:28:50 +01:00
|
|
|
m_ui->yubikeyProgress->setVisible(false);
|
|
|
|
QSizePolicy sp = m_ui->yubikeyProgress->sizePolicy();
|
|
|
|
sp.setRetainSizeWhenHidden(true);
|
|
|
|
m_ui->yubikeyProgress->setSizePolicy(sp);
|
|
|
|
|
2017-02-20 22:41:39 +01:00
|
|
|
connect(m_ui->challengeResponseGroup, SIGNAL(clicked(bool)), SLOT(challengeResponseGroupToggled(bool)));
|
2017-02-21 01:28:50 +01:00
|
|
|
connect(m_ui->challengeResponseGroup, SIGNAL(clicked(bool)), SLOT(setOkEnabled()));
|
2017-02-20 22:41:39 +01:00
|
|
|
connect(m_ui->buttonRedetectYubikey, SIGNAL(clicked()), SLOT(pollYubikey()));
|
|
|
|
|
|
|
|
connect(YubiKey::instance(), SIGNAL(detected(int,bool)), SLOT(yubikeyDetected(int,bool)), Qt::QueuedConnection);
|
|
|
|
connect(YubiKey::instance(), SIGNAL(notFound()), SLOT(noYubikeyFound()), Qt::QueuedConnection);
|
2017-02-21 01:06:32 +01:00
|
|
|
#else
|
|
|
|
m_ui->challengeResponseGroup->setVisible(false);
|
|
|
|
#endif
|
2012-01-13 17:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ChangeMasterKeyWidget::~ChangeMasterKeyWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-10 17:26:35 +01:00
|
|
|
void ChangeMasterKeyWidget::createKeyFile()
|
|
|
|
{
|
2012-06-11 18:39:16 +02:00
|
|
|
QString filters = QString("%1 (*.key);;%2 (*)").arg(tr("Key files"), tr("All files"));
|
|
|
|
QString fileName = fileDialog()->getSaveFileName(this, tr("Create Key File..."), QString(), filters);
|
2012-06-10 17:26:35 +01:00
|
|
|
|
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
QString errorMsg;
|
|
|
|
bool created = FileKey::create(fileName, &errorMsg);
|
2012-06-11 18:39:16 +02:00
|
|
|
if (!created) {
|
2015-01-21 16:19:05 +00:00
|
|
|
m_ui->messageWidget->showMessage(tr("Unable to create Key File : ").append(errorMsg), MessageWidget::Error);
|
2012-06-10 17:26:35 +01:00
|
|
|
}
|
2012-06-11 18:39:16 +02:00
|
|
|
else {
|
|
|
|
m_ui->keyFileCombo->setEditText(fileName);
|
2012-06-10 17:26:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-10 17:40:05 +01:00
|
|
|
void ChangeMasterKeyWidget::browseKeyFile()
|
|
|
|
{
|
2012-06-11 18:39:16 +02:00
|
|
|
QString filters = QString("%1 (*.key);;%2 (*)").arg(tr("Key files"), tr("All files"));
|
|
|
|
QString fileName = fileDialog()->getOpenFileName(this, tr("Select a key file"), QString(), filters);
|
2012-06-10 17:40:05 +01:00
|
|
|
|
2012-06-11 18:39:16 +02:00
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
m_ui->keyFileCombo->setEditText(fileName);
|
2012-06-10 17:40:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-13 17:52:37 +01:00
|
|
|
void ChangeMasterKeyWidget::clearForms()
|
|
|
|
{
|
|
|
|
m_key.clear();
|
|
|
|
|
2014-01-13 00:13:10 +01:00
|
|
|
m_ui->passwordGroup->setChecked(true);
|
2012-01-13 17:52:37 +01:00
|
|
|
m_ui->enterPasswordEdit->setText("");
|
|
|
|
m_ui->repeatPasswordEdit->setText("");
|
|
|
|
m_ui->keyFileGroup->setChecked(false);
|
2014-01-13 00:13:10 +01:00
|
|
|
m_ui->togglePasswordButton->setChecked(false);
|
2012-04-23 00:18:10 +02:00
|
|
|
|
2017-02-21 01:06:32 +01:00
|
|
|
#ifdef WITH_XC_YUBIKEY
|
2014-05-26 00:49:28 -07:00
|
|
|
m_ui->challengeResponseGroup->setChecked(false);
|
2017-02-20 22:41:39 +01:00
|
|
|
m_ui->comboChallengeResponse->clear();
|
2017-02-21 01:06:32 +01:00
|
|
|
#endif
|
2014-05-26 00:49:28 -07:00
|
|
|
|
2012-04-23 00:18:10 +02:00
|
|
|
m_ui->enterPasswordEdit->setFocus();
|
2012-01-13 17:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CompositeKey ChangeMasterKeyWidget::newMasterKey()
|
|
|
|
{
|
|
|
|
return m_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
QLabel* ChangeMasterKeyWidget::headlineLabel()
|
|
|
|
{
|
|
|
|
return m_ui->headlineLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChangeMasterKeyWidget::generateKey()
|
|
|
|
{
|
2015-05-12 16:31:14 -04:00
|
|
|
m_key.clear();
|
2012-01-13 17:52:37 +01:00
|
|
|
|
|
|
|
if (m_ui->passwordGroup->isChecked()) {
|
2012-04-11 13:53:46 +02:00
|
|
|
if (m_ui->enterPasswordEdit->text() == m_ui->repeatPasswordEdit->text()) {
|
|
|
|
if (m_ui->enterPasswordEdit->text().isEmpty()) {
|
2017-02-20 23:35:03 +01:00
|
|
|
if (MessageBox::warning(this, tr("Empty password"),
|
|
|
|
tr("Do you really want to use an empty string as password?"),
|
|
|
|
QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) {
|
2012-04-11 13:53:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_key.addKey(PasswordKey(m_ui->enterPasswordEdit->text()));
|
|
|
|
}
|
|
|
|
else {
|
2015-01-21 16:19:05 +00:00
|
|
|
m_ui->messageWidget->showMessage(tr("Different passwords supplied."), MessageWidget::Error);
|
2012-04-11 13:53:46 +02:00
|
|
|
m_ui->enterPasswordEdit->setText("");
|
|
|
|
m_ui->repeatPasswordEdit->setText("");
|
|
|
|
return;
|
|
|
|
}
|
2012-01-13 17:52:37 +01:00
|
|
|
}
|
|
|
|
if (m_ui->keyFileGroup->isChecked()) {
|
|
|
|
FileKey fileKey;
|
|
|
|
QString errorMsg;
|
2015-01-19 23:47:57 +00:00
|
|
|
QString fileKeyName = m_ui->keyFileCombo->currentText();
|
|
|
|
if (!fileKey.load(fileKeyName, &errorMsg)) {
|
2015-01-21 16:19:05 +00:00
|
|
|
m_ui->messageWidget->showMessage(
|
|
|
|
tr("Failed to set %1 as the Key file:\n%2").arg(fileKeyName, errorMsg), MessageWidget::Error);
|
2015-05-12 15:12:17 -04:00
|
|
|
return;
|
2012-01-13 17:52:37 +01:00
|
|
|
}
|
|
|
|
m_key.addKey(fileKey);
|
|
|
|
}
|
|
|
|
|
2017-02-21 01:06:32 +01:00
|
|
|
#ifdef WITH_XC_YUBIKEY
|
2014-05-26 00:49:28 -07:00
|
|
|
if (m_ui->challengeResponseGroup->isChecked()) {
|
2017-02-20 22:41:39 +01:00
|
|
|
int i = m_ui->comboChallengeResponse->currentIndex();
|
|
|
|
i = m_ui->comboChallengeResponse->itemData(i).toInt();
|
2017-02-20 22:50:12 +01:00
|
|
|
|
|
|
|
if (0 == i) {
|
|
|
|
m_ui->messageWidget->showMessage(tr("Changing master key failed: no YubiKey inserted."),
|
|
|
|
MessageWidget::Error);
|
|
|
|
return;
|
|
|
|
}
|
2017-02-23 23:52:36 +01:00
|
|
|
bool blocking = i & true;
|
|
|
|
int slot = i >> 1;
|
|
|
|
auto key = QSharedPointer<YkChallengeResponseKey>(new YkChallengeResponseKey(slot, blocking));
|
2014-05-26 00:49:28 -07:00
|
|
|
m_key.addChallengeResponseKey(key);
|
|
|
|
}
|
2017-02-21 01:06:32 +01:00
|
|
|
#endif
|
2014-05-26 00:49:28 -07:00
|
|
|
|
2015-01-21 16:19:05 +00:00
|
|
|
m_ui->messageWidget->hideMessage();
|
2017-02-20 22:41:39 +01:00
|
|
|
emit editFinished(true);
|
2012-01-13 17:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ChangeMasterKeyWidget::reject()
|
|
|
|
{
|
2017-02-20 22:41:39 +01:00
|
|
|
emit editFinished(false);
|
2012-01-13 17:52:37 +01:00
|
|
|
}
|
2014-05-26 00:49:28 -07:00
|
|
|
|
2017-02-20 22:41:39 +01:00
|
|
|
void ChangeMasterKeyWidget::challengeResponseGroupToggled(bool checked)
|
|
|
|
{
|
|
|
|
if (checked)
|
|
|
|
pollYubikey();
|
|
|
|
}
|
2014-05-26 00:49:28 -07:00
|
|
|
|
2017-02-20 22:41:39 +01:00
|
|
|
void ChangeMasterKeyWidget::pollYubikey()
|
|
|
|
{
|
|
|
|
m_ui->buttonRedetectYubikey->setEnabled(false);
|
|
|
|
m_ui->comboChallengeResponse->setEnabled(false);
|
|
|
|
m_ui->comboChallengeResponse->clear();
|
2017-02-20 23:20:32 +01:00
|
|
|
m_ui->yubikeyProgress->setVisible(true);
|
2017-02-21 01:28:50 +01:00
|
|
|
setOkEnabled();
|
2017-02-20 22:41:39 +01:00
|
|
|
|
|
|
|
// YubiKey init is slow, detect asynchronously to not block the UI
|
|
|
|
QtConcurrent::run(YubiKey::instance(), &YubiKey::detect);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChangeMasterKeyWidget::yubikeyDetected(int slot, bool blocking)
|
2014-05-26 00:49:28 -07:00
|
|
|
{
|
|
|
|
YkChallengeResponseKey yk(slot, blocking);
|
2017-02-23 23:52:36 +01:00
|
|
|
m_ui->comboChallengeResponse->addItem(yk.getName(), QVariant((slot << 1) | blocking));
|
2017-02-20 22:41:39 +01:00
|
|
|
m_ui->comboChallengeResponse->setEnabled(m_ui->challengeResponseGroup->isChecked());
|
|
|
|
m_ui->buttonRedetectYubikey->setEnabled(m_ui->challengeResponseGroup->isChecked());
|
2017-02-20 23:20:32 +01:00
|
|
|
m_ui->yubikeyProgress->setVisible(false);
|
2017-02-21 01:28:50 +01:00
|
|
|
setOkEnabled();
|
2017-02-20 22:41:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChangeMasterKeyWidget::noYubikeyFound()
|
|
|
|
{
|
|
|
|
m_ui->buttonRedetectYubikey->setEnabled(m_ui->challengeResponseGroup->isChecked());
|
2017-02-20 23:20:32 +01:00
|
|
|
m_ui->yubikeyProgress->setVisible(false);
|
2017-02-21 01:28:50 +01:00
|
|
|
setOkEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChangeMasterKeyWidget::setOkEnabled()
|
|
|
|
{
|
|
|
|
bool ok = m_ui->passwordGroup->isChecked() ||
|
|
|
|
(m_ui->challengeResponseGroup->isChecked() && !m_ui->comboChallengeResponse->currentText().isEmpty()) ||
|
|
|
|
(m_ui->keyFileGroup->isChecked() && !m_ui->keyFileCombo->currentText().isEmpty());
|
|
|
|
|
|
|
|
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
|
2014-05-26 00:49:28 -07:00
|
|
|
}
|
2017-02-20 20:24:38 +01:00
|
|
|
|
2017-02-16 23:18:04 +01:00
|
|
|
void ChangeMasterKeyWidget::setCancelEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
m_ui->buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(enabled);
|
|
|
|
}
|