Fixes #682 - Core crashes on client connection
[quassel.git] / src / core / coreidentity.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 "coreidentity.h"
22
23 #include "signalproxy.h"
24
25 CoreIdentity::CoreIdentity(IdentityId id, QObject *parent)
26   : Identity(id, parent)
27 #ifdef HAVE_SSL
28   , _certManager(*this)
29 #endif
30 {
31 #ifdef HAVE_SSL
32   connect(this, SIGNAL(idSet(IdentityId)), &_certManager, SLOT(setId(IdentityId)));
33   connect(&_certManager, SIGNAL(updated(const QVariantMap &)), this, SIGNAL(updated(const QVariantMap &)));
34 #endif
35 }
36
37 CoreIdentity::CoreIdentity(const Identity &other, QObject *parent)
38   : Identity(other, parent)
39 #ifdef HAVE_SSL
40   , _certManager(*this)
41 #endif
42 {
43 #ifdef HAVE_SSL
44   connect(this, SIGNAL(idSet(IdentityId)), &_certManager, SLOT(setId(IdentityId)));
45   connect(&_certManager, SIGNAL(updated(const QVariantMap &)), this, SIGNAL(updated(const QVariantMap &)));
46 #endif
47 }
48
49 CoreIdentity::CoreIdentity(const CoreIdentity &other, QObject *parent)
50   : Identity(other, parent)
51 #ifdef HAVE_SSL
52   , _sslKey(other._sslKey),
53     _sslCert(other._sslCert),
54     _certManager(*this)
55 #endif
56 {
57 #ifdef HAVE_SSL
58   connect(this, SIGNAL(idSet(IdentityId)), &_certManager, SLOT(setId(IdentityId)));
59   connect(&_certManager, SIGNAL(updated(const QVariantMap &)), this, SIGNAL(updated(const QVariantMap &)));
60 #endif
61 }
62
63 void CoreIdentity::synchronize(SignalProxy *proxy) {
64   proxy->synchronize(this);
65 #ifdef HAVE_SSL
66   proxy->synchronize(&_certManager);
67 #endif
68 }
69
70 #ifdef HAVE_SSL
71 void CoreIdentity::setSslKey(const QByteArray &encoded) {
72   QSslKey key(encoded, QSsl::Rsa);
73   if(key.isNull())
74     key = QSslKey(encoded, QSsl::Dsa);
75   setSslKey(key);
76 }
77
78 void CoreIdentity::setSslCert(const QByteArray &encoded) {
79   setSslCert(QSslCertificate(encoded));
80 }
81 #endif
82
83 CoreIdentity &CoreIdentity::operator=(const CoreIdentity &identity) {
84   Identity::operator=(identity);
85 #ifdef HAVE_SSL
86   _sslKey = identity._sslKey;
87   _sslCert = identity._sslCert;
88 #endif
89   return *this;
90 }
91
92 #ifdef HAVE_SSL
93 // ========================================
94 //  CoreCertManager
95 // ========================================
96 CoreCertManager::CoreCertManager(CoreIdentity &identity)
97   : CertManager(identity.id()),
98     identity(identity)
99 {
100   setAllowClientUpdates(true);
101 }
102
103 void CoreCertManager::setId(IdentityId id) {
104   renameObject(QString::number(id.toInt()));
105 }
106
107 void CoreCertManager::setSslKey(const QByteArray &encoded) {
108   identity.setSslKey(encoded);
109   CertManager::setSslKey(encoded);
110 }
111
112 void CoreCertManager::setSslCert(const QByteArray &encoded) {
113   identity.setSslCert(encoded);
114   CertManager::setSslCert(encoded);
115 }
116 #endif //HAVE_SSL