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