Send RLPx auth in EIP-8 format (#287)
This commit is contained in:
parent
7c9eed8d65
commit
ba011eba15
@ -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();
|
|
||||||
{
|
|
||||||
data[len - 1] = 0x0;
|
|
||||||
let (sig, rest) = data.split_at_mut(65);
|
|
||||||
let (hepubk, rest) = rest.split_at_mut(32);
|
|
||||||
let (pubk, rest) = rest.split_at_mut(64);
|
|
||||||
let (nonce, _) = rest.split_at_mut(32);
|
|
||||||
|
|
||||||
// E(remote-pubk, S(ecdhe-random, ecdh-shared-secret^nonce) || H(ecdhe-random-pubk) || pubk || nonce || 0x0)
|
|
||||||
let shared = *ecdh::agree(secret, &self.id)?;
|
let shared = *ecdh::agree(secret, &self.id)?;
|
||||||
sig.copy_from_slice(&*sign(self.ecdhe.secret(), &(shared ^ self.nonce))?);
|
rlp.append(&sign(self.ecdhe.secret(), &(shared ^ self.nonce))?.to_vec());
|
||||||
write_keccak(self.ecdhe.public(), hepubk);
|
rlp.append(public);
|
||||||
pubk.copy_from_slice(public);
|
rlp.append(&self.nonce);
|
||||||
nonce.copy_from_slice(&self.nonce);
|
rlp.append(&PROTOCOL_VERSION);
|
||||||
}
|
let mut encoded = rlp.out();
|
||||||
let message = ecies::encrypt(&self.id, &[], &data)?;
|
encoded.resize(
|
||||||
self.auth_cipher = message.clone();
|
encoded.len() + rand::thread_rng().gen_range::<usize>(100, 301),
|
||||||
self.connection.send(io, message);
|
0,
|
||||||
|
);
|
||||||
|
let len = (encoded.len() + ECIES_OVERHEAD) as u16;
|
||||||
|
let prefix = len.to_be_bytes();
|
||||||
|
let message = ecies::encrypt(&self.id, &prefix, &encoded)?;
|
||||||
|
self.auth_cipher.extend_from_slice(&prefix);
|
||||||
|
self.auth_cipher.extend_from_slice(&message);
|
||||||
|
self.connection.send(io, self.auth_cipher.clone());
|
||||||
self.connection.expect(V4_ACK_PACKET_SIZE);
|
self.connection.expect(V4_ACK_PACKET_SIZE);
|
||||||
self.state = HandshakeState::ReadingAck;
|
self.state = HandshakeState::ReadingAck;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
Reference in New Issue
Block a user