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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! This module contains the implementation of the processing functions for
//! public groups.
use openmls_traits::crypto::OpenMlsCrypto;
use tls_codec::Serialize;
use crate::{
ciphersuite::OpenMlsSignaturePublicKey,
credentials::{Credential, CredentialWithKey},
error::LibraryError,
framing::{
mls_auth_content::AuthenticatedContent, mls_content::FramedContentBody, ApplicationMessage,
DecryptedMessage, ProcessedMessage, ProcessedMessageContent, ProtocolMessage, Sender,
SenderContext, UnverifiedMessage,
},
group::{
errors::ValidationError, past_secrets::MessageSecretsStore, proposal_store::QueuedProposal,
PublicProcessMessageError,
},
messages::proposals::Proposal,
};
#[cfg(feature = "extensions-draft-08")]
use crate::prelude::processing::AppDataUpdates;
use super::PublicGroup;
impl PublicGroup {
/// 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 [PrivateMessage] 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
/// - ValSem245
pub(crate) fn parse_message<'a>(
&self,
decrypted_message: DecryptedMessage,
message_secrets_store_option: impl Into<Option<&'a MessageSecretsStore>>,
) -> Result<UnverifiedMessage, ValidationError> {
let message_secrets_store_option = message_secrets_store_option.into();
let verifiable_content = decrypted_message.verifiable_content();
// Checks the following semantic validation:
// - ValSem004
// - ValSem005
// - ValSem009
self.validate_verifiable_content(verifiable_content, message_secrets_store_option)?;
let message_epoch = verifiable_content.epoch();
// Depending on the epoch of the message, use the correct set of leaf nodes for getting the
// credential and signature key for the member with given index.
let look_up_credential_with_key = |leaf_node_index| {
if message_epoch == self.group_context().epoch() {
self.treesync()
.leaf(leaf_node_index)
.map(CredentialWithKey::from)
} else if let Some(store) = message_secrets_store_option {
// The message is from a past epoch, look up the member in the
// past secrets store based on the epoch and sender's leaf
// index.
store
.leaves_for_epoch(message_epoch)
.get(&leaf_node_index)
.map(|&member| CredentialWithKey::from(member))
} else {
None
}
};
// Extract the credential if the sender is a member or a new member.
// Checks the following semantic validation:
// - ValSem112
// - ValSem245
// - Prepares ValSem246 by setting the right credential. The remainder
// of ValSem246 is validated as part of ValSem010.
// External senders are not supported yet #106/#151.
let CredentialWithKey {
credential,
signature_key,
} = decrypted_message.credential(
look_up_credential_with_key,
self.group_context().extensions().external_senders(),
)?;
let signature_public_key = OpenMlsSignaturePublicKey::from_signature_key(
signature_key,
self.ciphersuite().signature_algorithm(),
);
// For commit messages, we need to check if the sender is a member or a
// new member and set the tree position accordingly.
let sender_context = match decrypted_message.sender() {
Sender::Member(leaf_index) => Some(SenderContext::Member((
self.group_id().clone(),
*leaf_index,
))),
Sender::NewMemberCommit => Some(SenderContext::ExternalCommit {
group_id: self.group_id().clone(),
leftmost_blank_index: self.treesync().free_leaf_index(),
self_removes_in_store: self.proposal_store.self_removes(),
}),
Sender::External(_) | Sender::NewMemberProposal => None,
};
Ok(UnverifiedMessage::from_decrypted_message(
decrypted_message,
credential,
signature_public_key,
sender_context,
))
}
/// This function is used to parse messages from the DS. It checks for
/// syntactic errors and does semantic validation as well. It returns a
/// [ProcessedMessage] enum. Checks the following semantic validation:
/// - ValSem002
/// - ValSem003
/// - ValSem004
/// - ValSem005
/// - ValSem006
/// - ValSem007
/// - ValSem008
/// - ValSem009
/// - ValSem010
/// - ValSem101
/// - ValSem102
/// - ValSem104
/// - ValSem106
/// - ValSem107
/// - ValSem108
/// - 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
/// - ValSem244
/// - ValSem245
/// - ValSem246 (as part of ValSem010)
pub fn process_message(
&self,
crypto: &impl OpenMlsCrypto,
message: impl Into<ProtocolMessage>,
) -> Result<ProcessedMessage, PublicProcessMessageError> {
let protocol_message = message.into();
// Checks the following semantic validation:
// - ValSem002
// - ValSem003
self.validate_framing(&protocol_message)?;
let decrypted_message = match protocol_message {
ProtocolMessage::PrivateMessage(_) => {
return Err(PublicProcessMessageError::IncompatibleWireFormat)
}
ProtocolMessage::PublicMessage(public_message) => {
DecryptedMessage::from_inbound_public_message(
*public_message,
None,
self.group_context()
.tls_serialize_detached()
.map_err(LibraryError::missing_bound_check)?,
crypto,
self.ciphersuite(),
)?
}
};
let unverified_message = self
.parse_message(decrypted_message, None)
.map_err(PublicProcessMessageError::from)?;
self.process_unverified_message(crypto, unverified_message)
}
}
impl PublicGroup {
/// This processing function does most of the semantic verifications.
/// It returns a [ProcessedMessage] enum.
/// Checks the following semantic validation:
/// - ValSem008
/// - ValSem010
/// - ValSem101
/// - ValSem102
/// - ValSem104
/// - ValSem106
/// - ValSem107
/// - ValSem108
/// - 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
/// - ValSem244
/// - ValSem246 (as part of ValSem010)
pub(crate) fn process_unverified_message(
&self,
crypto: &impl OpenMlsCrypto,
unverified_message: UnverifiedMessage,
) -> Result<ProcessedMessage, PublicProcessMessageError> {
// Checks the following semantic validation:
// - ValSem010
// - ValSem246 (as part of ValSem010)
// - https://validation.openmls.tech/#valn1203
let (content, credential) =
unverified_message.verify(self.ciphersuite(), crypto, self.version())?;
match content.sender() {
Sender::Member(_) | Sender::NewMemberCommit | Sender::NewMemberProposal => {
self.process_internal_authenticated_content(crypto, content, credential)
}
Sender::External(_) => {
self.process_external_authenticated_content(crypto, content, credential)
}
}
}
/// This processing function does most of the semantic verifications.
/// It returns a [ProcessedMessage] enum.
/// Checks the following semantic validation:
/// - ValSem008
/// - ValSem010
/// - ValSem101
/// - ValSem102
/// - ValSem104
/// - ValSem106
/// - ValSem107
/// - ValSem108
/// - 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
/// - ValSem244
/// - ValSem246 (as part of ValSem010)
#[cfg(feature = "extensions-draft-08")]
pub fn process_unverified_message_with_app_data_updates(
&self,
crypto: &impl OpenMlsCrypto,
unverified_message: UnverifiedMessage,
app_data_dict_updates: Option<AppDataUpdates>,
) -> Result<ProcessedMessage, PublicProcessMessageError> {
// Checks the following semantic validation:
// - ValSem010
// - ValSem246 (as part of ValSem010)
// - https://validation.openmls.tech/#valn1203
let (content, credential) =
unverified_message.verify(self.ciphersuite(), crypto, self.version())?;
match content.sender() {
Sender::Member(_) | Sender::NewMemberCommit | Sender::NewMemberProposal => self
.process_internal_authenticated_content_with_app_data_updates(
crypto,
content,
credential,
app_data_dict_updates,
),
Sender::External(_) => {
self.process_external_authenticated_content(crypto, content, credential)
}
}
}
fn process_internal_authenticated_content(
&self,
crypto: &impl OpenMlsCrypto,
content: AuthenticatedContent,
credential: Credential,
) -> Result<ProcessedMessage, PublicProcessMessageError> {
let sender = content.sender().clone();
let authenticated_data = content.authenticated_data().to_owned();
let content = match content.content() {
FramedContentBody::Application(application_message) => {
ProcessedMessageContent::ApplicationMessage(ApplicationMessage::new(
application_message.as_slice().to_owned(),
))
}
FramedContentBody::Proposal(_) => {
let proposal = Box::new(QueuedProposal::from_authenticated_content_by_ref(
self.ciphersuite(),
crypto,
content,
)?);
if matches!(sender, Sender::NewMemberProposal) {
ProcessedMessageContent::ExternalJoinProposalMessage(proposal)
} else {
ProcessedMessageContent::ProposalMessage(proposal)
}
}
FramedContentBody::Commit(_) => {
let staged_commit = self.stage_commit(&content, crypto)?;
ProcessedMessageContent::StagedCommitMessage(Box::new(staged_commit))
}
};
Ok(ProcessedMessage::new(
self.group_id().clone(),
self.group_context().epoch(),
sender,
authenticated_data,
content,
credential,
))
}
#[cfg(feature = "extensions-draft-08")]
fn process_internal_authenticated_content_with_app_data_updates(
&self,
crypto: &impl OpenMlsCrypto,
content: AuthenticatedContent,
credential: Credential,
app_data_dict_updates: Option<AppDataUpdates>,
) -> Result<ProcessedMessage, PublicProcessMessageError> {
let sender = content.sender().clone();
let authenticated_data = content.authenticated_data().to_owned();
debug_assert!(matches!(
sender,
Sender::Member(_) | Sender::NewMemberCommit | Sender::NewMemberProposal
));
let content = match content.content() {
FramedContentBody::Application(application_message) => {
ProcessedMessageContent::ApplicationMessage(ApplicationMessage::new(
application_message.as_slice().to_owned(),
))
}
FramedContentBody::Proposal(_) => {
let proposal = Box::new(QueuedProposal::from_authenticated_content_by_ref(
self.ciphersuite(),
crypto,
content,
)?);
if matches!(sender, Sender::NewMemberProposal) {
ProcessedMessageContent::ExternalJoinProposalMessage(proposal)
} else {
ProcessedMessageContent::ProposalMessage(proposal)
}
}
FramedContentBody::Commit(_) => {
let staged_commit = self.stage_commit_with_app_data_updates(
&content,
crypto,
app_data_dict_updates,
)?;
ProcessedMessageContent::StagedCommitMessage(Box::new(staged_commit))
}
};
Ok(ProcessedMessage::new(
self.group_id().clone(),
self.group_context().epoch(),
sender,
authenticated_data,
content,
credential,
))
}
fn process_external_authenticated_content(
&self,
crypto: &impl OpenMlsCrypto,
content: AuthenticatedContent,
credential: Credential,
) -> Result<ProcessedMessage, PublicProcessMessageError> {
let sender = content.sender().clone();
let data = content.authenticated_data().to_owned();
debug_assert!(matches!(sender, Sender::External(_)));
// https://validation.openmls.tech/#valn1501
match content.content() {
FramedContentBody::Application(_) => {
Err(PublicProcessMessageError::UnauthorizedExternalApplicationMessage)
}
// TODO: https://validation.openmls.tech/#valn1502
FramedContentBody::Proposal(Proposal::GroupContextExtensions(_)) => {
let content = ProcessedMessageContent::ProposalMessage(Box::new(
QueuedProposal::from_authenticated_content_by_ref(
self.ciphersuite(),
crypto,
content,
)?,
));
Ok(ProcessedMessage::new(
self.group_id().clone(),
self.group_context().epoch(),
sender,
data,
content,
credential,
))
}
FramedContentBody::Proposal(Proposal::Remove(_)) => {
let content = ProcessedMessageContent::ProposalMessage(Box::new(
QueuedProposal::from_authenticated_content_by_ref(
self.ciphersuite(),
crypto,
content,
)?,
));
Ok(ProcessedMessage::new(
self.group_id().clone(),
self.group_context().epoch(),
sender,
data,
content,
credential,
))
}
FramedContentBody::Proposal(Proposal::Add(_)) => {
let content = ProcessedMessageContent::ProposalMessage(Box::new(
QueuedProposal::from_authenticated_content_by_ref(
self.ciphersuite(),
crypto,
content,
)?,
));
Ok(ProcessedMessage::new(
self.group_id().clone(),
self.group_context().epoch(),
sender,
data,
content,
credential,
))
}
// TODO #151/#106
FramedContentBody::Proposal(_) => {
Err(PublicProcessMessageError::UnsupportedProposalType)
}
FramedContentBody::Commit(_) => {
Err(PublicProcessMessageError::UnauthorizedExternalCommitMessage)
}
}
}
}