Compare commits

...
Author SHA1 Message Date
rustdeskandClaude Fable 5.2 57d8b91f1d Update hbb_common: the datagram formats are pinned by a test
The submodule gains one test, which opens a recorded hello and welcome
and compares what is sealed after them with recorded bytes. Nothing
the client runs changes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:09 +08:00
rustdeskandClaude Fable 5.2 3a8f62a68a Register in plaintext beside the probe once fallen back
Update hbb_common, where any answer on the session sets the proof
state. Every fourth round of a fallen-back socket went sealed only, so
a server without the seal never answered it: that registration came
3 s late and counted as a timeout. It now goes in plaintext with an
empty hello beside it, and a server that seals by now is still noticed
in that round. A rebind also sent every socket back to probing, so one
that rebinds before its two probes time out never registered; a socket
that has not sealed keeps its count across a rebind now.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:09 +08:00
rustdeskandClaude Fable 5.2 0c384e3dbd Update hbb_common: the proof goes only until the server holds it
A registration carries the device's signature until the server's
answer says it holds the proof of the session, and none after, so a
registration after the first is 64 bytes shorter. Nothing changes here
but the submodule and the comment that said every registration carries
the proof.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:08 +08:00
rustdeskandClaude Opus 5.5 629d273ec9 Register under the session and prove the device there
Update hbb_common, whose hello no longer carries a signature: a
registration now goes only as data under the session, carrying the
device's proof, and with no session yet an empty hello goes instead,
the registration following its welcome at once. A punch reply goes
under the same session; only with none yet, as right after a cookie,
does it go as a hello.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:08 +08:00
rustdeskandClaude Opus 5.5 9302626cc0 Send the hello again with the cookie a server past its budget answers with
A server past its hello budget now answers a hello with a cookie for the
client's address instead of dropping it. The client sends that hello
again with the cookie, and the socket counts as sealed, since only a
server that seals sends one: forged hellos at the server can no longer
push a new socket into plaintext. Later hellos carry the cookie, the
one-off PunchHoleSent too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:07 +08:00
rustdeskandClaude Opus 5.5 516e5828da Update hbb_common: forward secrecy for the welcome
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:07 +08:00
rustdeskandClaude Opus 5.5 3d0123a307 Update hbb_common: keep the replaced session for receiving
A push sealed under the session a welcome replaced still opens until one
opens under the new session, so hbbs can wait to see the new session
used before pushing under it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:06 +08:00
rustdeskandClaude Opus 5.5 1dc1abceb0 Update hbb_common: name the standards behind udp_seal
Comments only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:06 +08:00
rustdeskandClaude Opus 5.5 9b168d81e1 Keep a punch reply sealed while a missed answer is retried
A missed registration answer moves a sealed socket to resealing, which
used to drop its host from the one-off seal list, so the PunchHoleSent of
a UDP punch went out in plaintext until the next answer. The socket's
server still seals then; only a rebind, which may reach another server,
or the socket going away forgets it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:05 +08:00
rustdeskandClaude Opus 5.5 cffe93f1a2 Update hbb_common: no hello budget for a datagram too short to be one
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:05 +08:00
rustdeskandClaude Opus 5.5 9082f3d912 Update hbb_common: 1024-number udp_seal replay window
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:05 +08:00
rustdeskandClaude Opus 5.5 5789712752 Retry a missed sealed answer under the session and with a hello at once
Taking turns cost a server that restarted one more timeout before its
hello. Each retry now sends both: the datagram under the session, which
a server that only lost an answer takes without its hello budget, and a
hello for one that restarted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:04 +08:00
rustdeskandClaude Opus 5.5 d12b8c5894 Retry a missed sealed answer under the session before a hello
A server that only lost an answer still holds the session, but the
retry went as a hello, which shares the server's hello budget with
anyone who floods it. The first retry now goes under the session, and
retries alternate with hellos for a server that restarted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:04 +08:00
rustdeskandClaude Opus 5.5 19f368c837 Re-seal after a missed answer instead of probing afresh
A socket whose answer went missing while sealed dropped its session
and probed again: until a new welcome, pushes under the session it
still shared with the server were lost and plaintext pushes were
taken, and three misses in a row fell back to taking plaintext
registration answers, a forged UUID_MISMATCH among them.

