Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing group change types #355

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions src/groups_v2/model.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{convert::TryFrom, convert::TryInto};

use derivative::Derivative;
use libsignal_protocol::ServiceId;
use libsignal_protocol::{Aci, Pni, ServiceId};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use zkgroup::profiles::ProfileKey;
Expand Down Expand Up @@ -54,6 +54,34 @@ impl PartialEq for RequestingMember {
}
}

#[derive(Derivative, Clone)]
#[derivative(Debug)]
pub struct BannedMember {
pub uuid: Uuid,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be Aci?

pub timestamp: u64,
}

impl PartialEq for BannedMember {
fn eq(&self, other: &Self) -> bool {
self.uuid == other.uuid
}
}

#[derive(Derivative, Clone)]
#[derivative(Debug)]
pub struct PromotedMember {
pub aci: Aci,
pub pni: Pni,
#[derivative(Debug = "ignore")]
pub profile_key: ProfileKey,
}

impl PartialEq for PromotedMember {
fn eq(&self, other: &Self) -> bool {
self.aci == other.aci && self.pni == other.pni
}
}

#[derive(Copy, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum AccessRequired {
Unknown,
Expand Down Expand Up @@ -105,31 +133,31 @@ pub enum GroupChange {
#[derivative(Debug = "ignore")]
profile_key: ProfileKey,
},
// for open groups
NewPendingMember(PendingMember),
DeletePendingMember(Uuid),
PromotePendingMember {
uuid: Uuid,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are pending members Uuids, ServiceIds or Acis? Maybe it's worth to comment the code whenever Uuid or ServiceId is used, to document why that specific form is used.

E.g., for invites, IIRC it's a ServiceId, because members can be invited based on their phone number (iirc).

#[derivative(Debug = "ignore")]
profile_key: ProfileKey,
},
// when admin control is enabled
NewRequestingMember(RequestingMember),
DeleteRequestingMember(Uuid),
PromoteRequestingMember {
uuid: Uuid,
role: Role,
},
// group metadata
Title(String),
Avatar(String),
Timer(Option<Timer>),
Description(Option<String>),
AttributeAccess(AccessRequired),
MemberAccess(AccessRequired),
InviteLinkAccess(AccessRequired),
NewRequestingMember(RequestingMember),
DeleteRequestingMember(Uuid),
PromoteRequestingMember {
uuid: Uuid,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question

role: Role,
},
InviteLinkPassword(String),
Description(Option<String>),
AnnouncementOnly(bool),
AddBannedMember(BannedMember),
DeleteBannedMember(Uuid),
PromotePendingPniAciMemberProfileKey(PromotedMember),
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
Expand Down
75 changes: 73 additions & 2 deletions src/groups_v2/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::convert::TryInto;

use base64::prelude::*;
use bytes::Bytes;
use libsignal_protocol::{Aci, ServiceId};
use libsignal_protocol::{Aci, Pni, ServiceId};
use prost::Message;
use zkgroup::{
groups::GroupSecretParams,
Expand All @@ -19,7 +19,9 @@ use crate::{
};

use super::{
model::{Member, PendingMember, RequestingMember},
model::{
BannedMember, Member, PendingMember, PromotedMember, RequestingMember,
},
Group, GroupChange, GroupChanges,
};

Expand All @@ -43,6 +45,8 @@ pub enum GroupDecodingError {
WrongEnumValue,
#[error("wrong service ID type: should be ACI")]
NotAci,
#[error("wrong service ID type: should be PNI")]
NotPni,
}

impl From<zkgroup::ZkGroupDeserializationFailure> for GroupDecodingError {
Expand Down Expand Up @@ -84,6 +88,19 @@ impl GroupOperations {
}
}

fn decrypt_pni(
&self,
ciphertext: &[u8],
) -> Result<Pni, GroupDecodingError> {
match self
.group_secret_params
.decrypt_service_id(bincode::deserialize(ciphertext)?)?
{
ServiceId::Pni(pni) => Ok(pni),
ServiceId::Aci(_aci) => Err(GroupDecodingError::NotPni),
}
}

fn decrypt_profile_key(
&self,
encrypted_profile_key: &[u8],
Expand Down Expand Up @@ -118,6 +135,20 @@ impl GroupOperations {
}
}

fn decrypt_pni_aci_promotion_presentation(
&self,
member: &proto::group_change::actions::PromotePendingPniAciMemberProfileKeyAction,
) -> Result<PromotedMember, GroupDecodingError> {
let aci = self.decrypt_aci(&member.user_id)?;
let pni = self.decrypt_pni(&member.pni)?;
let profile_key = self.decrypt_profile_key(&member.profile_key, aci)?;
Ok(PromotedMember {
aci,
pni,
profile_key,
})
}

fn decrypt_member(
&self,
member: EncryptedMember,
Expand Down Expand Up @@ -174,6 +205,17 @@ impl GroupOperations {
})
}

fn decrypt_banned_member(
&self,
member: proto::BannedMember,
) -> Result<BannedMember, GroupDecodingError> {
let aci = self.decrypt_aci(&member.user_id)?;
Ok(BannedMember {
uuid: aci.into(),
timestamp: member.timestamp,
})
}

fn decrypt_blob(&self, bytes: &[u8]) -> GroupAttributeBlob {
if bytes.is_empty() {
GroupAttributeBlob::default()
Expand Down Expand Up @@ -381,6 +423,32 @@ impl GroupOperations {
Ok(GroupChange::MemberAccess(m.members_access.try_into()?))
});

let add_banned_members = actions
.add_banned_members
.into_iter()
.filter_map(|m| m.added)
.map(|added| {
Ok(GroupChange::AddBannedMember(
self.decrypt_banned_member(added)?,
))
});

let delete_banned_members =
actions.delete_banned_members.into_iter().map(|added| {
Ok(GroupChange::DeleteBannedMember(
self.decrypt_aci(&added.deleted_user_id)?.into(),
))
});

let promote_pending_member = actions
.promote_pending_pni_aci_members
.into_iter()
.map(|m| {
let promoted =
self.decrypt_pni_aci_promotion_presentation(&m)?;
Ok(GroupChange::PromotePendingPniAciMemberProfileKey(promoted))
});

let modify_add_from_invite_link_access = actions
.modify_add_from_invite_link_access
.into_iter()
Expand Down Expand Up @@ -440,6 +508,9 @@ impl GroupOperations {
.chain(modify_attributes_access)
.chain(modify_description)
.chain(modify_member_access)
.chain(add_banned_members)
.chain(delete_banned_members)
.chain(promote_pending_member)
.chain(modify_add_from_invite_link_access)
.chain(add_requesting_members)
.chain(delete_requesting_members)
Expand Down
Loading