Bitcoin ABC  0.24.7
P2P Digital Currency
askpassphrasedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 The Bitcoin 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 #if defined(HAVE_CONFIG_H)
6 #include <config/bitcoin-config.h>
7 #endif
8 
10 #include <qt/forms/ui_askpassphrasedialog.h>
11 
12 #include <qt/guiconstants.h>
13 #include <qt/guiutil.h>
14 #include <qt/walletmodel.h>
16 
17 #include <QKeyEvent>
18 #include <QMessageBox>
19 #include <QPushButton>
20 
22  SecureString *passphrase_out)
23  : QDialog(parent), ui(new Ui::AskPassphraseDialog), mode(_mode),
24  model(nullptr), fCapsLock(false), m_passphrase_out(passphrase_out) {
25  ui->setupUi(this);
26 
27  ui->passEdit1->setMinimumSize(ui->passEdit1->sizeHint());
28  ui->passEdit2->setMinimumSize(ui->passEdit2->sizeHint());
29  ui->passEdit3->setMinimumSize(ui->passEdit3->sizeHint());
30 
31  ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
32  ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
33  ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
34 
35  // Setup Caps Lock detection.
36  ui->passEdit1->installEventFilter(this);
37  ui->passEdit2->installEventFilter(this);
38  ui->passEdit3->installEventFilter(this);
39 
40  switch (mode) {
41  case Encrypt: // Ask passphrase x2
42  ui->warningLabel->setText(
43  tr("Enter the new passphrase for the wallet.<br/>Please use a "
44  "passphrase of <b>ten or more random characters</b>, or "
45  "<b>eight or more words</b>."));
46  ui->passLabel1->hide();
47  ui->passEdit1->hide();
48  setWindowTitle(tr("Encrypt wallet"));
49  break;
50  case Unlock: // Ask passphrase
51  ui->warningLabel->setText(tr("This operation needs your wallet "
52  "passphrase to unlock the wallet."));
53  ui->passLabel2->hide();
54  ui->passEdit2->hide();
55  ui->passLabel3->hide();
56  ui->passEdit3->hide();
57  setWindowTitle(tr("Unlock wallet"));
58  break;
59  case Decrypt: // Ask passphrase
60  ui->warningLabel->setText(tr("This operation needs your wallet "
61  "passphrase to decrypt the wallet."));
62  ui->passLabel2->hide();
63  ui->passEdit2->hide();
64  ui->passLabel3->hide();
65  ui->passEdit3->hide();
66  setWindowTitle(tr("Decrypt wallet"));
67  break;
68  case ChangePass: // Ask old passphrase + new passphrase x2
69  setWindowTitle(tr("Change passphrase"));
70  ui->warningLabel->setText(tr(
71  "Enter the old passphrase and new passphrase for the wallet."));
72  break;
73  }
74  textChanged();
75  connect(ui->toggleShowPasswordButton, &QPushButton::toggled, this,
77  connect(ui->passEdit1, &QLineEdit::textChanged, this,
79  connect(ui->passEdit2, &QLineEdit::textChanged, this,
81  connect(ui->passEdit3, &QLineEdit::textChanged, this,
83 
85 }
86 
89  delete ui;
90 }
91 
93  this->model = _model;
94 }
95 
97  SecureString oldpass, newpass1, newpass2;
98  if (!model && mode != Encrypt) {
99  return;
100  }
101  oldpass.reserve(MAX_PASSPHRASE_SIZE);
102  newpass1.reserve(MAX_PASSPHRASE_SIZE);
103  newpass2.reserve(MAX_PASSPHRASE_SIZE);
104  // TODO: get rid of this .c_str() by implementing
105  // SecureString::operator=(std::string)
106  // Alternately, find a way to make this input mlock()'d to begin with.
107  oldpass.assign(ui->passEdit1->text().toStdString().c_str());
108  newpass1.assign(ui->passEdit2->text().toStdString().c_str());
109  newpass2.assign(ui->passEdit3->text().toStdString().c_str());
110 
112 
113  switch (mode) {
114  case Encrypt: {
115  if (newpass1.empty() || newpass2.empty()) {
116  // Cannot encrypt with empty passphrase
117  break;
118  }
119  QMessageBox::StandardButton retval = QMessageBox::question(
120  this, tr("Confirm wallet encryption"),
121  tr("Warning: If you encrypt your wallet and lose your "
122  "passphrase, you will <b>LOSE ALL OF YOUR BITCOINS</b>!") +
123  "<br><br>" +
124  tr("Are you sure you wish to encrypt your wallet?"),
125  QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
126  if (retval == QMessageBox::Yes) {
127  if (newpass1 == newpass2) {
128  QString encryption_reminder =
129  tr("Remember that encrypting your wallet cannot fully "
130  "protect your bitcoins from being stolen by malware "
131  "infecting your computer.");
132  if (m_passphrase_out) {
133  m_passphrase_out->assign(newpass1);
134  QMessageBox::warning(
135  this, tr("Wallet to be encrypted"),
136  "<qt>" +
137  tr("Your wallet is about to be encrypted. ") +
138  encryption_reminder + "</b></qt>");
139  } else {
140  assert(model != nullptr);
141  if (model->setWalletEncrypted(true, newpass1)) {
142  QMessageBox::warning(
143  this, tr("Wallet encrypted"),
144  "<qt>" + tr("Your wallet is now encrypted. ") +
145  encryption_reminder + "<br><br><b>" +
146  tr("IMPORTANT: Any previous backups you "
147  "have made of your wallet file should "
148  "be replaced with the newly generated, "
149  "encrypted wallet file. For security "
150  "reasons, previous backups of the "
151  "unencrypted wallet file will become "
152  "useless as soon as you start using the "
153  "new, encrypted wallet.") +
154  "</b></qt>");
155  } else {
156  QMessageBox::critical(
157  this, tr("Wallet encryption failed"),
158  tr("Wallet encryption failed due to an "
159  "internal error. Your wallet was not "
160  "encrypted."));
161  }
162  }
163  QDialog::accept(); // Success
164  } else {
165  QMessageBox::critical(
166  this, tr("Wallet encryption failed"),
167  tr("The supplied passphrases do not match."));
168  }
169  } else {
170  QDialog::reject(); // Cancelled
171  }
172  } break;
173  case Unlock:
174  try {
175  if (!model->setWalletLocked(false, oldpass)) {
176  QMessageBox::critical(
177  this, tr("Wallet unlock failed"),
178  tr("The passphrase entered for the wallet decryption "
179  "was incorrect."));
180  } else {
181  // Success
182  QDialog::accept();
183  }
184  } catch (const std::runtime_error &e) {
185  QMessageBox::critical(this, tr("Wallet unlock failed"),
186  e.what());
187  }
188  break;
189  case Decrypt:
190  if (!model->setWalletEncrypted(false, oldpass)) {
191  QMessageBox::critical(this, tr("Wallet decryption failed"),
192  tr("The passphrase entered for the "
193  "wallet decryption was incorrect."));
194  } else {
195  QDialog::accept(); // Success
196  }
197  break;
198  case ChangePass:
199  if (newpass1 == newpass2) {
200  if (model->changePassphrase(oldpass, newpass1)) {
201  QMessageBox::information(
202  this, tr("Wallet encrypted"),
203  tr("Wallet passphrase was successfully changed."));
204  QDialog::accept(); // Success
205  } else {
206  QMessageBox::critical(
207  this, tr("Wallet encryption failed"),
208  tr("The passphrase entered for the wallet decryption "
209  "was incorrect."));
210  }
211  } else {
212  QMessageBox::critical(
213  this, tr("Wallet encryption failed"),
214  tr("The supplied passphrases do not match."));
215  }
216  break;
217  }
218 }
219 
221  // Validate input, set Ok button to enabled when acceptable
222  bool acceptable = false;
223  switch (mode) {
224  case Encrypt: // New passphrase x2
225  acceptable = !ui->passEdit2->text().isEmpty() &&
226  !ui->passEdit3->text().isEmpty();
227  break;
228  case Unlock: // Old passphrase x1
229  case Decrypt:
230  acceptable = !ui->passEdit1->text().isEmpty();
231  break;
232  case ChangePass: // Old passphrase x1, new passphrase x2
233  acceptable = !ui->passEdit1->text().isEmpty() &&
234  !ui->passEdit2->text().isEmpty() &&
235  !ui->passEdit3->text().isEmpty();
236  break;
237  }
238  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
239 }
240 
241 bool AskPassphraseDialog::event(QEvent *event) {
242  // Detect Caps Lock key press.
243  if (event->type() == QEvent::KeyPress) {
244  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
245  if (ke->key() == Qt::Key_CapsLock) {
246  fCapsLock = !fCapsLock;
247  }
248  if (fCapsLock) {
249  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
250  } else {
251  ui->capsLabel->clear();
252  }
253  }
254  return QWidget::event(event);
255 }
256 
258  ui->toggleShowPasswordButton->setDown(show);
259  const auto renderingMode = show ? QLineEdit::Normal : QLineEdit::Password;
260  ui->passEdit1->setEchoMode(renderingMode);
261  ui->passEdit2->setEchoMode(renderingMode);
262  ui->passEdit3->setEchoMode(renderingMode);
263 }
264 
265 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event) {
266  /* Detect Caps Lock.
267  * There is no good OS-independent way to check a key state in Qt, but
268  * we can detect Caps Lock by checking for the following condition:
269  * Shift key is down and the result is a lower case character, or
270  * Shift key is not down and the result is an upper case character.
271  */
272  if (event->type() == QEvent::KeyPress) {
273  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
274  QString str = ke->text();
275  if (str.length() != 0) {
276  const QChar *psz = str.unicode();
277  bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
278  if ((fShift && *psz >= 'a' && *psz <= 'z') ||
279  (!fShift && *psz >= 'A' && *psz <= 'Z')) {
280  fCapsLock = true;
281  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
282  } else if (psz->isLetter()) {
283  fCapsLock = false;
284  ui->capsLabel->clear();
285  }
286  }
287  }
288  return QDialog::eventFilter(object, event);
289 }
290 
291 static void SecureClearQLineEdit(QLineEdit *edit) {
292  // Attempt to overwrite text so that they do not linger around in memory
293  edit->setText(QString(" ").repeated(edit->text().size()));
294  edit->clear();
295 }
296 
298  SecureClearQLineEdit(ui->passEdit1);
299  SecureClearQLineEdit(ui->passEdit2);
300  SecureClearQLineEdit(ui->passEdit3);
301 }
WalletModel::changePassphrase
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
Definition: walletmodel.cpp:359
AskPassphraseDialog::Unlock
@ Unlock
Ask passphrase and unlock.
Definition: askpassphrasedialog.h:27
AskPassphraseDialog::eventFilter
bool eventFilter(QObject *object, QEvent *event) override
Definition: askpassphrasedialog.cpp:265
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:47
AskPassphraseDialog::m_passphrase_out
SecureString * m_passphrase_out
Definition: askpassphrasedialog.h:45
walletmodel.h
AskPassphraseDialog::accept
void accept() override
Definition: askpassphrasedialog.cpp:96
AskPassphraseDialog::~AskPassphraseDialog
~AskPassphraseDialog()
Definition: askpassphrasedialog.cpp:87
AskPassphraseDialog
Multifunctional dialog to ask for passphrases.
Definition: askpassphrasedialog.h:21
AskPassphraseDialog::fCapsLock
bool fCapsLock
Definition: askpassphrasedialog.h:44
MAX_PASSPHRASE_SIZE
static const int MAX_PASSPHRASE_SIZE
Definition: guiconstants.h:14
AskPassphraseDialog::secureClearPassFields
void secureClearPassFields()
Definition: askpassphrasedialog.cpp:297
AskPassphraseDialog::event
bool event(QEvent *event) override
Definition: askpassphrasedialog.cpp:241
SecureString
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56
guiutil.h
AskPassphraseDialog::toggleShowPassword
void toggleShowPassword(bool)
Definition: askpassphrasedialog.cpp:257
AskPassphraseDialog::model
WalletModel * model
Definition: askpassphrasedialog.h:43
AskPassphraseDialog::textChanged
void textChanged()
Definition: askpassphrasedialog.cpp:220
secure.h
AskPassphraseDialog::Decrypt
@ Decrypt
Ask passphrase and decrypt wallet.
Definition: askpassphrasedialog.h:29
Ui
Definition: addressbookpage.h:14
WalletModel::setWalletEncrypted
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase)
Definition: walletmodel.cpp:341
AskPassphraseDialog::ui
Ui::AskPassphraseDialog * ui
Definition: askpassphrasedialog.h:41
guiconstants.h
AskPassphraseDialog::AskPassphraseDialog
AskPassphraseDialog(Mode mode, QWidget *parent, SecureString *passphrase_out=nullptr)
Definition: askpassphrasedialog.cpp:21
GUIUtil::handleCloseWindowShortcut
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:396
AskPassphraseDialog::ChangePass
@ ChangePass
Ask old passphrase + new passphrase twice.
Definition: askpassphrasedialog.h:28
AddressTablePriv::parent
AddressTableModel * parent
Definition: addresstablemodel.cpp:74
AskPassphraseDialog::mode
Mode mode
Definition: askpassphrasedialog.h:42
AskPassphraseDialog::Encrypt
@ Encrypt
Ask passphrase twice and encrypt.
Definition: askpassphrasedialog.h:26
AskPassphraseDialog::Mode
Mode
Definition: askpassphrasedialog.h:25
askpassphrasedialog.h
WalletModel::setWalletLocked
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
Definition: walletmodel.cpp:349
SecureClearQLineEdit
static void SecureClearQLineEdit(QLineEdit *edit)
Definition: askpassphrasedialog.cpp:291
AskPassphraseDialog::setModel
void setModel(WalletModel *model)
Definition: askpassphrasedialog.cpp:92