Skip to content

Commit ea0ab8b

Browse files
committed
feat(noise): Add WebTransport certhashes extension
1 parent 92c8cc4 commit ea0ab8b

File tree

10 files changed

+367
-12
lines changed

10 files changed

+367
-12
lines changed

Cargo.lock

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transports/noise/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55

66
- Remove deprecated APIs. See [PR 3511].
77

8+
- Add `Config::with_webtransport_certhashes`. See [PR 3991].
9+
This can be used by WebTransport implementers to send (responder) or verify (initiator) certhashes.
10+
811
[PR 3511]: https://github.com/libp2p/rust-libp2p/pull/3511
912
[PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715
13+
[PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3991
1014

1115
## 0.42.2
1216

transports/noise/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ futures = "0.3.28"
1515
libp2p-core = { workspace = true }
1616
libp2p-identity = { workspace = true, features = ["ed25519"] }
1717
log = "0.4"
18-
quick-protobuf = "0.8"
18+
multiaddr = "0.17.1"
19+
multihash = { version = "0.17.0", default-features = false }
1920
once_cell = "1.17.1"
21+
quick-protobuf = "0.8"
2022
rand = "0.8.3"
2123
sha2 = "0.10.0"
2224
static_assertions = "1"
@@ -34,8 +36,9 @@ snow = { version = "0.9.2", features = ["default-resolver"], default-features =
3436
env_logger = "0.10.0"
3537
futures_ringbuf = "0.3.1"
3638
quickcheck = { workspace = true }
39+
multihash = { version = "0.17.0", features = ["sha2"] }
3740

38-
# Passing arguments to the docsrs builder in order to properly document cfg's.
41+
# Passing arguments to the docsrs builder in order to properly document cfg's.
3942
# More information: https://docs.rs/about/builds#cross-compiling
4043
[package.metadata.docs.rs]
4144
all-features = true
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
syntax = "proto3";
2-
32
package payload.proto;
43

54
// Payloads for Noise handshake messages.
65

6+
message NoiseExtensions {
7+
repeated bytes webtransport_certhashes = 1;
8+
repeated string stream_muxers = 2;
9+
}
10+
711
message NoiseHandshakePayload {
812
bytes identity_key = 1;
913
bytes identity_sig = 2;
10-
bytes data = 3;
14+
optional NoiseExtensions extensions = 4;
1115
}

transports/noise/src/generated/payload/proto.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,48 @@ use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer
1313
use quick_protobuf::sizeofs::*;
1414
use super::super::*;
1515

16+
#[allow(clippy::derive_partial_eq_without_eq)]
17+
#[derive(Debug, Default, PartialEq, Clone)]
18+
pub struct NoiseExtensions {
19+
pub webtransport_certhashes: Vec<Vec<u8>>,
20+
pub stream_muxers: Vec<String>,
21+
}
22+
23+
impl<'a> MessageRead<'a> for NoiseExtensions {
24+
fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
25+
let mut msg = Self::default();
26+
while !r.is_eof() {
27+
match r.next_tag(bytes) {
28+
Ok(10) => msg.webtransport_certhashes.push(r.read_bytes(bytes)?.to_owned()),
29+
Ok(18) => msg.stream_muxers.push(r.read_string(bytes)?.to_owned()),
30+
Ok(t) => { r.read_unknown(bytes, t)?; }
31+
Err(e) => return Err(e),
32+
}
33+
}
34+
Ok(msg)
35+
}
36+
}
37+
38+
impl MessageWrite for NoiseExtensions {
39+
fn get_size(&self) -> usize {
40+
0
41+
+ self.webtransport_certhashes.iter().map(|s| 1 + sizeof_len((s).len())).sum::<usize>()
42+
+ self.stream_muxers.iter().map(|s| 1 + sizeof_len((s).len())).sum::<usize>()
43+
}
44+
45+
fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
46+
for s in &self.webtransport_certhashes { w.write_with_tag(10, |w| w.write_bytes(&**s))?; }
47+
for s in &self.stream_muxers { w.write_with_tag(18, |w| w.write_string(&**s))?; }
48+
Ok(())
49+
}
50+
}
51+
1652
#[allow(clippy::derive_partial_eq_without_eq)]
1753
#[derive(Debug, Default, PartialEq, Clone)]
1854
pub struct NoiseHandshakePayload {
1955
pub identity_key: Vec<u8>,
2056
pub identity_sig: Vec<u8>,
21-
pub data: Vec<u8>,
57+
pub extensions: Option<payload::proto::NoiseExtensions>,
2258
}
2359

2460
impl<'a> MessageRead<'a> for NoiseHandshakePayload {
@@ -28,7 +64,7 @@ impl<'a> MessageRead<'a> for NoiseHandshakePayload {
2864
match r.next_tag(bytes) {
2965
Ok(10) => msg.identity_key = r.read_bytes(bytes)?.to_owned(),
3066
Ok(18) => msg.identity_sig = r.read_bytes(bytes)?.to_owned(),
31-
Ok(26) => msg.data = r.read_bytes(bytes)?.to_owned(),
67+
Ok(34) => msg.extensions = Some(r.read_message::<payload::proto::NoiseExtensions>(bytes)?),
3268
Ok(t) => { r.read_unknown(bytes, t)?; }
3369
Err(e) => return Err(e),
3470
}
@@ -42,13 +78,13 @@ impl MessageWrite for NoiseHandshakePayload {
4278
0
4379
+ if self.identity_key.is_empty() { 0 } else { 1 + sizeof_len((&self.identity_key).len()) }
4480
+ if self.identity_sig.is_empty() { 0 } else { 1 + sizeof_len((&self.identity_sig).len()) }
45-
+ if self.data.is_empty() { 0 } else { 1 + sizeof_len((&self.data).len()) }
81+
+ self.extensions.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
4682
}
4783

4884
fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
4985
if !self.identity_key.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.identity_key))?; }
5086
if !self.identity_sig.is_empty() { w.write_with_tag(18, |w| w.write_bytes(&**&self.identity_sig))?; }
51-
if !self.data.is_empty() { w.write_with_tag(26, |w| w.write_bytes(&**&self.data))?; }
87+
if let Some(ref s) = self.extensions { w.write_with_tag(34, |w| w.write_message(s))?; }
5288
Ok(())
5389
}
5490
}

