//! A phone has no app-readable hardware UUID. Its installation identity is//! device-local, separate from the signing key, and never part of a database//! copy. Losing or replacing the signing key does not replace this identity.use std::io::{self, Write};usecrate::layout::Layout;usecrate::seams::{uuid_v4, Random, SystemRandom};pubfn installation_id(layout: &Layout) -> io::Result<String> { installation_id_with(layout, &SystemRandom)}fn read_id(path: &std::path::Path) -> io::Result<String> {let id = std::fs::read_to_string(path)?;let id = id.trim();if id.len() != 36 || !id.bytes().enumerate().all(|(i, ch)| {if [8, 13, 18, 23].contains(&i) { ch == b'-' } else { ch.is_ascii_hexdigit() } }) {returnErr(io::Error::new(io::ErrorKind::InvalidData, "the installation identity is not a UUID")); }Ok(id.to_ascii_lowercase())}fn installation_id_with(layout: &Layout, random: &dynRandom) -> io::Result<String> {let path = layout.installation_id();match read_id(&path) {Ok(id) => returnOk(id),Err(error) if error.kind() == io::ErrorKind::NotFound => {},Err(error) => returnErr(error), } std::fs::create_dir_all(layout.root())?;let id = uuid_v4(random);let temporary = layout.root().join(format!(".installation-id-{id}.tmp"));let result = (|| {letmut options = std::fs::OpenOptions::new(); options.write(true).create_new(true); #[cfg(unix)] {use std::os::unix::fs::OpenOptionsExt; options.mode(0o600); }letmut file = options.open(&temporary)?; file.write_all(id.as_bytes())?; file.sync_all()?;// not a copy: the device identity file.match std::fs::hard_link(&temporary, &path) {Ok(()) => std::fs::File::open(layout.root())?.sync_all()?,Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {},Err(error) => returnErr(error), } read_id(&path) })();// not a copy: the device identity file.let _ = std::fs::remove_file(&temporary); result}#[cfg(test)]mod tests {usesuper::*;usecrate::identity::DeviceIdentity;usecrate::seams::SeededRandom;/// RULE: A phone's installation identity survives changing its signing key, because a re-registration must recognize the stale approval without identifierForVendor. #[test]fn identity_survives_a_signing_key_change() {let root = tempfile::tempdir().unwrap();let layout = Layout::new(root.path().to_path_buf());let first = installation_id_with(&layout, &SeededRandom::new(1)).unwrap(); assert_ne!(DeviceIdentity::from_secret(&[1; 32]).unwrap().device_id(), DeviceIdentity::from_secret(&[2; 32]).unwrap().device_id()); assert_eq!(first, installation_id_with(&layout, &SeededRandom::new(2)).unwrap());let other = tempfile::tempdir().unwrap(); assert_ne!(first, installation_id_with(&Layout::new(other.path().to_path_buf()), &SeededRandom::new(3)).unwrap()); }/// RULE: Concurrent first registrations publish one complete installation identity, and corruption is a refusal rather than a new identity that could hide a stale key. #[test]fn concurrent_registrations_keep_one_identity_and_corruption_refuses() {let root = tempfile::tempdir().unwrap();let layout = Layout::new(root.path().to_path_buf());let threads: Vec<_> = (0..8).map(|_| {let layout = layout.clone(); std::thread::spawn(move || installation_id(&layout).unwrap()) }).collect();let ids: Vec<_> = threads.into_iter().map(|thread| thread.join().unwrap()).collect(); assert!(ids.iter().all(|id| id == &ids[0])); std::fs::write(layout.installation_id(), "broken").unwrap(); assert_eq!(installation_id(&layout).unwrap_err().kind(), io::ErrorKind::InvalidData); }}