Send RLPx auth in EIP-8 format (#287)

This commit is contained in:
rakita 2021-03-03 12:58:10 +01:00 committed by GitHub
parent 7c9eed8d65
commit ba011eba15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 21 deletions

View File

@ -20,14 +20,13 @@ use ethkey::{
crypto::{ecdh, ecies}, crypto::{ecdh, ecies},
recover, sign, Generator, KeyPair, Public, Random, Secret, recover, sign, Generator, KeyPair, Public, Random, Secret,
}; };
use hash::write_keccak;
use host::HostInfo; use host::HostInfo;
use io::{IoContext, StreamToken}; use io::{IoContext, StreamToken};
use mio::tcp::*; use mio::tcp::*;
use network::{Error, ErrorKind}; use network::{Error, ErrorKind};
use node_table::NodeId; use node_table::NodeId;
use parity_bytes::Bytes; use parity_bytes::Bytes;
use rand::random; use rand::{random, Rng};
use rlp::{Rlp, RlpStream}; use rlp::{Rlp, RlpStream};
use std::time::Duration; use std::time::Duration;
@ -314,25 +313,23 @@ impl Handshake {
Message: Send + Clone + Sync + 'static, Message: Send + Clone + Sync + 'static,
{ {
trace!(target: "network", "Sending handshake auth to {:?}", self.connection.remote_addr_str()); trace!(target: "network", "Sending handshake auth to {:?}", self.connection.remote_addr_str());
let mut data = [0u8; /*Signature::SIZE*/ 65 + /*H256::SIZE*/ 32 + /*Public::SIZE*/ 64 + /*H256::SIZE*/ 32 + 1]; //TODO: use associated constants let mut rlp = RlpStream::new_list(4);
let len = data.len(); let shared = *ecdh::agree(secret, &self.id)?;
{ rlp.append(&sign(self.ecdhe.secret(), &(shared ^ self.nonce))?.to_vec());
data[len - 1] = 0x0; rlp.append(public);
let (sig, rest) = data.split_at_mut(65); rlp.append(&self.nonce);
let (hepubk, rest) = rest.split_at_mut(32); rlp.append(&PROTOCOL_VERSION);
let (pubk, rest) = rest.split_at_mut(64); let mut encoded = rlp.out();
let (nonce, _) = rest.split_at_mut(32); encoded.resize(
encoded.len() + rand::thread_rng().gen_range::<usize>(100, 301),
// E(remote-pubk, S(ecdhe-random, ecdh-shared-secret^nonce) || H(ecdhe-random-pubk) || pubk || nonce || 0x0) 0,
let shared = *ecdh::agree(secret, &self.id)?; );
sig.copy_from_slice(&*sign(self.ecdhe.secret(), &(shared ^ self.nonce))?); let len = (encoded.len() + ECIES_OVERHEAD) as u16;
write_keccak(self.ecdhe.public(), hepubk); let prefix = len.to_be_bytes();
pubk.copy_from_slice(public); let message = ecies::encrypt(&self.id, &prefix, &encoded)?;
nonce.copy_from_slice(&self.nonce); self.auth_cipher.extend_from_slice(&prefix);
} self.auth_cipher.extend_from_slice(&message);
let message = ecies::encrypt(&self.id, &[], &data)?; self.connection.send(io, self.auth_cipher.clone());
self.auth_cipher = message.clone();
self.connection.send(io, message);
self.connection.expect(V4_ACK_PACKET_SIZE); self.connection.expect(V4_ACK_PACKET_SIZE);
self.state = HandshakeState::ReadingAck; self.state = HandshakeState::ReadingAck;
Ok(()) Ok(())