This repository was archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathRLPxHandshake.cpp
More file actions
412 lines (374 loc) · 12.7 KB
/
RLPxHandshake.cpp
File metadata and controls
412 lines (374 loc) · 12.7 KB
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
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file RLPXHandshake.cpp
* @author Alex Leverington <nessence@gmail.com>
* @date 2015
*/
#include "Host.h"
#include "Session.h"
#include "Peer.h"
#include "RLPxHandshake.h"
using namespace std;
using namespace dev;
using namespace dev::p2p;
using namespace dev::crypto;
void RLPXHandshake::writeAuth()
{
clog(NetP2PConnect) << "p2p.connect.egress sending auth to " << m_socket->remoteEndpoint();
m_auth.resize(Signature::size + h256::size + Public::size + h256::size + 1);
bytesRef sig(&m_auth[0], Signature::size);
bytesRef hepubk(&m_auth[Signature::size], h256::size);
bytesRef pubk(&m_auth[Signature::size + h256::size], Public::size);
bytesRef nonce(&m_auth[Signature::size + h256::size + Public::size], h256::size);
// E(remote-pubk, S(ecdhe-random, ecdh-shared-secret^nonce) || H(ecdhe-random-pubk) || pubk || nonce || 0x0)
Secret staticShared;
crypto::ecdh::agree(m_host->m_alias.secret(), m_remote, staticShared);
sign(m_ecdheLocal.secret(), staticShared.makeInsecure() ^ m_nonce).ref().copyTo(sig);
sha3(m_ecdheLocal.pub().ref(), hepubk);
m_host->m_alias.pub().ref().copyTo(pubk);
m_nonce.ref().copyTo(nonce);
m_auth[m_auth.size() - 1] = 0x0;
encryptECIES(m_remote, &m_auth, m_authCipher);
auto self(shared_from_this());
ba::async_write(m_socket->ref(), ba::buffer(m_authCipher), [this, self](boost::system::error_code ec, std::size_t)
{
transition(ec);
});
}
void RLPXHandshake::writeAck()
{
clog(NetP2PConnect) << "p2p.connect.ingress sending ack to " << m_socket->remoteEndpoint();
m_ack.resize(Public::size + h256::size + 1);
bytesRef epubk(&m_ack[0], Public::size);
bytesRef nonce(&m_ack[Public::size], h256::size);
m_ecdheLocal.pub().ref().copyTo(epubk);
m_nonce.ref().copyTo(nonce);
m_ack[m_ack.size() - 1] = 0x0;
encryptECIES(m_remote, &m_ack, m_ackCipher);
auto self(shared_from_this());
ba::async_write(m_socket->ref(), ba::buffer(m_ackCipher), [this, self](boost::system::error_code ec, std::size_t)
{
transition(ec);
});
}
void RLPXHandshake::writeAckEIP8()
{
clog(NetP2PConnect) << "p2p.connect.ingress sending EIP-8 ack to " << m_socket->remoteEndpoint();
RLPStream rlp;
rlp.appendList(3)
<< m_ecdheLocal.pub()
<< m_nonce
<< c_rlpxVersion;
m_ack = rlp.out();
int padAmount(rand()%100 + 100);
m_ack.resize(m_ack.size() + padAmount, 0);
bytes prefix(2);
toBigEndian<uint16_t>(m_ack.size() + c_eciesOverhead, prefix);
encryptECIES(m_remote, bytesConstRef(&prefix), &m_ack, m_ackCipher);
m_ackCipher.insert(m_ackCipher.begin(), prefix.begin(), prefix.end());
auto self(shared_from_this());
ba::async_write(m_socket->ref(), ba::buffer(m_ackCipher), [this, self](boost::system::error_code ec, std::size_t)
{
transition(ec);
});
}
void RLPXHandshake::setAuthValues(Signature const& _sig, Public const& _remotePubk, h256 const& _remoteNonce, uint64_t _remoteVersion)
{
_remotePubk.ref().copyTo(m_remote.ref());
_remoteNonce.ref().copyTo(m_remoteNonce.ref());
m_remoteVersion = _remoteVersion;
Secret sharedSecret;
crypto::ecdh::agree(m_host->m_alias.secret(), _remotePubk, sharedSecret);
m_ecdheRemote = recover(_sig, sharedSecret.makeInsecure() ^ _remoteNonce);
}
void RLPXHandshake::readAuth()
{
clog(NetP2PConnect) << "p2p.connect.ingress receiving auth from " << m_socket->remoteEndpoint();
m_authCipher.resize(307);
auto self(shared_from_this());
ba::async_read(m_socket->ref(), ba::buffer(m_authCipher, 307), [this, self](boost::system::error_code ec, std::size_t)
{
if (ec)
transition(ec);
else if (decryptECIES(m_host->m_alias.secret(), bytesConstRef(&m_authCipher), m_auth))
{
bytesConstRef data(&m_auth);
Signature sig(data.cropped(0, Signature::size));
Public pubk(data.cropped(Signature::size + h256::size, Public::size));
h256 nonce(data.cropped(Signature::size + h256::size + Public::size, h256::size));
setAuthValues(sig, pubk, nonce, 4);
transition();
}
else
readAuthEIP8();
});
}
void RLPXHandshake::readAuthEIP8()
{
assert(m_authCipher.size() == 307);
uint16_t size(m_authCipher[0]<<8 | m_authCipher[1]);
clog(NetP2PConnect) << "p2p.connect.ingress receiving " << size << "bytes EIP-8 auth from " << m_socket->remoteEndpoint();
m_authCipher.resize((size_t)size + 2);
auto rest = ba::buffer(ba::buffer(m_authCipher) + 307);
auto self(shared_from_this());
ba::async_read(m_socket->ref(), rest, [this, self](boost::system::error_code ec, std::size_t)
{
bytesConstRef ct(&m_authCipher);
if (ec)
transition(ec);
else if (decryptECIES(m_host->m_alias.secret(), ct.cropped(0, 2), ct.cropped(2), m_auth))
{
RLP rlp(m_auth, RLP::ThrowOnFail | RLP::FailIfTooSmall);
setAuthValues(
rlp[0].toHash<Signature>(),
rlp[1].toHash<Public>(),
rlp[2].toHash<h256>(),
rlp[3].toInt<uint64_t>()
);
m_nextState = AckAuthEIP8;
transition();
}
else
{
clog(NetP2PConnect) << "p2p.connect.ingress auth decrypt failed for" << m_socket->remoteEndpoint();
m_nextState = Error;
transition();
}
});
}
void RLPXHandshake::readAck()
{
clog(NetP2PConnect) << "p2p.connect.egress receiving ack from " << m_socket->remoteEndpoint();
m_ackCipher.resize(210);
auto self(shared_from_this());
ba::async_read(m_socket->ref(), ba::buffer(m_ackCipher, 210), [this, self](boost::system::error_code ec, std::size_t)
{
if (ec)
transition(ec);
else if (decryptECIES(m_host->m_alias.secret(), bytesConstRef(&m_ackCipher), m_ack))
{
bytesConstRef(&m_ack).cropped(0, Public::size).copyTo(m_ecdheRemote.ref());
bytesConstRef(&m_ack).cropped(Public::size, h256::size).copyTo(m_remoteNonce.ref());
m_remoteVersion = 4;
transition();
}
else
readAckEIP8();
});
}
void RLPXHandshake::readAckEIP8()
{
assert(m_ackCipher.size() == 210);
uint16_t size(m_ackCipher[0]<<8 | m_ackCipher[1]);
clog(NetP2PConnect) << "p2p.connect.egress receiving " << size << "bytes EIP-8 ack from " << m_socket->remoteEndpoint();
m_ackCipher.resize((size_t)size + 2);
auto rest = ba::buffer(ba::buffer(m_ackCipher) + 210);
auto self(shared_from_this());
ba::async_read(m_socket->ref(), rest, [this, self](boost::system::error_code ec, std::size_t)
{
bytesConstRef ct(&m_ackCipher);
if (ec)
transition(ec);
else if (decryptECIES(m_host->m_alias.secret(), ct.cropped(0, 2), ct.cropped(2), m_ack))
{
RLP rlp(m_ack, RLP::ThrowOnFail | RLP::FailIfTooSmall);
m_ecdheRemote = rlp[0].toHash<Public>();
m_remoteNonce = rlp[1].toHash<h256>();
m_remoteVersion = rlp[2].toInt<uint64_t>();
transition();
}
else
{
clog(NetP2PConnect) << "p2p.connect.egress ack decrypt failed for " << m_socket->remoteEndpoint();
m_nextState = Error;
transition();
}
});
}
void RLPXHandshake::cancel()
{
m_cancel = true;
m_idleTimer.cancel();
m_socket->close();
m_io.reset();
}
void RLPXHandshake::error()
{
auto connected = m_socket->isConnected();
if (connected && !m_socket->remoteEndpoint().address().is_unspecified())
clog(NetP2PConnect) << "Disconnecting " << m_socket->remoteEndpoint() << " (Handshake Failed)";
else
clog(NetP2PConnect) << "Handshake Failed (Connection reset by peer)";
cancel();
}
void RLPXHandshake::transition(boost::system::error_code _ech)
{
// reset timeout
m_idleTimer.cancel();
if (_ech || m_nextState == Error || m_cancel)
{
clog(NetP2PConnect) << "Handshake Failed (I/O Error:" << _ech.message() << ")";
return error();
}
auto self(shared_from_this());
assert(m_nextState != StartSession);
m_idleTimer.expires_from_now(c_timeout);
m_idleTimer.async_wait([this, self](boost::system::error_code const& _ec)
{
if (!_ec)
{
if (!m_socket->remoteEndpoint().address().is_unspecified())
clog(NetP2PConnect) << "Disconnecting " << m_socket->remoteEndpoint() << " (Handshake Timeout)";
cancel();
}
});
if (m_nextState == New)
{
m_nextState = AckAuth;
if (m_originated)
writeAuth();
else
readAuth();
}
else if (m_nextState == AckAuth)
{
m_nextState = WriteHello;
if (m_originated)
readAck();
else
writeAck();
}
else if (m_nextState == AckAuthEIP8)
{
m_nextState = WriteHello;
if (m_originated)
readAck();
else
writeAckEIP8();
}
else if (m_nextState == WriteHello)
{
m_nextState = ReadHello;
clog(NetP2PConnect) << (m_originated ? "p2p.connect.egress" : "p2p.connect.ingress") << "sending capabilities handshake";
/// This pointer will be freed if there is an error otherwise
/// it will be passed to Host which will take ownership.
m_io.reset(new RLPXFrameCoder(*this));
RLPStream s;
s.append((unsigned)HelloPacket).appendList(5)
<< dev::p2p::c_protocolVersion
<< m_host->m_clientVersion
<< m_host->caps()
<< m_host->listenPort()
<< m_host->id();
bytes packet;
s.swapOut(packet);
m_io->writeSingleFramePacket(&packet, m_handshakeOutBuffer);
ba::async_write(m_socket->ref(), ba::buffer(m_handshakeOutBuffer), [this, self](boost::system::error_code ec, std::size_t)
{
transition(ec);
});
}
else if (m_nextState == ReadHello)
{
// Authenticate and decrypt initial hello frame with initial RLPXFrameCoder
// and request m_host to start session.
m_nextState = StartSession;
// read frame header
unsigned const handshakeSize = 32;
m_handshakeInBuffer.resize(handshakeSize);
ba::async_read(m_socket->ref(), boost::asio::buffer(m_handshakeInBuffer, handshakeSize), [this, self](boost::system::error_code ec, std::size_t)
{
if (ec)
transition(ec);
else
{
if (!m_io)
{
clog(NetP2PWarn) << "Internal error in handshake: RLPXFrameCoder disappeared.";
m_nextState = Error;
transition();
return;
}
/// authenticate and decrypt header
if (!m_io->authAndDecryptHeader(bytesRef(m_handshakeInBuffer.data(), m_handshakeInBuffer.size())))
{
m_nextState = Error;
transition();
return;
}
clog(NetP2PNote) << (m_originated ? "p2p.connect.egress" : "p2p.connect.ingress") << "recvd hello header";
/// check frame size
bytes& header = m_handshakeInBuffer;
uint32_t frameSize = (uint32_t)(header[2]) | (uint32_t)(header[1])<<8 | (uint32_t)(header[0])<<16;
if (frameSize > 1024)
{
// all future frames: 16777216
clog(NetP2PWarn) << (m_originated ? "p2p.connect.egress" : "p2p.connect.ingress") << "hello frame is too large" << frameSize;
m_nextState = Error;
transition();
return;
}
/// rlp of header has protocol-type, sequence-id[, total-packet-size]
bytes headerRLP(header.size() - 3 - h128::size); // this is always 32 - 3 - 16 = 13. wtf?
bytesConstRef(&header).cropped(3).copyTo(&headerRLP);
/// read padded frame and mac
m_handshakeInBuffer.resize(frameSize + ((16 - (frameSize % 16)) % 16) + h128::size);
ba::async_read(m_socket->ref(), boost::asio::buffer(m_handshakeInBuffer, m_handshakeInBuffer.size()), [this, self, headerRLP](boost::system::error_code ec, std::size_t)
{
m_idleTimer.cancel();
if (ec)
transition(ec);
else
{
if (!m_io)
{
clog(NetP2PWarn) << "Internal error in handshake: RLPXFrameCoder disappeared.";
m_nextState = Error;
transition();
return;
}
bytesRef frame(&m_handshakeInBuffer);
if (!m_io->authAndDecryptFrame(frame))
{
clog(NetTriviaSummary) << (m_originated ? "p2p.connect.egress" : "p2p.connect.ingress") << "hello frame: decrypt failed";
m_nextState = Error;
transition();
return;
}
PacketType packetType = frame[0] == 0x80 ? HelloPacket : (PacketType)frame[0];
if (packetType != HelloPacket)
{
clog(NetTriviaSummary) << (m_originated ? "p2p.connect.egress" : "p2p.connect.ingress") << "hello frame: invalid packet type";
m_nextState = Error;
transition();
return;
}
clog(NetTriviaSummary) << (m_originated ? "p2p.connect.egress" : "p2p.connect.ingress") << "hello frame: success. starting session.";
try
{
RLP rlp(frame.cropped(1), RLP::ThrowOnFail | RLP::FailIfTooSmall);
m_host->startPeerSession(m_remote, rlp, move(m_io), m_socket);
}
catch (std::exception const& _e)
{
clog(NetWarn) << "Handshake causing an exception:" << _e.what();
m_nextState = Error;
transition();
}
}
});
}
});
}
}