use crypto::{
AeadAlgorithm, BlockCipherAlgorithm, CryptoAlgorithm, HmacAlgorithm, RTCCrypto,
StreamCipherAlgorithm,
};
use shared::error::{Error, Result};
use crate::key_derivation::Prf;
use crate::keying::KeyLifetime;
const AES_128_CM_REQUIREMENTS: &[CryptoAlgorithm] = &[
CryptoAlgorithm::BlockCipher(BlockCipherAlgorithm::Aes128),
CryptoAlgorithm::StreamCipher(StreamCipherAlgorithm::Aes128Ctr),
CryptoAlgorithm::Hmac(HmacAlgorithm::Sha1),
];
const AES_256_CM_REQUIREMENTS: &[CryptoAlgorithm] = &[
CryptoAlgorithm::BlockCipher(BlockCipherAlgorithm::Aes256),
CryptoAlgorithm::StreamCipher(StreamCipherAlgorithm::Aes256Ctr),
CryptoAlgorithm::Hmac(HmacAlgorithm::Sha1),
];
const AES_192_CM_REQUIREMENTS: &[CryptoAlgorithm] = &[
CryptoAlgorithm::BlockCipher(BlockCipherAlgorithm::Aes192),
CryptoAlgorithm::StreamCipher(StreamCipherAlgorithm::Aes192Ctr),
CryptoAlgorithm::Hmac(HmacAlgorithm::Sha1),
];
const NULL_REQUIREMENTS: &[CryptoAlgorithm] = &[
CryptoAlgorithm::BlockCipher(BlockCipherAlgorithm::Aes128),
CryptoAlgorithm::Hmac(HmacAlgorithm::Sha1),
];
const AEAD_AES_128_GCM_REQUIREMENTS: &[CryptoAlgorithm] = &[
CryptoAlgorithm::BlockCipher(BlockCipherAlgorithm::Aes128),
CryptoAlgorithm::StreamCipher(StreamCipherAlgorithm::Aes128Ctr),
CryptoAlgorithm::Aead(AeadAlgorithm::Aes128Gcm),
];
const AEAD_AES_256_GCM_REQUIREMENTS: &[CryptoAlgorithm] = &[
CryptoAlgorithm::BlockCipher(BlockCipherAlgorithm::Aes256),
CryptoAlgorithm::StreamCipher(StreamCipherAlgorithm::Aes256Ctr),
CryptoAlgorithm::Aead(AeadAlgorithm::Aes256Gcm),
];
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
#[non_exhaustive]
pub enum ProtectionProfile {
#[default]
Aes128CmHmacSha1_80 = 0x0001,
Aes128CmHmacSha1_32 = 0x0002,
Aes256CmHmacSha1_80 = 0x0003,
Aes256CmHmacSha1_32 = 0x0004,
NullHmacSha1_80 = 0x0005,
NullHmacSha1_32 = 0x0006,
AeadAes128Gcm = 0x0007,
AeadAes256Gcm = 0x0008,
Aes192CmHmacSha1_80 = 0x00F1,
Aes192CmHmacSha1_32 = 0x00F2,
}
impl ProtectionProfile {
#[must_use]
pub const fn required_crypto_algorithms(self) -> &'static [CryptoAlgorithm] {
match self {
Self::Aes128CmHmacSha1_32 | Self::Aes128CmHmacSha1_80 => AES_128_CM_REQUIREMENTS,
Self::Aes256CmHmacSha1_32 | Self::Aes256CmHmacSha1_80 => AES_256_CM_REQUIREMENTS,
Self::Aes192CmHmacSha1_32 | Self::Aes192CmHmacSha1_80 => AES_192_CM_REQUIREMENTS,
Self::NullHmacSha1_32 | Self::NullHmacSha1_80 => NULL_REQUIREMENTS,
Self::AeadAes128Gcm => AEAD_AES_128_GCM_REQUIREMENTS,
Self::AeadAes256Gcm => AEAD_AES_256_GCM_REQUIREMENTS,
}
}
pub fn ensure_crypto_supported(self, crypto: &dyn RTCCrypto) -> Result<()> {
for algorithm in self.required_crypto_algorithms() {
if !crypto.supports(*algorithm) {
return Err(Error::Crypto(format!(
"SRTP protection profile {self:?} requires unsupported algorithm {algorithm:?}"
)));
}
}
Ok(())
}
pub const ALL: [ProtectionProfile; 10] = [
Self::Aes128CmHmacSha1_80,
Self::Aes128CmHmacSha1_32,
Self::Aes192CmHmacSha1_80,
Self::Aes192CmHmacSha1_32,
Self::Aes256CmHmacSha1_80,
Self::Aes256CmHmacSha1_32,
Self::NullHmacSha1_80,
Self::NullHmacSha1_32,
Self::AeadAes128Gcm,
Self::AeadAes256Gcm,
];
#[must_use]
pub const fn dtls_srtp_id(self) -> Option<u16> {
match self {
Self::Aes128CmHmacSha1_80 => Some(0x0001),
Self::Aes128CmHmacSha1_32 => Some(0x0002),
Self::NullHmacSha1_80 => Some(0x0005),
Self::NullHmacSha1_32 => Some(0x0006),
Self::AeadAes128Gcm => Some(0x0007),
Self::AeadAes256Gcm => Some(0x0008),
Self::Aes192CmHmacSha1_80
| Self::Aes192CmHmacSha1_32
| Self::Aes256CmHmacSha1_80
| Self::Aes256CmHmacSha1_32 => None,
}
}
#[must_use]
pub const fn from_dtls_srtp_id(id: u16) -> Option<Self> {
match id {
0x0001 => Some(Self::Aes128CmHmacSha1_80),
0x0002 => Some(Self::Aes128CmHmacSha1_32),
0x0005 => Some(Self::NullHmacSha1_80),
0x0006 => Some(Self::NullHmacSha1_32),
0x0007 => Some(Self::AeadAes128Gcm),
0x0008 => Some(Self::AeadAes256Gcm),
_ => None,
}
}
#[must_use]
pub const fn is_null_cipher(self) -> bool {
matches!(self, Self::NullHmacSha1_80 | Self::NullHmacSha1_32)
}
#[must_use]
pub const fn is_aead(self) -> bool {
matches!(self, Self::AeadAes128Gcm | Self::AeadAes256Gcm)
}
pub(crate) const fn is_aes_cm_or_null(self) -> bool {
!self.is_aead()
}
pub(crate) const fn prf(self) -> Prf {
match self {
Self::Aes128CmHmacSha1_80
| Self::Aes128CmHmacSha1_32
| Self::NullHmacSha1_80
| Self::NullHmacSha1_32
| Self::AeadAes128Gcm => Prf::Aes128,
Self::Aes192CmHmacSha1_80 | Self::Aes192CmHmacSha1_32 => Prf::Aes192,
Self::Aes256CmHmacSha1_80 | Self::Aes256CmHmacSha1_32 | Self::AeadAes256Gcm => {
Prf::Aes256
}
}
}
#[must_use]
pub const fn default_key_lifetime(self) -> KeyLifetime {
match self {
Self::Aes192CmHmacSha1_80
| Self::Aes192CmHmacSha1_32
| Self::Aes256CmHmacSha1_80
| Self::Aes256CmHmacSha1_32 => KeyLifetime {
srtp_packets: 1 << 31,
srtcp_packets: 1 << 31,
},
_ => KeyLifetime::MAX,
}
}
pub fn key_len(&self) -> usize {
self.prf().key_len()
}
pub fn salt_len(&self) -> usize {
if self.is_aead() { 12 } else { 14 }
}
pub fn rtp_auth_tag_len(&self) -> usize {
match *self {
ProtectionProfile::Aes128CmHmacSha1_80
| ProtectionProfile::Aes192CmHmacSha1_80
| ProtectionProfile::Aes256CmHmacSha1_80
| ProtectionProfile::NullHmacSha1_80 => 10,
ProtectionProfile::Aes128CmHmacSha1_32
| ProtectionProfile::Aes192CmHmacSha1_32
| ProtectionProfile::Aes256CmHmacSha1_32
| ProtectionProfile::NullHmacSha1_32 => 4,
ProtectionProfile::AeadAes128Gcm | ProtectionProfile::AeadAes256Gcm => 0,
}
}
pub fn rtcp_auth_tag_len(&self) -> usize {
if self.is_aead() { 0 } else { 10 }
}
pub fn aead_auth_tag_len(&self) -> usize {
if self.is_aead() { 16 } else { 0 }
}
pub fn auth_key_len(&self) -> usize {
if self.is_aead() { 0 } else { 20 }
}
}
#[cfg(test)]
mod tests {
use super::*;
struct CapabilityCrypto {
missing: Option<CryptoAlgorithm>,
}
impl RTCCrypto for CapabilityCrypto {
fn supports(&self, algorithm: CryptoAlgorithm) -> bool {
self.missing != Some(algorithm)
}
}
const PROFILES: [ProtectionProfile; 10] = ProtectionProfile::ALL;
#[test]
fn complete_provider_supports_every_profile() {
let crypto = CapabilityCrypto { missing: None };
for profile in PROFILES {
profile.ensure_crypto_supported(&crypto).unwrap();
}
}
#[test]
fn every_required_capability_is_enforced() {
for profile in PROFILES {
for algorithm in profile.required_crypto_algorithms() {
let crypto = CapabilityCrypto {
missing: Some(*algorithm),
};
let error = profile.ensure_crypto_supported(&crypto).unwrap_err();
let message = error.to_string();
assert!(message.contains(&format!("{profile:?}")));
assert!(message.contains(&format!("{algorithm:?}")));
}
}
}
}