[domistream] Implementation: HTTP, pairing, RTSP/SDP, ENet, control, input, RTP, FEC, video and audio receivers
Per PROTOCOL.md and the Wolf docs; interop with a real host is unverified. Strict, capped parsers throughout, shared helpers in util.rs. Where PROTOCOL.md says UNKNOWN the Wolf-documented behaviour is used. The audio receiver waits for the jitter budget before rebuilding a missing packet, so a late packet beats a rebuilt one.
This commit is contained in:
@@ -8,7 +8,11 @@
|
||||
|
||||
use core::fmt;
|
||||
|
||||
use crate::Result;
|
||||
use aes_gcm::aead::{AeadInOut, KeyInit};
|
||||
use aes_gcm::Aes128Gcm;
|
||||
|
||||
use crate::util::{array, cap, malformed, u16_le, u32_le};
|
||||
use crate::{Error, Result};
|
||||
|
||||
/// Type field of an encrypted frame.
|
||||
pub const ENCRYPTED_FRAME_TYPE: u16 = 0x0001;
|
||||
@@ -35,10 +39,20 @@ pub enum Direction {
|
||||
/// The nonce for sequence number `sequence` in `direction`.
|
||||
///
|
||||
/// # Errors
|
||||
/// Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// None today.
|
||||
pub fn nonce(sequence: u32, direction: Direction) -> Result<[u8; NONCE_LEN]> {
|
||||
let _ = (sequence, direction);
|
||||
Err(crate::Error::Unimplemented)
|
||||
let mut n = [0u8; NONCE_LEN];
|
||||
let [a, b, c, d] = sequence.to_le_bytes();
|
||||
n[0] = a;
|
||||
n[1] = b;
|
||||
n[2] = c;
|
||||
n[3] = d;
|
||||
n[10] = match direction {
|
||||
Direction::ClientToHost => b'C',
|
||||
Direction::HostToClient => b'H',
|
||||
};
|
||||
n[11] = b'C';
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
/// A frame split into its fields, not yet decrypted.
|
||||
@@ -55,45 +69,78 @@ pub struct EncryptedFrame<'a> {
|
||||
/// Splits a frame (one ENet packet) into its fields. The length field must match the bytes present exactly.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::TooLarge`] over [`MAX_FRAME_BYTES`], [`crate::Error::Truncated`] under [`MIN_FRAME_BYTES`],
|
||||
/// [`crate::Error::UnexpectedValue`] for a type other than [`ENCRYPTED_FRAME_TYPE`], [`crate::Error::Malformed`]
|
||||
/// for a length that does not match. Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// [`Error::TooLarge`] over [`MAX_FRAME_BYTES`], [`Error::Truncated`] under [`MIN_FRAME_BYTES`],
|
||||
/// [`Error::UnexpectedValue`] for a type other than [`ENCRYPTED_FRAME_TYPE`], [`Error::Malformed`] for a length
|
||||
/// that does not match.
|
||||
pub fn parse_encrypted_frame(frame: &[u8]) -> Result<EncryptedFrame<'_>> {
|
||||
let _ = frame;
|
||||
Err(crate::Error::Unimplemented)
|
||||
const W: &str = "control frame";
|
||||
cap(W, MAX_FRAME_BYTES, frame.len())?;
|
||||
crate::util::need(W, MIN_FRAME_BYTES, frame.len())?;
|
||||
let kind = u16_le(W, frame, 0)?;
|
||||
if kind != ENCRYPTED_FRAME_TYPE {
|
||||
return Err(Error::UnexpectedValue { what: "control frame type", value: kind.into() });
|
||||
}
|
||||
if usize::from(u16_le(W, frame, 2)?) != frame.len() - 4 {
|
||||
return Err(malformed("control frame length"));
|
||||
}
|
||||
Ok(EncryptedFrame {
|
||||
sequence: u32_le(W, frame, 4)?,
|
||||
tag: array::<GCM_TAG_LEN>(W, frame, 8)?,
|
||||
ciphertext: frame.get(FRAME_HEADER_LEN..).unwrap_or(&[]),
|
||||
})
|
||||
}
|
||||
|
||||
/// AES-128-GCM with the session's `rikey`. `Debug` never prints the key.
|
||||
#[derive(Clone)]
|
||||
pub struct ControlCipher {
|
||||
key: [u8; 16],
|
||||
key: zeroize::Zeroizing<[u8; 16]>,
|
||||
}
|
||||
|
||||
impl ControlCipher {
|
||||
/// A cipher for `rikey`.
|
||||
#[must_use]
|
||||
pub const fn new(rikey: [u8; 16]) -> Self {
|
||||
Self { key: rikey }
|
||||
pub fn new(rikey: [u8; 16]) -> Self {
|
||||
Self { key: zeroize::Zeroizing::new(rikey) }
|
||||
}
|
||||
|
||||
fn aead(&self) -> Aes128Gcm {
|
||||
Aes128Gcm::new((&*self.key).into())
|
||||
}
|
||||
|
||||
/// Encrypts one message (plaintext = type, length, body) into a whole frame.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::TooLarge`] when the frame would exceed [`MAX_FRAME_BYTES`]. Phase A: always
|
||||
/// [`crate::Error::Unimplemented`].
|
||||
/// [`Error::TooLarge`] when the frame would exceed [`MAX_FRAME_BYTES`]; [`Error::Crypto`] if sealing fails.
|
||||
pub fn seal(&self, sequence: u32, direction: Direction, plaintext: &[u8]) -> Result<Vec<u8>> {
|
||||
let _ = (sequence, direction, plaintext, &self.key);
|
||||
Err(crate::Error::Unimplemented)
|
||||
cap("control frame", MAX_FRAME_BYTES, FRAME_HEADER_LEN.saturating_add(plaintext.len()))?;
|
||||
let n = nonce(sequence, direction)?;
|
||||
let mut body = plaintext.to_vec();
|
||||
let tag = self
|
||||
.aead()
|
||||
.encrypt_inout_detached(&n.into(), b"", body.as_mut_slice().into())
|
||||
.map_err(|_| Error::Crypto)?;
|
||||
let len = u16::try_from(4 + GCM_TAG_LEN + body.len()).map_err(|_| malformed("control frame length"))?;
|
||||
let mut out = Vec::with_capacity(FRAME_HEADER_LEN + body.len());
|
||||
out.extend_from_slice(&ENCRYPTED_FRAME_TYPE.to_le_bytes());
|
||||
out.extend_from_slice(&len.to_le_bytes());
|
||||
out.extend_from_slice(&sequence.to_le_bytes());
|
||||
out.extend_from_slice(tag.as_slice());
|
||||
out.extend_from_slice(&body);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Checks and decrypts one frame; returns its sequence number and the plaintext message.
|
||||
///
|
||||
/// # Errors
|
||||
/// Parser errors as [`parse_encrypted_frame`]; [`crate::Error::Crypto`] when the tag does not verify. Phase A:
|
||||
/// always [`crate::Error::Unimplemented`].
|
||||
/// Parser errors as [`parse_encrypted_frame`]; [`Error::Crypto`] when the tag does not verify.
|
||||
pub fn open(&self, direction: Direction, frame: &[u8]) -> Result<(u32, Vec<u8>)> {
|
||||
let _ = (direction, frame);
|
||||
Err(crate::Error::Unimplemented)
|
||||
let f = parse_encrypted_frame(frame)?;
|
||||
let n = nonce(f.sequence, direction)?;
|
||||
let mut body = f.ciphertext.to_vec();
|
||||
self.aead()
|
||||
.decrypt_inout_detached(&n.into(), b"", body.as_mut_slice().into(), &f.tag.into())
|
||||
.map_err(|_| Error::Crypto)?;
|
||||
Ok((f.sequence, body))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user