use shared::error::{Error, Result};
pub const MAX_SRTP_PACKETS_PER_KEY: u64 = 1 << 48;
pub const MAX_SRTCP_PACKETS_PER_KEY: u64 = 1 << 31;
pub const MAX_MKI_LEN: usize = 255;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct KeyDerivationRate(Option<u8>);
impl KeyDerivationRate {
pub const ZERO: Self = Self(None);
pub fn new(rate: u64) -> Result<Self> {
if rate == 0 {
return Ok(Self::ZERO);
}
if !rate.is_power_of_two() || rate > (1 << 24) {
return Err(Error::Other(format!(
"SRTP key derivation rate {rate} is not zero or a power of two up to 2^24"
)));
}
Ok(Self(Some(rate.trailing_zeros() as u8)))
}
pub fn get(self) -> u64 {
self.0.map_or(0, |shift| 1 << shift)
}
pub fn is_zero(self) -> bool {
self.0.is_none()
}
pub(crate) fn epoch(self, index: u64) -> u64 {
self.0.map_or(0, |shift| index >> shift)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Keying {
pub key_derivation_rate: KeyDerivationRate,
pub mki: Option<Vec<u8>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KeyLifetime {
pub srtp_packets: u64,
pub srtcp_packets: u64,
}
pub fn validate_key_lifetime(lifetime: KeyLifetime) -> shared::error::Result<()> {
lifetime.validate()
}
impl KeyLifetime {
pub const MAX: Self = Self {
srtp_packets: MAX_SRTP_PACKETS_PER_KEY,
srtcp_packets: MAX_SRTCP_PACKETS_PER_KEY,
};
pub(crate) fn validate(self) -> Result<()> {
if self.srtp_packets == 0
|| self.srtcp_packets == 0
|| self.srtp_packets > MAX_SRTP_PACKETS_PER_KEY
|| self.srtcp_packets > MAX_SRTCP_PACKETS_PER_KEY
{
return Err(Error::Other(format!(
"SRTP key lifetime {self:?} is outside 1..=2^48 SRTP and 1..=2^31 SRTCP packets"
)));
}
Ok(())
}
pub(crate) fn limit(self, kind: PacketKind) -> u64 {
match kind {
PacketKind::Rtp => self.srtp_packets,
PacketKind::Rtcp => self.srtcp_packets,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PacketKind {
Rtp,
Rtcp,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KeyUsage {
pub srtp_packets: u64,
pub srtcp_packets: u64,
pub lifetime: KeyLifetime,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ContextEvent {
KeyLifetimeExpiring {
mki: Vec<u8>,
kind: PacketKind,
remaining: u64,
},
KeyExhausted {
mki: Vec<u8>,
kind: PacketKind,
},
CryptexRequiredViolation {
ssrc: u32,
},
PacketDiscarded {
kind: PacketKind,
ssrc: u32,
index: u64,
reason: DiscardReason,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DiscardReason {
Replay,
AuthenticationFailed,
}
pub const MAX_PENDING_DISCARD_EVENTS: usize = 64;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn key_derivation_rate_accepts_only_the_rates_the_rfc_allows() {
assert_eq!(KeyDerivationRate::new(0).unwrap(), KeyDerivationRate::ZERO);
assert!(KeyDerivationRate::ZERO.is_zero());
for shift in 0..=24 {
let rate = KeyDerivationRate::new(1 << shift).unwrap();
assert_eq!(rate.get(), 1 << shift);
assert_eq!(rate.epoch(5 << shift), 5);
assert_eq!(rate.epoch((5 << shift) - 1), 4);
}
for bad in [3, 6, 1000, 1 << 25, u64::MAX] {
assert!(KeyDerivationRate::new(bad).is_err(), "{bad}");
}
assert_eq!(KeyDerivationRate::ZERO.epoch(u64::MAX), 0);
}
#[test]
fn key_lifetime_is_bounded_by_the_rfc_maximum() {
KeyLifetime::MAX.validate().unwrap();
for bad in [
KeyLifetime {
srtp_packets: MAX_SRTP_PACKETS_PER_KEY + 1,
..KeyLifetime::MAX
},
KeyLifetime {
srtcp_packets: MAX_SRTCP_PACKETS_PER_KEY + 1,
..KeyLifetime::MAX
},
KeyLifetime {
srtp_packets: 0,
..KeyLifetime::MAX
},
KeyLifetime {
srtcp_packets: 0,
..KeyLifetime::MAX
},
] {
assert!(bad.validate().is_err(), "{bad:?}");
}
}
}