Support intermediate CA certificates.
[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
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 QTcpSocket *SslServer::nextPendingConnection() {
51   if(_pendingConnections.isEmpty())
52     return 0;
53   else
54     return _pendingConnections.takeFirst();
55 }
56
57 void SslServer::incomingConnection(int socketDescriptor) {
58   QSslSocket *serverSocket = new QSslSocket(this);
59   if(serverSocket->setSocketDescriptor(socketDescriptor)) {
60     if(isCertValid()) {
61       serverSocket->setLocalCertificate(_cert);
62       serverSocket->setPrivateKey(_key);
63       serverSocket->addCaCertificates(_ca);
64     }
65     _pendingConnections << serverSocket;
66     emit newConnection();
67   } else {
68     delete serverSocket;
69   }
70 }
71
72 bool SslServer::setCertificate(const QString &path) {
73   _isCertValid = false;
74
75   if(path.isEmpty())
76     return false;
77
78   QFile certFile(path);
79   if(!certFile.exists()) {
80     quWarning() << "SslServer: Certificate file" << qPrintable(path) << "does not exist";
81     return false;
82   }
83
84   if(!certFile.open(QIODevice::ReadOnly)) {
85     quWarning()
86       << "SslServer: Failed to open certificate file" << qPrintable(path)
87       << "error:" << certFile.error();
88     return false;
89   }
90
91   QList<QSslCertificate> certList = QSslCertificate::fromDevice(&certFile);
92
93   if (certList.isEmpty()) {
94     quWarning() << "SslServer: Certificate file doesn't contain a certificate";
95     return false;
96   }
97
98   _cert = certList[0];
99   certList.removeFirst(); // remove server cert
100
101   // store CA and intermediates certs
102   _ca = certList;
103
104   if(!certFile.reset()) {
105     quWarning() << "SslServer: IO error reading certificate file";
106     return false;
107   }
108
109   _key = QSslKey(&certFile, QSsl::Rsa);
110   certFile.close();
111
112   if(_cert.isNull()) {
113     quWarning() << "SslServer:" << qPrintable(path) << "contains no certificate data";
114     return false;
115   }
116   if(!_cert.isValid()) {
117     quWarning() << "SslServer: Invalid certificate";
118     return false;
119   }
120   if(_key.isNull()) {
121     quWarning() << "SslServer:" << qPrintable(path) << "contains no key data";
122     return false;
123   }
124
125   _isCertValid = true;
126
127   return _isCertValid;
128 }
129
130 #endif // HAVE_SSL