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