diff options
| author | Will Dillon <william@housedillon.com> | 2025-11-15 08:24:05 +0000 |
|---|---|---|
| committer | Will Dillon <william@housedillon.com> | 2025-11-15 08:24:05 +0000 |
| commit | e3f274d98f7223ccfa6170d4456f8a2b9c11482c (patch) | |
| tree | 30d763bd0cb3195d65916b6179e5ac9db63e10e9 /src | |
| parent | Updated TOML formate and added test (diff) | |
| download | meshcore-rs-e3f274d98f7223ccfa6170d4456f8a2b9c11482c.tar.gz meshcore-rs-e3f274d98f7223ccfa6170d4456f8a2b9c11482c.zip | |
Anon reqest complete
Diffstat (limited to 'src')
| -rw-r--r-- | src/anon_req.rs | 20 | ||||
| -rw-r--r-- | src/identity.rs | 12 |
2 files changed, 22 insertions, 10 deletions
diff --git a/src/anon_req.rs b/src/anon_req.rs index b3e8d0c..d7aa090 100644 --- a/src/anon_req.rs +++ b/src/anon_req.rs @@ -77,16 +77,17 @@ impl From<Bytes> for ClearAnonRequest { // Just check for the whole fixed-size part at once if bytes.len() < 4 { return anon_req } - if let Some(timestamp) = DateTime::from_timestamp(bytes.get_u32() as i64, 0) { + if let Some(timestamp) = DateTime::from_timestamp(bytes.get_u32_le() as i64, 0) { anon_req.timestamp = timestamp; } - if bytes.len() < 4 { return anon_req; } - if let Some(timestamp) = DateTime::from_timestamp(bytes.get_u32() as i64, 0) { - anon_req.sync_timestamp = Some(timestamp); - } + // Strip-off any null characters after the password + let raw_password = String::from_utf8_lossy(&bytes).to_string(); + let trimmed_password: Vec<&str> = raw_password.splitn(2, "\0").collect(); + if let Some(password) = trimmed_password.first() { + anon_req.password = password.to_string(); - anon_req.password = String::from_utf8_lossy(&bytes).to_string(); + } anon_req } } @@ -96,6 +97,7 @@ mod tests { use std::collections::HashMap; use std::str::FromStr; use hex::decode; + use hex::encode; use crate::identity::KeystoreInput; use crate::packet::*; use crate::crypto::*; @@ -147,9 +149,11 @@ mod tests { println!("\"{}\"", rhs_packet); assert!(format!("{}", rhs_packet) == " Flood | v1 | | [] | | ANON REQ. | (12) -> (34) MAC: 4e7b ENCRYPTED"); rhs_packet.try_decrypt(&keystore); - println!("\"{}\"", rhs_packet); - // assert!(format!("{}", rhs_packet) == ""); + + let lhs_string = format!("{}", rhs_packet); + let rhs_string = " Flood | v1 | | [] | | ANON REQ. | (12) -> (34) MAC: 4e7b at: 2025-11-13 17:21:13 UTC password: \"12345\""; + assert!(lhs_string == rhs_string); } }
\ No newline at end of file diff --git a/src/identity.rs b/src/identity.rs index e68b7f2..0b8d8ec 100644 --- a/src/identity.rs +++ b/src/identity.rs @@ -1,7 +1,7 @@ use std::{collections::{HashMap, HashSet}, process::id, rc::Rc, str::FromStr}; use bytes::Bytes; use log::warn; -use crate::crypto::{PrivateKey, PublicKey, SharedSecret}; +use crate::{crypto::{PrivateKey, PublicKey, SharedSecret}, identity}; use structdiff::{Difference, StructDiff}; #[cfg(feature = "std")] @@ -122,7 +122,15 @@ impl Keystore { None } - pub fn decrypt_anon(&self, _dest: u8, _pub_key: &PublicKey, _mac: u16, _data: &Bytes) -> Option<(Bytes, Identity)> { + pub fn decrypt_anon(&self, _dest: u8, pub_key: &PublicKey, mac: u16, data: &Bytes) -> Option<(Bytes, Rc<String>)> { + // For each identity, create a shared secret against the provided public key and check it. + for identity in self.identities.iter() { + let secret = identity.1.private_key.create_secret(pub_key); + let result = secret.mac_then_decrypt(mac, data); + if let Some(result) = result { + return Some((result, identity.0.clone())); + } + } None } } |
