crytpo: Let the verification machine hold on to a raw CryptoStore.

This will later be useful when our higher level store wrapper holds on
to a verification machine to return higher level Device objects.
master
Damir Jelić 2020-10-01 11:17:27 +02:00
parent d644af7be9
commit c8ca93c924
8 changed files with 27 additions and 33 deletions

View File

@ -40,7 +40,7 @@ use crate::{
error::{EventError, OlmError, OlmResult, SignatureError}, error::{EventError, OlmError, OlmResult, SignatureError},
identities::{OwnUserIdentity, UserIdentities}, identities::{OwnUserIdentity, UserIdentities},
olm::Utility, olm::Utility,
store::{caches::ReadOnlyUserDevices, Result as StoreResult, Store}, store::{caches::ReadOnlyUserDevices, CryptoStore, Result as StoreResult},
verification::VerificationMachine, verification::VerificationMachine,
Sas, ToDeviceRequest, Sas, ToDeviceRequest,
}; };
@ -122,7 +122,7 @@ impl Device {
content: Value, content: Value,
) -> OlmResult<EncryptedEventContent> { ) -> OlmResult<EncryptedEventContent> {
self.inner self.inner
.encrypt(self.verification_machine.store.clone(), event_type, content) .encrypt(&**self.verification_machine.store, event_type, content)
.await .await
} }
} }
@ -321,7 +321,7 @@ impl ReadOnlyDevice {
pub(crate) async fn encrypt( pub(crate) async fn encrypt(
&self, &self,
store: Store, store: &dyn CryptoStore,
event_type: EventType, event_type: EventType,
content: Value, content: Value,
) -> OlmResult<EncryptedEventContent> { ) -> OlmResult<EncryptedEventContent> {

View File

@ -381,7 +381,7 @@ pub(crate) mod test {
fn manager() -> IdentityManager { fn manager() -> IdentityManager {
let user_id = Arc::new(user_id()); let user_id = Arc::new(user_id());
let store = Store::new(user_id.clone(), Box::new(MemoryStore::new())); let store = Store::new(user_id.clone(), Arc::new(Box::new(MemoryStore::new())));
IdentityManager::new(user_id, Arc::new(device_id()), store) IdentityManager::new(user_id, Arc::new(device_id()), store)
} }

View File

@ -667,7 +667,7 @@ pub(crate) mod test {
Device, ReadOnlyDevice, Device, ReadOnlyDevice,
}, },
olm::ReadOnlyAccount, olm::ReadOnlyAccount,
store::{MemoryStore, Store}, store::MemoryStore,
verification::VerificationMachine, verification::VerificationMachine,
}; };
@ -736,10 +736,7 @@ pub(crate) mod test {
let verification_machine = VerificationMachine::new( let verification_machine = VerificationMachine::new(
ReadOnlyAccount::new(second.user_id(), second.device_id()), ReadOnlyAccount::new(second.user_id(), second.device_id()),
Store::new( Arc::new(Box::new(MemoryStore::new())),
Arc::new(second.user_id().clone()),
Box::new(MemoryStore::new()),
),
); );
let first = Device { let first = Device {

View File

@ -93,9 +93,7 @@ impl Device {
event_type: EventType, event_type: EventType,
content: Value, content: Value,
) -> OlmResult<EncryptedEventContent> { ) -> OlmResult<EncryptedEventContent> {
self.inner self.inner.encrypt(&*self.store, event_type, content).await
.encrypt(self.store.clone(), event_type, content)
.await
} }
} }
@ -651,7 +649,7 @@ mod test {
fn bob_machine() -> KeyRequestMachine { fn bob_machine() -> KeyRequestMachine {
let user_id = Arc::new(bob_id()); let user_id = Arc::new(bob_id());
let store = Store::new(user_id.clone(), Box::new(MemoryStore::new())); let store = Store::new(user_id.clone(), Arc::new(Box::new(MemoryStore::new())));
KeyRequestMachine::new( KeyRequestMachine::new(
user_id, user_id,
@ -663,7 +661,7 @@ mod test {
fn get_machine() -> KeyRequestMachine { fn get_machine() -> KeyRequestMachine {
let user_id = Arc::new(alice_id()); let user_id = Arc::new(alice_id());
let store = Store::new(user_id.clone(), Box::new(MemoryStore::new())); let store = Store::new(user_id.clone(), Arc::new(Box::new(MemoryStore::new())));
KeyRequestMachine::new( KeyRequestMachine::new(
user_id, user_id,

View File

@ -125,8 +125,9 @@ impl OlmMachine {
) -> Self { ) -> Self {
let user_id = Arc::new(user_id.clone()); let user_id = Arc::new(user_id.clone());
let store = Store::new(user_id.clone(), store); let store = Arc::new(store);
let verification_machine = VerificationMachine::new(account.clone(), store.clone()); let verification_machine = VerificationMachine::new(account.clone(), store.clone());
let store = Store::new(user_id.clone(), store);
let device_id: Arc<DeviceIdBox> = Arc::new(device_id); let device_id: Arc<DeviceIdBox> = Arc::new(device_id);
let outbound_group_sessions = Arc::new(DashMap::new()); let outbound_group_sessions = Arc::new(DashMap::new());
let key_request_machine = KeyRequestMachine::new( let key_request_machine = KeyRequestMachine::new(

View File

@ -93,10 +93,10 @@ pub(crate) struct Store {
} }
impl Store { impl Store {
pub fn new(user_id: Arc<UserId>, store: Box<dyn CryptoStore>) -> Self { pub fn new(user_id: Arc<UserId>, store: Arc<Box<dyn CryptoStore>>) -> Self {
Self { Self {
user_id, user_id,
inner: Arc::new(store), inner: store,
} }
} }

View File

@ -27,20 +27,20 @@ use matrix_sdk_common::{
use super::sas::{content_to_request, Sas}; use super::sas::{content_to_request, Sas};
use crate::{ use crate::{
requests::{OutgoingRequest, ToDeviceRequest}, requests::{OutgoingRequest, ToDeviceRequest},
store::{CryptoStoreError, Store}, store::{CryptoStore, CryptoStoreError},
ReadOnlyAccount, ReadOnlyDevice, ReadOnlyAccount, ReadOnlyDevice,
}; };
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct VerificationMachine { pub struct VerificationMachine {
account: ReadOnlyAccount, account: ReadOnlyAccount,
pub(crate) store: Store, pub(crate) store: Arc<Box<dyn CryptoStore>>,
verifications: Arc<DashMap<String, Sas>>, verifications: Arc<DashMap<String, Sas>>,
outgoing_to_device_messages: Arc<DashMap<Uuid, OutgoingRequest>>, outgoing_to_device_messages: Arc<DashMap<Uuid, OutgoingRequest>>,
} }
impl VerificationMachine { impl VerificationMachine {
pub(crate) fn new(account: ReadOnlyAccount, store: Store) -> Self { pub(crate) fn new(account: ReadOnlyAccount, store: Arc<Box<dyn CryptoStore>>) -> Self {
Self { Self {
account, account,
store, store,
@ -233,7 +233,7 @@ mod test {
use super::{Sas, VerificationMachine}; use super::{Sas, VerificationMachine};
use crate::{ use crate::{
requests::OutgoingRequests, requests::OutgoingRequests,
store::{CryptoStore, MemoryStore, Store}, store::{CryptoStore, MemoryStore},
verification::test::{get_content_from_request, wrap_any_to_device_content}, verification::test::{get_content_from_request, wrap_any_to_device_content},
ReadOnlyAccount, ReadOnlyDevice, ReadOnlyAccount, ReadOnlyDevice,
}; };
@ -258,7 +258,7 @@ mod test {
let alice = ReadOnlyAccount::new(&alice_id(), &alice_device_id()); let alice = ReadOnlyAccount::new(&alice_id(), &alice_device_id());
let bob = ReadOnlyAccount::new(&bob_id(), &bob_device_id()); let bob = ReadOnlyAccount::new(&bob_id(), &bob_device_id());
let store = MemoryStore::new(); let store = MemoryStore::new();
let bob_store = Store::new(Arc::new(bob_id()), Box::new(MemoryStore::new())); let bob_store: Arc<Box<dyn CryptoStore>> = Arc::new(Box::new(MemoryStore::new()));
let bob_device = ReadOnlyDevice::from_account(&bob).await; let bob_device = ReadOnlyDevice::from_account(&bob).await;
let alice_device = ReadOnlyDevice::from_account(&alice).await; let alice_device = ReadOnlyDevice::from_account(&alice).await;
@ -269,8 +269,7 @@ mod test {
.await .await
.unwrap(); .unwrap();
let machine = let machine = VerificationMachine::new(alice, Arc::new(Box::new(store)));
VerificationMachine::new(alice, Store::new(Arc::new(alice_id()), Box::new(store)));
let (bob_sas, start_content) = Sas::start(bob, alice_device, bob_store, None); let (bob_sas, start_content) = Sas::start(bob, alice_device, bob_store, None);
machine machine
.receive_event(&mut wrap_any_to_device_content( .receive_event(&mut wrap_any_to_device_content(
@ -286,9 +285,8 @@ mod test {
#[test] #[test]
fn create() { fn create() {
let alice = ReadOnlyAccount::new(&alice_id(), &alice_device_id()); let alice = ReadOnlyAccount::new(&alice_id(), &alice_device_id());
let user_id = Arc::new(alice_id());
let store = MemoryStore::new(); let store = MemoryStore::new();
let _ = VerificationMachine::new(alice, Store::new(user_id, Box::new(store))); let _ = VerificationMachine::new(alice, Arc::new(Box::new(store)));
} }
#[tokio::test] #[tokio::test]

View File

@ -34,7 +34,7 @@ use matrix_sdk_common::{
use crate::{ use crate::{
identities::{LocalTrust, ReadOnlyDevice, UserIdentities}, identities::{LocalTrust, ReadOnlyDevice, UserIdentities},
store::{CryptoStoreError, Store}, store::{CryptoStore, CryptoStoreError},
ReadOnlyAccount, ToDeviceRequest, ReadOnlyAccount, ToDeviceRequest,
}; };
@ -47,7 +47,7 @@ use sas_state::{
/// Short authentication string object. /// Short authentication string object.
pub struct Sas { pub struct Sas {
inner: Arc<Mutex<InnerSas>>, inner: Arc<Mutex<InnerSas>>,
store: Store, store: Arc<Box<dyn CryptoStore>>,
account: ReadOnlyAccount, account: ReadOnlyAccount,
other_device: ReadOnlyDevice, other_device: ReadOnlyDevice,
other_identity: Option<UserIdentities>, other_identity: Option<UserIdentities>,
@ -104,7 +104,7 @@ impl Sas {
pub(crate) fn start( pub(crate) fn start(
account: ReadOnlyAccount, account: ReadOnlyAccount,
other_device: ReadOnlyDevice, other_device: ReadOnlyDevice,
store: Store, store: Arc<Box<dyn CryptoStore>>,
other_identity: Option<UserIdentities>, other_identity: Option<UserIdentities>,
) -> (Sas, StartEventContent) { ) -> (Sas, StartEventContent) {
let (inner, content) = InnerSas::start( let (inner, content) = InnerSas::start(
@ -139,7 +139,7 @@ impl Sas {
pub(crate) fn from_start_event( pub(crate) fn from_start_event(
account: ReadOnlyAccount, account: ReadOnlyAccount,
other_device: ReadOnlyDevice, other_device: ReadOnlyDevice,
store: Store, store: Arc<Box<dyn CryptoStore>>,
event: &ToDeviceEvent<StartEventContent>, event: &ToDeviceEvent<StartEventContent>,
other_identity: Option<UserIdentities>, other_identity: Option<UserIdentities>,
) -> Result<Sas, AnyToDeviceEventContent> { ) -> Result<Sas, AnyToDeviceEventContent> {
@ -654,7 +654,7 @@ mod test {
}; };
use crate::{ use crate::{
store::{MemoryStore, Store}, store::{CryptoStore, MemoryStore},
verification::test::{get_content_from_request, wrap_any_to_device_content}, verification::test::{get_content_from_request, wrap_any_to_device_content},
ReadOnlyAccount, ReadOnlyDevice, ReadOnlyAccount, ReadOnlyDevice,
}; };
@ -776,8 +776,8 @@ mod test {
let bob = ReadOnlyAccount::new(&bob_id(), &bob_device_id()); let bob = ReadOnlyAccount::new(&bob_id(), &bob_device_id());
let bob_device = ReadOnlyDevice::from_account(&bob).await; let bob_device = ReadOnlyDevice::from_account(&bob).await;
let alice_store = Store::new(Arc::new(alice_id()), Box::new(MemoryStore::new())); let alice_store: Arc<Box<dyn CryptoStore>> = Arc::new(Box::new(MemoryStore::new()));
let bob_store = Store::new(Arc::new(bob_id()), Box::new(MemoryStore::new())); let bob_store: Arc<Box<dyn CryptoStore>> = Arc::new(Box::new(MemoryStore::new()));
bob_store bob_store
.save_devices(&[alice_device.clone()]) .save_devices(&[alice_device.clone()])