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
//! This module contains validation functions for incoming messages
//! as defined in <https://github.com/openmls/openmls/wiki/Message-validation>
use std::collections::HashSet;
use crate::{
ciphersuite::hash_ref::HashReference,
error::LibraryError,
extensions::ExtensionType,
framing::Sender,
group::errors::ExternalCommitValidationError,
group::errors::ValidationError,
messages::proposals::{Proposal, ProposalOrRefType, ProposalType},
};
use super::{
proposals::ProposalQueue, ContentType, CoreGroup, KeyPackage, MlsMessageIn,
ProposalValidationError, VerifiableMlsPlaintext, WireFormat,
};
impl CoreGroup {
// === Messages ===
/// Checks the following semantic validation:
/// - ValSem002
/// - ValSem003
pub(crate) fn validate_framing(&self, message: &MlsMessageIn) -> Result<(), ValidationError> {
// ValSem002
if message.group_id() != self.group_id() {
return Err(ValidationError::WrongGroupId);
}
// ValSem003: Check boundaries for the epoch
// We differentiate depending on the content type
match message.content_type() {
// For application messages we allow messages for older epochs as well
ContentType::Application => {
if message.epoch() > self.context().epoch() {
return Err(ValidationError::WrongEpoch);
}
}
// For all other messages we only only accept the current epoch
_ => {
if message.epoch() != self.context().epoch() {
return Err(ValidationError::WrongEpoch);
}
}
}
Ok(())
}
/// Checks the following semantic validation:
/// - ValSem004
/// - ValSem005
/// - ValSem007
/// - ValSem009
pub(crate) fn validate_plaintext(
&self,
plaintext: &VerifiableMlsPlaintext,
) -> Result<(), ValidationError> {
// ValSem004
let sender = plaintext.sender();
if let Sender::Member(hash_ref) = sender {
// If the sender is a member, it has to be in the tree ...
if self
.treesync()
.leaf_from_id(hash_ref)
.is_none()
// ... or in a tree from a past epoch we still have around.
&& !self
.message_secrets_store
.epoch_has_leaf(plaintext.epoch(), hash_ref)
{
return Err(ValidationError::UnknownMember);
}
}
// ValSem005
// Application messages must always be encrypted
if plaintext.content_type() == ContentType::Application {
if plaintext.wire_format() != WireFormat::MlsCiphertext {
return Err(ValidationError::UnencryptedApplicationMessage);
} else if !plaintext.sender().is_member() {
return Err(ValidationError::NonMemberApplicationMessage);
}
}
// ValSem007
// If the sender is of type member and the message was not an MlsCiphertext,
// the member has to prove its ownership by adding a membership tag.
// The membership tag is checkecked in ValSem008.
if plaintext.sender().is_member()
&& plaintext.wire_format() != WireFormat::MlsCiphertext
&& plaintext.membership_tag().is_none()
{
return Err(ValidationError::MissingMembershipTag);
}
// ValSem009
if plaintext.content_type() == ContentType::Commit && plaintext.confirmation_tag().is_none()
{
return Err(ValidationError::MissingConfirmationTag);
}
Ok(())
}
// === Proposals ===
/// Validate Add proposals. This function implements the following checks:
/// - ValSem100
/// - ValSem101
/// - ValSem102
/// - ValSem103
/// - ValSem104
/// - ValSem105
/// - ValSem106
pub(crate) fn validate_add_proposals(
&self,
proposal_queue: &ProposalQueue,
) -> Result<(), ProposalValidationError> {
let add_proposals = proposal_queue.add_proposals();
let mut identity_set = HashSet::new();
let mut signature_key_set = HashSet::new();
let mut public_key_set = HashSet::new();
for add_proposal in add_proposals {
let identity = add_proposal
.add_proposal()
.key_package()
.credential()
.identity()
.to_vec();
// ValSem100
if !identity_set.insert(identity) {
return Err(ProposalValidationError::DuplicateIdentityAddProposal);
}
let signature_key = add_proposal
.add_proposal()
.key_package()
.credential()
.signature_key()
.as_slice()
.to_vec();
// ValSem101
if !signature_key_set.insert(signature_key) {
return Err(ProposalValidationError::DuplicateSignatureKeyAddProposal);
}
let public_key = add_proposal
.add_proposal()
.key_package()
.hpke_init_key()
.as_slice()
.to_vec();
// ValSem102
if !public_key_set.insert(public_key) {
return Err(ProposalValidationError::DuplicatePublicKeyAddProposal);
}
// ValSem106: Check the required capabilities of the add proposals
// This includes the following checks:
// - Do ciphersuite and version match that of the group?
// - Are the two listed in the `Capabilities` Extension?
// - If a `RequiredCapabilitiesExtension` is present in the group:
// Does the key package advertise the capabilities required by that
// extension?
// Check if ciphersuite and version of the group are correct.
if add_proposal.add_proposal().key_package().ciphersuite() != self.ciphersuite()
|| add_proposal.add_proposal().key_package().protocol_version() != self.version()
{
log::error!("Tried to commit an Add proposal, where either the `Ciphersuite` or the `ProtocolVersion` is not compatible with the group.");
return Err(ProposalValidationError::InsufficientCapabilities);
}
// Check if the ciphersuite and the version of the group are
// supported.
let capabilities = add_proposal
.add_proposal()
.key_package()
.extension_with_type(ExtensionType::Capabilities)
.ok_or(ProposalValidationError::InsufficientCapabilities)?
.as_capabilities_extension()
.map_err(|_| {
// Mismatches between Extensions and ExtensionTypes should be
// caught when constructing KeyPackages.
ProposalValidationError::LibraryError(LibraryError::custom(
"ExtensionType didn't match extension content.",
))
})?;
if !capabilities.ciphersuites().contains(&self.ciphersuite())
|| !capabilities.versions().contains(&self.version())
{
log::error!("Tried to commit an Add proposal, where either the group's `Ciphersuite` or the group's `ProtocolVersion` is not in the `KeyPackage`'s `Capabilities`.");
return Err(ProposalValidationError::InsufficientCapabilities);
}
// If there is a required capabilities extension, check if that one
// is supported.
if let Some(required_capabilities_extension) = self
.group_context_extensions()
.iter()
.find(|&e| e.extension_type() == ExtensionType::RequiredCapabilities)
{
let required_capabilities = required_capabilities_extension
.as_required_capabilities_extension()
.map_err(|_| {
// Mismatches between Extensions and ExtensionTypes should be
// caught when constructing KeyPackages.
ProposalValidationError::LibraryError(LibraryError::custom(
"ExtensionType didn't match extension content.",
))
})?;
// Check if all required capabilities are supported.
if !capabilities.supports_required_capabilities(required_capabilities) {
log::error!("Tried to commit an Add proposal, where the `Capabilities` of the given `KeyPackage` do not fulfill the `RequiredCapabilities` of the group.");
return Err(ProposalValidationError::InsufficientCapabilities);
}
}
}
for (_index, key_package) in self.treesync().full_leaves()? {
let identity = key_package.credential().identity();
// ValSem103
if identity_set.contains(identity) {
return Err(ProposalValidationError::ExistingIdentityAddProposal);
}
// ValSem104
let signature_key = key_package.credential().signature_key().as_slice();
if signature_key_set.contains(signature_key) {
return Err(ProposalValidationError::ExistingSignatureKeyAddProposal);
}
// ValSem105
let public_key = key_package.hpke_init_key().as_slice();
if public_key_set.contains(public_key) {
return Err(ProposalValidationError::ExistingPublicKeyAddProposal);
}
}
Ok(())
}
/// Validate Remove proposals. This function implements the following checks:
/// - ValSem107
/// - ValSem108
pub(crate) fn validate_remove_proposals(
&self,
proposal_queue: &ProposalQueue,
) -> Result<(), ProposalValidationError> {
let remove_proposals = proposal_queue.remove_proposals();
let mut removes_set = HashSet::new();
for remove_proposal in remove_proposals {
let removed = remove_proposal.remove_proposal().removed();
// ValSem107
if !removes_set.insert(*removed) {
return Err(ProposalValidationError::DuplicateMemberRemoval);
}
// TODO: ValSem108
if !self.treesync().leaves().contains_key(&Some(*removed)) {
return Err(ProposalValidationError::UnknownMemberRemoval);
}
}
Ok(())
}
/// Validate Update proposals. This function implements the following checks:
/// - ValSem109
/// - ValSem110
/// - ValSem111
/// - ValSem112
pub(crate) fn validate_update_proposals(
&self,
proposal_queue: &ProposalQueue,
committer: &HashReference,
) -> Result<HashSet<Vec<u8>>, ProposalValidationError> {
let mut public_key_set = HashSet::new();
for (_index, key_package) in self.treesync().full_leaves()? {
let public_key = key_package.hpke_init_key().as_slice().to_vec();
public_key_set.insert(public_key);
}
// Check the update proposals from the proposal queue first
let update_proposals = proposal_queue.update_proposals();
let tree = self.treesync();
for update_proposal in update_proposals {
let hash_ref = match update_proposal.sender() {
Sender::Member(hash_ref) => hash_ref,
_ => return Err(ProposalValidationError::UpdateFromNonMember),
};
// ValSem112
// The sender of a standalone update proposal must be of type member
if let Sender::Member(hash_ref) = update_proposal.sender() {
// ValSem111
// The sender of a full Commit must not include own update proposals
if committer == hash_ref {
return Err(ProposalValidationError::CommitterIncludedOwnUpdate);
}
} else {
return Err(ProposalValidationError::UpdateFromNonMember);
}
if let Some(leaf_node) = tree.leaf_from_id(hash_ref) {
let existing_key_package = leaf_node.key_package();
// ValSem109
// Identity must be unchanged between existing member and new proposal
if update_proposal
.update_proposal()
.key_package()
.credential()
.identity()
!= existing_key_package.credential().identity()
{
return Err(ProposalValidationError::UpdateProposalIdentityMismatch);
}
let public_key = update_proposal
.update_proposal()
.key_package()
.hpke_init_key()
.as_slice();
// ValSem110
// HPKE init key must be unique among existing members
if public_key_set.contains(public_key) {
return Err(ProposalValidationError::ExistingPublicKeyUpdateProposal);
}
} else {
return Err(ProposalValidationError::UnknownMember);
}
}
Ok(public_key_set)
}
/// Validate the new key package in a path
/// TODO: #730 - There's nothing testing this function.
/// - ValSem109
/// - ValSem110
pub(super) fn validate_path_key_package(
&self,
sender: u32,
key_package: &KeyPackage,
public_key_set: HashSet<Vec<u8>>,
proposal_sender: &Sender,
) -> Result<(), ProposalValidationError> {
let indexed_key_packages = self.treesync().full_leaves()?;
if let Some(existing_key_package) = indexed_key_packages.get(&sender) {
// ValSem109
if key_package.credential().identity() != existing_key_package.credential().identity() {
return Err(ProposalValidationError::UpdateProposalIdentityMismatch);
}
// ValSem110
if public_key_set.contains(key_package.hpke_init_key().as_slice()) {
return Err(ProposalValidationError::ExistingPublicKeyUpdateProposal);
}
} else if proposal_sender.is_member() {
return Err(ProposalValidationError::UnknownMember);
}
Ok(())
}
/// Validate constraints on an external commit. This function implements the following checks:
/// - ValSem240: External Commit, inline Proposals: There MUST be at least one ExternalInit proposal.
/// - ValSem241: External Commit, inline Proposals: There MUST be at most one ExternalInit proposal.
/// - ValSem242: External Commit, inline Proposals: There MUST NOT be any Add proposals.
/// - ValSem243: External Commit, inline Proposals: There MUST NOT be any Update proposals.
/// - ValSem244: External Commit, inline Remove Proposal: The identity and the endpoint_id of the removed
/// leaf are identical to the ones in the path KeyPackage.
/// - ValSem245: External Commit, referenced Proposals: There MUST NOT be any ExternalInit proposals.
pub(crate) fn validate_external_commit(
&self,
proposal_queue: &ProposalQueue,
path_key_package_option: Option<&KeyPackage>,
) -> Result<(), ExternalCommitValidationError> {
let mut external_init_proposals =
proposal_queue.filtered_by_type(ProposalType::ExternalInit);
// ValSem240: External Commit, inline Proposals: There MUST be at least one ExternalInit proposal.
if let Some(external_init_proposal) = external_init_proposals.next() {
// ValSem245: External Commit, referenced Proposals: There MUST NOT be any ExternalInit proposals.
if external_init_proposal.proposal_or_ref_type() == ProposalOrRefType::Reference {
return Err(ExternalCommitValidationError::ReferencedExternalInitProposal);
}
} else {
return Err(ExternalCommitValidationError::NoExternalInitProposals);
};
// ValSem241: External Commit, inline Proposals: There MUST be at most one ExternalInit proposal.
if external_init_proposals.next().is_some() {
// ValSem245: External Commit, referenced Proposals: There MUST NOT be any ExternalInit proposals.
return Err(ExternalCommitValidationError::MultipleExternalInitProposals);
}
let add_proposals = proposal_queue.filtered_by_type(ProposalType::Add);
for proposal in add_proposals {
// ValSem242: External Commit, inline Proposals: There MUST NOT be any Add proposals.
if proposal.proposal_or_ref_type() == ProposalOrRefType::Proposal {
return Err(ExternalCommitValidationError::InvalidInlineProposals);
}
}
let update_proposals = proposal_queue.filtered_by_type(ProposalType::Update);
for proposal in update_proposals {
// ValSem243: External Commit, inline Proposals: There MUST NOT be any Update proposals.
if proposal.proposal_or_ref_type() == ProposalOrRefType::Proposal {
return Err(ExternalCommitValidationError::InvalidInlineProposals);
}
}
let remove_proposals = proposal_queue.filtered_by_type(ProposalType::Remove);
for proposal in remove_proposals {
if proposal.proposal_or_ref_type() == ProposalOrRefType::Proposal {
if let Proposal::Remove(remove_proposal) = proposal.proposal() {
let removed_leaf = self
.treesync()
.leaf_from_id(remove_proposal.removed())
.ok_or(ExternalCommitValidationError::UnknownMemberRemoval)?;
if let Some(path_key_package) = path_key_package_option {
// ValSem244: External Commit, inline Remove Proposal: The identity and the endpoint_id of the removed leaf are identical to the ones in the path KeyPackage.
if removed_leaf.key_package().credential().identity()
!= path_key_package.credential().identity()
{
return Err(ExternalCommitValidationError::InvalidRemoveProposal);
}
};
}
}
}
Ok(())
}
}