bfcdbf26907ee54fd6a57922477513d3e8ab1c42
[quassel.git] / src / core / sslserver.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "sslserver.h"
22
23 #ifdef HAVE_SSL
24 #  include <QSslSocket>
25 #endif
26
27 #include <QFile>
28 #include <QDebug>
29
30 #include "logger.h"
31 #include "util.h"
32
33 #ifdef HAVE_SSL
34
35 static bool SslServer_longMessShown=false;
36
37 SslServer::SslServer(QObject *parent)
38   : QTcpServer(parent)
39 {
40   if (! setCertificate(quasselDir().absolutePath() + "/quasselCert.pem")) {
41     if (! SslServer_longMessShown) {
42       qWarning()
43         << "SslServer: Unable to set certificate file\n"
44         << "          Quassel Core will still work, but cannot provide SSL for client connections.\n"
45         << "          Please see http://quassel-irc.org/faq/cert to learn how to enable SSL support.";
46       SslServer_longMessShown=true;
47     }
48   }
49 }
50
51 QTcpSocket *SslServer::nextPendingConnection() {
52   if(_pendingConnections.isEmpty())
53     return 0;
54   else
55     return _pendingConnections.takeFirst();
56 }
57
58 void SslServer::incomingConnection(int socketDescriptor) {
59   QSslSocket *serverSocket = new QSslSocket(this);
60   if(serverSocket->setSocketDescriptor(socketDescriptor)) {
61     if(certIsValid()) {
62       serverSocket->setLocalCertificate(_cert);
63       serverSocket->setPrivateKey(_key);
64     }
65     _pendingConnections << serverSocket;
66     emit newConnection();
67   } else {
68     delete serverSocket;
69   }
70 }
71
72 bool SslServer::setCertificate(const QString &path) {
73   _certIsValid = false;
74
75   if (path.isNull()) {
76     return false;
77   }
78
79   QFile certFile(path);
80   if (! certFile.exists()) {
81     qWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
82     return false;
83   }
84
85   if (! certFile.open(QIODevice::ReadOnly)) {
86     qWarning()
87       << "SslServer: Failed to open certificate file" << qPrintable(path)
88       << "error:" << certFile.error();
89     return false;
90   }
91   _cert = QSslCertificate(&certFile);
92
93   if (! certFile.reset()) {
94     qWarning() << "SslServer: IO error reading certificate file";
95     return false;
96   }
97
98   _key = QSslKey(&certFile, QSsl::Rsa);
99   certFile.close();
100
101   if (_cert.isNull()) {
102     qWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
103     return false;
104   }
105   if (! _cert.isValid()) {
106     qWarning() << "SslServer: Invalid certificate";
107     return false;
108   }
109   if (_key.isNull()) {
110     qWarning() << "SslServer:" << qPrintable(path) << "contains no key data";
111     return false;
112   }
113
114   _certIsValid = true;
115
116   return _certIsValid;
117 }
118
119 #endif // HAVE_SSL