error out on error in tlv

This commit is contained in:
Alex Janka 2024-02-22 10:58:49 +11:00
parent 72525381c9
commit a308515aea
2 changed files with 24 additions and 11 deletions

View file

@ -1,10 +1,12 @@
use chacha20poly1305::{aead::generic_array::GenericArray, AeadInPlace, ChaCha20Poly1305, KeyInit}; use chacha20poly1305::{
aead::generic_array::GenericArray, AeadInPlace, ChaCha20Poly1305, KeyInit, Nonce,
};
use ed25519_dalek::{Signer, Verifier}; use ed25519_dalek::{Signer, Verifier};
use hkdf::Hkdf; use hkdf::Hkdf;
use sha2::Sha512; use sha2::Sha512;
use std::{collections::HashMap, path::PathBuf}; use std::{collections::HashMap, path::PathBuf};
use thiserror::Error; use thiserror::Error;
use tlv8::{HomekitState, TlvEncode, TlvType}; use tlv8::{HomekitState, TlvEncode, TlvError, TlvType};
use x25519_dalek::{EphemeralSecret, PublicKey}; use x25519_dalek::{EphemeralSecret, PublicKey};
use pairing_data::DevicePairingData; use pairing_data::DevicePairingData;
@ -164,13 +166,9 @@ impl DevicePairingData {
.encode(); .encode();
// 10. Encrypt sub-TLV: encryptedData, authTag = ChaCha20-Poly1305(SessionKey, Nonce=”PV-Msg03”, AAD=<none>, Msg=<Sub-TLV>) // 10. Encrypt sub-TLV: encryptedData, authTag = ChaCha20-Poly1305(SessionKey, Nonce=”PV-Msg03”, AAD=<none>, Msg=<Sub-TLV>)
let nonce: [u8; 12] = [0; 4] let mut nonce = [0; 12];
.iter() nonce[4..].copy_from_slice(b"PV-Msg03");
.chain(b"PV-Msg03") chacha.encrypt_in_place(Nonce::from_slice(&nonce), &[], &mut encrypted_tlv)?;
.copied()
.collect::<Vec<_>>()
.try_into()?;
chacha.encrypt_in_place(GenericArray::from_slice(&nonce), &[], &mut encrypted_tlv)?;
// 11/12 Construct TLV response and send to accessory // 11/12 Construct TLV response and send to accessory
let step3_response = decode( let step3_response = decode(
@ -196,6 +194,14 @@ impl DevicePairingData {
return Err(HomekitError::StateMismatch); return Err(HomekitError::StateMismatch);
} }
if let Some(error) = step3_response.get(&TlvType::Error.into()) {
if let Some(e) = error.first().and_then(|v| TlvError::try_from(*v).ok()) {
return Err(e.into());
}
log::error!("got tlv error from device but couldn't parse it from the data!");
return Err(HomekitError::TlvNotFound);
}
// 5.7.4 M4: Accessory -> iOS Device Verify Finish Responseʼ // 5.7.4 M4: Accessory -> iOS Device Verify Finish Responseʼ
// When the accessory receives <M3>, it must perform the following steps: // When the accessory receives <M3>, it must perform the following steps:
// //
@ -299,6 +305,14 @@ pub enum HomekitError {
SomethingElse(String), SomethingElse(String),
#[error("addr parse")] #[error("addr parse")]
AddrParse(#[from] std::net::AddrParseError), AddrParse(#[from] std::net::AddrParseError),
#[error("tlv error from device")]
TlvDeviceError(TlvError),
}
impl From<TlvError> for HomekitError {
fn from(value: TlvError) -> Self {
Self::TlvDeviceError(value)
}
} }
impl From<String> for HomekitError { impl From<String> for HomekitError {

View file

@ -1,8 +1,7 @@
use self::data_types::TlvError;
use std::collections::HashMap; use std::collections::HashMap;
use thiserror::Error; use thiserror::Error;
pub use data_types::{HomekitState, TlvType}; pub use data_types::{HomekitState, TlvError, TlvType};
mod data_types; mod data_types;