diff --git a/src/bc/de.rs b/src/bc/de.rs index a5b8de6..8b6aa60 100644 --- a/src/bc/de.rs +++ b/src/bc/de.rs @@ -126,7 +126,7 @@ fn bc_modern_msg<'a, 'b>( let (buf, payload_buf) = take(payload_len)(buf)?; let decrypted; - let processed_ext_buf = if !header.is_encrypted() { + let processed_ext_buf = if !context.get_encrypted() { ext_buf } else { decrypted = xml_crypto::crypt(header.enc_offset, ext_buf); @@ -153,7 +153,7 @@ fn bc_modern_msg<'a, 'b>( if payload_len > 0 { // Extract remainder of message as binary, if it exists let decrypted; - let processed_payload_buf = if !header.is_encrypted() { + let processed_payload_buf = if !context.get_encrypted() { payload_buf } else { decrypted = xml_crypto::crypt(header.enc_offset, payload_buf); diff --git a/src/bc/model.rs b/src/bc/model.rs index 41c43eb..149e76a 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -1,5 +1,6 @@ pub use super::xml::{BcPayloads, BcXml, Extension}; use std::collections::HashSet; +use std::sync::{Arc, atomic::{AtomicBool, Ordering}}; pub(super) const MAGIC_HEADER: u32 = 0xabcdef0; @@ -68,6 +69,7 @@ pub(super) struct BcSendInfo { #[derive(Debug)] pub struct BcContext { pub(super) in_bin_mode: HashSet, + pub(super) is_encrypted: Arc, } impl Bc { @@ -124,8 +126,17 @@ impl BcContext { pub fn new() -> BcContext { BcContext { in_bin_mode: HashSet::new(), + is_encrypted: Default::default(), } } + + pub fn set_encrypted(&mut self, is_encrypted: Arc) { + self.is_encrypted = is_encrypted; + } + + pub fn get_encrypted(&self) -> bool { + self.is_encrypted.load(Ordering::Relaxed) + } } impl BcHeader { diff --git a/src/bc/ser.rs b/src/bc/ser.rs index fc6558e..cef65ea 100644 --- a/src/bc/ser.rs +++ b/src/bc/ser.rs @@ -10,7 +10,7 @@ use std::io::Write; pub type Error = GenError; impl Bc { - pub fn serialize(&self, buf: W) -> Result { + pub fn serialize(&self, buf: W, encrypted: bool) -> Result { // Ideally this would be a combinator, but that would be hairy because we have to // serialize the XML to have the metadata to build the header let body_buf; @@ -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)), + opt_ref(&modern.extension, |ext| bc_ext(self.meta.client_idx, 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) + bc_payload(self.meta.client_idx, payload_offset, encrypted) }), temp_buf, )?; @@ -62,17 +62,25 @@ impl Bc { } } -fn bc_ext(enc_offset: u32, xml: &Extension) -> impl SerializeFn { +fn bc_ext(enc_offset: u32, xml: &Extension, encrypted: bool) -> impl SerializeFn { let xml_bytes = xml.serialize(vec![]).unwrap(); - let enc_bytes = xml_crypto::crypt(enc_offset, &xml_bytes); - slice(enc_bytes) + if encrypted { + let enc_bytes = xml_crypto::crypt(enc_offset, &xml_bytes); + slice(enc_bytes) + } else { + slice(xml_bytes) + } } -fn bc_payload(enc_offset: u32, payload: &BcPayloads) -> impl SerializeFn { +fn bc_payload(enc_offset: u32, payload: &BcPayloads, encrypted: bool) -> impl SerializeFn { let payload_bytes = match payload { BcPayloads::BcXml(x) => { let xml_bytes = x.serialize(vec![]).unwrap(); - xml_crypto::crypt(enc_offset, &xml_bytes) + if encrypted { + xml_crypto::crypt(enc_offset, &xml_bytes) + } else { + xml_bytes + } } BcPayloads::Binary(x) => x.to_owned(), }; diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index e55e93c..f31bf0a 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -139,6 +139,9 @@ impl BcCamera { })?; let nonce; + if let BcMeta {encrypted: true,..} = legacy_reply.meta { + connection.set_encrypted(true); + } match legacy_reply.body { BcBody::ModernMsg(ModernMsg { payload: diff --git a/src/bc_protocol/connection.rs b/src/bc_protocol/connection.rs index 7003b6a..8d29504 100644 --- a/src/bc_protocol/connection.rs +++ b/src/bc_protocol/connection.rs @@ -7,7 +7,7 @@ use std::collections::btree_map::Entry; use std::collections::BTreeMap; use std::net::{Shutdown, SocketAddr, TcpStream}; use std::sync::mpsc::{channel, Receiver, Sender}; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, atomic::{AtomicBool, Ordering}}; use std::thread::JoinHandle; use std::time::Duration; @@ -20,6 +20,7 @@ pub struct BcConnection { connection: Arc>, subscribers: Arc>>>, rx_thread: Option>, + is_encrypted: Arc, } pub struct BcSubscription<'a> { @@ -53,8 +54,11 @@ impl BcConnection { let mut subs = subscribers.clone(); let conn = tcp_conn.try_clone()?; + let is_encrypted = Arc::new(AtomicBool::new(false)); + let connections_isencrypted = is_encrypted.clone(); let rx_thread = std::thread::spawn(move || { let mut context = BcContext::new(); + context.set_encrypted(connections_isencrypted); while BcConnection::poll(&mut context, &conn, &mut subs).is_ok() {} }); @@ -62,6 +66,7 @@ impl BcConnection { connection: Arc::new(Mutex::new(tcp_conn)), subscribers, rx_thread: Some(rx_thread), + is_encrypted, }) } @@ -110,6 +115,14 @@ impl BcConnection { Ok(()) } + + pub fn set_encrypted(&self, value: bool) { + self.is_encrypted.store(value, Ordering::Relaxed); + } + + pub fn get_encrypted(&self) -> bool { + self.is_encrypted.load(Ordering::Relaxed) + } } impl Drop for BcConnection { @@ -136,7 +149,7 @@ impl<'a> BcSubscription<'a> { pub fn send(&self, bc: Bc) -> Result<()> { assert!(bc.meta.msg_id == self.msg_id); - bc.serialize(&*self.conn.connection.lock().unwrap())?; + bc.serialize(&*self.conn.connection.lock().unwrap(), self.conn.get_encrypted())?; Ok(()) } }