Base encryption solely off legacy login reply

This commit is contained in:
QuantumEntangledAndy committed 2020-10-29 12:52:04 +07:00
1 parent d92cb38ec9
commit 71de7da506
5 files changed
+47 -12

No files matched your search

+2 -2
View File
@@ -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);
+11
View File
@@ -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<u32>,
pub(super) is_encrypted: Arc<AtomicBool>,
}
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<AtomicBool>) {
self.is_encrypted = is_encrypted;
}
pub fn get_encrypted(&self) -> bool {
self.is_encrypted.load(Ordering::Relaxed)
}
}
impl BcHeader {
+16 -8
View File
@@ -10,7 +10,7 @@ use std::io::Write;
pub type Error = GenError;
impl Bc {
pub fn serialize<W: Write>(&self, buf: W) -> Result<W, GenError> {
pub fn serialize<W: Write>(&self, buf: W, encrypted: bool) -> Result<W, GenError> {
// 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<W: Write>(enc_offset: u32, xml: &Extension) -> impl SerializeFn<W> {
fn bc_ext<W: Write>(enc_offset: u32, xml: &Extension, encrypted: bool) -> impl SerializeFn<W> {
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<W: Write>(enc_offset: u32, payload: &BcPayloads) -> impl SerializeFn<W> {
fn bc_payload<W: Write>(enc_offset: u32, payload: &BcPayloads, encrypted: bool) -> impl SerializeFn<W> {
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(),
};
+3
View File
@@ -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:
+15 -2
View File
@@ -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<Mutex<TcpStream>>,
subscribers: Arc<Mutex<BTreeMap<u32, Sender<Bc>>>>,
rx_thread: Option<JoinHandle<()>>,
is_encrypted: Arc<AtomicBool>,
}
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(())
}
}