Anoncoin  0.9.4
P2P Digital Currency
askpassphrasedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "askpassphrasedialog.h"
8 
9 #include "guiconstants.h"
10 #include "walletmodel.h"
11 
12 #include "allocators.h"
13 
14 #include <QKeyEvent>
15 #include <QMessageBox>
16 #include <QPushButton>
17 
19  QDialog(parent),
20  ui(new Ui::AskPassphraseDialog),
21  mode(mode),
22  model(0),
23  fCapsLock(false)
24 {
25  ui->setupUi(this);
26 
27  ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
28  ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
29  ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
30 
31  // Setup Caps Lock detection.
32  ui->passEdit1->installEventFilter(this);
33  ui->passEdit2->installEventFilter(this);
34  ui->passEdit3->installEventFilter(this);
35 
36  switch(mode)
37  {
38  case Encrypt: // Ask passphrase x2
39  ui->passLabel1->hide();
40  ui->passEdit1->hide();
41  ui->warningLabel->setText(tr("Enter the new passphrase to the wallet.<br/>Please use a passphrase of <b>10 or more random characters</b>, or <b>eight or more words</b>."));
42  setWindowTitle(tr("Encrypt wallet"));
43  break;
44  case Unlock: // Ask passphrase
45  ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
46  ui->passLabel2->hide();
47  ui->passEdit2->hide();
48  ui->passLabel3->hide();
49  ui->passEdit3->hide();
50  setWindowTitle(tr("Unlock wallet"));
51  break;
52  case Decrypt: // Ask passphrase
53  ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
54  ui->passLabel2->hide();
55  ui->passEdit2->hide();
56  ui->passLabel3->hide();
57  ui->passEdit3->hide();
58  setWindowTitle(tr("Decrypt wallet"));
59  break;
60  case ChangePass: // Ask old passphrase + new passphrase x2
61  setWindowTitle(tr("Change passphrase"));
62  ui->warningLabel->setText(tr("Enter the old and new passphrase to the wallet."));
63  break;
64  }
65 
66  textChanged();
67  connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
68  connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
69  connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
70 }
71 
73 {
74  // Attempt to overwrite text so that they do not linger around in memory
75  ui->passEdit1->setText(QString(" ").repeated(ui->passEdit1->text().size()));
76  ui->passEdit2->setText(QString(" ").repeated(ui->passEdit2->text().size()));
77  ui->passEdit3->setText(QString(" ").repeated(ui->passEdit3->text().size()));
78  delete ui;
79 }
80 
82 {
83  this->model = model;
84 }
85 
87 {
88  SecureString oldpass, newpass1, newpass2;
89  if(!model)
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 
100  switch(mode)
101  {
102  case Encrypt: {
103  if(newpass1.empty() || newpass2.empty())
104  {
105  // Cannot encrypt with empty passphrase
106  break;
107  }
108  QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
109  tr("Warning: If you encrypt your wallet and lose your passphrase, you will <b>LOSE ALL OF YOUR ANONCOINS</b>!") + "<br><br>" + tr("Are you sure you wish to encrypt your wallet?"),
110  QMessageBox::Yes|QMessageBox::Cancel,
111  QMessageBox::Cancel);
112  if(retval == QMessageBox::Yes)
113  {
114  if(newpass1 == newpass2)
115  {
116  if(model->setWalletEncrypted(true, newpass1))
117  {
118  QMessageBox::warning(this, tr("Wallet encrypted"),
119  "<qt>" +
120  tr("Anoncoin will close now to finish the encryption process. "
121  "Remember that encrypting your wallet cannot fully protect "
122  "your anoncoins from being stolen by malware infecting your computer.") +
123  "<br><br><b>" +
124  tr("IMPORTANT: Any previous backups you have made of your wallet file "
125  "should be replaced with the newly generated, encrypted wallet file. "
126  "For security reasons, previous backups of the unencrypted wallet file "
127  "will become useless as soon as you start using the new, encrypted wallet.") +
128  "</b></qt>");
129  QApplication::quit();
130  }
131  else
132  {
133  QMessageBox::critical(this, tr("Wallet encryption failed"),
134  tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
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  if(!model->setWalletLocked(false, oldpass))
151  {
152  QMessageBox::critical(this, tr("Wallet unlock failed"),
153  tr("The passphrase entered for the wallet decryption was incorrect."));
154  }
155  else
156  {
157  QDialog::accept(); // Success
158  }
159  break;
160  case Decrypt:
161  if(!model->setWalletEncrypted(false, oldpass))
162  {
163  QMessageBox::critical(this, tr("Wallet decryption failed"),
164  tr("The passphrase entered for the wallet decryption was incorrect."));
165  }
166  else
167  {
168  QDialog::accept(); // Success
169  }
170  break;
171  case ChangePass:
172  if(newpass1 == newpass2)
173  {
174  if(model->changePassphrase(oldpass, newpass1))
175  {
176  QMessageBox::information(this, tr("Wallet encrypted"),
177  tr("Wallet passphrase was successfully changed."));
178  QDialog::accept(); // Success
179  }
180  else
181  {
182  QMessageBox::critical(this, tr("Wallet encryption failed"),
183  tr("The passphrase entered for the wallet decryption was incorrect."));
184  }
185  }
186  else
187  {
188  QMessageBox::critical(this, tr("Wallet encryption failed"),
189  tr("The supplied passphrases do not match."));
190  }
191  break;
192  }
193 }
194 
196 {
197  // Validate input, set Ok button to enabled when acceptable
198  bool acceptable = false;
199  switch(mode)
200  {
201  case Encrypt: // New passphrase x2
202  acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
203  break;
204  case Unlock: // Old passphrase x1
205  case Decrypt:
206  acceptable = !ui->passEdit1->text().isEmpty();
207  break;
208  case ChangePass: // Old passphrase x1, new passphrase x2
209  acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
210  break;
211  }
212  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
213 }
214 
215 bool AskPassphraseDialog::event(QEvent *event)
216 {
217  // Detect Caps Lock key press.
218  if (event->type() == QEvent::KeyPress) {
219  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
220  if (ke->key() == Qt::Key_CapsLock) {
221  fCapsLock = !fCapsLock;
222  }
223  if (fCapsLock) {
224  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
225  } else {
226  ui->capsLabel->clear();
227  }
228  }
229  return QWidget::event(event);
230 }
231 
232 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
233 {
234  /* Detect Caps Lock.
235  * There is no good OS-independent way to check a key state in Qt, but we
236  * can detect Caps Lock by checking for the following condition:
237  * Shift key is down and the result is a lower case character, or
238  * Shift key is not down and the result is an upper case character.
239  */
240  if (event->type() == QEvent::KeyPress) {
241  QKeyEvent *ke = static_cast<QKeyEvent *>(event);
242  QString str = ke->text();
243  if (str.length() != 0) {
244  const QChar *psz = str.unicode();
245  bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
246  if ((fShift && *psz >= 'a' && *psz <= 'z') || (!fShift && *psz >= 'A' && *psz <= 'Z')) {
247  fCapsLock = true;
248  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
249  } else if (psz->isLetter()) {
250  fCapsLock = false;
251  ui->capsLabel->clear();
252  }
253  }
254  }
255  return QDialog::eventFilter(object, event);
256 }
void setupUi(QDialog *AskPassphraseDialog)
bool event(QEvent *event)
Ask passphrase twice and encrypt.
Ask passphrase and unlock.
Ui::AskPassphraseDialog * ui
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: allocators.h:254
QDialogButtonBox * buttonBox
AskPassphraseDialog(Mode mode, QWidget *parent)
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
Interface to Anoncoin wallet from Qt view code.
Definition: walletmodel.h:97
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)