Skip to content

Commit

Permalink
Cut off noise crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp committed Nov 9, 2021
1 parent 9425cc5 commit 25ee1fe
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 199 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ ping = ["libp2p-ping", "libp2p-metrics/ping"]
plaintext = ["libp2p-plaintext"]
pnet = ["libp2p-pnet"]
quic = ["libp2p-quic"]
quic-noise = ["libp2p-quic", "libp2p-quic/noise"]
quic-tls = ["libp2p-quic", "libp2p-quic/tls"]
relay = ["libp2p-relay"]
request-response = ["libp2p-request-response"]
rendezvous = ["libp2p-rendezvous"]
Expand Down
24 changes: 11 additions & 13 deletions transports/quic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,34 @@ name = "libp2p-quic"
version = "0.6.0"
authors = ["David Craven <david@craven.ch>", "Parity Technologies <admin@parity.io>"]
edition = "2018"
description = "TLS and Noise based QUIC transport implementation for libp2p"
description = "TLS based QUIC transport implementation for libp2p"
repository = "https://github.com/libp2p/rust-libp2p"
license = "MIT"

[features]
noise = ["quinn-noise", "ed25519-dalek"]
tls = ["barebones-x509", "quinn-proto/tls-rustls", "rcgen", "ring", "rustls", "untrusted", "webpki", "yasna"]
tls = []
default = ["tls"]

[dependencies]
async-global-executor = "2.0.2"
async-io = "1.6.0"
barebones-x509 = { version = "0.5.0", optional = true, features = ["webpki", "rustls", "std"] }
barebones-x509 = { version = "0.5.0", features = ["webpki", "rustls", "std"] }
bytes = "1.0.1"
ed25519-dalek = { version = "1.0.1", optional = true }
futures = "0.3.15"
if-watch = "0.2.2"
libp2p-core = { version = "0.30.0-rc.1", path = "../../core" }
multihash = { version = "0.14.0", default-features = false }
parking_lot = "0.11.1"
quinn-noise = { version = "0.3.0", optional = true }
quinn-proto = { version = "0.7.3", default-features = false }
rcgen = { version = "0.8.11", optional = true }
ring = { version = "0.16.20", optional = true }
rustls = { version = "0.19.1", optional = true, features = ["dangerous_configuration"] }
quinn-proto = { version = "0.7.3", default-features = false, features = ["tls-rustls"] }
rcgen = { version = "0.8.11" }
ring = { version = "0.16.20" }
rustls = { version = "0.19.1", features = ["dangerous_configuration"] }
thiserror = "1.0.26"
tracing = "0.1.26"
udp-socket = "0.1.5"
untrusted = { version = "0.7.1", optional = true }
webpki = { version = "0.21.4", optional = true, features = ["std"] }
yasna = { version = "0.4.0", optional = true }
untrusted = { version = "0.7.1" }
webpki = { version = "0.21.4", features = ["std"] }
yasna = { version = "0.4.0" }

[dev-dependencies]
anyhow = "1.0.41"
Expand Down
105 changes: 4 additions & 101 deletions transports/quic/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,17 @@ use std::sync::Arc;

pub struct CryptoConfig<C: Crypto> {
pub keypair: C::Keypair,
pub psk: Option<[u8; 32]>,
pub keylogger: Option<C::Keylogger>,
pub transport: Arc<TransportConfig>,
}

#[cfg(feature = "noise")]
trait CloneKeypair {
fn clone_keypair(&self) -> Self;
}

#[cfg(feature = "noise")]
impl CloneKeypair for ed25519_dalek::Keypair {
fn clone_keypair(&self) -> Self {
ed25519_dalek::Keypair::from_bytes(&self.to_bytes()).expect("serde works")
}
}

pub trait ToLibp2p {
fn to_public(&self) -> libp2p_core::identity::PublicKey;
fn to_peer_id(&self) -> PeerId {
self.to_public().to_peer_id()
}
}

#[cfg(feature = "noise")]
impl ToLibp2p for ed25519_dalek::Keypair {
fn to_public(&self) -> libp2p_core::identity::PublicKey {
self.public.to_public()
}
}

#[cfg(feature = "noise")]
impl ToLibp2p for ed25519_dalek::PublicKey {
fn to_public(&self) -> libp2p_core::identity::PublicKey {
let public_key = self.to_bytes();
let public_key =
libp2p_core::identity::ed25519::PublicKey::decode(&public_key[..]).unwrap();
libp2p_core::identity::PublicKey::Ed25519(public_key)
}
}

