feat(client): protocol v2 wire layer, screen sharing, channel webhooks and secure voice
Verified against the real gateway via tests/gateway_interop.rs.
This commit is contained in:
parent
8460fb28fd
commit
3d7dd5413b
65 changed files with 6048 additions and 1156 deletions
|
|
@ -1,7 +1,6 @@
|
|||
use anyhow::{Result, anyhow};
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
use super::super::crypto::SessionCrypto;
|
||||
use super::super::payloads::HDR_SIZE;
|
||||
|
|
@ -11,7 +10,10 @@ use super::parse_header;
|
|||
const ENCRYPTED_FLAG: u16 = 1 << 1;
|
||||
const KNOWN_FLAGS: u16 = ENCRYPTED_FLAG;
|
||||
|
||||
pub async fn read_encrypted(stream: &mut TcpStream, crypto: &SessionCrypto) -> Result<RawPacket> {
|
||||
pub async fn read_encrypted(
|
||||
stream: &mut (impl tokio::io::AsyncRead + Unpin),
|
||||
crypto: &SessionCrypto,
|
||||
) -> Result<RawPacket> {
|
||||
let mut buf = [0u8; HDR_SIZE];
|
||||
stream.read_exact(&mut buf).await?;
|
||||
let (packet_id, flags, sequence, payload_length) = parse_header(&buf)?;
|
||||
|
|
@ -25,27 +27,160 @@ pub async fn read_encrypted(stream: &mut TcpStream, crypto: &SessionCrypto) -> R
|
|||
if !encrypted.is_empty() {
|
||||
stream.read_exact(&mut encrypted).await?;
|
||||
}
|
||||
let payload = crypto.decrypt_s2c(sequence as u64, &encrypted)?;
|
||||
Ok(RawPacket { packet_id, payload })
|
||||
let payload = crypto.decrypt_s2c_aad(sequence as u64, &encrypted, &buf)?;
|
||||
Ok(RawPacket {
|
||||
packet_id,
|
||||
sequence,
|
||||
payload,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn write_encrypted(
|
||||
stream: &mut TcpStream,
|
||||
stream: &mut (impl tokio::io::AsyncWrite + Unpin),
|
||||
id: u16,
|
||||
seq: &mut u32,
|
||||
payload: &[u8],
|
||||
crypto: &SessionCrypto,
|
||||
) -> Result<()> {
|
||||
let encrypted = crypto.encrypt_c2s(*seq as u64, payload);
|
||||
if *seq == u32::MAX {
|
||||
return Err(anyhow!("session sequence exhausted; reconnect required"));
|
||||
}
|
||||
if payload.len() > super::super::payloads::MAX_PAYLOAD as usize - 16 {
|
||||
return Err(anyhow!("payload too large"));
|
||||
}
|
||||
let mut hdr = [0u8; HDR_SIZE];
|
||||
hdr[0..2].copy_from_slice(&id.to_be_bytes());
|
||||
hdr[2..4].copy_from_slice(&ENCRYPTED_FLAG.to_be_bytes());
|
||||
hdr[4..8].copy_from_slice(&seq.to_be_bytes());
|
||||
hdr[8..12].copy_from_slice(&(encrypted.len() as u32).to_be_bytes());
|
||||
*seq = seq.wrapping_add(1);
|
||||
hdr[8..12].copy_from_slice(&((payload.len() + 16) as u32).to_be_bytes());
|
||||
let encrypted = crypto.encrypt_c2s_aad(*seq as u64, payload, &hdr);
|
||||
*seq += 1;
|
||||
let mut frame = Vec::with_capacity(HDR_SIZE + encrypted.len());
|
||||
frame.extend_from_slice(&hdr);
|
||||
frame.extend_from_slice(&encrypted);
|
||||
stream.write_all(&frame).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A dedicated reader retains partial frames across command/timer selection.
|
||||
/// Dropping a session cancels its reader and releases the socket half.
|
||||
pub struct Reader {
|
||||
receiver: tokio::sync::mpsc::Receiver<Result<RawPacket>>,
|
||||
task: tokio::task::JoinHandle<()>,
|
||||
}
|
||||
impl Reader {
|
||||
pub fn new(
|
||||
mut stream: impl tokio::io::AsyncRead + Unpin + Send + 'static,
|
||||
crypto: SessionCrypto,
|
||||
) -> Self {
|
||||
let (sender, receiver) = tokio::sync::mpsc::channel(32);
|
||||
let task = tokio::spawn(async move {
|
||||
let mut expected = 2u32;
|
||||
loop {
|
||||
let result = read_encrypted(&mut stream, &crypto)
|
||||
.await
|
||||
.and_then(|packet| {
|
||||
if packet.sequence != expected {
|
||||
return Err(anyhow!("replayed or reordered control packet"));
|
||||
}
|
||||
expected = expected
|
||||
.checked_add(1)
|
||||
.ok_or_else(|| anyhow!("session sequence exhausted"))?;
|
||||
Ok(packet)
|
||||
});
|
||||
let failed = result.is_err();
|
||||
if sender.send(result).await.is_err() || failed {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
Self { receiver, task }
|
||||
}
|
||||
pub async fn next(&mut self) -> Result<RawPacket> {
|
||||
self.receiver
|
||||
.recv()
|
||||
.await
|
||||
.ok_or_else(|| anyhow!("gateway reader stopped"))?
|
||||
}
|
||||
}
|
||||
impl Drop for Reader {
|
||||
fn drop(&mut self) {
|
||||
self.task.abort();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use hkdf::Hkdf;
|
||||
use sha2::{Digest, Sha256};
|
||||
use tokio::time::{Duration, timeout};
|
||||
|
||||
fn server_frame(pid: u16, seq: u32, payload: &[u8]) -> Vec<u8> {
|
||||
let mut keys = [0u8; 64];
|
||||
Hkdf::<Sha256>::new(Some(b"VNOX-LNEx-KDF-v2"), &[7; 32])
|
||||
.expand(b"session-keys", &mut keys)
|
||||
.unwrap();
|
||||
let key: [u8; 32] = keys[32..].try_into().unwrap();
|
||||
let cid: [u8; 8] = Sha256::digest(b"fixture-session")[..8].try_into().unwrap();
|
||||
let mut header = [0u8; 12];
|
||||
header[..2].copy_from_slice(&pid.to_be_bytes());
|
||||
header[2..4].copy_from_slice(&ENCRYPTED_FLAG.to_be_bytes());
|
||||
header[4..8].copy_from_slice(&seq.to_be_bytes());
|
||||
header[8..].copy_from_slice(&((payload.len() + 16) as u32).to_be_bytes());
|
||||
let body =
|
||||
crate::net::crypto::cipher::encrypt_with_aad(&key, &cid, seq as u64, payload, &header);
|
||||
[header.as_slice(), body.as_slice()].concat()
|
||||
}
|
||||
fn crypto() -> SessionCrypto {
|
||||
SessionCrypto::derive(&[7; 32], "fixture-session")
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cancelling_wait_retains_partial_control_frame() {
|
||||
let (reader, mut writer) = tokio::io::duplex(256);
|
||||
let mut reader = Reader::new(reader, crypto());
|
||||
let frame = server_frame(5, 2, b"protobuf");
|
||||
writer.write_all(&frame[..5]).await.unwrap();
|
||||
assert!(
|
||||
timeout(Duration::from_millis(5), reader.next())
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
writer.write_all(&frame[5..]).await.unwrap();
|
||||
let packet = timeout(Duration::from_secs(1), reader.next())
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(packet.payload, b"protobuf");
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn replayed_control_sequence_is_rejected() {
|
||||
let (reader, mut writer) = tokio::io::duplex(256);
|
||||
let mut reader = Reader::new(reader, crypto());
|
||||
let frame = server_frame(5, 2, b"");
|
||||
writer.write_all(&frame).await.unwrap();
|
||||
reader.next().await.unwrap();
|
||||
writer.write_all(&frame).await.unwrap();
|
||||
assert!(reader.next().await.is_err());
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn changing_packet_id_invalidates_authentication() {
|
||||
let (mut reader, mut writer) = tokio::io::duplex(256);
|
||||
let mut frame = server_frame(5, 2, b"same body");
|
||||
frame[1] ^= 1;
|
||||
writer.write_all(&frame).await.unwrap();
|
||||
assert!(read_encrypted(&mut reader, &crypto()).await.is_err());
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn sequence_exhaustion_never_wraps_nonce() {
|
||||
let (_, mut writer) = tokio::io::duplex(256);
|
||||
let mut seq = u32::MAX;
|
||||
assert!(
|
||||
write_encrypted(&mut writer, 4, &mut seq, b"", &crypto())
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(seq, u32::MAX);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue