Debian Codegraph @

mlspp/include/mls/state.h

raw symbols SCIP
1 #pragma once
2
3 #include "mls/crypto.h"
4 #include "mls/key_schedule.h"
5 #include "mls/messages.h"
6 #include "mls/treekem.h"
7 #include <list>
8 #include <optional>
9 #include <vector>
10
11 namespace mlspp {
12
13 // Index into the session roster
14 struct RosterIndex : public UInt32
15 {
16 using UInt32::UInt32;
17 };
18
19 struct CommitOpts
20 {
21 std::vector<Proposal> extra_proposals;
22 bool inline_tree;
23 bool force_path;
24 LeafNodeOptions leaf_node_opts;
25 };
26
27 struct MessageOpts
28 {
29 bool encrypt = false;
30 bytes authenticated_data;
31 size_t padding_size = 0;
32 };
33
34 class State
35 {
36 public:
37 ///
38 /// Constructors
39 ///
40
41 // Initialize an empty group
42 State(bytes group_id,
43 CipherSuite suite,
44 HPKEPrivateKey enc_priv,
45 SignaturePrivateKey sig_priv,
46 const LeafNode& leaf_node,
47 ExtensionList extensions);
48
49 // Initialize a group from a Welcome
50 State(const HPKEPrivateKey& init_priv,
51 HPKEPrivateKey leaf_priv,
52 SignaturePrivateKey sig_priv,
53 const KeyPackage& key_package,
54 const Welcome& welcome,
55 const std::optional<TreeKEMPublicKey>& tree,
56 std::map<bytes, bytes> psks);
57
58 // Join a group from outside
59 // XXX(RLB) To be fully general, we would need a few more options here, e.g.,
60 // whether to include PSKs or evict our prior appearance.
61 static std::tuple<MLSMessage, State> external_join(
62 const bytes& leaf_secret,
63 SignaturePrivateKey sig_priv,
64 const KeyPackage& key_package,
65 const GroupInfo& group_info,
66 const std::optional<TreeKEMPublicKey>& tree,
67 const MessageOpts& msg_opts,
68 std::optional<LeafIndex> remove_prior,
69 const std::map<bytes, bytes>& psks);
70
71 // Propose that a new member be added a group
72 static MLSMessage new_member_add(const bytes& group_id,
73 epoch_t epoch,
74 const KeyPackage& new_member,
75 const SignaturePrivateKey& sig_priv);
76
77 ///
78 /// Message factories
79 ///
80
81 Proposal add_proposal(const KeyPackage& key_package) const;
82 Proposal update_proposal(HPKEPrivateKey leaf_priv,
83 const LeafNodeOptions& opts);
84 Proposal remove_proposal(RosterIndex index) const;
85 Proposal remove_proposal(LeafIndex removed) const;
86 Proposal group_context_extensions_proposal(ExtensionList exts) const;
87 Proposal pre_shared_key_proposal(const bytes& external_psk_id) const;
88 Proposal pre_shared_key_proposal(const bytes& group_id, epoch_t epoch) const;
89 static Proposal reinit_proposal(bytes group_id,
90 ProtocolVersion version,
91 CipherSuite cipher_suite,
92 ExtensionList extensions);
93
94 MLSMessage add(const KeyPackage& key_package, const MessageOpts& msg_opts);
95 MLSMessage update(HPKEPrivateKey leaf_priv,
96 const LeafNodeOptions& opts,
97 const MessageOpts& msg_opts);
98 MLSMessage remove(RosterIndex index, const MessageOpts& msg_opts);
99 MLSMessage remove(LeafIndex removed, const MessageOpts& msg_opts);
100 MLSMessage group_context_extensions(ExtensionList exts,
101 const MessageOpts& msg_opts);
102 MLSMessage pre_shared_key(const bytes& external_psk_id,
103 const MessageOpts& msg_opts);
104 MLSMessage pre_shared_key(const bytes& group_id,
105 epoch_t epoch,
106 const MessageOpts& msg_opts);
107 MLSMessage reinit(bytes group_id,
108 ProtocolVersion version,
109 CipherSuite cipher_suite,
110 ExtensionList extensions,
111 const MessageOpts& msg_opts);
112
113 std::tuple<MLSMessage, Welcome, State> commit(
114 const bytes& leaf_secret,
115 const std::optional<CommitOpts>& opts,
116 const MessageOpts& msg_opts);
117
118 ///
119 /// Generic handshake message handlers
120 ///
121 std::optional<State> handle(const MLSMessage& msg);
122 std::optional<State> handle(const MLSMessage& msg,
123 std::optional<State> cached_state);
124
125 std::optional<State> handle(const ValidatedContent& content_auth);
126 std::optional<State> handle(const ValidatedContent& content_auth,
127 std::optional<State> cached_state);
128
129 ///
130 /// PSK management
131 ///
132 void add_resumption_psk(const bytes& group_id, epoch_t epoch, bytes secret);
133 void remove_resumption_psk(const bytes& group_id, epoch_t epoch);
134 void add_external_psk(const bytes& id, const bytes& secret);
135 void remove_external_psk(const bytes& id);
136
137 ///
138 /// Accessors
139 ///
140 const bytes& group_id() const { return _group_id; }
141 epoch_t epoch() const { return _epoch; }
142 LeafIndex index() const { return _index; }
143 CipherSuite cipher_suite() const { return _suite; }
144 const ExtensionList& extensions() const { return _extensions; }
145 const TreeKEMPublicKey& tree() const { return _tree; }
146 const bytes& resumption_psk() const { return _key_schedule.resumption_psk; }
147
148 bytes do_export(const std::string& label,
149 const bytes& context,
150 size_t size) const;
151 GroupInfo group_info(bool inline_tree) const;
152
153 // Ordered list of credentials from non-blank leaves
154 std::vector<LeafNode> roster() const;
155
156 bytes epoch_authenticator() const;
157
158 ///
159 /// Unwrap messages so that applications can inspect them
160 ///
161 ValidatedContent unwrap(const MLSMessage& msg);
162
163 ///
164 /// Application encryption and decryption
165 ///
166 MLSMessage protect(const bytes& authenticated_data,
167 const bytes& pt,
168 size_t padding_size);
169 std::tuple<bytes, bytes> unprotect(const MLSMessage& ct);
170
171 // Assemble a group context for this state
172 GroupContext group_context() const;
173
174 // Subgroup branching
175 std::tuple<State, Welcome> create_branch(
176 bytes group_id,
177 HPKEPrivateKey enc_priv,
178 SignaturePrivateKey sig_priv,
179 const LeafNode& leaf_node,
180 ExtensionList extensions,
181 const std::vector<KeyPackage>& key_packages,
182 const bytes& leaf_secret,
183 const CommitOpts& commit_opts) const;
184 State handle_branch(const HPKEPrivateKey& init_priv,
185 HPKEPrivateKey enc_priv,
186 SignaturePrivateKey sig_priv,
187 const KeyPackage& key_package,
188 const Welcome& welcome,
189 const std::optional<TreeKEMPublicKey>& tree) const;
190
191 // Reinitialization
192 struct Tombstone
193 {
194 std::tuple<State, Welcome> create_welcome(
195 HPKEPrivateKey enc_priv,
196 SignaturePrivateKey sig_priv,
197 const LeafNode& leaf_node,
198 const std::vector<KeyPackage>& key_packages,
199 const bytes& leaf_secret,
200 const CommitOpts& commit_opts) const;
201 State handle_welcome(const HPKEPrivateKey& init_priv,
202 HPKEPrivateKey enc_priv,
203 SignaturePrivateKey sig_priv,
204 const KeyPackage& key_package,
205 const Welcome& welcome,
206 const std::optional<TreeKEMPublicKey>& tree) const;
207
208 TLS_SERIALIZABLE(prior_group_id, prior_epoch, resumption_psk, reinit);
209
210 const bytes epoch_authenticator;
211 const ReInit reinit;
212
213 private:
214 Tombstone(const State& state_in, ReInit reinit_in);
215
216 bytes prior_group_id;
217 epoch_t prior_epoch;
218 bytes resumption_psk;
219
220 friend class State;
221 };
222
223 std::tuple<Tombstone, MLSMessage> reinit_commit(
224 const bytes& leaf_secret,
225 const std::optional<CommitOpts>& opts,
226 const MessageOpts& msg_opts);
227 Tombstone handle_reinit_commit(const MLSMessage& commit);
228
229 protected:
230 // Shared confirmed state
231 // XXX([email protected]): Can these be made const?
232 CipherSuite _suite;
233 bytes _group_id;
234 epoch_t _epoch;
235 TreeKEMPublicKey _tree;
236 TreeKEMPrivateKey _tree_priv;
237 TranscriptHash _transcript_hash;
238 ExtensionList _extensions;
239
240 // Shared secret state
241 KeyScheduleEpoch _key_schedule;
242 GroupKeySource _keys;
243
244 // Per-participant state
245 LeafIndex _index;
246 SignaturePrivateKey _identity_priv;
247
248 // Storage for PSKs
249 std::map<bytes, bytes> _external_psks;
250
251 using EpochRef = std::tuple<bytes, epoch_t>;
252 std::map<EpochRef, bytes> _resumption_psks;
253
254 // Cache of Proposals and update secrets
255 struct CachedProposal
256 {
257 ProposalRef ref;
258 Proposal proposal;
259 std::optional<LeafIndex> sender;
260 };
261 std::list<CachedProposal> _pending_proposals;
262
263 struct CachedUpdate
264 {
265 HPKEPrivateKey update_priv;
266 Update proposal;
267 };
268 std::optional<CachedUpdate> _cached_update;
269
270 // Assemble a preliminary, unjoined group state
271 State(SignaturePrivateKey sig_priv,
272 const GroupInfo& group_info,
273 const std::optional<TreeKEMPublicKey>& tree);
274
275 // Assemble a group from a Welcome, allowing for resumption PSKs
276 State(const HPKEPrivateKey& init_priv,
277 HPKEPrivateKey leaf_priv,
278 SignaturePrivateKey sig_priv,
279 const KeyPackage& key_package,
280 const Welcome& welcome,
281 const std::optional<TreeKEMPublicKey>& tree,
282 std::map<bytes, bytes> external_psks,
283 std::map<EpochRef, bytes> resumption_psks);
284
285 // Import a tree from an externally-provided tree or an extension
286 TreeKEMPublicKey import_tree(const bytes& tree_hash,
287 const std::optional<TreeKEMPublicKey>& external,
288 const ExtensionList& extensions);
289 bool validate_tree() const;
290
291 // Form a commit, covering all the cases with slightly different validation
292 // rules:
293 // * Normal
294 // * External
295 // * Branch
296 // * Reinit
297 struct NormalCommitParams
298 {};
299
300 struct ExternalCommitParams
301 {
302 KeyPackage joiner_key_package;
303 bytes force_init_secret;
304 };
305
306 struct RestartCommitParams
307 {
308 ResumptionPSKUsage allowed_usage;
309 };
310
311 struct ReInitCommitParams
312 {};
313
314 using CommitParams = var::variant<NormalCommitParams,
315 ExternalCommitParams,
316 RestartCommitParams,
317 ReInitCommitParams>;
318
319 std::tuple<MLSMessage, Welcome, State> commit(
320 const bytes& leaf_secret,
321 const std::optional<CommitOpts>& opts,
322 const MessageOpts& msg_opts,
323 CommitParams params);
324
325 std::optional<State> handle(
326 const MLSMessage& msg,
327 std::optional<State> cached_state,
328 const std::optional<CommitParams>& expected_params);
329 std::optional<State> handle(
330 const ValidatedContent& val_content,
331 std::optional<State> cached_state,
332 const std::optional<CommitParams>& expected_params);
333
334 // Create an MLSMessage encapsulating some content
335 template<typename Inner>
336 AuthenticatedContent sign(const Sender& sender,
337 Inner&& content,
338 const bytes& authenticated_data,
339 bool encrypt) const;
340
341 MLSMessage protect(AuthenticatedContent&& content_auth, size_t padding_size);
342
343 template<typename Inner>
344 MLSMessage protect_full(Inner&& content, const MessageOpts& msg_opts);
345
346 // Apply the changes requested by various messages
347 LeafIndex apply(const Add& add);
348 void apply(LeafIndex target, const Update& update);
349 void apply(LeafIndex target,
350 const Update& update,
351 const HPKEPrivateKey& leaf_priv);
352 LeafIndex apply(const Remove& remove);
353 void apply(const GroupContextExtensions& gce);
354 std::vector<LeafIndex> apply(const std::vector<CachedProposal>& proposals,
355 Proposal::Type required_type);
356 std::tuple<std::vector<LeafIndex>, std::vector<PSKWithSecret>> apply(
357 const std::vector<CachedProposal>& proposals);
358
359 // Verify that a specific key package or all members support a given set of
360 // extensions
361 bool extensions_supported(const ExtensionList& exts) const;
362
363 // Extract proposals and PSKs from cache
364 void cache_proposal(AuthenticatedContent content_auth);
365 std::optional<CachedProposal> resolve(
366 const ProposalOrRef& id,
367 std::optional<LeafIndex> sender_index) const;
368 std::vector<CachedProposal> must_resolve(
369 const std::vector<ProposalOrRef>& ids,
370 std::optional<LeafIndex> sender_index) const;
371
372 std::vector<PSKWithSecret> resolve(
373 const std::vector<PreSharedKeyID>& psks) const;
374
375 // Check properties of proposals
376 bool valid(const LeafNode& leaf_node,
377 LeafNodeSource required_source,
378 std::optional<LeafIndex> index) const;
379 bool valid(const KeyPackage& key_package) const;
380 bool valid(const Add& add) const;
381 bool valid(LeafIndex sender, const Update& update) const;
382 bool valid(const Remove& remove) const;
383 bool valid(const PreSharedKey& psk) const;
384 static bool valid(const ReInit& reinit);
385 bool valid(const ExternalInit& external_init) const;
386 bool valid(const GroupContextExtensions& gce) const;
387 bool valid(std::optional<LeafIndex> sender, const Proposal& proposal) const;
388
389 bool valid(const std::vector<CachedProposal>& proposals,
390 LeafIndex commit_sender,
391 const CommitParams& params) const;
392 bool valid_normal(const std::vector<CachedProposal>& proposals,
393 LeafIndex commit_sender) const;
394 bool valid_external(const std::vector<CachedProposal>& proposals) const;
395 static bool valid_reinit(const std::vector<CachedProposal>& proposals);
396 static bool valid_restart(const std::vector<CachedProposal>& proposals,
397 ResumptionPSKUsage allowed_usage);
398
399 static bool valid_external_proposal_type(const Proposal::Type proposal_type);
400
401 CommitParams infer_commit_type(
402 const std::optional<LeafIndex>& sender,
403 const std::vector<CachedProposal>& proposals,
404 const std::optional<CommitParams>& expected_params) const;
405 static bool path_required(const std::vector<CachedProposal>& proposals);
406
407 // Compare the **shared** attributes of the states
408 friend bool operator==(const State& lhs, const State& rhs);
409 friend bool operator!=(const State& lhs, const State& rhs);
410
411 // Derive and set the secrets for an epoch, given some new entropy
412 void update_epoch_secrets(const bytes& commit_secret,
413 const std::vector<PSKWithSecret>& psks,
414 const std::optional<bytes>& force_init_secret);
415
416 // Signature verification over a handshake message
417 bool verify_internal(const AuthenticatedContent& content_auth) const;
418 bool verify_external(const AuthenticatedContent& content_auth) const;
419 bool verify_new_member_proposal(
420 const AuthenticatedContent& content_auth) const;
421 bool verify_new_member_commit(const AuthenticatedContent& content_auth) const;
422 bool verify(const AuthenticatedContent& content_auth) const;
423
424 // Convert a Roster entry into LeafIndex
425 LeafIndex leaf_for_roster_entry(RosterIndex index) const;
426
427 // Create a draft successor state
428 State successor() const;
429 };
430
431 } // namespace mlspp