Skip to main content

core_crypto/
lib.rs

1//! Core Crypto is a wrapper on top of OpenMLS aimed to provide an ergonomic API for usage in web
2//! through Web Assembly and in mobile devices through FFI.
3//!
4//! The goal is provide a easier and less verbose API to create, manage and interact with MLS
5//! groups.
6#![doc = include_str!(env!("STRIPPED_README_PATH"))]
7#![cfg_attr(not(test), deny(missing_docs))]
8
9#[cfg(test)]
10#[macro_use]
11pub mod test_utils;
12// both imports above have to be defined at the beginning of the crate for rstest to work
13
14mod build_metadata;
15mod bytes_wrapper;
16mod ephemeral;
17mod error;
18mod identity;
19mod immutable_database;
20
21/// MLS Abstraction
22pub mod mls;
23mod mls_provider;
24/// Proteus Abstraction
25#[cfg(feature = "proteus")]
26pub mod proteus;
27#[cfg(test)]
28mod shared_database_tests;
29pub mod transaction_context;
30
31use std::sync::Arc;
32
33#[cfg(feature = "proteus")]
34use async_lock::Mutex;
35use async_lock::RwLock;
36pub(crate) use bytes_wrapper::bytes_wrapper;
37pub use core_crypto_keystore::{Database, DatabaseKey};
38#[cfg(test)]
39pub use core_crypto_macros::{dispotent, durable, idempotent};
40pub(crate) use immutable_database::ImmutableDatabase;
41pub use openmls::{
42    group::{MlsGroup, MlsGroupConfig},
43    prelude::{
44        Ciphersuite as MlsCiphersuite, GroupEpoch, KeyPackageIn, MlsMessageIn, MlsMessageInBody, Node, SignatureScheme,
45        group_info::VerifiableGroupInfo,
46    },
47};
48use wire_e2e_identity::pki_env::PkiEnvironment;
49
50pub use crate::{
51    build_metadata::{BUILD_METADATA, BuildMetadata},
52    ephemeral::{HISTORY_CLIENT_ID_PREFIX, HistorySecret},
53    error::{
54        Error, InnermostErrorMessage, KeystoreError, OpenMlsError, OpenMlsErrorKind, ProteusError, ProteusErrorKind,
55        RecursiveError, Result, TlsCodecError, ToRecursiveError,
56    },
57    identity::{WireIdentity, X509Identity},
58    mls::{
59        ExternalSender, UnknownCipherSuite,
60        cipher_suite::CipherSuite,
61        conversation::{
62            Commit, CommitBundle, ConversationConfiguration, ConversationId, DecryptedBytes, DecryptedMessage,
63            DecryptionOutcome, DeferralReason, GroupInfoBundle, GroupInfoEncryptionType, GroupInfoPayload,
64            PendingStatus, Proposal, RatchetTreeType, RejectionReason, WirePolicy,
65        },
66        credential::{
67            Credential, CredentialRef, CredentialType, FindFilters as CredentialFindFilters, x509::CertificateBundle,
68        },
69        key_package::{Keypackage, KeypackageRef},
70        session::{
71            EpochObserver, HistoryObserver, Session,
72            id::{ClientId, ClientIdRef},
73            user_id::UserId,
74        },
75    },
76    mls_provider::{CryptoProvider, EntropySeed, RawEntropySeed, RustCrypto},
77    transaction_context::{
78        e2e_identity::conversation_state::E2eiConversationState, key_package::KEYPACKAGE_DEFAULT_LIFETIME,
79    },
80};
81
82/// An entity / data which has been packaged by the application to be encrypted
83/// and transmitted in an application message.
84#[derive(Debug, derive_more::From, derive_more::Deref, serde::Serialize, serde::Deserialize)]
85pub struct TransportData(pub Vec<u8>);
86
87/// Client callbacks to allow communication with the delivery service.
88/// There are two different endpoints, one for messages and one for commit bundles.
89#[cfg_attr(target_os = "unknown", async_trait::async_trait(?Send))]
90#[cfg_attr(not(target_os = "unknown"), async_trait::async_trait)]
91pub trait MlsTransport: std::fmt::Debug + Send + Sync {
92    /// Send a commit bundle to the corresponding endpoint.
93    async fn send_commit_bundle(&self, commit_bundle: CommitBundle) -> Result<()>;
94
95    /// This function will be called before a history secret is sent to the mls transport to allow
96    /// the application to package it in a suitable transport container (json, protobuf, ...).
97    ///
98    /// The `secret` parameter contain the history client's secrets which will be sent over the mls transport.
99    ///
100    /// Returns the history secret packaged for transport
101    async fn prepare_for_transport(&self, secret: &HistorySecret) -> Result<TransportData>;
102}
103
104/// This provider is mainly used for the initialization of the history client session, the only case where transport
105/// doesn't need to be implemented.
106#[derive(Debug, Default)]
107pub struct CoreCryptoTransportNotImplementedProvider();
108
109#[cfg_attr(target_os = "unknown", async_trait::async_trait(?Send))]
110#[cfg_attr(not(target_os = "unknown"), async_trait::async_trait)]
111impl MlsTransport for CoreCryptoTransportNotImplementedProvider {
112    async fn send_commit_bundle(&self, _commit_bundle: CommitBundle) -> crate::Result<()> {
113        Err(Error::MlsTransportNotProvided)
114    }
115
116    async fn prepare_for_transport(&self, _secret: &HistorySecret) -> crate::Result<TransportData> {
117        Err(Error::MlsTransportNotProvided)
118    }
119}
120
121/// Wrapper superstruct for both [mls::session::Session] and [proteus::ProteusCentral]
122///
123/// As [std::ops::Deref] is implemented, this struct is automatically dereferred to [mls::session::Session] apart from
124/// `proteus_*` calls
125#[derive(Debug)]
126pub struct CoreCrypto {
127    database: Arc<Database>,
128    pki_environment: RwLock<Option<Arc<PkiEnvironment>>>,
129    mls: RwLock<Option<Arc<mls::session::Session>>>,
130    #[cfg(feature = "proteus")]
131    proteus: Mutex<Option<proteus::ProteusCentral>>,
132    #[cfg(not(feature = "proteus"))]
133    #[expect(dead_code)]
134    proteus: (),
135}
136
137impl CoreCrypto {
138    /// Create an new CoreCrypto client without any initialized session.
139    pub fn new(database: Arc<Database>) -> Arc<Self> {
140        Self {
141            database,
142            pki_environment: Default::default(),
143            mls: Default::default(),
144            proteus: Default::default(),
145        }
146        .into()
147    }
148
149    /// Set the session's PKI Environment
150    pub async fn set_pki_environment(&self, pki_environment: Option<Arc<PkiEnvironment>>) {
151        *self.pki_environment.write().await = pki_environment.clone();
152        if let Some(mls_session) = self.mls.read().await.as_ref() {
153            mls_session.crypto_provider.set_pki_environment(pki_environment).await;
154        }
155    }
156
157    /// Get the session's PKI Environment
158    pub async fn get_pki_environment(&self) -> Option<Arc<PkiEnvironment>> {
159        self.pki_environment.read().await.clone()
160    }
161
162    /// Get the mls session if initialized
163    pub async fn mls_session(&self) -> Result<Arc<Session>> {
164        match self.mls.read().await.as_ref() {
165            Some(session) => Ok(session.clone()),
166            None => Err(RecursiveError::context("Getting mls session")(mls::session::Error::MlsNotInitialized).into()),
167        }
168    }
169
170    /// Get the database
171    pub fn database(&self) -> ImmutableDatabase {
172        self.database.clone().into()
173    }
174}