-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Handshake.cpp
426 lines (359 loc) · 13.6 KB
/
Handshake.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/app/ledger/LedgerMaster.h>
#include <ripple/app/main/Application.h>
#include <ripple/basics/base64.h>
#include <ripple/basics/safe_cast.h>
#include <ripple/beast/core/LexicalCast.h>
#include <ripple/beast/rfc2616.h>
#include <ripple/overlay/impl/Handshake.h>
#include <ripple/protocol/digest.h>
#include <boost/regex.hpp>
#include <algorithm>
#include <chrono>
// VFALCO Shouldn't we have to include the OpenSSL
// headers or something for SSL_get_finished?
namespace ripple {
std::optional<std::string>
getFeatureValue(
boost::beast::http::fields const& headers,
std::string const& feature)
{
auto const header = headers.find("X-Protocol-Ctl");
if (header == headers.end())
return {};
boost::smatch match;
boost::regex rx(feature + "=([^;\\s]+)");
std::string const value = header->value();
if (boost::regex_search(value, match, rx))
return {match[1]};
return {};
}
bool
isFeatureValue(
boost::beast::http::fields const& headers,
std::string const& feature,
std::string const& value)
{
if (auto const fvalue = getFeatureValue(headers, feature))
return beast::rfc2616::token_in_list(fvalue.value(), value);
return false;
}
bool
featureEnabled(
boost::beast::http::fields const& headers,
std::string const& feature)
{
return isFeatureValue(headers, feature, "1");
}
std::string
makeFeaturesRequestHeader(
bool comprEnabled,
bool ledgerReplayEnabled,
bool txReduceRelayEnabled,
bool vpReduceRelayEnabled)
{
std::stringstream str;
if (comprEnabled)
str << FEATURE_COMPR << "=lz4" << DELIM_FEATURE;
if (ledgerReplayEnabled)
str << FEATURE_LEDGER_REPLAY << "=1" << DELIM_FEATURE;
if (txReduceRelayEnabled)
str << FEATURE_TXRR << "=1" << DELIM_FEATURE;
if (vpReduceRelayEnabled)
str << FEATURE_VPRR << "=1" << DELIM_FEATURE;
return str.str();
}
std::string
makeFeaturesResponseHeader(
http_request_type const& headers,
bool comprEnabled,
bool ledgerReplayEnabled,
bool txReduceRelayEnabled,
bool vpReduceRelayEnabled)
{
std::stringstream str;
if (comprEnabled && isFeatureValue(headers, FEATURE_COMPR, "lz4"))
str << FEATURE_COMPR << "=lz4" << DELIM_FEATURE;
if (ledgerReplayEnabled && featureEnabled(headers, FEATURE_LEDGER_REPLAY))
str << FEATURE_LEDGER_REPLAY << "=1" << DELIM_FEATURE;
if (txReduceRelayEnabled && featureEnabled(headers, FEATURE_TXRR))
str << FEATURE_TXRR << "=1" << DELIM_FEATURE;
if (vpReduceRelayEnabled && featureEnabled(headers, FEATURE_VPRR))
str << FEATURE_VPRR << "=1" << DELIM_FEATURE;
return str.str();
}
/** Hashes the latest finished message from an SSL stream.
@param ssl the session to get the message from.
@param get a pointer to the function to call to retrieve the finished
message. This can be either:
- `SSL_get_finished` or
- `SSL_get_peer_finished`.
@return `true` if successful, `false` otherwise.
@note This construct is non-standard. There are potential "standard"
alternatives that should be considered. For a discussion, on
this topic, see https://github.com/openssl/openssl/issues/5509 and
https://github.com/ripple/rippled/issues/2413.
*/
static std::optional<base_uint<512>>
hashLastMessage(SSL const* ssl, size_t (*get)(const SSL*, void*, size_t))
{
constexpr std::size_t sslMinimumFinishedLength = 12;
unsigned char buf[1024];
size_t len = get(ssl, buf, sizeof(buf));
if (len < sslMinimumFinishedLength)
return std::nullopt;
sha512_hasher h;
base_uint<512> cookie;
SHA512(buf, len, cookie.data());
return cookie;
}
std::optional<uint256>
makeSharedValue(stream_type& ssl, beast::Journal journal)
{
auto const cookie1 = hashLastMessage(ssl.native_handle(), SSL_get_finished);
if (!cookie1)
{
JLOG(journal.error()) << "Cookie generation: local setup not complete";
return std::nullopt;
}
auto const cookie2 =
hashLastMessage(ssl.native_handle(), SSL_get_peer_finished);
if (!cookie2)
{
JLOG(journal.error()) << "Cookie generation: peer setup not complete";
return std::nullopt;
}
auto const result = (*cookie1 ^ *cookie2);
// Both messages hash to the same value and the cookie
// is 0. Don't allow this.
if (result == beast::zero)
{
JLOG(journal.error())
<< "Cookie generation: identical finished messages";
return std::nullopt;
}
return sha512Half(Slice(result.data(), result.size()));
}
void
buildHandshake(
boost::beast::http::fields& h,
ripple::uint256 const& sharedValue,
std::optional<std::uint32_t> networkID,
beast::IP::Address public_ip,
beast::IP::Address remote_ip,
Application& app)
{
if (networkID)
{
// The network identifier, if configured, can be used to specify
// what network we intend to connect to and detect if the remote
// end connects to the same network.
h.insert("Network-ID", std::to_string(*networkID));
}
h.insert(
"Network-Time",
std::to_string(app.timeKeeper().now().time_since_epoch().count()));
h.insert(
"Public-Key",
toBase58(TokenType::NodePublic, app.nodeIdentity().first));
{
auto const sig = signDigest(
app.nodeIdentity().first, app.nodeIdentity().second, sharedValue);
h.insert("Session-Signature", base64_encode(sig.data(), sig.size()));
}
h.insert("Instance-Cookie", std::to_string(app.instanceID()));
if (!app.config().SERVER_DOMAIN.empty())
h.insert("Server-Domain", app.config().SERVER_DOMAIN);
if (beast::IP::is_public(remote_ip))
h.insert("Remote-IP", remote_ip.to_string());
if (!public_ip.is_unspecified())
h.insert("Local-IP", public_ip.to_string());
if (auto const cl = app.getLedgerMaster().getClosedLedger())
{
h.insert("Closed-Ledger", strHex(cl->info().hash));
h.insert("Previous-Ledger", strHex(cl->info().parentHash));
}
}
PublicKey
verifyHandshake(
boost::beast::http::fields const& headers,
ripple::uint256 const& sharedValue,
std::optional<std::uint32_t> networkID,
beast::IP::Address public_ip,
beast::IP::Address remote,
Application& app)
{
if (auto const iter = headers.find("Server-Domain"); iter != headers.end())
{
if (!isProperlyFormedTomlDomain(iter->value()))
throw std::runtime_error("Invalid server domain");
}
if (auto const iter = headers.find("Network-ID"); iter != headers.end())
{
std::uint32_t nid;
if (!beast::lexicalCastChecked(nid, std::string(iter->value())))
throw std::runtime_error("Invalid peer network identifier");
if (networkID && nid != *networkID)
throw std::runtime_error("Peer is on a different network");
}
if (auto const iter = headers.find("Network-Time"); iter != headers.end())
{
auto const netTime =
[str = std::string(iter->value())]() -> TimeKeeper::time_point {
TimeKeeper::duration::rep val;
if (beast::lexicalCastChecked(val, str))
return TimeKeeper::time_point{TimeKeeper::duration{val}};
// It's not an error for the header field to not be present but if
// it is present and it contains junk data, that is an error.
throw std::runtime_error("Invalid peer clock timestamp");
}();
using namespace std::chrono;
auto const ourTime = app.timeKeeper().now();
auto const tolerance = 20s;
// We can't blindly "return a-b;" because TimeKeeper::time_point
// uses an unsigned integer for representing durations, which is
// a problem when trying to subtract time points.
auto calculateOffset = [](TimeKeeper::time_point a,
TimeKeeper::time_point b) {
if (a > b)
return duration_cast<std::chrono::seconds>(a - b);
return -duration_cast<std::chrono::seconds>(b - a);
};
auto const offset = calculateOffset(netTime, ourTime);
if (abs(offset) > tolerance)
throw std::runtime_error("Peer clock is too far off");
}
PublicKey const publicKey = [&headers] {
if (auto const iter = headers.find("Public-Key"); iter != headers.end())
{
auto pk =
parseBase58<PublicKey>(TokenType::NodePublic, iter->value());
if (pk)
{
if (publicKeyType(*pk) != KeyType::secp256k1)
throw std::runtime_error("Unsupported public key type");
return *pk;
}
}
throw std::runtime_error("Bad node public key");
}();
// This check gets two birds with one stone:
//
// 1) it verifies that the node we are talking to has access to the
// private key corresponding to the public node identity it claims.
// 2) it verifies that our SSL session is end-to-end with that node
// and not through a proxy that establishes two separate sessions.
{
auto const iter = headers.find("Session-Signature");
if (iter == headers.end())
throw std::runtime_error("No session signature specified");
auto sig = base64_decode(iter->value());
if (!verifyDigest(publicKey, sharedValue, makeSlice(sig), false))
throw std::runtime_error("Failed to verify session");
}
if (publicKey == app.nodeIdentity().first)
throw std::runtime_error("Self connection");
if (auto const iter = headers.find("Local-IP"); iter != headers.end())
{
boost::system::error_code ec;
auto const local_ip =
boost::asio::ip::address::from_string(iter->value(), ec);
if (ec)
throw std::runtime_error("Invalid Local-IP");
if (beast::IP::is_public(remote) && remote != local_ip)
throw std::runtime_error(
"Incorrect Local-IP: " + remote.to_string() + " instead of " +
local_ip.to_string());
}
if (auto const iter = headers.find("Remote-IP"); iter != headers.end())
{
boost::system::error_code ec;
auto const remote_ip =
boost::asio::ip::address::from_string(iter->value(), ec);
if (ec)
throw std::runtime_error("Invalid Remote-IP");
if (beast::IP::is_public(remote) &&
!beast::IP::is_unspecified(public_ip))
{
// We know our public IP and peer reports our connection came
// from some other IP.
if (remote_ip != public_ip)
throw std::runtime_error(
"Incorrect Remote-IP: " + public_ip.to_string() +
" instead of " + remote_ip.to_string());
}
}
return publicKey;
}
auto
makeRequest(
bool crawlPublic,
bool comprEnabled,
bool ledgerReplayEnabled,
bool txReduceRelayEnabled,
bool vpReduceRelayEnabled) -> request_type
{
request_type m;
m.method(boost::beast::http::verb::get);
m.target("/");
m.version(11);
m.insert("User-Agent", BuildInfo::getFullVersionString());
m.insert("Upgrade", supportedProtocolVersions());
m.insert("Connection", "Upgrade");
m.insert("Connect-As", "Peer");
m.insert("Crawl", crawlPublic ? "public" : "private");
m.insert(
"X-Protocol-Ctl",
makeFeaturesRequestHeader(
comprEnabled,
ledgerReplayEnabled,
txReduceRelayEnabled,
vpReduceRelayEnabled));
return m;
}
http_response_type
makeResponse(
bool crawlPublic,
http_request_type const& req,
beast::IP::Address public_ip,
beast::IP::Address remote_ip,
uint256 const& sharedValue,
std::optional<std::uint32_t> networkID,
ProtocolVersion protocol,
Application& app)
{
http_response_type resp;
resp.result(boost::beast::http::status::switching_protocols);
resp.version(req.version());
resp.insert("Connection", "Upgrade");
resp.insert("Upgrade", to_string(protocol));
resp.insert("Connect-As", "Peer");
resp.insert("Server", BuildInfo::getFullVersionString());
resp.insert("Crawl", crawlPublic ? "public" : "private");
resp.insert(
"X-Protocol-Ctl",
makeFeaturesResponseHeader(
req,
app.config().COMPRESSION,
app.config().LEDGER_REPLAY,
app.config().TX_REDUCE_RELAY_ENABLE,
app.config().VP_REDUCE_RELAY_ENABLE));
buildHandshake(resp, sharedValue, networkID, public_ip, remote_ip, app);
return resp;
}
} // namespace ripple