From a3aa3c45564feea9b68142f19626bc5927f3625b Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Fri, 24 Feb 2023 07:23:06 +0700 Subject: [PATCH] Handle aes encrypt during deser too --- crates/core/src/bc/codex.rs | 10 +++++++++- crates/core/src/bc/de.rs | 8 +++++++- crates/core/src/bc/model.rs | 2 +- crates/core/src/bc_protocol/connection/bcsub.rs | 1 - 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/core/src/bc/codex.rs b/crates/core/src/bc/codex.rs index 3cc7821..b19decb 100644 --- a/crates/core/src/bc/codex.rs +++ b/crates/core/src/bc/codex.rs @@ -27,7 +27,15 @@ impl Encoder for BcCodex { fn encode(&mut self, item: Bc, dst: &mut BytesMut) -> Result<()> { // let context = self.context.read().unwrap(); let buf: Vec = Default::default(); - let buf = item.serialize(buf, self.context.get_encrypted())?; + let enc_protocol: EncryptionProtocol = match self.context.get_encrypted() { + EncryptionProtocol::Aes(_) 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 + } + n => *n, + }; + let buf = item.serialize(buf, &enc_protocol)?; dst.reserve(buf.len()); dst.extend_from_slice(buf.as_slice()); Ok(()) diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index 4f2e839..2029f6a 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -160,7 +160,13 @@ fn bc_modern_msg<'a>( response_code, .. } if (response_code & 0xff) == 0x02 => 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 - _ => context.get_encrypted().clone(), + 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 + } + _ => *context.get_encrypted(), }; let processed_payload_buf = diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 2ade671..46ee38a 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -169,7 +169,7 @@ pub(super) struct BcSendInfo { /// These are the encyption modes supported by the camera /// /// The mode is negotiated during login -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub enum EncryptionProtocol { /// Older camera use no encryption Unencrypted, diff --git a/crates/core/src/bc_protocol/connection/bcsub.rs b/crates/core/src/bc_protocol/connection/bcsub.rs index 279038e..9e54186 100644 --- a/crates/core/src/bc_protocol/connection/bcsub.rs +++ b/crates/core/src/bc_protocol/connection/bcsub.rs @@ -50,7 +50,6 @@ impl<'a> Stream for BcPayloadStream<'a> { ) -> Poll>>> { match Pin::new(&mut self.rx).poll_next(cx) { Poll::Ready(Some(bc)) => { - log::info!("Got Bc Packet"); match bc { Bc { body: