Skip to main content

core_crypto/transaction_context/conversation/
welcome.rs

1//! This module contains transactional conversation operations that are related to processing welcome messages.
2
3use openmls::prelude::{MlsMessageIn, MlsMessageInBody};
4
5use super::{Error, Result, TransactionContext};
6use crate::{ConversationConfiguration, ConversationId};
7
8impl TransactionContext {
9    /// Create a conversation from a received MLS Welcome message
10    ///
11    /// # Arguments
12    /// * `welcome` - a `Welcome` message received as a result of a commit adding new members to a group
13    ///
14    /// # Return type
15    /// This function will return the conversation/group id
16    ///
17    /// # Errors
18    /// Errors can be originating from the KeyStore of from OpenMls:
19    /// * if no [openmls::key_packages::KeyPackage] can be read from the KeyStore
20    /// * if the message can't be decrypted
21    #[cfg_attr(test, crate::dispotent)]
22    pub async fn process_welcome_message(&self, welcome: impl Into<MlsMessageIn>) -> Result<ConversationId> {
23        let MlsMessageInBody::Welcome(welcome) = welcome.into().extract() else {
24            return Err(Error::CallerError(
25                "the message provided to process_welcome_message was not a welcome message",
26            ));
27        };
28
29        let configuration = ConversationConfiguration {
30            cipher_suite: welcome.ciphersuite().into(),
31            ..Default::default()
32        };
33
34        let inner = self.inner()?;
35        let conversation = inner
36            .with_savepoint(
37                "process_welcome_message_savepoint",
38                async || {
39                    self.persist_conversation_from_welcome_message(welcome, configuration)
40                        .await
41                },
42                Error::savepoint,
43            )
44            .await?;
45
46        let id = conversation.id().to_owned();
47
48        Ok(id)
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use crate::test_utils::*;
55
56    #[apply(all_cred_cipher)]
57    async fn joining_from_welcome_should_prune_local_key_material(case: TestContext) {
58        let [alice, bob] = case.sessions().await;
59        Box::pin(async move {
60            // has to be before the original key_package count because it creates one
61            // Create a conversation from alice, where she invites bob
62            let commit_guard = case.create_conversation([&alice]).await.invite([&bob]).await;
63
64            // Keep track of the whatever amount was initially generated
65            let prev_count = bob.transaction.count_entities().await;
66            // Bob accepts the welcome message, and as such, it should prune the used keypackage from the store
67            commit_guard.notify_members().await;
68
69            // Ensure we're left with 1 less keypackage bundle in the store, because it was consumed with the OpenMLS
70            // Welcome message
71            let next_count = bob.transaction.count_entities().await;
72            assert_eq!(next_count.key_package, prev_count.key_package - 1);
73            assert_eq!(next_count.hpke_private_key, prev_count.hpke_private_key - 1);
74            assert_eq!(next_count.encryption_keypair, prev_count.encryption_keypair - 1);
75        })
76        .await;
77    }
78
79    #[apply(all_cred_cipher)]
80    async fn process_welcome_should_fail_when_already_exists(case: TestContext) {
81        let [alice, mut bob] = case.sessions().await;
82        Box::pin(async move {
83            let credential_ref = &bob.initial_credential;
84            let commit = case.create_conversation([&alice]).await.invite([&bob]).await;
85            let conversation = commit.conversation();
86            let id = conversation.id().clone();
87                // Meanwhile Bob creates a conversation with the exact same id as the one he's trying to join
88                bob
89                    .transaction
90                    .new_conversation(&id, credential_ref, case.cfg.clone())
91                    .await
92                    .unwrap();
93
94                // Bob's key packages before processing the welcome
95                let key_package_refs_before = bob.transaction.get_key_package_refs().await.unwrap();
96
97                let welcome = conversation.transport().await.latest_welcome_message().await;
98
99                // We need Bob's created key package to be persisted, so we can restore it on error.
100                // Assuming that key package creation happens in its own transaction matches a sufficiently large
101                // portion of real-world usage.
102                bob.commit_transaction().await;
103
104                let join_welcome = bob
105                    .transaction
106                    .process_welcome_message(welcome)
107                    .await;
108
109                // Bob's key packages after processing the welcome
110                let key_package_refs_after = bob.transaction.get_key_package_refs().await.unwrap();
111
112                assert!(!key_package_refs_before.is_empty());
113                assert_eq!(key_package_refs_before, key_package_refs_after);
114                assert!(innermost_source_matches!(join_welcome.unwrap_err(), super::Error::ConversationAlreadyExists(i) if i == &id));
115            })
116        .await;
117    }
118
119    /// Processing a welcome message makes openmls consume the key package it was addressed to; when
120    /// persisting the conversation then fails, the savepoint in `process_welcome_message` must put
121    /// that key material back.
122    ///
123    /// Unlike [`process_welcome_should_fail_when_already_exists`], this does not commit before
124    /// processing the welcome: a savepoint rolls back within the transaction which created the key
125    /// package, so restoration must work even then.
126    #[apply(all_cred_cipher)]
127    async fn failed_welcome_should_restore_key_material_in_same_transaction(case: TestContext) {
128        let [alice, bob] = case.sessions().await;
129        let credential_ref = &bob.initial_credential;
130        let commit = case.create_conversation([&alice]).await.invite([&bob]).await;
131        let conversation = commit.conversation();
132        let id = conversation.id().clone();
133
134        // Bob creates a conversation with the exact same id as the one he's trying to join, so
135        // persisting the conversation from the welcome message is bound to fail
136        bob.transaction
137            .new_conversation(&id, credential_ref, case.cfg.clone())
138            .await
139            .unwrap();
140
141        let welcome = conversation.transport().await.latest_welcome_message().await;
142
143        let count_before = bob.transaction.count_entities().await;
144        let key_package_refs_before = bob.transaction.get_key_package_refs().await.unwrap();
145        assert!(!key_package_refs_before.is_empty());
146
147        let join_welcome = bob.transaction.process_welcome_message(welcome).await;
148        assert!(innermost_source_matches!(
149            join_welcome.unwrap_err(),
150            super::Error::ConversationAlreadyExists(i) if i == &id
151        ));
152
153        // Every entity the welcome touched must be back where it was: the key package itself,
154        // its hpke init private key, and its leaf node encryption keypair.
155        let count_after = bob.transaction.count_entities().await;
156        assert_eq!(count_before, count_after);
157        let key_package_refs_after = bob.transaction.get_key_package_refs().await.unwrap();
158        assert_eq!(key_package_refs_before, key_package_refs_after);
159    }
160
161    /// Restoring the key material is only worth anything if it is complete: a key package whose
162    /// private keys were not restored is unusable. So once the reason the welcome failed is gone,
163    /// processing that same welcome message again has to succeed.
164    #[apply(all_cred_cipher)]
165    async fn restored_key_material_should_still_be_able_to_join_from_welcome(case: TestContext) {
166        let [alice, bob] = case.sessions().await;
167        let credential_ref = &bob.initial_credential;
168        let commit = case.create_conversation([&alice]).await.invite([&bob]).await;
169        let conversation = commit.conversation();
170        let id = conversation.id().clone();
171
172        // The conflicting conversation which makes the first attempt fail
173        bob.transaction
174            .new_conversation(&id, credential_ref, case.cfg.clone())
175            .await
176            .unwrap();
177
178        let welcome = conversation.transport().await.latest_welcome_message().await;
179
180        let join_welcome = bob.transaction.process_welcome_message(welcome.clone()).await;
181        assert!(innermost_source_matches!(
182            join_welcome.unwrap_err(),
183            super::Error::ConversationAlreadyExists(i) if i == &id
184        ));
185
186        // Bob gets rid of the conversation which was in the way
187        bob.transaction.conversation(&id).await.unwrap().wipe().await.unwrap();
188
189        // The restored key material is complete, so the second attempt goes through
190        let joined_id = bob.transaction.process_welcome_message(welcome).await.unwrap();
191        assert_eq!(joined_id, id);
192
193        // And the conversation Bob joined is the real thing: he can talk in it
194        let message = bob
195            .transaction
196            .conversation(&id)
197            .await
198            .unwrap()
199            .encrypt_message(b"hello")
200            .await
201            .unwrap();
202        let decrypted = conversation
203            .guard_of(&alice)
204            .await
205            .decrypt_message(&message)
206            .await
207            .unwrap();
208        assert_eq!(decrypted.as_application_message().unwrap().plaintext, b"hello");
209    }
210}