Create wallet security menu
[novacoin.git] / src / qt / askpassphrasedialog.cpp
1 #include "askpassphrasedialog.h"
2 #include "ui_askpassphrasedialog.h"
3
4 #include "guiconstants.h"
5 #include "walletmodel.h"
6
7 #include <QMessageBox>
8 #include <QPushButton>
9 #include <QKeyEvent>
10
11 extern bool fWalletUnlockMintOnly;
12
13 AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
14     QDialog(parent),
15     ui(new Ui::AskPassphraseDialog),
16     mode(mode),
17     model(0),
18     fCapsLock(false)
19 {
20     ui->setupUi(this);
21     ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
22     ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
23     ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
24     
25     // Setup Caps Lock detection.
26     ui->passEdit1->installEventFilter(this);
27     ui->passEdit2->installEventFilter(this);
28     ui->passEdit3->installEventFilter(this);
29
30     switch(mode)
31     {
32         case Encrypt: // Ask passphrase x2
33             ui->passLabel1->hide();
34             ui->passEdit1->hide();
35             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>."));
36             setWindowTitle(tr("Encrypt wallet"));
37             break;
38         case Unlock: // Ask passphrase
39         case UnlockMining:
40             ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
41             ui->passLabel2->hide();
42             ui->passEdit2->hide();
43             ui->passLabel3->hide();
44             ui->passEdit3->hide();
45             setWindowTitle(tr("Unlock wallet"));
46             break;
47         case Decrypt:   // Ask passphrase
48             ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
49             ui->passLabel2->hide();
50             ui->passEdit2->hide();
51             ui->passLabel3->hide();
52             ui->passEdit3->hide();
53             setWindowTitle(tr("Decrypt wallet"));
54             break;
55         case ChangePass: // Ask old passphrase + new passphrase x2
56             setWindowTitle(tr("Change passphrase"));
57             ui->warningLabel->setText(tr("Enter the old and new passphrase to the wallet."));
58             break;
59     }
60
61     textChanged();
62     connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
63     connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
64     connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
65 }
66
67 AskPassphraseDialog::~AskPassphraseDialog()
68 {
69     // Attempt to overwrite text so that they do not linger around in memory
70     ui->passEdit1->setText(QString(" ").repeated(ui->passEdit1->text().size()));
71     ui->passEdit2->setText(QString(" ").repeated(ui->passEdit2->text().size()));
72     ui->passEdit3->setText(QString(" ").repeated(ui->passEdit3->text().size()));
73     delete ui;
74 }
75
76 void AskPassphraseDialog::setModel(WalletModel *model)
77 {
78     this->model = model;
79 }
80
81 void AskPassphraseDialog::accept()
82 {
83     SecureString oldpass, newpass1, newpass2;
84     if(!model)
85         return;
86     oldpass.reserve(MAX_PASSPHRASE_SIZE);
87     newpass1.reserve(MAX_PASSPHRASE_SIZE);
88     newpass2.reserve(MAX_PASSPHRASE_SIZE);
89     // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
90     // Alternately, find a way to make this input mlock()'d to begin with.
91     oldpass.assign(ui->passEdit1->text().toStdString().c_str());
92     newpass1.assign(ui->passEdit2->text().toStdString().c_str());
93     newpass2.assign(ui->passEdit3->text().toStdString().c_str());
94
95     switch(mode)
96     {
97     case Encrypt: {
98         if(newpass1.empty() || newpass2.empty())
99         {
100             // Cannot encrypt with empty passphrase
101             break;
102         }
103         QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
104                  tr("Warning: If you encrypt your wallet and lose your passphrase, you will <b>LOSE ALL OF YOUR COINS</b>!") + "<br><br>" + tr("Are you sure you wish to encrypt your wallet?"),
105                  QMessageBox::Yes|QMessageBox::Cancel,
106                  QMessageBox::Cancel);
107         if(retval == QMessageBox::Yes)
108         {
109             if(newpass1 == newpass2)
110             {
111                 if(model->setWalletEncrypted(true, newpass1))
112                 {
113                     QMessageBox::warning(this, tr("Wallet encrypted"),
114                                          "<qt>" + 
115                                          tr("NovaCoin will close now to finish the encryption process. "
116                                          "Remember that encrypting your wallet cannot fully protect "
117                                          "your coins from being stolen by malware infecting your computer.") + 
118                                          "<br><br><b>" + 
119                                          tr("IMPORTANT: Any previous backups you have made of your wallet file "
120                                          "should be replaced with the newly generated, encrypted wallet file. "
121                                          "For security reasons, previous backups of the unencrypted wallet file "
122                                          "will become useless as soon as you start using the new, encrypted wallet.") + 
123                                          "</b></qt>");
124                     QApplication::quit();
125                 }
126                 else
127                 {
128                     QMessageBox::critical(this, tr("Wallet encryption failed"),
129                                          tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
130                 }
131                 QDialog::accept(); // Success
132             }
133             else
134             {
135                 QMessageBox::critical(this, tr("Wallet encryption failed"),
136                                      tr("The supplied passphrases do not match."));
137             }
138         }
139         else
140         {
141             QDialog::reject(); // Cancelled
142         }
143         } break;
144     case Unlock:
145         if(!model->setWalletLocked(false, oldpass))
146         {
147             QMessageBox::critical(this, tr("Wallet unlock failed"),
148                                   tr("The passphrase entered for the wallet decryption was incorrect."));
149         }
150         else
151         {
152             QDialog::accept(); // Success
153         }
154         break;
155     case UnlockMining:
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             fWalletUnlockMintOnly = true;
165         }
166         break;
167     case Decrypt:
168         if(!model->setWalletEncrypted(false, oldpass))
169         {
170             QMessageBox::critical(this, tr("Wallet decryption failed"),
171                                   tr("The passphrase entered for the wallet decryption was incorrect."));
172         }
173         else
174         {
175             QDialog::accept(); // Success
176         }
177         break;
178     case ChangePass:
179         if(newpass1 == newpass2)
180         {
181             if(model->changePassphrase(oldpass, newpass1))
182             {
183                 QMessageBox::information(this, tr("Wallet encrypted"),
184                                      tr("Wallet passphrase was successfully changed."));
185                 QDialog::accept(); // Success
186             }
187             else
188             {
189                 QMessageBox::critical(this, tr("Wallet encryption failed"),
190                                      tr("The passphrase entered for the wallet decryption was incorrect."));
191             }
192         }
193         else
194         {
195             QMessageBox::critical(this, tr("Wallet encryption failed"),
196                                  tr("The supplied passphrases do not match."));
197         }
198         break;
199     }
200 }
201
202 void AskPassphraseDialog::textChanged()
203 {
204     // Validate input, set Ok button to enabled when acceptable
205     bool acceptable = false;
206     switch(mode)
207     {
208     case Encrypt: // New passphrase x2
209         acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
210         break;
211     case Unlock: // Old passphrase x1
212     case UnlockMining:
213     case Decrypt:
214         acceptable = !ui->passEdit1->text().isEmpty();
215         break;
216     case ChangePass: // Old passphrase x1, new passphrase x2
217         acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
218         break;
219     }
220     ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
221 }
222
223 bool AskPassphraseDialog::event(QEvent *event)
224 {
225     // Detect Caps Lock key press.
226     if (event->type() == QEvent::KeyPress) {
227         QKeyEvent *ke = static_cast<QKeyEvent *>(event);
228         if (ke->key() == Qt::Key_CapsLock) {
229             fCapsLock = !fCapsLock;
230         }
231         if (fCapsLock) {
232             ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
233         } else {
234             ui->capsLabel->clear();
235         }
236     }
237     return QWidget::event(event);
238 }
239
240 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
241 {
242     /* Detect Caps Lock.
243      * There is no good OS-independent way to check a key state in Qt, but we
244      * can detect Caps Lock by checking for the following condition:
245      * Shift key is down and the result is a lower case character, or
246      * Shift key is not down and the result is an upper case character.
247      */
248     if (event->type() == QEvent::KeyPress) {
249         QKeyEvent *ke = static_cast<QKeyEvent *>(event);
250         QString str = ke->text();
251         if (str.length() != 0) {
252             const QChar *psz = str.unicode();
253             bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
254             if ((fShift && psz->isLower()) || (!fShift && psz->isUpper())) {
255                 fCapsLock = true;
256                 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
257             } else if (psz->isLetter()) {
258                 fCapsLock = false;
259                 ui->capsLabel->clear();
260             }
261         }
262     }
263     return QDialog::eventFilter(object, event);
264 }