Refactor AesKey to only contain key bytes

This commit is contained in:
George Hilliard committed 2021-04-17 01:49:29 -05:00
1 parent 02f4cab6ba
commit 1227ff1e96
5 files changed
+25 -48

No files matched your search

+4 -7
View File
@@ -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 {
+1 -23
View File
@@ -71,33 +71,11 @@ pub(super) struct BcSendInfo {
pub payload_offset: Option<u32>,
}
#[derive(Debug, Clone)]
pub struct AesKey {
pub nonce: Option<String>,
pub passwd: Option<String>,
}
impl AesKey {
pub fn get_key(&self) -> Option<Vec<u8>> {
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)]
+5 -5
View File
@@ -14,7 +14,7 @@ impl Bc {
pub fn serialize<W: Write>(
&self,
buf: W,
encryption_protocol: EncryptionProtocol,
encryption_protocol: &EncryptionProtocol,
) -> 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
@@ -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<W: Write>(
enc_offset: u32,
xml: &Extension,
encryption_protocol: EncryptionProtocol,
encryption_protocol: &EncryptionProtocol,
) -> impl SerializeFn<W> {
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<W: Write>(
fn bc_payload<W: Write>(
enc_offset: u32,
payload: &BcPayloads,
encryption_protocol: EncryptionProtocol,
encryption_protocol: &EncryptionProtocol,
) -> impl SerializeFn<W> {
let payload_bytes = match payload {
BcPayloads::BcXml(x) => {
+3 -9
View File
@@ -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<u8> {
pub fn crypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol) -> Vec<u8> {
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!();
}
}
}
+12 -4
View File
@@ -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.