diff --git a/crates/core/src/bc/codex.rs b/crates/core/src/bc/codex.rs index e137808..8c0d3aa 100644 --- a/crates/core/src/bc/codex.rs +++ b/crates/core/src/bc/codex.rs @@ -29,7 +29,9 @@ impl Encoder for BcCodex { // let context = self.context.read().unwrap(); let buf: Vec = Default::default(); let enc_protocol: EncryptionProtocol = match self.context.get_encrypted() { - EncryptionProtocol::Aes(_) if item.meta.msg_id == 1 => { + EncryptionProtocol::Aes(_) | EncryptionProtocol::FullAes(_) + if item.meta.msg_id == 1 => + { // During login the encyption protocol cannot go higher than BCEncrypt // even if we support AES. (BUt it can go lower i.e. None) EncryptionProtocol::BCEncrypt @@ -110,7 +112,10 @@ impl Decoder for BcCodex { match encryption_protocol_byte { 0x00 => self.context.set_encrypted(EncryptionProtocol::Unencrypted), 0x01 => self.context.set_encrypted(EncryptionProtocol::BCEncrypt), - 0x02 | 0x12 => self.context.set_encrypted(EncryptionProtocol::Aes( + 0x02 => self.context.set_encrypted(EncryptionProtocol::Aes( + self.context.credentials.make_aeskey(nonce), + )), + 0x12 => self.context.set_encrypted(EncryptionProtocol::FullAes( self.context.credentials.make_aeskey(nonce), )), _ => { diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index 931524a..acb67bd 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -149,24 +149,23 @@ fn bc_modern_msg<'a>( msg_id: 1, response_code, .. - } if (response_code & 0xff) == 0x00 => EncryptionProtocol::Unencrypted, - BcHeader { - msg_id: 1, - response_code, - .. - } if (response_code & 0xff) == 0x01 => EncryptionProtocol::BCEncrypt, - BcHeader { - msg_id: 1, - response_code, - .. - } if (response_code & 0xff) == 0x02 || (response_code & 0xff) == 0x12 => { - EncryptionProtocol::BCEncrypt - } // This is AES but the first packet with the NONCE is BCEcrypt, since the NONCE in this packet is required to build the AES key - BcHeader { msg_id: 1, .. } - if matches!(context.get_encrypted(), EncryptionProtocol::Aes(_)) => - { - // Maximum encryption level is BCEncrypt during login... Not sure why Reolink did it like that - EncryptionProtocol::BCEncrypt + } if (response_code >> 8) & 0xff == 0xdd => { + // 0xdd means we are setting the encryption method + // Durig login, the max encryption is BcEncrypt since + // the nonce has not been exchanged yet + match response_code & 0xff { + 0x00 => EncryptionProtocol::Unencrypted, + _ => EncryptionProtocol::BCEncrypt, + } + } + BcHeader { msg_id: 1, .. } => { + match *context.get_encrypted() { + EncryptionProtocol::Aes(_) | EncryptionProtocol::FullAes(_) => { + // During login max is BcEncrypt + EncryptionProtocol::BCEncrypt + } + n => n, + } } _ => *context.get_encrypted(), }; @@ -174,7 +173,12 @@ fn bc_modern_msg<'a>( let processed_payload_buf = xml_crypto::decrypt(header.channel_id as u32, payload_buf, &encryption_protocol); if context.in_bin_mode.contains(&(header.msg_num)) || in_binary { - payload = Some(BcPayloads::Binary(payload_buf.to_vec())); + payload = match context.get_encrypted() { + EncryptionProtocol::FullAes(_) => { + Some(BcPayloads::Binary(processed_payload_buf.to_vec())) + } + _ => Some(BcPayloads::Binary(payload_buf.to_vec())), + }; } else { let xml = BcXml::try_parse(processed_payload_buf.as_slice()).map_err(|_| { error!("header.msg_id: {}", header.msg_id); diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index d65650d..50e9483 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -200,6 +200,9 @@ pub enum EncryptionProtocol { /// Latest cameras/firmwares use Aes with the key derived from /// the camera's password and the negotiated NONCE Aes([u8; 16]), + /// Same as Aes but the media stream is also encrypted and not just + /// the control commands + FullAes([u8; 16]), } #[derive(Debug)] diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index 7c15cb2..0772660 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -259,6 +259,12 @@ pub struct Extension { /// The rfID used in the PIR #[yaserde(rename = "rfId")] pub rf_id: Option, + /// Encrypted binary has this to verify successful decryption + #[yaserde(rename = "checkPos")] + pub check_pos: Option, + /// Encrypted binary has this to verify successful decryption + #[yaserde(rename = "checkValue")] + pub check_value: Option, } impl Default for Extension { @@ -270,6 +276,8 @@ impl Default for Extension { token: None, channel_id: None, rf_id: None, + check_pos: None, + check_value: None, } } } diff --git a/crates/core/src/bc/xml_crypto.rs b/crates/core/src/bc/xml_crypto.rs index 0c3063b..bf0f086 100644 --- a/crates/core/src/bc/xml_crypto.rs +++ b/crates/core/src/bc/xml_crypto.rs @@ -21,7 +21,7 @@ pub fn decrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol .map(|(key, i)| *i ^ key ^ (offset as u8)) .collect() } - EncryptionProtocol::Aes(aeskey) => { + EncryptionProtocol::Aes(aeskey) | EncryptionProtocol::FullAes(aeskey) => { // AES decryption let mut decrypted = buf.to_vec(); @@ -41,7 +41,7 @@ pub fn encrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol // Encrypt is the same as decrypt decrypt(offset, buf, encryption_protocol) } - EncryptionProtocol::Aes(aeskey) => { + EncryptionProtocol::Aes(aeskey) | EncryptionProtocol::FullAes(aeskey) => { // AES encryption let mut encrypted = buf.to_vec(); Aes128CfbEnc::new(aeskey.into(), IV.into()).encrypt(&mut encrypted);