Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Libp2p quic second attempt #2159

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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",
"rendezvous",
Expand All @@ -37,16 +38,17 @@ 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", "libp2p-metrics/identify"]
kad = ["libp2p-kad", "libp2p-metrics/kad"]
gossipsub = ["libp2p-gossipsub"]
metrics = ["libp2p-metrics"]
mdns = ["libp2p-mdns"]
mplex = ["libp2p-mplex"]
noise = ["libp2p-noise"]
ping = ["libp2p-ping", "libp2p-metrics/ping"]
plaintext = ["libp2p-plaintext"]
pnet = ["libp2p-pnet"]
quic = ["libp2p-quic"]
relay = ["libp2p-relay"]
request-response = ["libp2p-request-response"]
rendezvous = ["libp2p-rendezvous"]
Expand Down Expand Up @@ -97,6 +99,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 @@ -132,6 +135,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 @@ -100,6 +100,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
45 changes: 45 additions & 0 deletions transports/quic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "libp2p-quic"
version = "0.6.0"
authors = ["David Craven <[email protected]>", "Parity Technologies <[email protected]>"]
edition = "2018"
description = "TLS and Noise 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"]

[dependencies]
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 = { version = "1.0.1", optional = true }
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 }
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"
dvc94ch marked this conversation as resolved.
Show resolved Hide resolved
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]
anyhow = "1.0.41"
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"
rand_core = "0.5.1"
tracing-subscriber = "0.2.19"
199 changes: 199 additions & 0 deletions transports/quic/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
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<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 {
self.public()
}
}

pub trait Crypto: std::fmt::Debug + Clone + 'static {
type Session: Session + Unpin;
type Keylogger: Send + Sync;
type Keypair: Send + Sync + ToLibp2p;
type PublicKey: Send + std::fmt::Debug + PartialEq<Self::PublicKey>;

fn new_server_config(
config: &Arc<CryptoConfig<Self>>,
) -> <Self::Session as Session>::ServerConfig;
fn new_client_config(
config: &Arc<CryptoConfig<Self>>,
remote_public: Self::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 extract_public_key(generic_key: libp2p_core::PublicKey) -> Option<Self::PublicKey>;
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;

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

fn new_server_config(
config: &Arc<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;
}
Arc::new(server)
}

fn new_client_config(
config: &Arc<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");
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 extract_public_key(generic_key: libp2p_core::PublicKey) -> Option<Self::PublicKey> {
Some(generic_key)
}

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