Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crypto-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ log-reload.workspace = true
log.workspace = true
mls-crypto-provider.workspace = true
obfuscate = { workspace = true, features = ["openmls"] }
paste = "1.0.15"
proteus-wasm = { workspace = true, optional = true }
rmp-serde.workspace = true
serde_json.workspace = true
Expand Down
11 changes: 5 additions & 6 deletions crypto-ffi/src/bundles/commit.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::sync::Arc;

use core_crypto::MlsCommitBundle;

use crate::{
CoreCryptoError, GroupInfoBundle,
core_crypto_context::mls::{WelcomeMaybeArc, welcome_coerce_maybe_arc},
};
use crate::{CoreCryptoError, GroupInfoBundle, core_crypto_context::mls::Welcome};

/// Information returned when a commit is created.
#[derive(uniffi::Record)]
pub struct CommitBundle {
/// A welcome message if there are pending Add proposals
pub welcome: Option<WelcomeMaybeArc>,
pub welcome: Option<Arc<Welcome>>,
/// The commit message
pub commit: Vec<u8>,
/// `GroupInfo` if the commit is merged
Expand All @@ -24,7 +23,7 @@ impl TryFrom<MlsCommitBundle> for CommitBundle {
fn try_from(msg: MlsCommitBundle) -> Result<Self, Self::Error> {
let encrypted_message = msg.encrypted_message.clone();
let (welcome, commit, group_info) = msg.to_bytes_triple()?;
let welcome = welcome.map(welcome_coerce_maybe_arc);
let welcome = welcome.map(Welcome::from).map(Arc::new);
let group_info = group_info.into();
Ok(Self {
welcome,
Expand Down
8 changes: 5 additions & 3 deletions crypto-ffi/src/bundles/group_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::Arc;

use core_crypto::MlsGroupInfoBundle;

use crate::core_crypto_context::mls::{GroupInfoMaybeArc, group_info_coerce_maybe_arc};
use crate::core_crypto_context::mls::GroupInfo;

#[derive(Debug, Clone, Copy, uniffi::Enum)]
#[repr(u8)]
Expand Down Expand Up @@ -68,15 +70,15 @@ pub struct GroupInfoBundle {
/// What kind of ratchet tree is used
pub ratchet_tree_type: MlsRatchetTreeType,
/// The group info
pub payload: GroupInfoMaybeArc,
pub payload: Arc<GroupInfo>,
}

impl From<MlsGroupInfoBundle> for GroupInfoBundle {
fn from(gi: MlsGroupInfoBundle) -> Self {
Self {
encryption_type: gi.encryption_type.into(),
ratchet_tree_type: gi.ratchet_tree_type.into(),
payload: group_info_coerce_maybe_arc(gi.payload.bytes()),
payload: Arc::new(gi.payload.bytes().into()),
}
}
}
8 changes: 5 additions & 3 deletions crypto-ffi/src/bundles/welcome.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::{ConversationIdMaybeArc, conversation_id_coerce_maybe_arc, crl::NewCrlDistributionPoints};
use std::sync::Arc;

use crate::{ConversationId, crl::NewCrlDistributionPoints};

/// see [core_crypto::WelcomeBundle]
#[derive(Debug, uniffi::Record)]
pub struct WelcomeBundle {
/// Identifier of the joined conversation
pub id: ConversationIdMaybeArc,
pub id: Arc<ConversationId>,
/// New CRL Distribution of members of this group
pub crl_new_distribution_points: NewCrlDistributionPoints,
}
Expand All @@ -16,8 +18,8 @@ impl From<core_crypto::WelcomeBundle> for WelcomeBundle {
crl_new_distribution_points,
}: core_crypto::WelcomeBundle,
) -> Self {
let id = conversation_id_coerce_maybe_arc(id);
let crl_new_distribution_points = crl_new_distribution_points.into();
let id = Arc::new(id.into());
Self {
id,
crl_new_distribution_points,
Expand Down
11 changes: 0 additions & 11 deletions crypto-ffi/src/bytes_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,6 @@ macro_rules! bytes_wrapper {
&self.0
}
}

paste::paste! {
#[allow(dead_code)]
pub(crate) type [<$id MaybeArc>] = std::sync::Arc<$id>;

#[allow(dead_code)]
#[inline]
pub(crate) fn [<$id:snake _coerce_maybe_arc>]<'a>(value: impl Into<std::borrow::Cow<'a, [u8]>>) -> [<$id MaybeArc>] {
std::sync::Arc::new($id(value.into().into_owned()))
}
}
};
}

Expand Down
12 changes: 3 additions & 9 deletions crypto-ffi/src/client_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#[derive(Debug, Clone, Eq, Hash, PartialEq, derive_more::From, uniffi::Object)]
pub struct ClientId(pub(crate) core_crypto::ClientId);

pub(crate) type ClientIdMaybeArc = std::sync::Arc<ClientId>;

#[uniffi::export]
impl ClientId {
/// Instantiate a client id from a byte array.
Expand All @@ -22,12 +20,8 @@ impl ClientId {
}
}

impl ClientId {
pub(crate) fn as_cc(&self) -> core_crypto::ClientId {
self.0.clone()
}

pub(crate) fn from_cc(id: core_crypto::ClientId) -> ClientIdMaybeArc {
std::sync::Arc::new(ClientId(id))
impl AsRef<core_crypto::ClientIdRef> for ClientId {
fn as_ref(&self) -> &core_crypto::ClientIdRef {
core_crypto::ClientIdRef::new(&self.0)
}
}
6 changes: 3 additions & 3 deletions crypto-ffi/src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::time::Duration;
use std::{sync::Arc, time::Duration};

use core_crypto::MlsCustomConfiguration;

use crate::{Ciphersuite, core_crypto_context::mls::ExternalSenderKeyMaybeArc};
use crate::{Ciphersuite, core_crypto_context::mls::ExternalSenderKey};

/// See [core_crypto::MlsWirePolicy]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
Expand Down Expand Up @@ -67,7 +67,7 @@ pub struct ConversationConfiguration {
/// The ciphersuite used in the group
pub ciphersuite: Option<Ciphersuite>,
/// Delivery service public signature key and credential
pub external_senders: Vec<ExternalSenderKeyMaybeArc>,
pub external_senders: Vec<Arc<ExternalSenderKey>>,
/// Implementation specific configuration
pub custom: CustomConfiguration,
}
11 changes: 5 additions & 6 deletions crypto-ffi/src/core_crypto/conversation.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::borrow::Borrow;
use std::{borrow::Borrow, sync::Arc};

use core_crypto::{
RecursiveError,
mls::conversation::{Conversation as _, ConversationIdRef},
};

use crate::{
Ciphersuite, ClientId, CoreCryptoFfi, CoreCryptoResult, bytes_wrapper::bytes_wrapper, client_id::ClientIdMaybeArc,
};
use crate::{Ciphersuite, ClientId, CoreCryptoFfi, CoreCryptoResult, bytes_wrapper::bytes_wrapper};

bytes_wrapper!(
/// A unique identifier for a single conversation.
Expand Down Expand Up @@ -65,7 +63,7 @@ impl CoreCryptoFfi {
}

/// See [core_crypto::mls::conversation::Conversation::get_client_ids]
pub async fn get_client_ids(&self, conversation_id: &ConversationId) -> CoreCryptoResult<Vec<ClientIdMaybeArc>> {
pub async fn get_client_ids(&self, conversation_id: &ConversationId) -> CoreCryptoResult<Vec<Arc<ClientId>>> {
let conversation = self
.inner
.get_raw_conversation(conversation_id.as_ref())
Expand All @@ -75,7 +73,8 @@ impl CoreCryptoFfi {
.get_client_ids()
.await
.into_iter()
.map(ClientId::from_cc)
.map(Into::into)
.map(Arc::new)
.collect())
}

Expand Down
8 changes: 4 additions & 4 deletions crypto-ffi/src/core_crypto/e2ei/identities.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;
use std::{collections::HashMap, sync::Arc};

use core_crypto::{RecursiveError, mls::conversation::Conversation as _};

use crate::{ConversationId, CoreCryptoFfi, CoreCryptoResult, WireIdentity, client_id::ClientIdMaybeArc};
use crate::{ClientId, ConversationId, CoreCryptoFfi, CoreCryptoResult, WireIdentity};

type DeviceIdentities = Vec<WireIdentity>;

Expand All @@ -14,14 +14,14 @@ impl CoreCryptoFfi {
pub async fn get_device_identities(
&self,
conversation_id: &ConversationId,
device_ids: Vec<ClientIdMaybeArc>,
device_ids: Vec<Arc<ClientId>>,
) -> CoreCryptoResult<DeviceIdentities> {
let conversation = self
.inner
.get_raw_conversation(conversation_id.as_ref())
.await
.map_err(RecursiveError::mls_client("getting raw conversation"))?;
let device_ids = device_ids.into_iter().map(|id| id.as_cc()).collect::<Vec<_>>();
let device_ids = device_ids.iter().map(|c| c.as_ref().as_ref()).collect::<Vec<_>>();
let wire_identities = conversation
.get_device_identities(&device_ids)
.await?
Expand Down
11 changes: 4 additions & 7 deletions crypto-ffi/src/core_crypto/epoch_observer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use std::sync::Arc;

use ::core_crypto::ConversationId;
use async_trait::async_trait;

use crate::{
ConversationIdMaybeArc, CoreCryptoError, CoreCryptoFfi, CoreCryptoResult, conversation_id_coerce_maybe_arc,
};
use crate::{ConversationId, CoreCryptoError, CoreCryptoFfi, CoreCryptoResult};

#[derive(Debug, thiserror::Error, uniffi::Error)]
#[uniffi(flat_error)]
Expand Down Expand Up @@ -35,7 +32,7 @@ pub trait EpochObserver: Send + Sync {
/// and ignore internal errors instead of propagating them, to the maximum extent possible.
async fn epoch_changed(
&self,
conversation_id: ConversationIdMaybeArc,
conversation_id: Arc<ConversationId>,
epoch: u64,
) -> Result<(), EpochChangedReportingError>;
}
Expand All @@ -50,10 +47,10 @@ struct ObserverShim(Arc<dyn EpochObserver>);
#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
impl core_crypto::mls::EpochObserver for ObserverShim {
async fn epoch_changed(&self, conversation_id: ConversationId, epoch: u64) {
async fn epoch_changed(&self, conversation_id: core_crypto::ConversationId, epoch: u64) {
if let Err(err) = self
.0
.epoch_changed(conversation_id_coerce_maybe_arc(&conversation_id), epoch)
.epoch_changed(Arc::new(ConversationId(conversation_id.as_ref().to_owned())), epoch)
.await
{
// we don't _care_ if an error is thrown by the notification function, per se,
Expand Down
16 changes: 8 additions & 8 deletions crypto-ffi/src/core_crypto/history_observer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use std::sync::Arc;

use async_trait::async_trait;
use core_crypto::ConversationId;

use crate::{
ConversationIdMaybeArc, CoreCryptoError, CoreCryptoFfi, CoreCryptoResult, HistorySecret,
conversation_id_coerce_maybe_arc,
};
use crate::{ConversationId, CoreCryptoError, CoreCryptoFfi, CoreCryptoResult, HistorySecret};

#[derive(Debug, thiserror::Error, uniffi::Error)]
#[uniffi(flat_error)]
Expand Down Expand Up @@ -36,7 +32,7 @@ pub trait HistoryObserver: Send + Sync {
/// and ignore internal errors instead of propagating them, to the maximum extent possible.
async fn history_client_created(
&self,
conversation_id: ConversationIdMaybeArc,
conversation_id: Arc<ConversationId>,
secret: HistorySecret,
) -> Result<(), NewHistoryClientReportingError>;
}
Expand All @@ -51,7 +47,11 @@ struct ObserverShim(Arc<dyn HistoryObserver>);
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
impl core_crypto::mls::HistoryObserver for ObserverShim {
async fn history_client_created(&self, conversation_id: ConversationId, secret: &core_crypto::HistorySecret) {
async fn history_client_created(
&self,
conversation_id: core_crypto::ConversationId,
secret: &core_crypto::HistorySecret,
) {
let Ok(secret) = HistorySecret::try_from(secret) else {
// weird that we couldn't convert this but ¯\_(ツ)_/¯
log::warn!(
Expand All @@ -61,7 +61,7 @@ impl core_crypto::mls::HistoryObserver for ObserverShim {
};
if let Err(err) = self
.0
.history_client_created(conversation_id_coerce_maybe_arc(&conversation_id), secret)
.history_client_created(Arc::new(ConversationId(conversation_id.clone().into())), secret)
.await
{
// we don't _care_ if an error is thrown by the notification function, per se,
Expand Down
2 changes: 1 addition & 1 deletion crypto-ffi/src/core_crypto/mls_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl core_crypto::MlsTransport for MlsTransportShim {
&self,
secret: &HistorySecret,
) -> core_crypto::Result<core_crypto::MlsTransportData> {
let client_id = ClientId::from_cc(secret.client_id.clone());
let client_id = ClientId::from(secret.client_id.clone()).into();
let history_secret = rmp_serde::to_vec(&secret)
.map(|secret| HistorySecretFfi {
client_id,
Expand Down
13 changes: 6 additions & 7 deletions crypto-ffi/src/core_crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ mod proteus;
#[cfg(feature = "wasm")]
mod randomness;

use std::sync::Arc;

use core_crypto::Session;

use crate::{
CoreCryptoResult,
database::{DatabaseMaybeArc, ToCc as _},
};
use crate::{CoreCryptoResult, Database};

/// CoreCrypto wraps around MLS and Proteus implementations and provides a transactional interface for each.
#[derive(Debug, uniffi::Object)]
Expand All @@ -31,17 +30,17 @@ pub struct CoreCryptoFfi {
/// MLS or proteus can be initialized with [core_crypto::transaction_context::TransactionContext::mls_init] or
/// [core_crypto::transaction_context::TransactionContext::proteus_init], respectively.
#[uniffi::export]
pub async fn core_crypto_new(database: &DatabaseMaybeArc) -> CoreCryptoResult<CoreCryptoFfi> {
pub async fn core_crypto_new(database: &Arc<Database>) -> CoreCryptoResult<CoreCryptoFfi> {
CoreCryptoFfi::new(database).await
}

impl CoreCryptoFfi {
/// Instantiate CC
pub async fn new(database: &DatabaseMaybeArc) -> CoreCryptoResult<Self> {
pub async fn new(database: &Arc<Database>) -> CoreCryptoResult<Self> {
#[cfg(target_family = "wasm")]
console_error_panic_hook::set_once();

let client = Session::try_new(database.as_cc()).await?;
let client = Session::try_new(database).await?;
let inner = core_crypto::CoreCrypto::from(client);

Ok(Self { inner })
Expand Down
9 changes: 4 additions & 5 deletions crypto-ffi/src/core_crypto_context/e2ei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::{collections::HashMap, sync::Arc};
use core_crypto::{mls::conversation::Conversation as _, transaction_context::Error as TransactionError};

use crate::{
Ciphersuite, ConversationId, CoreCryptoContext, CoreCryptoError, CoreCryptoResult, CrlRegistration,
E2eiConversationState, E2eiEnrollment, UserIdentities, WireIdentity, client_id::ClientIdMaybeArc,
crl::NewCrlDistributionPoints,
Ciphersuite, ClientId, ConversationId, CoreCryptoContext, CoreCryptoError, CoreCryptoResult, CrlRegistration,
E2eiConversationState, E2eiEnrollment, UserIdentities, WireIdentity, crl::NewCrlDistributionPoints,
};

type EnrollmentParameter = Arc<E2eiEnrollment>;
Expand Down Expand Up @@ -188,9 +187,9 @@ impl CoreCryptoContext {
pub async fn get_device_identities(
&self,
conversation_id: &ConversationId,
device_ids: Vec<ClientIdMaybeArc>,
device_ids: Vec<Arc<ClientId>>,
) -> CoreCryptoResult<Vec<WireIdentity>> {
let device_ids = device_ids.into_iter().map(|cid| cid.as_cc()).collect::<Vec<_>>();
let device_ids = device_ids.iter().map(|c| c.as_ref().as_ref()).collect::<Vec<_>>();

let conversation = self.inner.conversation(conversation_id.as_ref()).await?;
let wire_ids = conversation.get_device_identities(device_ids.as_slice()).await?;
Expand Down
Loading
Loading