QSslCertificate::isValid() no longer exists in Qt5
[quassel.git] / src / core / sslserver.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "sslserver.h"
22
23 #ifdef HAVE_SSL
24 #  include <QSslSocket>
25 #endif
26
27 #include <QFile>
28
29 #include "logger.h"
30 #include "quassel.h"
31
32 #ifdef HAVE_SSL
33
34 SslServer::SslServer(QObject *parent)
35     : QTcpServer(parent),
36     _isCertValid(false)
37 {
38     static bool sslWarningShown = false;
39     if (!setCertificate(Quassel::configDirPath() + "quasselCert.pem")) {
40         if (!sslWarningShown) {
41             quWarning()
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;
46         }
47     }
48 }
49
50
51 QTcpSocket *SslServer::nextPendingConnection()
52 {
53     if (_pendingConnections.isEmpty())
54         return 0;
55     else
56         return _pendingConnections.takeFirst();
57 }
58
59 #if QT_VERSION >= 0x050000
60 void SslServer::incomingConnection(qintptr socketDescriptor)
61 #else
62 void SslServer::incomingConnection(int socketDescriptor)
63 #endif
64 {
65     QSslSocket *serverSocket = new QSslSocket(this);
66     if (serverSocket->setSocketDescriptor(socketDescriptor)) {
67         if (isCertValid()) {
68             serverSocket->setLocalCertificate(_cert);
69             serverSocket->setPrivateKey(_key);
70             serverSocket->addCaCertificates(_ca);
71         }
72         _pendingConnections << serverSocket;
73         emit newConnection();
74     }
75     else {
76         delete serverSocket;
77     }
78 }
79
80
81 bool SslServer::setCertificate(const QString &path)
82 {
83     _isCertValid = false;
84
85     if (path.isEmpty())
86         return false;
87
88     QFile certFile(path);
89     if (!certFile.exists()) {
90         quWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
91         return false;
92     }
93
94     if (!certFile.open(QIODevice::ReadOnly)) {
95         quWarning()
96         << "SslServer: Failed to open certificate file" << qPrintable(path)
97         << "error:" << certFile.error();
98         return false;
99     }
100
101     QList<QSslCertificate> certList = QSslCertificate::fromDevice(&certFile);
102
103     if (certList.isEmpty()) {
104         quWarning() << "SslServer: Certificate file doesn't contain a certificate";
105         return false;
106     }
107
108     _cert = certList[0];
109     certList.removeFirst(); // remove server cert
110
111     // store CA and intermediates certs
112     _ca = certList;
113
114     if (!certFile.reset()) {
115         quWarning() << "SslServer: IO error reading certificate file";
116         return false;
117     }
118
119     _key = QSslKey(&certFile, QSsl::Rsa);
120     certFile.close();
121
122     if (_cert.isNull()) {
123         quWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
124         return false;
125     }
126
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();
131
132     else if (now > _cert.expiryDate())
133         quWarning() << "SslServer: Certificate expired on" << _cert.expiryDate().toString();
134
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())
138 #else
139         if (_cert.isBlacklisted())
140 #endif
141             quWarning() << "SslServer: Certificate blacklisted";
142     }
143     if (_key.isNull()) {
144         quWarning() << "SslServer:" << qPrintable(path) << "contains no key data";
145         return false;
146     }
147
148     _isCertValid = true;
149
150     return _isCertValid;
151 }
152
153
154 #endif // HAVE_SSL