Raven Core  3.0.0
P2P Digital Currency
askpassphrasedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #if defined(HAVE_CONFIG_H)
7 #include "config/raven-config.h"
8 #endif
9 
10 #include "askpassphrasedialog.h"
11 #include "ui_askpassphrasedialog.h"
12 
13 #include "guiconstants.h"
14 #include "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(0),
27  fCapsLock(false)
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 to 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 to the wallet."));
71  break;
72  }
73  textChanged();
74  connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
75  connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
76  connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
77 }
78 
80 {
82  delete ui;
83 }
84 
86 {
87  this->model = _model;
88 }
89 
91 {
92  SecureString oldpass, newpass1, newpass2;
93  if(!model)
94  return;
95  oldpass.reserve(MAX_PASSPHRASE_SIZE);
96  newpass1.reserve(MAX_PASSPHRASE_SIZE);
97  newpass2.reserve(MAX_PASSPHRASE_SIZE);
98  // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
99  // Alternately, find a way to make this input mlock()'d to begin with.
100  oldpass.assign(ui->passEdit1->text().toStdString().c_str());
101  newpass1.assign(ui->passEdit2->text().toStdString().c_str());
102  newpass2.assign(ui->passEdit3->text().toStdString().c_str());
103 
105 
106  switch(mode)
107  {
108  case Encrypt: {
109  if(newpass1.empty() || newpass2.empty())
110  {
111  // Cannot encrypt with empty passphrase
112  break;
113  }
114  QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
115  tr("Warning: If you encrypt your wallet and lose your passphrase, you will <b>LOSE ALL OF YOUR RAVENS</b>!") + "<br><br>" + tr("Are you sure you wish to encrypt your wallet?"),
116  QMessageBox::Yes|QMessageBox::Cancel,
117  QMessageBox::Cancel);
118  if(retval == QMessageBox::Yes)
119  {
120  if(newpass1 == newpass2)
121  {
122  if(model->setWalletEncrypted(true, newpass1))
123  {
124  QMessageBox::warning(this, tr("Wallet encrypted"),
125  "<qt>" +
126  tr("%1 will close now to finish the encryption process. "
127  "Remember that encrypting your wallet cannot fully protect "
128  "your ravens from being stolen by malware infecting your computer.").arg(tr(PACKAGE_NAME)) +
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  QApplication::quit();
136  }
137  else
138  {
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  QDialog::accept(); // Success
143  }
144  else
145  {
146  QMessageBox::critical(this, tr("Wallet encryption failed"),
147  tr("The supplied passphrases do not match."));
148  }
149  }
150  else
151  {
152  QDialog::reject(); // Cancelled
153  }
154  } break;
155  case Unlock:
156  if(!model->setWalletLocked(false, oldpass))
157  {
158  QMessageBox::critical(this, tr("Wallet unlock failed"),
159  tr("The passphrase entered for the wallet decryption was incorrect."));
160  }
161  else
162  {
163  QDialog::accept(); // Success
164  }
165  break;
166  case Decrypt:
167  if(!model->setWalletEncrypted(false, oldpass))
168  {
169  QMessageBox::critical(this, tr("Wallet decryption failed"),
170  tr("The passphrase entered for the wallet decryption was incorrect."));
171  }
172  else
173  {
174  QDialog::accept(); // Success
175  }
176  break;
177  case ChangePass:
178  if(newpass1 == newpass2)
179  {
180  if(model->changePassphrase(oldpass, newpass1))
181  {
182  QMessageBox::information(this, tr("Wallet encrypted"),
183  tr("Wallet passphrase was successfully changed."));
184  QDialog::accept(); // Success
185  }
186  else
187  {
188  QMessageBox::critical(this, tr("Wallet encryption failed"),
189  tr("The passphrase entered for the wallet decryption was incorrect."));
190  }
191  }
192  else
193  {
194  QMessageBox::critical(this, tr("Wallet encryption failed"),
195  tr("The supplied passphrases do not match."));
196  }
197  break;
198  }
199 }
200 
202 {
203  // Validate input, set Ok button to enabled when acceptable
204  bool acceptable = false;
205  switch(mode)
206  {
207  case Encrypt: // New passphrase x2
208  acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
209  break;
210  case Unlock: // Old passphrase x1
211  case Decrypt:
212  acceptable = !ui->passEdit1->text().isEmpty();
213  break;
214  case ChangePass: // Old passphrase x1, new passphrase x2
215  acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
216  break;
217  }
218  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
219 }
220 
222 {
223  // Detect Caps Lock key press.
224  if (event->type() == QEvent::KeyPress) {
225  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
226  if (ke->key() == Qt::Key_CapsLock) {
227  fCapsLock = !fCapsLock;
228  }
229  if (fCapsLock) {
230  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
231  } else {
232  ui->capsLabel->clear();
233  }
234  }
235  return QWidget::event(event);
236 }
237 
238 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
239 {
240  /* Detect Caps Lock.
241  * There is no good OS-independent way to check a key state in Qt, but we
242  * can detect Caps Lock by checking for the following condition:
243  * Shift key is down and the result is a lower case character, or
244  * Shift key is not down and the result is an upper case character.
245  */
246  if (event->type() == QEvent::KeyPress) {
247  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
248  QString str = ke->text();
249  if (str.length() != 0) {
250  const QChar *psz = str.unicode();
251  bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
252  if ((fShift && *psz >= 'a' && *psz <= 'z') || (!fShift && *psz >= 'A' && *psz <= 'Z')) {
253  fCapsLock = true;
254  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
255  } else if (psz->isLetter()) {
256  fCapsLock = false;
257  ui->capsLabel->clear();
258  }
259  }
260  }
261  return QDialog::eventFilter(object, event);
262 }
263 
264 static void SecureClearQLineEdit(QLineEdit* edit)
265 {
266  // Attempt to overwrite text so that they do not linger around in memory
267  edit->setText(QString(" ").repeated(edit->text().size()));
268  edit->clear();
269 }
270 
272 {
273  SecureClearQLineEdit(ui->passEdit1);
274  SecureClearQLineEdit(ui->passEdit2);
275  SecureClearQLineEdit(ui->passEdit3);
276 }
bool event(QEvent *event)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:57
Ask passphrase twice and encrypt.
#define PACKAGE_NAME
Definition: raven-config.h:350
Ask passphrase and unlock.
Ui::AskPassphraseDialog * ui
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
AddressTableModel * parent
AskPassphraseDialog(Mode mode, QWidget *parent)
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
Interface to Raven wallet from Qt view code.
Definition: walletmodel.h:165
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)