~ubuntu-branches/ubuntu/trusty/jreen/trusty

« back to all changes in this revision

Viewing changes to src/tlsfeature.cpp

  • Committer: Package Import Robot
  • Author(s): Prasad Murthy
  • Date: 2013-03-08 00:00:33 UTC
  • Revision ID: package-import@ubuntu.com-20130308000033-x8thp6syo1kkh63s
Tags: upstream-1.1.1
Import upstream version 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Jreen
 
4
**
 
5
** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru>
 
6
** Copyright © 2011 Aleksey Sidorov <gorthauer87@yandex.ru>
 
7
**
 
8
*****************************************************************************
 
9
**
 
10
** $JREEN_BEGIN_LICENSE$
 
11
** This program is free software: you can redistribute it and/or modify
 
12
** it under the terms of the GNU General Public License as published by
 
13
** the Free Software Foundation, either version 2 of the License, or
 
14
** (at your option) any later version.
 
15
**
 
16
** This program is distributed in the hope that it will be useful,
 
17
** but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
19
** See the GNU General Public License for more details.
 
20
**
 
21
** You should have received a copy of the GNU General Public License
 
22
** along with this program.  If not, see http://www.gnu.org/licenses/.
 
23
** $JREEN_END_LICENSE$
 
24
**
 
25
****************************************************************************/
 
26
 
 
27
#include "tlsfeature_p.h"
 
28
#include "tlsdatastream_p.h"
 
29
#include "client.h"
 
30
#include <QXmlStreamWriter>
 
31
#include "logger.h"
 
32
#include <QCoreApplication>
 
33
 
 
34
#define NS_TLS QLatin1String("urn:ietf:params:xml:ns:xmpp-tls")
 
35
 
 
36
namespace Jreen
 
