From 8ebca574aaeb11d93738fb73fb7da76409ce5e44 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Fri, 30 Sep 2022 10:47:36 +0400 Subject: [PATCH 1/2] set the Noise prologue in the WebRTC handshake --- src/libp2p/collection/multi_stream.rs | 29 ++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/libp2p/collection/multi_stream.rs b/src/libp2p/collection/multi_stream.rs index c6ed6227da..e194d49b30 100644 --- a/src/libp2p/collection/multi_stream.rs +++ b/src/libp2p/collection/multi_stream.rs @@ -25,7 +25,7 @@ use super::{ NotificationsOutErr, OverlayNetwork, PeerId, ShutdownCause, SubstreamId, }; -use alloc::{collections::VecDeque, string::ToString as _, sync::Arc}; +use alloc::{collections::VecDeque, string::ToString as _, sync::Arc, vec::Vec}; use core::{ hash::Hash, iter, @@ -140,16 +140,35 @@ where request_response_protocols: Arc<[ConfigRequestResponse]>, ping_protocol: Arc, ) -> Self { - // We only support one kind of handshake at the moment. Make sure (at compile time) that - // the value provided as parameter is indeed the one expected. - let MultiStreamHandshakeKind::WebRtc { .. } = handshake_kind; + // In the WebRTC handshake, the Noise prologue must be set to `"libp2p-webrtc-noise:"` + // followed with the multihash-encoded fingerprints of the local and remote certificates + // in ascending order. + // See . + let noise_prologue = { + let MultiStreamHandshakeKind::WebRtc { + local_tls_certificate_multihash, + remote_tls_certificate_multihash, + } = handshake_kind; + const PREFIX: &[u8] = b"libp2p-webrtc-noise:"; + let mut out = Vec::with_capacity( + PREFIX.len() + + local_tls_certificate_multihash.len() + + remote_tls_certificate_multihash.len(), + ); + out.extend_from_slice(PREFIX); + // Since smoldot always acts as a client (at least right now), we don't need to change + // the order of fingerprints. + out.extend_from_slice(&local_tls_certificate_multihash); + out.extend_from_slice(&remote_tls_certificate_multihash); + out + }; MultiStreamConnectionTask { connection: MultiStreamConnectionTaskInner::Handshake { handshake: Some(noise::HandshakeInProgress::new(noise::Config { key: &noise_key, is_initiator: true, // TODO: is_initiator? - prologue: &[], // TODO: this prologue isn't correct, WebRTC requires passing certificate fingerprints + prologue: &noise_prologue, })), opened_substream: None, extra_open_substreams: hashbrown::HashMap::with_capacity_and_hasher( From 994a7c4a908fc367f91f0252399862e3679f425b Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Tue, 4 Oct 2022 11:59:39 +0400 Subject: [PATCH 2/2] Update src/libp2p/collection/multi_stream.rs Co-authored-by: Pierre Krieger --- src/libp2p/collection/multi_stream.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libp2p/collection/multi_stream.rs b/src/libp2p/collection/multi_stream.rs index e194d49b30..af3bed1f1a 100644 --- a/src/libp2p/collection/multi_stream.rs +++ b/src/libp2p/collection/multi_stream.rs @@ -141,8 +141,9 @@ where ping_protocol: Arc, ) -> Self { // In the WebRTC handshake, the Noise prologue must be set to `"libp2p-webrtc-noise:"` - // followed with the multihash-encoded fingerprints of the local and remote certificates - // in ascending order. + // followed with the multihash-encoded fingerprints of the initiator's certificate + // and the receiver's certificate. + // TODO: we currently assume that the local node is always the initiator // See . let noise_prologue = { let MultiStreamHandshakeKind::WebRtc {