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