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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//! MLS group membership
//!
//! This module contains membership-related operations and exposes [`RemoveOperation`].
#[cfg(any(feature = "test-utils", test))]
use std::collections::BTreeMap;
use core_group::create_commit_params::CreateCommitParams;
use tls_codec::Serialize;
use crate::{ciphersuite::hash_ref::HashReference, ciphersuite::hash_ref::KeyPackageRef};
use super::{
errors::{AddMembersError, LeaveGroupError, RemoveMembersError},
*,
};
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 tuple of [MlsMessageOut] and [Welcome].
///
/// Returns an error if there is a pending commit.
pub fn add_members(
&mut self,
backend: &impl OpenMlsCryptoProvider,
key_packages: &[KeyPackage],
) -> Result<(MlsMessageOut, Welcome), AddMembersError> {
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>>();
let credential = self.credential()?;
let credential_bundle: CredentialBundle = backend
.key_store()
.read(
&credential
.signature_key()
.tls_serialize_detached()
.map_err(LibraryError::missing_bound_check)?,
)
.ok_or(AddMembersError::NoMatchingCredentialBundle)?;
// Create Commit over all proposals
// TODO #751
let params = CreateCommitParams::builder()
.framing_parameters(self.framing_parameters())
.credential_bundle(&credential_bundle)
.proposal_store(&self.proposal_store)
.inline_proposals(inline_proposals)
.build();
let create_commit_result = self.group.create_commit(params, backend)?;
let welcome = match create_commit_result.welcome_option {
Some(welcome) => welcome,
None => {
return Err(LibraryError::custom("No secrets to generate commit message.").into())
}
};
// Convert MlsPlaintext messages to MLSMessage and encrypt them if required by
// the configuration
let mls_messages = self.plaintext_to_mls_message(create_commit_result.commit, backend)?;
// 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,
)));
// Since the state of the group might be changed, arm the state flag
self.flag_state_change();
Ok((mls_messages, welcome))
}
/// Removes members from the group.
///
/// Members are removed by providing the index of their leaf in the tree.
///
/// If successful, it returns a tuple of [`MlsMessageOut`] and an optional [`Welcome`].
/// The [Welcome] is [Some] when the queue of pending proposals contained add proposals
///
/// Returns an error if there is a pending commit.
pub fn remove_members(
&mut self,
backend: &impl OpenMlsCryptoProvider,
members: &[KeyPackageRef],
) -> Result<(MlsMessageOut, Option<Welcome>), RemoveMembersError> {
self.is_operational()?;
if members.is_empty() {
return Err(RemoveMembersError::EmptyInput(
EmptyInputError::RemoveMembers,
));
}
// Create inline remove proposals
let inline_proposals = members
.iter()
.map(|member| Proposal::Remove(RemoveProposal { removed: *member }))
.collect::<Vec<Proposal>>();
let credential = self.credential()?;
let credential_bundle: CredentialBundle = backend
.key_store()
.read(
&credential
.signature_key()
.tls_serialize_detached()
.map_err(LibraryError::missing_bound_check)?,
)
.ok_or(RemoveMembersError::NoMatchingCredentialBundle)?;
// Create Commit over all proposals
// TODO #751
let params = CreateCommitParams::builder()
.framing_parameters(self.framing_parameters())
.credential_bundle(&credential_bundle)
.proposal_store(&self.proposal_store)
.inline_proposals(inline_proposals)
.build();
let create_commit_result = self.group.create_commit(params, backend)?;
// Convert MlsPlaintext messages to MLSMessage and encrypt them if required by
// the configuration
let mls_message = self.plaintext_to_mls_message(create_commit_result.commit, backend)?;
// 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,
)));
// Since the state of the group might be changed, arm the state flag
self.flag_state_change();
Ok((mls_message, create_commit_result.welcome_option))
}
/// Creates proposals to add members to the group.
///
/// Returns an error if there is a pending commit.
pub fn propose_add_member(
&mut self,
backend: &impl OpenMlsCryptoProvider,
key_package: &KeyPackage,
) -> Result<MlsMessageOut, ProposeAddMemberError> {
self.is_operational()?;
let credential = self.credential()?;
let credential_bundle: CredentialBundle = backend
.key_store()
.read(
&credential
.signature_key()
.tls_serialize_detached()
.map_err(LibraryError::missing_bound_check)?,
)
.ok_or(ProposeAddMemberError::NoMatchingCredentialBundle)?;
let add_proposal = self
.group
.create_add_proposal(
self.framing_parameters(),
&credential_bundle,
key_package.clone(),
backend,
)
.map_err(|e| match e {
crate::group::errors::CreateAddProposalError::LibraryError(e) => e.into(),
crate::group::errors::CreateAddProposalError::UnsupportedExtensions => {
ProposeAddMemberError::UnsupportedExtensions
}
})?;
self.proposal_store.add(QueuedProposal::from_mls_plaintext(
self.ciphersuite(),
backend,
add_proposal.clone(),
)?);
let mls_message = self.plaintext_to_mls_message(add_proposal, backend)?;
// Since the state of the group might be changed, arm the state flag
self.flag_state_change();
Ok(mls_message)
}
/// Creates proposals to remove members from the group.
///
/// Returns an error if there is a pending commit.
pub fn propose_remove_member(
&mut self,
backend: &impl OpenMlsCryptoProvider,
member: &KeyPackageRef,
) -> Result<MlsMessageOut, ProposeRemoveMemberError> {
self.is_operational()?;
let credential = self.credential()?;
let credential_bundle: CredentialBundle = backend
.key_store()
.read(
&credential
.signature_key()
.tls_serialize_detached()
.map_err(LibraryError::missing_bound_check)?,
)
.ok_or(ProposeRemoveMemberError::NoMatchingCredentialBundle)?;
let remove_proposal = self.group.create_remove_proposal(
self.framing_parameters(),
&credential_bundle,
member,
backend,
)?;
self.proposal_store.add(QueuedProposal::from_mls_plaintext(
self.ciphersuite(),
backend,
remove_proposal.clone(),
)?);
let mls_message = self.plaintext_to_mls_message(remove_proposal, backend)?;
// Since the state of the group might be changed, arm the state flag
self.flag_state_change();
Ok(mls_message)
}
/// 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(
&mut self,
backend: &impl OpenMlsCryptoProvider,
) -> Result<MlsMessageOut, LeaveGroupError> {
self.is_operational()?;
let credential = self
.credential()
// We checked we are in the right state above
.map_err(|_| LibraryError::custom("Wrong group state"))?;
let credential_bundle: CredentialBundle = backend
.key_store()
.read(
&credential
.signature_key()
.tls_serialize_detached()
.map_err(LibraryError::missing_bound_check)?,
)
.ok_or(LeaveGroupError::NoMatchingCredentialBundle)?;
let removed = self
.group
.key_package_ref()
.ok_or_else(|| LibraryError::custom("No key package reference for own key package."))?;
let remove_proposal = self.group.create_remove_proposal(
self.framing_parameters(),
&credential_bundle,
removed,
backend,
)?;
self.proposal_store.add(QueuedProposal::from_mls_plaintext(
self.ciphersuite(),
backend,
remove_proposal.clone(),
)?);
Ok(self.plaintext_to_mls_message(remove_proposal, backend)?)
}
/// Returns a list of [`KeyPackage`]s of the current group members.
pub fn members(&self) -> Vec<&KeyPackage> {
match self.group.treesync().full_leaves() {
Ok(leaves) => leaves.iter().map(|(_, &kp)| kp).collect(),
// This should not happen, but this way we avoid returning a library error
Err(e) => {
log::debug!("treesync::full_leaves() returned an error: {:?}", e);
Vec::new()
}
}
}
/// Returns the [`KeyPackage`] of a member corresponding to the given
/// [`KeyPackageRef`]. Returns `None` if no matching [`KeyPackage`] can be
/// found in this group.
pub fn member(&self, key_package_ref: &KeyPackageRef) -> Option<&KeyPackage> {
self.group
.treesync()
// Besides from returning an error if the member can't be found,
// this will only return an error in case OpenMLS is compiled with a
// sub-32 bit architecture. As a result, it should be safe to just
// return `None` instead of propagating an error.
.leaf_from_id(key_package_ref)
.map(|leaf| leaf.key_package())
}
/// Returns the current list of members, indexed with the leaf index.
/// This should go away in future when all tests are rewritten to use key
/// package references instead of leaf indices.
#[cfg(any(feature = "test-utils", test))]
pub fn indexed_members(&self) -> Result<BTreeMap<u32, &KeyPackage>, LibraryError> {
self.group
.treesync()
.full_leaves()
.map_err(|_| LibraryError::custom("Unexpected error in TreeSync"))
}
}
/// 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 [`HashReference`]) requested to leave
/// the group by issuing a remove proposal in the previous epoch and the
/// proposal has now been committed.
TheyLeft(HashReference),
/// Another member (indicated by the [`HashReference`]) was removed by the [`Sender`].
TheyWereRemovedBy((HashReference, Sender)),
/// We removed another member (indicated by the [`HashReference`]).
WeRemovedThem(HashReference),
}
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_hash_ref = match group.key_package_ref() {
Some(key_package_ref) => key_package_ref,
None => return Err(LibraryError::custom("Own KeyPackage was empty.")),
};
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(hash_ref) = sender {
// We authored the remove proposal
if hash_ref == own_hash_ref {
if removed == own_hash_ref {
// We left
return Ok(Self::WeLeft);
} else {
// We removed another member
return Ok(Self::WeRemovedThem(*removed));
}
}
// Another member left
if removed == hash_ref {
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_hash_ref {
// We were removed
Ok(Self::WeWereRemovedBy(sender.clone()))
} else {
// Another member was removed
Ok(Self::TheyWereRemovedBy((*removed, sender.clone())))
}
}
}