From 6e1cf2f0200a1ac1e53c1a4d307b69f915fd0453 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 16 Nov 2020 09:23:44 +0700 Subject: [PATCH] Work on switching encoffset with channelid and msg_mum --- src/bc/de.rs | 13 ++++++++----- src/bc/model.rs | 28 ++++++++++++++-------------- src/bc/ser.rs | 12 ++++++++---- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/bc/de.rs b/src/bc/de.rs index 8b6aa60..1ce15db 100644 --- a/src/bc/de.rs +++ b/src/bc/de.rs @@ -129,7 +129,7 @@ fn bc_modern_msg<'a, 'b>( let processed_ext_buf = if !context.get_encrypted() { ext_buf } else { - decrypted = xml_crypto::crypt(header.enc_offset, ext_buf); + decrypted = xml_crypto::crypt(header.channel_id as u32, ext_buf); &decrypted }; @@ -156,7 +156,7 @@ fn bc_modern_msg<'a, 'b>( let processed_payload_buf = if !context.get_encrypted() { payload_buf } else { - decrypted = xml_crypto::crypt(header.enc_offset, payload_buf); + decrypted = xml_crypto::crypt(header.channel_id as u32, payload_buf); &decrypted }; if let Ok(xml) = BcXml::try_parse(processed_payload_buf) { @@ -175,7 +175,9 @@ fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> { let (buf, _magic) = verify(le_u32, |x| *x == MAGIC_HEADER)(buf)?; let (buf, msg_id) = le_u32(buf)?; let (buf, body_len) = le_u32(buf)?; - let (buf, enc_offset) = le_u32(buf)?; + let (buf, channel_id) = le_u8(buf)?; + let (buf, stream_type) = le_u8(buf)?; + let (buf, msg_num) = le_u16(buf)?; let (buf, (response_code, _ignored, class)) = tuple((le_u8, le_u8, le_u16))(buf)?; // All modern messages are encrypted. In addition, it seems that the camera firmware checks @@ -190,8 +192,9 @@ fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> { BcHeader { msg_id, body_len, - enc_offset, - encrypted, + channel_id, + stream_type, + msg_num, class, payload_offset, }, diff --git a/src/bc/model.rs b/src/bc/model.rs index 149e76a..a508192 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -42,8 +42,9 @@ pub enum LegacyMsg { pub(super) struct BcHeader { pub body_len: u32, pub msg_id: u32, - pub enc_offset: u32, - pub encrypted: bool, + pub channel_id: u8, + pub stream_type: u8, + pub msg_num: u16, pub class: u16, pub payload_offset: Option, } @@ -53,9 +54,10 @@ pub(super) struct BcHeader { #[derive(Debug, PartialEq, Eq)] pub struct BcMeta { pub msg_id: u32, - pub client_idx: u32, + pub channel_id: u8, + pub stream_type: u8, + pub msg_num: u16, pub class: u16, - pub encrypted: bool, } /// The components of the Baichuan header that must be filled out after the body is serialized, or @@ -142,28 +144,25 @@ impl BcContext { impl BcHeader { pub fn is_modern(&self) -> bool { // Most modern messages have an extra word at the end of the header; this - // serves as the start offset of the appended binary data, if any. + // serves as the start offset of the appended payload data, if any. // A notable exception is the encrypted reply to the login message; // in this case the message is modern (with XML encryption etc), but there is // no extra word. // Here are the message classes: // 0x6514: legacy, no bin offset (initial login message, encrypted or not) // 0x6614: modern, no bin offset (reply to encrypted 0x6514 login) - // 0x6414: modern, has bin offset, always encrypted (re-sent login message) + // 0x6414: modern, has bin offset, encrypted if supported (re-sent login message) // 0x0000, modern, has bin offset (most modern messages) self.class != 0x6514 } - pub fn is_encrypted(&self) -> bool { - self.encrypted || self.class == 0x6414 - } - pub fn to_meta(&self) -> BcMeta { BcMeta { msg_id: self.msg_id, - client_idx: self.enc_offset, + msg_num: self.msg_num, + channel_id: self.channel_id, + stream_type: self.stream_type, class: self.class, - encrypted: self.encrypted, } } @@ -172,9 +171,10 @@ impl BcHeader { payload_offset, body_len, msg_id: meta.msg_id, - enc_offset: meta.client_idx, + channel_id: meta.channel_id, + stream_type: meta.stream_type, + msg_num: meta.msg_num, class: meta.class, - encrypted: meta.encrypted, } } } diff --git a/src/bc/ser.rs b/src/bc/ser.rs index cef65ea..3b6df92 100644 --- a/src/bc/ser.rs +++ b/src/bc/ser.rs @@ -20,7 +20,7 @@ impl Bc { BcBody::ModernMsg(ref modern) => { // First serialize ext let (temp_buf, ext_len) = gen( - opt_ref(&modern.extension, |ext| bc_ext(self.meta.client_idx, ext, encrypted)), + opt_ref(&modern.extension, |ext| bc_ext(self.meta.channel_id as u32, ext, encrypted)), vec![], )?; @@ -39,7 +39,7 @@ impl Bc { // Now get the payload part of the body and add to ext_buf let (temp_buf, _) = gen( opt_ref(&modern.payload, |payload_offset| { - bc_payload(self.meta.client_idx, payload_offset, encrypted) + bc_payload(self.meta.channel_id as u32, payload_offset, encrypted) }), temp_buf, )?; @@ -92,13 +92,17 @@ fn bc_header(header: &BcHeader) -> impl SerializeFn { let (signaled_encryption, spare) = if header.class == 0x0000 { (0x00, 0x00) } else { - (header.encrypted as u8, 0xdc) + // Client is always 01dc + // Camera may reply with 01dd (encrypted) or 00dd (unencrypted) + (0x01, 0xdc) }; tuple(( le_u32(MAGIC_HEADER), le_u32(header.msg_id), le_u32(header.body_len), - le_u32(header.enc_offset), + le_u8(header.channel_id), + le_u8(header.stream_type), + le_u16(header.msg_num), le_u8(signaled_encryption), le_u8(spare), // skipped byte le_u16(header.class),