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