Skip to content

Commit

Permalink
Store decrypted groups (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
gferon committed Jul 24, 2023
1 parent 2c1a594 commit 2bbbab2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
26 changes: 12 additions & 14 deletions presage-store-sled/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ pub enum SchemaVersion {
#[default]
V0 = 0,
V1 = 1,
/// the current version
V2 = 2,
V3 = 3,
}

impl SchemaVersion {
fn current() -> SchemaVersion {
Self::V2
Self::V3
}

/// return an iterator on all the necessary migration steps from another version
Expand All @@ -107,6 +107,7 @@ impl SchemaVersion {
.map(|i| match i {
1 => SchemaVersion::V1,
2 => SchemaVersion::V2,
3 => SchemaVersion::V3,
_ => unreachable!("oops, this not supposed to happen!"),
})
}
Expand Down Expand Up @@ -294,6 +295,12 @@ fn migrate(
db.flush()?;
}
}
SchemaVersion::V3 => {
debug!("migrating from schema v2 to v3: dropping encrypted group cache");
let db = store.write();
db.drop_tree(SLED_TREE_GROUPS)?;
db.flush()?;
}
_ => return Err(SledStoreError::MigrationConflict),
}

Expand Down Expand Up @@ -474,24 +481,15 @@ impl Store for SledStore {
&self,
master_key_bytes: GroupMasterKeyBytes,
) -> Result<Option<Group>, SledStoreError> {
let val: Option<Vec<u8>> = self.get(SLED_TREE_GROUPS, master_key_bytes)?;
match val {
Some(ref v) => {
let encrypted_group = proto::Group::decode(v.as_slice())?;
let group = decrypt_group(&master_key_bytes, encrypted_group)
.map_err(|_| SledStoreError::GroupDecryption)?;
Ok(Some(group))
}
None => Ok(None),
}
self.get(SLED_TREE_GROUPS, master_key_bytes)
}

fn save_group(
&self,
master_key: GroupMasterKeyBytes,
group: proto::Group,
group: &Group,
) -> Result<(), SledStoreError> {
self.insert(SLED_TREE_GROUPS, master_key, group.encode_to_vec())?;
self.insert(SLED_TREE_GROUPS, master_key, group)?;
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions presage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod prelude {
self, Content, ContentBody, DataMessage, GroupContext, GroupContextV2, GroupType,
Metadata, SyncMessage,
},
groups_v2::{AccessControl, Group, GroupChange, PendingMember, RequestingMember, Timer},
models::Contact,
prelude::{
phonenumber::{self, PhoneNumber},
Expand Down
13 changes: 7 additions & 6 deletions presage/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use libsignal_service::{
cipher,
configuration::{ServiceConfiguration, SignalServers, SignalingKey},
content::{ContentBody, DataMessage, DataMessageFlags, Metadata, SyncMessage},
groups_v2::{Group, GroupsManager, InMemoryCredentialsCache},
groups_v2::{decrypt_group, Group, GroupsManager, InMemoryCredentialsCache},
messagepipe::ServiceCredentials,
models::Contact,
prelude::{
Expand Down Expand Up @@ -1251,7 +1251,7 @@ async fn upsert_group<C: Store>(
master_key_bytes: &[u8],
revision: &u32,
) -> Result<Option<Group>, Error<C::Error>> {
let save_group = match config_store.group(master_key_bytes.try_into()?) {
let upsert_group = match config_store.group(master_key_bytes.try_into()?) {
Ok(Some(group)) => {
log::debug!("loaded group from local db {}", group.title);
group.revision < *revision
Expand All @@ -1263,11 +1263,12 @@ async fn upsert_group<C: Store>(
}
};

if save_group {
log::debug!("fetching group");
if upsert_group {
log::debug!("fetching and saving group");
match groups_manager.fetch_encrypted_group(master_key_bytes).await {
Ok(group) => {
if let Err(e) = config_store.save_group(master_key_bytes.try_into()?, group) {
Ok(encrypted_group) => {
let group = decrypt_group(&master_key_bytes, encrypted_group)?;

Check warning on line 1270 in presage/src/manager.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler
if let Err(e) = config_store.save_group(master_key_bytes.try_into()?, &group) {
log::error!("failed to save group {master_key_bytes:?}: {e}",);
}
}
Expand Down
7 changes: 2 additions & 5 deletions presage/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,8 @@ pub trait Store: ProtocolStore + SenderKeyStore + SessionStoreExt + Sync + Clone
fn clear_groups(&mut self) -> Result<(), Self::Error>;

/// Save a group in the cache
fn save_group(
&self,
master_key: GroupMasterKeyBytes,
group: crate::prelude::proto::Group,
) -> Result<(), Self::Error>;
fn save_group(&self, master_key: GroupMasterKeyBytes, group: &Group)
-> Result<(), Self::Error>;

/// Get an iterator on all cached groups
fn groups(&self) -> Result<Self::GroupsIter, Self::Error>;
Expand Down

0 comments on commit 2bbbab2

Please sign in to comment.