Skip to content

Commit

Permalink
fix: use the wording of actual and expected PeerId
Browse files Browse the repository at this point in the history
The `PeerId` coming from the address is the expected `PeerId`
and the one from the handshake is the actual `PeerId`.
  • Loading branch information
denis2glez committed Nov 27, 2023
1 parent c386182 commit 3eb5f1f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
6 changes: 3 additions & 3 deletions core/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ pub trait OutboundSecurityUpgrade<T>: UpgradeInfo {
/// method is called to start the handshake.
///
/// The `info` is the identifier of the protocol, as produced by `protocol_info`. Security
/// transports use the optional `remote_peer_id` parameter on outgoing upgrades to validate the
/// expected `PeerId`.
/// transports use the optional `expected_peer_id` parameter on outgoing upgrades to validate
/// the expected `PeerId`.
fn secure_outbound(
self,
socket: T,
info: Self::Info,
remote_peer_id: Option<PeerId>,
expected_peer_id: Option<PeerId>,
) -> Self::Future;
}
18 changes: 10 additions & 8 deletions transports/noise/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ where
self,
socket: T,
_: Self::Info,
remote_peer_id: Option<PeerId>,
expected_peer_id: Option<PeerId>,
) -> Self::Future {
async move {
let mut state = self.into_initiator(socket)?;
Expand All @@ -250,11 +250,13 @@ where
let (pk, io) = state.finish()?;

let peer_id = pk.to_peer_id();
match remote_peer_id {
Some(remote_peer_id) if remote_peer_id != peer_id => Err(Error::PeerIdMismatch {
peer_id,
remote_peer_id,
}),
match expected_peer_id {
Some(expected_peer_id) if expected_peer_id != peer_id => {
Err(Error::PeerIdMismatch {
peer_id,
expected_peer_id,
})
}
_ => Ok((peer_id, io)),
}
}
Expand Down Expand Up @@ -286,10 +288,10 @@ pub enum Error {
SigningError(#[from] libp2p_identity::SigningError),
#[error("Expected WebTransport certhashes ({}) are not a subset of received ones ({})", certhashes_to_string(.0), certhashes_to_string(.1))]
UnknownWebTransportCerthashes(HashSet<Multihash<64>>, HashSet<Multihash<64>>),
#[error("Invalid peer ID (actual {peer_id:?}, remote {remote_peer_id:?})")]
#[error("Invalid peer ID (actual {peer_id:?}, expected {expected_peer_id:?})")]
PeerIdMismatch {
peer_id: PeerId,
remote_peer_id: PeerId,
expected_peer_id: PeerId,
},
}

Expand Down
16 changes: 8 additions & 8 deletions transports/tls/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ pub enum UpgradeError {
ClientUpgrade(std::io::Error),
#[error("Failed to parse certificate")]
BadCertificate(#[from] certificate::ParseError),
#[error("Invalid peer ID (actual {peer_id:?}, remote {remote_peer_id:?})")]
#[error("Invalid peer ID (actual {peer_id:?}, expected {expected_peer_id:?})")]
PeerIdMismatch {
peer_id: PeerId,
remote_peer_id: PeerId,
expected_peer_id: PeerId,
},
}

Expand All @@ -66,14 +66,14 @@ impl Config {
})
}

pub(crate) fn with_remote_peer_id(
remote_peer_id: Option<PeerId>,
pub(crate) fn with_expected_peer_id(
expected_peer_id: Option<PeerId>,
) -> Result<Self, certificate::GenError> {
let identity = libp2p_identity::Keypair::generate_ed25519();

Ok(Self {
server: crate::make_server_config(&identity)?,
client: crate::make_client_config(&identity, remote_peer_id)?,
client: crate::make_client_config(&identity, expected_peer_id)?,
})
}
}
Expand Down Expand Up @@ -148,11 +148,11 @@ where
mut self,
socket: C,
_: Self::Info,
remote_peer_id: Option<PeerId>,
expected_peer_id: Option<PeerId>,
) -> Self::Future {
async move {
// Create new ad-hoc client and server configuration by passing the remote PeerId
self = Self::with_remote_peer_id(remote_peer_id)?;
// Create new ad-hoc client and server configuration by passing the expected PeerId
self = Self::with_expected_peer_id(expected_peer_id)?;

// Spec: In order to keep this flexibility for future versions, clients that only support
// the version of the handshake defined in this document MUST NOT send any value in the
Expand Down

0 comments on commit 3eb5f1f

Please sign in to comment.