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
use std::collections::HashSet;
use openmls_traits::OpenMlsCryptoProvider;
use crate::{
binary_tree::LeafIndex,
group::CoreGroupError,
key_packages::KeyPackageBundle,
messages::{
proposals::{AddProposal, ProposalType},
Proposal,
},
schedule::{InitSecret, PreSharedKeyId, PreSharedKeys},
treesync::{diff::TreeSyncDiff, node::leaf_node::LeafNode, TreeSyncError},
};
use super::{proposals::ProposalQueue, CoreGroup};
pub(crate) struct ApplyProposalsValues {
pub(crate) path_required: bool,
pub(crate) self_removed: bool,
pub(crate) invitation_list: Vec<(LeafIndex, AddProposal)>,
pub(crate) presharedkeys: PreSharedKeys,
pub(crate) external_init_secret_option: Option<InitSecret>,
}
impl ApplyProposalsValues {
pub(crate) fn exclusion_list(&self) -> HashSet<&LeafIndex> {
let new_leaves_indexes: HashSet<&LeafIndex> = self
.invitation_list
.iter()
.map(|(index, _)| index)
.collect();
new_leaves_indexes
}
}
impl CoreGroup {
pub(crate) fn apply_proposals(
&self,
diff: &mut TreeSyncDiff,
backend: &impl OpenMlsCryptoProvider,
proposal_queue: &ProposalQueue,
key_package_bundles: &[KeyPackageBundle],
) -> Result<ApplyProposalsValues, CoreGroupError> {
log::debug!("Applying proposal");
let mut has_updates = false;
let mut has_removes = false;
let mut self_removed = false;
let mut external_init_secret_option = None;
if let Some(queued_proposal) = proposal_queue
.filtered_by_type(ProposalType::ExternalInit)
.next()
{
if let Proposal::ExternalInit(external_init_proposal) = queued_proposal.proposal() {
let external_priv = self
.group_epoch_secrets()
.external_secret()
.derive_external_keypair(backend.crypto(), self.ciphersuite())
.private
.into();
external_init_secret_option = Some(InitSecret::from_kem_output(
backend,
self.ciphersuite(),
self.mls_version,
&external_priv,
external_init_proposal.kem_output(),
)?)
}
}
for queued_proposal in proposal_queue.filtered_by_type(ProposalType::Update) {
has_updates = true;
if let Proposal::Update(update_proposal) = queued_proposal.proposal() {
let sender = queued_proposal.sender();
let sender_index = self
.sender_index(sender.as_key_package_ref()?)
.map_err(|_| TreeSyncError::KeyPackageRefNotInTree)?;
let leaf_node: LeafNode = if sender_index == self.tree.own_leaf_index() {
let own_kpb = match key_package_bundles
.iter()
.find(|&kpb| kpb.key_package() == update_proposal.key_package())
{
Some(kpb) => kpb,
None => return Err(CoreGroupError::MissingKeyPackageBundle),
};
LeafNode::new_from_bundle(own_kpb.clone(), backend.crypto())
} else {
LeafNode::new(update_proposal.key_package().clone(), backend.crypto())
}?;
diff.update_leaf(leaf_node, sender_index)?;
}
}
for queued_proposal in proposal_queue.filtered_by_type(ProposalType::Remove) {
has_removes = true;
if let Proposal::Remove(remove_proposal) = queued_proposal.proposal() {
if let Some(own_kpr) = self.key_package_ref() {
if remove_proposal.removed() == own_kpr {
self_removed = true;
}
}
if let Ok(removed_index) = self.sender_index(remove_proposal.removed()) {
diff.blank_leaf(removed_index)?;
}
}
}
let add_proposals = proposal_queue
.filtered_by_type(ProposalType::Add)
.filter_map(|queued_proposal| {
if let Proposal::Add(add_proposal) = queued_proposal.proposal() {
Some(add_proposal)
} else {
None
}
});
let mut invitation_list = Vec::new();
for add_proposal in add_proposals {
let leaf_index = diff.add_leaf(add_proposal.key_package().clone(), backend.crypto())?;
invitation_list.push((leaf_index, add_proposal.clone()))
}
let psks: Vec<PreSharedKeyId> = proposal_queue
.filtered_by_type(ProposalType::Presharedkey)
.filter_map(|queued_proposal| {
if let Proposal::PreSharedKey(psk_proposal) = queued_proposal.proposal() {
Some(psk_proposal.clone().into_psk_id())
} else {
None
}
})
.collect();
let presharedkeys = PreSharedKeys { psks: psks.into() };
let path_required = has_updates || has_removes || external_init_secret_option.is_some();
Ok(ApplyProposalsValues {
path_required,
self_removed,
invitation_list,
presharedkeys,
external_init_secret_option,
})
}
}