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>
33 SslServer::SslServer(QObject* parent)
36 // Keep track if the SSL warning has been mentioned at least once before
37 static bool sslWarningShown = false;
39 if (Quassel::isOptionSet("ssl-cert")) {
40 _sslCertPath = Quassel::optionValue("ssl-cert");
43 _sslCertPath = Quassel::configDirPath() + "quasselCert.pem";
46 if (Quassel::isOptionSet("ssl-key")) {
47 _sslKeyPath = Quassel::optionValue("ssl-key");
50 _sslKeyPath = _sslCertPath;
53 // Initialize the certificates for first-time usage
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;
64 QTcpSocket* SslServer::nextPendingConnection()
66 if (_pendingConnections.isEmpty())
69 return _pendingConnections.takeFirst();
72 void SslServer::incomingConnection(qintptr socketDescriptor)
74 auto* serverSocket = new QSslSocket(this);
75 if (serverSocket->setSocketDescriptor(socketDescriptor)) {
77 serverSocket->setLocalCertificate(_cert);
78 serverSocket->setPrivateKey(_key);
79 serverSocket->addCaCertificates(_ca);
81 _pendingConnections << serverSocket;
89 bool SslServer::loadCerts()
91 // Load the certificates specified in the path. If needed, other prep work can be done here.
92 return setCertificate(_sslCertPath, _sslKeyPath);
95 bool SslServer::reloadCerts()
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.
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.";
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.";
117 bool SslServer::setCertificate(const QString& path, const QString& keyPath)
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;
129 QFile certFile(path);
130 if (!certFile.exists()) {
131 qWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
135 if (!certFile.open(QIODevice::ReadOnly)) {
136 qWarning() << "SslServer: Failed to open certificate file" << qPrintable(path) << "error:" << certFile.error();
140 QList<QSslCertificate> certList = QSslCertificate::fromDevice(&certFile);
142 if (certList.isEmpty()) {
143 qWarning() << "SslServer: Certificate file doesn't contain a certificate";
147 untestedCert = certList[0];
148 certList.removeFirst(); // remove server cert
150 // store CA and intermediates certs
151 untestedCA = certList;
153 if (!certFile.reset()) {
154 qWarning() << "SslServer: IO error reading certificate file";
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";
166 if (!keyFile.open(QIODevice::ReadOnly)) {
167 qWarning() << "SslServer: Failed to open key file" << qPrintable(keyPath) << "error:" << keyFile.error();
171 untestedKey = loadKey(&keyFile);
175 untestedKey = loadKey(&certFile);
180 if (untestedCert.isNull()) {
181 qWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
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();
190 else if (now > untestedCert.expiryDate()) {
191 qWarning() << "SslServer: Certificate expired on" << untestedCert.expiryDate().toString();
193 else if (untestedCert.isBlacklisted()) {
194 qWarning() << "SslServer: Certificate blacklisted";
197 if (untestedKey.isNull()) {
198 qWarning() << "SslServer:" << qPrintable(keyPath) << "contains no key data";
204 // All keys are valid, update the externally visible copy used for new connections.
205 _cert = untestedCert;
212 QSslKey SslServer::loadKey(QFile* keyFile)
215 key = QSslKey(keyFile, QSsl::Rsa);
217 if (!keyFile->reset()) {
218 qWarning() << "SslServer: IO error reading key file";
221 key = QSslKey(keyFile, QSsl::Ec);