1 | /*************************************************************************** |
2 | * Copyright (C) 2005-2014 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 <QDateTime> |
22 | #include <QHostAddress> |
23 | #include <QSslCipher> |
24 | #include <QSslSocket> |
25 | |
26 | #include "sslinfodlg.h" |
27 | #include "util.h" |
28 | |
29 | // ======================================== |
30 | // SslInfoDlg |
31 | // ======================================== |
32 | |
33 | SslInfoDlg::SslInfoDlg(const QSslSocket *socket, QWidget *parent) |
34 | : QDialog(parent), |
35 | _socket(socket) |
36 | { |
37 | ui.setupUi(this); |
38 | |
39 | QSslCipher cipher = socket->sessionCipher(); |
40 | |
41 | ui.hostname->setText(socket->peerName()); |
42 | ui.address->setText(socket->peerAddress().toString()); |
43 | ui.encryption->setText(cipher.name()); |
44 | ui.protocol->setText(cipher.protocolString()); |
45 | |
46 | connect(ui.certificateChain, SIGNAL(currentIndexChanged(int)), SLOT(setCurrentCert(int))); |
47 | foreach(const QSslCertificate &cert, socket->peerCertificateChain()) { |
48 | ui.certificateChain->addItem(subjectInfo(cert, QSslCertificate::CommonName)); |
49 | } |
50 | } |
51 | |
52 | |
53 | void SslInfoDlg::setCurrentCert(int index) |
54 | { |
55 | QSslCertificate cert = socket()->peerCertificateChain().at(index); |
56 | ui.subjectCommonName->setText(subjectInfo(cert, QSslCertificate::CommonName)); |
57 | ui.subjectOrganization->setText(subjectInfo(cert, QSslCertificate::Organization)); |
58 | ui.subjectOrganizationalUnit->setText(subjectInfo(cert, QSslCertificate::OrganizationalUnitName)); |
59 | ui.subjectCountry->setText(subjectInfo(cert, QSslCertificate::CountryName)); |
60 | ui.subjectState->setText(subjectInfo(cert, QSslCertificate::StateOrProvinceName)); |
61 | ui.subjectCity->setText(subjectInfo(cert, QSslCertificate::LocalityName)); |
62 | |
63 | ui.issuerCommonName->setText(issuerInfo(cert, QSslCertificate::CommonName)); |
64 | ui.issuerOrganization->setText(issuerInfo(cert, QSslCertificate::Organization)); |
65 | ui.issuerOrganizationalUnit->setText(issuerInfo(cert, QSslCertificate::OrganizationalUnitName)); |
66 | ui.issuerCountry->setText(issuerInfo(cert, QSslCertificate::CountryName)); |
67 | ui.issuerState->setText(issuerInfo(cert, QSslCertificate::StateOrProvinceName)); |
68 | ui.issuerCity->setText(issuerInfo(cert, QSslCertificate::LocalityName)); |
69 | |
70 | if (socket()->sslErrors().isEmpty()) |
71 | ui.trusted->setText(tr("Yes" )); |
72 | else { |
73 | QString errorString = tr("No, for the following reasons:<ul>" ); |
74 | foreach(const QSslError &error, socket()->sslErrors()) |
75 | errorString += "<li>" + error.errorString() + "</li>" ; |
76 | errorString += "</ul>" ; |
77 | ui.trusted->setText(errorString); |
78 | } |
79 | |
80 | ui.validity->setText(tr("%1 to %2" ).arg(cert.effectiveDate().date().toString(Qt::ISODate), cert.expiryDate().date().toString(Qt::ISODate))); |
81 | ui.md5Digest->setText(prettyDigest(cert.digest(QCryptographicHash::Md5))); |
82 | ui.sha1Digest->setText(prettyDigest(cert.digest(QCryptographicHash::Sha1))); |
83 | } |
84 | |
85 | // in Qt5, subjectInfo returns a QStringList(); turn this into a comma-separated string instead |
86 | QString SslInfoDlg::subjectInfo(const QSslCertificate &cert, QSslCertificate::SubjectInfo subjectInfo) const |
87 | { |
88 | #if QT_VERSION < 0x050000 |
89 | return cert.subjectInfo(subjectInfo); |
90 | #else |
91 | return cert.subjectInfo(subjectInfo).join(", " ); |
92 | #endif |
93 | } |
94 | |
95 | |
96 | // same here |
97 | QString SslInfoDlg::issuerInfo(const QSslCertificate &cert, QSslCertificate::SubjectInfo subjectInfo) const |
98 | { |
99 | #if QT_VERSION < 0x050000 |
100 | return cert.issuerInfo(subjectInfo); |
101 | #else |
102 | return cert.issuerInfo(subjectInfo).join(", " ); |
103 | #endif |
104 | } |
105 | |