forked from blueprint-freespeech/ricochet-refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserIdentity.cpp
249 lines (212 loc) · 9.05 KB
/
UserIdentity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Ricochet - https://ricochet.im/
* Copyright (C) 2014, John Brooks <john.brooks@dereferenced.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the names of the copyright owners nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "signals.hpp"
#include "context.hpp"
#include "ed25519.hpp"
#include "globals.hpp"
using tego::g_globals;
#include "UserIdentity.h"
#include "tor/TorControl.h"
#include "tor/HiddenService.h"
#include "core/ContactIDValidator.h"
#include "core/ContactUser.h"
#include "protocol/Connection.h"
#include "utils/Useful.h"
using namespace Protocol;
UserIdentity::UserIdentity(int id, const QString& serviceID, QObject *parent)
: QObject(parent)
, uniqueID(id)
, contacts(this)
, m_hiddenService(0)
, m_incomingServer(0)
{
setupService(serviceID);
}
UserIdentity *UserIdentity::createIdentity(int uniqueID)
{
// There is actually no support for multiple identities currently.
Q_ASSERT(uniqueID == 0);
if (uniqueID != 0)
return 0;
return new UserIdentity(uniqueID, "", {});
}
// TODO: Handle the error cases of this function in a useful way
void UserIdentity::setupService(const QString& serviceID)
{
g_globals.context->set_host_user_state(tego_host_user_state_offline);
QString keyData = serviceID;
if (!keyData.isEmpty()) {
CryptoKey key;
if (!key.loadFromKeyBlob(keyData.toLatin1())) {
qWarning() << "Cannot load service key from configuration";
return;
}
m_hiddenService = new Tor::HiddenService(key, this);
} else {
m_hiddenService = new Tor::HiddenService(this);
connect(m_hiddenService, &Tor::HiddenService::privateKeyChanged, this,
[&]() {
QString key = QString::fromLatin1(m_hiddenService->privateKey().encodedKeyBlob());
const QByteArray rawKey = key.toUtf8();
// convert keyblob string to tego_ed25519_private key
std::unique_ptr<tego_ed25519_private_key_t> privateKey;
tego_ed25519_private_key_from_ed25519_keyblob(
tego::out(privateKey),
rawKey.data(),
rawKey.size(),
tego::throw_on_error());
g_globals.context->callback_registry_.emit_new_identity_created(privateKey.release());
}
);
}
g_globals.context->set_host_user_state(tego_host_user_state_connecting);
Q_ASSERT(m_hiddenService);
connect(m_hiddenService, SIGNAL(statusChanged(int,int)), SLOT(onStatusChanged(int,int)));
// Generally, these are not used, and we bind to localhost and port 0
// for an automatic (and portable) selection.
QHostAddress address = QHostAddress::LocalHost;
quint16 port = 0;
// If Ricochet is running on a different machine than Tor (i.e.
// Workstation/Gateway setup), then listen on 0.0.0.0 as per this spec:
// http://www.dds6qkxpwdeubwucdiaord2xgbbeyds25rbsgr73tbfpqpt4a6vjwsyd.onion/wiki/Dev/Whonix_friendly_applications_best_practices#Listen_Interface
if (QFile::exists("/usr/share/anon-ws-base-files/workstation")) {
address = QHostAddress::AnyIPv4;
}
m_incomingServer = new QTcpServer(this);
if (!m_incomingServer->listen(address, port)) {
// XXX error case
qWarning() << "Failed to open incoming socket:" << m_incomingServer->errorString();
return;
}
connect(m_incomingServer, &QTcpServer::newConnection, this, &UserIdentity::onIncomingConnection);
m_hiddenService->addTarget(9878, m_incomingServer->serverAddress(), m_incomingServer->serverPort());
g_globals.context->torControl->addHiddenService(m_hiddenService);
}
QString UserIdentity::hostname() const
{
return m_hiddenService ? m_hiddenService->hostname() : QString();
}
QString UserIdentity::contactID() const
{
return ContactIDValidator::idFromHostname(hostname());
}
void UserIdentity::onStatusChanged(int newStatus, int oldStatus)
{
if (oldStatus == Tor::HiddenService::NotCreated && newStatus > oldStatus)
{
emit contactIDChanged();
}
emit statusChanged();
}
bool UserIdentity::isServiceOnline() const
{
return m_hiddenService && m_hiddenService->status() == Tor::HiddenService::Online;
}
/* Handle an incoming connection to this service
*
* A Protocol::Connection is created to handle this socket. The
* connection initially has a purpose of Unknown. It times out
* and automatically closes after ConnectionPrivate::UnknownPurposeTimeout
* seconds, unless the purpose is changed.
*
* If the connection successfully completes authentication,
* handleIncomingAuthedConnection is called to link it to a ContactUser
* (if applicable) and set the purpose.
*/
void UserIdentity::onIncomingConnection()
{
while (m_incomingServer->hasPendingConnections()) {
QTcpSocket *socket = m_incomingServer->nextPendingConnection();
/* The localHostname property is used by Connection to determine the
* server onion hostname that this socket is connected to, which is
* used by the serverHostname() method.
*/
socket->setProperty("localHostname", m_hiddenService->hostname());
qDebug() << "Accepted new incoming connection";
QSharedPointer<Connection> conn(new Connection(socket, Connection::ServerSide), &QObject::deleteLater);
Q_ASSERT(socket->parent());
m_incomingConnections.append(conn);
Connection *connPtr = conn.data();
/* When the connection is closed, if it's not claimed, take it out of the
* incoming connection list and destroy the reference
*/
connect(connPtr, &Connection::closed, this,
[this,connPtr]() {
QSharedPointer<Connection> conn(takeIncomingConnection(connPtr));
if (conn)
qDebug() << "Deleting closed incoming connection that was never claimed by an owner";
}
);
connect(connPtr, &Connection::authenticated, this,
[this,connPtr](Connection::AuthenticationType type) {
if (type == Connection::HiddenServiceAuth)
handleIncomingAuthedConnection(connPtr);
}
);
emit incomingConnection(connPtr);
}
}
void UserIdentity::handleIncomingAuthedConnection(Connection *conn)
{
if (conn->purpose() != Connection::Purpose::Unknown)
return;
QString clientName = conn->authenticatedIdentity(Connection::HiddenServiceAuth);
if (clientName.isEmpty()) {
TEGO_BUG() << "Called to handle incoming authed connection without any authed name";
return;
}
ContactUser *user = contacts.lookupHostname(clientName);
if (!user) {
// This client can start a contact request, for example. The purpose stays unknown, and the
// connection will be killed if the purpose isn't changed before the timeout.
qDebug() << "Have an incoming connection authenticated as unknown client" << clientName;
return;
}
QSharedPointer<Connection> connPtr(takeIncomingConnection(conn));
if (!connPtr) {
TEGO_BUG() << "Called to handle incoming authed connection, but the connection is already out of the incoming list";
return;
}
qDebug() << "Incoming connection authenticated as contact with hostname" << clientName;
user->assignConnection(connPtr);
}
QSharedPointer<Connection> UserIdentity::takeIncomingConnection(Connection *match)
{
for (auto it = m_incomingConnections.begin(); it != m_incomingConnections.end(); it++) {
if (it->data() == match) {
QSharedPointer<Connection> re = *it;
m_incomingConnections.erase(it);
return re;
}
}
return QSharedPointer<Connection>();
}