1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/*
* Copyright (C) by Klaas Freitag <freitag@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "configfile.h"
#include "sslerrordialog.h"
#include <QtGui>
#include <QtNetwork>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QtWidgets>
#endif
#include "ui_sslerrordialog.h"
namespace OCC {
Q_LOGGING_CATEGORY(lcSslErrorDialog, "gui.sslerrordialog", QtInfoMsg)
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
namespace Utility {
// Used for QSSLCertificate::subjectInfo which returns a QStringList in Qt5, but a QString in Qt4
QString escape(const QStringList &l) { return escape(l.join(';')); }
}
#endif
bool SslDialogErrorHandler::handleErrors(QList<QSslError> errors, const QSslConfiguration &conf, QList<QSslCertificate> *certs, AccountPtr account)
{
(void)conf;
if (!certs) {
qCCritical(lcSslErrorDialog) << "Certs parameter required but is NULL!";
return false;
}
SslErrorDialog dlg(account);
// whether the failing certs have previously been accepted
if (dlg.checkFailingCertsKnown(errors)) {
*certs = dlg.unknownCerts();
return true;
}
// whether the user accepted the certs
if (dlg.exec() == QDialog::Accepted) {
if (dlg.trustConnection()) {
*certs = dlg.unknownCerts();
return true;
}
}
return false;
}
SslErrorDialog::SslErrorDialog(AccountPtr account, QWidget *parent)
: QDialog(parent)
, _allTrusted(false)
, _ui(new Ui::SslErrorDialog)
, _account(account)
{
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
_ui->setupUi(this);
setWindowTitle(tr("Untrusted Certificate"));
QPushButton *okButton =
_ui->_dialogButtonBox->button(QDialogButtonBox::Ok);
QPushButton *cancelButton =
_ui->_dialogButtonBox->button(QDialogButtonBox::Cancel);
okButton->setEnabled(false);
connect(_ui->_cbTrustConnect, SIGNAL(clicked(bool)),
okButton, SLOT(setEnabled(bool)));
if (okButton) {
okButton->setDefault(true);
connect(okButton, SIGNAL(clicked()), SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), SLOT(reject()));
}
}
SslErrorDialog::~SslErrorDialog()
{
delete _ui;
}
QString SslErrorDialog::styleSheet() const
{
const QString style = QLatin1String(
"#cert {margin-left: 5px;} "
"#ca_error { color:#a00011; margin-left:5px; margin-right:5px; }"
"#ca_error p { margin-top: 2px; margin-bottom:2px; }"
"#ccert { margin-left: 5px; }"
"#issuer { margin-left: 5px; }"
"tt { font-size: small; }");
return style;
}
#define QL(x) QLatin1String(x)
bool SslErrorDialog::checkFailingCertsKnown(const QList<QSslError> &errors)
{
// check if unknown certs caused errors.
_unknownCerts.clear();
QStringList errorStrings;
QList<QSslCertificate> trustedCerts = _account->approvedCerts();
for (int i = 0; i < errors.count(); ++i) {
QSslError error = errors.at(i);
if (trustedCerts.contains(error.certificate()) || _unknownCerts.contains(error.certificate())) {
continue;
}
errorStrings += error.errorString();
if (!error.certificate().isNull()) {
_unknownCerts.append(error.certificate());
}
}
// if there are no errors left, all Certs were known.
if (errorStrings.isEmpty()) {
_allTrusted = true;
return true;
}
QString msg = QL("<html><head>");
msg += QL("<link rel='stylesheet' type='text/css' href='format.css'>");
msg += QL("</head><body>");
auto host = _account->url().host();
msg += QL("<h3>") + tr("Cannot connect securely to <i>%1</i>:").arg(host) + QL("</h3>");
// loop over the unknown certs and line up their errors.
msg += QL("<div id=\"ca_errors\">");
foreach (const QSslCertificate &cert, _unknownCerts) {
msg += QL("<div id=\"ca_error\">");
// add the errors for this cert
foreach (QSslError err, errors) {
if (err.certificate() == cert) {
msg += QL("<p>") + err.errorString() + QL("</p>");
}
}
msg += QL("</div>");
msg += certDiv(cert);
if (_unknownCerts.count() > 1) {
msg += QL("<hr/>");
}
}
msg += QL("</div></body></html>");
QTextDocument *doc = new QTextDocument(0);
QString style = styleSheet();
doc->addResource(QTextDocument::StyleSheetResource, QUrl(QL("format.css")), style);
doc->setHtml(msg);
_ui->_tbErrors->setDocument(doc);
_ui->_tbErrors->show();
return false;
}
QString SslErrorDialog::certDiv(QSslCertificate cert) const
{
QString msg;
msg += QL("<div id=\"cert\">");
msg += QL("<h3>") + tr("with Certificate %1").arg(Utility::escape(cert.subjectInfo(QSslCertificate::CommonName))) + QL("</h3>");
msg += QL("<div id=\"ccert\">");
QStringList li;
QString org = Utility::escape(cert.subjectInfo(QSslCertificate::Organization));
QString unit = Utility::escape(cert.subjectInfo(QSslCertificate::OrganizationalUnitName));
QString country = Utility::escape(cert.subjectInfo(QSslCertificate::CountryName));
if (unit.isEmpty())
unit = tr("<not specified>");
if (org.isEmpty())
org = tr("<not specified>");
if (country.isEmpty())
country = tr("<not specified>");
li << tr("Organization: %1").arg(org);
li << tr("Unit: %1").arg(unit);
li << tr("Country: %1").arg(country);
msg += QL("<p>") + li.join(QL("<br/>")) + QL("</p>");
msg += QL("<p>");
QString md5sum = Utility::formatFingerprint(cert.digest(QCryptographicHash::Md5).toHex());
QString sha1sum = Utility::formatFingerprint(cert.digest(QCryptographicHash::Sha1).toHex());
msg += tr("Fingerprint (MD5): <tt>%1</tt>").arg(md5sum) + QL("<br/>");
msg += tr("Fingerprint (SHA1): <tt>%1</tt>").arg(sha1sum) + QL("<br/>");
msg += QL("<br/>");
msg += tr("Effective Date: %1").arg(cert.effectiveDate().toString()) + QL("<br/>");
msg += tr("Expiration Date: %1").arg(cert.expiryDate().toString()) + QL("</p>");
msg += QL("</div>");
msg += QL("<h3>") + tr("Issuer: %1").arg(Utility::escape(cert.issuerInfo(QSslCertificate::CommonName))) + QL("</h3>");
msg += QL("<div id=\"issuer\">");
li.clear();
li << tr("Organization: %1").arg(Utility::escape(cert.issuerInfo(QSslCertificate::Organization)));
li << tr("Unit: %1").arg(Utility::escape(cert.issuerInfo(QSslCertificate::OrganizationalUnitName)));
li << tr("Country: %1").arg(Utility::escape(cert.issuerInfo(QSslCertificate::CountryName)));
msg += QL("<p>") + li.join(QL("<br/>")) + QL("</p>");
msg += QL("</div>");
msg += QL("</div>");
return msg;
}
bool SslErrorDialog::trustConnection()
{
if (_allTrusted)
return true;
bool stat = (_ui->_cbTrustConnect->checkState() == Qt::Checked);
qCInfo(lcSslErrorDialog) << "SSL-Connection is trusted: " << stat;
return stat;
}
} // end namespace
|