From 818907602f1ac568381d95c7624c84469c8d63cb Mon Sep 17 00:00:00 2001 From: rustdesk Date: Mon, 21 Sep 2026 14:22:34 +0800 Subject: [PATCH] key exchange: say 0 on WebRTC, where no stream key applies A WebRTC stream is encrypted by DTLS and `set_key_split` collapses to `set_key` on it, which sets nothing but `peer_verified`. Both sides still put 1 on the wire, the controlled peer in its signed identity and the controller in its pick, and agreed on a version neither applied. That held only because both fell into the same branch: the day one side splits keys on WebRTC and the other does not, they would agree on 1, derive different keys, and have no version mismatch to point at. The controlled peer now advertises 0 on WebRTC and refuses a pick above what it advertised rather than above the newest it speaks anywhere; the controller picks 0 there. What is on the wire is what runs. Also bumps hbb_common for the echo contract: the server tags every message after the exchange, not the first alone, so dropping one frame at a known position does not rid an attacker of the downgrade check. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab --- libs/hbb_common | 2 +- src/client.rs | 8 +++++++- src/server.rs | 18 ++++++++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/libs/hbb_common b/libs/hbb_common index 445486cca..1266b5615 160000 --- a/libs/hbb_common +++ b/libs/hbb_common @@ -1 +1 @@ -Subproject commit 445486ccaae6cbe9c45adca11916a04969cdefd1 +Subproject commit 1266b56150da889dfec318e68b9b20d7c30c0bb6 diff --git a/src/client.rs b/src/client.rs index 38c77bb84..b720c488a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1712,7 +1712,13 @@ impl Client { } let (asymmetric_value, symmetric_value, key) = create_symmetric_key_msg(their_pk_b); - let picked = hbb_common::tcp::kx_version_for(kx_version); + // A WebRTC stream is encrypted by DTLS and takes no stream + // key of its own, split or not, so the pick says what runs: 0. + let picked = if is_webrtc { + 0 + } else { + hbb_common::tcp::kx_version_for(kx_version) + }; let mut msg_out = Message::new(); msg_out.set_public_key(PublicKey { asymmetric_value: asymmetric_value.clone(), diff --git a/src/server.rs b/src/server.rs index 34f60080b..1e77d3076 100644 --- a/src/server.rs +++ b/src/server.rs @@ -276,13 +276,21 @@ async fn identity_handshake(stream: &mut Stream, secure: bool) -> ResultType<()> if stream.is_webrtc() && dtls_fingerprint.is_empty() { bail!("WebRTC local DTLS fingerprint unavailable"); } + // A WebRTC stream is encrypted by DTLS and takes no stream key of its own, split or + // not, so what it advertises is what it runs: 0. Saying 1 there would have a future + // controller that splits keys on WebRTC agree on a version this side never applies. + let advertised = if stream.is_webrtc() { + 0 + } else { + tcp::KX_VERSION_LATEST + }; msg_out.set_signed_id(SignedId { id: sign::sign( &IdPk { id: Config::get_id(), pk: Bytes::from(our_pk_b.0.to_vec()), dtls_fingerprint, - kx_version: tcp::KX_VERSION_LATEST, + kx_version: advertised, ..Default::default() } .write_to_bytes() @@ -305,8 +313,10 @@ async fn identity_handshake(stream: &mut Stream, secure: bool) -> ResultType<()> &our_sk_b, )?; // The controller picks from what we offered; anything above that - // is a bug or tampering, never a legitimate peer. - if pk.kx_version > tcp::KX_VERSION_LATEST { + // is a bug or tampering, never a legitimate peer. Below it needs no + // check: the version picked goes into both transcripts, so a pick + // lowered on the way leaves the two sides with different keys. + if pk.kx_version > advertised { bail!( "Handshake failed: key exchange version {} not offered", pk.kx_version @@ -319,7 +329,7 @@ async fn identity_handshake(stream: &mut Stream, secure: bool) -> ResultType<()> &tcp::KxTranscript { initiator_pk: &pk.asymmetric_value, responder_pk: &our_pk_b.0, - advertised: tcp::KX_VERSION_LATEST, + advertised, picked: pk.kx_version, }, )?;