Such a socket now sends hellos while keeping the session for pushes
until a welcome replaces it, and never falls back, since its server
seals. A rebind, which may reach another server, still probes afresh
and may fall back, so a server rolled back to an older build is
reached again within about a minute.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:03 +08:00
rustdeskandClaude Opus 5.5 de85cea9cd Take only an empty sealed payload as the welcome to an empty hello
Any sealed message whose union this build does not know also parsed
with no union, so a newer server's push cleared the pending
registration and its timeout. The welcome to an empty hello is the
only sealed datagram with nothing in it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:03 +08:00
rustdeskandClaude Opus 5.5 5ac7a3bafe Keep the sealed session while a signed hello replaces it
Re-signing a session from an empty hello reset it first, so for one
round trip pushes under the old key were dropped and plaintext pushes
were taken from a server that seals every push. The signed hello now
goes out beside the session, which the welcome replaces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:02 +08:00
rustdeskandClaude Opus 5.5 54c8c2e018 Drop forged registration answers while probing, follow key changes
While probing every registration goes as a hello, so a plaintext answer to
one is forged: acting on it let a spoofed UUID_MISMATCH change the device ID.
A RegisterPk with a new key reloads the device key for later hellos. A rebind
in a timed-out round now leaves the new socket its two hellos.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:02 +08:00
rustdeskandClaude Opus 5.5 925443d770 Seal the UDP registration and punch replies to the rendezvous server
A socket probes with signed hellos, falls back to plaintext after two go
unanswered (an older server) and re-probes every fourth round; once a sealed
datagram opens it sends only sealed data and drops plaintext answers to its
registrations. A hello never carries a RegisterPk, which a replay from
another address would move the route with; after a RegisterPk is answered the
next RegisterPeer goes as a signed hello again, since the server may not have
had the device's key when the first one came. A UDP punch's PunchHoleSent goes
sealed while the server is. Without a key for the server nothing changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
2026-12-02 18:29:01 +08:00
3 changed files with 674 additions and 4 deletions

No files matched your search

