1 /***************************************************************************
2 * Copyright (C) 2005-2014 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 static bool sslWarningShown = false;
39 if (!setCertificate(Quassel::configDirPath() + "quasselCert.pem")) {
40 if (!sslWarningShown) {
42 << "SslServer: Unable to set certificate file\n"
43 << " Quassel Core will still work, but cannot provide SSL for client connections.\n"
44 << " Please see http://quassel-irc.org/faq/cert to learn how to enable SSL support.";
45 sslWarningShown = true;
51 QTcpSocket *SslServer::nextPendingConnection()
53 if (_pendingConnections.isEmpty())
56 return _pendingConnections.takeFirst();
59 #if QT_VERSION >= 0x050000
60 void SslServer::incomingConnection(qintptr socketDescriptor)
62 void SslServer::incomingConnection(int socketDescriptor)
65 QSslSocket *serverSocket = new QSslSocket(this);
66 if (serverSocket->setSocketDescriptor(socketDescriptor)) {
68 serverSocket->setLocalCertificate(_cert);
69 serverSocket->setPrivateKey(_key);
70 serverSocket->addCaCertificates(_ca);
72 _pendingConnections << serverSocket;
81 bool SslServer::setCertificate(const QString &path)
89 if (!certFile.exists()) {
90 quWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
94 if (!certFile.open(QIODevice::ReadOnly)) {
96 << "SslServer: Failed to open certificate file" << qPrintable(path)
97 << "error:" << certFile.error();
101 QList<QSslCertificate> certList = QSslCertificate::fromDevice(&certFile);
103 if (certList.isEmpty()) {
104 quWarning() << "SslServer: Certificate file doesn't contain a certificate";
109 certList.removeFirst(); // remove server cert
111 // store CA and intermediates certs
114 if (!certFile.reset()) {
115 quWarning() << "SslServer: IO error reading certificate file";
119 _key = QSslKey(&certFile, QSsl::Rsa);
122 if (_cert.isNull()) {
123 quWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
127 // We allow the core to offer SSL anyway, so no "return false" here. Client will warn about the cert being invalid.
128 const QDateTime now = QDateTime::currentDateTime();
129 if (now < _cert.effectiveDate())
130 quWarning() << "SslServer: Certificate won't be valid before" << _cert.effectiveDate().toString();
132 else if (now > _cert.expiryDate())
133 quWarning() << "SslServer: Certificate expired on" << _cert.expiryDate().toString();
135 else { // Qt4's isValid() checks for time range and blacklist; avoid a double warning, hence the else block
136 #if QT_VERSION < 0x050000
137 if (!_cert.isValid())
139 if (_cert.isBlacklisted())
141 quWarning() << "SslServer: Certificate blacklisted";
144 quWarning() << "SslServer:" << qPrintable(path) << "contains no key data";