Skip to content

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

Add central tokens.rs (#2468)

Leendert de Borstad50c65ea3a2
diff --git a/core/rust/src/crypto/key_chain.rs b/core/rust/src/crypto/key_chain.rs
index 3f97e72a4..86955903c 100644
--- a/core/rust/src/crypto/key_chain.rs
+++ b/core/rust/src/crypto/key_chain.rs
@@ -13,14 +13,12 @@ use super::aad;
use super::aes_gcm::{generate_key_base64, symmetric_decrypt_bytes_with_aad, symmetric_decrypt_with_aad, symmetric_encrypt_bytes_with_aad, symmetric_encrypt_with_aad};
use super::rsa_oaep::{generate_rsa_key_pair, validate_rsa_key_pair, RsaKeyPair};
use super::signing::{self, generate_signing_key_pair};
+use super::tokens::{ALGORITHM_AES256_GCM, ENCRYPTION_TYPE_ARGON2ID, ENCRYPTION_TYPE_ARGON2ID_HKDF};
use crate::common::encoding::{base64_decode, base64_encode, hex_encode_upper};
use crate::common::error::VaultResult;
-/// The algorithm this build encrypts every Account Key with, as a VaultKeyAlgorithm token.
-pub const ACCOUNT_KEY_WRAP_ALGORITHM: &str = "aes256-gcm";
-
fn account_key_wrap_algorithm() -> String {
- ACCOUNT_KEY_WRAP_ALGORITHM.to_string()
+ ALGORITHM_AES256_GCM.to_string()
}
/// The wrapped halves of an account key hierarchy: what the server stores.
@@ -70,12 +68,6 @@ 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.
const SRP_INFO: &[u8] = b"AliasVault SRP v1";
-/// The `EncryptionType` of a verifier made from the unlock key itself (accounts created before 0.31.0).
-pub const ENCRYPTION_TYPE_ARGON2ID: &str = "Argon2Id";
-
-/// The `EncryptionType` of a verifier made from the HKDF-derived SRP input: every new account and password change.
-pub const ENCRYPTION_TYPE_ARGON2ID_HKDF: &str = "Argon2IdHkdf";
-
/// Why an SRP password hash could not be derived.
#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
@@ -380,4 +372,17 @@ mod tests {
assert!(open_account_private_key(&blobs.encrypted_account_private_key, &account_key).is_ok());
assert!(symmetric_decrypt_with_aad(&blobs.encrypted_account_private_key, &account_key, aad::PERSONAL_VEK).is_err());
}
+
+ #[test]
+ fn an_account_key_encrypted_under_any_other_label_is_refused() {
+ let unlock_key = generate_key_base64();
+ let hierarchy = create_account_key_hierarchy(&unlock_key).unwrap();
+ let account_key = unwrap_account_key(&hierarchy.account_keys.encrypted_account_key, &unlock_key).unwrap();
+ let kek = derive_kek_base64(&unlock_key).unwrap();
+ for label in [aad::PERSONAL_VEK, aad::ACCOUNT_PRIVATE_KEY, aad::ACCOUNT_SIGNING_PRIVATE_KEY, aad::MOBILE_LOGIN_ACCOUNT_KEY, aad::MOBILE_LOGIN_PAYLOAD_KEY, b""] {
+ let foreign = wrap_key(&account_key, &kek, label).unwrap();
+ let opened = open_account_key_chain(&unlock_key, &foreign, &hierarchy.account_keys.encrypted_vek, None);
+ assert!(matches!(opened, Err(KeyChainError::UnlockKeyRejected)), "label {:?}", String::from_utf8_lossy(label));
+ }
+ }
}
diff --git a/core/rust/src/crypto/mod.rs b/core/rust/src/crypto/mod.rs
index 4cb7fa1fb..434730cd2 100644
--- a/core/rust/src/crypto/mod.rs
+++ b/core/rust/src/crypto/mod.rs
@@ -8,6 +8,7 @@ pub mod pin;
pub mod rsa_oaep;
pub mod signing;
pub mod srp;
+pub mod tokens;
pub use aes_gcm::{
generate_key_base64, symmetric_decrypt, symmetric_decrypt_bytes, symmetric_decrypt_bytes_with_aad, symmetric_decrypt_with_aad, symmetric_encrypt, symmetric_encrypt_bytes, symmetric_encrypt_bytes_with_aad,
diff --git a/core/rust/src/crypto/tokens.rs b/core/rust/src/crypto/tokens.rs
new file mode 100644
index 000000000..6ecc2001f
--- /dev/null
+++ b/core/rust/src/crypto/tokens.rs
@@ -0,0 +1,30 @@
+//! Const tokens that name an algorithm, key type or KDF; each is stored or sent as-is and never changes.
+
+/// AES-256-GCM: the algorithm every Account Key and personal VEK is encrypted with (VaultKeyAlgorithm).
+pub const ALGORITHM_AES256_GCM: &str = "aes256-gcm";
+
+/// RSA-OAEP with SHA-256: grants and delivery keys.
+pub const ALGORITHM_RSA_OAEP_SHA256: &str = "rsa-oaep-sha256";
+
+/// A manifest whose VEK is encrypted with the Account Key (the personal manifest).
+pub const KEY_TYPE_ACCOUNT_KEY: &str = "account-key";
+
+/// A manifest whose VEK reaches this account as an RSA-OAEP grant (a shared manifest).
+pub const KEY_TYPE_GRANT_KEY: &str = "grant-key";
+
+/// The `EncryptionType` of a verifier made from the unlock key itself (accounts created before 0.31.0).
+pub const ENCRYPTION_TYPE_ARGON2ID: &str = "Argon2Id";
+
+/// The `EncryptionType` of a verifier made from the HKDF-derived SRP input: every new account and password change (since 0.31.0).
+pub const ENCRYPTION_TYPE_ARGON2ID_HKDF: &str = "Argon2IdHkdf";
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn tokens_are_stable() {
+ let tokens = [ALGORITHM_AES256_GCM, ALGORITHM_RSA_OAEP_SHA256, KEY_TYPE_ACCOUNT_KEY, KEY_TYPE_GRANT_KEY, ENCRYPTION_TYPE_ARGON2ID, ENCRYPTION_TYPE_ARGON2ID_HKDF];
+ assert_eq!(tokens, ["aes256-gcm", "rsa-oaep-sha256", "account-key", "grant-key", "Argon2Id", "Argon2IdHkdf"]);
+ }
+}
diff --git a/core/rust/src/vault_sync/types.rs b/core/rust/src/vault_sync/types.rs
index 80c93f4fd..0970df36f 100644
--- a/core/rust/src/vault_sync/types.rs
+++ b/core/rust/src/vault_sync/types.rs
@@ -1,5 +1,7 @@
//! Every JSON shape the engine exchanges.
+use crate::common::secret::serialize_option;
+use secrecy::SecretString;
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
@@ -50,14 +52,14 @@ pub struct SharingParams {
pub struct SyncRequest {
pub operation: SyncOperation,
pub username: String,
- #[serde(default)]
- pub encryption_key: Option<String>,
+ #[serde(default, serialize_with = "serialize_option")]
+ pub encryption_key: Option<SecretString>,
#[serde(default)]
pub account_public_key: Option<String>,
- #[serde(default)]
- pub account_private_key: Option<String>,
- #[serde(default)]
- pub account_key: Option<String>,
+ #[serde(default, serialize_with = "serialize_option")]
+ pub account_private_key: Option<SecretString>,
+ #[serde(default, serialize_with = "serialize_option")]
+ pub account_key: Option<SecretString>,
#[serde(default)]
pub is_dirty: bool,
#[serde(default)]
@@ -172,8 +174,8 @@ pub struct ResolveVaultKeyResult {
pub success: bool,
/// Whether the account has a key chain (false: legacy vault, the migration push creates one).
pub has_vault_key: bool,
- #[serde(skip_serializing_if = "Option::is_none")]
- pub encryption_key: Option<String>,
+ #[serde(skip_serializing_if = "Option::is_none", serialize_with = "serialize_option")]
+ pub encryption_key: Option<SecretString>,
#[serde(flatten)]
pub failure: FailureFields,
}
@@ -283,8 +285,8 @@ pub enum Command {
VaultStore {
encrypted_blob: String,
mark_dirty: bool,
- #[serde(skip_serializing_if = "Option::is_none")]
- encryption_key: Option<String>,
+ #[serde(skip_serializing_if = "Option::is_none", serialize_with = "serialize_option")]
+ encryption_key: Option<SecretString>,
#[serde(skip_serializing_if = "Option::is_none")]
expected_mutation_seq: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
@@ -537,10 +539,7 @@ pub struct EmailDomainsDto {
pub public_email_domain_list: Vec<String>,
}
-pub const KEY_TYPE_ACCOUNT_KEY: &str = "account-key";
-pub const KEY_TYPE_GRANT_KEY: &str = "grant-key";
-pub const ALGORITHM_RSA_OAEP_SHA256: &str = "rsa-oaep-sha256";
-pub const ALGORITHM_AES256_GCM: &str = crate::crypto::key_chain::ACCOUNT_KEY_WRAP_ALGORITHM;
+pub use crate::crypto::tokens::{ALGORITHM_AES256_GCM, ALGORITHM_RSA_OAEP_SHA256, KEY_TYPE_ACCOUNT_KEY, KEY_TYPE_GRANT_KEY};
/// One manifest element of `POST v2/Vault`.
#[derive(Debug, Clone, Serialize)]