1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
//! MLS group membership
//!
//! This module contains membership-related operations and exposes [`RemoveOperation`].
use core_group::create_commit_params::CreateCommitParams;
use openmls_traits::{signatures::Signer, storage::StorageProvider as _};
use super::{
errors::{AddMembersError, LeaveGroupError, RemoveMembersError},
*,
};
use crate::{
binary_tree::array_representation::LeafNodeIndex, messages::group_info::GroupInfo,
storage::OpenMlsProvider, treesync::LeafNode,
};
impl MlsGroup {
/// Adds members to the group.
///
/// New members are added by providing a `KeyPackage` for each member.
///
/// This operation results in a Commit with a `path`, i.e. it includes an
/// update of the committer's leaf [KeyPackage].
///
/// If successful, it returns a triple of [`MlsMessageOut`]s, where the first
/// contains the commit, the second one the [`Welcome`] and the third an optional [GroupInfo] that
/// will be [Some] if the group has the `use_ratchet_tree_extension` flag set.
///
/// Returns an error if there is a pending commit.
///
/// [`Welcome`]: crate::messages::Welcome
// FIXME: #1217
#[allow(clippy::type_complexity)]
pub fn add_members<Provider: OpenMlsProvider>(
&mut self,
provider: &Provider,
signer: &impl Signer,
key_packages: &[KeyPackage],
) -> Result<
(MlsMessageOut, MlsMessageOut, Option<GroupInfo>),
AddMembersError<Provider::StorageError>,
> {
self.is_operational()?;
if key_packages.is_empty() {
return Err(AddMembersError::EmptyInput(EmptyInputError::AddMembers));
}
// Create inline add proposals from key packages
let inline_proposals = key_packages
.iter()
.map(|key_package| {
Proposal::Add(AddProposal {
key_package: key_package.clone(),
})
})
.collect::<Vec<Proposal>>();
// Create Commit over all proposals
// TODO #751
let params = CreateCommitParams::builder()
.framing_parameters(self.framing_parameters())
.proposal_store(&self.proposal_store)
.inline_proposals(inline_proposals)
.build();
let create_commit_result = self.group.create_commit(params, provider, signer)?;
let welcome = match create_commit_result.welcome_option {
Some(welcome) => welcome,
None => {
return Err(LibraryError::custom("No secrets to generate commit message.").into())
}
};
// Convert PublicMessage messages to MLSMessage and encrypt them if required by
// the configuration
let mls_messages = self.content_to_mls_message(create_commit_result.commit, provider)?;
// Set the current group state to [`MlsGroupState::PendingCommit`],
// storing the current [`StagedCommit`] from the commit results
self.group_state = MlsGroupState::PendingCommit(Box::new(PendingCommitState::Member(
create_commit_result.staged_commit,
)));
provider
.storage()
.write_group_state(self.group_id(), &self.group_state)
.map_err(AddMembersError::StorageError)?;
Ok((
mls_messages,
MlsMessageOut::from_welcome(welcome, self.group.version()),
create_commit_result.group_info,
))
}
/// Returns a reference to the own [`LeafNode`].
pub fn own_leaf(&self) -> Option<&LeafNode> {
self.group.public_group().leaf(self.group.own_leaf_index())
}
/// Removes members from the group.
///
/// Members are removed by providing the member's leaf index.
///
/// If successful, it returns a tuple of [`MlsMessageOut`] (containing the
/// commit), an optional [`MlsMessageOut`] (containing the [`Welcome`]) and the current
/// [GroupInfo].
/// The [`Welcome`] is [Some] when the queue of pending proposals contained
/// add proposals
/// The [GroupInfo] is [Some] if the group has the `use_ratchet_tree_extension` flag set.
///
/// Returns an error if there is a pending commit.
///
/// [`Welcome`]: crate::messages::Welcome
// FIXME: #1217
#[allow(clippy::type_complexity)]
pub fn remove_members<Provider: OpenMlsProvider>(
&mut self,
provider: &Provider,
signer: &impl Signer,
members: &[LeafNodeIndex],
) -> Result<
(MlsMessageOut, Option<MlsMessageOut>, Option<GroupInfo>),
RemoveMembersError<Provider::StorageError>,
> {
self.is_operational()?;
if members.is_empty() {
return Err(RemoveMembersError::EmptyInput(
EmptyInputError::RemoveMembers,
));
}
// Create inline remove proposals
let mut inline_proposals = Vec::new();
for member in members.iter() {
inline_proposals.push(Proposal::Remove(RemoveProposal { removed: *member }))
}
// Create Commit over all proposals
// TODO #751
let params = CreateCommitParams::builder()
.framing_parameters(self.framing_parameters())
.proposal_store(&self.proposal_store)
.inline_proposals(inline_proposals)
.build();
let create_commit_result = self.group.create_commit(params, provider, signer)?;
// Convert PublicMessage messages to MLSMessage and encrypt them if required by
// the configuration
let mls_message = self.content_to_mls_message(create_commit_result.commit, provider)?;
// Set the current group state to [`MlsGroupState::PendingCommit`],
// storing the current [`StagedCommit`] from the commit results
self.group_state = MlsGroupState::PendingCommit(Box::new(PendingCommitState::Member(
create_commit_result.staged_commit,
)));
provider
.storage()
.write_group_state(self.group_id(), &self.group_state)
.map_err(RemoveMembersError::StorageError)?;
Ok((
mls_message,
create_commit_result
.welcome_option
.map(|w| MlsMessageOut::from_welcome(w, self.group.version())),
create_commit_result.group_info,
))
}
/// Leave the group.
///
/// Creates a Remove Proposal that needs to be covered by a Commit from a different member.
/// The Remove Proposal is returned as a [`MlsMessageOut`].
///
/// Returns an error if there is a pending commit.
pub fn leave_group<Provider: OpenMlsProvider>(
&mut self,
provider: &Provider,
signer: &impl Signer,
) -> Result<MlsMessageOut, LeaveGroupError<Provider::StorageError>> {
self.is_operational()?;
let removed = self.group.own_leaf_index();
let remove_proposal = self
.group
.create_remove_proposal(self.framing_parameters(), removed, signer)
.map_err(|_| LibraryError::custom("Creating a self removal should not fail"))?;
self.proposal_store
.add(QueuedProposal::from_authenticated_content_by_ref(
self.ciphersuite(),
provider.crypto(),
remove_proposal.clone(),
)?);
Ok(self.content_to_mls_message(remove_proposal, provider)?)
}
/// Returns a list of [`Member`]s in the group.
pub fn members(&self) -> impl Iterator<Item = Member> + '_ {
self.group.public_group().members()
}
/// Returns the [`Credential`] of a member corresponding to the given
/// leaf index. Returns `None` if the member can not be found in this group.
pub fn member(&self, leaf_index: LeafNodeIndex) -> Option<&Credential> {
self.group
.public_group()
// This will return an error if the member can't be found.
.leaf(leaf_index)
.map(|leaf| leaf.credential())
}
}
/// Helper `enum` that classifies the kind of remove operation. This can be used to
/// better interpret the semantic value of a remove proposal that is covered in a
/// Commit message.
#[derive(Debug)]
pub enum RemoveOperation {
/// We issued a remove proposal for ourselves in the previous epoch and
/// the proposal has now been committed.
WeLeft,
/// Someone else (indicated by the [`Sender`]) removed us from the group.
WeWereRemovedBy(Sender),
/// Another member (indicated by the leaf index) requested to leave
/// the group by issuing a remove proposal in the previous epoch and the
/// proposal has now been committed.
TheyLeft(LeafNodeIndex),
/// Another member (indicated by the leaf index) was removed by the [`Sender`].
TheyWereRemovedBy((LeafNodeIndex, Sender)),
/// We removed another member (indicated by the leaf index).
WeRemovedThem(LeafNodeIndex),
}
impl RemoveOperation {
/// Constructs a new [`RemoveOperation`] from a [`QueuedRemoveProposal`] and the
/// corresponding [`MlsGroup`].
pub fn new(
queued_remove_proposal: QueuedRemoveProposal,
group: &MlsGroup,
) -> Result<Self, LibraryError> {
let own_index = group.own_leaf_index();
let sender = queued_remove_proposal.sender();
let removed = queued_remove_proposal.remove_proposal().removed();
// We start with the cases where the sender is a group member
if let Sender::Member(leaf_index) = sender {
// We authored the remove proposal
if *leaf_index == own_index {
if removed == own_index {
// We left
return Ok(Self::WeLeft);
} else {
// We removed another member
return Ok(Self::WeRemovedThem(removed));
}
}
// Another member left
if removed == *leaf_index {
return Ok(Self::TheyLeft(removed));
}
}
// The sender is not necessarily a group member. This covers all sender
// types (members, pre-configured senders and new members).
if removed == own_index {
// We were removed
Ok(Self::WeWereRemovedBy(sender.clone()))
} else {
// Another member was removed
Ok(Self::TheyWereRemovedBy((removed, sender.clone())))
}
}
}