142 lines
5.9 KiB
Rust
142 lines
5.9 KiB
Rust
//! Fuzzes the host's pairing state machine: a session fed arbitrary sequences of phases, PINs, timer wake-ups and
|
|
//! cancels, with arbitrary values, transports and client ids.
|
|
//! Asserts invariants, not only "no panic": a fuzzer that holds no client key never gets the session to Paired,
|
|
//! Failed is terminal, and the session has a deadline exactly while it is in progress.
|
|
//! How to run: see the comment at the top of `fuzz/Cargo.toml`.
|
|
|
|
#![no_main]
|
|
|
|
use core::time::Duration;
|
|
|
|
use libfuzzer_sys::fuzz_target;
|
|
|
|
use domistream::host::pairing::{
|
|
HostIdentity, HostPairingConfig, HostPairingSession, HostPairingState, HostPairingStep, PairPhase, PairRequest,
|
|
Transport,
|
|
};
|
|
use domistream::pairing::Pin;
|
|
use domistream::{Error, Instant, RandomSource};
|
|
|
|
const VECTORS: &str = include_str!("../../crates/domistream/tests/fixtures/pairing.txt");
|
|
/// The certificate phase 1 presents: a test-only certificate whose key the fuzzer does not have.
|
|
const CLIENT_CERT_PEM: &str = include_str!("../../crates/domistream/tests/fixtures/test-only-host-cert.pem");
|
|
|
|
fn value(name: &str) -> Vec<u8> {
|
|
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(0x5A);
|
|
}
|
|
}
|
|
|
|
/// Reads the fuzzer's bytes as a script.
|
|
struct Script<'a>(&'a [u8]);
|
|
|
|
impl Script<'_> {
|
|
fn byte(&mut self) -> Option<u8> {
|
|
let (&b, rest) = self.0.split_first()?;
|
|
self.0 = rest;
|
|
Some(b)
|
|
}
|
|
|
|
fn bytes(&mut self, n: usize) -> Vec<u8> {
|
|
let n = n.min(self.0.len());
|
|
let (head, rest) = self.0.split_at(n);
|
|
self.0 = rest;
|
|
head.to_vec()
|
|
}
|
|
}
|
|
|
|
fuzz_target!(|data: &[u8]| {
|
|
// Any RSA identity serves as the host's: the client fixture's.
|
|
let Ok(host) = HostIdentity::from_der(&value("client_cert_der"), &value("client_key_pkcs8_der")) else { return };
|
|
let host_cert_der = value("host_cert_der");
|
|
let mut s = HostPairingSession::new(&host, HostPairingConfig::default(), Instant::ZERO);
|
|
let mut now = Instant::ZERO;
|
|
let mut script = Script(data);
|
|
let mut steps = 0;
|
|
while let Some(op) = script.byte() {
|
|
steps += 1;
|
|
if steps > 24 {
|
|
break;
|
|
}
|
|
let before = s.state();
|
|
let flags = script.byte().unwrap_or(0);
|
|
now = now.saturating_add(Duration::from_millis(u64::from(flags & 0x0F) * 700));
|
|
let unique_id = if flags & 0x10 != 0 {
|
|
String::from_utf8_lossy(&script.bytes(usize::from(flags >> 5) * 3)).into_owned()
|
|
} else {
|
|
"0123456789ABCDEF".to_string()
|
|
};
|
|
let len = usize::from(script.byte().unwrap_or(0));
|
|
let phase = match op % 8 {
|
|
0 => {
|
|
let mut salt = [0u8; 16];
|
|
let raw = script.bytes(16);
|
|
salt[..raw.len()].copy_from_slice(&raw);
|
|
let client_cert_pem =
|
|
if len % 4 == 0 { script.bytes(len * 8) } else { CLIENT_CERT_PEM.as_bytes().to_vec() };
|
|
Some(PairPhase::GetServerCert { salt, client_cert_pem })
|
|
}
|
|
1 => Some(PairPhase::ClientChallenge(script.bytes(len % 40))),
|
|
2 => Some(PairPhase::ServerChallengeResp(script.bytes(len % 72))),
|
|
3 => Some(PairPhase::ClientPairingSecret(script.bytes(len * 2))),
|
|
4 => Some(PairPhase::PairChallenge),
|
|
5 => {
|
|
let digits = format!("{:04}", u16::from_le_bytes([len as u8, flags]) % 10_000);
|
|
if let Ok(pin) = Pin::from_digits(&digits) {
|
|
let r = s.supply_pin(&host, pin, now);
|
|
if r.is_ok() {
|
|
assert_eq!(before, HostPairingState::AwaitPin);
|
|
assert_eq!(s.state(), HostPairingState::AwaitClientChallenge);
|
|
}
|
|
}
|
|
None
|
|
}
|
|
6 => {
|
|
let late = now.saturating_add(Duration::from_secs(u64::from(flags)));
|
|
let fired = s.handle_timeout(late);
|
|
assert_eq!(fired, matches!(s.state(), HostPairingState::Failed(_)) && !matches!(before, HostPairingState::Failed(_)));
|
|
None
|
|
}
|
|
_ => {
|
|
s.cancel();
|
|
None
|
|
}
|
|
};
|
|
if let Some(phase) = phase {
|
|
let device_name = (flags & 0x80 != 0).then(|| "fuzz".to_string());
|
|
let request = PairRequest { unique_id, device_name, phase };
|
|
let other_cert;
|
|
let transport = match len % 3 {
|
|
0 => Transport::Http,
|
|
1 => Transport::Https { client_cert_der: &host_cert_der },
|
|
_ => {
|
|
other_cert = script.bytes(len);
|
|
Transport::Https { client_cert_der: &other_cert }
|
|
}
|
|
};
|
|
let r = s.handle(&host, &request, transport, now, &mut Fixed);
|
|
assert!(!matches!(r, Ok(HostPairingStep::Paired(_))), "paired without the client's key");
|
|
if matches!(before, HostPairingState::Failed(_) | HostPairingState::Paired) {
|
|
assert!(matches!(r, Err(Error::State { .. })));
|
|
}
|
|
if r.is_err() {
|
|
assert!(matches!(s.state(), HostPairingState::Failed(_)), "an error ends the session: {r:?}");
|
|
}
|
|
}
|
|
assert_ne!(s.state(), HostPairingState::Paired, "paired without the client's key");
|
|
assert!(s.paired_client().is_none());
|
|
if let HostPairingState::Failed(_) = before {
|
|
assert_eq!(s.state(), before, "Failed is terminal");
|
|
}
|
|
assert_eq!(s.poll_timeout().is_none(), matches!(s.state(), HostPairingState::Failed(_)), "a deadline while in progress");
|
|
}
|
|
});
|