Add support for Elliptic Curve keys for CertFP
[quassel.git] / src / qtui / settingspages / identityeditwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "identityeditwidget.h"
22
23 #include <QDragEnterEvent>
24 #include <QDropEvent>
25 #include <QFileDialog>
26 #include <QIcon>
27 #include <QMimeData>
28 #include <QUrl>
29 #include <QMessageBox>
30
31 #if QT_VERSION < 0x050000
32 #  include <QDesktopServices>
33 #else
34 #  include <QStandardPaths>
35 #endif
36
37 #include "client.h"
38
39 IdentityEditWidget::IdentityEditWidget(QWidget *parent)
40     : QWidget(parent)
41 {
42     ui.setupUi(this);
43
44     ui.addNick->setIcon(QIcon::fromTheme("list-add"));
45     ui.deleteNick->setIcon(QIcon::fromTheme("edit-delete"));
46     ui.renameNick->setIcon(QIcon::fromTheme("edit-rename"));
47     ui.nickUp->setIcon(QIcon::fromTheme("go-up"));
48     ui.nickDown->setIcon(QIcon::fromTheme("go-down"));
49
50     // We need to know whenever the state of input widgets changes...
51     connect(ui.realName, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
52     connect(ui.nicknameList, SIGNAL(itemChanged(QListWidgetItem *)), this, SIGNAL(widgetHasChanged()));
53     connect(ui.awayNick, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
54     connect(ui.awayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
55     connect(ui.autoAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
56     connect(ui.autoAwayTime, SIGNAL(valueChanged(int)), this, SIGNAL(widgetHasChanged()));
57     connect(ui.autoAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
58     connect(ui.autoAwayReasonEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
59     connect(ui.detachAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
60     connect(ui.detachAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
61     connect(ui.ident, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
62     connect(ui.kickReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
63     connect(ui.partReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
64     connect(ui.quitReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
65
66     setWidgetStates();
67     connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
68
69     connect(ui.continueUnsecured, SIGNAL(clicked()), this, SIGNAL(requestEditSsl()));
70
71     // we would need this if we enabled drag and drop in the nicklist...
72     //connect(ui.nicknameList, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(setWidgetStates()));
73     //connect(ui.nicknameList->model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(nicklistHasChanged()));
74
75     // disabling unused stuff
76     ui.autoAwayEnabled->hide();
77     ui.awayNick->hide();
78     ui.awayNickLabel->hide();
79
80     ui.detachAwayEnabled->setVisible(!Client::internalCore());
81
82 #ifdef HAVE_SSL
83     ui.sslKeyGroupBox->setAcceptDrops(true);
84     ui.sslKeyGroupBox->installEventFilter(this);
85     ui.sslCertGroupBox->setAcceptDrops(true);
86     ui.sslCertGroupBox->installEventFilter(this);
87 #endif
88
89     if (Client::isCoreFeatureEnabled(Quassel::Feature::AwayFormatTimestamp)) {
90         // Core allows formatting %%timestamp%% messages in away strings.  Update tooltips.
91         QString strFormatTooltip;
92         QTextStream formatTooltip( &strFormatTooltip, QIODevice::WriteOnly );
93         formatTooltip << "<qt><style>.bold { font-weight: bold; } "
94                          ".italic { font-style: italic; }</style>";
95
96         // Function to add a row to the tooltip table
97         auto addRow = [&](const QString& key, const QString& value, bool condition) {
98             if (condition) {
99                 formatTooltip << "<tr><td class='bold' align='right'>"
100                         << key << "</td><td>" << value << "</td></tr>";
101             }
102         };
103
104         // Original tooltip goes here
105         formatTooltip << "<p>%1</p>";
106         // New timestamp formatting guide here
107         formatTooltip << "<p>" << tr("You can add date/time to this message "
108                                "using the syntax: "
109                                "<br/>%%<span class='italic'>&lt;format&gt;</span>%%, where "
110                                "<span class='italic'>&lt;format&gt;</span> is:") << "</p>";
111         formatTooltip << "<table cellspacing='5' cellpadding='0'>";
112         addRow("hh", tr("the hour"), true);
113         addRow("mm", tr("the minutes"), true);
114         addRow("ss", tr("seconds"), true);
115         addRow("AP", tr("AM/PM"), true);
116         addRow("dd", tr("day"), true);
117         addRow("MM", tr("month"), true);
118 #if QT_VERSION > 0x050000
119         // Alas, this was only added in Qt 5.  We don't know what version the core has, just hope
120         // for the best (Qt 4 will soon be dropped).
121         addRow("t", tr("current timezone"), true);
122 #endif
123         formatTooltip << "</table>";
124         formatTooltip << "<p>" << tr("Example: Away since %%hh:mm%% on %%dd.MM%%.") << "</p>";
125         formatTooltip << "<p>" << tr("%%%% without anything inside represents %%.  Other format "
126                                      "codes are available.") << "</p>";
127         formatTooltip << "</qt>";
128
129         // Split up the message to allow re-using translations:
130         // [Original tool-tip]  [Timestamp format message]
131         ui.awayReason->setToolTip(strFormatTooltip.arg(ui.awayReason->toolTip()));
132         ui.detachAwayEnabled->setToolTip(strFormatTooltip.arg(ui.detachAwayEnabled->toolTip()));
133     } // else: Do nothing, leave the original translated string
134 }
135
136
137 void IdentityEditWidget::setWidgetStates()
138 {
139     if (ui.nicknameList->selectedItems().count()) {
140         ui.renameNick->setEnabled(true);
141         ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
142         ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
143     }
144     else {
145         ui.renameNick->setDisabled(true);
146         ui.nickUp->setDisabled(true);
147         ui.nickDown->setDisabled(true);
148     }
149     ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
150 }
151
152
153 void IdentityEditWidget::displayIdentity(CertIdentity *id, CertIdentity *saveId)
154 {
155     if (saveId) {
156         saveToIdentity(saveId);
157     }
158
159     if (!id)
160         return;
161
162     ui.realName->setText(id->realName());
163     ui.nicknameList->clear();
164     ui.nicknameList->addItems(id->nicks());
165     //for(int i = 0; i < ui.nicknameList->count(); i++) {
166     //  ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
167     //}
168     if (ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
169     ui.awayNick->setText(id->awayNick());
170     ui.awayReason->setText(id->awayReason());
171     ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
172     ui.autoAwayTime->setValue(id->autoAwayTime());
173     ui.autoAwayReason->setText(id->autoAwayReason());
174     ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
175     ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
176     ui.detachAwayReason->setText(id->detachAwayReason());
177     ui.ident->setText(id->ident());
178     ui.kickReason->setText(id->kickReason());
179     ui.partReason->setText(id->partReason());
180     ui.quitReason->setText(id->quitReason());
181 #ifdef HAVE_SSL
182     showKeyState(id->sslKey());
183     showCertState(id->sslCert());
184 #endif
185 }
186
187
188 void IdentityEditWidget::saveToIdentity(CertIdentity *id)
189 {
190     QRegExp linebreaks = QRegExp("[\\r\\n]");
191     id->setRealName(ui.realName->text());
192     QStringList nicks;
193     for (int i = 0; i < ui.nicknameList->count(); i++) {
194         nicks << ui.nicknameList->item(i)->text();
195     }
196     id->setNicks(nicks);
197     id->setAwayNick(ui.awayNick->text());
198     id->setAwayNickEnabled(true);
199     id->setAwayReason(ui.awayReason->text().remove(linebreaks));
200     id->setAwayReasonEnabled(true);
201     id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
202     id->setAutoAwayTime(ui.autoAwayTime->value());
203     id->setAutoAwayReason(ui.autoAwayReason->text().remove(linebreaks));
204     id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
205     id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
206     id->setDetachAwayReason(ui.detachAwayReason->text().remove(linebreaks));
207     id->setDetachAwayReasonEnabled(true);
208     id->setIdent(ui.ident->text());
209     id->setKickReason(ui.kickReason->text().remove(linebreaks));
210     id->setPartReason(ui.partReason->text().remove(linebreaks));
211     id->setQuitReason(ui.quitReason->text().remove(linebreaks));
212 #ifdef HAVE_SSL
213     id->setSslKey(QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
214     id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
215 #endif
216 }
217
218
219 void IdentityEditWidget::on_addNick_clicked()
220 {
221     QStringList existing;
222     for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
223     NickEditDlg dlg(QString(), existing, this);
224     if (dlg.exec() == QDialog::Accepted) {
225         ui.nicknameList->addItem(dlg.nick());
226         ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
227         setWidgetStates();
228         emit widgetHasChanged();
229     }
230 }
231
232
233 void IdentityEditWidget::on_deleteNick_clicked()
234 {
235     // no confirmation, since a nickname is really nothing hard to recreate
236     if (ui.nicknameList->selectedItems().count()) {
237         delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
238         ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
239         setWidgetStates();
240         emit widgetHasChanged();
241     }
242 }
243
244
245 void IdentityEditWidget::on_renameNick_clicked()
246 {
247     if (!ui.nicknameList->selectedItems().count()) return;
248     QString old = ui.nicknameList->selectedItems()[0]->text();
249     QStringList existing;
250     for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
251     NickEditDlg dlg(old, existing, this);
252     if (dlg.exec() == QDialog::Accepted) {
253         ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
254     }
255 }
256
257
258 void IdentityEditWidget::on_nickUp_clicked()
259 {
260     if (!ui.nicknameList->selectedItems().count()) return;
261     int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
262     if (row > 0) {
263         ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
264         ui.nicknameList->setCurrentRow(row-1);
265         setWidgetStates();
266         emit widgetHasChanged();
267     }
268 }
269
270
271 void IdentityEditWidget::on_nickDown_clicked()
272 {
273     if (!ui.nicknameList->selectedItems().count()) return;
274     int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
275     if (row < ui.nicknameList->count()-1) {
276         ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
277         ui.nicknameList->setCurrentRow(row+1);
278         setWidgetStates();
279         emit widgetHasChanged();
280     }
281 }
282
283
284 void IdentityEditWidget::showAdvanced(bool advanced)
285 {
286     int idx = ui.tabWidget->indexOf(ui.advancedTab);
287     if (advanced) {
288         if (idx != -1)
289             return;  // already added
290         ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
291     }
292     else {
293         if (idx == -1)
294             return;  // already removed
295         ui.tabWidget->removeTab(idx);
296     }
297 }
298
299
300 void IdentityEditWidget::setSslState(SslState state)
301 {
302     switch (state) {
303     case NoSsl:
304         ui.keyAndCertSettings->setCurrentIndex(0);
305         break;
306     case UnsecureSsl:
307         ui.keyAndCertSettings->setCurrentIndex(1);
308         break;
309     case AllowSsl:
310         ui.keyAndCertSettings->setCurrentIndex(2);
311         break;
312     }
313 }
314
315
316 #ifdef HAVE_SSL
317 bool IdentityEditWidget::eventFilter(QObject *watched, QEvent *event)
318 {
319     bool isCert = (watched == ui.sslCertGroupBox);
320     switch (event->type()) {
321     case QEvent::DragEnter:
322         sslDragEnterEvent(static_cast<QDragEnterEvent *>(event));
323         return true;
324     case QEvent::Drop:
325         sslDropEvent(static_cast<QDropEvent *>(event), isCert);
326         return true;
327     default:
328         return false;
329     }
330 }
331
332
333 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event)
334 {
335     if (event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
336         event->setDropAction(Qt::CopyAction);
337         event->accept();
338     }
339 }
340
341
342 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert)
343 {
344     QByteArray rawUris;
345     if (event->mimeData()->hasFormat("text/uri-list"))
346         rawUris = event->mimeData()->data("text/uri-list");
347     else
348         rawUris = event->mimeData()->data("text/uri");
349
350     QTextStream uriStream(rawUris);
351     QString filename = QUrl(uriStream.readLine()).toLocalFile();
352
353     if (isCert) {
354         QSslCertificate cert = certByFilename(filename);
355         if (!cert.isNull())
356             showCertState(cert);
357     }
358     else {
359         QSslKey key = keyByFilename(filename);
360         if (!key.isNull())
361             showKeyState(key);
362     }
363     event->accept();
364     emit widgetHasChanged();
365 }
366
367
368 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked()
369 {
370     QSslKey key;
371
372     if (ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
373         key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"),
374 #if QT_VERSION < 0x050000
375                                                          QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
376 #else
377                                                          QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
378 #endif
379
380     showKeyState(key);
381     emit widgetHasChanged();
382 }
383
384
385 QSslKey IdentityEditWidget::keyByFilename(const QString &filename)
386 {
387     QSslKey key;
388
389     QFile keyFile(filename);
390     keyFile.open(QIODevice::ReadOnly);
391     QByteArray keyRaw = keyFile.read(2 << 20);
392     keyFile.close();
393
394     for (int i = 0; i < 2; i++) {
395 #if QT_VERSION >= 0x050500
396         // On Qt5.5+, support QSsl::KeyAlgorithm::Rsa (1), QSsl::KeyAlgorithm::Dsa (2), and QSsl::KeyAlgorithm::Ec (3)
397         for (int j = 1; j < 4; j++) {
398 #elif QT_VERSION >= 0x050000
399         // On Qt5.0-Qt5.4, support QSsl::KeyAlgorithm::Rsa (1) and QSsl::KeyAlgorithm::Dsa (2) (Ec wasn't added until 5.5)
400         for (int j = 1; j < 3; j++) {
401 #else
402         // On Qt4, support QSsl::KeyAlgorithm::Rsa (0) and QSsl::KeyAlgorithm::Dsa (1) (Qt4 uses different indices for the values)
403         for (int j = 0; j < 2; j++) {
404 #endif
405             key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
406             if (!key.isNull())
407                 goto returnKey;
408         }
409     }
410     QMessageBox::information(this, tr("Failed to read key"), tr("Failed to read the key file. It is either incompatible or invalid. Note that the key file must not have a passphrase."));
411 returnKey:
412 #if QT_VERSION >= 0x050500
413     if(!key.isNull() && key.algorithm() == QSsl::KeyAlgorithm::Ec && !Client::isCoreFeatureEnabled(Quassel::Feature::EcdsaCertfpKeys)) {
414         QMessageBox::information(this, tr("Core does not support ECDSA keys"), tr("You loaded an ECDSA key, but the core does not support ECDSA keys. Please contact the core administrator."));
415         key.clear();
416     }
417 #endif
418     return key;
419 }
420
421
422 void IdentityEditWidget::showKeyState(const QSslKey &key)
423 {
424     if (key.isNull()) {
425         ui.keyTypeLabel->setText(tr("No Key loaded"));
426         ui.clearOrLoadKeyButton->setText(tr("Load"));
427     }
428     else {
429         switch (key.algorithm()) {
430         case QSsl::Rsa:
431             ui.keyTypeLabel->setText(tr("RSA"));
432             break;
433 #if QT_VERSION >= 0x050500
434         case QSsl::Ec:
435             ui.keyTypeLabel->setText(tr("ECDSA"));
436             break;
437 #endif
438         case QSsl::Dsa:
439             ui.keyTypeLabel->setText(tr("DSA"));
440             break;
441         default:
442             ui.keyTypeLabel->setText(tr("Invalid key or no key loaded"));
443         }
444         ui.clearOrLoadKeyButton->setText(tr("Clear"));
445     }
446     ui.keyTypeLabel->setProperty("sslKey", key.toPem());
447     ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
448 }
449
450
451 void IdentityEditWidget::on_clearOrLoadCertButton_clicked()
452 {
453     QSslCertificate cert;
454
455     if (ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
456         cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"),
457 #if QT_VERSION < 0x050000
458                                                            QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
459 #else
460                                                            QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
461 #endif
462     showCertState(cert);
463     emit widgetHasChanged();
464 }
465
466
467 QSslCertificate IdentityEditWidget::certByFilename(const QString &filename)
468 {
469     QSslCertificate cert;
470     QFile certFile(filename);
471     certFile.open(QIODevice::ReadOnly);
472     QByteArray certRaw = certFile.read(2 << 20);
473     certFile.close();
474
475     for (int i = 0; i < 2; i++) {
476         cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
477         if (!cert.isNull())
478             break;
479     }
480     return cert;
481 }
482
483
484 void IdentityEditWidget::showCertState(const QSslCertificate &cert)
485 {
486     if (cert.isNull()) {
487         ui.certOrgLabel->setText(tr("No Certificate loaded"));
488         ui.certCNameLabel->setText(tr("No Certificate loaded"));
489         ui.clearOrLoadCertButton->setText(tr("Load"));
490     }
491     else {
492 #if QT_VERSION < 0x050000
493         ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
494         ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
495 #else
496         ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization).join(", "));
497         ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName).join(", "));
498 #endif
499         ui.clearOrLoadCertButton->setText(tr("Clear"));
500     }
501     ui.certOrgLabel->setProperty("sslCert", cert.toPem());
502 }
503
504
505 #endif //HAVE_SSL