From e1baa257132eed06b5e80d652bbb8d85b6a1c4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 26 Apr 2022 17:47:39 +0200 Subject: [PATCH] fix(sled): Don't derive a StoreCipher twice if we're reusing the db --- crates/matrix-sdk-sled/src/cryptostore.rs | 25 +++++++++++++---------- crates/matrix-sdk-sled/src/state_store.rs | 7 ++----- crates/matrix-sdk/src/store.rs | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/crates/matrix-sdk-sled/src/cryptostore.rs b/crates/matrix-sdk-sled/src/cryptostore.rs index 709c681ea..0eaeba84b 100644 --- a/crates/matrix-sdk-sled/src/cryptostore.rs +++ b/crates/matrix-sdk-sled/src/cryptostore.rs @@ -221,13 +221,23 @@ impl SledStore { .open() .map_err(|e| CryptoStoreError::Backend(anyhow!(e)))?; - SledStore::open_helper(db, Some(path), passphrase) + let store_cipher = if let Some(passphrase) = passphrase { + Some(Self::get_or_create_store_cipher(passphrase, &db)?) + } else { + None + } + .into(); + + SledStore::open_helper(db, Some(path), store_cipher) } /// Create a sled based cryptostore using the given sled database. /// The given passphrase will be used to encrypt private data. - pub fn open_with_database(db: Db, passphrase: Option<&str>) -> Result { - SledStore::open_helper(db, None, passphrase) + pub fn open_with_database( + db: Db, + store_cipher: Arc>, + ) -> Result { + SledStore::open_helper(db, None, store_cipher) } fn get_account_info(&self) -> Option { @@ -347,7 +357,7 @@ impl SledStore { fn open_helper( db: Db, path: Option, - passphrase: Option<&str>, + store_cipher: Arc>, ) -> Result { let account = db.open_tree("account")?; let private_identity = db.open_tree("private_identity")?; @@ -369,13 +379,6 @@ impl SledStore { let session_cache = SessionStore::new(); - let store_cipher = if let Some(passphrase) = passphrase { - Some(Self::get_or_create_store_cipher(passphrase, &db)?) - } else { - None - } - .into(); - let database = Self { account_info: RwLock::new(None).into(), path, diff --git a/crates/matrix-sdk-sled/src/state_store.rs b/crates/matrix-sdk-sled/src/state_store.rs index 58eeae824..f49927229 100644 --- a/crates/matrix-sdk-sled/src/state_store.rs +++ b/crates/matrix-sdk-sled/src/state_store.rs @@ -295,11 +295,8 @@ impl SledStore { /// /// The given passphrase will be used to encrypt private data. #[cfg(feature = "crypto-store")] - pub fn open_crypto_store( - &self, - passphrase: Option<&str>, - ) -> Result { - CryptoStore::open_with_database(self.inner.clone(), passphrase) + pub fn open_crypto_store(&self) -> Result { + CryptoStore::open_with_database(self.inner.clone(), self.store_cipher.clone()) } fn serialize_event(&self, event: &impl Serialize) -> Result, SledStoreError> { diff --git a/crates/matrix-sdk/src/store.rs b/crates/matrix-sdk/src/store.rs index 3ed61c9e5..91101a8f2 100644 --- a/crates/matrix-sdk/src/store.rs +++ b/crates/matrix-sdk/src/store.rs @@ -80,11 +80,11 @@ fn open_stores_with_path( ) -> Result<(Box, Box), OpenStoreError> { if let Some(passphrase) = passphrase { let state_store = StateStore::open_with_passphrase(path, passphrase)?; - let crypto_store = state_store.open_crypto_store(Some(passphrase))?; + let crypto_store = state_store.open_crypto_store()?; Ok((Box::new(state_store), Box::new(crypto_store))) } else { let state_store = StateStore::open_with_path(path)?; - let crypto_store = state_store.open_crypto_store(None)?; + let crypto_store = state_store.open_crypto_store()?; Ok((Box::new(state_store), Box::new(crypto_store))) } }