+42 -3
View File
@@ -37,6 +37,8 @@ use crate::{
server::{check_zombie, new as new_server, ConnectionMeta, ServerPtr},
};
mod seal_state;
type Message = RendezvousMessage;
const FAST_RETRY_TIMEOUT: u64 = 3_000;
@@ -421,6 +423,12 @@ impl RendezvousMediator {
host_prefix: Self::get_host_prefix(&host),
keep_alive: crate::DEFAULT_KEEP_ALIVE,
};
let mut seal = seal_state::SealState::new(
&rz.host,
&crate::get_key(true).await,
crate::using_public_server(),
seal_state::device_key(),
);
let mut timer = crate::rustdesk_interval(interval(crate::TIMER_OUT));
const MIN_REG_TIMEOUT: i64 = 3_000;
@@ -467,8 +475,15 @@ impl RendezvousMediator {
n = socket.next() => {
match n {
Some(Ok((bytes, _))) => {
if seal.on_cookie(&bytes, &mut socket, &addr).await? { continue; }
let Some((bytes, sealed)) = seal.open(bytes) else { continue };
if let Ok(msg) = Message::parse_from_bytes(&bytes) {
rz.handle_resp(msg.union, Sink::Framed(&mut socket, &addr), &server, &mut update_latency).await?;
if sealed && bytes.is_empty() {
rz.register_peer(seal.round_sink(&mut socket, &addr)).await?;
last_register_sent = Some(Instant::now());
} else if seal.on_message(sealed, &msg.union) {
rz.handle_resp(msg.union, seal.reply_sink(&mut socket, &addr, sealed), &server, &mut update_latency).await?;
}
} else {
log::debug!("Non-protobuf message bytes received: {:?}", bytes);
}
@@ -502,6 +517,7 @@ impl RendezvousMediator {
}
}
if timeout || (last_register_sent.is_none() && expired) {
seal.on_round(timeout);
if timeout {
fails += 1;
if fails >= MAX_FAILS2 {
@@ -514,6 +530,7 @@ impl RendezvousMediator {
socket = s;
rz.addr = new_addr.clone();
addr = new_addr;
seal.on_rebind();
}
last_dns_check = Instant::now();
}
@@ -522,7 +539,7 @@ impl RendezvousMediator {
old_latency = 0;
}
}
rz.register_peer(Sink::Framed(&mut socket, &addr)).await?;
rz.register_peer(seal.round_sink(&mut socket, &addr)).await?;
last_register_sent = now;
}
}
@@ -1259,7 +1276,7 @@ impl RendezvousMediator {
let mut msg_out = Message::new();
msg_out.set_punch_hole_sent(msg_punch);
let (socket, addr) = new_direct_udp_for(&self.host).await?;
let data = msg_out.write_to_bytes()?;
let data = seal_state::seal_one_shot(&self.host, msg_out.write_to_bytes()?);
socket.send_to(&data, addr).await?;
// The reply is out, and with it the answer and the v6 address: declined is the listen
// alone, and the socket goes at once - a declined request is not worth one kept for its
@@ -1478,6 +1495,17 @@ async fn direct_server(server: ServerPtr) {
enum Sink<'a> {
Framed(&'a mut FramedSocket, &'a TargetAddr<'a>),
Stream(&'a mut Stream),
Sealed(
&'a mut FramedSocket,
&'a TargetAddr<'a>,
&'a mut hbb_common::udp_seal::ClientSeal,
bool,
),
Probe(
&'a mut FramedSocket,
&'a TargetAddr<'a>,
&'a mut hbb_common::udp_seal::ClientSeal,
),
}
impl Sink<'_> {
@@ -1485,6 +1513,17 @@ impl Sink<'_> {
match self {
Sink::Framed(socket, addr) => socket.send(msg, addr.to_owned()).await,
Sink::Stream(stream) => stream.send(msg).await,
Sink::Sealed(socket, addr, seal, rehello) => {
for bytes in seal_state::seal_round(seal, msg, rehello, seal_state::device_key)? {
socket.send_bytes(bytes.into(), addr.to_owned()).await?;
}
Ok(())
}
Sink::Probe(socket, addr, seal) => {
socket.send(msg, addr.to_owned()).await?;
let hello = seal_state::probe(seal)?;
socket.send_bytes(hello.into(), addr.to_owned()).await
}
}
}
}
+631
View File
@@ -0,0 +1,631 @@
//! Which registration datagrams go sealed, and which received ones are acted on.
//!
//! A socket starts out probing: every registration goes sealed. Two unanswered in a row and the
//! server is taken for one without the seal: registrations go plaintext, and every fourth one
//! takes a hello to notice an upgrade. A sealed datagram that opens makes the socket sealed, where
//! nothing goes plaintext and a plaintext answer to a registration is dropped as forged. A
//! missed answer while sealed is retried both under the session and with a hello, since a server
//! restart retires every session; the session stays for pushes until a welcome replaces it. Such
//! a socket never falls back: its server seals. Only a new socket, which may reach another
//! server, probes afresh. A server past its hello budget answers with a cookie, which only a
//! server that seals sends: the hello goes again with it and the socket is sealed.
use super::{Sink, TargetAddr};
use hbb_common::{
anyhow::bail,
bytes::BytesMut,
config::{Config, RS_PUB_KEY},
protobuf::Message as _,
rendezvous_proto::{rendezvous_message, RendezvousMessage},
sodiumoxide::crypto::sign,
udp::FramedSocket,
udp_seal::{self, ClientSeal, Cookie, OneShot},
ResultType,
};
use std::sync::Mutex;
const PROBE_TRIES: u8 = 2;
const PROBE_EVERY: u8 = 4;
// The hosts whose registration is sealed now, their keys, cookies and sessions: a one-off datagram
// to one of them may go sealed too. Only a socket with a session or a cookie adds its host, so an
// older server never gets one.
static SEALED: Mutex<Vec<(String, sign::PublicKey, Option<Cookie>, Option<OneShot>)>> =
Mutex::new(Vec::new());
#[derive(Clone, Copy, Debug, PartialEq)]
enum Mode {
Probing(u8),
Fallback(u8),
Sealed,
Resealing,
}
impl Mode {
/// A missed answer is no sign the server stopped sealing.
fn seals(self) -> bool {
matches!(self, Mode::Sealed | Mode::Resealing)
}
}
pub(super) struct SealState {
host: String,
server: Option<sign::PublicKey>,
seal: Option<ClientSeal>,
mode: Mode,
}
/// The device's signing key; the server has its public half from RegisterPk.
pub(super) fn device_key() -> Option<sign::SecretKey> {
sign::SecretKey::from_slice(&Config::get_key_pair().0)
}
impl SealState {
/// Without a key for this server nothing is sealed and every path runs as before. The public
/// server's key stands in for an empty setting, and it is no key for any other server.
pub(super) fn new(
host: &str,
key: &str,
public_server: bool,
device: Option<sign::SecretKey>,
) -> Self {
let server = if key == RS_PUB_KEY && !public_server {
None
} else {
crate::common::get_rs_pk(key)
};
Self {
host: host.to_owned(),
server,
seal: server.and_then(|pk| ClientSeal::new(&pk, device)),
mode: Mode::Probing(0),
}
}
/// Before a register round sends; `timed_out` is whether the previous one went unanswered.
pub(super) fn on_round(&mut self, timed_out: bool) {
if self.seal.is_none() {
return;
}
let mode = match self.mode {
Mode::Probing(n) if timed_out && n + 1 >= PROBE_TRIES => Mode::Fallback(0),
Mode::Probing(n) if timed_out => Mode::Probing(n + 1),
Mode::Sealed if timed_out => Mode::Resealing,
Mode::Fallback(r) => Mode::Fallback((r + 1) % PROBE_EVERY),
mode => mode,
};
self.set_mode(mode);
}
/// A new socket may be a new path, so one that sealed is probed again. One that had not
/// keeps its count: a rebind every minute would otherwise keep it probing for good.
pub(super) fn on_rebind(&mut self) {
if let Some(seal) = self.seal.as_mut() {
seal.reset();
}
if self.mode.seals() {
self.set_mode(Mode::Probing(0));
}
}
pub(super) fn round_sink<'a>(
&'a mut self,
socket: &'a mut FramedSocket,
addr: &'a TargetAddr<'a>,
) -> Sink<'a> {
let sealed = !matches!(self.mode, Mode::Fallback(_));
match self.seal.as_mut() {
Some(seal) if sealed => Sink::Sealed(socket, addr, seal, self.mode == Mode::Resealing),
Some(seal) if self.mode == Mode::Fallback(PROBE_EVERY - 1) => {
Sink::Probe(socket, addr, seal)
}
_ => Sink::Framed(socket, addr),
}
}
/// A reply goes the way the datagram it answers came.
pub(super) fn reply_sink<'a>(
&'a mut self,
socket: &'a mut FramedSocket,
addr: &'a TargetAddr<'a>,
sealed: bool,
) -> Sink<'a> {
match self.seal.as_mut() {
Some(seal) if sealed => Sink::Sealed(socket, addr, seal, false),
_ => Sink::Framed(socket, addr),
}
}
/// The bytes to parse and whether they came sealed, or `None` to drop the datagram.
pub(super) fn open(&mut self, bytes: BytesMut) -> Option<(BytesMut, bool)> {
let seal = match self.seal.as_mut() {
Some(seal) if udp_seal::is_sealed(&bytes) => seal,
_ => return Some((bytes, false)),
};
let payload = seal.open(&bytes)?;
self.set_mode(Mode::Sealed);
// A welcome may have brought a new session.
self.publish();
Some((BytesMut::from(&payload[..]), true))
}
/// Whether `bytes` was a cookie reply; one to the latest hello sends that hello again with it.
pub(super) async fn on_cookie(
&mut self,
bytes: &[u8],
socket: &mut FramedSocket,
addr: &TargetAddr<'_>,
) -> ResultType<bool> {
let Some(seal) = self.seal.as_mut().filter(|_| udp_seal::is_cookie(bytes)) else {
return Ok(false);
};
if let Some(again) = seal.on_cookie(bytes) {
let cookie = seal.cookie();
socket.send_bytes(again.into(), addr.to_owned()).await?;
self.set_mode(Mode::Sealed);
if let Some(entry) = SEALED.lock().unwrap().iter_mut().find(|e| e.0 == self.host) {
entry.2 = cookie;
}
}
Ok(true)
}
/// Whether to act on a parsed message. Unless the socket fell back, every registration went
/// sealed, so a plaintext answer to one is forged; once sealed, so is a plaintext push from a
/// server that seals every push.
pub(super) fn on_message(
&mut self,
sealed: bool,
msg: &Option<rendezvous_message::Union>,
) -> bool {
if sealed {
return true;
}
let Some(seal) = self.seal.as_ref() else {
return true;
};
match (self.mode, msg) {
(Mode::Fallback(_), _) => true,
(
_,
Some(rendezvous_message::Union::RegisterPeerResponse(_))
| Some(rendezvous_message::Union::RegisterPkResponse(_)),
) => false,
(Mode::Probing(_), _) => true,
(Mode::Sealed | Mode::Resealing, _) => !seal.pushes_sealed(),
}
}
fn set_mode(&mut self, mode: Mode) {
if self.mode == mode {
return;
}
let was_sealed = self.mode.seals();
self.mode = mode;
match self.server {
Some(_) if mode == Mode::Sealed => self.publish(),
_ if was_sealed && !mode.seals() => self.forget_sealed(),
_ => {}
}
}
fn publish(&self) {
let (Some(server), Some(seal)) = (self.server, self.seal.as_ref()) else {
return;
};
let mut sealed = SEALED.lock().unwrap();
sealed.retain(|e| e.0 != self.host);
sealed.push((self.host.clone(), server, seal.cookie(), seal.one_shot()));
}
fn forget_sealed(&self) {
SEALED.lock().unwrap().retain(|e| e.0 != self.host);
}
}
impl Drop for SealState {
fn drop(&mut self) {
if self.mode.seals() {
self.forget_sealed();
}
}
}
/// The datagram a registration goes out as: data under the session, where it carries the
/// device's proof until the server holds it, or with no session yet an empty hello, the
/// registration following its welcome.
/// `hello` asks for one even under a session, as after a missed answer. A RegisterPk with a key
/// other than the one proofs are signed by means the key pair changed: `device` loads the new
/// one, and is called for nothing else.
fn seal_message(
seal: &mut ClientSeal,
msg: &RendezvousMessage,
hello: bool,
device: impl Fn() -> Option<sign::SecretKey>,
) -> ResultType<Vec<u8>> {
let pk = &msg.register_pk().pk;
if !pk.is_empty() && !seal.signs_as(pk) {
seal.set_device(device().filter(|sk| sk.0[32..] == pk[..]));
}
let sealed = if hello || !seal.has_session() {
seal.hello(&[])
} else {
seal.seal(&msg.write_to_bytes()?)
};
match sealed {
Some(bytes) => Ok(bytes),
None => bail!("failed to seal a datagram to the rendezvous server"),
}
}
/// The datagrams a round's registration goes out as: under the session, and with `rehello` a
/// hello too, which a server that restarted needs and one that only lost an answer does not.
pub(super) fn seal_round(
seal: &mut ClientSeal,
msg: &RendezvousMessage,
rehello: bool,
device: impl Fn() -> Option<sign::SecretKey>,
) -> ResultType<Vec<Vec<u8>>> {
let mut datagrams = vec![seal_message(seal, msg, false, &device)?];
if rehello && !udp_seal::is_hello(&datagrams[0]) {
datagrams.push(seal_message(seal, msg, true, &device)?);
}
Ok(datagrams)
}
/// The hello a fallen-back socket sends beside a plaintext registration, so the round is
/// answered whether or not the server seals by now.
pub(super) fn probe(seal: &mut ClientSeal) -> ResultType<Vec<u8>> {
match seal.hello(&[]) {
Some(bytes) => Ok(bytes),
None => bail!("failed to seal a datagram to the rendezvous server"),
}
}
/// A one-off datagram to `host`, the PunchHoleSent of a UDP punch, goes sealed only while that
/// host's registration is: an older server would drop it, and the punch with it. It goes under the
/// registration's session; with none yet, as right after a cookie, it goes as a hello, 0-RTT data
/// without forward secrecy (RFC 8446 §2.3).
pub(super) fn seal_one_shot(host: &str, data: Vec<u8>) -> Vec<u8> {
let server = SEALED
.lock()
.unwrap()
.iter()
.find(|e| e.0 == host)
.map(|e| (e.1, e.2, e.3.clone()));
let sealed = server.and_then(|(pk, cookie, one_shot)| match one_shot {
Some(one_shot) => one_shot.seal(&data),
None => {
let mut seal = ClientSeal::new(&pk, None)?;
seal.set_cookie(cookie);
seal.seal(&data)
}
});
match sealed {
Some(sealed) => sealed,
None => data,
}
}
#[cfg(test)]
mod tests {
use super::*;
use hbb_common::{
rendezvous_proto::{RegisterPeer, RegisterPk},
tokio,
udp_seal::{Reply, ServerKey, CAP_PUSHES_SEALED},
};
// Each test names its own host: the sealed host is process-wide and tests run in parallel.
fn state(host: &str) -> (SealState, ServerKey) {
let (pk, sk) = sign::gen_keypair();
let device = Some(sign::gen_keypair().1);
(
SealState::new(host, &crate::encode64(pk.0), false, device),
ServerKey::new(&sk, [1u8; 32]).unwrap(),
)
}
fn welcomed(state: &mut SealState, server: &ServerKey, caps: u8) {
let hello = state.seal.as_mut().unwrap().seal(b"").unwrap();
let (_, mut reply) = server.open(&hello, || 1).unwrap();
let welcome = server.seal(&mut reply, caps, b"", || 0).unwrap();
assert!(state.open(BytesMut::from(&welcome[..])).unwrap().1);
}
async fn rounds(state: &mut SealState, timeouts: &[bool]) -> Vec<bool> {
let mut socket = FramedSocket::new("127.0.0.1:0").await.unwrap();
let addr = TargetAddr::Ip("127.0.0.1:9".parse().unwrap());
let mut sealed = Vec::new();
for &timed_out in timeouts {
state.on_round(timed_out);
sealed.push(matches!(
state.round_sink(&mut socket, &addr),
Sink::Sealed(..) | Sink::Probe(..)
));
}
sealed
}
fn message(set: impl FnOnce(&mut RendezvousMessage)) -> RendezvousMessage {
let mut msg = RendezvousMessage::new();
set(&mut msg);
msg
}
#[tokio::test]
async fn two_unanswered_hellos_fall_back_and_every_fourth_round_probes() {
let (mut state, _) = state("fall-back");
let timeouts = [false, true, true, false, false, false, false, false];
assert_eq!(
rounds(&mut state, &timeouts).await,
[true, true, false, false, false, true, false, false]
);
}
#[tokio::test]
async fn without_a_key_for_this_server_nothing_is_sealed() {
for key in ["", RS_PUB_KEY] {
let mut state = SealState::new("no-key", key, false, None);
assert_eq!(rounds(&mut state, &[false, true, true]).await, [false; 3]);
let bytes = BytesMut::from(&udp_seal::MAGIC[..]);
assert_eq!(state.open(bytes.clone()), Some((bytes, false)));
}
let mut public = SealState::new("public", RS_PUB_KEY, true, None);
assert_eq!(rounds(&mut public, &[false]).await, [true]);
}
#[test]
fn once_sealed_plaintext_answers_are_dropped_and_pushes_follow_the_server() {
for caps in [0, CAP_PUSHES_SEALED] {
let (mut state, server) = state("answers");
let answer = Some(rendezvous_message::Union::RegisterPkResponse(
Default::default(),
));
let push = Some(rendezvous_message::Union::PunchHole(Default::default()));
welcomed(&mut state, &server, caps);
assert!(state.on_message(true, &answer));
assert!(!state.on_message(false, &answer));
assert_eq!(state.on_message(false, &push), caps == 0);
}
}
#[tokio::test]
async fn while_probing_plaintext_answers_are_forged_but_fallback_takes_them() {
let (mut state, _) = state("probing");
let answer = Some(rendezvous_message::Union::RegisterPkResponse(
Default::default(),
));
let push = Some(rendezvous_message::Union::PunchHole(Default::default()));
assert!(!state.on_message(false, &answer));
assert!(state.on_message(false, &push));
rounds(&mut state, &[false, true, true]).await;
assert!(
state.on_message(false, &answer),
"an older server answers in plaintext"
);
let mut off = SealState::new("probing-off", "", false, None);
assert!(
off.on_message(false, &answer),
"without a key nothing changes"
);
}
#[test]
fn a_register_pk_with_a_new_device_key_proves_it_with_that_key() {
let (pk, sk) = sign::gen_keypair();
let server = ServerKey::new(&sk, [1u8; 32]).unwrap();
let mut seal = ClientSeal::new(&pk, Some(sign::gen_keypair().1)).unwrap();
let (new_pk, new_sk) = sign::gen_keypair();
let register_peer = message(|m| m.set_register_peer(RegisterPeer::default()));
let hello = seal_message(&mut seal, &register_peer, false, || unreachable!()).unwrap();
let (_, mut reply) = server.open(&hello, || 1).unwrap();
seal.open(&server.seal(&mut reply, 0, b"", || 0).unwrap())
.unwrap();
let register_pk = message(|m| {
m.set_register_pk(RegisterPk {
pk: new_pk.0.to_vec().into(),
..Default::default()
})
});
let data = seal_message(&mut seal, &register_pk, false, || Some(new_sk.clone())).unwrap();
let (opened, _) = server.open(&data, || unreachable!()).unwrap();
assert!(opened.proven_by(new_pk.as_ref()));
}
async fn registration(state: &mut SealState, msg: &RendezvousMessage) -> Vec<Vec<u8>> {
let mut socket = FramedSocket::new("127.0.0.1:0").await.unwrap();
let server = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
let addr = TargetAddr::Ip(server.local_addr().unwrap());
state
.round_sink(&mut socket, &addr)
.send(msg)
.await
.unwrap();
let mut buf = [0u8; 2048];
let mut datagrams = Vec::new();
let wait = std::time::Duration::from_millis(100);
while let Ok(Ok(n)) = tokio::time::timeout(wait, server.recv(&mut buf)).await {
datagrams.push(buf[..n].to_vec());
}
datagrams
}
#[tokio::test]
async fn a_missed_answer_while_sealed_retries_under_the_session_and_with_a_hello() {
let (mut state, server) = state("missed");
let register_peer = message(|m| m.set_register_peer(RegisterPeer::default()));
let hello = registration(&mut state, &register_peer).await;
let (_, mut reply) = server.open(&hello[0], || 1).unwrap();
let welcome = server
.seal(&mut reply, CAP_PUSHES_SEALED, b"", || 0)
.unwrap();
assert!(state.open(BytesMut::from(&welcome[..])).unwrap().1);
assert_eq!(registration(&mut state, &register_peer).await.len(), 1);
state.on_round(true);
let retry = registration(&mut state, &register_peer).await;
assert_eq!(retry.len(), 2);
assert!(
server.open(&retry[0], || 2).unwrap().0.pn.is_some(),
"a server that only lost an answer takes it without the hello budget"
);
assert!(
udp_seal::is_hello(&retry[1]),
"a restarted one takes the hello"
);
let punch = Some(rendezvous_message::Union::PunchHole(Default::default()));
assert!(
!state.on_message(false, &punch),
"a plaintext push is still forged"
);
let push = server.seal(&mut Reply::push(1), 0, b"", || 7).unwrap();
assert!(
state.open(BytesMut::from(&push[..])).is_some(),
"a push under the session still opens"
);
}
#[tokio::test]
async fn a_cookie_sends_the_hello_again_and_the_socket_stays_sealed() {
let (mut state, server) = state("cookie");
let register_peer = message(|m| m.set_register_peer(RegisterPeer::default()));
let hello = registration(&mut state, &register_peer).await;
let reply = udp_seal::cookie_reply(&hello[0], [5u8; 16]).unwrap();
let mut socket = FramedSocket::new("127.0.0.1:0").await.unwrap();
let peer = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
let addr = TargetAddr::Ip(peer.local_addr().unwrap());
assert!(state.on_cookie(&reply, &mut socket, &addr).await.unwrap());
let mut buf = [0u8; 2048];
let wait = std::time::Duration::from_millis(500);
let n = tokio::time::timeout(wait, peer.recv(&mut buf))
.await
.unwrap()
.unwrap();
assert_eq!(udp_seal::hello_cookie(&buf[..n]), Some([5u8; 16]));
assert!(server.open(&buf[..n], || 1).is_some());
assert_eq!(
rounds(&mut state, &[true; 4]).await,
[true; 4],
"only a server that seals sends a cookie"
);
let punch = seal_one_shot("cookie", b"\x0a\x00".to_vec());
assert_eq!(udp_seal::hello_cookie(&punch), Some([5u8; 16]));
}
#[tokio::test]
async fn a_fallen_back_probe_registers_in_plaintext_beside_its_hello() {
let (mut state, _) = state("probe-round");
rounds(&mut state, &[false, true, true, false, false, false]).await;
let register_peer = message(|m| m.set_register_peer(RegisterPeer::default()));
let probe = registration(&mut state, &register_peer).await;
assert_eq!(probe.len(), 2);
assert_eq!(
probe[0],
register_peer.write_to_bytes().unwrap(),
"an older server answers it as any other round"
);
assert!(udp_seal::is_hello(&probe[1]));
}
#[tokio::test]
async fn a_rebind_does_not_send_a_socket_that_never_sealed_back_to_probing() {
let (mut state, _) = state("rebind-unsealed");
assert_eq!(rounds(&mut state, &[false, true]).await, [true, true]);
state.on_rebind();
assert_eq!(
rounds(&mut state, &[true, false]).await,
[false, false],
"its two probes went unanswered already"
);
state.on_rebind();
assert_eq!(rounds(&mut state, &[false]).await, [false]);
}
#[tokio::test]
async fn a_socket_that_sealed_does_not_fall_back_until_it_rebinds() {
let (mut state, server) = state("no-fall-back");
welcomed(&mut state, &server, 0);
let answer = Some(rendezvous_message::Union::RegisterPkResponse(
Default::default(),
));
assert_eq!(rounds(&mut state, &[true; 6]).await, [true; 6]);
assert!(
!state.on_message(false, &answer),
"its server seals, so a plaintext answer is forged"
);
state.on_rebind();
assert_eq!(
rounds(&mut state, &[false, true, true]).await,
[true, true, false]
);
assert!(
state.on_message(false, &answer),
"a new socket may reach an older server"
);
}
#[test]
fn a_registration_never_rides_a_hello_and_goes_proven_under_the_session() {
let (pk, sk) = sign::gen_keypair();
let server = ServerKey::new(&sk, [1u8; 32]).unwrap();
let (device_pk, device_sk) = sign::gen_keypair();
let mut seal = ClientSeal::new(&pk, Some(device_sk)).unwrap();
let register_peer = message(|m| m.set_register_peer(RegisterPeer::default()));
let register_pk = message(|m| m.set_register_pk(RegisterPk::default()));
let mut reply = None;
for msg in [&register_peer, &register_pk] {
let hello = seal_message(&mut seal, msg, false, || None).unwrap();
let (opened, r) = server.open(&hello, || 1).unwrap();
assert!(opened.pn.is_none() && opened.payload.is_empty());
reply = Some(r);
}
let welcome = server.seal(&mut reply.unwrap(), 0, b"", || 0).unwrap();
seal.open(&welcome).unwrap();
for msg in [&register_peer, &register_pk] {
let data = seal_message(&mut seal, msg, false, || None).unwrap();
let (opened, _) = server.open(&data, || unreachable!()).unwrap();
assert!(opened.proven_by(device_pk.as_ref()));
assert_eq!(
RendezvousMessage::parse_from_bytes(&opened.payload).unwrap(),
*msg
);
}
}
#[test]
fn a_punch_goes_under_the_session_only_while_its_server_seals() {
let data = b"\x0a\x00".to_vec();
assert_eq!(seal_one_shot("punch", data.clone()), data);
let (mut state, server) = state("punch");
welcomed(&mut state, &server, 0);
let (mut other, other_server) = self::state("punch-other");
welcomed(&mut other, &other_server, 0);
let sealed = seal_one_shot("punch", data.clone());
let (opened, _) = server.open(&sealed, || unreachable!()).unwrap();
assert_eq!((opened.key_id, &opened.payload), (1, &data));
assert_eq!(seal_one_shot("another-host", data.clone()), data);
let hello = state.seal.as_mut().unwrap().hello(b"").unwrap();
let (_, mut reply) = server.open(&hello, || 2).unwrap();
let welcome = server.seal(&mut reply, 0, b"", || 0).unwrap();
state.open(BytesMut::from(&welcome[..])).unwrap();
let sealed = seal_one_shot("punch", data.clone());
assert_eq!(
server.open(&sealed, || unreachable!()).unwrap().0.key_id,
2,
"the session a welcome brought"
);
state.on_round(true);
assert!(
server
.open(&seal_one_shot("punch", data.clone()), || unreachable!())
.is_some(),
"a missed answer is no sign the server stopped sealing"
);
state.on_rebind();
assert_eq!(seal_one_shot("punch", data.clone()), data);
welcomed(&mut state, &server, 0);
state.on_round(true);
drop(state);
assert_eq!(seal_one_shot("punch", data.clone()), data);
}
}