1 /***************************************************************************
2 * Copyright (C) 2005-2020 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"
29 SslServer::SslServer(QObject* parent)
32 // Keep track if the SSL warning has been mentioned at least once before
33 static bool sslWarningShown = false;
35 if (Quassel::isOptionSet("ssl-cert")) {
36 _sslCertPath = Quassel::optionValue("ssl-cert");
39 _sslCertPath = Quassel::configDirPath() + "quasselCert.pem";
42 if (Quassel::isOptionSet("ssl-key")) {
43 _sslKeyPath = Quassel::optionValue("ssl-key");
46 _sslKeyPath = _sslCertPath;
49 // Initialize the certificates for first-time usage
51 if (!sslWarningShown) {
52 qWarning() << "SslServer: Unable to set certificate file\n"
53 << " Quassel Core will still work, but cannot provide SSL for client connections.\n"
54 << " Please see https://quassel-irc.org/faq/cert to learn how to enable SSL support.";
55 sslWarningShown = true;
60 QTcpSocket* SslServer::nextPendingConnection()
62 if (_pendingConnections.isEmpty())
65 return _pendingConnections.takeFirst();
68 void SslServer::incomingConnection(qintptr socketDescriptor)
70 auto* serverSocket = new QSslSocket(this);
71 if (serverSocket->setSocketDescriptor(socketDescriptor)) {
73 serverSocket->setLocalCertificate(_cert);
74 serverSocket->setPrivateKey(_key);
75 serverSocket->addCaCertificates(_ca);
77 _pendingConnections << serverSocket;
85 bool SslServer::loadCerts()
87 // Load the certificates specified in the path. If needed, other prep work can be done here.
88 return setCertificate(_sslCertPath, _sslKeyPath);
91 bool SslServer::reloadCerts()
97 // Reloading certificates currently only occur in response to a request. Always print an
98 // error if something goes wrong, in order to simplify checking if it's working.
100 qWarning() << "SslServer: Unable to reload certificate file, reverting\n"
101 << " Quassel Core will use the previous key to provide SSL for client connections.\n"
102 << " Please see https://quassel-irc.org/faq/cert to learn how to enable SSL support.";
105 qWarning() << "SslServer: Unable to reload certificate file\n"
106 << " Quassel Core will still work, but cannot provide SSL for client connections.\n"
107 << " Please see https://quassel-irc.org/faq/cert to learn how to enable SSL support.";
113 bool SslServer::setCertificate(const QString& path, const QString& keyPath)
115 // Don't reset _isCertValid here, in case an older but valid certificate is still loaded.
116 // Use temporary variables in order to avoid overwriting the existing certificates until
117 // everything is confirmed good.
118 QSslCertificate untestedCert;
119 QList<QSslCertificate> untestedCA;
125 QFile certFile(path);
126 if (!certFile.exists()) {
127 qWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
131 if (!certFile.open(QIODevice::ReadOnly)) {
132 qWarning() << "SslServer: Failed to open certificate file" << qPrintable(path) << "error:" << certFile.error();
136 QList<QSslCertificate> certList = QSslCertificate::fromDevice(&certFile);
138 if (certList.isEmpty()) {
139 qWarning() << "SslServer: Certificate file doesn't contain a certificate";
143 untestedCert = certList[0];
144 certList.removeFirst(); // remove server cert
146 // store CA and intermediates certs
147 untestedCA = certList;
149 if (!certFile.reset()) {
150 qWarning() << "SslServer: IO error reading certificate file";
154 // load key from keyPath if it differs from path, otherwise load key from path
155 if (path != keyPath) {
156 QFile keyFile(keyPath);
157 if (!keyFile.exists()) {
158 qWarning() << "SslServer: Key file" << qPrintable(keyPath) << "does not exist";
162 if (!keyFile.open(QIODevice::ReadOnly)) {
163 qWarning() << "SslServer: Failed to open key file" << qPrintable(keyPath) << "error:" << keyFile.error();
167 untestedKey = loadKey(&keyFile);
171 untestedKey = loadKey(&certFile);
176 if (untestedCert.isNull()) {
177 qWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
181 // We allow the core to offer SSL anyway, so no "return false" here. Client will warn about the cert being invalid.
182 const QDateTime now = QDateTime::currentDateTime();
183 if (now < untestedCert.effectiveDate()) {
184 qWarning() << "SslServer: Certificate won't be valid before" << untestedCert.effectiveDate().toString();
186 else if (now > untestedCert.expiryDate()) {
187 qWarning() << "SslServer: Certificate expired on" << untestedCert.expiryDate().toString();
189 else if (untestedCert.isBlacklisted()) {
190 qWarning() << "SslServer: Certificate blacklisted";
193 if (untestedKey.isNull()) {
194 qWarning() << "SslServer:" << qPrintable(keyPath) << "contains no key data";
198 _certificateExpires = untestedCert.expiryDate();
199 if (_metricsServer) {
200 _metricsServer->setCertificateExpires(_certificateExpires);
205 // All keys are valid, update the externally visible copy used for new connections.
206 _cert = untestedCert;
213 QSslKey SslServer::loadKey(QFile* keyFile)
216 key = QSslKey(keyFile, QSsl::Rsa);
218 if (!keyFile->reset()) {
219 qWarning() << "SslServer: IO error reading key file";
222 key = QSslKey(keyFile, QSsl::Ec);
227 void SslServer::setMetricsServer(MetricsServer* metricsServer) {
228 _metricsServer = metricsServer;
229 if (_metricsServer) {
230 _metricsServer->setCertificateExpires(_certificateExpires);