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, Extension, ExtensionType, ExternalPubExtension,
24        GroupContext, GroupEpoch, GroupId, MlsGroupJoinConfig, MlsGroupStateError,
25        OutgoingWireFormatPolicy, PublicGroup, RatchetTreeExtension, RequiredCapabilitiesExtension,
26        StagedCommit,
27    },
28    key_packages::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,
43    },
44    versions::ProtocolVersion,
45};
46use openmls_traits::{signatures::Signer, storage::StorageProvider as _, types::Ciphersuite};
47
48#[cfg(feature = "extensions-draft-08")]
49use crate::schedule::{application_export_tree::ApplicationExportTree, ApplicationExportSecret};
50
51// Private
52mod application;
53mod exporting;
54mod updates;
55
56use config::*;
57
58// Crate
59pub(crate) mod builder;
60pub(crate) mod commit_builder;
61pub(crate) mod config;
62pub(crate) mod creation;
63pub(crate) mod errors;
64pub(crate) mod membership;
65pub(crate) mod past_secrets;
66pub(crate) mod processing;
67pub(crate) mod proposal;
68pub(crate) mod proposal_store;
69pub(crate) mod staged_commit;
70
71#[cfg(feature = "extensions-draft-08")]
72pub(crate) mod app_ephemeral;
73
74// Tests
75#[cfg(test)]
76pub(crate) mod tests_and_kats;
77
78#[derive(Debug)]
79pub(crate) struct CreateCommitResult {
80    pub(crate) commit: AuthenticatedContent,
81    pub(crate) welcome_option: Option<Welcome>,
82    pub(crate) staged_commit: StagedCommit,
83    pub(crate) group_info: Option<GroupInfo>,
84}
85
86/// A member in the group is identified by this [`Member`] struct.
87#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
88pub struct Member {
89    /// The member's leaf index in the ratchet tree.
90    pub index: LeafNodeIndex,
91    /// The member's credential.
92    pub credential: Credential,
93    /// The member's public HPHKE encryption key.
94    pub encryption_key: Vec<u8>,
95    /// The member's public signature key.
96    pub signature_key: Vec<u8>,
97}
98
99impl Member {
100    /// Create new member.
101    pub fn new(
102        index: LeafNodeIndex,
103        encryption_key: Vec<u8>,
104        signature_key: Vec<u8>,
105        credential: Credential,
106    ) -> Self {
107        Self {
108            index,
109            encryption_key,
110            signature_key,
111            credential,
112        }
113    }
114}
115
116/// Pending Commit state. Differentiates between Commits issued by group members
117/// and External Commits.
118#[derive(Debug, Serialize, Deserialize)]
119#[cfg_attr(any(test, feature = "test-utils"), derive(Clone, PartialEq))]
120pub enum PendingCommitState {
121    /// Commit from a group member
122    Member(StagedCommit),
123    /// Commit from an external joiner
124    External(StagedCommit),
125}
126
127impl PendingCommitState {
128    /// Returns a reference to the [`StagedCommit`] contained in the
129    /// [`PendingCommitState`] enum.
130    pub(crate) fn staged_commit(&self) -> &StagedCommit {
131        match self {
132            PendingCommitState::Member(pc) => pc,
133            PendingCommitState::External(pc) => pc,
134        }
135    }
136}
137
138impl From<PendingCommitState> for StagedCommit {
139    fn from(pcs: PendingCommitState) -> Self {
140        match pcs {
141            PendingCommitState::Member(pc) => pc,
142            PendingCommitState::External(pc) => pc,
143        }
144    }
145}
146
147/// [`MlsGroupState`] determines the state of an [`MlsGroup`]. The different
148/// states and their transitions are as follows:
149///
150/// * [`MlsGroupState::Operational`]: This is the main state of the group, which
151///   allows access to all of its functionality, (except merging pending commits,
152///   see the [`MlsGroupState::PendingCommit`] for more information) and it's the
153///   state the group starts in (except when created via
154///   [`MlsGroup::external_commit_builder()`], see the functions documentation for
155///   more information). From this `Operational`, the group state can either
156///   transition to [`MlsGroupState::Inactive`], when it processes a commit that
157///   removes this client from the group, or to [`MlsGroupState::PendingCommit`],
158///   when this client creates a commit.
159///
160/// * [`MlsGroupState::Inactive`]: A group can enter this state from any other
161///   state when it processes a commit that removes this client from the group.
162///   This is a terminal state that the group can not exit from. If the clients
163///   wants to re-join the group, it can either be added by a group member or it
164///   can join via external commit.
165///
166/// * [`MlsGroupState::PendingCommit`]: This state is split into two possible
167///   sub-states, one for each Commit type:
168///   [`PendingCommitState::Member`] and [`PendingCommitState::External`]:
169///
170///   * If the client creates a commit for this group, the `PendingCommit` state
171///     is entered with [`PendingCommitState::Member`] and with the [`StagedCommit`] as
172///     additional state variable. In this state, it can perform the same
173///     operations as in the [`MlsGroupState::Operational`], except that it cannot
174///     create proposals or commits. However, it can merge or clear the stored
175///     [`StagedCommit`], where both actions result in a transition to the
176///     [`MlsGroupState::Operational`]. Additionally, if a commit from another
177///     group member is processed, the own pending commit is also cleared and
178///     either the `Inactive` state is entered (if this client was removed from
179///     the group as part of the processed commit), or the `Operational` state is
180///     entered.
181///
182///   * A group can enter the [`PendingCommitState::External`] sub-state only as
183///     the initial state when the group is created via
184///     [`MlsGroup::external_commit_builder()`]. In contrast to the
185///     [`PendingCommitState::Member`] `PendingCommit` state, the only possible
186///     functionality that can be used is the [`MlsGroup::merge_pending_commit()`]
187///     function, which merges the pending external commit and transitions the
188///     state to [`MlsGroupState::PendingCommit`]. For more information on the
189///     external commit process, see [`MlsGroup::external_commit_builder()`] or
190///     Section 11.2.1 of the MLS specification.
191#[derive(Debug, Serialize, Deserialize)]
192#[cfg_attr(any(test, feature = "test-utils"), derive(Clone, PartialEq))]
193pub enum MlsGroupState {
194    /// There is currently a pending Commit that hasn't been merged yet.
195    PendingCommit(Box<PendingCommitState>),
196    /// The group state is in an opertaional state, where new messages and Commits can be created.
197    Operational,
198    /// The group is inactive because the member has been removed.
199    Inactive,
200}
201
202/// A `MlsGroup` represents an MLS group with a high-level API. The API exposes
203/// high level functions to manage a group by adding/removing members, get the
204/// current member list, etc.
205///
206/// The API is modeled such that it can serve as a direct interface to the
207/// Delivery Service. Functions that modify the public state of the group will
208/// return a `Vec<MLSMessageOut>` that can be sent to the Delivery Service
209/// directly. Conversely, incoming messages from the Delivery Service can be fed
210/// into [process_message()](`MlsGroup::process_message()`).
211///
212/// An `MlsGroup` has an internal queue of pending proposals that builds up as
213/// new messages are processed. When creating proposals, those messages are not
214/// automatically appended to this queue, instead they have to be processed
215/// again through [process_message()](`MlsGroup::process_message()`). This
216/// allows the Delivery Service to reject them (e.g. if they reference the wrong
217/// epoch).
218///
219/// If incoming messages or applied operations are semantically or syntactically
220/// incorrect, an error event will be returned with a corresponding error
221/// message and the state of the group will remain unchanged.
222///
223/// An `MlsGroup` has an internal state variable determining if it is active or
224/// inactive, as well as if it has a pending commit. See [`MlsGroupState`] for
225/// more information.
226#[derive(Debug)]
227#[cfg_attr(feature = "test-utils", derive(Clone, PartialEq))]
228pub struct MlsGroup {
229    /// The group configuration. See [`MlsGroupJoinConfig`] for more information.
230    mls_group_config: MlsGroupJoinConfig,
231    /// The public state of the group.
232    public_group: PublicGroup,
233    /// Epoch-specific secrets of the group.
234    group_epoch_secrets: GroupEpochSecrets,
235    /// The own leaf index in the ratchet tree.
236    own_leaf_index: LeafNodeIndex,
237    /// A [`MessageSecretsStore`] that stores message secrets.
238    /// By default this store has the length of 1, i.e. only the [`MessageSecrets`]
239    /// of the current epoch is kept.
240    /// If more secrets from past epochs should be kept in order to be
241    /// able to decrypt application messages from previous epochs, the size of
242    /// the store must be increased through [`max_past_epochs()`].
243    message_secrets_store: MessageSecretsStore,
244    // Resumption psk store. This is where the resumption psks are kept in a rollover list.
245    resumption_psk_store: ResumptionPskStore,
246    // Own [`LeafNode`]s that were created for update proposals and that
247    // are needed in case an update proposal is committed by another group
248    // member. The vector is emptied after every epoch change.
249    own_leaf_nodes: Vec<LeafNode>,
250    // Additional authenticated data (AAD) for the next outgoing message. This
251    // is ephemeral and will be reset by every API call that successfully
252    // returns an [`MlsMessageOut`].
253    aad: Vec<u8>,
254    // A variable that indicates the state of the group. See [`MlsGroupState`]
255    // for more information.
256    group_state: MlsGroupState,
257    /// The state of the Application Exporter. See the MLS Extensions Draft 08
258    /// for more information. This is `None` if an old OpenMLS group state was
259    /// loaded and has not yet merged a commit.
260    #[cfg(feature = "extensions-draft-08")]
261    application_export_tree: Option<ApplicationExportTree>,
262}
263
264impl MlsGroup {
265    // === Configuration ===
266
267    /// Returns the configuration.
268    pub fn configuration(&self) -> &MlsGroupJoinConfig {
269        &self.mls_group_config
270    }
271
272    /// Sets the configuration.
273    pub fn set_configuration<Storage: StorageProvider>(
274        &mut self,
275        storage: &Storage,
276        mls_group_config: &MlsGroupJoinConfig,
277    ) -> Result<(), Storage::Error> {
278        self.mls_group_config = mls_group_config.clone();
279        storage.write_mls_join_config(self.group_id(), mls_group_config)
280    }
281
282    /// Sets the additional authenticated data (AAD) for the next outgoing
283    /// message. This is ephemeral and will be reset by every API call that
284    /// successfully returns an [`MlsMessageOut`].
285    pub fn set_aad(&mut self, aad: Vec<u8>) {
286        self.aad = aad;
287    }
288
289    /// Returns the additional authenticated data (AAD) for the next outgoing
290    /// message.
291    pub fn aad(&self) -> &[u8] {
292        &self.aad
293    }
294
295    // === Advanced functions ===
296
297    /// Returns the group's ciphersuite.
298    pub fn ciphersuite(&self) -> Ciphersuite {
299        self.public_group.ciphersuite()
300    }
301
302    /// Get confirmation tag.
303    pub fn confirmation_tag(&self) -> &ConfirmationTag {
304        self.public_group.confirmation_tag()
305    }
306
307    /// Returns whether the own client is still a member of the group or if it
308    /// was already evicted
309    pub fn is_active(&self) -> bool {
310        !matches!(self.group_state, MlsGroupState::Inactive)
311    }
312
313    /// Returns own credential. If the group is inactive, it returns a
314    /// `UseAfterEviction` error.
315    pub fn credential(&self) -> Result<&Credential, MlsGroupStateError> {
316        if !self.is_active() {
317            return Err(MlsGroupStateError::UseAfterEviction);
318        }
319        self.public_group
320            .leaf(self.own_leaf_index())
321            .map(|node| node.credential())
322            .ok_or_else(|| LibraryError::custom("Own leaf node missing").into())
323    }
324
325    /// Returns the leaf index of the client in the tree owning this group.
326    pub fn own_leaf_index(&self) -> LeafNodeIndex {
327        self.own_leaf_index
328    }
329
330    /// Returns the leaf node of the client in the tree owning this group.
331    pub fn own_leaf_node(&self) -> Option<&LeafNode> {
332        self.public_group().leaf(self.own_leaf_index())
333    }
334
335    /// Returns the group ID.
336    pub fn group_id(&self) -> &GroupId {
337        self.public_group.group_id()
338    }
339
340    /// Returns the epoch.
341    pub fn epoch(&self) -> GroupEpoch {
342        self.public_group.group_context().epoch()
343    }
344
345    /// Returns an `Iterator` over pending proposals.
346    pub fn pending_proposals(&self) -> impl Iterator<Item = &QueuedProposal> {
347        self.proposal_store().proposals()
348    }
349
350    /// Returns a reference to the [`StagedCommit`] of the most recently created
351    /// commit. If there was no commit created in this epoch, either because
352    /// this commit or another commit was merged, it returns `None`.
353    pub fn pending_commit(&self) -> Option<&StagedCommit> {
354        match self.group_state {
355            MlsGroupState::PendingCommit(ref pending_commit_state) => {
356                Some(pending_commit_state.staged_commit())
357            }
358            MlsGroupState::Operational => None,
359            MlsGroupState::Inactive => None,
360        }
361    }
362
363    /// Sets the `group_state` to [`MlsGroupState::Operational`], thus clearing
364    /// any potentially pending commits.
365    ///
366    /// Note that this has no effect if the group was created through an external commit and
367    /// the resulting external commit has not been merged yet. For more
368    /// information, see [`MlsGroup::external_commit_builder()`].
369    ///
370    /// Use with caution! This function should only be used if it is clear that
371    /// the pending commit will not be used in the group. In particular, if a
372    /// pending commit is later accepted by the group, this client will lack the
373    /// key material to encrypt or decrypt group messages.
374    pub fn clear_pending_commit<Storage: StorageProvider>(
375        &mut self,
376        storage: &Storage,
377    ) -> Result<(), Storage::Error> {
378        match self.group_state {
379            MlsGroupState::PendingCommit(ref pending_commit_state) => {
380                if let PendingCommitState::Member(_) = **pending_commit_state {
381                    self.group_state = MlsGroupState::Operational;
382                    storage.write_group_state(self.group_id(), &self.group_state)
383                } else {
384                    Ok(())
385                }
386            }
387            MlsGroupState::Operational | MlsGroupState::Inactive => Ok(()),
388        }
389    }
390
391    /// Clear the pending proposals, if the proposal store is not empty.
392    ///
393    /// Warning: Once the pending proposals are cleared it will be impossible to process
394    /// a Commit message that references those proposals. Only use this
395    /// function as a last resort, e.g. when a call to
396    /// `MlsGroup::commit_to_pending_proposals` fails.
397    pub fn clear_pending_proposals<Storage: StorageProvider>(
398        &mut self,
399        storage: &Storage,
400    ) -> Result<(), Storage::Error> {
401        // If the proposal store is not empty...
402        if !self.proposal_store().is_empty() {
403            // Empty the proposal store
404            self.proposal_store_mut().empty();
405
406            // Clear proposals in storage
407            storage.clear_proposal_queue::<GroupId, ProposalRef>(self.group_id())?;
408        }
409
410        Ok(())
411    }
412
413    /// Get a reference to the group context [`Extensions`] of this [`MlsGroup`].
414    pub fn extensions(&self) -> &Extensions<GroupContext> {
415        self.public_group().group_context().extensions()
416    }
417
418    /// Returns the index of the sender of a staged, external commit.
419    pub fn ext_commit_sender_index(
420        &self,
421        commit: &StagedCommit,
422    ) -> Result<LeafNodeIndex, LibraryError> {
423        self.public_group().ext_commit_sender_index(commit)
424    }
425
426    // === Storage Methods ===
427
428    /// Loads the state of the group with given id from persisted state.
429    pub fn load<Storage: crate::storage::StorageProvider>(
430        storage: &Storage,
431        group_id: &GroupId,
432    ) -> Result<Option<MlsGroup>, Storage::Error> {
433        let public_group = PublicGroup::load(storage, group_id)?;
434        let group_epoch_secrets = storage.group_epoch_secrets(group_id)?;
435        let own_leaf_index = storage.own_leaf_index(group_id)?;
436        let message_secrets_store = storage.message_secrets(group_id)?;
437        let resumption_psk_store = storage.resumption_psk_store(group_id)?;
438        let mls_group_config = storage.mls_group_join_config(group_id)?;
439        let own_leaf_nodes = storage.own_leaf_nodes(group_id)?;
440        let group_state = storage.group_state(group_id)?;
441        #[cfg(feature = "extensions-draft-08")]
442        let application_export_tree = storage.application_export_tree(group_id)?;
443
444        let build = || -> Option<Self> {
445            Some(Self {
446                public_group: public_group?,
447                group_epoch_secrets: group_epoch_secrets?,
448                own_leaf_index: own_leaf_index?,
449                message_secrets_store: message_secrets_store?,
450                resumption_psk_store: resumption_psk_store?,
451                mls_group_config: mls_group_config?,
452                own_leaf_nodes,
453                aad: vec![],
454                group_state: group_state?,
455                #[cfg(feature = "extensions-draft-08")]
456                application_export_tree,
457            })
458        };
459
460        Ok(build())
461    }
462
463    /// Remove the persisted state of this group from storage. Note that
464    /// signature key material is not managed by OpenMLS and has to be removed
465    /// from the storage provider separately (if desired).
466    pub fn delete<Storage: crate::storage::StorageProvider>(
467        &mut self,
468        storage: &Storage,
469    ) -> Result<(), Storage::Error> {
470        PublicGroup::delete(storage, self.group_id())?;
471        storage.delete_own_leaf_index(self.group_id())?;
472        storage.delete_group_epoch_secrets(self.group_id())?;
473        storage.delete_message_secrets(self.group_id())?;
474        storage.delete_all_resumption_psk_secrets(self.group_id())?;
475        storage.delete_group_config(self.group_id())?;
476        storage.delete_own_leaf_nodes(self.group_id())?;
477        storage.delete_group_state(self.group_id())?;
478        storage.clear_proposal_queue::<GroupId, ProposalRef>(self.group_id())?;
479
480        #[cfg(feature = "extensions-draft-08")]
481        storage.delete_application_export_tree::<_, ApplicationExportTree>(self.group_id())?;
482
483        self.proposal_store_mut().empty();
484        storage.delete_encryption_epoch_key_pairs(
485            self.group_id(),
486            &self.epoch(),
487            self.own_leaf_index().u32(),
488        )?;
489
490        Ok(())
491    }
492
493    // === Extensions ===
494
495    /// Exports the Ratchet Tree.
496    pub fn export_ratchet_tree(&self) -> RatchetTree {
497        self.public_group().export_ratchet_tree()
498    }
499}
500
501// Crate-public functions
502impl MlsGroup {
503    /// Get the required capabilities extension of this group.
504    pub(crate) fn required_capabilities(&self) -> Option<&RequiredCapabilitiesExtension> {
505        self.public_group.required_capabilities()
506    }
507
508    /// Get a reference to the group epoch secrets from the group
509    pub(crate) fn group_epoch_secrets(&self) -> &GroupEpochSecrets {
510        &self.group_epoch_secrets
511    }
512
513    /// Get a reference to the message secrets from a group
514    pub(crate) fn message_secrets(&self) -> &MessageSecrets {
515        self.message_secrets_store.message_secrets()
516    }
517
518    /// Sets the size of the [`MessageSecretsStore`], i.e. the number of past
519    /// epochs to keep.
520    /// This allows application messages from previous epochs to be decrypted.
521    pub(crate) fn set_max_past_epochs(&mut self, max_past_epochs: usize) {
522        self.message_secrets_store.resize(max_past_epochs);
523    }
524
525    /// Get the message secrets. Either from the secrets store or from the group.
526    pub(crate) fn message_secrets_mut(
527        &mut self,
528        epoch: GroupEpoch,
529    ) -> Result<&mut MessageSecrets, SecretTreeError> {
530        if epoch < self.context().epoch() {
531            self.message_secrets_store
532                .secrets_for_epoch_mut(epoch)
533                .ok_or(SecretTreeError::TooDistantInThePast)
534        } else {
535            Ok(self.message_secrets_store.message_secrets_mut())
536        }
537    }
538
539    /// Get the message secrets. Either from the secrets store or from the group.
540    pub(crate) fn message_secrets_for_epoch(
541        &self,
542        epoch: GroupEpoch,
543    ) -> Result<&MessageSecrets, SecretTreeError> {
544        if epoch < self.context().epoch() {
545            self.message_secrets_store
546                .secrets_for_epoch(epoch)
547                .ok_or(SecretTreeError::TooDistantInThePast)
548        } else {
549            Ok(self.message_secrets_store.message_secrets())
550        }
551    }
552
553    /// Get the message secrets and leaves for the given epoch. Either from the
554    /// secrets store or from the group.
555    ///
556    /// Note that the leaves vector is empty for message secrets of the current
557    /// epoch. The caller can use treesync in this case.
558    pub(crate) fn message_secrets_and_leaves_mut(
559        &mut self,
560        epoch: GroupEpoch,
561    ) -> Result<(&mut MessageSecrets, &[Member]), SecretTreeError> {
562        if epoch < self.context().epoch() {
563            self.message_secrets_store
564                .secrets_and_leaves_for_epoch_mut(epoch)
565                .ok_or(SecretTreeError::TooDistantInThePast)
566        } else {
567            // No need for leaves here. The tree of the current epoch is
568            // available to the caller.
569            Ok((self.message_secrets_store.message_secrets_mut(), &[]))
570        }
571    }
572
573    /// Create a new group context extension proposal
574    pub(crate) fn create_group_context_ext_proposal<Provider: OpenMlsProvider>(
575        &self,
576        framing_parameters: FramingParameters,
577        extensions: Extensions<GroupContext>,
578        signer: &impl Signer,
579    ) -> Result<AuthenticatedContent, CreateGroupContextExtProposalError<Provider::StorageError>>
580    {
581        // Ensure that the group supports all the extensions that are wanted.
582        let required_extension = extensions
583            .iter()
584            .find(|extension| extension.extension_type() == ExtensionType::RequiredCapabilities);
585        if let Some(required_extension) = required_extension {
586            let required_capabilities = required_extension.as_required_capabilities_extension()?;
587            // Ensure we support all the capabilities.
588            self.own_leaf_node()
589                .ok_or_else(|| LibraryError::custom("Tree has no own leaf."))?
590                .capabilities()
591                .supports_required_capabilities(required_capabilities)?;
592
593            // Ensure that all other leaf nodes support all the required
594            // extensions as well.
595            self.public_group()
596                .check_extension_support(required_capabilities.extension_types())?;
597        }
598        let proposal = GroupContextExtensionProposal::new(extensions);
599        let proposal = Proposal::GroupContextExtensions(Box::new(proposal));
600        AuthenticatedContent::member_proposal(
601            framing_parameters,
602            self.own_leaf_index(),
603            proposal,
604            self.context(),
605            signer,
606        )
607        .map_err(|e| e.into())
608    }
609
610    // Encrypt an AuthenticatedContent into an PrivateMessage
611    pub(crate) fn encrypt<Provider: OpenMlsProvider>(
612        &mut self,
613        public_message: AuthenticatedContent,
614        provider: &Provider,
615    ) -> Result<PrivateMessage, MessageEncryptionError<Provider::StorageError>> {
616        let padding_size = self.configuration().padding_size();
617        let msg = PrivateMessage::try_from_authenticated_content(
618            provider.crypto(),
619            provider.rand(),
620            &public_message,
621            self.ciphersuite(),
622            self.message_secrets_store.message_secrets_mut(),
623            padding_size,
624        )?;
625
626        provider
627            .storage()
628            .write_message_secrets(self.group_id(), &self.message_secrets_store)
629            .map_err(MessageEncryptionError::StorageError)?;
630
631        Ok(msg)
632    }
633
634    /// Group framing parameters
635    pub(crate) fn framing_parameters(&self) -> FramingParameters<'_> {
636        FramingParameters::new(
637            &self.aad,
638            self.mls_group_config.wire_format_policy().outgoing(),
639        )
640    }
641
642    /// Returns a reference to the proposal store.
643    pub fn proposal_store(&self) -> &ProposalStore {
644        self.public_group.proposal_store()
645    }
646
647    /// Returns a mutable reference to the proposal store.
648    pub(crate) fn proposal_store_mut(&mut self) -> &mut ProposalStore {
649        self.public_group.proposal_store_mut()
650    }
651
652    /// Get the group context
653    pub(crate) fn context(&self) -> &GroupContext {
654        self.public_group.group_context()
655    }
656
657    /// Get the MLS version used in this group.
658    pub(crate) fn version(&self) -> ProtocolVersion {
659        self.public_group.version()
660    }
661
662    /// Resets the AAD.
663    #[inline]
664    pub(crate) fn reset_aad(&mut self) {
665        self.aad.clear();
666    }
667
668    /// Returns a reference to the public group.
669    pub(crate) fn public_group(&self) -> &PublicGroup {
670        &self.public_group
671    }
672}
673
674// Private methods of MlsGroup
675impl MlsGroup {
676    /// Store the given [`EncryptionKeyPair`]s in the `provider`'s key store
677    /// indexed by this group's [`GroupId`] and [`GroupEpoch`].
678    ///
679    /// Returns an error if access to the key store fails.
680    pub(super) fn store_epoch_keypairs<Storage: StorageProvider>(
681        &self,
682        store: &Storage,
683        keypair_references: &[EncryptionKeyPair],
684    ) -> Result<(), Storage::Error> {
685        store.write_encryption_epoch_key_pairs(
686            self.group_id(),
687            &self.context().epoch(),
688            self.own_leaf_index().u32(),
689            keypair_references,
690        )
691    }
692
693    /// Read the [`EncryptionKeyPair`]s of this group and its current
694    /// [`GroupEpoch`] from the `provider`'s storage.
695    ///
696    /// Returns an error if the lookup in the [`StorageProvider`] fails.
697    pub(super) fn read_epoch_keypairs<Storage: StorageProvider>(
698        &self,
699        store: &Storage,
700    ) -> Result<Vec<EncryptionKeyPair>, Storage::Error> {
701        store.encryption_epoch_key_pairs(
702            self.group_id(),
703            &self.context().epoch(),
704            self.own_leaf_index().u32(),
705        )
706    }
707
708    /// Delete the [`EncryptionKeyPair`]s from the previous [`GroupEpoch`] from
709    /// the `provider`'s key store.
710    ///
711    /// Returns an error if access to the key store fails.
712    pub(super) fn delete_previous_epoch_keypairs<Storage: StorageProvider>(
713        &self,
714        store: &Storage,
715    ) -> Result<(), Storage::Error> {
716        store.delete_encryption_epoch_key_pairs(
717            self.group_id(),
718            &GroupEpoch::from(self.context().epoch().as_u64() - 1),
719            self.own_leaf_index().u32(),
720        )
721    }
722
723    /// Stores the state of this group. Only to be called from constructors to
724    /// store the initial state of the group.
725    pub(super) fn store<Storage: crate::storage::StorageProvider>(
726        &self,
727        storage: &Storage,
728    ) -> Result<(), Storage::Error> {
729        self.public_group.store(storage)?;
730        storage.write_group_epoch_secrets(self.group_id(), &self.group_epoch_secrets)?;
731        storage.write_own_leaf_index(self.group_id(), &self.own_leaf_index)?;
732        storage.write_message_secrets(self.group_id(), &self.message_secrets_store)?;
733        storage.write_resumption_psk_store(self.group_id(), &self.resumption_psk_store)?;
734        storage.write_mls_join_config(self.group_id(), &self.mls_group_config)?;
735        storage.write_group_state(self.group_id(), &self.group_state)?;
736        #[cfg(feature = "extensions-draft-08")]
737        if let Some(application_export_tree) = &self.application_export_tree {
738            storage.write_application_export_tree(self.group_id(), application_export_tree)?;
739        }
740
741        Ok(())
742    }
743
744    /// Converts PublicMessage to MlsMessage. Depending on whether handshake
745    /// message should be encrypted, PublicMessage messages are encrypted to
746    /// PrivateMessage first.
747    fn content_to_mls_message(
748        &mut self,
749        mls_auth_content: AuthenticatedContent,
750        provider: &impl OpenMlsProvider,
751    ) -> Result<MlsMessageOut, LibraryError> {
752        let msg = match self.configuration().wire_format_policy().outgoing() {
753            OutgoingWireFormatPolicy::AlwaysPlaintext => {
754                let mut plaintext: PublicMessage = mls_auth_content.into();
755                // Set the membership tag only if the sender type is `Member`.
756                if plaintext.sender().is_member() {
757                    plaintext.set_membership_tag(
758                        provider.crypto(),
759                        self.ciphersuite(),
760                        self.message_secrets().membership_key(),
761                        self.message_secrets().serialized_context(),
762                    )?;
763                }
764                plaintext.into()
765            }
766            OutgoingWireFormatPolicy::AlwaysCiphertext => {
767                let ciphertext = self
768                    .encrypt(mls_auth_content, provider)
769                    // We can be sure the encryption will work because the plaintext was created by us
770                    .map_err(|_| LibraryError::custom("Malformed plaintext"))?;
771                MlsMessageOut::from_private_message(ciphertext, self.version())
772            }
773        };
774        Ok(msg)
775    }
776
777    /// Check if the group is operational. Throws an error if the group is
778    /// inactive or if there is a pending commit.
779    fn is_operational(&self) -> Result<(), MlsGroupStateError> {
780        match self.group_state {
781            MlsGroupState::PendingCommit(_) => Err(MlsGroupStateError::PendingCommit),
782            MlsGroupState::Inactive => Err(MlsGroupStateError::UseAfterEviction),
783            MlsGroupState::Operational => Ok(()),
784        }
785    }
786}
787
788// Methods used in tests
789impl MlsGroup {
790    #[cfg(any(feature = "test-utils", test))]
791    pub fn export_group_context(&self) -> &GroupContext {
792        self.context()
793    }
794
795    #[cfg(any(feature = "test-utils", test))]
796    pub fn tree_hash(&self) -> &[u8] {
797        self.public_group().group_context().tree_hash()
798    }
799
800    #[cfg(any(feature = "test-utils", test))]
801    pub(crate) fn message_secrets_test_mut(&mut self) -> &mut MessageSecrets {
802        self.message_secrets_store.message_secrets_mut()
803    }
804
805    #[cfg(any(feature = "test-utils", test))]
806    pub fn print_ratchet_tree(&self, message: &str) {
807        println!("{}: {}", message, self.public_group().export_ratchet_tree());
808    }
809
810    #[cfg(any(feature = "test-utils", test))]
811    pub(crate) fn context_mut(&mut self) -> &mut GroupContext {
812        self.public_group.context_mut()
813    }
814
815    #[cfg(test)]
816    pub(crate) fn set_own_leaf_index(&mut self, own_leaf_index: LeafNodeIndex) {
817        self.own_leaf_index = own_leaf_index;
818    }
819
820    #[cfg(test)]
821    pub(crate) fn own_tree_position(&self) -> TreePosition {
822        TreePosition::new(self.group_id().clone(), self.own_leaf_index())
823    }
824
825    #[cfg(test)]
826    pub(crate) fn message_secrets_store(&self) -> &MessageSecretsStore {
827        &self.message_secrets_store
828    }
829
830    #[cfg(test)]
831    pub(crate) fn resumption_psk_store(&self) -> &ResumptionPskStore {
832        &self.resumption_psk_store
833    }
834
835    #[cfg(test)]
836    pub(crate) fn set_group_context(&mut self, group_context: GroupContext) {
837        self.public_group.set_group_context(group_context)
838    }
839
840    #[cfg(any(test, feature = "test-utils"))]
841    pub fn ensure_persistence(&self, storage: &impl StorageProvider) -> Result<(), LibraryError> {
842        let loaded = MlsGroup::load(storage, self.group_id())
843            .map_err(|_| LibraryError::custom("Failed to load group from storage"))?;
844        let other = loaded.ok_or_else(|| LibraryError::custom("Group not found in storage"))?;
845
846        if self != &other {
847            let mut diagnostics = Vec::new();
848
849            if self.mls_group_config != other.mls_group_config {
850                diagnostics.push(format!(
851                    "mls_group_config:\n  Current: {:?}\n  Loaded:  {:?}",
852                    self.mls_group_config, other.mls_group_config
853                ));
854            }
855            if self.public_group != other.public_group {
856                diagnostics.push(format!(
857                    "public_group:\n  Current: {:?}\n  Loaded:  {:?}",
858                    self.public_group, other.public_group
859                ));
860            }
861            if self.group_epoch_secrets != other.group_epoch_secrets {
862                diagnostics.push(format!(
863                    "group_epoch_secrets:\n  Current: {:?}\n  Loaded:  {:?}",
864                    self.group_epoch_secrets, other.group_epoch_secrets
865                ));
866            }
867            if self.own_leaf_index != other.own_leaf_index {
868                diagnostics.push(format!(
869                    "own_leaf_index:\n  Current: {:?}\n  Loaded:  {:?}",
870                    self.own_leaf_index, other.own_leaf_index
871                ));
872            }
873            if self.message_secrets_store != other.message_secrets_store {
874                diagnostics.push(format!(
875                    "message_secrets_store:\n  Current: {:?}\n  Loaded:  {:?}",
876                    self.message_secrets_store, other.message_secrets_store
877                ));
878            }
879            if self.resumption_psk_store != other.resumption_psk_store {
880                diagnostics.push(format!(
881                    "resumption_psk_store:\n  Current: {:?}\n  Loaded:  {:?}",
882                    self.resumption_psk_store, other.resumption_psk_store
883                ));
884            }
885            if self.own_leaf_nodes != other.own_leaf_nodes {
886                diagnostics.push(format!(
887                    "own_leaf_nodes:\n  Current: {:?}\n  Loaded:  {:?}",
888                    self.own_leaf_nodes, other.own_leaf_nodes
889                ));
890            }
891            if self.aad != other.aad {
892                diagnostics.push(format!(
893                    "aad:\n  Current: {:?}\n  Loaded:  {:?}",
894                    self.aad, other.aad
895                ));
896            }
897            if self.group_state != other.group_state {
898                diagnostics.push(format!(
899                    "group_state:\n  Current: {:?}\n  Loaded:  {:?}",
900                    self.group_state, other.group_state
901                ));
902            }
903            #[cfg(feature = "extensions-draft-08")]
904            if self.application_export_tree != other.application_export_tree {
905                diagnostics.push(format!(
906                    "application_export_tree:\n  Current: {:?}\n  Loaded:  {:?}",
907                    self.application_export_tree, other.application_export_tree
908                ));
909            }
910
911            log::error!(
912                "Loaded group does not match current group! Differing fields ({}):\n\n{}",
913                diagnostics.len(),
914                diagnostics.join("\n\n")
915            );
916
917            return Err(LibraryError::custom(
918                "Loaded group does not match current group",
919            ));
920        }
921
922        Ok(())
923    }
924}
925
926/// A [`StagedWelcome`] can be inspected and then turned into a [`MlsGroup`].
927/// This allows checking who authored the Welcome message.
928#[derive(Debug)]
929pub struct StagedWelcome {
930    // The group configuration. See [`MlsGroupJoinConfig`] for more information.
931    mls_group_config: MlsGroupJoinConfig,
932    public_group: PublicGroup,
933    group_epoch_secrets: GroupEpochSecrets,
934    own_leaf_index: LeafNodeIndex,
935
936    /// A [`MessageSecretsStore`] that stores message secrets.
937    /// By default this store has the length of 1, i.e. only the [`MessageSecrets`]
938    /// of the current epoch is kept.
939    /// If more secrets from past epochs should be kept in order to be
940    /// able to decrypt application messages from previous epochs, the size of
941    /// the store must be increased through [`max_past_epochs()`].
942    message_secrets_store: MessageSecretsStore,
943
944    /// A secret that is not stored as part of the [`MlsGroup`] after the group is created.
945    /// It can be used by the application to derive forward secure secrets.
946    #[cfg(feature = "extensions-draft-08")]
947    application_export_secret: ApplicationExportSecret,
948
949    /// Resumption psk store. This is where the resumption psks are kept in a rollover list.
950    resumption_psk_store: ResumptionPskStore,
951
952    /// The [`VerifiableGroupInfo`] from the [`Welcome`] message.
953    verifiable_group_info: VerifiableGroupInfo,
954
955    /// The key package bundle used for this welcome.
956    key_package_bundle: KeyPackageBundle,
957
958    /// If we got a path secret, these are the derived path keys.
959    path_keypairs: Option<Vec<EncryptionKeyPair>>,
960}
961
962/// A `Welcome` message that has been processed but not staged yet.
963///
964/// This may be used in order to retrieve information from the `Welcome` about
965/// the ratchet tree and PSKs.
966///
967/// Use `into_staged_welcome` to stage it into a [`StagedWelcome`].
968pub struct ProcessedWelcome {
969    // The group configuration. See [`MlsGroupJoinConfig`] for more information.
970    mls_group_config: MlsGroupJoinConfig,
971
972    // The following is the state after parsing the Welcome message, before actually
973    // building the group.
974    ciphersuite: Ciphersuite,
975    group_secrets: GroupSecrets,
976    key_schedule: crate::schedule::KeySchedule,
977    verifiable_group_info: crate::messages::group_info::VerifiableGroupInfo,
978    resumption_psk_store: crate::schedule::psk::store::ResumptionPskStore,
979    key_package_bundle: KeyPackageBundle,
980}