transports/noise/src/io/framed.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ impl<T> NoiseFramed<T, snow::HandshakeState> {
8181
}
8282
}
8383

84+
pub(crate) fn is_initiator(&self) -> bool {
85+
self.session.is_initiator()
86+
}
87+
88+
pub(crate) fn is_responder(&self) -> bool {
89+
!self.session.is_initiator()
90+
}
91+
8492
/// Converts the `NoiseFramed` into a `NoiseOutput` encrypted data stream
8593
/// once the handshake is complete, including the static DH [`PublicKey`]
8694
/// of the remote, if received.

transports/noise/src/io/handshake.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
mod proto {
2424
#![allow(unreachable_pub)]
2525
include!("../generated/mod.rs");
26+
pub use self::payload::proto::NoiseExtensions;
2627
pub use self::payload::proto::NoiseHandshakePayload;
2728
}
2829

@@ -32,7 +33,9 @@ use crate::{DecodeError, Error};
3233
use bytes::Bytes;
3334
use futures::prelude::*;
3435
use libp2p_identity as identity;
36+
use multihash::Multihash;
3537
use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer};
38+
use std::collections::HashSet;
3639
use std::io;
3740

3841
//////////////////////////////////////////////////////////////////////////////
@@ -49,6 +52,15 @@ pub(crate) struct State<T> {
4952
dh_remote_pubkey_sig: Option<Vec<u8>>,
5053
/// The known or received public identity key of the remote, if any.
5154
id_remote_pubkey: Option<identity::PublicKey>,
55+
/// The WebTransport certhashes of the responder, if any.
56+
responder_webtransport_certhashes: Option<HashSet<Multihash>>,
57+
/// The received extensions of the remote, if any.
58+
remote_extensions: Option<Extensions>,
59+
}
60+
61+
/// Extensions
62+
struct Extensions {
63+
webtransport_certhashes: HashSet<Multihash>,
5264
}
5365