37
{
 
38
 
 
39
TLSFeature::TLSFeature() : StreamFeature(SecurityLayer)
 
40
{
 
41
        QCA::init();
 
42
        QCA::setAppName(QCoreApplication::applicationName());
 
43
        m_required = false;
 
44
        m_available = false;
 
45
        m_hasTls = QCA::isSupported("tls");
 
46
        if (!m_hasTls)
 
47
                Logger::warning() << "Jreen: TLS is not provided by QCA";
 
48
}
 
49
 
 
50
void TLSFeature::init()
 
51
{
 
52
        Q_ASSERT(m_hasTls);
 
53
        Q_ASSERT(!m_tls);
 
54
        m_tls.reset(new QCA::TLS(this));
 
55
        m_tls->setTrustedCertificates(QCA::systemStore());
 
56
        connect(m_tls.data(), SIGNAL(handshaken()), SLOT(onHandshaken()));
 
57
        connect(m_tls.data(), SIGNAL(closed()), SLOT(onClosed()));
 
58
        connect(m_tls.data(), SIGNAL(error()), SLOT(onError()));
 
59
}
 
60
 
 
61
void TLSFeature::setStreamInfo(StreamInfo *info)
 
62
{
 
63
        if(m_client)
 
64
                m_client->disconnect(this);
 
65
        StreamFeature::setStreamInfo(info);
 
66
        connect(m_client, SIGNAL(disconnected(Jreen::Client::DisconnectReason)),
 
67
                SLOT(onDisconnected()));
 
68
}
 
69
 
 
70
void TLSFeature::reset()
 
71
{
 
72
        m_required = false;
 
73
        m_available = false;
 
74
}
 
75
 
 
76
bool TLSFeature::canParse(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
 
77
{
 
78
        // All other methods shouldn't be called is canParse returnes false
 
79
        if (!m_hasTls)
 
80
                return false;
 
81
        Q_UNUSED(uri);
 
82
        Q_UNUSED(name);
 
83
        Q_UNUSED(attributes);
 
84
        Logger::debug() << Q_FUNC_INFO << name << uri;
 
85
        return uri == NS_TLS;
 
86
}
 
87
 
 
88
void TLSFeature::handleStartElement(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
 
89
{
 
90
        Q_UNUSED(uri);
 
91
        Q_UNUSED(attributes);
 
92
        if (name == QLatin1String("starttls"))
 
93
                m_available = true;
 
94
        else if (name == QLatin1String("required"))
 
95
                m_required = true;
 
96
        //              Q_UNUSED(uri);
 
97
        //              m_depth++;
 
98
        //              Logger::debug() << Q_FUNC_INFO << m_depth << name;
 
99
        //              if (m_depth == 1) {
 
100
        //                      if (name == QLatin1String("mechanisms")) {
 
101
        //                              m_state = AtMechanisms;
 
102
        //                              m_mechs.clear();
 
103
        //                      } else if (name == QLatin1String("challenge")) {
 
104
        //                              m_state = AtChallenge;
 
105
        //                      }
 
106
        //              } else if (m_depth == 2 && name == QLatin1String("mechanism")) {
 
107
        //                      m_state = AtMechanism;
 
108
        //              }
 
109
}
 
110
 
 
111
void TLSFeature::handleEndElement(const QStringRef &name, const QStringRef &uri)
 
112
{
 
113
        Q_UNUSED(uri);
 
114
        if (name == QLatin1String("proceed")) {
 
115
                Logger::debug() << Q_FUNC_INFO;
 
116
                m_info->addDataStream(new TLSDataStream(m_tls.data()));
 
117
                m_tls->startClient(m_info->jid().domain());
 
118
        }
 
119
        //              if (m_depth == 2 && m_state == AtMechanism)
 
120
        //                      m_state = AtMechanisms;
 
121
        //              else if (m_depth == 1) {
 
122
        //                      Logger::debug() << Q_FUNC_INFO << m_mechs;
 
123
        //                      m_state = AtStart;
 
124
        //                      if (name == QLatin1String("success"))
 
125
        //                              m_info->completed(StreamInfo::Authorized);
 
126
        //              }
 
127
        //              m_depth--;
 
128
}
 
129
 
 
130
void TLSFeature::handleCharacterData(const QStringRef &text)
 
131
{
 
132
        Q_UNUSED(text);
 
133
        //              if (m_state == AtMechanism) {
 
134
        //                      Logger::debug() << Q_FUNC_INFO << "mechanism" << text;
 
135
        //                      m_mechs.append(text.toString());
 
136
        //              } else if (m_state == AtChallenge) {
 
137
        //                      Logger::debug() << Q_FUNC_INFO << "challenge" << text;
 
138
        //                      //                      if (m_firstStep)
 
139
        //                      //                              m_tls->putServerFirstStep("DIGEST-MD5");
 
140
        //                      m_tls->putStep(QByteArray::fromBase64(text.toString().toLatin1()));
 
141
        //                      m_firstStep = false;
 
142
        //              }
 
143
}
 
144
 
 
145
bool TLSFeature::isActivatable()
 
146
{
 
147
        return m_hasTls && m_available;
 
148
}
 
149
 
 
150
bool TLSFeature::activate()
 
151
{
 
152
        init();
 
153
        QXmlStreamWriter *writer = m_info->writer();
 
154
        writer->writeEmptyElement(QLatin1String("starttls"));
 
155
        writer->writeDefaultNamespace(NS_TLS);
 
156
        writer->writeCharacters(QString());
 
157
        //              Logger::debug() << Q_FUNC_INFO << m_info->password();
 
158
        //              m_tls->setPassword(QCA::SecureArray(m_info->password().toUtf8()));
 
159
        //              m_tls->setUsername(m_info->jid().node());
 
160
        //              m_tls->setRealm(m_info->jid().domain());
 
161
        //              m_tls->setAuthzid(m_info->jid().bare());
 
162
        //              m_tls->startClient("xmpp", QUrl::toAce(m_info->jid().domain()), m_mechs, QCA::TLS::AllowClientSendFirst);
 
163
        return true;
 
164
}
 
165
 
 
166
void TLSFeature::onHandshaken()
 
167
{
 
168
        Logger::debug() << Q_FUNC_INFO;
 
169
        m_tls->continueAfterStep();
 
170
        m_info->completed();
 
171
}
 
172
 
 
173
void TLSFeature::onClosed()
 
174
{
 
175
        Logger::debug() << Q_FUNC_INFO;
 
176
        reset();
 
177
}
 
178
 
 
179
void TLSFeature::onError()
 
180
{
 
181
        Logger::debug() << Q_FUNC_INFO;
 
182
        reset();
 
183
        m_client->disconnectFromServer(true);
 
184
}
 
185
 
 
186
void TLSFeature::onDisconnected()
 
187
{
 
188
        m_tls.reset(0);
 
189
        Logger::debug() << Q_FUNC_INFO;
 
190
}
 
191
 
 
192
} //namespace Jreen
 
193
 
 
194