Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add read-side hash verification during blob retrieval

Leendert de Borst26269c7d7b2b
diff --git a/core/rust/src/vault_codec/mod.rs b/core/rust/src/vault_codec/mod.rs
index c08bafb0e..125534252 100644
--- a/core/rust/src/vault_codec/mod.rs
+++ b/core/rust/src/vault_codec/mod.rs
@@ -30,6 +30,7 @@ pub use manifest::{
pub use materialize::materialize_as_sqlite;
pub use logos::logo_id_for;
pub use sharing::extract_encryption_key_for_public_key;
+pub(crate) use hash::salted_blob_hash;
pub(crate) use types::{blob_spec_for, ensure_readable_schema_version};
pub use types::{bucket_categories, identity_part, is_bucketed_table, is_readable_schema_version, is_skip_table, manifest_scoped_tables, tables_for_category};
pub use validate::{validate_data_bucket, validate_manifest, ValidationResult};
diff --git a/core/rust/src/vault_sync/blob_keys.rs b/core/rust/src/vault_sync/blob_keys.rs
index c726cace1..88427ef83 100644
--- a/core/rust/src/vault_sync/blob_keys.rs
+++ b/core/rust/src/vault_sync/blob_keys.rs
@@ -4,7 +4,8 @@ use serde::{Deserialize, Serialize};
use crate::crypto::{self, aad};
use crate::common::encoding::{base64_decode, base64_encode};
-use crate::common::error::VaultResult;
+use crate::common::error::{VaultError, VaultResult};
+use crate::vault_codec;
/// A blob as the server stores it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -26,10 +27,14 @@ pub(crate) fn encrypt_blob(bytes: &[u8], vek: &str, manifest_id: &str, hash: &st
})
}
-/// Decrypt the blob stored at `(manifest_id, hash)` with the manifest's VEK.
-pub(crate) fn decrypt_blob(blob: &EncryptedBlob, vek: &str, manifest_id: &str, hash: &str) -> VaultResult<Vec<u8>> {
+/// Decrypt the blob stored at `(manifest_id, hash)` with the manifest's VEK, and check that its bytes hash to that address.
+pub(crate) fn decrypt_blob(blob: &EncryptedBlob, vek: &str, manifest_id: &str, manifest_salt: &str, hash: &str) -> VaultResult<Vec<u8>> {
let blob_key = crypto::decrypt_key(&blob.encrypted_blob_key, vek, &aad::blob_key(manifest_id, hash))?;
- crypto::aes_gcm::decrypt(&blob.encrypted_data, &blob_key, &aad::blob_data(manifest_id, hash))
+ let bytes = crypto::aes_gcm::decrypt(&blob.encrypted_data, &blob_key, &aad::blob_data(manifest_id, hash))?;
+ if !vault_codec::salted_blob_hash(&bytes, manifest_salt)?.eq_ignore_ascii_case(hash) {
+ return Err(VaultError::General(format!("blob {} does not hash to its address", hash)));
+ }
+ Ok(bytes)
}
/// Serde for bytes stored as a standard base64 string.
diff --git a/core/rust/src/vault_sync/pull.rs b/core/rust/src/vault_sync/pull.rs
index b0871bed4..bab4d2652 100644
--- a/core/rust/src/vault_sync/pull.rs
+++ b/core/rust/src/vault_sync/pull.rs
@@ -335,12 +335,12 @@ async fn download_referenced_blobs(ctx: &Ctx, resolved: &[ResolvedManifest]) ->
continue;
};
let owner = owners[&reference.hash];
- match blob_keys::decrypt_blob(encrypted, owner.vek.expose_secret(), &owner.manifest_id, &reference.hash) {
+ match blob_keys::decrypt_blob(encrypted, owner.vek.expose_secret(), &owner.manifest_id, &owner.manifest.manifest_salt, &reference.hash) {
Ok(bytes) => {
blob_map.insert(reference.hash.clone(), bytes);
pruned_cache.insert(reference.hash.clone(), encrypted.clone());
}
- Err(_) => ctx.warn(format!("[Pull] Referenced {} blob {} did not decrypt with the manifest key; its row stays not loaded and keeps the reference.", reference.category, reference.hash)).await,
+ Err(_) => ctx.warn(format!("[Pull] Referenced {} blob {} did not decrypt with the manifest key or does not hash to its address; its row stays not loaded and keeps the reference.", reference.category, reference.hash)).await,
}
}
state::set(&ctx.host, state::VAULT_BLOB_CIPHER_CACHE, &pruned_cache).await?;
diff --git a/core/rust/src/vault_sync/tests/blob_keys.rs b/core/rust/src/vault_sync/tests/blob_keys.rs
index 0b097b641..ad37eb95a 100644
--- a/core/rust/src/vault_sync/tests/blob_keys.rs
+++ b/core/rust/src/vault_sync/tests/blob_keys.rs
@@ -1,48 +1,68 @@
use crate::crypto::{self, aad};
+use crate::vault_codec;
use crate::vault_sync::blob_keys::*;
const MANIFEST: &str = "0a1b2c3d-0000-0000-0000-000000000001";
-const HASH: &str = "aa11";
+const SALT: &str = "5a17";
+
+/// The address of `bytes` in a manifest salted with [`SALT`].
+fn address(bytes: &[u8]) -> String {
+ vault_codec::salted_blob_hash(bytes, SALT).unwrap()
+}
#[test]
fn a_blob_opens_with_the_vek_through_its_own_key() {
let vek = crypto::aes_gcm::generate_key();
- let blob = encrypt_blob(&[1, 2, 3, 4], &vek, MANIFEST, HASH).unwrap();
+ let hash = address(&[1, 2, 3, 4]);
+ let blob = encrypt_blob(&[1, 2, 3, 4], &vek, MANIFEST, &hash).unwrap();
- assert_eq!(decrypt_blob(&blob, &vek, MANIFEST, HASH).unwrap(), vec![1, 2, 3, 4]);
+ assert_eq!(decrypt_blob(&blob, &vek, MANIFEST, SALT, &hash).unwrap(), vec![1, 2, 3, 4]);
assert!(crypto::aes_gcm::decrypt(&blob.encrypted_data, &vek, &[]).is_err(), "the bytes are not encrypted with the VEK itself");
- assert!(decrypt_blob(&blob, &crypto::aes_gcm::generate_key(), MANIFEST, HASH).is_err());
+ assert!(decrypt_blob(&blob, &crypto::aes_gcm::generate_key(), MANIFEST, SALT, &hash).is_err());
}
#[test]
fn a_blob_does_not_open_at_another_address() {
let vek = crypto::aes_gcm::generate_key();
- let blob = encrypt_blob(&[1, 2, 3, 4], &vek, MANIFEST, HASH).unwrap();
- assert!(decrypt_blob(&blob, &vek, MANIFEST, "bb22").is_err(), "served under another hash");
- assert!(decrypt_blob(&blob, &vek, "0a1b2c3d-0000-0000-0000-000000000002", HASH).is_err(), "served under another manifest");
+ let (hash, other_hash) = (address(&[1, 2, 3, 4]), address(&[5, 6]));
+ let blob = encrypt_blob(&[1, 2, 3, 4], &vek, MANIFEST, &hash).unwrap();
+ assert!(decrypt_blob(&blob, &vek, MANIFEST, SALT, &other_hash).is_err(), "served under another hash");
+ assert!(decrypt_blob(&blob, &vek, "0a1b2c3d-0000-0000-0000-000000000002", SALT, &hash).is_err(), "served under another manifest");
// The blob key of one blob next to the bytes of another: neither half opens the other.
- let other = encrypt_blob(&[5, 6], &vek, MANIFEST, "bb22").unwrap();
+ let other = encrypt_blob(&[5, 6], &vek, MANIFEST, &other_hash).unwrap();
let mixed = EncryptedBlob { encrypted_data: other.encrypted_data, encrypted_blob_key: blob.encrypted_blob_key };
- assert!(decrypt_blob(&mixed, &vek, MANIFEST, HASH).is_err());
+ assert!(decrypt_blob(&mixed, &vek, MANIFEST, SALT, &hash).is_err());
+}
+
+#[test]
+fn a_blob_whose_bytes_do_not_hash_to_its_address_is_refused() {
+ // A writer holding the VEK can encrypt any bytes under any address; the address must still match the content.
+ let vek = crypto::aes_gcm::generate_key();
+ let hash = address(&[1, 2, 3, 4]);
+ let forged = encrypt_blob(&[5, 6], &vek, MANIFEST, &hash).unwrap();
+
+ assert!(decrypt_blob(&forged, &vek, MANIFEST, SALT, &hash).is_err());
+ assert!(decrypt_blob(&encrypt_blob(&[1, 2, 3, 4], &vek, MANIFEST, &hash).unwrap(), &vek, MANIFEST, "5a18", &hash).is_err(), "hashed with another manifest's salt");
}
#[test]
fn changing_the_vek_only_encrypts_the_blob_key_again() {
let (old_vek, new_vek) = (crypto::aes_gcm::generate_key(), crypto::aes_gcm::generate_key());
- let blob = encrypt_blob(&[9; 64], &old_vek, MANIFEST, HASH).unwrap();
+ let hash = address(&[9; 64]);
+ let blob = encrypt_blob(&[9; 64], &old_vek, MANIFEST, &hash).unwrap();
- let key_aad = aad::blob_key(MANIFEST, HASH);
+ let key_aad = aad::blob_key(MANIFEST, &hash);
let blob_key = crypto::decrypt_key(&blob.encrypted_blob_key, &old_vek, &key_aad).unwrap();
let rekeyed = EncryptedBlob { encrypted_data: blob.encrypted_data.clone(), encrypted_blob_key: crypto::encrypt_key(&blob_key, &new_vek, &key_aad).unwrap() };
- assert_eq!(decrypt_blob(&rekeyed, &new_vek, MANIFEST, HASH).unwrap(), vec![9; 64]);
- assert!(decrypt_blob(&rekeyed, &old_vek, MANIFEST, HASH).is_err());
+ assert_eq!(decrypt_blob(&rekeyed, &new_vek, MANIFEST, SALT, &hash).unwrap(), vec![9; 64]);
+ assert!(decrypt_blob(&rekeyed, &old_vek, MANIFEST, SALT, &hash).is_err());
}
#[test]
fn a_cached_blob_reads_back_as_it_was_written() {
- let blob = encrypt_blob(&[5], &crypto::aes_gcm::generate_key(), MANIFEST, HASH).unwrap();
+ let blob = encrypt_blob(&[5], &crypto::aes_gcm::generate_key(), MANIFEST, &address(&[5])).unwrap();
let read: EncryptedBlob = serde_json::from_value(serde_json::to_value(&blob).unwrap()).unwrap();
assert_eq!(read, blob);
}
diff --git a/core/rust/src/vault_sync/tests/blobs.rs b/core/rust/src/vault_sync/tests/blobs.rs
index 583322d27..bea32c90d 100644
--- a/core/rust/src/vault_sync/tests/blobs.rs
+++ b/core/rust/src/vault_sync/tests/blobs.rs
@@ -33,6 +33,8 @@ enum NotLoaded {
Withheld,
/// The server served it encrypted under a key this device does not hold.
Undecryptable,
+ /// A writer of the manifest stored other bytes at its address, under the right keys.
+ Forged,
}
/// A device that pulled `vault_with_blobs` while none of its blobs loaded.
@@ -48,6 +50,14 @@ fn synced_without_blobs(how: NotLoaded) -> Synced {
server.borrow_mut().faults.withhold_blobs = true;
}
NotLoaded::Undecryptable => server.borrow_mut().publish_with_blob_key(&db, &vek, &salt, 7, &crypto::aes_gcm::generate_key()),
+ NotLoaded::Forged => {
+ server.borrow_mut().publish(&db, &vek, &salt, 7);
+ for (hash, stored) in server.borrow_mut().blobs.get_mut(PERSONAL_MANIFEST_ID).unwrap().iter_mut() {
+ let forged = blob_keys::encrypt_blob(&[0xee; 16], &vek, PERSONAL_MANIFEST_ID, hash).unwrap();
+ stored["encryptedDataBase64"] = json!(crate::common::encoding::base64_encode(&forged.encrypted_data));
+ stored["encryptedBlobKey"] = json!(forged.encrypted_blob_key);
+ }
+ }
}
let mut host = TestHost::logged_in(&vek, &server);
let pulled = host.sync();
@@ -86,7 +96,7 @@ fn pushed_blobs_are_encrypted_under_their_own_key_and_stored_with_their_manifest
// The uploaded blob opens through its own key at its own address (the key scheme itself is `blob_keys`' to test).
let uploaded: blob_keys::EncryptedBlob = serde_json::from_value(upload["blobs"][0].clone()).unwrap();
- assert_eq!(blob_keys::decrypt_blob(&uploaded, &s.vek, PERSONAL_MANIFEST_ID, hash).unwrap(), vec![1u8, 2, 3, 4]);
+ assert_eq!(blob_keys::decrypt_blob(&uploaded, &s.vek, PERSONAL_MANIFEST_ID, &s.salt, hash).unwrap(), vec![1u8, 2, 3, 4]);
s.server.borrow().assert_converged(&s.host);
}
@@ -109,7 +119,7 @@ fn the_missing_blob_check_stays_within_the_servers_hash_cap() {
#[test]
fn a_blob_that_did_not_load_keeps_its_reference_on_the_next_push() {
- for how in [NotLoaded::Withheld, NotLoaded::Undecryptable] {
+ for how in [NotLoaded::Withheld, NotLoaded::Undecryptable, NotLoaded::Forged] {
let mut s = synced_without_blobs(how);
let server_hashes = s.server.borrow().stored_blob_hashes();