Useful error message on certificate error.
[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 SslServer::SslServer(QObject *parent)
36   : QTcpServer(parent)
37 {
38   if (! setCertificate(quasselDir().absolutePath() + "/quasselCert.pem")) {
39     qWarning()
40       << "SslServer: Unable to set certificate file\n"
41       << "          Quassel Core will still work, but cannot provide SSL for client connections.\n"
42       << "          Please see http://quassel-irc.org/faq/cert to learn how to enable SSL support.";
43   }
44 }
45
46 QTcpSocket *SslServer::nextPendingConnection() {
47   if(_pendingConnections.isEmpty())
48     return 0;
49   else
50     return _pendingConnections.takeFirst();
51 }
52
53 void SslServer::incomingConnection(int socketDescriptor) {
54   QSslSocket *serverSocket = new QSslSocket(this);
55   if(serverSocket->setSocketDescriptor(socketDescriptor)) {
56     if(certIsValid()) {
57       serverSocket->setLocalCertificate(_cert);
58       serverSocket->setPrivateKey(_key);
59     }
60     _pendingConnections << serverSocket;
61     emit newConnection();
62   } else {
63     delete serverSocket;
64   }
65 }
66
67 bool SslServer::setCertificate(const QString &path) {
68   _certIsValid = false;
69
70   if (path.isNull()) {
71     return false;
72   }
73
74   QFile certFile(path);
75   if (! certFile.exists()) {
76     qWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
77     return false;
78   }
79
80   if (! certFile.open(QIODevice::ReadOnly)) {
81     qWarning()
82       << "SslServer: Failed to open certificate file" << qPrintable(path)
83       << "error:" << certFile.error();
84     return false;
85   }
86   _cert = QSslCertificate(&certFile);
87
88   if (! certFile.reset()) {
89     qWarning() << "SslServer: IO error reading certificate file";
90     return false;
91   }
92
93   _key = QSslKey(&certFile, QSsl::Rsa);
94   certFile.close();
95
96   if (_cert.isNull()) {
97     qWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
98     return false;
99   }
100   if (! _cert.isValid()) {
101     qWarning() << "SslServer: Invalid certificate";
102     return false;
103   }
104   if (_key.isNull()) {
105     qWarning() << "SslServer:" << qPrintable(path) << "contains no key data";
106     return false;
107   }
108
109   _certIsValid = true;
110
111   return _certIsValid;
112 }
113
114 #endif // HAVE_SSL