Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions pkgs/id/src/hosted_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ pub enum ExportError {
/// The manifest names a different binding or key than the
/// binding record it travels with.
ManifestBindingMismatch,
/// The manifest's §7 client-side-encryption choice differs from
/// the binding record's. The flag can only change by
/// re-encrypting via export/import, so an export whose halves
/// disagree does not describe one stored data set (§6.2 step 1,
/// §7).
CseChoiceMismatch,
/// The manifest signature does not verify (against the user's
/// root key, or the service key for an `unkeyed_export`).
ManifestSignatureInvalid,
Expand Down Expand Up @@ -330,6 +336,12 @@ impl std::fmt::Display for ExportError {
"manifest does not name the binding record it travels with"
)
}
Self::CseChoiceMismatch => {
write!(
f,
"manifest's client-side-encryption choice differs from the binding record's"
)
}
Self::ManifestSignatureInvalid => {
write!(f, "manifest signature does not verify")
}
Expand Down Expand Up @@ -518,6 +530,17 @@ pub fn verify_import(
) {
return Err(ExportError::ManifestSignatureInvalid);
}
// Step 1, §7: the manifest's CSE choice is part of naming the
// binding's stored state. It is covered by the manifest
// signature (canonical_bytes), and the binding record's own
// flag is attested by the service; disagreement means the two
// halves do not describe one stored data set — an export
// stating the wrong choice misdirects every downstream reader
// of the payloads (§7: the flag changes only by re-encrypting
// via export/import).
if manifest.client_side_encryption != binding.client_side_encryption {
return Err(ExportError::CseChoiceMismatch);
}

// Step 2: the binding chain. The transcript is reconstructed
// from the exported record itself, never fetched.
Expand Down Expand Up @@ -741,6 +764,60 @@ mod tests {
assert!(!verified.client_side_encryption);
}

#[test]
fn manifest_cse_choice_mismatching_the_binding_is_refused() {
// §7: the flag can only change by re-encrypting via
// export/import, so a manifest stating a different choice
// than the binding record it travels with does not name
// that binding (§6.2 step 1). Stating encryption under a
// binding that stored plaintext would decrypt wrong;
// stating plaintext under an encrypted binding downgrades
// opaque payloads — the import refuses, and ingests
// nothing.
let (_svc, mut export) = sample_export(Some(false));
assert!(!export.binding.client_side_encryption);
export.manifest.client_side_encryption = true;
// Re-sign so only the choice rule can fail: this is the
// misassembled pair (or the re-signed forgery), not a
// tampered manifest.
export.manifest.signature = hex_encode(
&root_key(21)
.sign(&export.manifest.canonical_bytes())
.to_bytes(),
);
assert_eq!(
verify_import(&export, false),
Err(ExportError::CseChoiceMismatch)
);
// The other direction — CSE binding claimed as plaintext —
// is refused the same way.
let (_svc, mut cse_export) = sample_export(None);
assert!(cse_export.binding.client_side_encryption);
cse_export.manifest.client_side_encryption = false;
cse_export.manifest.signature = hex_encode(
&root_key(21)
.sign(&cse_export.manifest.canonical_bytes())
.to_bytes(),
);
assert_eq!(
verify_import(&cse_export, false),
Err(ExportError::CseChoiceMismatch)
);
}

#[test]
fn flipping_the_manifest_cse_flag_without_resigning_is_refused() {
// The CSE choice is inside the manifest's canonical bytes,
// so a flag flip without the key fails the signature first
// — the choice is authenticated, not decorative.
let (_svc, mut export) = sample_export(None);
export.manifest.client_side_encryption = false;
assert_eq!(
verify_import(&export, false),
Err(ExportError::ManifestSignatureInvalid)
);
}

#[test]
fn tampered_manifest_is_refused() {
let (_svc, mut export) = sample_export(None);
Expand Down
26 changes: 24 additions & 2 deletions pkgs/id/src/hosted_ingest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ async fn place_blob(blobs: &BlobStore, blob: &ExportBlob) -> EntryOutcome {
///
/// `verified` is the token [`crate::hosted_export::verify_import`]
/// minted for this export; it must name the export's binding, key,
/// and entry counts. `now_ms` is the importing node's clock, handed
/// to the event store like any other ingest's.
/// entry counts, and §7 client-side-encryption choice. `now_ms` is
/// the importing node's clock, handed to the event store like any
/// other ingest's.
///
/// # Errors
///
Expand All @@ -270,6 +271,7 @@ pub async fn ingest_import(
|| verified.iroh_id != export.manifest.iroh_id
|| verified.object_count != export.objects.len() as u64
|| verified.blob_count != export.blobs.len() as u64
|| verified.client_side_encryption != export.manifest.client_side_encryption
{
return Err(ImportError::VerificationMismatch);
}
Expand Down Expand Up @@ -557,4 +559,24 @@ mod tests {
let author = *p2p_key(SEED).verifying_key().as_bytes();
assert!(target.event(&author, 0).unwrap().is_none());
}

#[tokio::test]
async fn a_token_with_a_different_cse_choice_is_refused_before_any_write() {
let (events, content) = source_chain();
let export = make_export(&events, Some(&content));
let mut wrong = verify_import(&export, false).unwrap();
wrong.client_side_encryption = !wrong.client_side_encryption;
let (_dir, mut target) = open_store();
let blobs = blob_store();
let err = ingest_import(&mut target, &blobs, &export, &wrong, 0)
.await
.unwrap_err();
assert!(matches!(err, ImportError::VerificationMismatch));
// The token is the authority to proceed only with the exact
// verified interpretation; reject before any object or blob
// is written.
let author = *p2p_key(SEED).verifying_key().as_bytes();
assert!(target.event(&author, 0).unwrap().is_none());
assert!(blobs.get_bytes(BlobHash::new(&content)).await.is_err());
}
}