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

set the Noise prologue in the WebRTC handshake #2807

Merged
merged 3 commits into from
Oct 13, 2022
Merged
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
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