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>
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();
74 #if QT_VERSION >= 0x050000
75 void SslServer::incomingConnection(qintptr socketDescriptor)
77 void SslServer::incomingConnection(int socketDescriptor)
80 QSslSocket *serverSocket = new QSslSocket(this);
81 if (serverSocket->setSocketDescriptor(socketDescriptor)) {
83 serverSocket->setLocalCertificate(_cert);
84 serverSocket->setPrivateKey(_key);
85 serverSocket->addCaCertificates(_ca);
87 _pendingConnections << serverSocket;
96 bool SslServer::loadCerts()
98 // Load the certificates specified in the path. If needed, other prep work can be done here.
99 return setCertificate(_sslCertPath, _sslKeyPath);
103 bool SslServer::reloadCerts()
108 // Reloading certificates currently only occur in response to a request. Always print an
109 // error if something goes wrong, in order to simplify checking if it's working.
112 << "SslServer: Unable to reload certificate file, reverting\n"
113 << " Quassel Core will use the previous key to provide SSL for client connections.\n"
114 << " Please see https://quassel-irc.org/faq/cert to learn how to enable SSL support.";
117 << "SslServer: Unable to reload certificate file\n"
118 << " Quassel Core will still work, but cannot provide SSL for client connections.\n"
119 << " Please see https://quassel-irc.org/faq/cert to learn how to enable SSL support.";
126 bool SslServer::setCertificate(const QString &path, const QString &keyPath)
128 // Don't reset _isCertValid here, in case an older but valid certificate is still loaded.
129 // Use temporary variables in order to avoid overwriting the existing certificates until
130 // everything is confirmed good.
131 QSslCertificate untestedCert;
132 QList<QSslCertificate> untestedCA;
138 QFile certFile(path);
139 if (!certFile.exists()) {
140 quWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
144 if (!certFile.open(QIODevice::ReadOnly)) {
146 << "SslServer: Failed to open certificate file" << qPrintable(path)
147 << "error:" << certFile.error();
151 QList<QSslCertificate> certList = QSslCertificate::fromDevice(&certFile);
153 if (certList.isEmpty()) {
154 quWarning() << "SslServer: Certificate file doesn't contain a certificate";
158 untestedCert = certList[0];
159 certList.removeFirst(); // remove server cert
161 // store CA and intermediates certs
162 untestedCA = certList;
164 if (!certFile.reset()) {
165 quWarning() << "SslServer: IO error reading certificate file";
169 // load key from keyPath if it differs from path, otherwise load key from path
170 if(path != keyPath) {
171 QFile keyFile(keyPath);
172 if(!keyFile.exists()) {
173 quWarning() << "SslServer: Key file" << qPrintable(keyPath) << "does not exist";
177 if (!keyFile.open(QIODevice::ReadOnly)) {
179 << "SslServer: Failed to open key file" << qPrintable(keyPath)
180 << "error:" << keyFile.error();
184 untestedKey = loadKey(&keyFile);
187 untestedKey = loadKey(&certFile);
192 if (untestedCert.isNull()) {
193 quWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
197 // We allow the core to offer SSL anyway, so no "return false" here. Client will warn about the cert being invalid.
198 const QDateTime now = QDateTime::currentDateTime();
199 if (now < untestedCert.effectiveDate())
200 quWarning() << "SslServer: Certificate won't be valid before" << untestedCert.effectiveDate().toString();
202 else if (now > untestedCert.expiryDate())
203 quWarning() << "SslServer: Certificate expired on" << untestedCert.expiryDate().toString();
205 else { // Qt4's isValid() checks for time range and blacklist; avoid a double warning, hence the else block
206 #if QT_VERSION < 0x050000
207 if (!untestedCert.isValid())
209 if (untestedCert.isBlacklisted())
211 quWarning() << "SslServer: Certificate blacklisted";
213 if (untestedKey.isNull()) {
214 quWarning() << "SslServer:" << qPrintable(keyPath) << "contains no key data";
220 // All keys are valid, update the externally visible copy used for new connections.
221 _cert = untestedCert;
229 QSslKey SslServer::loadKey(QFile *keyFile)
232 key = QSslKey(keyFile, QSsl::Rsa);
233 #if QT_VERSION >= 0x050500
235 if (!keyFile->reset()) {
236 quWarning() << "SslServer: IO error reading key file";
239 key = QSslKey(keyFile, QSsl::Ec);