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