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