From a24d22f201b4edc87fcaad31ad9651528fb629c0 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Tue, 15 Sep 2026 12:22:08 +0800 Subject: [PATCH] key exchange version 1 on the rendezvous and peer handshakes Bump hbb_common for key exchange version 1, one stream key per direction, and negotiate it on both handshakes. Rendezvous: `secure_tcp` reads the version the server advertises in `KeyExchange.version`, answers with the highest both speak, splits the key when that is at least 1, and arms the check of the server's echo on its first encrypted message. Peer: the controlled side advertises `KX_VERSION_LATEST` inside the signed `IdPk`, the controller answers in `PublicKey.kx_version` and both split the key when the pick is at least 1. A pick above what was offered is refused as a bug or tampering. `decode_id_pk_dtls` returns the advertised version along with the fingerprint. An absent field on either side is version 0, so any old peer or server keeps today's stream byte for byte. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns --- libs/base/protos/message.proto | 3 +++ libs/hbb_common | 2 +- src/client.rs | 23 ++++++++++++++++--- src/common.rs | 40 ++++++++++++++++++++++++---------- src/server.rs | 27 +++++++++++++++++++++-- 5 files changed, 78 insertions(+), 17 deletions(-) diff --git a/libs/base/protos/message.proto b/libs/base/protos/message.proto index 1276deaa8..dd62942be 100644 --- a/libs/base/protos/message.proto +++ b/libs/base/protos/message.proto @@ -705,6 +705,9 @@ message TestDelay { message PublicKey { bytes asymmetric_value = 1; bytes symmetric_value = 2; + // The key exchange version the controller picked, the highest both sides speak; absent, + // i.e. 0, is the original scheme. + uint32 kx_version = 3; } message SignedId { bytes id = 1; } diff --git a/libs/hbb_common b/libs/hbb_common index d9895ff9c..e272fede3 160000 --- a/libs/hbb_common +++ b/libs/hbb_common @@ -1 +1 @@ -Subproject commit d9895ff9cb4c77137fb05ce3161bcf7210177aef +Subproject commit e272fede36fafa63e6f6b8c71b918267a7ea4195 diff --git a/src/client.rs b/src/client.rs index 0fca4dfcd..38c77bb84 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1694,7 +1694,9 @@ impl Client { let bytes = res?; if let Ok(msg_in) = Message::parse_from_bytes(&bytes) { if let Some(message::Union::SignedId(si)) = msg_in.union { - if let Ok((id, their_pk_b, signed_fp)) = decode_id_pk_dtls(&si.id, &sign_pk) { + if let Ok((id, their_pk_b, signed_fp, kx_version)) = + decode_id_pk_dtls(&si.id, &sign_pk) + { if id == peer_id { // WebRTC only: bind the DTLS channel to the verified peer identity. // webrtc-rs already bound the certificate to the remote SDP, so @@ -1710,14 +1712,29 @@ 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); let mut msg_out = Message::new(); msg_out.set_public_key(PublicKey { - asymmetric_value, + asymmetric_value: asymmetric_value.clone(), symmetric_value, + kx_version: picked, ..Default::default() }); timeout(CONNECT_TIMEOUT, conn.send(&msg_out)).await??; - conn.set_key(key); + if picked >= 1 { + conn.set_key_split( + key, + true, + &hbb_common::tcp::KxTranscript { + initiator_pk: &asymmetric_value, + responder_pk: &their_pk_b, + advertised: kx_version, + picked, + }, + )?; + } else { + conn.set_key(key); + } } else { if is_webrtc { bail!("WebRTC handshake id mismatch (possible MITM)"); diff --git a/src/common.rs b/src/common.rs index bf8964a55..92bbfbe32 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2100,17 +2100,33 @@ async fn key_exchange(conn: &mut Stream, key: &str, log_on_success: bool) -> Res } let their_pk_b = sign::verify(&ex.keys[0], &rs_pk) .map_err(|_| anyhow!("Signature mismatch in key exchange"))?; - let (asymmetric_value, symmetric_value, key) = create_symmetric_key_msg( - get_pk(&their_pk_b) - .context("Wrong their public length in key exchange")?, - ); + let their_pk_b = get_pk(&their_pk_b) + .context("Wrong their public length in key exchange")?; + let (asymmetric_value, symmetric_value, key) = + create_symmetric_key_msg(their_pk_b); + let version = hbb_common::tcp::kx_version_for(ex.version); let mut msg_out = RendezvousMessage::new(); msg_out.set_key_exchange(KeyExchange { - keys: vec![asymmetric_value, symmetric_value], + keys: vec![asymmetric_value.clone(), symmetric_value], + version, ..Default::default() }); timeout(CONNECT_TIMEOUT, conn.send(&msg_out)).await??; - conn.set_key(key); + if version >= 1 { + conn.set_key_split( + key, + true, + &hbb_common::tcp::KxTranscript { + initiator_pk: &asymmetric_value, + responder_pk: &their_pk_b, + advertised: ex.version, + picked: version, + }, + )?; + } else { + conn.set_key(key); + } + conn.check_kx_advertised(ex.version); if log_on_success { log::info!("Connection secured"); } @@ -2170,21 +2186,23 @@ pub fn get_rs_pk(str_base64: &str) -> Option { } pub fn decode_id_pk(signed: &[u8], key: &sign::PublicKey) -> ResultType<(String, [u8; 32])> { - let (id, pk, _) = decode_id_pk_dtls(signed, key)?; + let (id, pk, _, _) = decode_id_pk_dtls(signed, key)?; Ok((id, pk)) } /// Like [`decode_id_pk`] but also returns the signed DTLS certificate fingerprint (empty string -/// for non-WebRTC peers), used to bind a WebRTC DTLS channel to the verified peer identity. +/// for non-WebRTC peers), used to bind a WebRTC DTLS channel to the verified peer identity, and +/// the newest key exchange version the peer speaks (0, the original scheme, for a peer from +/// before versions). pub fn decode_id_pk_dtls( signed: &[u8], key: &sign::PublicKey, -) -> ResultType<(String, [u8; 32], String)> { +) -> ResultType<(String, [u8; 32], String, u32)> { let res = IdPk::parse_from_bytes( &sign::verify(signed, key).map_err(|_| anyhow!("Signature mismatch"))?, )?; if let Some(pk) = get_pk(&res.pk) { - Ok((res.id, pk, res.dtls_fingerprint)) + Ok((res.id, pk, res.dtls_fingerprint, res.kx_version)) } else { bail!("Wrong their public length"); } @@ -3395,7 +3413,7 @@ mod tests { &sk, ); - let (id, their_pk, signed_fp) = decode_id_pk_dtls(&signed, &pk).unwrap(); + let (id, their_pk, signed_fp, _) = decode_id_pk_dtls(&signed, &pk).unwrap(); assert_eq!(id, "123456789"); assert_eq!(their_pk, [7u8; 32]); assert_eq!(signed_fp, fp); diff --git a/src/server.rs b/src/server.rs index 7b2cbe792..34f60080b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -282,6 +282,7 @@ async fn identity_handshake(stream: &mut Stream, secure: bool) -> ResultType<()> id: Config::get_id(), pk: Bytes::from(our_pk_b.0.to_vec()), dtls_fingerprint, + kx_version: tcp::KX_VERSION_LATEST, ..Default::default() } .write_to_bytes() @@ -298,11 +299,33 @@ async fn identity_handshake(stream: &mut Stream, secure: bool) -> ResultType<()> if let Ok(msg_in) = Message::parse_from_bytes(&bytes) { if let Some(message::Union::PublicKey(pk)) = msg_in.union { if pk.asymmetric_value.len() == box_::PUBLICKEYBYTES { - stream.set_key(tcp::Encrypt::decode( + let key = tcp::Encrypt::decode( &pk.symmetric_value, &pk.asymmetric_value, &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 { + bail!( + "Handshake failed: key exchange version {} not offered", + pk.kx_version + ); + } + if pk.kx_version >= 1 { + stream.set_key_split( + key, + false, + &tcp::KxTranscript { + initiator_pk: &pk.asymmetric_value, + responder_pk: &our_pk_b.0, + advertised: tcp::KX_VERSION_LATEST, + picked: pk.kx_version, + }, + )?; + } else { + stream.set_key(key); + } } else if pk.asymmetric_value.is_empty() { Config::set_key_confirmed(false); log::info!("Force to update pk");