//! Fuzzes the pairing state machine: a session with the test-only identity fed fuzzer bodies at each step. //! Asserts invariants, not only "no panic": a host that knows neither the PIN's key nor the host key never gets the session to Paired, and Failed is terminal. //! How to run: see the comment at the top of `fuzz/Cargo.toml`. #![no_main] use libfuzzer_sys::fuzz_target; use domistream::RandomSource; use domistream::pairing::{ClientIdentity, PairingSession, PairingState, PairingStep, Pin}; const VECTORS: &str = include_str!("../../crates/domistream/tests/fixtures/pairing.txt"); fn value(name: &str) -> Vec { let prefix = format!("{name} = "); let line = VECTORS.lines().find(|l| l.starts_with(&prefix)).expect("fixture value"); let hex = &line[prefix.len()..]; (0..hex.len()).step_by(2).map(|i| u8::from_str_radix(&hex[i..i + 2], 16).expect("hex")).collect() } struct Fixed; impl RandomSource for Fixed { fn fill(&mut self, dest: &mut [u8]) { dest.fill(0x33); } } fuzz_target!(|data: &[u8]| { let Ok(id) = ClientIdentity::from_der(&value("client_cert_der"), &value("client_key_pkcs8_der")) else { return }; let Ok(pin) = Pin::from_digits("4071") else { return }; let Ok((mut s, _)) = PairingSession::start(&id, "0123456789ABCDEF", "fuzz", pin, &mut Fixed) else { return }; // Bodies separated by 0xFF bytes (never valid in the XML); the HTTP status is always 200. for body in data.split(|b| *b == 0xFF).take(6) { let before = s.state(); let r = s.handle_response(&id, 200, body, &mut Fixed); assert!(!matches!(r, Ok(PairingStep::Paired(_))), "paired with a host that cannot know the PIN key"); if let PairingState::Failed(_) = before { assert!(r.is_err()); assert_eq!(s.state(), before, "Failed is terminal"); } } });