summaryrefslogtreecommitdiff
path: root/src/pairing.cpp
blob: dca5b03cd0f24d814279be0dc39b6e00eb7e41c9 (plain)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <erebos/pairing.h>

#include "service.h"

#include <future>
#include <openssl/rand.h>

#include <arpa/inet.h>

#include <algorithm>
#include <stdexcept>
#include <thread>
#include <vector>

using namespace erebos;

using std::lock_guard;
using std::make_shared;
using std::runtime_error;
using std::scoped_lock;
using std::thread;
using std::unique_lock;

PairingServiceBase::PairingServiceBase(Config && c):
	config(move(c))
{
}

PairingServiceBase::~PairingServiceBase()
{
	// There may be some threads in waitForConfirmation waiting on client
	// promise, so make sure they do not touch the service state anymore:
	for (auto & [peer, state] : peerStates) {
		scoped_lock lock(state->lock);
		if (state->phase != StatePhase::PairingDone &&
				state->phase != StatePhase::PairingFailed) {
			state->outcome.set_value(Outcome::Stale);
			state->phase = StatePhase::PairingFailed;
		}
	}
}

PairingServiceBase::Config & PairingServiceBase::Config::onRequestInit(RequestInitHook hook)
{
	requestInitHook = hook;
	return *this;
}

PairingServiceBase::Config & PairingServiceBase::Config::onResponse(ConfirmHook hook)
{
	responseHook = hook;
	return *this;
}

PairingServiceBase::Config & PairingServiceBase::Config::onRequest(ConfirmHook hook)
{
	requestHook = hook;
	return *this;
}

PairingServiceBase::Config & PairingServiceBase::Config::onRequestNonceFailed(RequestNonceFailedHook hook)
{
	requestNonceFailedHook = hook;
	return *this;
}

void PairingServiceBase::handle(Context & ctx)
{
	auto rec = ctx.ref()->asRecord();
	if (!rec)
		return;

	auto pid = ctx.peer().identity();
	if (!pid)
		throw runtime_error("Pairing request for peer without known identity");

	lock_guard lock(stateLock);
	auto & state = peerStates.try_emplace(ctx.peer(), new State()).first->second;
	unique_lock lock_state(state->lock);

	if (auto request = rec->item("request").asBinary()) {
		auto idReqRef = rec->item("id-req").asRef();
		if (!idReqRef)
			return;
		auto idReq = Identity::load(*idReqRef);
		if (!idReq)
			return;
		if (!idReq->sameAs(*pid))
			return;

		auto idRspRef = rec->item("id-rsp").asRef();
		if (!idRspRef)
			return;
		auto idRsp = Identity::load(*idRspRef);
		if (!idRsp)
			return;
		if (!idRsp->sameAs(ctx.peer().server().identity()))
			return;

		if (state->phase >= StatePhase::PairingDone) {
			auto nstate = make_shared<State>();
			lock_state = unique_lock(nstate->lock);
			state = move(nstate);
		} else if (state->phase != StatePhase::NoPairing)
			return;

		if (config.requestInitHook)
			config.requestInitHook(ctx.peer());

		state->phase = StatePhase::PeerRequest;
		state->idReq = idReq;
		state->idRsp = idRsp;
		state->peerCheck = *request;
		state->nonce.resize(32);
		RAND_bytes(state->nonce.data(), state->nonce.size());

		ctx.peer().send(uuid(), Object(Record({
			{ "response", state->nonce },
		})));
	}

	else if (auto response = rec->item("response").asBinary()) {
		if (state->phase != StatePhase::OurRequest) {
			fprintf(stderr, "Unexpected pairing response.\n"); // TODO
			return;
		}

		if (config.responseHook) {
			string confirm = confirmationNumber(nonceDigest(
				*state->idReq, *state->idRsp,
				state->nonce, *response));
			std::thread(&PairingServiceBase::waitForConfirmation,
					this, ctx.peer(), state, confirm, config.responseHook).detach();
		}

		state->phase = StatePhase::OurRequestConfirm;

		ctx.peer().send(uuid(), Object(Record({
			{ "reqnonce", state->nonce },
		})));
	}

	else if (auto reqnonce = rec->item("reqnonce").asBinary()) {
		if (state->phase != StatePhase::PeerRequest)
			return;

		auto check = nonceDigest(
				*state->idReq, *state->idRsp,
				*reqnonce, vector<uint8_t>());
		if (check != state->peerCheck) {
			if (config.requestNonceFailedHook)
				config.requestNonceFailedHook(ctx.peer());
			if (state->phase < StatePhase::PairingDone) {
				state->phase = StatePhase::PairingFailed;
				ctx.afterCommit([&]() {
					state->outcome.set_value(Outcome::NonceMismatch);
				});
			}
			return;
		}

		if (config.requestHook) {
			string confirm = confirmationNumber(nonceDigest(
				*state->idReq, *state->idRsp,
				*reqnonce, state->nonce));
			std::thread(&PairingServiceBase::waitForConfirmation,
					this, ctx.peer(), state, confirm, config.requestHook).detach();
		}

		state->phase = StatePhase::PeerRequestConfirm;
	}

	else if (rec->item("reject")) {
		if (state->phase < StatePhase::PairingDone) {
			state->phase = StatePhase::PairingFailed;
			ctx.afterCommit([&]() {
				state->outcome.set_value(Outcome::PeerRejected);
			});
		}
	}

	else {
		if (state->phase == StatePhase::OurRequestReady) {
			handlePairingResult(ctx);
			state->phase = StatePhase::PairingDone;
			ctx.afterCommit([&]() {
				state->outcome.set_value(Outcome::Success);
			});
		} else {
			result = ctx.ref();
		}
	}
}