#[cfg(feature = "tls")]
impl ToLibp2p for libp2p_core::identity::Keypair {
fn to_public(&self) -> libp2p_core::identity::PublicKey {
Expand All @@ -80,10 +50,10 @@ pub trait Crypto: std::fmt::Debug + Clone + 'static {
type PublicKey: Send + std::fmt::Debug + PartialEq<Self::PublicKey>;

fn new_server_config(
config: &Arc<CryptoConfig<Self>>,
config: &CryptoConfig<Self>,
) -> <Self::Session as Session>::ServerConfig;
fn new_client_config(
config: &Arc<CryptoConfig<Self>>,
config: &CryptoConfig<Self>,
remote_public: Self::PublicKey,
) -> <Self::Session as Session>::ClientConfig;
fn supported_quic_versions() -> Vec<u32>;
Expand All @@ -93,71 +63,6 @@ pub trait Crypto: std::fmt::Debug + Clone + 'static {
fn keylogger() -> Self::Keylogger;
}

#[cfg(feature = "noise")]
#[derive(Clone, Copy, Debug)]
pub struct NoiseCrypto;

#[cfg(feature = "noise")]
impl Crypto for NoiseCrypto {
type Session = quinn_noise::NoiseSession;
type Keylogger = Arc<dyn quinn_noise::KeyLog>;
type Keypair = ed25519_dalek::Keypair;
type PublicKey = ed25519_dalek::PublicKey;

fn new_server_config(
config: &Arc<CryptoConfig<Self>>,
) -> <Self::Session as Session>::ServerConfig {
Arc::new(
quinn_noise::NoiseServerConfig {
keypair: config.keypair.clone_keypair(),
psk: config.psk,
keylogger: config.keylogger.clone(),
supported_protocols: vec![b"libp2p".to_vec()],
}
.into(),
)
}

fn new_client_config(
config: &Arc<CryptoConfig<Self>>,
remote_public_key: Self::PublicKey,
) -> <Self::Session as Session>::ClientConfig {
quinn_noise::NoiseClientConfig {
keypair: config.keypair.clone_keypair(),
psk: config.psk,
alpn: b"libp2p".to_vec(),
remote_public_key,
keylogger: config.keylogger.clone(),
}
.into()
}

fn supported_quic_versions() -> Vec<u32> {
quinn_noise::SUPPORTED_QUIC_VERSIONS.to_vec()
}

fn default_quic_version() -> u32 {
quinn_noise::DEFAULT_QUIC_VERSION
}

fn peer_id(session: &Self::Session) -> Option<PeerId> {
Some(session.peer_identity()?.to_peer_id())
}

fn extract_public_key(generic_key: libp2p_core::PublicKey) -> Option<Self::PublicKey> {
let public_key = if let libp2p_core::PublicKey::Ed25519(public_key) = generic_key {
public_key.encode()
} else {
return None;
};
Self::PublicKey::from_bytes(&public_key).ok()
}

fn keylogger() -> Self::Keylogger {
Arc::new(quinn_noise::KeyLogFile::new())
}
}

#[cfg(feature = "tls")]
#[derive(Clone, Copy, Debug)]
pub struct TlsCrypto;
Expand All @@ -170,9 +75,8 @@ impl Crypto for TlsCrypto {
type PublicKey = libp2p_core::identity::PublicKey;

fn new_server_config(
config: &Arc<CryptoConfig<Self>>,
config: &CryptoConfig<Self>,
) -> <Self::Session as Session>::ServerConfig {
assert!(config.psk.is_none(), "invalid config");
let mut server = crate::tls::make_server_config(&config.keypair).expect("invalid config");
if let Some(key_log) = config.keylogger.clone() {
server.key_log = key_log;
Expand All @@ -181,10 +85,9 @@ impl Crypto for TlsCrypto {
}

fn new_client_config(
config: &Arc<CryptoConfig<Self>>,
config: &CryptoConfig<Self>,
remote_public: Self::PublicKey,
) -> <Self::Session as Session>::ClientConfig {
assert!(config.psk.is_none(), "invalid config");
let mut client =
crate::tls::make_client_config(&config.keypair, remote_public.to_peer_id())
.expect("invalid config");
Expand Down
1 change: 0 additions & 1 deletion transports/quic/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ impl<C: Crypto> EndpointConfig<C> {

let crypto_config = Arc::new(CryptoConfig {
keypair: config.keypair,
psk: config.psk,
keylogger: config.keylogger,
transport: transport.clone(),
});
Expand Down
7 changes: 0 additions & 7 deletions transports/quic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ mod tls;
mod transport;

pub use crate::crypto::Crypto;
#[cfg(feature = "noise")]
pub use crate::crypto::NoiseCrypto;
#[cfg(feature = "tls")]
pub use crate::crypto::TlsCrypto;
pub use crate::crypto::ToLibp2p;
pub use crate::muxer::{QuicMuxer, QuicMuxerError};
pub use crate::transport::{QuicDial, QuicTransport};
#[cfg(feature = "noise")]
pub use quinn_noise::{KeyLog, KeyLogFile};
pub use quinn_proto::{ConfigError, ConnectError, ConnectionError, TransportConfig};

use libp2p_core::transport::TransportError;
Expand All @@ -45,7 +41,6 @@ use thiserror::Error;
/// Quic configuration.
pub struct QuicConfig<C: Crypto> {
pub keypair: C::Keypair,
pub psk: Option<[u8; 32]>,
pub transport: TransportConfig,
pub keylogger: Option<C::Keylogger>,
}
Expand All @@ -54,7 +49,6 @@ impl<C: Crypto> std::fmt::Debug for QuicConfig<C> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("QuicConfig")
.field("keypair", &self.keypair.to_public())
.field("psk", &self.psk)
.field("transport", &self.transport)
.finish()
}
Expand All @@ -70,7 +64,6 @@ where
pub fn new(keypair: C::Keypair) -> Self {
Self {
keypair,
psk: None,
transport: TransportConfig::default(),
keylogger: None,
}
Expand Down
11 changes: 0 additions & 11 deletions transports/quic/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,6 @@ mod tests {
);
}

#[cfg(feature = "noise")]
#[test]
fn multiaddr_to_udp_noise() {
multiaddr_to_udp_conversion::<crate::NoiseCrypto>();
}
#[cfg(feature = "tls")]
#[test]
fn multiaddr_to_udp_tls() {
Expand Down Expand Up @@ -396,10 +391,4 @@ mod tests {
let keypair = libp2p_core::identity::Keypair::generate_ed25519();
multiaddr_to_pk_conversion::<crate::TlsCrypto>(keypair);
}
#[cfg(feature = "noise")]
#[test]
fn multiaddr_to_pk_noise() {
let keypair = ed25519_dalek::Keypair::generate(&mut rand_core::OsRng {});
multiaddr_to_pk_conversion::<crate::NoiseCrypto>(keypair);
}
}
64 changes: 0 additions & 64 deletions transports/quic/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,11 @@ use quinn_proto::crypto::Session;
use rand::RngCore;
use std::{io, iter};

#[cfg(feature = "noise")]
fn generate_noise_keypair() -> ed25519_dalek::Keypair {
ed25519_dalek::Keypair::generate(&mut rand_core::OsRng {})
}
#[cfg(feature = "tls")]
fn generate_tls_keypair() -> libp2p::identity::Keypair {
libp2p::identity::Keypair::generate_ed25519()
}

#[cfg(feature = "noise")]
#[async_std::test]
async fn smoke_noise() -> Result<()> {
smoke::<libp2p_quic::NoiseCrypto>().await
}

#[cfg(feature = "tls")]
#[async_std::test]
async fn smoke_tls() -> Result<()> {
Expand All @@ -40,13 +30,6 @@ trait GenerateKeypair: Crypto {
fn generate_keypair() -> Self::Keypair;
}

#[cfg(feature = "noise")]
impl GenerateKeypair for libp2p_quic::NoiseCrypto {
fn generate_keypair() -> Self::Keypair {
generate_noise_keypair()
}
}

#[cfg(feature = "tls")]
impl GenerateKeypair for libp2p_quic::TlsCrypto {
fn generate_keypair() -> Self::Keypair {
Expand Down Expand Up @@ -291,53 +274,6 @@ impl RequestResponseCodec for PingCodec {
}
}

#[cfg(feature = "noise")]
#[async_std::test]
async fn dial_failure_noise() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.try_init()
.ok();
log_panics::init();

let mut a = create_swarm::<libp2p_quic::NoiseCrypto>(true).await?;
let mut b = create_swarm::<libp2p_quic::NoiseCrypto>(false).await?;

Swarm::listen_on(&mut a, "/ip4/127.0.0.1/udp/0/quic".parse()?)?;

let keypair = libp2p_quic::NoiseCrypto::generate_keypair();
let fake_peer_id = keypair.to_peer_id();

let mut addr = match a.next().await {
Some(SwarmEvent::NewListenAddr { address, .. }) => address,
e => panic!("{:?}", e),
};
addr.push(Protocol::P2p(fake_peer_id.into()));

b.behaviour_mut().add_address(&fake_peer_id, addr);
b.behaviour_mut()
.send_request(&fake_peer_id, Ping(b"hello world".to_vec()));

match b.next().await {
Some(SwarmEvent::Dialing(_)) => {}
e => panic!("{:?}", e),
}

match b.next().await {
Some(SwarmEvent::ConnectionEstablished { .. }) => {}
e => panic!("{:?}", e),
};

match b.next().await {
Some(SwarmEvent::ConnectionClosed { .. }) => {}
e => panic!("{:?}", e),
};

assert!(a.next().now_or_never().is_none());

Ok(())
}

#[cfg(feature = "tls")]
#[async_std::test]
async fn dial_failure_tls() -> Result<()> {
Expand Down

0 comments on commit 25ee1fe

Please sign in to comment.