Skip to content

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

Refactor rotate keychain to reuse create account hierarchy model

Leendert de Borstae740d3273e5
diff --git a/core/rust/src/crypto/key_chain.rs b/core/rust/src/crypto/key_chain.rs
index 61c0e0c7b..4a4383320 100644
--- a/core/rust/src/crypto/key_chain.rs
+++ b/core/rust/src/crypto/key_chain.rs
@@ -1,6 +1,6 @@
//! The account key hierarchy: the KEK encrypts the Account Key, which encrypts the Vault Encryption Key, the account
-//! private key and the account signing private key.
-//! The KEK and SRP password hash are each HKDF-derived from the unlock key using separate labels.
+//! private key and the account signing private key. The KEK and SRP password hash are each HKDF-derived from the unlock
+//! key using separate labels.
use hkdf::Hkdf;
use serde::{Deserialize, Serialize};
@@ -26,7 +26,6 @@ fn account_key_encryption_algorithm() -> String {
#[serde(rename_all = "camelCase")]
pub struct AccountKeyBlobs {
pub encrypted_account_key: String,
- /// The algorithm of `encrypted_account_key`, as a VaultKeyAlgorithm token.
#[serde(default = "account_key_encryption_algorithm")]
pub encrypted_account_key_algorithm: String,
pub encrypted_vek: String,
@@ -34,8 +33,6 @@ pub struct AccountKeyBlobs {
pub encrypted_account_private_key: String,
pub signing_public_key: String,
pub encrypted_signing_private_key: String,
-
- /// The signing key's signature over `account_public_key` (see [`signing::ACCOUNT_PUBLIC_KEY_LABEL`]).
pub account_public_key_signature: String,
}
@@ -61,7 +58,7 @@ impl fmt::Debug for AccountKeyHierarchy {
/// HKDF `info` that binds the derived key to its use as the KEK; changing it breaks every stored Account Key.
const KEK_INFO: &[u8] = b"AliasVault KEK v1";
-/// HKDF `info` that binds the derived key to its use as the SRP input; changing it breaks every upgraded verifier.
+/// HKDF `info` that binds the derived key to its use as the SRP input; changing it breaks every verifier.
const SRP_INFO: &[u8] = b"AliasVault SRP v1";
/// Why an SRP password hash could not be derived.
@@ -170,7 +167,7 @@ pub struct OpenedKeyChain {
}
/// Open a key chain with the unlock key (the password's Argon2id output): the KEK derived from it decrypts the Account
-/// Key, which decrypts the VEK. Never accepts the Account Key itself; see [`open_key_chain_with_account_key`].
+/// Key, which decrypts the VEK.
pub fn open_key_chain_with_unlock_key(unlock_key_base64: &str, encrypted_account_key: &str, encrypted_vek: &str, encrypted_account_private_key: Option<&str>) -> Result<OpenedKeyChain, KeyChainError> {
let account_key = decrypt_account_key(encrypted_account_key, unlock_key_base64).map_err(|_| KeyChainError::UnlockKeyRejected)?;
let vault_encryption_key = decrypt_key(encrypted_vek, &account_key, aad::PERSONAL_VEK).map_err(|e| KeyChainError::KeyChainUnreadable(e.to_string()))?;
@@ -178,20 +175,20 @@ pub fn open_key_chain_with_unlock_key(unlock_key_base64: &str, encrypted_account
Ok(OpenedKeyChain { vault_encryption_key, account_key, account_private_key })
}
-/// Open a key chain with the Account Key, as a device unlock method or an unlocked session holds it. Never accepts an
-/// unlock key; see [`open_key_chain_with_unlock_key`].
+/// Open a key chain with the Account Key, as a device unlock method or an unlocked session holds it.
pub fn open_key_chain_with_account_key(account_key_base64: &str, encrypted_vek: &str, encrypted_account_private_key: Option<&str>) -> Result<OpenedKeyChain, KeyChainError> {
let vault_encryption_key = decrypt_key(encrypted_vek, account_key_base64, aad::PERSONAL_VEK).map_err(|_| KeyChainError::AccountKeyRejected)?;
let account_private_key = open_optional_account_private_key(encrypted_account_private_key, account_key_base64);
Ok(OpenedKeyChain { vault_encryption_key, account_key: Zeroizing::new(account_key_base64.to_string()), account_private_key })
}
-/// The account private key when the chain carries one that opens; a failure keeps grants closed, not the vault.
+/// The account private key when the chain carries one that opens.
fn open_optional_account_private_key(encrypted_account_private_key: Option<&str>, account_key_base64: &str) -> Option<Zeroizing<String>> {
encrypted_account_private_key.filter(|e| !e.is_empty()).and_then(|e| open_account_private_key(e, account_key_base64).ok())
}
-/// Create a new account key hierarchy encrypted with the KEK derived from an unlock key.
+/// Create a new Account Key, VEK, account keypair and signing keypair, with the Account Key encrypted under the KEK of
+/// `unlock_key_base64`.
pub fn create_account_key_hierarchy(unlock_key_base64: &str) -> VaultResult<AccountKeyHierarchy> {
let key_pair = generate_rsa_key_pair()?;
let vault_encryption_key = generate_key();
@@ -229,36 +226,3 @@ pub fn reencrypt_account_key(encrypted_account_key: &str, old_unlock_key_base64:
let new_encrypted_account_key = encrypt_account_key(&account_key, new_unlock_key_base64).map_err(|e| KeyChainError::KeyChainUnreadable(e.to_string()))?;
Ok(ReencryptedAccountKey { account_key: account_key.to_string(), new_encrypted_account_key })
}
-
-/// A rotated account key hierarchy: the new encrypted blobs the server stores, what ties them to the old generation,
-/// and the plaintext halves the client keeps.
-#[derive(Clone, Serialize, Deserialize, Zeroize, ZeroizeOnDrop)]
-#[serde(rename_all = "camelCase")]
-pub struct RotatedAccountKeys {
- pub account_key: String,
- pub vault_encryption_key: String,
- pub account_private_key: String,
- pub signing_private_key: String,
-
- #[zeroize(skip)]
- pub account_keys: AccountKeyBlobs,
-}
-
-impl fmt::Debug for RotatedAccountKeys {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- f.debug_struct("RotatedAccountKeys").field("account_keys", &self.account_keys).finish_non_exhaustive()
- }
-}
-
-/// Rotate an account's keys: a new Account Key, VEK, account keypair and signing keypair, encrypted with the KEK of
-/// `new_unlock_key_base64`.
-pub fn rotate_account_keys(new_unlock_key_base64: &str) -> VaultResult<RotatedAccountKeys> {
- let hierarchy = create_account_key_hierarchy(new_unlock_key_base64)?;
- Ok(RotatedAccountKeys {
- account_key: hierarchy.account_key.clone(),
- vault_encryption_key: hierarchy.vault_encryption_key.clone(),
- account_private_key: hierarchy.account_private_key.clone(),
- signing_private_key: hierarchy.signing_private_key.clone(),
- account_keys: hierarchy.account_keys.clone(),
- })
-}
diff --git a/core/rust/src/crypto/mod.rs b/core/rust/src/crypto/mod.rs
index dc162c662..101232a10 100644
--- a/core/rust/src/crypto/mod.rs
+++ b/core/rust/src/crypto/mod.rs
@@ -36,8 +36,8 @@ mod tests;
pub use avex::{avex_decrypt, avex_encrypt, avex_parse_header, AvexError, AvexHeader};
pub use key_chain::{
account_signing_key_fingerprint, create_account_key_hierarchy, derive_srp_password_hash, open_account_private_key, open_account_signing_private_key,
- decrypt_account_key, decrypt_key, encrypt_key, open_key_chain_with_account_key, open_key_chain_with_unlock_key, reencrypt_account_key, rotate_account_keys, AccountKeyBlobs, AccountKeyHierarchy, KeyChainError,
- RotatedAccountKeys, SrpInputError,
+ decrypt_account_key, decrypt_key, encrypt_key, open_key_chain_with_account_key, open_key_chain_with_unlock_key, reencrypt_account_key, AccountKeyBlobs, AccountKeyHierarchy, KeyChainError,
+ SrpInputError,
};
pub use mobile_login::{mobile_login_encrypt_account_key, mobile_login_open_approval, mobile_login_public_key_hash, mobile_login_verification_code, MobileLoginApproval};
pub use pin::{pin_decrypt, pin_encrypt, pin_extension_salt, pin_generate_salt, pin_is_locked, pin_register_failure, PinFailure};
diff --git a/core/rust/src/crypto/tests/key_chain.rs b/core/rust/src/crypto/tests/key_chain.rs
index 55fd10334..72328d10c 100644
--- a/core/rust/src/crypto/tests/key_chain.rs
+++ b/core/rust/src/crypto/tests/key_chain.rs
@@ -114,27 +114,6 @@ fn a_private_key_that_does_not_open_leaves_grants_closed_but_opens_the_vault() {
assert!(open_key_chain_with_unlock_key(&unlock_key, &blobs.encrypted_account_key, &blobs.encrypted_vek, Some("")).unwrap().account_private_key.is_none());
}
-#[test]
-fn rotated_keys_open_with_the_new_unlock_key() {
- let old_hierarchy = create_account_key_hierarchy(&generate_key()).unwrap();
- let new_unlock_key = generate_key();
-
- let rotated = rotate_account_keys(&new_unlock_key).unwrap();
- let blobs = &rotated.account_keys;
-
- // The new chain opens with the new unlock key and yields the new VEK and private keys.
- let opened = open_key_chain_with_unlock_key(&new_unlock_key, &blobs.encrypted_account_key, &blobs.encrypted_vek, Some(&blobs.encrypted_account_private_key)).unwrap();
- assert_eq!(*opened.vault_encryption_key, rotated.vault_encryption_key);
- assert_eq!(*opened.account_key, rotated.account_key);
- assert_eq!(opened.account_private_key.as_deref().map(String::as_str), Some(rotated.account_private_key.as_str()));
- assert_ne!(rotated.vault_encryption_key, old_hierarchy.vault_encryption_key);
- assert_ne!(blobs.account_public_key, old_hierarchy.account_keys.account_public_key);
- assert_ne!(blobs.signing_public_key, old_hierarchy.account_keys.signing_public_key);
-
- // The new signing key vouches for the new account public key.
- assert!(signing::verify(&blobs.signing_public_key, &signing::account_public_key_message(&blobs.account_public_key), &blobs.account_public_key_signature));
-}
-
#[test]
fn hierarchy_carries_a_signing_key_that_signed_the_account_public_key() {
let unlock_key = generate_key();
diff --git a/core/rust/src/vault_sync/rotation.rs b/core/rust/src/vault_sync/rotation.rs
index 5a2caa554..e4fba1be0 100644
--- a/core/rust/src/vault_sync/rotation.rs
+++ b/core/rust/src/vault_sync/rotation.rs
@@ -197,7 +197,7 @@ async fn rotate(ctx: &mut Ctx) -> Outcome {
let blob_keys: BlobKeysResponse = http::with_outdated_server_guard(http::get(&ctx.host, &format!("Vault/blob-keys/{}", personal_manifest_id), false).await)?;
let certifications: Vec<CertificationEntry> = http::with_outdated_server_guard(http::get(&ctx.host, "Groups/certifications", false).await)?;
- let rotated = crypto::rotate_account_keys(params.new_unlock_key.expose_secret())?;
+ let rotated = crypto::create_account_key_hierarchy(params.new_unlock_key.expose_secret())?;
let new_vek = SecretString::from(rotated.vault_encryption_key.as_str());
ctx.log("[KeyRotation] Generated the new Account Key, VEK, account keypair and signing keypair.").await;
@@ -230,7 +230,7 @@ type Built = (Applied, HashMap<String, String>, HashMap<String, SharedManifestDt
/// Encrypt everything under the new keys, send it, and read the server's answer.
#[allow(clippy::too_many_arguments)]
-async fn build_and_post(ctx: &mut Ctx, params: &KeyRotationParams, personal_manifest_id: &str, rotated: &crypto::RotatedAccountKeys, current_signing_key: &str, new_vek: &SecretString, old_vek: &SecretString, shared_records: &HashMap<String, SharedManifestDto>, shared_veks: &HashMap<String, SecretString>, server_blob_keys: &[BlobKeyEntry], server_certifications: &[CertificationEntry], delivery_public_key: &str) -> SyncResult<Result<Built, KeyRotationRefusal>> {
+async fn build_and_post(ctx: &mut Ctx, params: &KeyRotationParams, personal_manifest_id: &str, rotated: &crypto::AccountKeyHierarchy, current_signing_key: &str, new_vek: &SecretString, old_vek: &SecretString, shared_records: &HashMap<String, SharedManifestDto>, shared_veks: &HashMap<String, SecretString>, server_blob_keys: &[BlobKeyEntry], server_certifications: &[CertificationEntry], delivery_public_key: &str) -> SyncResult<Result<Built, KeyRotationRefusal>> {
let manifest_revisions: HashMap<String, i64> = state::get(&ctx.host, state::SERVER_MANIFEST_REVISIONS).await?.unwrap_or_default();
let bucket_revisions: HashMap<String, i64> = state::get(&ctx.host, state::VAULT_BUCKET_REVISIONS).await?.unwrap_or_default();
let current_revision = manifest_revisions.get(personal_manifest_id).copied().unwrap_or(0);
@@ -376,7 +376,7 @@ async fn build_and_post(ctx: &mut Ctx, params: &KeyRotationParams, personal_mani
/// Refuse to send a rotation whose new key chain does not open from the new password down to the VEKs: once the
/// server applies it, the old password no longer reaches any key, so a broken chain could not be recovered.
-fn ensure_key_chain_opens(params: &KeyRotationParams, rotated: &crypto::RotatedAccountKeys, new_vek: &SecretString) -> SyncResult<()> {
+fn ensure_key_chain_opens(params: &KeyRotationParams, rotated: &crypto::AccountKeyHierarchy, new_vek: &SecretString) -> SyncResult<()> {
let blobs = &rotated.account_keys;
let account_key = crypto::decrypt_account_key(&blobs.encrypted_account_key, params.new_unlock_key.expose_secret()).ok().filter(|key| **key == rotated.account_key);
ensure_opens(account_key.is_some(), "the new Account Key")?;
@@ -405,7 +405,7 @@ fn ensure_opens(opens: bool, what: &str) -> SyncResult<()> {
/// Take the new keys over: the cached chain, the derivation parameters, the revisions and fingerprints the server
/// now holds, the blob keys and grants under the new keys, and the stored vault under the new VEK.
#[allow(clippy::too_many_arguments)]
-async fn adopt(ctx: &mut Ctx, params: &KeyRotationParams, personal_manifest_id: &str, rotated: &crypto::RotatedAccountKeys, new_vek: &SecretString, applied: &Applied, reencrypted_blob_keys: &HashMap<String, String>, reencrypted_grants: &HashMap<String, SharedManifestDto>, fingerprints: &HashMap<String, String>) -> SyncResult<()> {
+async fn adopt(ctx: &mut Ctx, params: &KeyRotationParams, personal_manifest_id: &str, rotated: &crypto::AccountKeyHierarchy, new_vek: &SecretString, applied: &Applied, reencrypted_blob_keys: &HashMap<String, String>, reencrypted_grants: &HashMap<String, SharedManifestDto>, fingerprints: &HashMap<String, String>) -> SyncResult<()> {
let blobs = &rotated.account_keys;
state::set(&ctx.host, state::ENCRYPTED_ACCOUNT_KEY, &blobs.encrypted_account_key).await?;
state::set(&ctx.host, state::ENCRYPTED_VEK, &blobs.encrypted_vek).await?;