diff --git a/pkgs/id/src/hosted_binding.rs b/pkgs/id/src/hosted_binding.rs index 8e0886c89..3efccf4d8 100644 --- a/pkgs/id/src/hosted_binding.rs +++ b/pkgs/id/src/hosted_binding.rs @@ -3011,6 +3011,36 @@ mod tests { .unwrap(); } + #[test] + fn key_signature_verifies_for_a_rebound_binding() { + let mut service = service(); + let binding = bind(&mut service, SUB, 21); + let new_key = root_key(22); + let (challenge_id, proof) = signed_challenge(&mut service, SUB, &new_key); + let (request, _) = service + .request_delayed_rebind(SUB, &challenge_id, &proof) + .unwrap(); + let counter = countersign( + &binding.binding_id, + &binding.iroh_id, + &new_key, + REBIND_VIA_DELAYED, + ); + service.set_now_override(Some(T0 + REBIND_WAITING_PERIOD_SECS)); + let (updated, _) = service + .effect_due_rebind(&request.rebind_id, &counter) + .unwrap(); + // The transcript still names the ceremony key, now superseded + // into history: the signature verifies, linked to the current + // key by the attested history and the rebind records. + assert_eq!(updated.challenge_transcript.iroh_id, binding.iroh_id); + assert!(HostedBindingService::verify_key_signature(&updated)); + // A transcript naming a key outside the chain still fails. + let mut foreign = updated.clone(); + foreign.challenge_transcript.iroh_id = hex_encode(&root_key(77).verifying_key().to_bytes()); + assert!(!HostedBindingService::verify_key_signature(&foreign)); + } + #[test] fn delayed_rebind_effect_refuses_a_target_bound_elsewhere_without_mutation() { let mut service = service(); diff --git a/pkgs/id/src/hosted_export.rs b/pkgs/id/src/hosted_export.rs index b3f718d6f..0ff4d0eb1 100644 --- a/pkgs/id/src/hosted_export.rs +++ b/pkgs/id/src/hosted_export.rs @@ -630,6 +630,10 @@ pub fn verify_import( #[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] mod tests { use super::*; + use crate::hosted_binding::{ + Challenge, REBIND_VIA_DELAYED, REBIND_VIA_OLD_KEY, parse_rfc3339, + rebind_authorization_bytes, + }; const SUB: &str = "google-subject-1"; const CREATED: &str = "2026-11-13T00:00:00Z"; @@ -989,4 +993,394 @@ mod tests { assert_eq!(verified.blob_count, 0); assert_eq!(verified.iroh_id, updated.iroh_id); } + // -- binding-record history (§6.2 step 2) ------------------------------- + + const OTHER_SUB: &str = "other-subject-9"; + + /// A fresh §3 challenge for `key` on `sub`, with the proof signature. + fn challenged( + service: &mut HostedBindingService, + sub: &str, + key: &SigningKey, + ) -> (Challenge, [u8; 64]) { + let iroh_id = key.verifying_key().to_bytes(); + let challenge = service.request_challenge(sub, &iroh_id).unwrap(); + let proof = key + .sign(&HostedBindingService::canonical_transcript(&challenge)) + .to_bytes(); + (challenge, proof) + } + + /// Drive a delayed rebind to effect: request, wait out the 30-day + /// period, countersign, effect. Returns the updated binding (whose + /// history names the old key) and the published rebind record. + fn rebind_delayed( + service: &mut HostedBindingService, + sub: &str, + new_key: &SigningKey, + ) -> (BindingRecord, RebindRecord) { + let (challenge, proof) = challenged(service, sub, new_key); + let (request, _) = service + .request_delayed_rebind(sub, &challenge.challenge_id, &proof) + .unwrap(); + let effective = parse_rfc3339(&request.effective_at).unwrap(); + service.set_now_override(Some(effective + 1)); + let probe = RebindRecord { + rebind_id: String::new(), + binding_id: request.binding_id.clone(), + old_iroh_id: request.old_iroh_id.clone(), + new_iroh_id: request.new_iroh_id.clone(), + effective_at: String::new(), + via: REBIND_VIA_DELAYED.to_owned(), + service_signature: String::new(), + new_key_countersignature: String::new(), + }; + let countersignature = new_key.sign(&probe.countersign_bytes()).to_bytes(); + service + .effect_due_rebind(&request.rebind_id, &countersignature) + .unwrap() + } + + /// Drive the §4.1 fast path: the old key authorizes the transfer from + /// a fresh challenge and the new key countersigns. Effect is + /// immediate; the history step needs no backing record in the export. + fn rebind_fast( + service: &mut HostedBindingService, + sub: &str, + old_key: &SigningKey, + new_key: &SigningKey, + ) -> (BindingRecord, RebindRecord) { + let binding = service.binding_for_sub(sub).unwrap().clone(); + let (challenge, proof) = challenged(service, sub, new_key); + let auth = old_key + .sign(&rebind_authorization_bytes( + &binding.binding_id, + &challenge.iroh_id, + &challenge.challenge_id, + )) + .to_bytes(); + let probe = RebindRecord { + rebind_id: String::new(), + binding_id: binding.binding_id.clone(), + old_iroh_id: binding.iroh_id.clone(), + new_iroh_id: hex_encode(&new_key.verifying_key().to_bytes()), + effective_at: String::new(), + via: REBIND_VIA_OLD_KEY.to_owned(), + service_signature: String::new(), + new_key_countersignature: String::new(), + }; + let countersignature = new_key.sign(&probe.countersign_bytes()).to_bytes(); + service + .rebind_with_old_key_authorization( + sub, + None, + &challenge.challenge_id, + &proof, + &auth, + &countersignature, + ) + .unwrap() + } + + /// Flip the last hex digit of `hex` to a different valid hex digit, + /// so the value stays well-formed but no longer matches. + fn flip_last_hex(hex: &str) -> String { + let mut out = hex.to_owned(); + let last = out.pop().unwrap(); + out.push(if last == '0' { '1' } else { '0' }); + out + } + + /// An export object over a freshly signed envelope by `key` + /// (§6.3: the envelope is the object; the document content + /// keeps objects from the same key distinct, and `blob_refs` + /// is the export-layer citation set the verifier checks + /// against the carried blobs). + fn signed_object(key: &SigningKey, content: &[u8], blob_refs: Vec) -> ExportObject { + let document = crate::documents::DocumentRef::for_bytes("text/plain", content).unwrap(); + let event = envelope(key, 0, None, ext("document.put", Some(document))); + ExportObject::from_envelope(event.bytes, blob_refs).unwrap() + } + + #[test] + fn delayed_rebind_history_round_trip() { + let mut svc = service(); + let old_key = root_key(21); + let new_key = root_key(22); + bind(&mut svc, 21, None); + let (binding, record) = rebind_delayed(&mut svc, SUB, &new_key); + assert_eq!(binding.history.len(), 1); + assert_eq!(binding.history[0].via, "delayed-rebind"); + + // Objects from both sides of the rotation stay verifiable: the + // binding chain in step 2 admits every key in the history. + let blob = ExportBlob::new(b"shared blob".to_vec()); + let old_object = signed_object( + &old_key, + b"written before the rebind", + vec![blob.hash.clone()], + ); + let new_object = signed_object(&new_key, b"written after", Vec::new()); + let export = build_export( + &svc, + &binding, + vec![record], + vec![old_object, new_object], + vec![blob], + Some(&new_key), + "export-rebound", + CREATED, + ); + let verified = verify_import(&export, false).unwrap(); + assert_eq!(verified.object_count, 2); + assert_eq!(verified.blob_count, 1); + assert_eq!(verified.iroh_id, binding.iroh_id); + } + + #[test] + fn fast_path_history_verifies_without_backing_records() { + let mut svc = service(); + let old_key = root_key(21); + let new_key = root_key(22); + bind(&mut svc, 21, None); + let (binding, _record) = rebind_fast(&mut svc, SUB, &old_key, &new_key); + assert_eq!(binding.history[0].via, "old-key-signature"); + // The fast path is the old key's signature over the transfer, + // checked at publication: the export carries no backing record. + let object = signed_object(&old_key, b"pre-rotation", Vec::new()); + let export = build_export( + &svc, + &binding, + Vec::new(), + vec![object], + Vec::new(), + Some(&new_key), + "export-fast", + CREATED, + ); + verify_import(&export, false).unwrap(); + } + + #[test] + fn delayed_history_without_backing_record_is_refused() { + let mut svc = service(); + bind(&mut svc, 21, None); + let (binding, _record) = rebind_delayed(&mut svc, SUB, &root_key(22)); + let object = signed_object(&root_key(22), b"post-rotation", Vec::new()); + let export = build_export( + &svc, + &binding, + Vec::new(), + vec![object], + Vec::new(), + Some(&root_key(22)), + "export-unbacked", + CREATED, + ); + assert_eq!( + verify_import(&export, false), + Err(ExportError::HistoryUnbacked { + iroh_id: binding.history[0].iroh_id.clone(), + }) + ); + } + + #[test] + fn rebind_record_for_another_binding_is_refused() { + let mut svc = service(); + bind(&mut svc, 21, None); + let (binding, mut record) = rebind_delayed(&mut svc, SUB, &root_key(22)); + // A well-formed, service-signed record that belongs to a + // different binding: only the binding-ID check can catch it. + record.binding_id = "binding-from-elsewhere".to_owned(); + let export = build_export( + &svc, + &binding, + vec![record], + Vec::new(), + Vec::new(), + Some(&root_key(22)), + "export-foreign-record", + CREATED, + ); + assert_eq!( + verify_import(&export, false), + Err(ExportError::RebindRecordMismatch) + ); + } + + #[test] + fn tampered_rebind_record_is_refused() { + let mut svc = service(); + bind(&mut svc, 21, None); + let (binding, mut record) = rebind_delayed(&mut svc, SUB, &root_key(22)); + record.service_signature = flip_last_hex(&record.service_signature); + let export = build_export( + &svc, + &binding, + vec![record], + Vec::new(), + Vec::new(), + Some(&root_key(22)), + "export-tampered-record", + CREATED, + ); + assert_eq!( + verify_import(&export, false), + Err(ExportError::HistoryUnbacked { + iroh_id: binding.history[0].iroh_id.clone(), + }) + ); + } + + // -- remaining step 1–3 refusals ---------------------------------------- + + #[test] + fn manifest_for_another_binding_is_refused() { + let mut svc = service(); + let binding = bind(&mut svc, 21, None); + svc.signup_account(OTHER_SUB, None, "free").unwrap(); + let other = svc + .complete_browser_ceremony(OTHER_SUB, &root_key(23), None) + .unwrap(); + let export = build_export( + &svc, + &binding, + Vec::new(), + Vec::new(), + Vec::new(), + Some(&root_key(21)), + "export-mismatch", + CREATED, + ); + let mut swapped = export; + swapped.binding = other; + assert_eq!( + verify_import(&swapped, false), + Err(ExportError::ManifestBindingMismatch) + ); + } + + #[test] + fn tampered_binding_key_signature_is_refused() { + let (_svc, mut export) = sample_export(None); + export.binding.key_signature = flip_last_hex(&export.binding.key_signature); + assert_eq!( + verify_import(&export, false), + Err(ExportError::BindingKeySignatureInvalid) + ); + } + + #[test] + fn tampered_service_attestation_is_refused() { + let (_svc, mut export) = sample_export(None); + export.binding.service_attestation = flip_last_hex(&export.binding.service_attestation); + assert_eq!( + verify_import(&export, false), + Err(ExportError::BindingAttestationInvalid) + ); + } + + #[test] + fn count_mismatch_is_refused() { + let (_svc, mut export) = sample_export(None); + // One more object than the manifest counts: the manifest + // signature still verifies, so only the count rule can fail. + export + .objects + .push(signed_object(&root_key(21), b"smuggled", Vec::new())); + assert_eq!( + verify_import(&export, false), + Err(ExportError::CountMismatch) + ); + } + + #[test] + fn stale_objects_digest_is_refused() { + let (_svc, mut export) = sample_export(None); + // Swap in a different well-formed object from the same key: + // counts still match, but the manifest digest is stale. + export.objects[0] = signed_object(&root_key(21), b"replacement", Vec::new()); + export.blobs.clear(); + export.manifest.blob_count = 0; + // Re-sign over the unchanged (now stale) counts and digest. + export.manifest.signature = hex_encode( + &root_key(21) + .sign(&export.manifest.canonical_bytes()) + .to_bytes(), + ); + assert_eq!( + verify_import(&export, false), + Err(ExportError::ObjectsDigestMismatch) + ); + } + + #[test] + fn duplicate_object_is_refused() { + let (_svc, mut export) = sample_export(None); + export.objects.push(export.objects[0].clone()); + export.manifest.object_count = 2; + export.manifest.objects_digest = objects_digest(&export.objects); + export.manifest.signature = hex_encode( + &root_key(21) + .sign(&export.manifest.canonical_bytes()) + .to_bytes(), + ); + assert_eq!( + verify_import(&export, false), + Err(ExportError::DuplicateObject) + ); + } + + #[test] + fn missing_blob_is_refused() { + let mut svc = service(); + let binding = bind(&mut svc, 21, None); + // The object cites a blob the export does not carry. + let object = signed_object(&root_key(21), b"cites the void", vec!["ab".repeat(32)]); + let export = build_export( + &svc, + &binding, + Vec::new(), + vec![object], + Vec::new(), + Some(&root_key(21)), + "export-missing-blob", + CREATED, + ); + assert_eq!(verify_import(&export, false), Err(ExportError::MissingBlob)); + } + + // -- canonical form and the wire ---------------------------------------- + + #[test] + fn export_is_canonical_and_survives_the_wire() { + let mut svc = service(); + let binding = bind(&mut svc, 21, None); + let blob_b = ExportBlob::new(b"second".to_vec()); + let blob_a = ExportBlob::new(b"first".to_vec()); + let obj_b = signed_object(&root_key(21), b"bbb", vec![blob_b.hash.clone()]); + let obj_a = signed_object(&root_key(21), b"a", vec![blob_a.hash.clone()]); + // Deliberately scrambled input order. + let export = build_export( + &svc, + &binding, + Vec::new(), + vec![obj_b, obj_a], + vec![blob_b, blob_a], + Some(&root_key(21)), + "export-canonical", + CREATED, + ); + // Stored in canonical (hash) order regardless of input order. + assert!(export.objects.windows(2).all(|w| w[0].hash <= w[1].hash)); + assert!(export.blobs.windows(2).all(|w| w[0].hash <= w[1].hash)); + // Identical by canonical object/hash set after a JSON round trip. + let wire = serde_json::to_string(&export).unwrap(); + let received: HostedExport = serde_json::from_str(&wire).unwrap(); + assert_eq!(received, export); + let verified = verify_import(&received, false).unwrap(); + assert_eq!(verified.object_count, 2); + assert_eq!(verified.blob_count, 2); + } }