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
use std::mem;
use core_group::{
create_commit_params::CreateCommitParams, proposals::QueuedProposal,
staged_commit::StagedCommit,
};
use tls_codec::Serialize;
use super::*;
impl MlsGroup {
pub fn parse_message(
&mut self,
message: MlsMessageIn,
backend: &impl OpenMlsCryptoProvider,
) -> Result<UnverifiedMessage, MlsGroupError> {
if !self.is_active() {
return Err(MlsGroupError::UseAfterEviction(UseAfterEviction::Error));
}
if message.is_handshake_message()
&& !self
.configuration()
.wire_format_policy()
.incoming()
.is_compatible_with(message.wire_format())
{
return Err(MlsGroupError::IncompatibleWireFormat);
}
self.flag_state_change();
let sender_ratchet_configuration =
self.configuration().sender_ratchet_configuration().clone();
self.group
.parse_message(backend, message, &sender_ratchet_configuration)
.map_err(MlsGroupError::Group)
}
pub fn process_unverified_message(
&mut self,
unverified_message: UnverifiedMessage,
signature_key: Option<&SignaturePublicKey>,
backend: &impl OpenMlsCryptoProvider,
) -> Result<ProcessedMessage, MlsGroupError> {
self.group
.process_unverified_message(
unverified_message,
signature_key,
&self.proposal_store,
&self.own_kpbs,
backend,
)
.map_err(|e| e.into())
}
pub fn store_pending_proposal(&mut self, proposal: QueuedProposal) {
self.proposal_store.add(proposal);
self.flag_state_change();
}
pub fn commit_to_pending_proposals(
&mut self,
backend: &impl OpenMlsCryptoProvider,
) -> Result<(MlsMessageOut, Option<Welcome>), MlsGroupError> {
self.is_operational()?;
let credential = self.credential()?;
let credential_bundle: CredentialBundle = backend
.key_store()
.read(&credential.signature_key().tls_serialize_detached()?)
.ok_or(MlsGroupError::NoMatchingCredentialBundle)?;
let params = CreateCommitParams::builder()
.framing_parameters(self.framing_parameters())
.credential_bundle(&credential_bundle)
.proposal_store(&self.proposal_store)
.build();
let create_commit_result = self.group.create_commit(params, backend)?;
let mls_message = self.plaintext_to_mls_message(create_commit_result.commit, backend)?;
self.group_state = MlsGroupState::PendingCommit(Box::new(PendingCommitState::Member(
create_commit_result.staged_commit,
)));
self.flag_state_change();
Ok((mls_message, create_commit_result.welcome_option))
}
pub fn merge_staged_commit(
&mut self,
staged_commit: StagedCommit,
) -> Result<(), MlsGroupError> {
if staged_commit.self_removed() {
self.group_state = MlsGroupState::Inactive;
}
self.flag_state_change();
self.group
.merge_staged_commit(staged_commit, &mut self.proposal_store)
.map_err(MlsGroupError::Group)?;
let resumption_secret = self.group.group_epoch_secrets().resumption_secret();
self.resumption_secret_store
.add(self.group.context().epoch(), resumption_secret.clone());
self.own_kpbs.clear();
self.clear_pending_commit()?;
Ok(())
}
pub fn merge_pending_commit(&mut self) -> Result<(), MlsGroupError> {
match &self.group_state {
MlsGroupState::PendingCommit(_) => {
let old_state = mem::replace(&mut self.group_state, MlsGroupState::Operational);
if let MlsGroupState::PendingCommit(pending_commit_state) = old_state {
self.merge_staged_commit((*pending_commit_state).into())?
}
Ok(())
}
MlsGroupState::Operational => Err(MlsGroupError::NoPendingCommit),
MlsGroupState::Inactive => {
Err(MlsGroupError::UseAfterEviction(UseAfterEviction::Error))
}
}
}
}