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