Skip to content

Commit

Permalink
set the Noise prologue in the WebRTC handshake (#2807)
Browse files Browse the repository at this point in the history
Updates #2759
See melekes/rust-libp2p#10 and
libp2p/specs#412 (comment)

Co-authored-by: Pierre Krieger <[email protected]>
  • Loading branch information
melekes and tomaka authored Oct 13, 2022
1 parent 5b91f07 commit 3e7ffe1
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/libp2p/collection/multi_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -140,16 +140,36 @@ where
request_response_protocols: Arc<[ConfigRequestResponse]>,
ping_protocol: Arc<str>,
) -> 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 initiator's certificate
// and the receiver's certificate.
// TODO: we currently assume that the local node is always the initiator
// See <https://github.com/libp2p/specs/pull/412>.
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(
Expand Down

0 comments on commit 3e7ffe1

Please sign in to comment.