1 /***************************************************************************
2 * Copyright (C) 2005-2018 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>
30 #include "logmessage.h"
34 SslServer::SslServer(QObject *parent)
38 // Keep track if the SSL warning has been mentioned at least once before
39 static bool sslWarningShown = false;
41 if(Quassel::isOptionSet("ssl-cert")) {
42 _sslCertPath = Quassel::optionValue("ssl-cert");
44 _sslCertPath = Quassel::configDirPath() + "quasselCert.pem";
47 if(Quassel::isOptionSet("ssl-key")) {
48 _sslKeyPath = Quassel::optionValue("ssl-key");
50 _sslKeyPath = _sslCertPath;
53 // Initialize the certificates for first-time usage
55 if (!sslWarningShown) {
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;
66 QTcpSocket *SslServer::nextPendingConnection()
68 if (_pendingConnections.isEmpty())
71 return _pendingConnections.takeFirst();
75 void SslServer::incomingConnection(qintptr socketDescriptor)
77 QSslSocket *serverSocket = new QSslSocket(this);
78 if (serverSocket->setSocketDescriptor(socketDescriptor)) {
80 serverSocket->setLocalCertificate(_cert);
81 serverSocket->setPrivateKey(_key);
82 serverSocket->addCaCertificates(_ca);
84 _pendingConnections << serverSocket;
93 bool SslServer::loadCerts()
95 // Load the certificates specified in the path. If needed, other prep work can be done here.
96 return setCertificate(_sslCertPath, _sslKeyPath);
100 bool SslServer::reloadCerts()
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.
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.";
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.";
123 bool SslServer::setCertificate(const QString &path, const QString &keyPath)
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;
135 QFile certFile(path);
136 if (!certFile.exists()) {
137 quWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
141 if (!certFile.open(QIODevice::ReadOnly)) {
143 << "SslServer: Failed to open certificate file" << qPrintable(path)
144 << "error:" << certFile.error();
148 QList<QSslCertificate> certList = QSslCertificate::fromDevice(&certFile);
150 if (certList.isEmpty()) {
151 quWarning() << "SslServer: Certificate file doesn't contain a certificate";
155 untestedCert = certList[0];
156 certList.removeFirst(); // remove server cert
158 // store CA and intermediates certs
159 untestedCA = certList;
161 if (!certFile.reset()) {
162 quWarning() << "SslServer: IO error reading certificate file";
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";
174 if (!keyFile.open(QIODevice::ReadOnly)) {
176 << "SslServer: Failed to open key file" << qPrintable(keyPath)
177 << "error:" << keyFile.error();
181 untestedKey = loadKey(&keyFile);
184 untestedKey = loadKey(&certFile);
189 if (untestedCert.isNull()) {
190 quWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
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();
199 else if (now > untestedCert.expiryDate()) {
200 quWarning() << "SslServer: Certificate expired on" << untestedCert.expiryDate().toString();
202 else if (untestedCert.isBlacklisted()) {
203 quWarning() << "SslServer: Certificate blacklisted";
206 if (untestedKey.isNull()) {
207 quWarning() << "SslServer:" << qPrintable(keyPath) << "contains no key data";
213 // All keys are valid, update the externally visible copy used for new connections.
214 _cert = untestedCert;
222 QSslKey SslServer::loadKey(QFile *keyFile)
225 key = QSslKey(keyFile, QSsl::Rsa);
227 if (!keyFile->reset()) {
228 quWarning() << "SslServer: IO error reading key file";
231 key = QSslKey(keyFile, QSsl::Ec);