feat(server): protocol v2 with signed handshake, secure voice relay, media broadcast and channel authorization

Revoke voice access on channel delete, kick and permission change, and
acknowledge the sender's own chat messages.
This commit is contained in:
Loki 2026-10-24 16:51:13 +02:00
parent bc5157046e
commit 3e7f5b22dc
Signed by: boba
GPG key ID: 253067914055423B
44 changed files with 2825 additions and 386 deletions

View file

@ -15,6 +15,16 @@ pub(super) fn encrypt_with_key(
cid: &[u8; 8],
seq: u64,
plaintext: &[u8],
) -> Vec<u8> {
encrypt_with_aad(key, cid, seq, plaintext, &[])
}
pub(super) fn encrypt_with_aad(
key: &[u8; 32],
cid: &[u8; 8],
seq: u64,
plaintext: &[u8],
aad: &[u8],
) -> Vec<u8> {
let cipher = ChaCha20Poly1305::new(&Key::from(*key));
let nonce_bytes = make_nonce(cid, seq);
@ -24,7 +34,7 @@ pub(super) fn encrypt_with_key(
&nonce,
Payload {
msg: plaintext,
aad: &[],
aad,
},
)
.expect("ChaCha20-Poly1305 encryption is infallible")
@ -35,6 +45,16 @@ pub(super) fn decrypt_with_key(
cid: &[u8; 8],
seq: u64,
ciphertext: &[u8],
) -> anyhow::Result<Vec<u8>> {
decrypt_with_aad(key, cid, seq, ciphertext, &[])
}
pub(super) fn decrypt_with_aad(
key: &[u8; 32],
cid: &[u8; 8],
seq: u64,
ciphertext: &[u8],
aad: &[u8],
) -> anyhow::Result<Vec<u8>> {
let cipher = ChaCha20Poly1305::new(&Key::from(*key));
let nonce_bytes = make_nonce(cid, seq);
@ -44,7 +64,7 @@ pub(super) fn decrypt_with_key(
&nonce,
Payload {
msg: ciphertext,
aad: &[],
aad,
},
)
.map_err(|e| anyhow::anyhow!("decryption failed: {e:?}"))

View file

@ -13,7 +13,7 @@ pub struct SessionCrypto {
impl SessionCrypto {
pub fn derive(shared_secret: &[u8; 32], session_id: &str) -> Self {
let salt = b"VNOX-LNEx-KDF-v1";
let salt = b"VNOX-LNEx-KDF-v2";
let hk = Hkdf::<Sha256>::new(Some(salt), shared_secret);
let mut keys = [0u8; 64];
@ -42,6 +42,22 @@ impl SessionCrypto {
cipher::decrypt_with_key(&self.c2s_key, &self.cid, seq, ciphertext)
}
#[cfg(test)]
pub(crate) fn encrypt_c2s_frame(&self, seq: u64, plaintext: &[u8], header: &[u8]) -> Vec<u8> {
cipher::encrypt_with_aad(&self.c2s_key, &self.cid, seq, plaintext, header)
}
pub fn encrypt_s2c_frame(&self, seq: u64, plaintext: &[u8], header: &[u8]) -> Vec<u8> {
cipher::encrypt_with_aad(&self.s2c_key, &self.cid, seq, plaintext, header)
}
pub fn decrypt_c2s_frame(
&self,
seq: u64,
ciphertext: &[u8],
header: &[u8],
) -> anyhow::Result<Vec<u8>> {
cipher::decrypt_with_aad(&self.c2s_key, &self.cid, seq, ciphertext, header)
}
pub fn new_ephemeral() -> (EphemeralSecret, PublicKey) {
let mut rng = rand::rng();
let sk = EphemeralSecret::random_from_rng(&mut rng);

View file

@ -1,6 +1,9 @@
#[repr(u16)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PacketId {
WebhookErrorResult = 0x0206,
MediaControl = 0x0180,
MediaEvent = 0x0181,
WebhookCreate = 0x0200,
WebhookList = 0x0201,
WebhookRotate = 0x0202,
@ -12,6 +15,7 @@ pub enum PacketId {
Session = 0x0003,
Ping = 0x0004,
Pong = 0x0005,
VoiceSession = 0x0012,
VoicePacket = 0x0010,
VoiceState = 0x0011,
ChatMessage = 0x0020,
@ -65,6 +69,7 @@ pub enum PacketId {
PresenceUpdate = 0x0140,
PresenceSync = 0x0141,
PresenceEvent = 0x0142,
FriendEvent = 0x0158,
FriendRequest = 0x0150,
FriendAccept = 0x0151,
FriendDecline = 0x0152,
@ -80,6 +85,9 @@ pub enum PacketId {
impl PacketId {
pub fn from_u16(v: u16) -> Option<Self> {
match v {
0x0206 => Some(Self::WebhookErrorResult),
0x0180 => Some(Self::MediaControl),
0x0181 => Some(Self::MediaEvent),
0x0200 => Some(Self::WebhookCreate),
0x0201 => Some(Self::WebhookList),
0x0202 => Some(Self::WebhookRotate),
@ -91,6 +99,7 @@ impl PacketId {
0x0003 => Some(Self::Session),
0x0004 => Some(Self::Ping),
0x0005 => Some(Self::Pong),
0x0012 => Some(Self::VoiceSession),
0x0010 => Some(Self::VoicePacket),
0x0011 => Some(Self::VoiceState),
0x0020 => Some(Self::ChatMessage),
@ -144,6 +153,7 @@ impl PacketId {
0x0140 => Some(Self::PresenceUpdate),
0x0141 => Some(Self::PresenceSync),
0x0142 => Some(Self::PresenceEvent),
0x0158 => Some(Self::FriendEvent),
0x0150 => Some(Self::FriendRequest),
0x0151 => Some(Self::FriendAccept),
0x0152 => Some(Self::FriendDecline),