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