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

feat: add WebTransport protocol #4874

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1c2c377
attempt to extend QUIC transport impl
dgarus Nov 16, 2023
8bf8d0c
example
dgarus Nov 16, 2023
cabc7de
example
dgarus Nov 16, 2023
83058d5
Merge branch 'master' into web-transport-quic
dgarus Nov 17, 2023
fdcd176
Merge branch 'master' into web-transport-quic
dgarus Nov 21, 2023
8325840
Merge branch 'master' into web-transport-quic
dgarus Nov 22, 2023
7a54fd3
Merge branch 'master' into web-transport-quic
dgarus Nov 23, 2023
dc80864
Merge branch 'master' into web-transport-quic
dgarus Dec 12, 2023
13cc3df
added CertManager
dgarus Dec 22, 2023
5ceccfd
Merge branch 'master' into web-transport-quic
dgarus Dec 22, 2023
cd9d646
Merge branch 'master' into web-transport-quic
dgarus Dec 25, 2023
10d97fe
noise
dgarus Dec 25, 2023
d418dc1
single endpoint for quic and wt
dgarus Dec 25, 2023
cefaebe
Merge remote-tracking branch 'origin/web-transport-quic' into web-tra…
dgarus Dec 25, 2023
94fd970
Merge branch 'master' into web-transport-quic
dgarus Dec 25, 2023
c1dca8c
Merge branch 'master' into web-transport-quic
dgarus Dec 29, 2023
d01f149
Merge branch 'master' into web-transport-quic
dgarus Jan 9, 2024
1f2297c
Merge branch 'master' into web-transport-quic
dgarus Jan 18, 2024
dc08909
msg
dgarus Jan 25, 2024
a3e3a5e
Merge remote-tracking branch 'origin/web-transport-quic' into web-tra…
dgarus Jan 25, 2024
2b06072
Merge branch 'master' into web-transport-quic
dgarus Jan 25, 2024
c3a8e7d
Merge branch 'master' into web-transport-quic
dgarus Feb 12, 2024
f024ded
Merge branch 'master' into web-transport-quic
dgarus Apr 16, 2024
1cdecf1
Merge branch 'master' into web-transport-quic
dgarus May 2, 2024
9b8b27e
Merge branch 'master' into web-transport-quic
dgarus May 7, 2024
c5ff302
Merge branch 'refs/heads/master' into web-transport-quic
dgarus Jul 9, 2024
098b58b
Merge branch 'master' into web-transport-quic
dgarus Jul 17, 2024
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
13 changes: 13 additions & 0 deletions transports/quic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ tracing = { workspace = true }
socket2 = "0.5.7"
ring = { workspace = true }

sha2 = "0.10"
hex = "0.4"
hex-literal = "0.4"

# WebTransport
http = "1.0.0"
h3 = { git = "https://github.com/hyperium/h3" }
h3-quinn = { git = "https://github.com/hyperium/h3" }
h3-webtransport = { git = "https://github.com/hyperium/h3" }
Comment on lines +36 to +38
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there no released versions of these libraries yet? We won't be able to merge this without released versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With git versions, everything is ok:

image

But when I define h3 = "0.0.4" I got two versions 0.0.3 and 0.0.4:

image

And, accordingly, I got a lot of errors.
I stuck with this problem for the first time, do you know how to fix it?

Copy link
Contributor

Choose a reason for hiding this comment

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

It is because there hasn't yet been a release of h3-webtransport with h3 0.0.4. See https://crates.io/crates/h3-webtransport/0.1.0/dependencies. I'd suggest nudging the maintainers for a new release!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OMG
Thanks, I'll do it a bit later

libp2p-noise = { workspace = true }
rcgen = "0.11.3"
time = "0.3"

[features]
tokio = ["dep:tokio", "if-watch/tokio", "quinn/runtime-tokio"]
async-std = ["dep:async-std", "if-watch/smol", "quinn/runtime-async-std"]
Expand Down
156 changes: 156 additions & 0 deletions transports/quic/src/certificate_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use std::sync::Arc;

