diff --git a/src/bc/de.rs b/src/bc/de.rs index 0a4c4b9..b0bcd99 100644 --- a/src/bc/de.rs +++ b/src/bc/de.rs @@ -128,11 +128,8 @@ fn bc_modern_msg<'a, 'b>( match encryption_protocol_byte { 0x00 => context.set_encrypted(EncryptionProtocol::Unencrypted), 0x01 => context.set_encrypted(EncryptionProtocol::BCEncrypt), - 0x03 => context.set_encrypted(EncryptionProtocol::Aes(AesKey { - nonce: None, - passwd: None, - })), - _ => context.set_encrypted(EncryptionProtocol::Unknown), + 0x03 => context.set_encrypted(EncryptionProtocol::Aes(None)), + _ => return Err(Err::Error(make_error(buf, ErrorKind::MapRes))), } } @@ -144,7 +141,7 @@ fn bc_modern_msg<'a, 'b>( let processed_ext_buf = match context.get_encrypted() { EncryptionProtocol::Unencrypted => ext_buf, encryption_protocol => { - decrypted = xml_crypto::crypt(header.channel_id as u32, ext_buf, encryption_protocol); + decrypted = xml_crypto::crypt(header.channel_id as u32, ext_buf, &encryption_protocol); &decrypted } }; @@ -170,7 +167,7 @@ fn bc_modern_msg<'a, 'b>( // Extract remainder of message as binary, if it exists let encryption_protocol = context.get_encrypted(); let processed_payload_buf = - xml_crypto::crypt(header.channel_id as u32, payload_buf, encryption_protocol); + xml_crypto::crypt(header.channel_id as u32, payload_buf, &encryption_protocol); if let Ok(xml) = BcXml::try_parse(processed_payload_buf.as_slice()) { payload = Some(BcPayloads::BcXml(xml)); } else { diff --git a/src/bc/model.rs b/src/bc/model.rs index 1d766e7..de842aa 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -71,33 +71,11 @@ pub(super) struct BcSendInfo { pub payload_offset: Option, } -#[derive(Debug, Clone)] -pub struct AesKey { - pub nonce: Option, - pub passwd: Option, -} - -impl AesKey { - pub fn get_key(&self) -> Option> { - if let (Some(nonce), Some(passwd)) = (&self.nonce, &self.passwd) { - let key_phrase = format!("{}-{}", nonce, passwd); - let key_phrase_hash = format!("{:X}\0", md5::compute(&key_phrase)) - .to_uppercase() - .into_bytes(); - let key = &key_phrase_hash[0..16]; - Some(key.to_vec()) - } else { - None - } - } -} - #[derive(Debug, Clone)] pub enum EncryptionProtocol { Unencrypted, BCEncrypt, - Aes(AesKey), - Unknown, + Aes(Option<[u8; 16]>), } #[derive(Debug)] diff --git a/src/bc/ser.rs b/src/bc/ser.rs index f87bb45..230db00 100644 --- a/src/bc/ser.rs +++ b/src/bc/ser.rs @@ -14,7 +14,7 @@ impl Bc { pub fn serialize( &self, buf: W, - encryption_protocol: EncryptionProtocol, + encryption_protocol: &EncryptionProtocol, ) -> 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 @@ -29,7 +29,7 @@ impl Bc { bc_ext( self.meta.channel_id as u32, ext, - encryption_protocol.clone(), + encryption_protocol, ) }), vec![], @@ -53,7 +53,7 @@ impl Bc { bc_payload( self.meta.channel_id as u32, payload_offset, - encryption_protocol.clone(), + encryption_protocol, ) }), temp_buf, @@ -83,7 +83,7 @@ impl Bc { fn bc_ext( enc_offset: u32, xml: &Extension, - encryption_protocol: EncryptionProtocol, + encryption_protocol: &EncryptionProtocol, ) -> impl SerializeFn { let xml_bytes = xml.serialize(vec![]).unwrap(); let enc_bytes = xml_crypto::crypt(enc_offset, &xml_bytes, encryption_protocol); @@ -93,7 +93,7 @@ fn bc_ext( fn bc_payload( enc_offset: u32, payload: &BcPayloads, - encryption_protocol: EncryptionProtocol, + encryption_protocol: &EncryptionProtocol, ) -> impl SerializeFn { let payload_bytes = match payload { BcPayloads::BcXml(x) => { diff --git a/src/bc/xml_crypto.rs b/src/bc/xml_crypto.rs index e4f5e57..de03ac5 100644 --- a/src/bc/xml_crypto.rs +++ b/src/bc/xml_crypto.rs @@ -1,11 +1,10 @@ use super::model::EncryptionProtocol; -use log::error; use openssl::symm::*; const XML_KEY: [u8; 8] = [0x1F, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78, 0xFF]; const IV: &[u8] = b"0123456789abcdef"; -pub fn crypt(offset: u32, buf: &[u8], encryption_protocol: EncryptionProtocol) -> Vec { +pub fn crypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol) -> Vec { match encryption_protocol { EncryptionProtocol::Unencrypted => buf.to_vec(), EncryptionProtocol::BCEncrypt => { @@ -17,20 +16,15 @@ pub fn crypt(offset: u32, buf: &[u8], encryption_protocol: EncryptionProtocol) - } EncryptionProtocol::Aes(key) => { // New protocol here - let aeskey = key.get_key(); - if let Some(aeskey) = &aeskey { + if let Some(aeskey) = key { let t = Cipher::aes_128_cfb128(); decrypt(t, aeskey, Some(IV), &buf).unwrap() } else { // Not yet ready to decrypt (still in login phase) // Use BCEncrypt - crypt(offset, buf, EncryptionProtocol::BCEncrypt) + crypt(offset, buf, &EncryptionProtocol::BCEncrypt) } } - _ => { - error!("Unknown encryption protocol"); - unimplemented!(); - } } } diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index fda0692..63d4ae4 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -6,6 +6,7 @@ use crate::gst::GstOutputs; use adpcm::adpcm_to_pcm; use err_derive::Error; use log::*; +use std::convert::TryInto; use std::io::Write; use std::net::{SocketAddr, ToSocketAddrs}; use std::sync::atomic::{AtomicU16, Ordering}; @@ -257,10 +258,9 @@ impl BcCamera { if let EncryptionProtocol::Aes(_) = connection.get_encrypted() { // We setup the data for the AES key now // as all subsequent communications will use it - let nonce = Some(nonce); - let passwd = Some(password.unwrap_or("").to_string()); - let full_key = AesKey { nonce, passwd }; - connection.set_encrypted(EncryptionProtocol::Aes(full_key)); + let passwd = password.unwrap_or(""); + let full_key = make_aes_key(&nonce, passwd); + connection.set_encrypted(EncryptionProtocol::Aes(Some(full_key))); } Ok(device_info) @@ -431,6 +431,14 @@ fn md5_string(input: &str, trunc: Md5Trunc) -> String { md5 } +pub fn make_aes_key(nonce: &str, passwd: &str) -> [u8; 16] { + let key_phrase = format!("{}-{}", nonce, passwd); + let key_phrase_hash = format!("{:X}\0", md5::compute(&key_phrase)) + .to_uppercase() + .into_bytes(); + key_phrase_hash[0..16].try_into().unwrap() +} + #[test] fn test_md5_string() { // Note that these literals are only 31 characters long - see explanation above.