From adf8905d9fe2cb3b6bbb4fecfe76aba1880e992e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 4 Sep 2020 12:42:40 +0200 Subject: [PATCH] crypto: Rename the memory stores into caches and reorder the store module. --- matrix_sdk_base/src/client.rs | 4 +- matrix_sdk_base/src/lib.rs | 4 +- matrix_sdk_crypto/src/identities/device.rs | 4 +- matrix_sdk_crypto/src/identities/user.rs | 2 +- matrix_sdk_crypto/src/lib.rs | 7 +- matrix_sdk_crypto/src/machine.rs | 3 +- .../src/{memory_stores.rs => store/caches.rs} | 4 +- matrix_sdk_crypto/src/store/memorystore.rs | 21 ++++-- matrix_sdk_crypto/src/store/mod.rs | 66 ++++++++++++++----- matrix_sdk_crypto/src/store/sqlite.rs | 9 ++- matrix_sdk_crypto/src/verification/machine.rs | 10 ++- matrix_sdk_crypto/src/verification/sas/mod.rs | 7 +- 12 files changed, 92 insertions(+), 49 deletions(-) rename matrix_sdk_crypto/src/{memory_stores.rs => store/caches.rs} (99%) diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index 5f2e14ea..97532ada 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -46,8 +46,8 @@ use matrix_sdk_common::{ }; #[cfg(feature = "encryption")] use matrix_sdk_crypto::{ - CryptoStore, CryptoStoreError, Device, IncomingResponse, OlmError, OlmMachine, OutgoingRequest, - Sas, UserDevices, + store::{CryptoStore, CryptoStoreError}, + Device, IncomingResponse, OlmError, OlmMachine, OutgoingRequest, Sas, UserDevices, }; use zeroize::Zeroizing; diff --git a/matrix_sdk_base/src/lib.rs b/matrix_sdk_base/src/lib.rs index f96d1728..c1002046 100644 --- a/matrix_sdk_base/src/lib.rs +++ b/matrix_sdk_base/src/lib.rs @@ -57,8 +57,8 @@ pub use state::{AllRooms, ClientState}; #[cfg(feature = "encryption")] #[cfg_attr(feature = "docs", doc(cfg(encryption)))] pub use matrix_sdk_crypto::{ - CryptoStoreError, Device, IncomingResponse, LocalTrust, OutgoingRequest, OutgoingRequests, - ReadOnlyDevice, Sas, UserDevices, + store::CryptoStoreError, Device, IncomingResponse, LocalTrust, OutgoingRequest, + OutgoingRequests, ReadOnlyDevice, Sas, UserDevices, }; #[cfg(feature = "messages")] diff --git a/matrix_sdk_crypto/src/identities/device.rs b/matrix_sdk_crypto/src/identities/device.rs index 925b7f83..60b87925 100644 --- a/matrix_sdk_crypto/src/identities/device.rs +++ b/matrix_sdk_crypto/src/identities/device.rs @@ -41,9 +41,9 @@ use crate::{Account, OlmMachine}; use crate::{ error::{EventError, OlmError, OlmResult, SignatureError}, identities::{OwnUserIdentity, UserIdentities}, - store::Result as StoreResult, + store::{caches::ReadOnlyUserDevices, Result as StoreResult}, verification::VerificationMachine, - verify_json, ReadOnlyUserDevices, Sas, + verify_json, Sas, }; /// A read-only version of a `Device`. diff --git a/matrix_sdk_crypto/src/identities/user.rs b/matrix_sdk_crypto/src/identities/user.rs index a1cd66aa..7d7acfad 100644 --- a/matrix_sdk_crypto/src/identities/user.rs +++ b/matrix_sdk_crypto/src/identities/user.rs @@ -513,7 +513,7 @@ mod test { identities::{Device, ReadOnlyDevice}, machine::test::response_from_file, olm::Account, - store::memorystore::MemoryStore, + store::MemoryStore, verification::VerificationMachine, }; diff --git a/matrix_sdk_crypto/src/lib.rs b/matrix_sdk_crypto/src/lib.rs index 675e5668..0280d408 100644 --- a/matrix_sdk_crypto/src/lib.rs +++ b/matrix_sdk_crypto/src/lib.rs @@ -30,10 +30,9 @@ mod error; mod identities; mod machine; -pub mod memory_stores; pub mod olm; mod requests; -mod store; +pub mod store; mod verification; pub use error::{MegolmError, OlmError}; @@ -41,13 +40,9 @@ pub use identities::{ Device, LocalTrust, OwnUserIdentity, ReadOnlyDevice, UserDevices, UserIdentities, UserIdentity, }; pub use machine::OlmMachine; -pub use memory_stores::ReadOnlyUserDevices; pub(crate) use olm::Account; pub use olm::EncryptionSettings; pub use requests::{IncomingResponse, OutgoingRequest, OutgoingRequests}; -#[cfg(feature = "sqlite_cryptostore")] -pub use store::sqlite::SqliteStore; -pub use store::{CryptoStore, CryptoStoreError}; pub use verification::Sas; use error::SignatureError; diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index 0d9a8987..95a390bf 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -63,9 +63,8 @@ use super::{ OlmMessage, OutboundGroupSession, }, requests::{IncomingResponse, OutgoingRequest}, - store::{memorystore::MemoryStore, Result as StoreResult}, + store::{CryptoStore, MemoryStore, Result as StoreResult}, verification::{Sas, VerificationMachine}, - CryptoStore, }; /// State machine implementation of the Olm/Megolm encryption protocol used for diff --git a/matrix_sdk_crypto/src/memory_stores.rs b/matrix_sdk_crypto/src/store/caches.rs similarity index 99% rename from matrix_sdk_crypto/src/memory_stores.rs rename to matrix_sdk_crypto/src/store/caches.rs index 83ee06fb..91344257 100644 --- a/matrix_sdk_crypto/src/memory_stores.rs +++ b/matrix_sdk_crypto/src/store/caches.rs @@ -25,7 +25,7 @@ use matrix_sdk_common::{ locks::Mutex, }; -use super::{ +use crate::{ identities::ReadOnlyDevice, olm::{InboundGroupSession, Session}, }; @@ -209,8 +209,8 @@ impl DeviceStore { mod test { use crate::{ identities::device::test::get_device, - memory_stores::{DeviceStore, GroupSessionStore, SessionStore}, olm::{test::get_account_and_session, InboundGroupSession}, + store::caches::{DeviceStore, GroupSessionStore, SessionStore}, }; use matrix_sdk_common::identifiers::room_id; diff --git a/matrix_sdk_crypto/src/store/memorystore.rs b/matrix_sdk_crypto/src/store/memorystore.rs index d43ee531..f7d3d753 100644 --- a/matrix_sdk_crypto/src/store/memorystore.rs +++ b/matrix_sdk_crypto/src/store/memorystore.rs @@ -21,11 +21,13 @@ use matrix_sdk_common::{ }; use matrix_sdk_common_macros::async_trait; -use super::{Account, CryptoStore, InboundGroupSession, Result, Session}; -use crate::{ - identities::{ReadOnlyDevice, UserIdentities}, - memory_stores::{DeviceStore, GroupSessionStore, ReadOnlyUserDevices, SessionStore}, +use super::{ + caches::{DeviceStore, GroupSessionStore, ReadOnlyUserDevices, SessionStore}, + Account, CryptoStore, InboundGroupSession, Result, Session, }; +use crate::identities::{ReadOnlyDevice, UserIdentities}; + +/// An in-memory only store that will forget all the E2EE key once it's dropped. #[derive(Debug, Clone)] pub struct MemoryStore { sessions: SessionStore, @@ -36,8 +38,8 @@ pub struct MemoryStore { identities: Arc>, } -impl MemoryStore { - pub fn new() -> Self { +impl Default for MemoryStore { + fn default() -> Self { MemoryStore { sessions: SessionStore::new(), inbound_group_sessions: GroupSessionStore::new(), @@ -49,6 +51,13 @@ impl MemoryStore { } } +impl MemoryStore { + /// Create a new empty `MemoryStore`. + pub fn new() -> Self { + Self::default() + } +} + #[async_trait] impl CryptoStore for MemoryStore { async fn load_account(&self) -> Result> { diff --git a/matrix_sdk_crypto/src/store/mod.rs b/matrix_sdk_crypto/src/store/mod.rs index 9fc1dc04..a7cce545 100644 --- a/matrix_sdk_crypto/src/store/mod.rs +++ b/matrix_sdk_crypto/src/store/mod.rs @@ -12,8 +12,55 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Types and traits to implement the storage layer for the [`OlmMachine`] +//! +//! The storage layer for the [`OlmMachine`] can be customized using a trait. +//! Implementing your own [`CryptoStore`] +//! +//! An in-memory only store is provided as well as a SQLite based one, depending +//! on your needs and targets a custom store may be implemented, e.g. for +//! `wasm-unknown-unknown` an indexeddb store would be needed +//! +//! ``` +//! # use matrix_sdk_crypto::{ +//! # OlmMachine, +//! # store::MemoryStore, +//! # }; +//! # use matrix_sdk_common::identifiers::{user_id, DeviceIdBox}; +//! # let user_id = user_id!("@example:localhost"); +//! # let device_id: DeviceIdBox = "TEST".into(); +//! let store = Box::new(MemoryStore::new()); +//! +//! let machine = OlmMachine::new_with_store(user_id, device_id, store); +//! ``` +//! +//! [`OlmMachine`]: /matrix_sdk_crypto/struct.OlmMachine.html +//! [`CryptoStore`]: trait.Cryptostore.html + +pub mod caches; +mod memorystore; +#[cfg(not(target_arch = "wasm32"))] +#[cfg(feature = "sqlite_cryptostore")] +pub(crate) mod sqlite; + +use caches::ReadOnlyUserDevices; +pub use memorystore::MemoryStore; +#[cfg(not(target_arch = "wasm32"))] +#[cfg(feature = "sqlite_cryptostore")] +pub use sqlite::SqliteStore; + use std::{collections::HashSet, fmt::Debug, io::Error as IoError, sync::Arc}; +use olm_rs::errors::{OlmAccountError, OlmGroupSessionError, OlmSessionError}; +use serde_json::Error as SerdeError; +use thiserror::Error; +use url::ParseError; + +#[cfg_attr(feature = "docs", doc(cfg(r#sqlite_cryptostore)))] +#[cfg(not(target_arch = "wasm32"))] +#[cfg(feature = "sqlite_cryptostore")] +use sqlx::Error as SqlxError; + use matrix_sdk_common::{ identifiers::{DeviceId, Error as IdentifierValidationError, RoomId, UserId}, locks::Mutex, @@ -22,28 +69,15 @@ use matrix_sdk_common_macros::async_trait; #[cfg(not(target_arch = "wasm32"))] use matrix_sdk_common_macros::send_sync; -use olm_rs::errors::{OlmAccountError, OlmGroupSessionError, OlmSessionError}; -use serde_json::Error as SerdeError; -use thiserror::Error; -use url::ParseError; - use super::{ identities::{ReadOnlyDevice, UserIdentities}, - memory_stores::ReadOnlyUserDevices, olm::{Account, InboundGroupSession, Session}, }; use crate::error::SessionUnpicklingError; -pub mod memorystore; - -#[cfg(not(target_arch = "wasm32"))] -#[cfg(feature = "sqlite_cryptostore")] -pub mod sqlite; - -#[cfg(not(target_arch = "wasm32"))] -#[cfg(feature = "sqlite_cryptostore")] -use sqlx::Error as SqlxError; +/// A `CryptoStore` specific result type. +pub type Result = std::result::Result; #[derive(Error, Debug)] /// The crypto store's error type. @@ -93,8 +127,6 @@ pub enum CryptoStoreError { UrlParse(#[from] ParseError), } -pub type Result = std::result::Result; - /// Trait abstracting a store that the `OlmMachine` uses to store cryptographic /// keys. #[async_trait] diff --git a/matrix_sdk_crypto/src/store/sqlite.rs b/matrix_sdk_crypto/src/store/sqlite.rs index f29a4ce4..50b0b5f4 100644 --- a/matrix_sdk_crypto/src/store/sqlite.rs +++ b/matrix_sdk_crypto/src/store/sqlite.rs @@ -33,10 +33,12 @@ use sqlx::{query, query_as, sqlite::SqliteQueryAs, Connect, Executor, SqliteConn use url::Url; use zeroize::Zeroizing; -use super::{CryptoStore, CryptoStoreError, Result}; +use super::{ + caches::{DeviceStore, GroupSessionStore, ReadOnlyUserDevices, SessionStore}, + CryptoStore, CryptoStoreError, Result, +}; use crate::{ identities::{LocalTrust, ReadOnlyDevice, UserIdentities}, - memory_stores::{DeviceStore, GroupSessionStore, ReadOnlyUserDevices, SessionStore}, olm::{ Account, AccountPickle, IdentityKeys, InboundGroupSession, InboundGroupSessionPickle, PickledAccount, PickledInboundGroupSession, PickledSession, PicklingMode, Session, @@ -44,8 +46,9 @@ use crate::{ }, }; -#[derive(Clone)] /// SQLite based implementation of a `CryptoStore`. +#[derive(Clone)] +#[cfg_attr(feature = "docs", doc(cfg(r#sqlite_cryptostore)))] pub struct SqliteStore { user_id: Arc, device_id: Arc>, diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index 898c5830..65050ff2 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -26,7 +26,11 @@ use matrix_sdk_common::{ }; use super::sas::{content_to_request, Sas}; -use crate::{requests::OutgoingRequest, Account, CryptoStore, CryptoStoreError, ReadOnlyDevice}; +use crate::{ + requests::OutgoingRequest, + store::{CryptoStore, CryptoStoreError}, + Account, ReadOnlyDevice, +}; #[derive(Clone, Debug)] pub struct VerificationMachine { @@ -229,9 +233,9 @@ mod test { use super::{Sas, VerificationMachine}; use crate::{ requests::OutgoingRequests, - store::memorystore::MemoryStore, + store::{CryptoStore, MemoryStore}, verification::test::{get_content_from_request, wrap_any_to_device_content}, - Account, CryptoStore, ReadOnlyDevice, + Account, ReadOnlyDevice, }; fn alice_id() -> UserId { diff --git a/matrix_sdk_crypto/src/verification/sas/mod.rs b/matrix_sdk_crypto/src/verification/sas/mod.rs index dde0f1c6..1be3a236 100644 --- a/matrix_sdk_crypto/src/verification/sas/mod.rs +++ b/matrix_sdk_crypto/src/verification/sas/mod.rs @@ -36,7 +36,8 @@ use matrix_sdk_common::{ use crate::{ identities::{LocalTrust, ReadOnlyDevice, UserIdentities}, - Account, CryptoStore, CryptoStoreError, + store::{CryptoStore, CryptoStoreError}, + Account, }; pub use helpers::content_to_request; @@ -658,9 +659,9 @@ mod test { }; use crate::{ - store::memorystore::MemoryStore, + store::{CryptoStore, MemoryStore}, verification::test::{get_content_from_request, wrap_any_to_device_content}, - Account, CryptoStore, ReadOnlyDevice, + Account, ReadOnlyDevice, }; use super::{Accepted, Created, Sas, SasState, Started};