use rustls;
use sha2::Digest;
use time::{Duration, OffsetDateTime};

Check failure on line 25 in transports/quic/src/certificate_manager.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/transports/quic/src/certificate_manager.rs

use libp2p_core::multihash::Multihash;
use libp2p_tls::{
certificate, P2P_ALPN, verifier,
};

const MULTIHASH_SHA256_CODE: u64 = 0x12;
const CERT_VALID_PERIOD: Duration = Duration::days(14);

#[derive(Clone, Debug)]
pub(crate) struct ServerCertManager {
keypair: libp2p_identity::Keypair,

items: Vec<CertItem>,
}

#[derive(Clone, Debug)]
struct CertItem {
server_tls_config: Arc<rustls::ServerConfig>,
start: OffsetDateTime,
end: OffsetDateTime,
cert_hash: Multihash<64>,
}

impl ServerCertManager {
pub(crate) fn new(keypair: libp2p_identity::Keypair) -> Self {
Self {
keypair,
items: Vec::with_capacity(3),
}
}

Check failure on line 56 in transports/quic/src/certificate_manager.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/transports/quic/src/certificate_manager.rs

/// Gets TLS server config and certificate hashes.
pub(crate) fn get_config(&mut self) -> Result<(Arc<rustls::ServerConfig>, Vec<Multihash<64>>), certificate::GenError> {
self.check_and_roll_items()?;

let cur_item = self.items.first()
.expect("The first element exists");
let cert_hashes = self.items.iter()
.map(|item| item.cert_hash.clone())
.collect();

Ok((cur_item.server_tls_config.clone(), cert_hashes))
}

Check failure on line 69 in transports/quic/src/certificate_manager.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/transports/quic/src/certificate_manager.rs

fn create_cert_item(&self, start: OffsetDateTime) -> Result<CertItem, certificate::GenError> {
let not_after = start.clone()
.checked_add(CERT_VALID_PERIOD)
.expect("Addition does not overflow");

let (cert, private_key) =
certificate::generate_webtransport_certificate(&self.keypair, start, not_after)?;

Check failure on line 77 in transports/quic/src/certificate_manager.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/transports/quic/src/certificate_manager.rs

let cert_hash = Multihash::wrap(
MULTIHASH_SHA256_CODE, sha2::Sha256::digest(cert.as_ref().as_ref()).as_ref(),
).expect("fingerprint's len to be 32 bytes");

let mut tls_config = rustls::ServerConfig::builder()
.with_cipher_suites(verifier::CIPHERSUITES)
.with_safe_default_kx_groups()
.with_protocol_versions(verifier::PROTOCOL_VERSIONS)
.expect("Cipher suites and kx groups are configured; qed")
.with_client_cert_verifier(Arc::new(verifier::Libp2pCertificateVerifier::new()))
.with_single_cert(vec![cert], private_key)
.expect("Server cert key DER is valid; qed");

Check failure on line 91 in transports/quic/src/certificate_manager.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/transports/quic/src/certificate_manager.rs
tls_config.alpn_protocols = alpn_protocols();

Ok(CertItem { server_tls_config: Arc::new(tls_config), start, end: not_after, cert_hash })
}

/// https://github.com/libp2p/specs/tree/master/webtransport#certificates
/// Servers need to take care of regularly renewing their certificates.At first boot of the node,
/// it creates one self-signed certificate with a validity of 14 days, starting immediately,
/// and another certificate with the 14 day validity period starting on the expiry date of the first certificate.
/// The node advertises a multiaddress containing the certificate hashes of these two certificates.
/// Once the first certificate has expired, the node starts using the already generated next certificate.
/// At the same time, it again generates a new certificate for the following period and updates the multiaddress it advertises.
fn check_and_roll_items(&mut self) -> Result<(), certificate::GenError> {
if self.items.len() == 0 {
let current = self.create_cert_item(OffsetDateTime::now_utc())?;
let next_start = current.end.clone();
self.items.push(current);

Check failure on line 108 in transports/quic/src/certificate_manager.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/transports/quic/src/certificate_manager.rs
self.items.push(self.create_cert_item(next_start)?);
} else {
let next = self.items.get(1)
.expect("Element with index 1 exists");

if OffsetDateTime::now_utc() >= next.start {
let next_start = next.end.clone();
self.items.push(self.create_cert_item(next_start)?);
if self.items.len() == 3 {
self.items.remove(0);
}
}
};

Ok(())
}
}

