qa: Resolve deprecation warnings in newer Qt versions
[quassel.git] / src / core / sslserver.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 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 "sslserver.h"
22
23 #include <QDateTime>
24 #include <QSslConfiguration>
25 #include <QSslSocket>
26
27 #include "core.h"
28 #include "quassel.h"
29
30 SslServer::SslServer(QObject* parent)
31     : QTcpServer(parent)
32 {
33     // Keep track if the SSL warning has been mentioned at least once before
34     static bool sslWarningShown = false;
35
36     if (Quassel::isOptionSet("ssl-cert")) {
37         _sslCertPath = Quassel::optionValue("ssl-cert");
38     }
39     else {
40         _sslCertPath = Quassel::configDirPath() + "quasselCert.pem";
41     }
42
43     if (Quassel::isOptionSet("ssl-key")) {
44         _sslKeyPath = Quassel::optionValue("ssl-key");
45     }
46     else {
47         _sslKeyPath = _sslCertPath;
48     }
49
50     // Initialize the certificates for first-time usage
51     if (!loadCerts()) {
52         if (!sslWarningShown) {
53             qWarning() << "SslServer: Unable to set certificate file\n"
54                        << "          Quassel Core will still work, but cannot provide SSL for client connections.\n"
55                        << "          Please see https://quassel-irc.org/faq/cert to learn how to enable SSL support.";
56             sslWarningShown = true;
57         }
58     }
59 }
60
61 void SslServer::incomingConnection(qintptr socketDescriptor)
62 {
63     auto* socket = new QSslSocket(this);
64     if (socket->setSocketDescriptor(socketDescriptor)) {
65         if (isCertValid()) {
66             auto config = socket->sslConfiguration();
67             config.setLocalCertificate(_cert);
68             config.setPrivateKey(_key);
69             auto certificates = config.caCertificates();
70             certificates += _ca;
71             config.setCaCertificates(certificates);
72             socket->setSslConfiguration(config);
73         }
74         addPendingConnection(socket);
75     }
76     else {
77         delete socket;
78     }
79 }
80
81 bool SslServer::loadCerts()
82 {
83     // Load the certificates specified in the path.  If needed, other prep work can be done here.
84     return setCertificate(_sslCertPath, _sslKeyPath);
85 }
86
87 bool SslServer::reloadCerts()
88 {
89     if (loadCerts()) {
90         return true;
91     }
92     else {
93         // Reloading certificates currently only occur in response to a request.  Always print an
94         // error if something goes wrong, in order to simplify checking if it's working.
95         if (isCertValid()) {
96             qWarning() << "SslServer: Unable to reload certificate file, reverting\n"
97                        << "          Quassel Core will use the previous key to provide SSL for client connections.\n"
98                        << "          Please see https://quassel-irc.org/faq/cert to learn how to enable SSL support.";
99         }
100         else {
101             qWarning() << "SslServer: Unable to reload certificate file\n"
102                        << "          Quassel Core will still work, but cannot provide SSL for client connections.\n"
103                        << "          Please see https://quassel-irc.org/faq/cert to learn how to enable SSL support.";
104         }
105         return false;
106     }
107 }
108
109 bool SslServer::setCertificate(const QString& path, const QString& keyPath)
110 {
111     // Don't reset _isCertValid here, in case an older but valid certificate is still loaded.
112     // Use temporary variables in order to avoid overwriting the existing certificates until
113     // everything is confirmed good.
114     QSslCertificate untestedCert;
115     QList<QSslCertificate> untestedCA;
116     QSslKey untestedKey;
117
118     if (path.isEmpty())
119         return false;
120
121     QFile certFile(path);
122     if (!certFile.exists()) {
123         qWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
124         return false;
125     }
126
127     if (!certFile.open(QIODevice::ReadOnly)) {
128         qWarning() << "SslServer: Failed to open certificate file" << qPrintable(path) << "error:" << certFile.error();
129         return false;
130     }
131
132     QList<QSslCertificate> certList = QSslCertificate::fromDevice(&certFile);
133
134     if (certList.isEmpty()) {
135         qWarning() << "SslServer: Certificate file doesn't contain a certificate";
136         return false;
137     }
138
139     untestedCert = certList[0];
140     certList.removeFirst();  // remove server cert
141
142     // store CA and intermediates certs
143     untestedCA = certList;
144
145     if (!certFile.reset()) {
146         qWarning() << "SslServer: IO error reading certificate file";
147         return false;
148     }
149
150     // load key from keyPath if it differs from path, otherwise load key from path
151     if (path != keyPath) {
152         QFile keyFile(keyPath);
153         if (!keyFile.exists()) {
154             qWarning() << "SslServer: Key file" << qPrintable(keyPath) << "does not exist";
155             return false;
156         }
157
158         if (!keyFile.open(QIODevice::ReadOnly)) {
159             qWarning() << "SslServer: Failed to open key file" << qPrintable(keyPath) << "error:" << keyFile.error();
160             return false;
161         }
162
163         untestedKey = loadKey(&keyFile);
164         keyFile.close();
165     }
166     else {
167         untestedKey = loadKey(&certFile);
168     }
169
170     certFile.close();
171
172     if (untestedCert.isNull()) {
173         qWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
174         return false;
175     }
176
177     // We allow the core to offer SSL anyway, so no "return false" here. Client will warn about the cert being invalid.
178     const QDateTime now = QDateTime::currentDateTime();
179     if (now < untestedCert.effectiveDate()) {
180         qWarning() << "SslServer: Certificate won't be valid before" << untestedCert.effectiveDate().toString();
181     }
182     else if (now > untestedCert.expiryDate()) {
183         qWarning() << "SslServer: Certificate expired on" << untestedCert.expiryDate().toString();
184     }
185     else if (untestedCert.isBlacklisted()) {
186         qWarning() << "SslServer: Certificate blacklisted";
187     }
188
189     if (untestedKey.isNull()) {
190         qWarning() << "SslServer:" << qPrintable(keyPath) << "contains no key data";
191         return false;
192     }
193
194     _certificateExpires = untestedCert.expiryDate();
195     if (_metricsServer) {
196         _metricsServer->setCertificateExpires(_certificateExpires);
197     }
198
199     _isCertValid = true;
200
201     // All keys are valid, update the externally visible copy used for new connections.
202     _cert = untestedCert;
203     _ca = untestedCA;
204     _key = untestedKey;
205
206     return _isCertValid;
207 }
208
209 QSslKey SslServer::loadKey(QFile* keyFile)
210 {
211     QSslKey key;
212     key = QSslKey(keyFile, QSsl::Rsa);
213     if (key.isNull()) {
214         if (!keyFile->reset()) {
215             qWarning() << "SslServer: IO error reading key file";
216             return key;
217         }
218         key = QSslKey(keyFile, QSsl::Ec);
219     }
220     return key;
221 }
222
223 void SslServer::setMetricsServer(MetricsServer* metricsServer)
224 {
225     _metricsServer = metricsServer;
226     if (_metricsServer) {
227         _metricsServer->setCertificateExpires(_certificateExpires);
228     }
229 }