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
use core_group::{proposals::QueuedProposal, staged_commit::StagedCommit};
use crate::group::{errors::ValidationError, mls_group::errors::UnverifiedMessageError};
use super::{proposals::ProposalStore, *};
impl CoreGroup {
/// This function is used to parse messages from the DS.
/// It checks for syntactic errors and makes some semantic checks as well.
/// If the input is a [MlsCiphertext] message, it will be decrypted.
/// Returns an [UnverifiedMessage] that can be inspected and later processed in
/// [Self::process_unverified_message()].
/// Checks the following semantic validation:
/// - ValSem002
/// - ValSem003
/// - ValSem004
/// - ValSem005
/// - ValSem006
/// - ValSem007
/// - ValSem009
/// - ValSem112
/// - ValSem246
pub(crate) fn parse_message(
&mut self,
backend: &impl OpenMlsCryptoProvider,
message: MlsMessageIn,
sender_ratchet_configuration: &SenderRatchetConfiguration,
) -> Result<UnverifiedMessage, ValidationError> {
// Checks the following semantic validation:
// - ValSem002
// - ValSem003
self.validate_framing(&message)?;
// Checks the following semantic validation:
// - ValSem006
let decrypted_message = match message.wire_format() {
WireFormat::MlsPlaintext => DecryptedMessage::from_inbound_plaintext(message)?,
WireFormat::MlsCiphertext => {
// If the message is older than the current epoch, we need to fetch the correct secret tree first
DecryptedMessage::from_inbound_ciphertext(
message,
backend,
self,
sender_ratchet_configuration,
)?
}
};
// Checks the following semantic validation:
// - ValSem004
// - ValSem005
// - ValSem007
// - ValSem009
self.validate_plaintext(decrypted_message.plaintext())?;
// Extract the credential if the sender is a member or a new member.
// Checks the following semantic validation:
// - ValSem112
// - ValSem246
// - Prepares ValSem247 by setting the right credential. The remainder
// of ValSem247 is validated as part of ValSem010.
// Preconfigured senders are not supported yet #106/#151.
let credential = decrypted_message.credential(
self.treesync(),
self.message_secrets_store
.leaves_for_epoch(decrypted_message.plaintext().epoch()),
)?;
Ok(UnverifiedMessage::from_decrypted_message(
decrypted_message,
Some(credential),
))
}
/// This processing function does most of the semantic verifications.
/// It returns a [ProcessedMessage] enum.
/// Checks the following semantic validation:
/// - ValSem008
/// - ValSem010
/// - ValSem100
/// - ValSem101
/// - ValSem102
/// - ValSem103
/// - ValSem104
/// - ValSem105
/// - ValSem106
/// - ValSem107
/// - ValSem108
/// - ValSem109
/// - ValSem110
/// - ValSem111
/// - ValSem112
/// - ValSem200
/// - ValSem201
/// - ValSem202: Path must be the right length
/// - ValSem203: Path secrets must decrypt correctly
/// - ValSem204: Public keys from Path must be verified and match the
/// private keys from the direct path
/// - ValSem205
/// - ValSem240
/// - ValSem241
/// - ValSem242
/// - ValSem243
/// - ValSem244
/// - ValSem245
/// - ValSem247 (as part of ValSem010)
pub(crate) fn process_unverified_message(
&mut self,
unverified_message: UnverifiedMessage,
signature_key: Option<&SignaturePublicKey>,
proposal_store: &ProposalStore,
own_kpbs: &[KeyPackageBundle],
backend: &impl OpenMlsCryptoProvider,
) -> Result<ProcessedMessage, UnverifiedMessageError> {
// Add the context to the message and verify the membership tag if necessary.
// If the message is older than the current epoch, we need to fetch the correct secret tree first.
let message_secrets = self
.message_secrets_mut(unverified_message.epoch())
.map_err(|e| match e {
SecretTreeError::TooDistantInThePast => UnverifiedMessageError::NoPastEpochData,
_ => LibraryError::custom("Unexpected return value").into(),
})?;
// Checks the following semantic validation:
// - ValSem008
let context_plaintext = UnverifiedContextMessage::from_unverified_message(
unverified_message,
message_secrets,
backend,
)
.map_err(|_| UnverifiedMessageError::InvalidMembershipTag)?;
match context_plaintext {
UnverifiedContextMessage::Group(unverified_message) => {
// Checks the following semantic validation:
// - ValSem010
// - ValSem247 (as part of ValSem010)
let verified_member_message = unverified_message
.into_verified(backend, signature_key)
.map_err(|_| UnverifiedMessageError::InvalidSignature)?;
Ok(match verified_member_message.plaintext().content() {
MlsPlaintextContentType::Application(application_message) => {
ProcessedMessage::ApplicationMessage(ApplicationMessage::new(
application_message.as_slice().to_vec(),
))
}
MlsPlaintextContentType::Proposal(_proposal) => {
ProcessedMessage::ProposalMessage(Box::new(
QueuedProposal::from_mls_plaintext(
self.ciphersuite(),
backend,
verified_member_message.take_plaintext(),
)?,
))
}
MlsPlaintextContentType::Commit(_commit) => {
// - ValSem100
// - ValSem101
// - ValSem102
// - ValSem103
// - ValSem104
// - ValSem105
// - ValSem106
// - ValSem107
// - ValSem108
// - ValSem109
// - ValSem110
// - ValSem111
// - ValSem112
// - ValSem200
// - ValSem201
// - ValSem202: Path must be the right length
// - ValSem203: Path secrets must decrypt correctly
// - ValSem204: Public keys from Path must be verified
// and match the private keys from the
// direct path
// - ValSem205
// - ValSem240
// - ValSem241
// - ValSem242
// - ValSem243
// - ValSem244
// - ValSem245
let staged_commit = self.stage_commit(
verified_member_message.plaintext(),
proposal_store,
own_kpbs,
backend,
)?;
ProcessedMessage::StagedCommitMessage(Box::new(staged_commit))
}
})
}
UnverifiedContextMessage::Preconfigured(external_message) => {
// Signature verification
if let Some(signature_public_key) = signature_key {
let _verified_external_message = external_message
.into_verified(backend, signature_public_key)
.map_err(|_| UnverifiedMessageError::InvalidSignature)?;
} else {
return Err(UnverifiedMessageError::MissingSignatureKey);
}
// We don't support external messages from preconfigured senders yet
// TODO #151/#106
todo!()
}
}
}
/// Merge a [StagedCommit] into the group after inspection
pub(crate) fn merge_staged_commit(
&mut self,
staged_commit: StagedCommit,
proposal_store: &mut ProposalStore,
) -> Result<(), LibraryError> {
// Save the past epoch
let past_epoch = self.context().epoch();
// We may need to keep a mapping from key package references to indices.
let leaves = self
.treesync()
.full_leaves()
// This should disappear after refactoring TreeSync, fetching the leaves should never fail
.map_err(|_| LibraryError::custom("Unexpected error in TreeSync"))?;
let mut my_leaves = Vec::with_capacity(leaves.len());
for (&i, _) in leaves.iter() {
my_leaves.push((
i,
self.treesync().leaf_id(i).ok_or_else(|| {
LibraryError::custom(
"Unable to get the key package reference for a leaf from \
tree. This indicates a bug in the library where the tree \
isn't built correctly.",
)
})?,
))
}
// Merge the staged commit into the group state and store the secret tree from the
// previous epoch in the message secrets store.
if let Some(message_secrets) = self.merge_commit(staged_commit)? {
self.message_secrets_store
.add(past_epoch, message_secrets, my_leaves);
}
// Empty the proposal store
proposal_store.empty();
Ok(())
}
}