Skip to main content

openmls/group/mls_group/
mod.rs

1//! MLS Group
2//!
3//! This module contains [`MlsGroup`] and its submodules.
4//!
5
6use past_secrets::MessageSecretsStore;
7use proposal_store::ProposalQueue;
8use serde::{Deserialize, Serialize};
9use tls_codec::Serialize as _;
10
11#[cfg(test)]
12use crate::treesync::node::leaf_node::TreePosition;
13
14use super::proposal_store::{ProposalStore, QueuedProposal};
15use crate::{
16    binary_tree::array_representation::LeafNodeIndex,
17    ciphersuite::{hash_ref::ProposalRef, signable::Signable},
18    credentials::Credential,
19    error::LibraryError,
20    extensions::Extensions,
21    framing::{mls_auth_content::AuthenticatedContent, *},
22    group::{
23        CreateGroupContextExtProposalError, DeletePastEpochSecretsError, Extension, ExtensionType,
24        ExternalPubExtension, GroupContext, GroupEpoch, GroupId, MlsGroupJoinConfig,
25        MlsGroupStateError, OutgoingWireFormatPolicy, PublicGroup, RatchetTreeExtension,
26        RequiredCapabilitiesExtension, SetPastEpochDeletionPolicyError, StagedCommit,
27    },
28    key_packages::{InitKey, KeyPackageBundle},
29    messages::{
30        group_info::{GroupInfo, GroupInfoTBS, VerifiableGroupInfo},
31        proposals::*,
32        ConfirmationTag, GroupSecrets, Welcome,
33    },
34    schedule::{
35        message_secrets::MessageSecrets,
36        psk::{load_psks, store::ResumptionPskStore, PskSecret},
37        GroupEpochSecrets, JoinerSecret, KeySchedule,
38    },
39    storage::{OpenMlsProvider, StorageProvider},
40    treesync::{
41        node::{encryption_keys::EncryptionKeyPair, leaf_node::LeafNode},
42        RatchetTree, TreeSync,
43    },
44    versions::ProtocolVersion,
45};
46use openmls_traits::{
47    crypto::OpenMlsCrypto, signatures::Signer, storage::StorageProvider as _, types::Ciphersuite,
48};
49
50#[cfg(feature = "extensions-draft")]
51use crate::schedule::{application_export_tree::ApplicationExportTree, ApplicationExportSecret};
52
53#[cfg(all(feature = "virtual-clients-draft", not(target_arch = "wasm32")))]
54use std::time::SystemTime;
55
56#[cfg(all(feature = "virtual-clients-draft", target_arch = "wasm32"))]
57use web_time::SystemTime;
58
59#[cfg(feature = "virtual-clients-draft")]
60use crate::group::{
61    VcDerivationEpochDeletion, VcDerivationEpochDeletionResult, VcDerivationEpochDeletionTime,
62    VcDerivationEpochRetentionPolicy,
63};
64
65// Private
66mod application;
67mod exporting;
68mod updates;
69
70#[cfg(feature = "migration-import")]
71pub(crate) mod migration_import;
72
73#[cfg(feature = "virtual-clients-draft")]
74pub use application::UnconfirmedMessage;
75pub use branch::BranchInfo;
76pub use exporting::{
77    ExportedSecret, GroupExport, ProcessedWelcomeExport, StagedCommitExport, StagedWelcomeExport,
78};
79#[cfg(feature = "extensions-draft")]
80pub use exporting::{GroupSafeExport, PendingSafeExport, StagedCommitSafeExport};
81pub use proposal::Propose;
82pub use reinit::ReInitInfo;
83
84use config::*;
85
86// Crate
87pub(crate) mod branch;
88pub(crate) mod builder;
89pub(crate) mod commit_builder;
90pub(crate) mod config;
91pub(crate) mod creation;
92pub(crate) mod errors;
93pub(crate) mod membership;
94pub(crate) mod past_secrets;
95pub(crate) mod processing;
96pub(crate) mod proposal;
97pub(crate) mod proposal_store;
98pub(crate) mod reinit;
99pub(crate) mod staged_commit;
100
101#[cfg(feature = "extensions-draft")]
102pub(crate) mod app_ephemeral;
103
104#[cfg(feature = "targeted-messages-draft")]
105mod targeted_messages;
106
107#[cfg(feature = "virtual-clients-draft")]
108mod vc_application_secret;
109
110// Tests
111#[cfg(test)]
112pub(crate) mod tests_and_kats;
113
114#[derive(Debug)]
115pub(crate) struct CreateCommitResult {
116    pub(crate) commit: AuthenticatedContent,
117    pub(crate) welcome_option: Option<Welcome>,
118    pub(crate) staged_commit: StagedCommit,
119    pub(crate) group_info: Option<GroupInfo>,
120}
121
122/// A member in the group is identified by this [`Member`] struct.
123#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
124pub struct Member {
125    /// The member's leaf index in the ratchet tree.
126    pub index: LeafNodeIndex,
127    /// The member's credential.
128    pub credential: Credential,
129    /// The member's public HPHKE encryption key.
130    pub encryption_key: Vec<u8>,
131    /// The member's public signature key.
132    pub signature_key: Vec<u8>,
133}
134
135impl Member {
136    /// Create new member.
137    pub fn new(
138        index: LeafNodeIndex,
139        encryption_key: Vec<u8>,
140        signature_key: Vec<u8>,
141        credential: Credential,
142    ) -> Self {
143        Self {
144            index,
145            encryption_key,
146            signature_key,
147            credential,
148        }
149    }
150}
151
152/// Pending Commit state. Differentiates between Commits issued by group members
153/// and External Commits.
154#[derive(Debug, Serialize, Deserialize)]
155#[cfg_attr(any(test, feature = "test-utils"), derive(Clone, PartialEq))]
156pub enum PendingCommitState {
157    /// Commit from a group member
158    Member(StagedCommit),
159    /// Commit from an external joiner
160    External(StagedCommit),
161}
162
163impl PendingCommitState {
164    /// Returns a reference to the [`StagedCommit`] contained in the
165    /// [`PendingCommitState`] enum.
166    pub(crate) fn staged_commit(&self) -> &StagedCommit {
167        match self {
168            PendingCommitState::Member(pc) => pc,
169            PendingCommitState::External(pc) => pc,
170        }
171    }
172}
173
174impl From<PendingCommitState> for StagedCommit {
175    fn from(pcs: PendingCommitState) -> Self {
176        match pcs {
177            PendingCommitState::Member(pc) => pc,
178            PendingCommitState::External(pc) => pc,
179        }
180    }
181}
182
183/// [`MlsGroupState`] determines the state of an [`MlsGroup`]. The different
184/// states and their transitions are as follows:
185///
186/// * [`MlsGroupState::Operational`]: This is the main state of the group, which
187///   allows access to all of its functionality, (except merging pending commits,
188///   see the [`MlsGroupState::PendingCommit`] for more information) and it's the
189///   state the group starts in (except when created via
190///   [`MlsGroup::external_commit_builder()`], see the functions documentation for
191///   more information). From this `Operational`, the group state can either
192///   transition to [`MlsGroupState::Inactive`], when it processes a commit that
193///   removes this client from the group, or to [`MlsGroupState::PendingCommit`],
194///   when this client creates a commit.
195///
196/// * [`MlsGroupState::Inactive`]: A group can enter this state from any other
197///   state when it processes a commit that removes this client from the group.
198///   This is a terminal state that the group can not exit from. If the clients
199///   wants to re-join the group, it can either be added by a group member or it
200///   can join via external commit.
201///
202/// * [`MlsGroupState::PendingCommit`]: This state is split into two possible
203///   sub-states, one for each Commit type:
204///   [`PendingCommitState::Member`] and [`PendingCommitState::External`]:
205///
206///   * If the client creates a commit for this group, the `PendingCommit` state
207///     is entered with [`PendingCommitState::Member`] and with the [`StagedCommit`] as
208///     additional state variable. In this state, it can perform the same
209///     operations as in the [`MlsGroupState::Operational`], except that it cannot
210///     create proposals or commits. However, it can merge or clear the stored
211///     [`StagedCommit`], where both actions result in a transition to the
212///     [`MlsGroupState::Operational`]. Additionally, if a commit from another
213///     group member is processed, the own pending commit is also cleared and
214///     either the `Inactive` state is entered (if this client was removed from
215///     the group as part of the processed commit), or the `Operational` state is
216///     entered.
217///
218///   * A group can enter the [`PendingCommitState::External`] sub-state only as
219///     the initial state when the group is created via
220///     [`MlsGroup::external_commit_builder()`]. In contrast to the
221///     [`PendingCommitState::Member`] `PendingCommit` state, the only possible
222///     functionality that can be used is the [`MlsGroup::merge_pending_commit()`]
223///     function, which merges the pending external commit and transitions the
224///     state to [`MlsGroupState::PendingCommit`]. For more information on the
225///     external commit process, see [`MlsGroup::external_commit_builder()`] or
226///     Section 11.2.1 of the MLS specification.
227#[derive(Debug, Serialize, Deserialize)]
228#[cfg_attr(any(test, feature = "test-utils"), derive(Clone, PartialEq))]
229pub enum MlsGroupState {
230    /// There is currently a pending Commit that hasn't been merged yet.
231    PendingCommit(Box<PendingCommitState>),
232    /// The group state is in an opertaional state, where new messages and Commits can be created.
233    Operational,
234    /// The group is inactive because the member has been removed.
235    Inactive,
236}
237
238/// A `MlsGroup` represents an MLS group with a high-level API. The API exposes
239/// high level functions to manage a group by adding/removing members, get the
240/// current member list, etc.
241///
242/// The API is modeled such that it can serve as a direct interface to the
243/// Delivery Service. Functions that modify the public state of the group will
244/// return a `Vec<MLSMessageOut>` that can be sent to the Delivery Service
245/// directly. Conversely, incoming messages from the Delivery Service can be fed
246/// into [process_message()](`MlsGroup::process_message()`).
247///
248/// An `MlsGroup` has an internal queue of pending proposals that builds up as
249/// new messages are processed. When creating proposals, those messages are not
250/// automatically appended to this queue, instead they have to be processed
251/// again through [process_message()](`MlsGroup::process_message()`). This
252/// allows the Delivery Service to reject them (e.g. if they reference the wrong
253/// epoch).
254///
255/// If incoming messages or applied operations are semantically or syntactically
256/// incorrect, an error event will be returned with a corresponding error
257/// message and the state of the group will remain unchanged.
258///
259/// An `MlsGroup` has an internal state variable determining if it is active or
260/// inactive, as well as if it has a pending commit. See [`MlsGroupState`] for
261/// more information.
262#[derive(Debug)]
263#[cfg_attr(feature = "migration-import", derive(serde::Deserialize))]
264#[cfg_attr(
265    all(feature = "migration-import", feature = "test-utils"),
266    derive(serde::Serialize)
267)]
268#[cfg_attr(feature = "test-utils", derive(Clone, PartialEq))]
269pub struct MlsGroup {
270    /// The group configuration. See [`MlsGroupJoinConfig`] for more information.
271    mls_group_config: MlsGroupJoinConfig,
272    /// The public state of the group.
273    public_group: PublicGroup,
274    /// Epoch-specific secrets of the group.
275    group_epoch_secrets: GroupEpochSecrets,
276    /// The own leaf index in the ratchet tree.
277    own_leaf_index: LeafNodeIndex,
278    /// A [`MessageSecretsStore`] that stores message secrets.
279    /// By default this store has the length of 1, i.e. only the [`MessageSecrets`]
280    /// of the current epoch is kept.
281    /// If more secrets from past epochs should be kept in order to be
282    /// able to decrypt application messages from previous epochs, the size of
283    /// the store must be increased through [`max_past_epochs()`].
284    message_secrets_store: MessageSecretsStore,
285    // Resumption psk store. This is where the resumption psks are kept in a rollover list.
286    resumption_psk_store: ResumptionPskStore,
287    // Own [`LeafNode`]s that were created for update proposals and that
288    // are needed in case an update proposal is committed by another group
289    // member. The vector is emptied after every epoch change.
290    own_leaf_nodes: Vec<LeafNode>,
291    // Additional authenticated data (AAD) for the next outgoing message. This
292    // is ephemeral and will be reset by every API call that successfully
293    // returns an [`MlsMessageOut`].
294    aad: Vec<u8>,
295    // Safe AAD items to attach to the next outgoing message. Ephemeral, reset
296    // alongside `aad`. Only consulted when the group's GroupContext requires
297    // Safe AAD framing.
298    #[cfg(feature = "extensions-draft")]
299    // Migration bridge:
300    // absent from older serializations, so default it to empty on import — it is
301    // ephemeral, so a freshly migrated group has nothing staged anyway.
302    #[cfg_attr(
303        feature = "migration-import",
304        serde(default = "crate::framing::SafeAad::empty")
305    )]
306    safe_aad: SafeAad,
307    // A variable that indicates the state of the group. See [`MlsGroupState`]
308    // for more information.
309    group_state: MlsGroupState,
310    /// The state of the Application Exporter. See the MLS Extensions Draft 08
311    /// for more information. This is `None` if an old OpenMLS group state was
312    /// loaded and has not yet merged a commit.
313    #[cfg(feature = "extensions-draft")]
314    // Migration bridge (see the note on the struct): absent when migrating in a
315    // group from a version that did not have `extensions-draft`, so default it to
316    // `None` on import — it initializes on the next merged commit.
317    #[cfg_attr(feature = "migration-import", serde(default))]
318    application_export_tree: Option<ApplicationExportTree>,
319    /// Whether this group is an emulation group of a virtual client. Not
320    /// persisted on its own: [`MlsGroup::load`] recovers it from the presence of
321    /// the group's derivation-epoch registration record.
322    #[cfg(feature = "virtual-clients-draft")]
323    // Migration bridge (see the note on the struct): a group migrated in from a
324    // version without `virtual-clients-draft` is never an emulation group.
325    #[cfg_attr(feature = "migration-import", serde(default))]
326    emulation_group: bool,
327}
328
329impl MlsGroup {
330    // === Configuration ===
331
332    /// Returns the configuration.
333    pub fn configuration(&self) -> &MlsGroupJoinConfig {
334        &self.mls_group_config
335    }
336
337    /// Sets the configuration.
338    pub fn set_configuration<Storage: StorageProvider>(
339        &mut self,
340        storage: &Storage,
341        mls_group_config: &MlsGroupJoinConfig,
342    ) -> Result<(), Storage::Error> {
343        let policy_changed = self.mls_group_config.past_epoch_deletion_policy()
344            != mls_group_config.past_epoch_deletion_policy();
345        #[cfg(feature = "virtual-clients-draft")]
346        let retention_changed = self.mls_group_config.vc_derivation_epoch_retention_policy()
347            != mls_group_config.vc_derivation_epoch_retention_policy();
348
349        self.mls_group_config = mls_group_config.clone();
350        storage.write_mls_join_config(self.group_id(), mls_group_config)?;
351
352        if policy_changed {
353            // Resize the store to adhere to the new policy.
354            self.resize_message_secrets_store(mls_group_config.past_epoch_deletion_policy());
355            storage.write_message_secrets(self.group_id(), &self.message_secrets_store)?;
356        }
357
358        #[cfg(feature = "virtual-clients-draft")]
359        if retention_changed {
360            self.apply_vc_derivation_epoch_retention(storage)?;
361        }
362
363        Ok(())
364    }
365
366    /// Sets the additional authenticated data (AAD) for the next outgoing
367    /// message. This is ephemeral and will be reset by every API call that
368    /// successfully returns an [`MlsMessageOut`].
369    pub fn set_aad(&mut self, aad: Vec<u8>) {
370        self.aad = aad;
371    }
372
373    /// Returns the additional authenticated data (AAD) for the next outgoing
374    /// message.
375    pub fn aad(&self) -> &[u8] {
376        &self.aad
377    }
378
379    /// Stage Safe AAD items for the next outgoing message. Items must be
380    /// sorted by [`ComponentId`] in strictly-increasing order and contain no
381    /// duplicates; otherwise the call fails and the previously staged items
382    /// are left untouched.
383    ///
384    /// Ephemeral, like [`Self::set_aad`]: cleared whenever an outgoing message
385    /// is produced.
386    ///
387    /// [`ComponentId`]: crate::component::ComponentId
388    #[cfg(feature = "extensions-draft")]
389    pub fn set_safe_aad(&mut self, items: Vec<SafeAadItem>) -> Result<(), SafeAadError> {
390        self.safe_aad = SafeAad::from_items(items)?;
391        Ok(())
392    }
393
394    /// Returns the currently staged Safe AAD items for the next outgoing
395    /// message.
396    #[cfg(feature = "extensions-draft")]
397    pub fn safe_aad_items(&self) -> &[SafeAadItem] {
398        self.safe_aad.items()
399    }
400
401    // === Advanced functions ===
402
403    /// Returns the group's ciphersuite.
404    pub fn ciphersuite(&self) -> Ciphersuite {
405        self.public_group.ciphersuite()
406    }
407
408    /// Get confirmation tag.
409    pub fn confirmation_tag(&self) -> &ConfirmationTag {
410        self.public_group.confirmation_tag()
411    }
412
413    /// Returns whether the own client is still a member of the group or if it
414    /// was already evicted
415    pub fn is_active(&self) -> bool {
416        !matches!(self.group_state, MlsGroupState::Inactive)
417    }
418
419    /// Returns own credential. If the group is inactive, it returns a
420    /// `UseAfterEviction` error.
421    pub fn credential(&self) -> Result<&Credential, MlsGroupStateError> {
422        if !self.is_active() {
423            return Err(MlsGroupStateError::UseAfterEviction);
424        }
425        self.public_group
426            .leaf(self.own_leaf_index())
427            .map(|node| node.credential())
428            .ok_or_else(|| LibraryError::custom("Own leaf node missing").into())
429    }
430
431    /// Returns the leaf index of the client in the tree owning this group.
432    pub fn own_leaf_index(&self) -> LeafNodeIndex {
433        self.own_leaf_index
434    }
435
436    /// Returns the leaf node of the client in the tree owning this group.
437    pub fn own_leaf_node(&self) -> Option<&LeafNode> {
438        self.public_group().leaf(self.own_leaf_index())
439    }
440
441    /// Returns the group ID.
442    pub fn group_id(&self) -> &GroupId {
443        self.public_group.group_id()
444    }
445
446    /// Returns the epoch.
447    pub fn epoch(&self) -> GroupEpoch {
448        self.public_group.group_context().epoch()
449    }
450
451    /// Returns an `Iterator` over pending proposals.
452    pub fn pending_proposals(&self) -> impl Iterator<Item = &QueuedProposal> {
453        self.proposal_store().proposals()
454    }
455
456    /// Returns the current tree state of the group, in the form of a [`TreeSync`].
457    pub fn treesync(&self) -> &TreeSync {
458        self.public_group.treesync()
459    }
460
461    /// Returns a reference to the [`StagedCommit`] of the most recently created
462    /// commit. If there was no commit created in this epoch, either because
463    /// this commit or another commit was merged, it returns `None`.
464    pub fn pending_commit(&self) -> Option<&StagedCommit> {
465        match self.group_state {
466            MlsGroupState::PendingCommit(ref pending_commit_state) => {
467                Some(pending_commit_state.staged_commit())
468            }
469            MlsGroupState::Operational => None,
470            MlsGroupState::Inactive => None,
471        }
472    }
473
474    /// Sets the `group_state` to [`MlsGroupState::Operational`], thus clearing
475    /// any potentially pending commits.
476    ///
477    /// Note that this has no effect if the group was created through an external commit and
478    /// the resulting external commit has not been merged yet. For more
479    /// information, see [`MlsGroup::external_commit_builder()`].
480    ///
481    /// Use with caution! This function should only be used if it is clear that
482    /// the pending commit will not be used in the group. In particular, if a
483    /// pending commit is later accepted by the group, this client will lack the
484    /// key material to encrypt or decrypt group messages.
485    pub fn clear_pending_commit<Storage: StorageProvider>(
486        &mut self,
487        storage: &Storage,
488    ) -> Result<(), Storage::Error> {
489        match self.group_state {
490            MlsGroupState::PendingCommit(ref pending_commit_state) => {
491                if let PendingCommitState::Member(_) = **pending_commit_state {
492                    self.group_state = MlsGroupState::Operational;
493                    storage.write_group_state(self.group_id(), &self.group_state)
494                } else {
495                    Ok(())
496                }
497            }
498            MlsGroupState::Operational | MlsGroupState::Inactive => Ok(()),
499        }
500    }
501
502    /// Clear the pending proposals, if the proposal store is not empty.
503    ///
504    /// Warning: Once the pending proposals are cleared it will be impossible to process
505    /// a Commit message that references those proposals. Only use this
506    /// function as a last resort, e.g. when a call to
507    /// `MlsGroup::commit_to_pending_proposals` fails.
508    pub fn clear_pending_proposals<Storage: StorageProvider>(
509        &mut self,
510        storage: &Storage,
511    ) -> Result<(), Storage::Error> {
512        // If the proposal store is not empty...
513        if !self.proposal_store().is_empty() {
514            // Empty the proposal store
515            self.proposal_store_mut().empty();
516
517            // Clear proposals in storage
518            storage.clear_proposal_queue::<GroupId, ProposalRef>(self.group_id())?;
519        }
520
521        Ok(())
522    }
523
524    /// Get a reference to the group context [`Extensions`] of this [`MlsGroup`].
525    pub fn extensions(&self) -> &Extensions<GroupContext> {
526        self.public_group().group_context().extensions()
527    }
528
529    /// Returns the index of the sender of a staged, external commit.
530    pub fn ext_commit_sender_index(
531        &self,
532        commit: &StagedCommit,
533    ) -> Result<LeafNodeIndex, LibraryError> {
534        self.public_group().ext_commit_sender_index(commit)
535    }
536
537    // === Storage Methods ===
538
539    /// Loads the state of the group with given id from persisted state.
540    pub fn load<Storage: crate::storage::StorageProvider>(
541        storage: &Storage,
542        group_id: &GroupId,
543    ) -> Result<Option<MlsGroup>, Storage::Error> {
544        let public_group = PublicGroup::load(storage, group_id)?;
545        let group_epoch_secrets = storage.group_epoch_secrets(group_id)?;
546        let own_leaf_index = storage.own_leaf_index(group_id)?;
547        let message_secrets_store = storage.message_secrets(group_id)?;
548        let resumption_psk_store = storage.resumption_psk_store(group_id)?;
549        let mls_group_config = storage.mls_group_join_config(group_id)?;
550        let own_leaf_nodes = storage.own_leaf_nodes(group_id)?;
551        let group_state = storage.group_state(group_id)?;
552        #[cfg(feature = "extensions-draft")]
553        let application_export_tree = storage.application_export_tree(group_id)?;
554        // A group has a derivation-epoch registration record for exactly as long
555        // as it is an emulation group. The record is written by the initial
556        // registration at creation or Welcome join and removed by `delete`.
557        #[cfg(feature = "virtual-clients-draft")]
558        let emulation_group =
559            crate::components::vc_derivation_info::newest_vc_derivation_epoch(storage, group_id)?
560                .is_some();
561
562        let build = || -> Option<Self> {
563            Some(Self {
564                public_group: public_group?,
565                group_epoch_secrets: group_epoch_secrets?,
566                own_leaf_index: own_leaf_index?,
567                message_secrets_store: message_secrets_store?,
568                resumption_psk_store: resumption_psk_store?,
569                mls_group_config: mls_group_config?,
570                own_leaf_nodes,
571                aad: vec![],
572                #[cfg(feature = "extensions-draft")]
573                safe_aad: SafeAad::empty(),
574                group_state: group_state?,
575                #[cfg(feature = "extensions-draft")]
576                application_export_tree,
577                #[cfg(feature = "virtual-clients-draft")]
578                emulation_group,
579            })
580        };
581
582        Ok(build())
583    }
584
585    /// Remove the persisted state of this group from storage. Note that
586    /// signature key material is not managed by OpenMLS and has to be removed
587    /// from the storage provider separately (if desired).
588    pub fn delete<Storage: crate::storage::StorageProvider>(
589        &mut self,
590        storage: &Storage,
591    ) -> Result<(), Storage::Error> {
592        PublicGroup::delete(storage, self.group_id())?;
593        storage.delete_own_leaf_index(self.group_id())?;
594        storage.delete_group_epoch_secrets(self.group_id())?;
595        storage.delete_message_secrets(self.group_id())?;
596        storage.delete_all_resumption_psk_secrets(self.group_id())?;
597        storage.delete_group_config(self.group_id())?;
598        storage.delete_own_leaf_nodes(self.group_id())?;
599        storage.delete_group_state(self.group_id())?;
600        storage.clear_proposal_queue::<GroupId, ProposalRef>(self.group_id())?;
601
602        #[cfg(feature = "extensions-draft")]
603        storage.delete_application_export_tree::<_, ApplicationExportTree>(self.group_id())?;
604
605        // The derivation-epoch state itself is keyed on the epoch rather than on
606        // this group, so it only goes if this group held the last reference.
607        #[cfg(feature = "virtual-clients-draft")]
608        self.drop_all_vc_derivation_epoch_references(storage)?;
609
610        self.proposal_store_mut().empty();
611        storage.delete_encryption_epoch_key_pairs(
612            self.group_id(),
613            &self.epoch(),
614            self.own_leaf_index().u32(),
615        )?;
616
617        Ok(())
618    }
619
620    // === Extensions ===
621
622    /// Exports the Ratchet Tree.
623    pub fn export_ratchet_tree(&self) -> RatchetTree {
624        self.public_group().export_ratchet_tree()
625    }
626}
627
628/// Error resolving the [`VcDerivationEpochState`] bound to a group at a given
629/// epoch via [`MlsGroup::vc_derivation_state_at_epoch`]. Callers map it to
630/// their own error type.
631///
632/// [`VcDerivationEpochState`]: crate::components::vc_derivation_info::VcDerivationEpochState
633#[cfg(feature = "virtual-clients-draft")]
634#[derive(thiserror::Error, Debug, PartialEq, Clone)]
635pub(crate) enum VcDerivationStateError<StorageError> {
636    /// Reading the binding or the derivation-epoch state from storage failed.
637    #[error("Error reading the binding or derivation-epoch state from storage: {0}")]
638    Storage(StorageError),
639    /// The group is bound to a derivation epoch, but its state is missing.
640    #[error("The group is bound to a derivation epoch, but its state is missing.")]
641    MissingDerivationEpochState,
642}
643
644// Crate-public functions
645impl MlsGroup {
646    /// Get the required capabilities extension of this group.
647    pub(crate) fn required_capabilities(&self) -> Option<&RequiredCapabilitiesExtension> {
648        self.public_group.required_capabilities()
649    }
650
651    /// Get a reference to the group epoch secrets from the group
652    pub(crate) fn group_epoch_secrets(&self) -> &GroupEpochSecrets {
653        &self.group_epoch_secrets
654    }
655
656    /// Get a reference to the message secrets from a group
657    pub(crate) fn message_secrets(&self) -> &MessageSecrets {
658        self.message_secrets_store.message_secrets()
659    }
660
661    /// Sets the size of the [`MessageSecretsStore`], i.e. the number of past
662    /// epochs to keep.
663    /// This allows application messages from previous epochs to be decrypted.
664    pub(crate) fn resize_message_secrets_store(&mut self, policy: &PastEpochDeletionPolicy) {
665        self.message_secrets_store.resize(policy);
666    }
667
668    /// Get the past epoch secret deletion policy for the group.
669    pub fn past_epoch_deletion_policy(&self) -> &PastEpochDeletionPolicy {
670        self.mls_group_config.past_epoch_deletion_policy()
671    }
672
673    /// Set the past epoch secret deletion policy for the group.
674    pub fn set_past_epoch_deletion_policy<Provider: OpenMlsProvider>(
675        &mut self,
676        provider: &Provider,
677        policy: PastEpochDeletionPolicy,
678    ) -> Result<(), SetPastEpochDeletionPolicyError<Provider::StorageError>> {
679        // resize the store
680        self.resize_message_secrets_store(&policy);
681
682        // set the policy on the join config
683        self.mls_group_config.past_epoch_deletion_policy = policy;
684
685        // persist the join config
686        provider
687            .storage()
688            .write_mls_join_config(self.group_id(), &self.mls_group_config)?;
689
690        // update the message secrets store in storage
691        provider
692            .storage()
693            .write_message_secrets(self.group_id(), &self.message_secrets_store)?;
694
695        Ok(())
696    }
697
698    /// Get the derivation-epoch retention policy for the group.
699    #[cfg(feature = "virtual-clients-draft")]
700    pub fn vc_derivation_epoch_retention_policy(&self) -> &VcDerivationEpochRetentionPolicy {
701        self.mls_group_config.vc_derivation_epoch_retention_policy()
702    }
703
704    /// Set the derivation-epoch retention policy for the group and apply it
705    /// right away. See [`VcDerivationEpochRetentionPolicy`].
706    #[cfg(feature = "virtual-clients-draft")]
707    pub fn set_vc_derivation_epoch_retention_policy<Provider: OpenMlsProvider>(
708        &mut self,
709        provider: &Provider,
710        policy: VcDerivationEpochRetentionPolicy,
711    ) -> Result<(), Provider::StorageError> {
712        self.mls_group_config.vc_derivation_epoch_retention_policy = policy;
713        provider
714            .storage()
715            .write_mls_join_config(self.group_id(), &self.mls_group_config)?;
716        self.apply_vc_derivation_epoch_retention(provider.storage())
717    }
718
719    /// Get the message secrets. Either from the secrets store or from the group.
720    pub(crate) fn message_secrets_for_epoch_mut(
721        &mut self,
722        epoch: GroupEpoch,
723    ) -> Result<&mut MessageSecrets, SecretTreeError> {
724        if epoch < self.context().epoch() {
725            self.message_secrets_store
726                .secrets_for_epoch_mut(epoch)
727                .ok_or(SecretTreeError::TooDistantInThePast)
728        } else {
729            Ok(self.message_secrets_store.message_secrets_mut())
730        }
731    }
732
733    /// Get the message secrets. Either from the secrets store or from the group.
734    pub(crate) fn message_secrets_for_epoch(
735        &self,
736        epoch: GroupEpoch,
737    ) -> Result<&MessageSecrets, SecretTreeError> {
738        if epoch < self.context().epoch() {
739            self.message_secrets_store
740                .secrets_for_epoch(epoch)
741                .ok_or(SecretTreeError::TooDistantInThePast)
742        } else {
743            Ok(self.message_secrets_store.message_secrets())
744        }
745    }
746
747    /// Get the message secrets and leaves for the given epoch. Either from the
748    /// secrets store or from the group.
749    ///
750    /// Note that the leaves vector is empty for message secrets of the current
751    /// epoch. The caller can use treesync in this case.
752    pub(crate) fn message_secrets_and_leaves(
753        &self,
754        epoch: GroupEpoch,
755    ) -> Result<(&MessageSecrets, &[Member]), SecretTreeError> {
756        if epoch < self.context().epoch() {
757            self.message_secrets_store
758                .secrets_and_leaves_for_epoch(epoch)
759                .ok_or(SecretTreeError::TooDistantInThePast)
760        } else {
761            // No need for leaves here. The tree of the current epoch is
762            // available to the caller.
763            Ok((self.message_secrets_store.message_secrets(), &[]))
764        }
765    }
766
767    /// Create a new group context extension proposal
768    pub(crate) fn create_group_context_ext_proposal<Provider: OpenMlsProvider>(
769        &self,
770        framing_parameters: FramingParameters,
771        extensions: Extensions<GroupContext>,
772        signer: &impl Signer,
773    ) -> Result<AuthenticatedContent, CreateGroupContextExtProposalError<Provider::StorageError>>
774    {
775        // Ensure that the group supports all the extensions that are wanted.
776        let required_extension = extensions
777            .iter()
778            .find(|extension| extension.extension_type() == ExtensionType::RequiredCapabilities);
779        if let Some(required_extension) = required_extension {
780            let required_capabilities = required_extension.as_required_capabilities_extension()?;
781            // Ensure we support all the capabilities.
782            self.own_leaf_node()
783                .ok_or_else(|| LibraryError::custom("Tree has no own leaf."))?
784                .capabilities()
785                .supports_required_capabilities(required_capabilities)?;
786
787            // Ensure that all other leaf nodes support all the required
788            // extensions as well.
789            self.public_group()
790                .check_extension_support(required_capabilities.extension_types())?;
791        }
792        let proposal = GroupContextExtensionProposal::new(extensions);
793        let proposal = Proposal::GroupContextExtensions(Box::new(proposal));
794        AuthenticatedContent::member_proposal(
795            framing_parameters,
796            self.own_leaf_index(),
797            proposal,
798            self.context(),
799            signer,
800        )
801        .map_err(|e| e.into())
802    }
803
804    /// Load the [`VcDerivationEpochState`] this group is bound to at `epoch`,
805    /// if any. Returns `None` when the group has no virtual-clients binding for
806    /// that epoch. The binding is resolved at the epoch a message was sent in,
807    /// so a delayed message from a past epoch deprotects with the state that
808    /// was bound then, not the latest one.
809    ///
810    /// [`VcDerivationEpochState`]: crate::components::vc_derivation_info::VcDerivationEpochState
811    #[cfg(feature = "virtual-clients-draft")]
812    pub(crate) fn vc_derivation_state_at_epoch<Storage: StorageProvider>(
813        &self,
814        storage: &Storage,
815        epoch: GroupEpoch,
816    ) -> Result<
817        Option<crate::components::vc_derivation_info::VcDerivationEpochState>,
818        VcDerivationStateError<Storage::Error>,
819    > {
820        let binding: Option<crate::components::vc_derivation_info::VcEmulationBinding> = storage
821            .vc_emulation_binding(self.group_id(), &epoch)
822            .map_err(VcDerivationStateError::Storage)?;
823        let Some(epoch_id) = binding.map(|binding| binding.into_epoch_id()) else {
824            return Ok(None);
825        };
826        let state = storage
827            .vc_derivation_epoch_state(&epoch_id)
828            .map_err(VcDerivationStateError::Storage)?
829            .ok_or_else(|| {
830                log::error!("vc: group is bound to derivation epoch, but state is missing");
831                VcDerivationStateError::MissingDerivationEpochState
832            })?;
833        Ok(Some(state))
834    }
835
836    /// Returns the [`EpochId`] of the derivation epoch this group is bound to
837    /// at `epoch`, or `None` if the group has no virtual-clients binding for
838    /// that epoch.
839    ///
840    /// [`EpochId`]: crate::components::vc_derivation_info::EpochId
841    #[cfg(feature = "virtual-clients-draft")]
842    pub fn vc_derivation_epoch_at<Storage: StorageProvider>(
843        &self,
844        storage: &Storage,
845        epoch: GroupEpoch,
846    ) -> Result<Option<crate::components::vc_derivation_info::EpochId>, Storage::Error> {
847        let binding: Option<crate::components::vc_derivation_info::VcEmulationBinding> =
848            storage.vc_emulation_binding(self.group_id(), &epoch)?;
849        Ok(binding.map(|binding| binding.into_epoch_id()))
850    }
851
852    /// Returns whether this group is an emulation group of a virtual client.
853    ///
854    /// The flag is set when the application creates the group as an emulation
855    /// group or joins one, and it is restored from storage when the group is
856    /// loaded. See [`MlsGroupCreateConfigBuilder::emulation_group`].
857    ///
858    /// [`MlsGroupCreateConfigBuilder::emulation_group`]: crate::group::MlsGroupCreateConfigBuilder::emulation_group
859    #[cfg(feature = "virtual-clients-draft")]
860    pub fn is_emulation_group(&self) -> bool {
861        self.emulation_group
862    }
863
864    /// Returns the [`EpochId`] of the newest derivation epoch of this emulation
865    /// group, or `None` if none was registered yet.
866    ///
867    /// All virtual-client operations resolve to this derivation epoch. It is
868    /// sourced from the newest group epoch that was a derivation epoch, which
869    /// may be older than the group's current epoch: only commits that change
870    /// membership or that carry a `new_derivation_epoch` action create one.
871    ///
872    /// The sender-side operation entry points take the emulation group and
873    /// resolve the epoch themselves, so this getter is for inspection only.
874    ///
875    /// Returns `None` for groups that are not emulation groups.
876    ///
877    /// [`EpochId`]: crate::components::vc_derivation_info::EpochId
878    #[cfg(feature = "virtual-clients-draft")]
879    pub fn newest_vc_derivation_epoch<Storage: StorageProvider>(
880        &self,
881        storage: &Storage,
882    ) -> Result<Option<crate::components::vc_derivation_info::EpochId>, Storage::Error> {
883        crate::components::vc_derivation_info::newest_vc_derivation_epoch(storage, self.group_id())
884    }
885
886    /// Delete the derivation epochs of this emulation group that `deletion`
887    /// selects, unless something else still references them. See
888    /// [`VcDerivationEpochDeletion`] and [`VcDerivationEpochDeletionResult`].
889    ///
890    /// Performs several storage writes, so wrap the call in a storage
891    /// transaction.
892    #[cfg(feature = "virtual-clients-draft")]
893    pub fn delete_vc_derivation_epochs<Provider: OpenMlsProvider>(
894        &self,
895        provider: &Provider,
896        deletion: VcDerivationEpochDeletion,
897    ) -> Result<VcDerivationEpochDeletionResult, Provider::StorageError> {
898        use crate::components::vc_derivation_info::VcDerivationEpochLog;
899
900        let storage = provider.storage();
901        let mut log = VcDerivationEpochLog::load(storage, self.group_id())?;
902        if log.is_empty() {
903            return Ok(VcDerivationEpochDeletionResult::default());
904        }
905        let cutoff = match deletion.time {
906            VcDerivationEpochDeletionTime::BeforeTimestamp(timestamp) => timestamp,
907            // A duration longer than the time since the epoch leaves nothing
908            // superseded before the cutoff, which is what an unreachably long
909            // retention window should mean.
910            VcDerivationEpochDeletionTime::OlderThanDuration(duration) => SystemTime::now()
911                .checked_sub(duration)
912                .unwrap_or(SystemTime::UNIX_EPOCH),
913        };
914        let mut dropped = log.drop_superseded_before(cutoff);
915        if let Some(max_epochs) = deletion.max_epochs {
916            dropped.extend(log.shrink_to(max_epochs));
917        }
918        self.release_vc_derivation_epochs(storage, dropped)
919    }
920
921    /// Shrink this group's derivation-epoch log to its retention policy and
922    /// release the epochs that dropped out.
923    #[cfg(feature = "virtual-clients-draft")]
924    fn apply_vc_derivation_epoch_retention<Storage: StorageProvider>(
925        &self,
926        storage: &Storage,
927    ) -> Result<(), Storage::Error> {
928        use crate::components::vc_derivation_info::VcDerivationEpochLog;
929
930        let mut log = VcDerivationEpochLog::load(storage, self.group_id())?;
931        let max_epochs = self
932            .mls_group_config
933            .vc_derivation_epoch_retention_policy()
934            .max_epochs()
935            .unwrap_or(usize::MAX);
936        let dropped = log.shrink_to(max_epochs);
937        if dropped.is_empty() {
938            return Ok(());
939        }
940        self.release_vc_derivation_epochs(storage, dropped)?;
941        Ok(())
942    }
943
944    /// Delete this group's log entries for the `dropped` epochs and sweep,
945    /// reporting which of them were deleted and which were kept. Epochs whose
946    /// state was already absent appear in neither list.
947    #[cfg(feature = "virtual-clients-draft")]
948    fn release_vc_derivation_epochs<Storage: StorageProvider>(
949        &self,
950        storage: &Storage,
951        dropped: Vec<crate::components::vc_derivation_info::EpochId>,
952    ) -> Result<VcDerivationEpochDeletionResult, Storage::Error> {
953        use crate::components::vc_derivation_info::{EpochId, VcDerivationEpochState};
954
955        storage.delete_vc_derivation_epoch_log_entries(self.group_id(), &dropped)?;
956        let swept: Vec<EpochId> = storage.delete_unreferenced_vc_derivation_epoch_states()?;
957        let mut result = VcDerivationEpochDeletionResult::default();
958        for epoch_id in dropped {
959            if swept.contains(&epoch_id) {
960                result.deleted.push(epoch_id);
961                continue;
962            }
963            // The sweep reports only what it deleted, so an epoch it left
964            // alone is either still referenced or was already gone.
965            let state: Option<VcDerivationEpochState> =
966                storage.vc_derivation_epoch_state(&epoch_id)?;
967            if state.is_some() {
968                result.kept.push(epoch_id);
969            }
970        }
971        Ok(result)
972    }
973
974    /// Drop every reference this group holds to a derivation epoch, both its
975    /// emulation bindings and its own derivation-epoch log, then sweep the
976    /// epochs that are now unreferenced.
977    #[cfg(feature = "virtual-clients-draft")]
978    fn drop_all_vc_derivation_epoch_references<Storage: StorageProvider>(
979        &self,
980        storage: &Storage,
981    ) -> Result<(), Storage::Error> {
982        use crate::components::vc_derivation_info::EpochId;
983
984        storage.delete_all_vc_emulation_bindings(self.group_id())?;
985        storage.delete_vc_derivation_epoch_log(self.group_id())?;
986        storage.delete_unreferenced_vc_derivation_epoch_states::<EpochId>()?;
987        Ok(())
988    }
989
990    // Encrypt an AuthenticatedContent into an PrivateMessage
991    pub(crate) fn encrypt<Provider: OpenMlsProvider>(
992        &mut self,
993        public_message: AuthenticatedContent,
994        provider: &Provider,
995    ) -> Result<EncryptionOutput, MessageEncryptionError<Provider::StorageError>> {
996        let padding_size = self.configuration().padding_size();
997
998        // If this group is bound to a derivation epoch at its current epoch,
999        // load the state so the framing layer can derive a deterministic
1000        // reuse guard.
1001        #[cfg(feature = "virtual-clients-draft")]
1002        let derivation_state = self
1003            .vc_derivation_state_at_epoch(provider.storage(), self.epoch())
1004            .map_err(|e| match e {
1005                VcDerivationStateError::Storage(e) => MessageEncryptionError::StorageError(e),
1006                VcDerivationStateError::MissingDerivationEpochState => {
1007                    MessageEncryptionError::VirtualClientsError(
1008                        crate::components::vc_derivation_info::VirtualClientsError::MissingDerivationEpochState,
1009                    )
1010                }
1011            })?;
1012        #[cfg(feature = "virtual-clients-draft")]
1013        let emulator_ctx: Option<crate::framing::EmulatorReuseGuardCtx<'_>> = derivation_state
1014            .as_ref()
1015            .map(|state| state.reuse_guard_inputs());
1016
1017        let msg = PrivateMessage::try_from_authenticated_content(
1018            provider.crypto(),
1019            provider.rand(),
1020            &public_message,
1021            self.ciphersuite(),
1022            self.message_secrets_store.message_secrets_mut(),
1023            padding_size,
1024            #[cfg(feature = "virtual-clients-draft")]
1025            emulator_ctx.as_ref(),
1026        )?;
1027
1028        // When the group is bound to a derivation epoch, derive the generation
1029        // ID the application hands to the DS to detect generation collisions
1030        // between siblings. Application content draws it from the application
1031        // ratchet, proposals and commits from the handshake ratchet.
1032        #[cfg(feature = "virtual-clients-draft")]
1033        let msg = {
1034            use crate::components::vc_derivation_info::RatchetType;
1035            let mut msg = msg;
1036            if let Some(state) = &derivation_state {
1037                let ratchet_type = match public_message.content().content_type() {
1038                    ContentType::Application => RatchetType::Application,
1039                    ContentType::Proposal | ContentType::Commit => RatchetType::Handshake,
1040                };
1041                let generation_id = state
1042                    .derive_generation_id(
1043                        provider.crypto(),
1044                        self.group_id(),
1045                        self.epoch(),
1046                        msg.generation,
1047                        ratchet_type,
1048                    )
1049                    .map_err(MessageEncryptionError::VirtualClientsError)?;
1050                msg.generation_id = Some(generation_id);
1051            }
1052            msg
1053        };
1054
1055        provider
1056            .storage()
1057            .write_message_secrets(self.group_id(), &self.message_secrets_store)
1058            .map_err(MessageEncryptionError::StorageError)?;
1059
1060        Ok(msg)
1061    }
1062
1063    /// Outgoing wire format derived from the group's configured policy.
1064    pub(crate) fn outgoing_wire_format(&self) -> WireFormat {
1065        self.mls_group_config.wire_format_policy().outgoing().into()
1066    }
1067
1068    /// Owned `authenticated_data` bytes for the next outgoing message, taking
1069    /// the GroupContext's Safe AAD requirement into account.
1070    ///
1071    /// Callers borrow the returned buffer into a [`FramingParameters`] for the
1072    /// duration of message construction.
1073    pub(crate) fn outgoing_authenticated_data(&self) -> Result<Vec<u8>, LibraryError> {
1074        #[cfg(feature = "extensions-draft")]
1075        {
1076            self.assembled_authenticated_data()
1077        }
1078        #[cfg(not(feature = "extensions-draft"))]
1079        {
1080            Ok(self.aad.clone())
1081        }
1082    }
1083
1084    /// Build the bytes that go into `authenticated_data` for the next outgoing
1085    /// message. When the GroupContext requires Safe AAD framing, the result is
1086    /// the TLS serialization of the staged [`SafeAad`] followed by the bytes of
1087    /// `self.aad`. Otherwise, the result is `self.aad` unchanged.
1088    #[cfg(feature = "extensions-draft")]
1089    pub(crate) fn assembled_authenticated_data(&self) -> Result<Vec<u8>, LibraryError> {
1090        if !self.context().safe_aad_required() {
1091            return Ok(self.aad.clone());
1092        }
1093        crate::framing::safe_aad::assemble_authenticated_data(&self.safe_aad, &self.aad)
1094            .map_err(|_| LibraryError::custom("SafeAad serialization failed"))
1095    }
1096
1097    /// Delete all past epoch secrets.
1098    ///
1099    /// For more information on the arguments to this method, see [`PastEpochDeletion`].
1100    pub fn delete_past_epoch_secrets<Provider: OpenMlsProvider>(
1101        &mut self,
1102        provider: &Provider,
1103        policy: PastEpochDeletion,
1104    ) -> Result<(), DeletePastEpochSecretsError<Provider::StorageError>> {
1105        // delete past epoch secrets in memory
1106        self.message_secrets_store.delete_past_epoch_secrets(policy);
1107        // update the message secrets store in storage
1108        provider
1109            .storage()
1110            .write_message_secrets(self.group_id(), &self.message_secrets_store)?;
1111
1112        Ok(())
1113    }
1114
1115    /// Returns a reference to the proposal store.
1116    pub fn proposal_store(&self) -> &ProposalStore {
1117        self.public_group.proposal_store()
1118    }
1119
1120    /// Returns a mutable reference to the proposal store.
1121    pub(crate) fn proposal_store_mut(&mut self) -> &mut ProposalStore {
1122        self.public_group.proposal_store_mut()
1123    }
1124
1125    /// Get the group context
1126    pub(crate) fn context(&self) -> &GroupContext {
1127        self.public_group.group_context()
1128    }
1129
1130    /// Get the MLS version used in this group.
1131    pub(crate) fn version(&self) -> ProtocolVersion {
1132        self.public_group.version()
1133    }
1134
1135    /// Resets the AAD, including any staged Safe AAD items.
1136    #[inline]
1137    pub(crate) fn reset_aad(&mut self) {
1138        self.aad.clear();
1139        #[cfg(feature = "extensions-draft")]
1140        {
1141            self.safe_aad = SafeAad::empty();
1142        }
1143    }
1144
1145    /// Returns a reference to the public group.
1146    pub fn public_group(&self) -> &PublicGroup {
1147        &self.public_group
1148    }
1149}
1150
1151/// Bookkeeping a virtual client needs to confirm a handshake message
1152/// (proposal or commit) that was framed as a PrivateMessage.
1153///
1154/// Pass `epoch` and `generation` to [`MlsGroup::confirm_handshake_message`]
1155/// once the DS has accepted the message, to delete the retained handshake
1156/// secret. `generation_id` is present when the group is bound to a derivation
1157/// epoch and is attached to the fanned-out message so a strongly-consistent DS
1158/// can detect generation collisions between siblings; it is `None` otherwise.
1159///
1160/// [`MlsGroup::confirm_handshake_message`]: crate::group::MlsGroup::confirm_handshake_message
1161#[cfg(feature = "virtual-clients-draft")]
1162#[derive(Debug, Clone)]
1163pub struct HandshakeConfirmationData {
1164    /// The epoch the message was encrypted in, which is the epoch before a
1165    /// commit is merged.
1166    pub epoch: GroupEpoch,
1167    /// The handshake-ratchet generation used for encryption.
1168    pub generation: u32,
1169    /// The [`GenerationId`] to attach to the fanned-out message, present when
1170    /// the group is bound to a derivation epoch and `None` otherwise.
1171    ///
1172    /// [`GenerationId`]: crate::components::vc_derivation_info::GenerationId
1173    pub generation_id: Option<crate::components::vc_derivation_info::GenerationId>,
1174}
1175
1176/// Result of framing an [`AuthenticatedContent`] handshake message into an
1177/// [`MlsMessageOut`]. Mirrors the cfg-gated field pattern of
1178/// [`EncryptionOutput`]: with the `virtual-clients-draft` feature it also
1179/// carries the [`HandshakeConfirmationData`] for a ciphertext-framed message
1180/// (`None` when the message was framed as a plaintext PublicMessage).
1181///
1182/// [`EncryptionOutput`]: crate::framing::EncryptionOutput
1183pub(crate) struct HandshakeFramingOutput {
1184    pub(crate) message: MlsMessageOut,
1185    #[cfg(feature = "virtual-clients-draft")]
1186    pub(crate) confirmation: Option<HandshakeConfirmationData>,
1187}
1188
1189// Private methods of MlsGroup
1190impl MlsGroup {
1191    /// Store the given [`EncryptionKeyPair`]s in the `provider`'s key store
1192    /// indexed by this group's [`GroupId`] and [`GroupEpoch`].
1193    ///
1194    /// Returns an error if access to the key store fails.
1195    pub(super) fn store_epoch_keypairs<Storage: StorageProvider>(
1196        &self,
1197        store: &Storage,
1198        keypair_references: &[EncryptionKeyPair],
1199    ) -> Result<(), Storage::Error> {
1200        store.write_encryption_epoch_key_pairs(
1201            self.group_id(),
1202            &self.context().epoch(),
1203            self.own_leaf_index().u32(),
1204            keypair_references,
1205        )
1206    }
1207
1208    /// Read the [`EncryptionKeyPair`]s of this group and its current
1209    /// [`GroupEpoch`] from the `provider`'s storage.
1210    ///
1211    /// Returns an error if the lookup in the [`StorageProvider`] fails.
1212    pub(super) fn read_epoch_keypairs<Storage: StorageProvider>(
1213        &self,
1214        store: &Storage,
1215    ) -> Result<Vec<EncryptionKeyPair>, Storage::Error> {
1216        store.encryption_epoch_key_pairs(
1217            self.group_id(),
1218            &self.context().epoch(),
1219            self.own_leaf_index().u32(),
1220        )
1221    }
1222
1223    /// Delete the [`EncryptionKeyPair`]s from the previous [`GroupEpoch`] from
1224    /// the `provider`'s key store.
1225    ///
1226    /// Returns an error if access to the key store fails.
1227    #[cfg(not(feature = "virtual-clients-draft"))]
1228    pub(super) fn delete_previous_epoch_keypairs<Storage: StorageProvider>(
1229        &self,
1230        store: &Storage,
1231    ) -> Result<(), Storage::Error> {
1232        store.delete_encryption_epoch_key_pairs(
1233            self.group_id(),
1234            &GroupEpoch::from(self.context().epoch().as_u64() - 1),
1235            self.own_leaf_index().u32(),
1236        )
1237    }
1238
1239    #[cfg(feature = "virtual-clients-draft")]
1240    pub(super) fn delete_previous_epoch_keypairs<Storage: StorageProvider>(
1241        &self,
1242        store: &Storage,
1243        previous_own_leaf_index: LeafNodeIndex,
1244    ) -> Result<(), Storage::Error> {
1245        // In the sibling-resync flow, `merge_commit` installs the joiner's
1246        // leaf as our own leaf before it filters and stores the new epoch
1247        // keypairs. Previous-epoch keypairs are still stored under the leaf
1248        // index from that previous epoch, so the caller must pass that index
1249        // explicitly instead of having this helper read `self.own_leaf_index()`.
1250        store.delete_encryption_epoch_key_pairs(
1251            self.group_id(),
1252            &GroupEpoch::from(self.context().epoch().as_u64() - 1),
1253            previous_own_leaf_index.u32(),
1254        )
1255    }
1256
1257    /// Stores the state of this group. Only to be called from constructors to
1258    /// store the initial state of the group.
1259    pub(super) fn store<Storage: crate::storage::StorageProvider>(
1260        &self,
1261        storage: &Storage,
1262    ) -> Result<(), Storage::Error> {
1263        self.public_group.store(storage)?;
1264        storage.write_group_epoch_secrets(self.group_id(), &self.group_epoch_secrets)?;
1265        storage.write_own_leaf_index(self.group_id(), &self.own_leaf_index)?;
1266        storage.write_message_secrets(self.group_id(), &self.message_secrets_store)?;
1267        storage.write_resumption_psk_store(self.group_id(), &self.resumption_psk_store)?;
1268        storage.write_mls_join_config(self.group_id(), &self.mls_group_config)?;
1269        storage.write_group_state(self.group_id(), &self.group_state)?;
1270        #[cfg(feature = "extensions-draft")]
1271        if let Some(application_export_tree) = &self.application_export_tree {
1272            storage.write_application_export_tree(self.group_id(), application_export_tree)?;
1273        }
1274
1275        Ok(())
1276    }
1277
1278    /// Converts PublicMessage to MlsMessage. Depending on whether handshake
1279    /// message should be encrypted, PublicMessage messages are encrypted to
1280    /// PrivateMessage first.
1281    fn content_to_mls_message(
1282        &mut self,
1283        mls_auth_content: AuthenticatedContent,
1284        provider: &impl OpenMlsProvider,
1285    ) -> Result<HandshakeFramingOutput, LibraryError> {
1286        let output = match self.configuration().wire_format_policy().outgoing() {
1287            OutgoingWireFormatPolicy::AlwaysPlaintext => {
1288                let mut plaintext: PublicMessage = mls_auth_content.into();
1289                // Set the membership tag only if the sender type is `Member`.
1290                if plaintext.sender().is_member() {
1291                    plaintext.set_membership_tag(
1292                        provider.crypto(),
1293                        self.ciphersuite(),
1294                        self.message_secrets().membership_key(),
1295                        self.message_secrets().serialized_context(),
1296                    )?;
1297                }
1298                HandshakeFramingOutput {
1299                    message: plaintext.into(),
1300                    #[cfg(feature = "virtual-clients-draft")]
1301                    confirmation: None,
1302                }
1303            }
1304            OutgoingWireFormatPolicy::AlwaysCiphertext => {
1305                // A ciphertext-framed handshake message ties its confirmation
1306                // to the epoch it was encrypted in, which is the current epoch
1307                // at framing time, before a commit is merged.
1308                #[cfg(feature = "virtual-clients-draft")]
1309                let epoch = self.epoch();
1310                let encryption_output = self
1311                    .encrypt(mls_auth_content, provider)
1312                    // We can be sure the encryption will work because the plaintext was created by us
1313                    .map_err(|_| LibraryError::custom("Malformed plaintext"))?;
1314                let message = MlsMessageOut::from_private_message(
1315                    encryption_output.private_message,
1316                    self.version(),
1317                );
1318                HandshakeFramingOutput {
1319                    message,
1320                    #[cfg(feature = "virtual-clients-draft")]
1321                    confirmation: Some(HandshakeConfirmationData {
1322                        epoch,
1323                        generation: encryption_output.generation,
1324                        generation_id: encryption_output.generation_id,
1325                    }),
1326                }
1327            }
1328        };
1329        Ok(output)
1330    }
1331
1332    /// Check if the group is operational. Throws an error if the group is
1333    /// inactive or if there is a pending commit.
1334    fn is_operational(&self) -> Result<(), MlsGroupStateError> {
1335        match self.group_state {
1336            MlsGroupState::PendingCommit(_) => Err(MlsGroupStateError::PendingCommit),
1337            MlsGroupState::Inactive => Err(MlsGroupStateError::UseAfterEviction),
1338            MlsGroupState::Operational => Ok(()),
1339        }
1340    }
1341}
1342
1343// Methods used in tests
1344impl MlsGroup {
1345    #[cfg(any(feature = "test-utils", test))]
1346    pub fn export_group_context(&self) -> &GroupContext {
1347        self.context()
1348    }
1349
1350    #[cfg(any(feature = "test-utils", test))]
1351    pub fn tree_hash(&self) -> &[u8] {
1352        self.public_group().group_context().tree_hash()
1353    }
1354
1355    #[cfg(any(feature = "test-utils", test))]
1356    pub(crate) fn message_secrets_test_mut(&mut self) -> &mut MessageSecrets {
1357        self.message_secrets_store.message_secrets_mut()
1358    }
1359
1360    #[cfg(any(feature = "test-utils", test))]
1361    pub fn print_ratchet_tree(&self, message: &str) {
1362        println!("{}: {}", message, self.public_group().export_ratchet_tree());
1363    }
1364
1365    #[cfg(any(feature = "test-utils", test))]
1366    pub(crate) fn context_mut(&mut self) -> &mut GroupContext {
1367        self.public_group.context_mut()
1368    }
1369
1370    #[cfg(test)]
1371    pub(crate) fn set_own_leaf_index(&mut self, own_leaf_index: LeafNodeIndex) {
1372        self.own_leaf_index = own_leaf_index;
1373    }
1374
1375    #[cfg(test)]
1376    pub(crate) fn own_tree_position(&self) -> TreePosition {
1377        TreePosition::new(self.group_id().clone(), self.own_leaf_index())
1378    }
1379
1380    #[cfg(test)]
1381    pub(crate) fn message_secrets_store(&self) -> &MessageSecretsStore {
1382        &self.message_secrets_store
1383    }
1384
1385    #[cfg(test)]
1386    pub(crate) fn resumption_psk_store(&self) -> &ResumptionPskStore {
1387        &self.resumption_psk_store
1388    }
1389
1390    #[cfg(test)]
1391    pub(crate) fn set_group_context(&mut self, group_context: GroupContext) {
1392        self.public_group.set_group_context(group_context)
1393    }
1394
1395    #[cfg(any(test, feature = "test-utils"))]
1396    pub fn ensure_persistence(&self, storage: &impl StorageProvider) -> Result<(), LibraryError> {
1397        let loaded = MlsGroup::load(storage, self.group_id())
1398            .map_err(|_| LibraryError::custom("Failed to load group from storage"))?;
1399        let other = loaded.ok_or_else(|| LibraryError::custom("Group not found in storage"))?;
1400
1401        if self != &other {
1402            let mut diagnostics = Vec::new();
1403
1404            if self.mls_group_config != other.mls_group_config {
1405                diagnostics.push(format!(
1406                    "mls_group_config:\n  Current: {:?}\n  Loaded:  {:?}",
1407                    self.mls_group_config, other.mls_group_config
1408                ));
1409            }
1410            if self.public_group != other.public_group {
1411                diagnostics.push(format!(
1412                    "public_group:\n  Current: {:?}\n  Loaded:  {:?}",
1413                    self.public_group, other.public_group
1414                ));
1415            }
1416            if self.group_epoch_secrets != other.group_epoch_secrets {
1417                diagnostics.push(format!(
1418                    "group_epoch_secrets:\n  Current: {:?}\n  Loaded:  {:?}",
1419                    self.group_epoch_secrets, other.group_epoch_secrets
1420                ));
1421            }
1422            if self.own_leaf_index != other.own_leaf_index {
1423                diagnostics.push(format!(
1424                    "own_leaf_index:\n  Current: {:?}\n  Loaded:  {:?}",
1425                    self.own_leaf_index, other.own_leaf_index
1426                ));
1427            }
1428            if self.message_secrets_store != other.message_secrets_store {
1429                diagnostics.push(format!(
1430                    "message_secrets_store:\n  Current: {:?}\n  Loaded:  {:?}",
1431                    self.message_secrets_store, other.message_secrets_store
1432                ));
1433            }
1434            if self.resumption_psk_store != other.resumption_psk_store {
1435                diagnostics.push(format!(
1436                    "resumption_psk_store:\n  Current: {:?}\n  Loaded:  {:?}",
1437                    self.resumption_psk_store, other.resumption_psk_store
1438                ));
1439            }
1440            if self.own_leaf_nodes != other.own_leaf_nodes {
1441                diagnostics.push(format!(
1442                    "own_leaf_nodes:\n  Current: {:?}\n  Loaded:  {:?}",
1443                    self.own_leaf_nodes, other.own_leaf_nodes
1444                ));
1445            }
1446            if self.aad != other.aad {
1447                diagnostics.push(format!(
1448                    "aad:\n  Current: {:?}\n  Loaded:  {:?}",
1449                    self.aad, other.aad
1450                ));
1451            }
1452            if self.group_state != other.group_state {
1453                diagnostics.push(format!(
1454                    "group_state:\n  Current: {:?}\n  Loaded:  {:?}",
1455                    self.group_state, other.group_state
1456                ));
1457            }
1458            #[cfg(feature = "extensions-draft")]
1459            if self.application_export_tree != other.application_export_tree {
1460                diagnostics.push(format!(
1461                    "application_export_tree:\n  Current: {:?}\n  Loaded:  {:?}",
1462                    self.application_export_tree, other.application_export_tree
1463                ));
1464            }
1465            #[cfg(feature = "virtual-clients-draft")]
1466            if self.emulation_group != other.emulation_group {
1467                diagnostics.push(format!(
1468                    "emulation_group:\n  Current: {:?}\n  Loaded:  {:?}",
1469                    self.emulation_group, other.emulation_group
1470                ));
1471            }
1472
1473            log::error!(
1474                "Loaded group does not match current group! Differing fields ({}):\n\n{}",
1475                diagnostics.len(),
1476                diagnostics.join("\n\n")
1477            );
1478
1479            return Err(LibraryError::custom(
1480                "Loaded group does not match current group",
1481            ));
1482        }
1483
1484        Ok(())
1485    }
1486}
1487
1488/// A [`StagedWelcome`] can be inspected and then turned into a [`MlsGroup`].
1489/// This allows checking who authored the Welcome message.
1490#[derive(Debug)]
1491pub struct StagedWelcome {
1492    // The group configuration. See [`MlsGroupJoinConfig`] for more information.
1493    mls_group_config: MlsGroupJoinConfig,
1494    public_group: PublicGroup,
1495    group_epoch_secrets: GroupEpochSecrets,
1496    own_leaf_index: LeafNodeIndex,
1497
1498    /// A [`MessageSecretsStore`] that stores message secrets.
1499    /// By default this store has the length of 1, i.e. only the [`MessageSecrets`]
1500    /// of the current epoch is kept.
1501    /// If more secrets from past epochs should be kept in order to be
1502    /// able to decrypt application messages from previous epochs, the size of
1503    /// the store must be increased through [`max_past_epochs()`].
1504    message_secrets_store: MessageSecretsStore,
1505
1506    /// A secret that is not stored as part of the [`MlsGroup`] after the group is created.
1507    /// It can be used by the application to derive forward secure secrets.
1508    #[cfg(feature = "extensions-draft")]
1509    application_export_secret: ApplicationExportSecret,
1510
1511    /// Resumption psk store. This is where the resumption psks are kept in a rollover list.
1512    resumption_psk_store: ResumptionPskStore,
1513
1514    /// The [`VerifiableGroupInfo`] from the [`Welcome`] message.
1515    verifiable_group_info: VerifiableGroupInfo,
1516
1517    /// The key material used to join via this welcome.
1518    key_material: WelcomeKeyMaterial,
1519
1520    /// If we got a path secret, these are the derived path keys.
1521    path_keypairs: Option<Vec<EncryptionKeyPair>>,
1522
1523    /// Whether to join the group as an emulation group of a virtual client. Set
1524    /// by [`Self::emulation_group`].
1525    #[cfg(feature = "virtual-clients-draft")]
1526    emulation_group: bool,
1527}
1528
1529/// A `Welcome` message that has been processed but not staged yet.
1530///
1531/// This may be used in order to retrieve information from the `Welcome` about
1532/// the ratchet tree and PSKs.
1533///
1534/// Use `into_staged_welcome` to stage it into a [`StagedWelcome`].
1535pub struct ProcessedWelcome {
1536    // The group configuration. See [`MlsGroupJoinConfig`] for more information.
1537    mls_group_config: MlsGroupJoinConfig,
1538
1539    // The following is the state after parsing the Welcome message, before actually
1540    // building the group.
1541    ciphersuite: Ciphersuite,
1542    group_secrets: GroupSecrets,
1543    epoch_secrets: crate::schedule::EpochSecretsResult,
1544    verifiable_group_info: crate::messages::group_info::VerifiableGroupInfo,
1545    resumption_psk_store: crate::schedule::psk::store::ResumptionPskStore,
1546    key_material: WelcomeKeyMaterial,
1547}
1548
1549/// The key material a client uses to process a [`Welcome`] message.
1550#[derive(Debug)]
1551pub struct WelcomeKeyMaterial {
1552    inner: WelcomeKeyMaterialInner,
1553}
1554
1555/// The inner data of a [`WelcomeKeyMaterial`].
1556///
1557/// A regular member holds a local [`KeyPackageBundle`]. A sibling emulator
1558/// joining a higher-level group as a virtual client has no local bundle: it
1559/// derives the init and leaf-encryption keys from the operation secret tree of
1560/// the derivation epoch the KeyPackage belongs to.
1561///
1562/// [`Welcome`]: crate::messages::Welcome
1563#[derive(Debug)]
1564pub(crate) enum WelcomeKeyMaterialInner {
1565    /// A locally stored [`KeyPackageBundle`]. Boxed to keep the enum small,
1566    /// since the virtual-client variant is much smaller.
1567    KeyPackage(Box<KeyPackageBundle>),
1568    /// Virtual-client material derived from a derivation epoch's operation
1569    /// secret tree.
1570    #[cfg(feature = "virtual-clients-draft")]
1571    VirtualClient(crate::components::vc_derivation_info::VcWelcomeMaterial),
1572}
1573
1574impl WelcomeKeyMaterial {
1575    /// Create a new [`WelcomeKeyMaterial`] from a [`KeyPackageBundle`].
1576    pub(crate) fn with_key_package_bundle(key_package: KeyPackageBundle) -> Self {
1577        Self {
1578            inner: WelcomeKeyMaterialInner::KeyPackage(Box::new(key_package)),
1579        }
1580    }
1581
1582    /// Create a new [`WelcomeKeyMaterial`] from a [`VcWelcomeMaterial`].
1583    ///
1584    /// [`VcWelcomeMaterial`]: crate::components::vc_derivation_info::VcWelcomeMaterial
1585    #[cfg(feature = "virtual-clients-draft")]
1586    pub(crate) fn with_vc_welcome_material(
1587        material: crate::components::vc_derivation_info::VcWelcomeMaterial,
1588    ) -> Self {
1589        Self {
1590            inner: WelcomeKeyMaterialInner::VirtualClient(material),
1591        }
1592    }
1593
1594    pub(crate) fn inner(&self) -> &WelcomeKeyMaterialInner {
1595        &self.inner
1596    }
1597
1598    /// The [`KeyPackageRef`] addressed by the welcome's encrypted group
1599    /// secrets. The bundle computes it from its KeyPackage, the virtual-client
1600    /// material carries the ref it was matched on.
1601    ///
1602    /// [`KeyPackageRef`]: crate::ciphersuite::hash_ref::KeyPackageRef
1603    pub fn key_package_ref(
1604        &self,
1605        crypto: &impl OpenMlsCrypto,
1606    ) -> Result<crate::ciphersuite::hash_ref::KeyPackageRef, LibraryError> {
1607        match &self.inner {
1608            WelcomeKeyMaterialInner::KeyPackage(bundle) => bundle.key_package().hash_ref(crypto),
1609            #[cfg(feature = "virtual-clients-draft")]
1610            WelcomeKeyMaterialInner::VirtualClient(material) => {
1611                Ok(material.key_package_ref.clone())
1612            }
1613        }
1614    }
1615
1616    /// The init private key used to decrypt the encrypted group secrets.
1617    pub fn init_private_key(&self) -> &crate::ciphersuite::HpkePrivateKey {
1618        match &self.inner {
1619            WelcomeKeyMaterialInner::KeyPackage(bundle) => bundle.init_private_key(),
1620            #[cfg(feature = "virtual-clients-draft")]
1621            WelcomeKeyMaterialInner::VirtualClient(material) => &material.init_private_key,
1622        }
1623    }
1624
1625    /// The public init key the encrypted group secrets are encrypted to.
1626    pub fn hpke_init_key(&self) -> &InitKey {
1627        match &self.inner {
1628            WelcomeKeyMaterialInner::KeyPackage(bundle) => bundle.key_package().hpke_init_key(),
1629            #[cfg(feature = "virtual-clients-draft")]
1630            WelcomeKeyMaterialInner::VirtualClient(material) => &material.init_key,
1631        }
1632    }
1633
1634    /// The local [`KeyPackageBundle`] on the regular path, or `None` on the
1635    /// virtual-client path. Checks that only apply when there is a local
1636    /// KeyPackage to compare against branch on this value.
1637    pub fn key_package_bundle(&self) -> Option<&KeyPackageBundle> {
1638        match &self.inner {
1639            WelcomeKeyMaterialInner::KeyPackage(bundle) => Some(bundle),
1640            #[cfg(feature = "virtual-clients-draft")]
1641            WelcomeKeyMaterialInner::VirtualClient(_) => None,
1642        }
1643    }
1644
1645    /// The virtual-client material on the virtual-client path, or `None` on
1646    /// the regular path.
1647    #[cfg(feature = "virtual-clients-draft")]
1648    pub(crate) fn vc_welcome_material(
1649        &self,
1650    ) -> Option<&crate::components::vc_derivation_info::VcWelcomeMaterial> {
1651        match &self.inner {
1652            WelcomeKeyMaterialInner::KeyPackage(_) => None,
1653            WelcomeKeyMaterialInner::VirtualClient(material) => Some(material),
1654        }
1655    }
1656
1657    /// The joiner's leaf encryption keypair.
1658    fn encryption_key_pair(&self) -> EncryptionKeyPair {
1659        match &self.inner {
1660            WelcomeKeyMaterialInner::KeyPackage(bundle) => bundle.encryption_key_pair(),
1661            #[cfg(feature = "virtual-clients-draft")]
1662            WelcomeKeyMaterialInner::VirtualClient(material) => material.encryption_keypair.clone(),
1663        }
1664    }
1665}