Check failure on line 125 in transports/quic/src/certificate_manager.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/transports/quic/src/certificate_manager.rs

fn alpn_protocols() -> Vec<Vec<u8>> {
vec![P2P_ALPN.to_vec(),
b"h3".to_vec(),
b"h3-32".to_vec(),
b"h3-31".to_vec(),
b"h3-30".to_vec(),
b"h3-29".to_vec(), ]
}

/*
#[cfg(test)]
mod tests {
use std::fmt::{Debug, Formatter};
use rcgen::SerialNumber;
use ring::rand::{SecureRandom, SystemRandom};
use ring::{hkdf, signature};
use ring::error::Unspecified;
use ring::signature::EcdsaKeyPair;
use time::macros::datetime;

#[test]
fn key_pair_generate() {
let alg = &signature::ECDSA_P256_SHA256_ASN1_SIGNING;
let rnd = SystemRandom::new();

let doc = EcdsaKeyPair::generate_pkcs8(alg, &rnd);

assert!(doc.is_ok())

Check failure on line 154 in transports/quic/src/certificate_manager.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/transports/quic/src/certificate_manager.rs
}
}*/
5 changes: 3 additions & 2 deletions transports/quic/src/connection/connecting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@

use futures::{
future::{select, Either, FutureExt, Select},
prelude::*,

Check failure on line 27 in transports/quic/src/connection/connecting.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/transports/quic/src/connection/connecting.rs
};
use futures_timer::Delay;
use libp2p_identity::PeerId;
use quinn::rustls::pki_types::CertificateDer;
use std::{
pin::Pin,
task::{Context, Poll},

Check failure on line 34 in transports/quic/src/connection/connecting.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/transports/quic/src/connection/connecting.rs
time::Duration,
};
use libp2p_core::muxing::StreamMuxerBox;

/// A QUIC connection currently being negotiated.
#[derive(Debug)]
Expand Down Expand Up @@ -68,7 +69,7 @@
}

impl Future for Connecting {
type Output = Result<(PeerId, Connection), Error>;
type Output = Result<(PeerId, StreamMuxerBox), Error>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let connection = match futures::ready!(self.connecting.poll_unpin(cx)) {
Expand All @@ -78,6 +79,6 @@

let peer_id = Self::remote_peer_id(&connection);
let muxer = Connection::new(connection);
Poll::Ready(Ok((peer_id, muxer)))
Poll::Ready(Ok((peer_id, StreamMuxerBox::new(muxer))))
}
}
9 changes: 9 additions & 0 deletions transports/quic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,19 @@

#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

mod certificate_manager;
mod config;
mod connection;
mod hole_punching;
mod provider;
mod transport;
mod webtransport;

use std::net::SocketAddr;

pub use config::Config;
pub use connection::{Connecting, Connection, Stream};
use libp2p_tls::certificate;

#[cfg(feature = "async-std")]
pub use provider::async_std;
Expand Down Expand Up @@ -101,6 +104,12 @@ pub enum Error {
/// Error when holepunching for a remote is already in progress
#[error("Already punching hole for {0}).")]
HolePunchInProgress(SocketAddr),

#[error(transparent)]
CertificateGenerationError(#[from] certificate::GenError),

#[error("WebTransport error.")]
WebTransportError,
Comment on lines +108 to +112
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately, the enum is not non_exhaustive so this would be a breaking change. Could we (in a first version at least) not add these variants? Worst case we could "hide" them in a custom IO error using io::Error::new().

Copy link
Contributor

Choose a reason for hiding this comment

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

(I've not checked where these errors are used and whether we could avoid them otherwise).

}

/// Dialing a remote peer failed.
Expand Down
Loading
Loading