void PairingServiceBase::requestPairing(UUID serviceId, const Peer & peer)
{
	auto pid = peer.identity();
	if (!pid)
		throw runtime_error("Pairing request for peer without known identity");

	unique_lock lock(stateLock);
	auto & state = peerStates.try_emplace(peer, new State()).first->second;

	if (state->phase != StatePhase::NoPairing) {
		auto nstate = make_shared<State>();
		lock = unique_lock(nstate->lock);
		state = move(nstate);
	}

	state->phase = StatePhase::OurRequest;
	state->idReq = peer.server().identity();
	state->idRsp = pid;
	state->nonce.resize(32);
	RAND_bytes(state->nonce.data(), state->nonce.size());

	vector<Record::Item> items;
	items.emplace_back("id-req", state->idReq->ref().value());
	items.emplace_back("id-rsp", state->idRsp->ref().value());
	items.emplace_back("request", nonceDigest(
				*state->idReq, *state->idRsp,
				state->nonce, vector<uint8_t>()));

	peer.send(serviceId, Object(Record(std::move(items))));
}

vector<uint8_t> PairingServiceBase::nonceDigest(const Identity & idReq, const Identity & idRsp,
	const vector<uint8_t> & nonceReq, const vector<uint8_t> & nonceRsp)
{
	vector<Record::Item> items;
	items.emplace_back("id-req", idReq.ref().value());
	items.emplace_back("id-rsp", idRsp.ref().value());
	items.emplace_back("nonce-req", nonceReq);
	items.emplace_back("nonce-rsp", nonceRsp);

	const auto arr = Digest::of(Object(Record(std::move(items)))).arr();
	vector<uint8_t> ret(arr.size());
	std::copy_n(arr.begin(), arr.size(), ret.begin());
	return ret;
}

string PairingServiceBase::confirmationNumber(const vector<uint8_t> & digest)
{
	uint32_t confirm;
	memcpy(&confirm, digest.data(), sizeof(confirm));
	string ret(6, '\0');
	snprintf(ret.data(), ret.size() + 1, "%06d", ntohl(confirm) % 1000000);
	return ret;
}

void PairingServiceBase::waitForConfirmation(Peer peer, weak_ptr<State> wstate, string confirm, ConfirmHook hook)
{
	future<Outcome> outcome;
	if (auto state = wstate.lock()) {
		outcome = state->outcome.get_future();
	} else {
		return;
	}

	bool ok;
	try {
		ok = hook(peer, confirm, std::move(outcome)).get();
	}
	catch (const std::future_error & e) {
		if (e.code() == std::future_errc::broken_promise)
			ok = false;
		else
			throw;
	}

	auto state = wstate.lock();
	if (!state)
		return; // Server was closed

	scoped_lock lock(state->lock);

	if (ok) {
		if (state->phase == StatePhase::OurRequestConfirm) {
			if (result) {
				peer.server().localHead().update([&] (const Stored<LocalState> & local) {
					Service::Context ctx(new Service::Context::Priv {
						.ref = *result,
						.peer = peer,
						.local = local,
					});

					handlePairingResult(ctx);
					return ctx.local();
				});
				state->phase = StatePhase::PairingDone;
				state->outcome.set_value(Outcome::Success);
			} else {
				state->phase = StatePhase::OurRequestReady;
			}
		} else if (state->phase == StatePhase::PeerRequestConfirm) {
			peer.send(uuid(), handlePairingCompleteRef(peer));
			state->phase = StatePhase::PairingDone;
			state->outcome.set_value(Outcome::Success);
		}
	} else {
		if (state->phase != StatePhase::PairingFailed) {
			peer.send(uuid(), Object(Record({{ "reject", Record::Item::Empty {} }})));
			state->phase = StatePhase::PairingFailed;
			state->outcome.set_value(Outcome::UserRejected);
		}
	}
}