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::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
104  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?"),
105  QMessageBox::Yes|QMessageBox::Cancel,
106  QMessageBox::Cancel);
107  if(retval == QMessageBox::Yes)
108  {
109  if(newpass1 == newpass2)
110  {
111  QString encryption_reminder = tr("Remember that encrypting your wallet cannot fully protect "
112  "your bitcoins from being stolen by malware infecting your computer.");
113  if (m_passphrase_out) {
114  m_passphrase_out->assign(newpass1);
115  QMessageBox::warning(this, tr("Wallet to be encrypted"),
116  "<qt>" +
117  tr("Your wallet is about to be encrypted. ") + encryption_reminder +
118  "</b></qt>");
119  } else {
120  assert(model != nullptr);
121  if (model->setWalletEncrypted(newpass1)) {
122  QMessageBox::warning(this, tr("Wallet encrypted"),
123  "<qt>" +
124  tr("Your wallet is now encrypted. ") + encryption_reminder +
125  "<br><br><b>" +
126  tr("IMPORTANT: Any previous backups you have made of your wallet file "
127  "should be replaced with the newly generated, encrypted wallet file. "
128  "For security reasons, previous backups of the unencrypted wallet file "
129  "will become useless as soon as you start using the new, encrypted wallet.") +
130  "</b></qt>");
131  } else {
132  QMessageBox::critical(this, tr("Wallet encryption failed"),
133  tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
134  }
135  }
136  QDialog::accept(); // Success
137  }
138  else
139  {
140  QMessageBox::critical(this, tr("Wallet encryption failed"),
141  tr("The supplied passphrases do not match."));
142  }
143  }
144  else
145  {
146  QDialog::reject(); // Cancelled
147  }
148  } break;
149  case Unlock:
150  try {
151  if (!model->setWalletLocked(false, oldpass)) {
152  // Check if the passphrase has a null character (see #27067 for details)
153  if (oldpass.find('\0') == std::string::npos) {
154  QMessageBox::critical(this, tr("Wallet unlock failed"),
155  tr("The passphrase entered for the wallet decryption was incorrect."));
156  } else {
157  QMessageBox::critical(this, tr("Wallet unlock failed"),
158  tr("The passphrase entered for the wallet decryption is incorrect. "
159  "It contains a null character (ie - a zero byte). "
160  "If the passphrase was set with a version of this software prior to 25.0, "
161  "please try again with only the characters up to — but not including — "
162  "the first null character. If this is successful, please set a new "
163  "passphrase to avoid this issue in the future."));
164  }
165  } else {
166  if (m_passphrase_out) {
167  m_passphrase_out->assign(oldpass);
168  }
169  QDialog::accept(); // Success
170  }
171  } catch (const std::runtime_error& e) {
172  QMessageBox::critical(this, tr("Wallet unlock failed"), e.what());
173  }
174  break;
175  case ChangePass:
176  if(newpass1 == newpass2)
177  {
178  if(model->changePassphrase(oldpass, newpass1))
179  {
180  QMessageBox::information(this, tr("Wallet encrypted"),
181  tr("Wallet passphrase was successfully changed."));
182  QDialog::accept(); // Success
183  }
184  else
185  {
186  // Check if the old passphrase had a null character (see #27067 for details)
187  if (oldpass.find('\0') == std::string::npos) {
188  QMessageBox::critical(this, tr("Passphrase change failed"),
189  tr("The passphrase entered for the wallet decryption was incorrect."));
190  } else {
191  QMessageBox::critical(this, tr("Passphrase change failed"),
192  tr("The old passphrase entered for the wallet decryption is incorrect. "
193  "It contains a null character (ie - a zero byte). "
194  "If the passphrase was set with a version of this software prior to 25.0, "
195  "please try again with only the characters up to — but not including — "
196  "the first null character."));
197  }
198  }
199  }
200  else
201  {
202  QMessageBox::critical(this, tr("Wallet encryption failed"),
203  tr("The supplied passphrases do not match."));
204  }
205  break;
206  }
207 }
208 
210 {
211  // Validate input, set Ok button to enabled when acceptable
212  bool acceptable = false;
213  switch(mode)
214  {
215  case Encrypt: // New passphrase x2
216  acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
217  break;
218  case Unlock: // Old passphrase x1
219  acceptable = !ui->passEdit1->text().isEmpty();
220  break;
221  case ChangePass: // Old passphrase x1, new passphrase x2
222  acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
223  break;
224  }
225  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
226 }
227 
228 bool AskPassphraseDialog::event(QEvent *event)
229 {
230  // Detect Caps Lock key press.
231  if (event->type() == QEvent::KeyPress) {
232  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
233  if (ke->key() == Qt::Key_CapsLock) {
234  fCapsLock = !fCapsLock;
235  }
236  if (fCapsLock) {
237  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
238  } else {
239  ui->capsLabel->clear();
240  }
241  }
242  return QWidget::event(event);
243 }
244 
246 {
247  ui->toggleShowPasswordButton->setDown(show);
248  const auto mode = show ? QLineEdit::Normal : QLineEdit::Password;
249  ui->passEdit1->setEchoMode(mode);
250  ui->passEdit2->setEchoMode(mode);
251  ui->passEdit3->setEchoMode(mode);
252 }
253 
254 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
255 {
256  /* Detect Caps Lock.
257  * There is no good OS-independent way to check a key state in Qt, but we
258  * can detect Caps Lock by checking for the following condition:
259  * Shift key is down and the result is a lower case character, or
260  * Shift key is not down and the result is an upper case character.
261  */
262  if (event->type() == QEvent::KeyPress) {
263  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
264  QString str = ke->text();
265  if (str.length() != 0) {
266  const QChar *psz = str.unicode();
267  bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
268  if ((fShift && *psz >= 'a' && *psz <= 'z') || (!fShift && *psz >= 'A' && *psz <= 'Z')) {
269  fCapsLock = true;
270  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
271  } else if (psz->isLetter()) {
272  fCapsLock = false;
273  ui->capsLabel->clear();
274  }
275  }
276  }
277  return QDialog::eventFilter(object, event);
278 }
279 
280 static void SecureClearQLineEdit(QLineEdit* edit)
281 {
282  // Attempt to overwrite text so that they do not linger around in memory
283  edit->setText(QString(" ").repeated(edit->text().size()));
284  edit->clear();
285 }
286 
288 {
289  SecureClearQLineEdit(ui->passEdit1);
290  SecureClearQLineEdit(ui->passEdit2);
291  SecureClearQLineEdit(ui->passEdit3);
292 }
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:60
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:423
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())