1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "sslserver.h"
24 # include <QSslSocket>
34 SslServer::SslServer(QObject* parent)
37 // Keep track if the SSL warning has been mentioned at least once before
38 static bool sslWarningShown = false;
40 if (Quassel::isOptionSet("ssl-cert")) {
41 _sslCertPath = Quassel::optionValue("ssl-cert");
44 _sslCertPath = Quassel::configDirPath() + "quasselCert.pem";
47 if (Quassel::isOptionSet("ssl-key")) {
48 _sslKeyPath = Quassel::optionValue("ssl-key");
51 _sslKeyPath = _sslCertPath;
54 // Initialize the certificates for first-time usage
56 if (!sslWarningShown) {
57 qWarning() << "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;
65 QTcpSocket* SslServer::nextPendingConnection()
67 if (_pendingConnections.isEmpty())
70 return _pendingConnections.takeFirst();
73 void SslServer::incomingConnection(qintptr socketDescriptor)
75 auto* serverSocket = new QSslSocket(this);
76 if (serverSocket->setSocketDescriptor(socketDescriptor)) {
78 serverSocket->setLocalCertificate(_cert);
79 serverSocket->setPrivateKey(_key);
80 serverSocket->addCaCertificates(_ca);
82 _pendingConnections << serverSocket;
90 bool SslServer::loadCerts()
92 // Load the certificates specified in the path. If needed, other prep work can be done here.
93 return setCertificate(_sslCertPath, _sslKeyPath);
96 bool SslServer::reloadCerts()
102 // Reloading certificates currently only occur in response to a request. Always print an
103 // error if something goes wrong, in order to simplify checking if it's working.
105 qWarning() << "SslServer: Unable to reload certificate file, reverting\n"
106 << " Quassel Core will use the previous key to provide SSL for client connections.\n"
107 << " Please see https://quassel-irc.org/faq/cert to learn how to enable SSL support.";
110 qWarning() << "SslServer: Unable to reload certificate file\n"
111 << " Quassel Core will still work, but cannot provide SSL for client connections.\n"
112 << " Please see https://quassel-irc.org/faq/cert to learn how to enable SSL support.";
118 bool SslServer::setCertificate(const QString& path, const QString& keyPath)
120 // Don't reset _isCertValid here, in case an older but valid certificate is still loaded.
121 // Use temporary variables in order to avoid overwriting the existing certificates until
122 // everything is confirmed good.
123 QSslCertificate untestedCert;
124 QList<QSslCertificate> untestedCA;
130 QFile certFile(path);
131 if (!certFile.exists()) {
132 qWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
136 if (!certFile.open(QIODevice::ReadOnly)) {
137 qWarning() << "SslServer: Failed to open certificate file" << qPrintable(path) << "error:" << certFile.error();
141 QList<QSslCertificate> certList = QSslCertificate::fromDevice(&certFile);
143 if (certList.isEmpty()) {
144 qWarning() << "SslServer: Certificate file doesn't contain a certificate";
148 untestedCert = certList[0];
149 certList.removeFirst(); // remove server cert
151 // store CA and intermediates certs
152 untestedCA = certList;
154 if (!certFile.reset()) {
155 qWarning() << "SslServer: IO error reading certificate file";
159 // load key from keyPath if it differs from path, otherwise load key from path
160 if (path != keyPath) {
161 QFile keyFile(keyPath);
162 if (!keyFile.exists()) {
163 qWarning() << "SslServer: Key file" << qPrintable(keyPath) << "does not exist";
167 if (!keyFile.open(QIODevice::ReadOnly)) {
168 qWarning() << "SslServer: Failed to open key file" << qPrintable(keyPath) << "error:" << keyFile.error();
172 untestedKey = loadKey(&keyFile);
176 untestedKey = loadKey(&certFile);
181 if (untestedCert.isNull()) {
182 qWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
186 // We allow the core to offer SSL anyway, so no "return false" here. Client will warn about the cert being invalid.
187 const QDateTime now = QDateTime::currentDateTime();
188 if (now < untestedCert.effectiveDate()) {
189 qWarning() << "SslServer: Certificate won't be valid before" << untestedCert.effectiveDate().toString();
191 else if (now > untestedCert.expiryDate()) {
192 qWarning() << "SslServer: Certificate expired on" << untestedCert.expiryDate().toString();
194 else if (untestedCert.isBlacklisted()) {
195 qWarning() << "SslServer: Certificate blacklisted";
198 if (untestedKey.isNull()) {
199 qWarning() << "SslServer:" << qPrintable(keyPath) << "contains no key data";
203 _certificateExpires = untestedCert.expiryDate();
204 if (_metricsServer) {
205 _metricsServer->setCertificateExpires(_certificateExpires);
210 // All keys are valid, update the externally visible copy used for new connections.
211 _cert = untestedCert;
218 QSslKey SslServer::loadKey(QFile* keyFile)
221 key = QSslKey(keyFile, QSsl::Rsa);
223 if (!keyFile->reset()) {
224 qWarning() << "SslServer: IO error reading key file";
227 key = QSslKey(keyFile, QSsl::Ec);
232 void SslServer::setMetricsServer(MetricsServer* metricsServer) {
233 _metricsServer = metricsServer;
234 if (_metricsServer) {
235 _metricsServer->setCertificateExpires(_certificateExpires);