Abstract away the protocol handshake code
[quassel.git] / src / core / sslserver.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 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
60 void SslServer::incomingConnection(int socketDescriptor)
61 {
62     QSslSocket *serverSocket = new QSslSocket(this);
63     if (serverSocket->setSocketDescriptor(socketDescriptor)) {
64         if (isCertValid()) {
65             serverSocket->setLocalCertificate(_cert);
66             serverSocket->setPrivateKey(_key);
67             serverSocket->addCaCertificates(_ca);
68         }
69         _pendingConnections << serverSocket;
70         emit newConnection();
71     }
72     else {
73         delete serverSocket;
74     }
75 }
76
77
78 bool SslServer::setCertificate(const QString &path)
79 {
80     _isCertValid = false;
81
82     if (path.isEmpty())
83         return false;
84
85     QFile certFile(path);
86     if (!certFile.exists()) {
87         quWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
88         return false;
89     }
90
91     if (!certFile.open(QIODevice::ReadOnly)) {
92         quWarning()
93         << "SslServer: Failed to open certificate file" << qPrintable(path)
94         << "error:" << certFile.error();
95         return false;
96     }
97
98     QList<QSslCertificate> certList = QSslCertificate::fromDevice(&certFile);
99
100     if (certList.isEmpty()) {
101         quWarning() << "SslServer: Certificate file doesn't contain a certificate";
102         return false;
103     }
104
105     _cert = certList[0];
106     certList.removeFirst(); // remove server cert
107
108     // store CA and intermediates certs
109     _ca = certList;
110
111     if (!certFile.reset()) {
112         quWarning() << "SslServer: IO error reading certificate file";
113         return false;
114     }
115
116     _key = QSslKey(&certFile, QSsl::Rsa);
117     certFile.close();
118
119     if (_cert.isNull()) {
120         quWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
121         return false;
122     }
123     if (!_cert.isValid()) {
124         quWarning() << "SslServer: Invalid certificate (most likely expired)";
125         // We allow the core to offer SSL anyway, so no "return false" here. Client will warn about the cert being invalid.
126     }
127     if (_key.isNull()) {
128         quWarning() << "SslServer:" << qPrintable(path) << "contains no key data";
129         return false;
130     }
131
132     _isCertValid = true;
133
134     return _isCertValid;
135 }
136
137
138 #endif // HAVE_SSL