Skip to main content

openmls/framing/
validation.rs

1//! # Validation steps for incoming messages
2//!
3//! ```text
4//!
5//!                             MlsMessageIn
6//!                                  │                    -.
7//!                                  │                      │
8//!                                  │                      │
9//!                                  ▼                      │
10//!                           DecryptedMessage              +-- parse_message
11//!                                  │                      │
12//!                                  │                      │
13//!                                  │                      │
14//!                                  ▼                    -'
15//!                           UnverifiedMessage
16//!                                  │                    -.
17//!                                  │                      │
18//!                                  │                      +-- process_unverified_message
19//!                                  │                      │
20//!                                  ▼                    -'
21//!                          ProcessedMessage
22//!
23//! ```
24
25use openmls_traits::{crypto::OpenMlsCrypto, types::Ciphersuite};
26use proposal_store::QueuedProposal;
27
28use crate::{
29    binary_tree::LeafNodeIndex,
30    ciphersuite::signable::Verifiable,
31    error::LibraryError,
32    extensions::ExternalSendersExtension,
33    group::{errors::ValidationError, mls_group::staged_commit::StagedCommit},
34    tree::sender_ratchet::SenderRatchetConfiguration,
35    versions::ProtocolVersion,
36};
37
38#[cfg(feature = "extensions-draft-08")]
39use crate::{
40    component::ComponentId, framing::safe_aad::SafeAad, messages::proposals_in::ProposalOrRefIn,
41};
42
43use super::{
44    mls_auth_content::AuthenticatedContent,
45    mls_auth_content_in::{AuthenticatedContentIn, VerifiableAuthenticatedContentIn},
46    private_message_in::PrivateMessageIn,
47    public_message_in::PublicMessageIn,
48    *,
49};
50
51/// Intermediate message that can be constructed either from a public message or from private message.
52/// If it it constructed from a ciphertext message, the ciphertext message is decrypted first.
53/// This function implements the following checks:
54///  - ValSem005
55///  - ValSem007
56///  - ValSem009
57#[derive(Debug)]
58pub(crate) struct DecryptedMessage {
59    verifiable_content: VerifiableAuthenticatedContentIn,
60    /// Recovered sender emulation-group leaf index for an application
61    /// message from a sibling emulator client.
62    #[cfg(feature = "virtual-clients-draft")]
63    emulator_sender_leaf_index: Option<LeafNodeIndex>,
64}
65
66impl DecryptedMessage {
67    /// Constructs a [DecryptedMessage] from a [VerifiableAuthenticatedContent].
68    pub(crate) fn from_inbound_public_message<'a>(
69        public_message: PublicMessageIn,
70        message_secrets_option: impl Into<Option<&'a MessageSecrets>>,
71        serialized_context: Vec<u8>,
72        crypto: &impl OpenMlsCrypto,
73        ciphersuite: Ciphersuite,
74    ) -> Result<Self, ValidationError> {
75        if public_message.sender().is_member() {
76            // ValSem007 Membership tag presence
77            if public_message.membership_tag().is_none() {
78                return Err(ValidationError::MissingMembershipTag);
79            }
80
81            if let Some(message_secrets) = message_secrets_option.into() {
82                // Verify the membership tag. This needs to be done explicitly for PublicMessage messages,
83                // it is implicit for PrivateMessage messages (because the encryption can only be known by members).
84                // ValSem008
85                // https://validation.openmls.tech/#valn1302
86                public_message.verify_membership(
87                    crypto,
88                    ciphersuite,
89                    message_secrets.membership_key(),
90                    message_secrets.serialized_context(),
91                )?;
92            }
93        }
94
95        let verifiable_content = public_message.into_verifiable_content(serialized_context);
96
97        // Public messages don't carry a reuse_guard, so no emulator
98        // sender leaf index to recover.
99        Self::from_verifiable_content(
100            verifiable_content,
101            #[cfg(feature = "virtual-clients-draft")]
102            None,
103        )
104    }
105
106    /// Constructs a [DecryptedMessage] from a [PrivateMessage] by attempting to decrypt it
107    /// to a [VerifiableAuthenticatedContent] first.
108    pub(crate) fn from_inbound_ciphertext(
109        ciphertext: PrivateMessageIn,
110        crypto: &impl OpenMlsCrypto,
111        group: &mut MlsGroup,
112        sender_ratchet_configuration: &SenderRatchetConfiguration,
113        #[cfg(feature = "virtual-clients-draft")] emulator_ctx: Option<
114            &crate::framing::private_message::EmulatorReuseGuardCtx<'_>,
115        >,
116    ) -> Result<Self, ValidationError> {
117        // This will be refactored with #265.
118        let ciphersuite = group.ciphersuite();
119        // TODO: #819 The old leaves should not be needed any more.
120        //       Revisit when the transition is further along.
121        let (message_secrets, _old_leaves) = group
122            .message_secrets_and_leaves(ciphertext.epoch())
123            .map_err(MessageDecryptionError::SecretTreeError)?;
124        let sender_data = ciphertext.sender_data(message_secrets, crypto, ciphersuite)?;
125        // Check if we are the sender. With the `virtual-clients` feature,
126        // decrypting own messages is allowed, so we skip this check
127        #[cfg(not(feature = "virtual-clients-draft"))]
128        if sender_data.leaf_index == group.own_leaf_index() {
129            return Err(ValidationError::CannotDecryptOwnMessage);
130        }
131        #[cfg(feature = "virtual-clients-draft")]
132        let effective_emulator_ctx = match emulator_ctx {
133            Some(ctx) if sender_data.leaf_index == group.own_leaf_index() => Some(ctx),
134            _ => None,
135        };
136        let message_secrets = group
137            .message_secrets_for_epoch_mut(ciphertext.epoch())
138            .map_err(|_| MessageDecryptionError::AeadError)?;
139        let decrypted = ciphertext.to_verifiable_content(
140            ciphersuite,
141            crypto,
142            message_secrets,
143            sender_data.leaf_index,
144            sender_ratchet_configuration,
145            sender_data,
146            #[cfg(feature = "virtual-clients-draft")]
147            effective_emulator_ctx,
148        )?;
149        Self::from_verifiable_content(
150            decrypted.verifiable,
151            #[cfg(feature = "virtual-clients-draft")]
152            decrypted.emulator_sender_leaf_index,
153        )
154    }
155
156    // Internal constructor function. Does the following checks:
157    // - Confirmation tag must be present for Commit messages
158    // - Membership tag must be present for member messages, if the original incoming message was not an PrivateMessage
159    // - Ensures application messages were originally PrivateMessage messages
160    fn from_verifiable_content(
161        verifiable_content: VerifiableAuthenticatedContentIn,
162        #[cfg(feature = "virtual-clients-draft")] emulator_sender_leaf_index: Option<LeafNodeIndex>,
163    ) -> Result<Self, ValidationError> {
164        // ValSem009
165        if verifiable_content.content_type() == ContentType::Commit
166            && verifiable_content.confirmation_tag().is_none()
167        {
168            return Err(ValidationError::MissingConfirmationTag);
169        }
170        // ValSem005
171        if verifiable_content.content_type() == ContentType::Application {
172            if verifiable_content.wire_format() != WireFormat::PrivateMessage {
173                return Err(ValidationError::UnencryptedApplicationMessage);
174            } else if !verifiable_content.sender().is_member() {
175                // This should not happen because the sender of an PrivateMessage should always be a member
176                return Err(LibraryError::custom("Expected sender to be member.").into());
177            }
178        }
179        Ok(DecryptedMessage {
180            verifiable_content,
181            #[cfg(feature = "virtual-clients-draft")]
182            emulator_sender_leaf_index,
183        })
184    }
185
186    /// Gets the correct credential from the message depending on the sender type.
187    ///
188    /// The closure argument is used to look up the credential and signature key. If the epoch of
189    /// the message is the same as that of the group, look it up in the tree; else, look in up in
190    /// the past trees of the message secret store.
191    ///
192    /// Checks the following semantic validation:
193    ///  - ValSem112
194    ///  - ValSem245
195    ///  - Prepares ValSem246 by setting the right credential. The remainder
196    ///    of ValSem246 is validated as part of ValSem010.
197    ///  - [valn1301](https://validation.openmls.tech/#valn1301)
198    ///
199    /// Returns the [`Credential`] and the leaf's [`SignaturePublicKey`].
200    pub(crate) fn credential(
201        &self,
202        look_up_credential_with_key: impl Fn(LeafNodeIndex) -> Option<CredentialWithKey>,
203        external_senders: Option<&ExternalSendersExtension>,
204    ) -> Result<CredentialWithKey, ValidationError> {
205        let sender = self.sender();
206        match sender {
207            Sender::Member(leaf_index) => {
208                // https://validation.openmls.tech/#valn1306
209                look_up_credential_with_key(*leaf_index).ok_or(ValidationError::UnknownMember)
210            }
211            Sender::External(index) => {
212                let sender = external_senders
213                    .ok_or(ValidationError::NoExternalSendersExtension)?
214                    .get(index.index())
215                    .ok_or(ValidationError::UnauthorizedExternalSender)?;
216                Ok(CredentialWithKey {
217                    credential: sender.credential().clone(),
218                    signature_key: sender.signature_key().clone(),
219                })
220            }
221            Sender::NewMemberCommit | Sender::NewMemberProposal => {
222                // Fetch the credential from the message itself.
223                // https://validation.openmls.tech/#valn0407
224                self.verifiable_content.new_member_credential()
225            }
226        }
227    }
228
229    /// Returns the sender.
230    pub fn sender(&self) -> &Sender {
231        self.verifiable_content.sender()
232    }
233
234    /// Returns the [`VerifiableAuthenticatedContent`].
235    pub(crate) fn verifiable_content(&self) -> &VerifiableAuthenticatedContentIn {
236        &self.verifiable_content
237    }
238}
239
240/// Result of [`UnverifiedMessage::verify`].
241pub(crate) struct VerifiedMessage {
242    pub(crate) content: AuthenticatedContent,
243    pub(crate) credential: Credential,
244    #[cfg(feature = "virtual-clients-draft")]
245    pub(crate) emulator_sender_leaf_index: Option<LeafNodeIndex>,
246}
247
248/// Context that is needed to verify the signature of a the leaf node of an
249/// UpdatePath or an update proposal.
250#[derive(Debug, Clone)]
251pub(crate) enum SenderContext {
252    Member((GroupId, LeafNodeIndex)),
253    ExternalCommit {
254        group_id: GroupId,
255        leftmost_blank_index: LeafNodeIndex,
256        self_removes_in_store: Vec<SelfRemoveInStore>,
257    },
258}
259
260/// Partially checked and potentially decrypted message (if it was originally encrypted).
261/// Use this to inspect the [`Credential`] of the message sender
262/// and the optional `aad` if the original message was encrypted.
263/// The [`OpenMlsSignaturePublicKey`] is used to verify the signature of the
264/// message.
265#[derive(Debug, Clone)]
266pub struct UnverifiedMessage {
267    verifiable_content: VerifiableAuthenticatedContentIn,
268    credential: Credential,
269    sender_pk: OpenMlsSignaturePublicKey,
270    sender_context: Option<SenderContext>,
271    /// See [`DecryptedMessage::emulator_sender_leaf_index`].
272    #[cfg(feature = "virtual-clients-draft")]
273    emulator_sender_leaf_index: Option<LeafNodeIndex>,
274}
275
276impl UnverifiedMessage {
277    /// Construct an [UnverifiedMessage] from a [DecryptedMessage] and an optional [Credential].
278    pub(crate) fn from_decrypted_message(
279        decrypted_message: DecryptedMessage,
280        credential: Credential,
281        sender_pk: OpenMlsSignaturePublicKey,
282        sender_context: Option<SenderContext>,
283    ) -> Self {
284        #[cfg(feature = "virtual-clients-draft")]
285        let emulator_sender_leaf_index = decrypted_message.emulator_sender_leaf_index;
286        UnverifiedMessage {
287            verifiable_content: decrypted_message.verifiable_content,
288            credential,
289            sender_pk,
290            sender_context,
291            #[cfg(feature = "virtual-clients-draft")]
292            emulator_sender_leaf_index,
293        }
294    }
295
296    /// Verify the [`UnverifiedMessage`].
297    pub(crate) fn verify(
298        self,
299        ciphersuite: Ciphersuite,
300        crypto: &impl OpenMlsCrypto,
301        protocol_version: ProtocolVersion,
302    ) -> Result<VerifiedMessage, ValidationError> {
303        let content: AuthenticatedContentIn = self
304            .verifiable_content
305            .verify(crypto, &self.sender_pk)
306            .map_err(|_| ValidationError::InvalidSignature)?;
307        // https://validation.openmls.tech/#valn1302
308        // https://validation.openmls.tech/#valn1304
309        let content =
310            content.validate(ciphersuite, crypto, self.sender_context, protocol_version)?;
311        Ok(VerifiedMessage {
312            content,
313            credential: self.credential,
314            #[cfg(feature = "virtual-clients-draft")]
315            emulator_sender_leaf_index: self.emulator_sender_leaf_index,
316        })
317    }
318
319    /// Get the proposals of the commit, if it is one. If not, return `None`.
320    #[cfg(feature = "extensions-draft-08")]
321    pub fn committed_proposals(&self) -> Option<&[ProposalOrRefIn]> {
322        self.verifiable_content.committed_proposals()
323    }
324}
325
326/// A message that has passed all syntax and semantics checks.
327#[derive(Debug)]
328pub struct ProcessedMessage {
329    group_id: GroupId,
330    epoch: GroupEpoch,
331    sender: Sender,
332    authenticated_data: Vec<u8>,
333    content: ProcessedMessageContent,
334    credential: Credential,
335    /// See [`Self::emulator_sender_leaf_index`].
336    #[cfg(feature = "virtual-clients-draft")]
337    emulator_sender_leaf_index: Option<LeafNodeIndex>,
338    /// Parsed Safe AAD prefix, populated only when the message's GroupContext
339    /// required Safe AAD framing. `None` otherwise.
340    #[cfg(feature = "extensions-draft-08")]
341    safe_aad: Option<SafeAad>,
342    /// Length in bytes of the Safe AAD prefix at the start of
343    /// `authenticated_data`. Zero when [`Self::safe_aad`] is `None`.
344    #[cfg(feature = "extensions-draft-08")]
345    safe_aad_prefix_len: usize,
346}
347
348impl ProcessedMessage {
349    /// Create a new `ProcessedMessage`.
350    pub(crate) fn new(
351        group_id: GroupId,
352        epoch: GroupEpoch,
353        sender: Sender,
354        authenticated_data: Vec<u8>,
355        content: ProcessedMessageContent,
356        credential: Credential,
357        #[cfg(feature = "virtual-clients-draft")] emulator_sender_leaf_index: Option<LeafNodeIndex>,
358    ) -> Self {
359        Self {
360            group_id,
361            epoch,
362            sender,
363            authenticated_data,
364            content,
365            credential,
366            #[cfg(feature = "virtual-clients-draft")]
367            emulator_sender_leaf_index,
368            #[cfg(feature = "extensions-draft-08")]
369            safe_aad: None,
370            #[cfg(feature = "extensions-draft-08")]
371            safe_aad_prefix_len: 0,
372        }
373    }
374
375    /// Parse the Safe AAD prefix at the start of `authenticated_data` and
376    /// attach it to this message. Callers should invoke this only when the receiving
377    /// group's GroupContext requires Safe AAD framing. Otherwise, `safe_aad`
378    /// stays `None` and `authenticated_data` is the caller-supplied bytes
379    /// untouched.
380    #[cfg(feature = "extensions-draft-08")]
381    pub(crate) fn try_attach_safe_aad(&mut self) -> Result<(), crate::framing::SafeAadError> {
382        let (safe_aad, prefix_len) =
383            crate::framing::safe_aad::parse_authenticated_data_prefix(&self.authenticated_data)?;
384        self.safe_aad = Some(safe_aad);
385        self.safe_aad_prefix_len = prefix_len;
386        Ok(())
387    }
388
389    /// Returns the parsed Safe AAD struct, or `None` if Safe AAD was not
390    /// active for the group this message belongs to.
391    #[cfg(feature = "extensions-draft-08")]
392    pub fn safe_aad(&self) -> Option<&SafeAad> {
393        self.safe_aad.as_ref()
394    }
395
396    /// Look up a Safe AAD item by [`ComponentId`].
397    #[cfg(feature = "extensions-draft-08")]
398    pub fn safe_aad_item(&self, component_id: crate::component::ComponentId) -> Option<&[u8]> {
399        self.safe_aad
400            .as_ref()
401            .and_then(|safe_aad| safe_aad.get(component_id))
402    }
403
404    /// Returns the bytes of `authenticated_data` after any Safe AAD prefix.
405    /// Equal to [`Self::aad`] when no Safe AAD prefix is present.
406    #[cfg(feature = "extensions-draft-08")]
407    pub fn tail_aad(&self) -> &[u8] {
408        &self.authenticated_data[self.safe_aad_prefix_len..]
409    }
410
411    /// Returns the sender's leaf index in the emulation group when this
412    /// message is a private message from a sibling emulator client.
413    #[cfg(feature = "virtual-clients-draft")]
414    pub fn emulator_sender_leaf_index(&self) -> Option<LeafNodeIndex> {
415        self.emulator_sender_leaf_index
416    }
417
418    /// Returns the group ID of the message.
419    pub fn group_id(&self) -> &GroupId {
420        &self.group_id
421    }
422
423    /// Returns the epoch of the message.
424    pub fn epoch(&self) -> GroupEpoch {
425        self.epoch
426    }
427
428    /// Returns the sender of the message.
429    pub fn sender(&self) -> &Sender {
430        &self.sender
431    }
432
433    /// Returns the additional authenticated data (AAD) of the message.
434    pub fn aad(&self) -> &[u8] {
435        &self.authenticated_data
436    }
437
438    /// Returns the content of the message.
439    pub fn content(&self) -> &ProcessedMessageContent {
440        &self.content
441    }
442
443    /// Returns the content of the message and consumes the message.
444    pub fn into_content(self) -> ProcessedMessageContent {
445        self.content
446    }
447
448    /// Returns the credential of the message.
449    pub fn credential(&self) -> &Credential {
450        &self.credential
451    }
452
453    /// Safely export a value if the content of the processed message is a
454    /// [`StagedCommit`].
455    #[cfg(feature = "extensions-draft-08")]
456    pub fn safe_export_secret<Crypto: OpenMlsCrypto>(
457        &mut self,
458        crypto: &Crypto,
459        component_id: ComponentId,
460    ) -> Result<Vec<u8>, ProcessedMessageSafeExportSecretError> {
461        if let ProcessedMessageContent::StagedCommitMessage(ref mut staged_commit) =
462            &mut self.content
463        {
464            let secret = staged_commit.safe_export_secret(crypto, component_id)?;
465            Ok(secret)
466        } else {
467            Err(ProcessedMessageSafeExportSecretError::NotACommit)
468        }
469    }
470}
471
472/// Content of a processed message.
473///
474/// See the content variants' documentation for more information.
475/// [`StagedCommit`] and [`QueuedProposal`] can be inspected for authorization purposes.
476#[derive(Debug)]
477pub enum ProcessedMessageContent {
478    /// An application message.
479    ///
480    /// The [`ApplicationMessage`] contains a vector of bytes that can be used right-away.
481    ApplicationMessage(ApplicationMessage),
482    /// A standalone proposal.
483    ///
484    /// The [`QueuedProposal`] can be inspected for authorization purposes by the application.
485    /// If the proposal is deemed to be allowed, it should be added to the group's proposal
486    /// queue using [`MlsGroup::store_pending_proposal()`](crate::group::mls_group::MlsGroup::store_pending_proposal()).
487    ProposalMessage(Box<QueuedProposal>),
488    /// An [external join proposal](crate::prelude::JoinProposal) sent by a
489    /// [NewMemberProposal](crate::prelude::Sender::NewMemberProposal) sender which is outside the group.
490    ///
491    /// Since this originates from a party outside the group, the [`QueuedProposal`] SHOULD be
492    /// inspected for authorization purposes by the application. If the proposal is deemed to be
493    /// allowed, it should be added to the group's proposal queue using
494    /// [`MlsGroup::store_pending_proposal()`](crate::group::mls_group::MlsGroup::store_pending_proposal()).
495    ExternalJoinProposalMessage(Box<QueuedProposal>),
496    /// A Commit message.
497    ///
498    /// The [`StagedCommit`] can be inspected for authorization purposes by the application.
499    /// If the type of the commit and the proposals it covers are deemed to be allowed,
500    /// the commit should be merged into the group's state using
501    /// [`MlsGroup::merge_staged_commit()`](crate::group::mls_group::MlsGroup::merge_staged_commit()).
502    StagedCommitMessage(Box<StagedCommit>),
503}
504
505/// Application message received through a [ProcessedMessage].
506#[derive(Debug, PartialEq, Eq)]
507pub struct ApplicationMessage {
508    bytes: Vec<u8>,
509}
510
511impl ApplicationMessage {
512    /// Create a new [ApplicationMessage].
513    pub(crate) fn new(bytes: Vec<u8>) -> Self {
514        Self { bytes }
515    }
516
517    /// Returns the inner bytes and consumes the [`ApplicationMessage`].
518    pub fn into_bytes(self) -> Vec<u8> {
519        self.bytes
520    }
521}