core_crypto/
ephemeral.rs

1//! Utilities for ephemeral CoreCrypto instances.
2//!
3//! Ephemeral instances are intended to support history sharing. History sharing works like this:
4//! every history-enabled conversation has a passive "history client" as a member. This client
5//! is a member of the MLS group (and can therefore decrypt messages), but it is not actively running
6//! on any device or decrypting any messages.
7//!
8//! Approximately once daily, and whenever a member is removed from the group, a new history-sharing era
9//! begins. The client submitting the commit which instantiates the new history-sharing era is responsible
10//! for ensuring that the old history client is removed from the group, and new one is added. Additionally,
11//! one of the first application messages in the new history-sharing era contains the serialized history
12//! secret.
13//!
14//! When a new client joins the history-enabled conversation, they receive a list of history secrets
15//! and their associated history-sharing eras (identified by the epoch number at which they start).
16//! For each history-sharing era, they can instantiate an ephemeral client from the history secret,
17//! and use that client to decrypt all messages in this era.
18//!
19//! Though ephemeral clients are full instances of `CoreCrypto` and contain the same API, they cannot
20//! be used to generate messages for sending, as they don't posess a credential with a signature key:
21//! Any attempt to encrypt a message will fail because the client cannot retrieve the signature key from
22//! its keystore.
23
24use core_crypto_keystore::{ConnectionType, Database};
25use mls_crypto_provider::DatabaseKey;
26use obfuscate::{Obfuscate, Obfuscated};
27use openmls::prelude::KeyPackageSecretEncapsulation;
28
29use crate::{
30    ClientId, ClientIdentifier, CoreCrypto, Error, MlsCiphersuite, MlsCredentialType, MlsError, RecursiveError, Result,
31    Session, SessionConfig,
32};
33
34/// We always instantiate history clients with this prefix in their client id, so
35/// we can use prefix testing to determine with some accuracy whether or not something is a history client.
36pub const HISTORY_CLIENT_ID_PREFIX: &str = "history-client";
37
38/// A `HistorySecret` encodes sufficient client state that it can be used to instantiate an
39/// ephemeral client.
40#[derive(serde::Serialize, serde::Deserialize)]
41pub struct HistorySecret {
42    /// Client id of the associated history client
43    pub client_id: ClientId,
44    pub(crate) key_package: KeyPackageSecretEncapsulation,
45}
46
47impl Obfuscate for HistorySecret {
48    fn obfuscate(&self, f: &mut std::fmt::Formatter<'_>) -> core::fmt::Result {
49        f.debug_struct("HistorySecret")
50            .field("client_id", &self.client_id)
51            .field("key_package", &Obfuscated::from(&self.key_package))
52            .finish()
53    }
54}
55
56/// Create a new [`CoreCrypto`] with an **uninitialized** mls session.
57///
58/// You must initialize the session yourself before using this!
59async fn in_memory_cc_with_ciphersuite(ciphersuite: impl Into<MlsCiphersuite>) -> Result<CoreCrypto> {
60    let db = Database::open(ConnectionType::InMemory, &DatabaseKey::generate())
61        .await
62        .unwrap();
63    let config = SessionConfig::builder()
64        .ciphersuites([ciphersuite.into()])
65        .database(db)
66        .build()
67        .validate()
68        .map_err(RecursiveError::mls("validating ephemeral session configuration"))?;
69
70    // Construct the MLS session, but don't initialize it. The implementation when `client_id` is `None` just
71    // does construction, which is what we need.
72    let session = Session::try_new(config)
73        .await
74        .map_err(RecursiveError::mls("creating ephemeral session"))?;
75
76    Ok(session.into())
77}
78
79/// Generate a new [`HistorySecret`].
80///
81/// This is useful when it's this client's turn to generate a new history client.
82///
83/// The generated secret is cryptographically unrelated to the current CoreCrypto client.
84///
85/// Note that this is a crate-private function; the public interface for this feature is
86/// [`Conversation::generate_history_secret`][core_crypto::mls::conversation::Conversation::generate_history_secret].
87/// This implementation lives here instead of there for organizational reasons.
88pub(crate) async fn generate_history_secret(ciphersuite: MlsCiphersuite) -> Result<HistorySecret> {
89    // generate a new completely arbitrary client id
90    let client_id = uuid::Uuid::new_v4();
91    let client_id = format!("{HISTORY_CLIENT_ID_PREFIX}-{client_id}");
92    let client_id = ClientId::from(client_id.into_bytes());
93    let identifier = ClientIdentifier::Basic(client_id.clone());
94
95    let cc = in_memory_cc_with_ciphersuite(ciphersuite).await?;
96    let tx = cc
97        .new_transaction()
98        .await
99        .map_err(RecursiveError::transaction("creating new transaction"))?;
100    cc.init(identifier, &[ciphersuite], &cc.crypto_provider)
101        .await
102        .map_err(RecursiveError::mls_client("initializing ephemeral cc"))?;
103
104    // we can generate a key package from the ephemeral cc and ciphersutite
105    let [key_package] = tx
106        .get_or_create_client_keypackages(ciphersuite, MlsCredentialType::Basic, 1)
107        .await
108        .map_err(RecursiveError::transaction("generating keypackages"))?
109        .try_into()
110        .expect("generating 1 keypackage returns 1 keypackage");
111    let key_package = KeyPackageSecretEncapsulation::load(&cc.crypto_provider, key_package)
112        .await
113        .map_err(MlsError::wrap("encapsulating key package"))?;
114
115    // we don't need to finish the transaction here--the point of the ephemeral CC was that no mutations would be saved there
116
117    Ok(HistorySecret { client_id, key_package })
118}
119
120pub(crate) fn is_history_client(client_id: &ClientId) -> bool {
121    client_id.starts_with(HISTORY_CLIENT_ID_PREFIX.as_bytes())
122}
123
124impl CoreCrypto {
125    /// Instantiate a history client.
126    ///
127    /// This client exposes the full interface of `CoreCrypto`, but it should only be used to decrypt messages.
128    /// Other use is a logic error.
129    pub async fn history_client(history_secret: HistorySecret) -> Result<Self> {
130        if !history_secret
131            .client_id
132            .starts_with(HISTORY_CLIENT_ID_PREFIX.as_bytes())
133        {
134            return Err(Error::InvalidHistorySecret("client id has invalid format"));
135        }
136
137        let session = in_memory_cc_with_ciphersuite(history_secret.key_package.ciphersuite()).await?;
138        let tx = session
139            .new_transaction()
140            .await
141            .map_err(RecursiveError::transaction("creating new transaction"))?;
142
143        session
144            .restore_from_history_secret(history_secret)
145            .await
146            .map_err(RecursiveError::mls_client(
147                "restoring ephemeral session from history secret",
148            ))?;
149
150        tx.finish()
151            .await
152            .map_err(RecursiveError::transaction("finishing transaction"))?;
153
154        Ok(session)
155    }
156}
157
158#[cfg(test)]
159mod tests {
160    use rstest::rstest;
161    use rstest_reuse::apply;
162
163    use crate::test_utils::{TestContext, all_cred_cipher};
164
165    /// Create a history secret, and restore it into a CoreCrypto instance
166    #[apply(all_cred_cipher)]
167    async fn can_create_ephemeral_client(case: TestContext) {
168        let [alice] = case.sessions().await;
169        let conversation = case
170            .create_conversation([&alice])
171            .await
172            .enable_history_sharing_notify()
173            .await;
174
175        assert_eq!(
176            conversation.member_count().await,
177            2,
178            "the convesation should now magically have a second member"
179        );
180
181        let ephemeral_client = conversation.members().nth(1).unwrap();
182        assert!(
183            conversation.can_one_way_communicate(&alice, ephemeral_client).await,
184            "alice can send messages to the history client"
185        );
186        assert!(
187            !conversation.can_one_way_communicate(ephemeral_client, &alice).await,
188            "the history client cannot send messages"
189        );
190    }
191}