|
| 1 | +// Copyright (c) 2021 The XEP Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <qt/applocker.h> |
| 6 | +#include <qt/forms/ui_applocker.h> |
| 7 | + |
| 8 | +#include <crypto/pbkdf2_hmac.h> |
| 9 | + |
| 10 | +AppLocker::AppLocker(QWidget *parent) : |
| 11 | + QWidget(parent), |
| 12 | + ui(new Ui::AppLocker) |
| 13 | +{ |
| 14 | + ui->setupUi(this); |
| 15 | + this->setWindowTitle(tr("Wallet locker")); |
| 16 | + this->setWindowModality(Qt::ApplicationModal); |
| 17 | + QRegExpValidator *validatorReg = new QRegExpValidator(QRegExp("[1-9]\\d{5,9}"), this); |
| 18 | + |
| 19 | + // Lock view (index 1) |
| 20 | + ui->stackedWidget->setCurrentIndex(1); |
| 21 | + ui->headLabel->setText(tr("Set a PIN to lock your wallet:") + "<br>"); |
| 22 | + ui->messageLabel->setText("<br>- " + tr("Your PIN must be at least 6 digits long.") + |
| 23 | + "<br>- " + tr("The PIN is only valid for this session.")); |
| 24 | + ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Lock")); |
| 25 | + ui->buttonBox->button(QDialogButtonBox::Ok)->setDefault(true); |
| 26 | + ui->buttonBox->button(QDialogButtonBox::Cancel)->setAutoDefault(true); |
| 27 | + ui->pinLineEdit->setValidator(validatorReg); |
| 28 | + ui->pinLineEdit->setEchoMode(QLineEdit::Password); |
| 29 | + ui->confirmLineEdit->setValidator(validatorReg); |
| 30 | + ui->confirmLineEdit->setEchoMode(QLineEdit::Password); |
| 31 | + |
| 32 | + // Unlock view |
| 33 | + ui->lockLabel->setText(tr("Your wallet is locked.") + "<br>"); |
| 34 | + ui->unlocklabel->setText(tr("PIN")); |
| 35 | + ui->unlockLineEdit->setValidator(validatorReg); |
| 36 | + ui->unlockLineEdit->setEchoMode(QLineEdit::Password); |
| 37 | + |
| 38 | + connect(ui->unlockLineEdit, &QLineEdit::textChanged, [this] { |
| 39 | + if (ui->unlockLineEdit->text().size() >= 6) { |
| 40 | + ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true); |
| 41 | + } else if (ui->stackedWidget->currentIndex() == 0) { |
| 42 | + ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); |
| 43 | + } |
| 44 | + }); |
| 45 | + connect(ui->pinLineEdit, &QLineEdit::returnPressed, ui->buttonBox, &QDialogButtonBox::accepted); |
| 46 | + connect(ui->confirmLineEdit, &QLineEdit::returnPressed, ui->buttonBox, &QDialogButtonBox::accepted); |
| 47 | + connect(ui->unlockLineEdit, &QLineEdit::returnPressed, ui->buttonBox, &QDialogButtonBox::accepted); |
| 48 | + connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &AppLocker::setLock); |
| 49 | + connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &AppLocker::close); |
| 50 | +} |
| 51 | + |
| 52 | +AppLocker::~AppLocker() |
| 53 | +{ |
| 54 | + delete ui; |
| 55 | +} |
| 56 | + |
| 57 | +void AppLocker::setLock() |
| 58 | +{ |
| 59 | + switch (ui->stackedWidget->currentIndex()) { |
| 60 | + case 0: |
| 61 | + if (pbkdf2_hmac_sha256_time_check(reinterpret_cast<const unsigned char*>(ui->unlockLineEdit->text().toUtf8().constData()), ui->unlockLineEdit->text().size(), reinterpret_cast<const unsigned char*>(salt.constData()), salt.size(), 3, pinHash)) { |
| 62 | + walletLocked = false; |
| 63 | + secureClearPinFields(); |
| 64 | + ui->stackedWidget->setCurrentIndex(1); |
| 65 | + ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Lock")); |
| 66 | + ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true); |
| 67 | + ui->buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(true); |
| 68 | + ui->buttonBox->button(QDialogButtonBox::Cancel)->setVisible(true); |
| 69 | + ui->pinLineEdit->setFocus(); |
| 70 | + Q_EMIT lockingApp(false); |
| 71 | + } else { |
| 72 | + QMessageBox::warning(this, tr("Error"), tr("The entered PIN is incorrect."), QMessageBox::Ok); |
| 73 | + } |
| 74 | + break; |
| 75 | + case 1: |
| 76 | + if (ui->pinLineEdit->text().isEmpty() || ui->confirmLineEdit->text().isEmpty()) { |
| 77 | + QMessageBox::information(this, tr("Error"), tr("Please enter and confirm your PIN."), QMessageBox::Ok); |
| 78 | + } else if (ui->pinLineEdit->text().size() < 6 || ui->confirmLineEdit->text().size() < 6) { |
| 79 | + QMessageBox::information(this, tr("Error"), tr("Your PIN must be at least 6 digits long."), QMessageBox::Ok); |
| 80 | + } else if (ui->pinLineEdit->text() != ui->confirmLineEdit->text()) { |
| 81 | + QMessageBox::warning(this, tr("Error"), tr("The entered PINs don't match, please try again."), QMessageBox::Ok); |
| 82 | + } else { |
| 83 | + walletLocked = true; |
| 84 | + salt = QString::number(QDateTime::currentMSecsSinceEpoch()).toUtf8(); |
| 85 | + // pinHash = QPasswordDigestor::deriveKeyPbkdf2(QCryptographicHash::Sha256, ui->pinLineEdit->text().toUtf8(), salt, 10000, quint64(32)); |
| 86 | + pbkdf2_hmac_sha256_time(reinterpret_cast<const unsigned char*>(ui->pinLineEdit->text().toUtf8().constData()), ui->pinLineEdit->text().size(), reinterpret_cast<const unsigned char*>(salt.constData()), salt.size(), 1, pinHash); |
| 87 | + secureClearPinFields(); |
| 88 | + ui->stackedWidget->setCurrentIndex(0); // move to unlock view |
| 89 | + ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Unlock")); |
| 90 | + ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); |
| 91 | + ui->buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(false); |
| 92 | + ui->buttonBox->button(QDialogButtonBox::Cancel)->setVisible(false); |
| 93 | + ui->unlockLineEdit->setFocus(); |
| 94 | + Q_EMIT lockingApp(true); |
| 95 | + } |
| 96 | + break; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void AppLocker::showLocker() |
| 101 | +{ |
| 102 | + this->move(QGuiApplication::primaryScreen()->geometry().center() - this->rect().center()); |
| 103 | + if (ui->stackedWidget->currentIndex() == 1) { |
| 104 | + ui->pinLineEdit->setFocus(); |
| 105 | + } |
| 106 | + this->show(); |
| 107 | + this->setFixedSize(this->size()); |
| 108 | +} |
| 109 | + |
| 110 | +void AppLocker::closeEvent(QCloseEvent *event) |
| 111 | +{ |
| 112 | + if (walletLocked) { |
| 113 | + int ret = -1; |
| 114 | + if (!forceClose) { |
| 115 | + ret = QMessageBox::warning(this, tr("Warning"), tr("The wallet application will exit, would you like to continue?"), QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel); |
| 116 | + if (ret == QMessageBox::Cancel) { |
| 117 | + event->ignore(); |
| 118 | + } |
| 119 | + } |
| 120 | + if (forceClose || ret == QMessageBox::Ok) { |
| 121 | + // Clear memory being used by app locker |
| 122 | + secureClearPinFields(); |
| 123 | + Q_EMIT quitAppFromWalletLocker(); |
| 124 | + event->accept(); |
| 125 | + } |
| 126 | + } else if (ui->stackedWidget->currentIndex() == 1) { |
| 127 | + // Clear memory being used by app locker |
| 128 | + secureClearPinFields(); |
| 129 | + event->accept(); |
| 130 | + } else { |
| 131 | + event->ignore(); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +void AppLocker::secureClearPinFields() |
| 136 | +{ |
| 137 | + // Overwrite text so that it does not remain in memory |
| 138 | + ui->unlockLineEdit->setText(QString(" ").repeated(ui->unlockLineEdit->text().size())); |
| 139 | + ui->pinLineEdit->setText(QString(" ").repeated(ui->pinLineEdit->text().size())); |
| 140 | + ui->confirmLineEdit->setText(QString(" ").repeated(ui->confirmLineEdit->text().size())); |
| 141 | + ui->unlockLineEdit->clear(); |
| 142 | + ui->pinLineEdit->clear(); |
| 143 | + ui->confirmLineEdit->clear(); |
| 144 | +} |
0 commit comments