Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ default = [
"deflate",
"dns-async-std",
"floodsub",
"gossipsub",
"identify",
"kad",
"gossipsub",
"mdns",
"mplex",
"noise",
"ping",
"plaintext",
"pnet",
"quic",
"relay",
"request-response",
"secp256k1",
Expand All @@ -36,15 +37,16 @@ deflate = ["libp2p-deflate"]
dns-async-std = ["libp2p-dns", "libp2p-dns/async-std"]
dns-tokio = ["libp2p-dns", "libp2p-dns/tokio"]
floodsub = ["libp2p-floodsub"]
gossipsub = ["libp2p-gossipsub"]
identify = ["libp2p-identify"]
kad = ["libp2p-kad"]
gossipsub = ["libp2p-gossipsub"]
mdns = ["libp2p-mdns"]
mplex = ["libp2p-mplex"]
noise = ["libp2p-noise"]
ping = ["libp2p-ping"]
plaintext = ["libp2p-plaintext"]
pnet = ["libp2p-pnet"]
quic = ["libp2p-quic"]
relay = ["libp2p-relay"]
request-response = ["libp2p-request-response"]
tcp-async-io = ["libp2p-tcp", "libp2p-tcp/async-io"]
Expand Down Expand Up @@ -92,6 +94,7 @@ wasm-timer = "0.2.4"
libp2p-deflate = { version = "0.30.0", path = "transports/deflate", optional = true }
libp2p-dns = { version = "0.30.0", path = "transports/dns", optional = true, default-features = false }
libp2p-mdns = { version = "0.32.0", path = "protocols/mdns", optional = true }
libp2p-quic = { version = "0.6.0", path = "transports/quic", optional = true }
libp2p-tcp = { version = "0.30.0", path = "transports/tcp", default-features = false, optional = true }
libp2p-websocket = { version = "0.31.0", path = "transports/websocket", optional = true }

Expand Down Expand Up @@ -123,6 +126,7 @@ members = [
"transports/noise",
"transports/plaintext",
"transports/pnet",
"transports/quic",
"transports/tcp",
"transports/uds",
"transports/websocket",
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ pub use libp2p_plaintext as plaintext;
#[cfg_attr(docsrs, doc(cfg(feature = "pnet")))]
#[doc(inline)]
pub use libp2p_pnet as pnet;
#[cfg(feature = "quic")]
#[cfg_attr(docsrs, doc(cfg(feature = "quic")))]
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
#[doc(inline)]
pub use libp2p_quic as quic;
#[cfg(feature = "relay")]
#[cfg_attr(docsrs, doc(cfg(feature = "relay")))]
#[doc(inline)]
Expand Down
46 changes: 46 additions & 0 deletions transports/quic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = "libp2p-quic"
version = "0.6.0"
authors = ["David Craven <[email protected]>", "Parity Technologies <[email protected]>"]
edition = "2018"
description = "libp2p-quic is a noise based quic implementation for rust-libp2p."
repository = "https://github.com/ipfs-rust/libp2p-quic"
license = "ISC"

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

[dependencies]
anyhow = "1.0.41"
async-global-executor = "2.0.2"
async-io = "1.6.0"
barebones-x509 = { version = "0.5.0", optional = true, features = ["webpki", "rustls", "std"] }
bytes = "1.0.1"
ed25519-dalek = "1.0.1"
fnv = "1.0.7"
futures = "0.3.15"
if-watch = "0.2.2"
libp2p-core = { version = "0.30.0", 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 }
rand_core = "0.5.1"
rcgen = { version = "0.8.11", optional = true }
ring = { version = "0.16.20", optional = true }
rustls = { version = "0.19.1", optional = true, 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 }

[dev-dependencies]
async-std = { version = "1.9.0", features = ["attributes"] }
async-trait = "0.1.50"
libp2p = { version = "0.40.0", default-features = false, features = ["request-response"], path = "../.." }
log-panics = "2.0.0"
rand = "0.8.4"
tracing-subscriber = "0.2.19"
150 changes: 150 additions & 0 deletions transports/quic/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use ed25519_dalek::{Keypair, PublicKey};
use libp2p_core::PeerId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of consistency, would you mind including license headers here?

use quinn_proto::crypto::Session;
use quinn_proto::TransportConfig;
use std::sync::Arc;

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

#[cfg(feature = "noise")]
impl<K> CryptoConfig<K> {
fn clone_keypair(&self) -> Keypair {
Keypair::from_bytes(&self.keypair.to_bytes()).expect("serde works")
}
}

pub trait Crypto: std::fmt::Debug + Clone + 'static {
type Session: Session + Unpin;
type Keylogger: Send + Sync;

fn new_server_config(
config: &Arc<CryptoConfig<Self::Keylogger>>,
) -> <Self::Session as Session>::ServerConfig;
fn new_client_config(
config: &Arc<CryptoConfig<Self::Keylogger>>,
remote_public: PublicKey,
) -> <Self::Session as Session>::ClientConfig;
fn supported_quic_versions() -> Vec<u32>;
fn default_quic_version() -> u32;
fn peer_id(session: &Self::Session) -> Option<PeerId>;
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>;

fn new_server_config(
config: &Arc<CryptoConfig<Self::Keylogger>>,
) -> <Self::Session as Session>::ServerConfig {
Arc::new(
quinn_noise::NoiseServerConfig {
keypair: config.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::Keylogger>>,
remote_public_key: PublicKey,
) -> <Self::Session as Session>::ClientConfig {
quinn_noise::NoiseClientConfig {
keypair: config.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> {
use crate::ToLibp2p;
Some(session.peer_identity()?.to_peer_id())
}

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

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

#[cfg(feature = "tls")]
impl Crypto for TlsCrypto {
type Session = quinn_proto::crypto::rustls::TlsSession;
type Keylogger = Arc<dyn rustls::KeyLog>;

fn new_server_config(
config: &Arc<CryptoConfig<Self::Keylogger>>,
) -> <Self::Session as Session>::ServerConfig {
assert!(config.psk.is_none(), "invalid config");
use crate::ToLibp2p;
let mut server =
crate::tls::make_server_config(&config.keypair.to_keypair()).expect("invalid config");
if let Some(key_log) = config.keylogger.clone() {
server.key_log = key_log;
}
Arc::new(server)
}

fn new_client_config(
config: &Arc<CryptoConfig<Self::Keylogger>>,
remote_public: PublicKey,
) -> <Self::Session as Session>::ClientConfig {
assert!(config.psk.is_none(), "invalid config");
use crate::ToLibp2p;
let mut client = crate::tls::make_client_config(
&config.keypair.to_keypair(),
remote_public.to_peer_id(),
)
.expect("invalid config");
if let Some(key_log) = config.keylogger.clone() {
client.key_log = key_log;
}
Arc::new(client)
}

fn supported_quic_versions() -> Vec<u32> {
quinn_proto::DEFAULT_SUPPORTED_VERSIONS.to_vec()
}

fn default_quic_version() -> u32 {
quinn_proto::DEFAULT_SUPPORTED_VERSIONS[0]
}

fn peer_id(session: &Self::Session) -> Option<PeerId> {
let certificate = session.get_peer_certificates()?.into_iter().next()?;
Some(crate::tls::extract_peerid_or_panic(
quinn_proto::Certificate::from(certificate).as_der(),
))
}

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