//! Verifies a Histor receipt with nothing but upstream TLSNotary (v0.1.0-alpha.15) and the //! witness's public key. No Histor code is involved. //! //! cp verify_upstream.rs tlsn/crates/examples/examples/ //! cd tlsn && cargo run --release -p tlsn-examples --example verify_upstream \ //! --features tlsn/mozilla-certs -- receipt.json //! //! A Histor receipt is a JSON object whose `presentation` field is the standard base64 of a //! bincode-encoded TLSNotary `Presentation`. Everything else in the file is a label. use anyhow::{anyhow, bail, Context, Result}; use k256::sha2::{Digest, Sha256}; use tlsn::{ attestation::{ CryptoProvider, presentation::{Presentation, PresentationOutput}, }, verifier::ServerCertVerifier, }; #[derive(serde::Deserialize)] struct Receipt { format: String, presentation: String, } /// Standard base64 with padding (RFC 4648), so this file needs no extra crate. fn base64_decode(text: &str) -> Result> { const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; let mut out = Vec::with_capacity(text.len() * 3 / 4); let (mut acc, mut bits) = (0u32, 0u32); for byte in text.bytes().filter(|b| !b.is_ascii_whitespace()) { if byte == b'=' { break; } let value = ALPHABET.iter().position(|&c| c == byte).context("bad base64")? as u32; acc = (acc << 6) | value; bits += 6; if bits >= 8 { bits -= 8; out.push((acc >> bits) as u8); acc &= (1 << bits) - 1; } } Ok(out) } fn main() -> Result<()> { let args: Vec = std::env::args().collect(); let [_, path, trusted_key] = args.as_slice() else { bail!("usage: verify_upstream "); }; let receipt: Receipt = serde_json::from_slice(&std::fs::read(path)?)?; if receipt.format != "histor-receipt/0.1" { bail!("not a histor-receipt/0.1 file"); } let bytes = base64_decode(&receipt.presentation)?; println!("receipt_id {}", hex::encode(Sha256::digest(&bytes))); // 1. The witness that signed must be the one you trust. let presentation: Presentation = bincode::deserialize(&bytes)?; let key = hex::encode(&presentation.verifying_key().data); if !key.eq_ignore_ascii_case(trusted_key) { bail!("signed by witness {key}, which is not the key you trust"); } // 2. Upstream verification: the witness signature over the attestation, the server's // certificate chain to a Mozilla root for the disclosed name, the handshake signature, // and every disclosed transcript range against its hash commitment. let provider = CryptoProvider { cert: ServerCertVerifier::mozilla(), ..Default::default() }; let PresentationOutput { server_name, connection_info, transcript, .. } = presentation.verify(&provider).map_err(|e| anyhow!("{e}"))?; let server = server_name.context("the receipt discloses no server identity")?; println!("witness_key {key}"); println!("server {server}"); println!("time_unix {}", connection_info.time); println!("tls {:?}", connection_info.version); // 3. Read what the agent chose to disclose. Bytes outside the authenticated ranges are // not evidence; print them as blocks. let transcript = transcript.context("the receipt discloses no transcript")?; let render = |data: &[u8], authed: &tlsn::rangeset::set::RangeSet| -> String { data.iter() .enumerate() .map(|(i, b)| if authed.contains(&i) { (*b as char).to_string() } else { "\u{2588}".into() }) .collect() }; println!("--- sent ---\n{}", render(transcript.sent_unsafe(), transcript.sent_authed())); println!("--- received ---\n{}", render(transcript.received_unsafe(), transcript.received_authed())); Ok(()) }