Refractor to apply ext xml before payload_offset

This commit is contained in:
QuantumEntangledAndy authored and Andrew committed 2020-10-15 11:03:19 +07:00
1 parent 31aefdbc74
commit d0f760ea7b
5 files changed
+57 -68

No files matched your search

+15 -15
View File
@@ -1,5 +1,5 @@
use super::model::*; use super::model::*;
use super::xml::{BcPayloads, BcXml, BcXmls}; use super::xml::{BcPayloads, BcXml};
use super::xml_crypto; use super::xml_crypto;
use err_derive::Error; use err_derive::Error;
use log::*; use log::*;
@@ -116,33 +116,33 @@ fn bc_modern_msg<'a, 'b>(
Err, Err,
}; };
let xml_len = match header.payload_offset { let ext_len = match header.payload_offset {
Some(off) => off, Some(off) => off,
_ => header.body_len, _ => 0, // If missing payload_offset treat all as payload
}; };
let (buf, xml_buf) = take(xml_len)(buf)?; let (buf, ext_buf) = take(ext_len)(buf)?;
let payload_len = header.body_len - xml_len; let payload_len = header.body_len - ext_len;
let (buf, payload_buf) = take(payload_len)(buf)?; let (buf, payload_buf) = take(payload_len)(buf)?;
let decrypted; let decrypted;
let processed_xml_buf = if !header.is_encrypted() { let processed_ext_buf = if !header.is_encrypted() {
xml_buf ext_buf
} else { } else {
decrypted = xml_crypto::crypt(header.enc_offset, xml_buf); decrypted = xml_crypto::crypt(header.enc_offset, ext_buf);
&decrypted &decrypted
}; };
// Now we'll take the buffer that Nom gave a ref to and parse it. // Now we'll take the buffer that Nom gave a ref to and parse it.
let xml; let extension;
if xml_len > 0 { if ext_len > 0 {
// Apply the XML parse function, but throw away the reference to decrypted in the Ok and // Apply the XML parse function, but throw away the reference to decrypted in the Ok and
// Err case. This error-error-error thing is the same idiom Nom uses internally. // Err case. This error-error-error thing is the same idiom Nom uses internally.
let parsed = BcXmls::try_parse(processed_xml_buf) let parsed = Extension::try_parse(processed_ext_buf)
.map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?; .map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?;
xml = Some(parsed); extension = Some(parsed);
} else { } else {
xml = None; extension = None;
} }
// Now to handle the payload block // Now to handle the payload block
@@ -154,7 +154,7 @@ fn bc_modern_msg<'a, 'b>(
// Extract remainder of message as binary, if it exists // Extract remainder of message as binary, if it exists
let decrypted; let decrypted;
let processed_payload_buf = if !header.is_encrypted() { let processed_payload_buf = if !header.is_encrypted() {
xml_buf payload_buf
} else { } else {
decrypted = xml_crypto::crypt(header.enc_offset, payload_buf); decrypted = xml_crypto::crypt(header.enc_offset, payload_buf);
&decrypted &decrypted
@@ -168,7 +168,7 @@ fn bc_modern_msg<'a, 'b>(
payload = None; payload = None;
} }
Ok((buf, ModernMsg { xml, payload })) Ok((buf, ModernMsg { extension, payload }))
} }
fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> { fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> {
+17 -7
View File
@@ -1,4 +1,4 @@
pub use super::xml::{BcXml, Extension, BcXmls, BcPayloads}; pub use super::xml::{BcPayloads, BcXml, Extension};
use std::collections::HashSet; use std::collections::HashSet;
pub(super) const MAGIC_HEADER: u32 = 0xabcdef0; pub(super) const MAGIC_HEADER: u32 = 0xabcdef0;
@@ -27,7 +27,7 @@ pub enum BcBody {
#[derive(Debug, Default, PartialEq, Eq)] #[derive(Debug, Default, PartialEq, Eq)]
pub struct ModernMsg { pub struct ModernMsg {
pub xml: Option<BcXmls>, pub extension: Option<Extension>,
pub payload: Option<BcPayloads>, pub payload: Option<BcPayloads>,
} }
@@ -77,17 +77,17 @@ impl Bc {
Bc { Bc {
meta, meta,
body: BcBody::ModernMsg(ModernMsg { body: BcBody::ModernMsg(ModernMsg {
xml: Some(BcXmls::BcXml(xml)), extension: None,
payload: None, payload: Some(BcPayloads::BcXml(xml)),
}), }),
} }
} }
pub fn new_from_ext_xml(meta: BcMeta, xml: Extension) -> Bc { pub fn new_from_ext(meta: BcMeta, xml: Extension) -> Bc {
Bc { Bc {
meta, meta,
body: BcBody::ModernMsg(ModernMsg { body: BcBody::ModernMsg(ModernMsg {
xml: Some(BcXmls::Extension(xml)), extension: Some(xml),
payload: None, payload: None,
}), }),
} }
@@ -97,11 +97,21 @@ impl Bc {
Bc { Bc {
meta, meta,
body: BcBody::ModernMsg(ModernMsg { body: BcBody::ModernMsg(ModernMsg {
xml: None, extension: None,
payload: None, payload: None,
}), }),
} }
} }
pub fn new_from_ext_ext(meta: BcMeta, ext: Extension, xml: BcXml) -> Bc {
Bc {
meta,
body: BcBody::ModernMsg(ModernMsg {
extension: Some(ext),
payload: Some(BcPayloads::BcXml(xml)),
}),
}
}
} }
impl Default for BcContext { impl Default for BcContext {
+22 -22
View File
@@ -1,5 +1,5 @@
use super::model::*; use super::model::*;
use super::xml::{BcXmls, BcPayloads}; use super::xml::{BcPayloads};
use super::xml_crypto; use super::xml_crypto;
use cookie_factory::bytes::*; use cookie_factory::bytes::*;
use cookie_factory::sequence::tuple; use cookie_factory::sequence::tuple;
@@ -15,55 +15,55 @@ impl Bc {
// serialize the XML to have the metadata to build the header // serialize the XML to have the metadata to build the header
let body_buf; let body_buf;
let payload_offset; let payload_offset;
match &self.body { match &self.body {
BcBody::ModernMsg(ref modern) => { BcBody::ModernMsg(ref modern) => {
let (buf, xml_len) = gen( // First serialize ext
opt_ref(&modern.xml, |xml| bc_xml(self.meta.client_idx, xml)), let (temp_buf, ext_len) = gen(
opt_ref(&modern.extension, |ext| bc_ext(self.meta.client_idx, ext)),
vec![], vec![],
)?; )?;
body_buf = buf;
// Now get the offset of the payload
payload_offset = if has_payload_offset(self.meta.class) { payload_offset = if has_payload_offset(self.meta.class) {
// If we're required to put binary length, put 0 if we have no binary // If we're required to put binary length, put 0 if we have no binary
Some(if modern.payload.is_some() { Some(if modern.payload.is_some() {
xml_len as u32 ext_len as u32
} else { } else {
0 0
}) })
} else { } else {
None None
}; };
}
// 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)),
temp_buf,
)?;
body_buf = temp_buf;
},
BcBody::LegacyMsg(ref legacy) => { BcBody::LegacyMsg(ref legacy) => {
let (buf, _) = gen(bc_legacy(legacy), vec![])?; let (buf, _) = gen(bc_legacy(legacy), vec![])?;
body_buf = buf; body_buf = buf;
payload_offset = None; payload_offset = None;
} }
} };
// Now have enough info to create the header // Now have enough info to create the header
let header = BcHeader::from_meta(&self.meta, body_buf.len() as u32, payload_offset); let header = BcHeader::from_meta(&self.meta, body_buf.len() as u32, payload_offset);
let (mut buf, _n) = gen(tuple((bc_header(&header), slice(body_buf))), buf)?; let (buf, _n) = gen(tuple((bc_header(&header), slice(body_buf))), buf)?;
// Put the binary part of the body, TODO this is poorly written
if let BcBody::ModernMsg(ModernMsg {
payload: Some(ref payload),
..
}) = self.body
{
let (buf2, _) = gen(bc_payload(header.enc_offset, payload), buf)?;
buf = buf2;
}
Ok(buf) Ok(buf)
} }
} }
fn bc_xml<W: Write>(enc_offset: u32, xml: &BcXmls) -> impl SerializeFn<W> { fn bc_ext<W: Write>(enc_offset: u32, xml: &Extension) -> impl SerializeFn<W> {
let xml_bytes = match xml { let xml_bytes = xml.serialize(vec![]).unwrap();
BcXmls::BcXml(x) => x.serialize(vec![]).unwrap(),
BcXmls::Extension(x) => x.serialize(vec![]).unwrap(),
};
let enc_bytes = xml_crypto::crypt(enc_offset, &xml_bytes); let enc_bytes = xml_crypto::crypt(enc_offset, &xml_bytes);
slice(enc_bytes) slice(enc_bytes)
} }
-21
View File
@@ -9,21 +9,6 @@ use yaserde_derive::{YaDeserialize, YaSerialize};
#[cfg(test)] #[cfg(test)]
use indoc::indoc; use indoc::indoc;
#[derive(PartialEq, Eq, Debug, YaDeserialize)]
#[yaserde(flatten)]
pub enum BcXmls {
#[yaserde(rename = "body")]
BcXml(BcXml),
Extension(Extension),
}
// Required for YaDeserialize
impl Default for BcXmls {
fn default() -> Self {
BcXmls::BcXml(Default::default())
}
}
#[derive(PartialEq, Eq, Debug, YaDeserialize)] #[derive(PartialEq, Eq, Debug, YaDeserialize)]
#[yaserde(flatten)] #[yaserde(flatten)]
pub enum BcPayloads { pub enum BcPayloads {
@@ -59,12 +44,6 @@ pub struct BcXml {
pub norm: Option<Norm>, pub norm: Option<Norm>,
} }
impl BcXmls {
pub fn try_parse(s: impl Read) -> Result<Self, String> {
yaserde::de::from_reader(s)
}
}
impl BcXml { impl BcXml {
pub fn try_parse(s: impl Read) -> Result<Self, String> { pub fn try_parse(s: impl Read) -> Result<Self, String> {
yaserde::de::from_reader(s) yaserde::de::from_reader(s)
+3 -3
View File
@@ -143,8 +143,8 @@ impl BcCamera {
let nonce; let nonce;
match legacy_reply.body { match legacy_reply.body {
BcBody::ModernMsg(ModernMsg { BcBody::ModernMsg(ModernMsg {
xml: payload:
Some(BcXmls::BcXml(BcXml { Some(BcPayloads::BcXml(BcXml {
encryption: Some(encryption), encryption: Some(encryption),
.. ..
}), }),
@@ -215,7 +215,7 @@ impl BcCamera {
device_info = info; device_info = info;
} }
BcBody::ModernMsg(ModernMsg { BcBody::ModernMsg(ModernMsg {
xml: None, extension: None,
payload: None, payload: None,
}) => return Err(Error::AuthFailed), }) => return Err(Error::AuthFailed),
_ => { _ => {