1 /***************************************************************************
2 * Copyright (C) 2005-2015 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>
35 SslServer::SslServer(QObject *parent)
39 static bool sslWarningShown = false;
44 if(Quassel::isOptionSet("ssl-cert")) {
45 ssl_cert = Quassel::optionValue("ssl-cert");
47 ssl_cert = Quassel::configDirPath() + "quasselCert.pem";
50 if(Quassel::isOptionSet("ssl-key")) {
51 ssl_key = Quassel::optionValue("ssl-key");
56 if (!setCertificate(ssl_cert, ssl_key)) {
57 if (!sslWarningShown) {
59 << "SslServer: Unable to set certificate file\n"
60 << " Quassel Core will still work, but cannot provide SSL for client connections.\n"
61 << " Please see http://quassel-irc.org/faq/cert to learn how to enable SSL support.";
62 sslWarningShown = true;
68 QTcpSocket *SslServer::nextPendingConnection()
70 if (_pendingConnections.isEmpty())
73 return _pendingConnections.takeFirst();
76 #if QT_VERSION >= 0x050000
77 void SslServer::incomingConnection(qintptr socketDescriptor)
79 void SslServer::incomingConnection(int socketDescriptor)
82 QSslSocket *serverSocket = new QSslSocket(this);
83 if (serverSocket->setSocketDescriptor(socketDescriptor)) {
85 serverSocket->setLocalCertificate(_cert);
86 serverSocket->setPrivateKey(_key);
87 serverSocket->addCaCertificates(_ca);
89 _pendingConnections << serverSocket;
98 bool SslServer::setCertificate(const QString &path, const QString &keyPath)
100 _isCertValid = false;
105 QFile certFile(path);
106 if (!certFile.exists()) {
107 quWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
111 if (!certFile.open(QIODevice::ReadOnly)) {
113 << "SslServer: Failed to open certificate file" << qPrintable(path)
114 << "error:" << certFile.error();
118 QList<QSslCertificate> certList = QSslCertificate::fromDevice(&certFile);
120 if (certList.isEmpty()) {
121 quWarning() << "SslServer: Certificate file doesn't contain a certificate";
126 certList.removeFirst(); // remove server cert
128 // store CA and intermediates certs
131 if (!certFile.reset()) {
132 quWarning() << "SslServer: IO error reading certificate file";
136 // load key from keyPath if it differs from path, otherwise load key from path
137 if(path != keyPath) {
138 QFile keyFile(keyPath);
139 if(!keyFile.exists()) {
140 quWarning() << "SslServer: Key file" << qPrintable(keyPath) << "does not exist";
144 if (!keyFile.open(QIODevice::ReadOnly)) {
146 << "SslServer: Failed to open key file" << qPrintable(keyPath)
147 << "error:" << keyFile.error();
151 _key = QSslKey(&keyFile, QSsl::Rsa);
154 _key = QSslKey(&certFile, QSsl::Rsa);
159 if (_cert.isNull()) {
160 quWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
164 // We allow the core to offer SSL anyway, so no "return false" here. Client will warn about the cert being invalid.
165 const QDateTime now = QDateTime::currentDateTime();
166 if (now < _cert.effectiveDate())
167 quWarning() << "SslServer: Certificate won't be valid before" << _cert.effectiveDate().toString();
169 else if (now > _cert.expiryDate())
170 quWarning() << "SslServer: Certificate expired on" << _cert.expiryDate().toString();
172 else { // Qt4's isValid() checks for time range and blacklist; avoid a double warning, hence the else block
173 #if QT_VERSION < 0x050000
174 if (!_cert.isValid())
176 if (_cert.isBlacklisted())
178 quWarning() << "SslServer: Certificate blacklisted";
181 quWarning() << "SslServer:" << qPrintable(keyPath) << "contains no key data";