1 use super::*; 2 use crate::error::Result; 3 use util::marshal::*; 4 5 use bytes::Bytes; 6 7 impl Context { 8 /// DecryptRTCP decrypts a RTCP packet with an encrypted payload decrypt_rtcp(&mut self, encrypted: &[u8]) -> Result<Bytes>9 pub fn decrypt_rtcp(&mut self, encrypted: &[u8]) -> Result<Bytes> { 10 let mut buf = encrypted; 11 rtcp::header::Header::unmarshal(&mut buf)?; 12 13 let index = self.cipher.get_rtcp_index(encrypted); 14 let ssrc = u32::from_be_bytes([encrypted[4], encrypted[5], encrypted[6], encrypted[7]]); 15 16 { 17 if let Some(state) = self.get_srtcp_ssrc_state(ssrc) { 18 if let Some(replay_detector) = &mut state.replay_detector { 19 if !replay_detector.check(index as u64) { 20 return Err(Error::SrtcpSsrcDuplicated(ssrc, index)); 21 } 22 } 23 } else { 24 return Err(Error::SsrcMissingFromSrtcp(ssrc)); 25 } 26 } 27 28 let dst = self.cipher.decrypt_rtcp(encrypted, index, ssrc)?; 29 30 { 31 if let Some(state) = self.get_srtcp_ssrc_state(ssrc) { 32 if let Some(replay_detector) = &mut state.replay_detector { 33 replay_detector.accept(); 34 } 35 } 36 } 37 38 Ok(dst) 39 } 40 41 /// EncryptRTCP marshals and encrypts an RTCP packet, writing to the dst buffer provided. 42 /// If the dst buffer does not have the capacity to hold `len(plaintext) + 14` bytes, a new one will be allocated and returned. encrypt_rtcp(&mut self, decrypted: &[u8]) -> Result<Bytes>43 pub fn encrypt_rtcp(&mut self, decrypted: &[u8]) -> Result<Bytes> { 44 let mut buf = decrypted; 45 rtcp::header::Header::unmarshal(&mut buf)?; 46 47 let ssrc = u32::from_be_bytes([decrypted[4], decrypted[5], decrypted[6], decrypted[7]]); 48 49 let index; 50 { 51 if let Some(state) = self.get_srtcp_ssrc_state(ssrc) { 52 state.srtcp_index += 1; 53 if state.srtcp_index > MAX_SRTCP_INDEX { 54 state.srtcp_index = 0; 55 } 56 index = state.srtcp_index; 57 } else { 58 return Err(Error::SsrcMissingFromSrtcp(ssrc)); 59 } 60 } 61 62 self.cipher.encrypt_rtcp(decrypted, index, ssrc) 63 } 64 } 65