5466
impl<T> State<T> {
@@ -63,12 +75,15 @@ impl<T> State<T> {
6375
session: snow::HandshakeState,
6476
identity: KeypairIdentity,
6577
expected_remote_key: Option<identity::PublicKey>,
78+
responder_webtransport_certhashes: Option<HashSet<Multihash>>,
6679
) -> Self {
6780
Self {
6881
identity,
6982
io: NoiseFramed::new(io, session),
7083
dh_remote_pubkey_sig: None,
7184
id_remote_pubkey: expected_remote_key,
85+
responder_webtransport_certhashes,
86+
remote_extensions: None,
7287
}
7388
}
7489
}
@@ -77,6 +92,7 @@ impl<T> State<T> {
7792
/// Finish a handshake, yielding the established remote identity and the
7893
/// [`Output`] for communicating on the encrypted channel.
7994
pub(crate) fn finish(self) -> Result<(identity::PublicKey, Output<T>), Error> {
95+
let is_initiator = self.io.is_initiator();
8096
let (pubkey, io) = self.io.into_transport()?;
8197

8298
let id_pk = self
@@ -91,10 +107,43 @@ impl<T> State<T> {
91107
return Err(Error::BadSignature);
92108
}
93109

110+
// Check WebTransport certhashes that responder reported back to us
111+
if is_initiator {
112+
// We check only if we care (i.e. Config::with_webtransport_certhashes was used)
113+
if let Some(expected_certhashes) = self.responder_webtransport_certhashes {
114+
let ext = self.remote_extensions.ok_or_else(|| {
115+
Error::UnknownWebTransportCerthashes(expected_certhashes.to_owned())
116+
})?;
117+
118+
// Expected WebTransport certhashes must be a strict subset
119+
// of the reported ones
120+
let unknown_certhashes = expected_certhashes
121+
.difference(&ext.webtransport_certhashes)
122+
.cloned()
123+
.collect::<HashSet<_>>();
124+
125+
if !unknown_certhashes.is_empty() {
126+
return Err(Error::UnknownWebTransportCerthashes(unknown_certhashes));
127+
}
128+
}
129+
}
130+
94131
Ok((id_pk, io))
95132
}
96133
}
97134

135+
impl From<proto::NoiseExtensions> for Extensions {
136+
fn from(value: proto::NoiseExtensions) -> Self {
137+
Extensions {
138+
webtransport_certhashes: value
139+
.webtransport_certhashes
140+
.into_iter()
141+
.filter_map(|bytes| Multihash::read(&bytes[..]).ok())
142+
.collect(),
143+
}
144+
}
145+
}
146+
98147
//////////////////////////////////////////////////////////////////////////////
99148
// Handshake Message Futures
100149

@@ -149,6 +198,10 @@ where
149198
state.dh_remote_pubkey_sig = Some(pb.identity_sig);
150199
}
151200

201+
if let Some(extensions) = pb.extensions {
202+
state.remote_extensions = Some(extensions.into());
203+
}
204+
152205
Ok(())
153206
}
154207

@@ -164,6 +217,17 @@ where
164217

165218
pb.identity_sig = state.identity.signature.clone();
166219

220+
// If this is the responder then send WebTransport certhashes to initiator, if any
221+
if state.io.is_responder() {
222+
if let Some(ref certhashes) = state.responder_webtransport_certhashes {
223+
let ext = pb
224+
.extensions
225+
.get_or_insert_with(proto::NoiseExtensions::default);
226+
227+
ext.webtransport_certhashes = certhashes.iter().map(|hash| hash.to_bytes()).collect();
228+
}
229+
}
230+
167231
let mut msg = Vec::with_capacity(pb.get_size());
168232

169233
let mut writer = Writer::new(&mut msg);

0 commit comments

Comments
 (0)