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