working test vectors for Noise handshake

pull/19/head
Niko 2 years ago
parent 8443939184
commit b984cd7532
  1. 16
      ng-app-js/src/lib.rs
  2. 22
      ngd/src/main.rs
  3. 15
      p2p-broker/src/server_ws.rs
  4. 23
      p2p-client-ws/src/remote_ws.rs
  5. 6
      p2p-client-ws/src/remote_ws_wasm.rs
  6. 16
      p2p-net/src/broker.rs
  7. 30
      p2p-net/src/connection.rs
  8. 9
      p2p-net/src/utils.rs

@ -28,16 +28,16 @@ pub async fn greet(name: &str) {
//spawn_and_log_error(testt("ws://127.0.0.1:3012")); //spawn_and_log_error(testt("ws://127.0.0.1:3012"));
async fn method() -> ResultSend<()> { async fn method() -> ResultSend<()> {
let pubkey_null: [u8; 32] = [ let server_key = PubKey::Ed25519PubKey([
59, 106, 39, 188, 206, 182, 164, 45, 98, 163, 168, 208, 42, 111, 13, 115, 101, 50, 21, 22, 140, 190, 111, 82, 151, 27, 133, 83, 121, 71, 36, 209, 53, 53, 114, 52, 254, 218,
119, 29, 226, 67, 166, 58, 192, 72, 161, 139, 89, 218, 41, 241, 52, 155, 231, 83, 188, 189, 47, 135, 105, 213, 39, 91,
]; ]);
let server_key = PubKey::Ed25519PubKey(pubkey_null);
log!("start connecting"); log!("start connecting");
//let cnx = Arc::new(); //let cnx = Arc::new();
let (priv_key, pub_key) = generate_keypair(); let keys = p2p_net::utils::gen_keys();
let pub_key = PubKey::Ed25519PubKey(keys.1);
let res = BROKER let res = BROKER
.write() .write()
.await .await
@ -45,7 +45,7 @@ pub async fn greet(name: &str) {
Box::new(ConnectionWebSocket {}), Box::new(ConnectionWebSocket {}),
IP::try_from(&IpAddr::from_str("127.0.0.1").unwrap()).unwrap(), IP::try_from(&IpAddr::from_str("127.0.0.1").unwrap()).unwrap(),
None, None,
priv_key, keys.0,
pub_key, pub_key,
server_key, server_key,
) )

@ -8,6 +8,7 @@
// according to those terms. // according to those terms.
use p2p_broker::server_ws::run_server; use p2p_broker::server_ws::run_server;
use p2p_net::utils::{gen_keys, Sensitive, U8Array};
use p2p_net::WS_PORT; use p2p_net::WS_PORT;
use p2p_repo::{ use p2p_repo::{
types::{PrivKey, PubKey}, types::{PrivKey, PubKey},
@ -17,17 +18,22 @@ use p2p_repo::{
#[async_std::main] #[async_std::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
println!("Starting NextGraph daemon..."); println!("Starting NextGraph daemon...");
//let keys = generate_keypair(); // let keys = generate_keypair();
//println!("Public key of node: {:?}", keys.1); // let keys = gen_keys();
//println!("Private key of node: {:?}", keys.0); // println!("Public key of node: {:?}", keys.1);
// println!("Private key of node: {:?}", keys.0.as_slice());
let pubkey = PubKey::Ed25519PubKey([ let pubkey = PubKey::Ed25519PubKey([
158, 209, 118, 156, 133, 101, 241, 72, 91, 80, 160, 184, 201, 66, 245, 2, 91, 16, 10, 143, 22, 140, 190, 111, 82, 151, 27, 133, 83, 121, 71, 36, 209, 53, 53, 114, 52, 254, 218, 241,
50, 206, 222, 187, 24, 122, 51, 59, 214, 132, 169, 154, 52, 155, 231, 83, 188, 189, 47, 135, 105, 213, 39, 91,
]); ]);
let privkey = PrivKey::Ed25519PrivKey([ let privkey = Sensitive::<[u8; 32]>::from_slice(&[
254, 127, 162, 204, 53, 25, 141, 12, 4, 118, 23, 42, 52, 246, 37, 52, 76, 11, 176, 219, 31, 160, 133, 237, 116, 151, 53, 156, 151, 21, 227, 226, 35, 1, 224, 44, 207, 148, 33, 79, 160,
241, 25, 73, 199, 118, 209, 85, 159, 234, 31, 206, 115, 173, 154, 118, 251, 146, 34, 204, 40, 190, 155, 112,
]); ]);
//let keys = gen_keys();
println!("Public key of node: {:?}", pubkey);
println!("Private key of node: {:?}", privkey.as_slice());
run_server(format!("127.0.0.1:{}", WS_PORT).as_str(), privkey, pubkey).await?; run_server(format!("127.0.0.1:{}", WS_PORT).as_str(), privkey, pubkey).await?;
Ok(()) Ok(())

@ -24,11 +24,13 @@ use p2p_client_ws::remote_ws::ConnectionWebSocket;
use p2p_net::broker::*; use p2p_net::broker::*;
use p2p_net::connection::IAccept; use p2p_net::connection::IAccept;
use p2p_net::types::IP; use p2p_net::types::IP;
use p2p_net::utils::Sensitive;
use p2p_repo::types::{PrivKey, PubKey}; use p2p_repo::types::{PrivKey, PubKey};
use p2p_repo::utils::generate_keypair; use p2p_repo::utils::generate_keypair;
use p2p_stores_lmdb::broker_store::LmdbBrokerStore; use p2p_stores_lmdb::broker_store::LmdbBrokerStore;
use p2p_stores_lmdb::repo_store::LmdbRepoStore; use p2p_stores_lmdb::repo_store::LmdbRepoStore;
use std::fs; use std::fs;
use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
use std::{thread, time}; use std::{thread, time};
use tempfile::Builder; use tempfile::Builder;
@ -146,10 +148,10 @@ pub async fn run_server_accept_one(addrs: &str) -> std::io::Result<()> {
Ok(()) Ok(())
} }
use p2p_net::utils::U8Array;
pub async fn run_server( pub async fn run_server(
addrs: &str, addrs: &str,
peer_priv_key: PrivKey, peer_priv_key: Sensitive<[u8; 32]>,
peer_pub_key: PubKey, peer_pub_key: PubKey,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
let root = tempfile::Builder::new() let root = tempfile::Builder::new()
@ -175,7 +177,14 @@ pub async fn run_server(
let mut ws = accept_async(tcp).await.unwrap(); let mut ws = accept_async(tcp).await.unwrap();
let cws = ConnectionWebSocket {}; let cws = ConnectionWebSocket {};
let base = cws.accept(peer_priv_key, peer_pub_key, ws).await.unwrap(); let base = cws
.accept(
Sensitive::<[u8; 32]>::from_slice(peer_priv_key.deref()),
peer_pub_key,
ws,
)
.await
.unwrap();
//TODO FIXME get remote_peer_id from ConnectionBase (once it is available) //TODO FIXME get remote_peer_id from ConnectionBase (once it is available)
let (priv_key, pub_key) = generate_keypair(); let (priv_key, pub_key) = generate_keypair();

@ -28,7 +28,7 @@ use async_std::task;
use p2p_net::errors::*; use p2p_net::errors::*;
use p2p_net::log; use p2p_net::log;
use p2p_net::types::*; use p2p_net::types::*;
use p2p_net::utils::{spawn_and_log_error, Receiver, ResultSend, Sender}; use p2p_net::utils::{spawn_and_log_error, Receiver, ResultSend, Sender, Sensitive};
use p2p_net::{connection::*, WS_PORT}; use p2p_net::{connection::*, WS_PORT};
use p2p_repo::types::*; use p2p_repo::types::*;
use p2p_repo::utils::{generate_keypair, now_timestamp}; use p2p_repo::utils::{generate_keypair, now_timestamp};
@ -44,7 +44,7 @@ impl IConnect for ConnectionWebSocket {
async fn open( async fn open(
&self, &self,
ip: IP, ip: IP,
peer_privk: PrivKey, peer_privk: Sensitive<[u8; 32]>,
peer_pubk: PubKey, peer_pubk: PubKey,
remote_peer: DirectPeerId, remote_peer: DirectPeerId,
) -> Result<ConnectionBase, NetError> { ) -> Result<ConnectionBase, NetError> {
@ -169,7 +169,7 @@ impl IAccept for ConnectionWebSocket {
type Socket = WebSocketStream<TcpStream>; type Socket = WebSocketStream<TcpStream>;
async fn accept( async fn accept(
&self, &self,
peer_privk: PrivKey, peer_privk: Sensitive<[u8; 32]>,
peer_pubk: PubKey, peer_pubk: PubKey,
socket: Self::Socket, socket: Self::Socket,
) -> Result<ConnectionBase, NetError> { ) -> Result<ConnectionBase, NetError> {
@ -211,8 +211,9 @@ async fn close_ws(
} else { } else {
ConnectionCommand::Error(NetError::try_from(code - 4949).unwrap()) ConnectionCommand::Error(NetError::try_from(code - 4949).unwrap())
}; };
log!("sending to read loop {:?}", cmd);
let _ = futures::SinkExt::send(receiver, cmd).await; let _ = futures::SinkExt::send(receiver, cmd).await;
stream stream
.close(Some(CloseFrame { .close(Some(CloseFrame {
code: CloseCode::Library(code), code: CloseCode::Library(code),
@ -350,12 +351,16 @@ mod test {
getrandom::getrandom(&mut random_buf).unwrap(); getrandom::getrandom(&mut random_buf).unwrap();
let server_key = PubKey::Ed25519PubKey([ let server_key = PubKey::Ed25519PubKey([
158, 209, 118, 156, 133, 101, 241, 72, 91, 80, 160, 184, 201, 66, 245, 2, 91, 16, 10, 22, 140, 190, 111, 82, 151, 27, 133, 83, 121, 71, 36, 209, 53, 53, 114, 52, 254, 218,
143, 50, 206, 222, 187, 24, 122, 51, 59, 214, 132, 169, 154, 241, 52, 155, 231, 83, 188, 189, 47, 135, 105, 213, 39, 91,
]); ]);
let keys = p2p_net::utils::gen_keys();
let pub_key = PubKey::Ed25519PubKey(keys.1);
log!("start connecting"); log!("start connecting");
let (priv_key, pub_key) = generate_keypair(); //let (priv_key, pub_key) = generate_keypair();
{ {
let res = BROKER let res = BROKER
.write() .write()
@ -364,8 +369,8 @@ mod test {
Box::new(ConnectionWebSocket {}), Box::new(ConnectionWebSocket {}),
IP::try_from(&IpAddr::from_str("127.0.0.1").unwrap()).unwrap(), IP::try_from(&IpAddr::from_str("127.0.0.1").unwrap()).unwrap(),
None, None,
priv_key, keys.0,
pub_key, pub_key.clone(),
server_key, server_key,
) )
.await; .await;

@ -37,8 +37,8 @@ impl IConnect for ConnectionWebSocket {
async fn open( async fn open(
&self, &self,
ip: IP, ip: IP,
peer_pubk: PrivKey, peer_privk: Sensitive<[u8; 32]>,
peer_privk: PubKey, peer_pubk: PubKey,
remote_peer: DirectPeerId, remote_peer: DirectPeerId,
) -> Result<ConnectionBase, NetError> { ) -> Result<ConnectionBase, NetError> {
//pub async fn testt(url: &str) -> ResultSend<()> { //pub async fn testt(url: &str) -> ResultSend<()> {
@ -54,7 +54,7 @@ impl IConnect for ConnectionWebSocket {
//let (mut sender_tx, sender_rx) = mpsc::unbounded(); //let (mut sender_tx, sender_rx) = mpsc::unbounded();
//let (mut receiver_tx, receiver_rx) = mpsc::unbounded(); //let (mut receiver_tx, receiver_rx) = mpsc::unbounded();
cnx.start_read_loop(peer_pubk, Some(remote_peer)); cnx.start_read_loop(peer_privk, Some(remote_peer));
let mut shutdown = cnx.set_shutdown(); let mut shutdown = cnx.set_shutdown();
spawn_and_log_error(ws_loop( spawn_and_log_error(ws_loop(

@ -9,11 +9,14 @@ use async_std::stream::StreamExt;
use async_std::sync::{Arc, RwLock}; use async_std::sync::{Arc, RwLock};
use futures::channel::mpsc; use futures::channel::mpsc;
use futures::SinkExt; use futures::SinkExt;
use noise_protocol::U8Array;
use noise_rust_crypto::sensitive::Sensitive;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use p2p_repo::types::{PrivKey, PubKey}; use p2p_repo::types::{PrivKey, PubKey};
use p2p_repo::utils::generate_keypair; use p2p_repo::utils::generate_keypair;
use std::collections::HashMap; use std::collections::HashMap;
use std::net::IpAddr; use std::net::IpAddr;
use std::ops::Deref;
#[derive(Debug)] #[derive(Debug)]
pub enum PeerConnection { pub enum PeerConnection {
@ -212,7 +215,7 @@ impl Broker {
cnx: Box<dyn IConnect>, cnx: Box<dyn IConnect>,
ip: IP, ip: IP,
core: Option<String>, // the interface used as egress for this connection core: Option<String>, // the interface used as egress for this connection
peer_privk: PrivKey, peer_privk: Sensitive<[u8; 32]>,
peer_pubk: PubKey, peer_pubk: PubKey,
remote_peer_id: DirectPeerId, remote_peer_id: DirectPeerId,
) -> Result<(), NetError> { ) -> Result<(), NetError> {
@ -226,7 +229,14 @@ impl Broker {
//let cnx = Arc::new(); //let cnx = Arc::new();
//let (priv_key, pub_key) = generate_keypair(); //let (priv_key, pub_key) = generate_keypair();
log!("CONNECTING"); log!("CONNECTING");
let mut connection = cnx.open(ip, peer_privk, peer_pubk, remote_peer_id).await?; let mut connection = cnx
.open(
ip,
Sensitive::<[u8; 32]>::from_slice(peer_privk.deref()),
peer_pubk,
remote_peer_id,
)
.await?;
let join = connection.take_shutdown(); let join = connection.take_shutdown();
@ -254,7 +264,7 @@ impl Broker {
cnx: Box<dyn IConnect>, cnx: Box<dyn IConnect>,
ip: IP, ip: IP,
core: Option<String>, // the interface used as egress for this connection core: Option<String>, // the interface used as egress for this connection
peer_privk: PrivKey, peer_privk: Sensitive<[u8; 32]>,
peer_pubkey: PubKey, peer_pubkey: PubKey,
remote_peer_id: DirectPeerId, remote_peer_id: DirectPeerId,
) -> ResultSend<()> { ) -> ResultSend<()> {

@ -39,7 +39,7 @@ pub trait IConnect: Send + Sync {
async fn open( async fn open(
&self, &self,
ip: IP, ip: IP,
peer_privk: PrivKey, peer_privk: Sensitive<[u8; 32]>,
peer_pubk: PubKey, peer_pubk: PubKey,
remote_peer: DirectPeerId, remote_peer: DirectPeerId,
) -> Result<ConnectionBase, NetError>; ) -> Result<ConnectionBase, NetError>;
@ -51,7 +51,7 @@ pub trait IAccept: Send + Sync {
type Socket; type Socket;
async fn accept( async fn accept(
&self, &self,
peer_privk: PrivKey, peer_privk: Sensitive<[u8; 32]>,
peer_pubk: PubKey, peer_pubk: PubKey,
socket: Self::Socket, socket: Self::Socket,
) -> Result<ConnectionBase, NetError>; ) -> Result<ConnectionBase, NetError>;
@ -95,7 +95,7 @@ pub struct NoiseFSM {
noise_cipher_state_enc: Option<CipherState<ChaCha20Poly1305>>, noise_cipher_state_enc: Option<CipherState<ChaCha20Poly1305>>,
noise_cipher_state_dec: Option<CipherState<ChaCha20Poly1305>>, noise_cipher_state_dec: Option<CipherState<ChaCha20Poly1305>>,
from: PrivKey, from: Option<Sensitive<[u8; 32]>>,
to: Option<PubKey>, to: Option<PubKey>,
} }
@ -120,7 +120,7 @@ impl NoiseFSM {
dir: ConnectionDir, dir: ConnectionDir,
actors: Arc<Mutex<HashMap<i64, Sender<ConnectionCommand>>>>, actors: Arc<Mutex<HashMap<i64, Sender<ConnectionCommand>>>>,
sender: Sender<ConnectionCommand>, sender: Sender<ConnectionCommand>,
from: PrivKey, from: Sensitive<[u8; 32]>,
to: Option<PubKey>, to: Option<PubKey>,
) -> Self { ) -> Self {
Self { Self {
@ -135,7 +135,7 @@ impl NoiseFSM {
noise_handshake_state: None, noise_handshake_state: None,
noise_cipher_state_enc: None, noise_cipher_state_enc: None,
noise_cipher_state_dec: None, noise_cipher_state_dec: None,
from, from: Some(from),
to, to,
} }
} }
@ -232,7 +232,7 @@ impl NoiseFSM {
noise_xk(), noise_xk(),
true, true,
&[], &[],
Some(Sensitive::from_slice(self.from.slice())), Some(self.from.take().unwrap()),
None, None,
Some(*self.to.unwrap().slice()), Some(*self.to.unwrap().slice()),
None, None,
@ -260,18 +260,23 @@ impl NoiseFSM {
noise_xk(), noise_xk(),
false, false,
&[], &[],
Some(Sensitive::from_slice(self.from.slice())), Some(self.from.take().unwrap()),
None, None,
None, None,
None, None,
); );
let payload = let mut payload =
handshake.read_message_vec(noise.data()).map_err(|e| { handshake.read_message_vec(noise.data()).map_err(|e| {
debug_println!("{:?}", e); debug_println!("{:?}", e);
ProtocolError::NoiseHandshakeFailed ProtocolError::NoiseHandshakeFailed
})?; })?;
payload = handshake.write_message_vec(&payload).map_err(|e| {
debug_println!("{:?}", e);
ProtocolError::NoiseHandshakeFailed
})?;
let noise = Noise::V0(NoiseV0 { data: payload }); let noise = Noise::V0(NoiseV0 { data: payload });
self.sender.send(ConnectionCommand::Msg(noise.into())).await; self.sender.send(ConnectionCommand::Msg(noise.into())).await;
@ -291,10 +296,15 @@ impl NoiseFSM {
if let ProtocolMessage::Noise(noise) = msg { if let ProtocolMessage::Noise(noise) = msg {
let handshake = self.noise_handshake_state.as_mut().unwrap(); let handshake = self.noise_handshake_state.as_mut().unwrap();
let payload = handshake let mut payload = handshake
.read_message_vec(noise.data()) .read_message_vec(noise.data())
.map_err(|e| ProtocolError::NoiseHandshakeFailed)?; .map_err(|e| ProtocolError::NoiseHandshakeFailed)?;
payload = handshake.write_message_vec(&payload).map_err(|e| {
debug_println!("{:?}", e);
ProtocolError::NoiseHandshakeFailed
})?;
if !handshake.completed() { if !handshake.completed() {
return Err(ProtocolError::NoiseHandshakeFailed); return Err(ProtocolError::NoiseHandshakeFailed);
} }
@ -586,7 +596,7 @@ impl ConnectionBase {
} }
} }
pub fn start_read_loop(&mut self, from: PrivKey, to: Option<PubKey>) { pub fn start_read_loop(&mut self, from: Sensitive<[u8; 32]>, to: Option<PubKey>) {
let (sender_tx, sender_rx) = mpsc::unbounded(); let (sender_tx, sender_rx) = mpsc::unbounded();
let (receiver_tx, receiver_rx) = mpsc::unbounded(); let (receiver_tx, receiver_rx) = mpsc::unbounded();
self.sender = Some(sender_rx); self.sender = Some(sender_rx);

@ -1,6 +1,9 @@
use crate::log; use crate::log;
use async_std::task; use async_std::task;
use futures::{channel::mpsc, select, Future, FutureExt, SinkExt}; use futures::{channel::mpsc, select, Future, FutureExt, SinkExt};
pub use noise_protocol::U8Array;
use noise_protocol::DH;
pub use noise_rust_crypto::sensitive::Sensitive;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
pub fn spawn_and_log_error<F>(fut: F) -> task::JoinHandle<()> pub fn spawn_and_log_error<F>(fut: F) -> task::JoinHandle<()>
@ -33,3 +36,9 @@ where
pub type Sender<T> = mpsc::UnboundedSender<T>; pub type Sender<T> = mpsc::UnboundedSender<T>;
pub type Receiver<T> = mpsc::UnboundedReceiver<T>; pub type Receiver<T> = mpsc::UnboundedReceiver<T>;
pub fn gen_keys() -> (Sensitive<[u8; 32]>, [u8; 32]) {
let pri = noise_rust_crypto::X25519::genkey();
let publ = noise_rust_crypto::X25519::pubkey(&pri);
(pri, publ)
}

Loading…
Cancel
Save