webrtc_dtls/extension/
extension_use_srtp.rs

1#[cfg(test)]
2mod extension_use_srtp_test;
3
4use super::*;
5
6// SRTPProtectionProfile defines the parameters and options that are in effect for the SRTP processing
7/// ## Specifications
8///
9/// * [RFC 5764 §4.1.2]
10///
11/// [RFC 5764 §4.1.2]: https://tools.ietf.org/html/rfc5764#section-4.1.2
12#[allow(non_camel_case_types)]
13#[derive(Copy, Clone, Debug, PartialEq, Eq)]
14pub enum SrtpProtectionProfile {
15    Srtp_Aes128_Cm_Hmac_Sha1_80 = 0x0001,
16    Srtp_Aes128_Cm_Hmac_Sha1_32 = 0x0002,
17    Srtp_Aead_Aes_128_Gcm = 0x0007,
18    Srtp_Aead_Aes_256_Gcm = 0x0008,
19    Unsupported,
20}
21
22impl From<u16> for SrtpProtectionProfile {
23    fn from(val: u16) -> Self {
24        match val {
25            0x0001 => SrtpProtectionProfile::Srtp_Aes128_Cm_Hmac_Sha1_80,
26            0x0002 => SrtpProtectionProfile::Srtp_Aes128_Cm_Hmac_Sha1_32,
27            0x0007 => SrtpProtectionProfile::Srtp_Aead_Aes_128_Gcm,
28            0x0008 => SrtpProtectionProfile::Srtp_Aead_Aes_256_Gcm,
29            _ => SrtpProtectionProfile::Unsupported,
30        }
31    }
32}
33
34const EXTENSION_USE_SRTPHEADER_SIZE: usize = 6;
35
36/// ## Specifications
37///
38/// * [RFC 8422]
39///
40/// [RFC 8422]: https://tools.ietf.org/html/rfc8422
41#[allow(non_camel_case_types)]
42#[derive(Clone, Debug, PartialEq, Eq)]
43pub struct ExtensionUseSrtp {
44    pub(crate) protection_profiles: Vec<SrtpProtectionProfile>,
45}
46
47impl ExtensionUseSrtp {
48    pub fn extension_value(&self) -> ExtensionValue {
49        ExtensionValue::UseSrtp
50    }
51
52    pub fn size(&self) -> usize {
53        2 + 2 + self.protection_profiles.len() * 2 + 1
54    }
55
56    pub fn marshal<W: Write>(&self, writer: &mut W) -> Result<()> {
57        writer.write_u16::<BigEndian>(
58            2 + /* MKI Length */ 1 + 2 * self.protection_profiles.len() as u16,
59        )?;
60        writer.write_u16::<BigEndian>(2 * self.protection_profiles.len() as u16)?;
61        for v in &self.protection_profiles {
62            writer.write_u16::<BigEndian>(*v as u16)?;
63        }
64
65        /* MKI Length */
66        writer.write_u8(0x00)?;
67
68        Ok(writer.flush()?)
69    }
70
71    pub fn unmarshal<R: Read>(reader: &mut R) -> Result<Self> {
72        let _ = reader.read_u16::<BigEndian>()?;
73
74        let profile_count = reader.read_u16::<BigEndian>()? as usize / 2;
75        let mut protection_profiles = vec![];
76        for _ in 0..profile_count {
77            let protection_profile = reader.read_u16::<BigEndian>()?.into();
78            protection_profiles.push(protection_profile);
79        }
80
81        /* MKI Length */
82        let _ = reader.read_u8()?;
83
84        Ok(ExtensionUseSrtp {
85            protection_profiles,
86        })
87    }
88}