diff --git a/src/bc/bc_xml.rs b/src/bc/bc_xml.rs index ae6f4ee..f748e08 100644 --- a/src/bc/bc_xml.rs +++ b/src/bc/bc_xml.rs @@ -9,19 +9,28 @@ use yaserde::{YaDeserialize, YaSerialize}; #[allow(pub_use_of_private_extern_crate)] use yaserde::log; +#[cfg(test)] +use indoc::indoc; + #[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)] #[yaserde(rename="body")] -struct Body { +pub struct Body { #[yaserde(rename="Encryption")] - encryption: Option, + pub encryption: Option, #[yaserde(rename="LoginUser")] - login_user: Option, + pub login_user: Option, #[yaserde(rename="LoginNet")] - login_net: Option, + pub login_net: Option, +} + +impl Body { + pub fn try_parse(s: impl Read) -> Result { + yaserde::de::from_reader(s) + } } #[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)] -struct Encryption { +pub struct Encryption { #[yaserde(attribute)] pub version: String, #[yaserde(rename="type")] @@ -31,7 +40,7 @@ struct Encryption { #[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)] #[yaserde(rename_all="camelCase")] -struct LoginUser { +pub struct LoginUser { #[yaserde(attribute)] pub version: String, #[yaserde(rename="userName")] @@ -43,7 +52,7 @@ struct LoginUser { #[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)] #[yaserde(rename_all="camelCase")] -struct LoginNet { +pub struct LoginNet { #[yaserde(attribute)] pub version: String, #[yaserde(rename="type")] diff --git a/src/bc/mod.rs b/src/bc/mod.rs index f906a24..ea377d9 100644 --- a/src/bc/mod.rs +++ b/src/bc/mod.rs @@ -1,6 +1,5 @@ -#[allow(dead_code)] // while I'm still fleshing this out - mod model; mod bc_xml; +mod xml_crypto; pub use model::Bc; diff --git a/src/bc/model.rs b/src/bc/model.rs index 20a2b9a..fb5bdc8 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -1,11 +1,145 @@ -pub enum Bc { +use nom::{IResult, error::ParseError}; +use nom::{bytes::complete::take, number::complete::*, combinator::*, sequence::*}; +use super::bc_xml::Body; +use super::xml_crypto; + +const MAGIC_HEADER: u32 = 0xabcdef0; + +const MSG_ID_LOGIN: u32 = 1; +const MSG_ID_VIDEO: u32 = 3; + +pub struct Bc { + header: BcHeader, + body: BcBody, +} + +pub enum BcBody { LegacyMsg(LegacyMsg), ModernMsg(ModernMsg), - Unknown, } pub struct ModernMsg { - + xml: Option, + binary: Option>, } -pub enum LegacyMsg {} +pub enum LegacyMsg { + LoginMsg { + username: String, + password: String, + } +} + +struct BcHeader { + body_len: u32, + msg_id: u32, + enc_offset: u32, + encrypted: bool, + class: u16, + bin_offset: Option, +} + +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. + // 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) + // 0x0000, modern, has bin offset (most modern messages) + self.class != 0x6514 + } + + pub fn is_encrypted(&self) -> bool { + self.encrypted || self.class == 0x6414 + } +} + +fn bc_msg(buf: &[u8]) -> IResult<&[u8], Bc> { + let (buf, header) = bc_header(buf)?; + let (buf, msg) = bc_modern_msg(&header, buf)?; + + let bc = Bc { + header, + body: BcBody::ModernMsg(msg), + }; + + Ok((buf, bc)) +} + +fn bc_modern_msg<'a, 'b, E: ParseError<&'b [u8]>>(header: &'a BcHeader, buf: &'b [u8]) -> IResult<&'b [u8], ModernMsg, E> { + let end_of_xml = header.bin_offset.unwrap_or(buf.len() as u32); + + let (mut buf, body_buf) = take(end_of_xml)(buf)?; + + let decrypted; + let processed_body_buf = if !header.is_encrypted() { buf } else { + decrypted = xml_crypto::crypt(header.enc_offset, body_buf); + &decrypted + }; + + // Apply the 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. + use nom::{Err, error::{make_error, ErrorKind}}; + let body = Body::try_parse(processed_body_buf) + .map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?; + + // Extract remainder of message as binary, if it exists + let mut binary = None; + if let Some(bin_offset) = header.bin_offset { + let (buf_after, payload) = map(take(header.body_len - bin_offset), |x: &[u8]| x.to_vec())(buf)?; + binary = Some(payload); + buf = buf_after; + } + + let msg = ModernMsg { + xml: Some(body), + binary, + }; + + Ok((buf, msg)) +} + +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, (encrypted, _ignored, class)) = tuple((le_u8, le_u8, le_u16))(buf)?; + let encrypted = encrypted != 0; + + // See BcHeader::is_modern() for a description of which packets have the bin offset + let has_bin_offset = class == 0x6414 || class == 0x0000; + let (buf, bin_offset) = cond(has_bin_offset, le_u32)(buf)?; + + Ok((buf, BcHeader { + msg_id, + body_len, + enc_offset, + encrypted, + class, + bin_offset, + })) +} + +#[test] +fn test_bc_modern_login() { + let sample = include_bytes!("samples/model_sample_modern_login.bin"); + + let (_, msg) = bc_msg(&sample[..]).unwrap(); + assert_eq!(msg.header.msg_id, 1); + assert_eq!(msg.header.body_len, 145); + assert_eq!(msg.header.enc_offset, 0x1000000); + assert_eq!(msg.header.encrypted, true); + assert_eq!(msg.header.class, 0x6614); + match msg.body { + BcBody::ModernMsg(ModernMsg{ xml: Some(xml), binary: None }) => { + assert_eq!(xml.encryption.unwrap().nonce, "9E6D1FCB9E69846D") + } + _ => assert!(false), + } +} diff --git a/src/bc/samples/model_sample_modern_login.bin b/src/bc/samples/model_sample_modern_login.bin new file mode 100644 index 0000000..dcc4861 Binary files /dev/null and b/src/bc/samples/model_sample_modern_login.bin differ diff --git a/src/bc/samples/xml_crypto_sample1.bin b/src/bc/samples/xml_crypto_sample1.bin new file mode 100644 index 0000000..cf13985 Binary files /dev/null and b/src/bc/samples/xml_crypto_sample1.bin differ diff --git a/src/bc/samples/xml_crypto_sample1_plaintext.bin b/src/bc/samples/xml_crypto_sample1_plaintext.bin new file mode 100644 index 0000000..a11c6f8 --- /dev/null +++ b/src/bc/samples/xml_crypto_sample1_plaintext.bin @@ -0,0 +1,7 @@ + + + +md5 +9E6D1FCB9E69846D + + diff --git a/src/bc/xml_crypto.rs b/src/bc/xml_crypto.rs new file mode 100644 index 0000000..06bbd63 --- /dev/null +++ b/src/bc/xml_crypto.rs @@ -0,0 +1,15 @@ +const XML_KEY: [u8; 8] = [0x1F, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78, 0xFF]; + +pub fn crypt(offset: u32, buf: &[u8]) -> Vec { + let key_iter = XML_KEY.iter().cycle().skip(offset as usize % 8); + key_iter.zip(buf).map(|(key, i)| *i ^ key ^ (offset as u8)).collect() +} + +#[test] +fn test_xml_crypto() { + let sample = include_bytes!("samples/xml_crypto_sample1.bin"); + let should_be = include_bytes!("samples/xml_crypto_sample1_plaintext.bin"); + + let decrypted = crypt(0, &sample[..]); + assert_eq!(decrypted, &should_be[..]); +} diff --git a/src/main.rs b/src/main.rs index dedea24..a0d333e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ -#[cfg(test)] #[macro_use] -extern crate indoc; +#![allow(dead_code)] // while I'm still fleshing this out mod bc;