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