Skip to content

Commit

Permalink
refactor(crypto-ffi): core-crypto-ffi builds given new errors in wasm
Browse files Browse the repository at this point in the history
Weirdly, this was _much_ simpler than doing so for non-wasm. I'll take
it though!
  • Loading branch information
coriolinus committed Dec 13, 2024
1 parent f1c2ded commit 0971126
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 115 deletions.
39 changes: 23 additions & 16 deletions crypto-ffi/src/wasm/context/e2ei.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
use crate::wasm::context::CoreCryptoContext;
use crate::wasm::E2eiConversationState;
use crate::{
Ciphersuite, CommitBundle, CoreCryptoError, CredentialType, CrlRegistration, E2eiDumpedPkiEnv, E2eiEnrollment,
RotateBundle, WasmCryptoResult, WireIdentity,
use std::{
collections::HashMap,
ops::{Deref, DerefMut},
};

use core_crypto::{
prelude::{CiphersuiteName, ClientId, ConversationId, MlsCiphersuite, VerifiableGroupInfo},
RecursiveError,
};
use core_crypto::prelude::{CiphersuiteName, ClientId, ConversationId, MlsCiphersuite, VerifiableGroupInfo};
use core_crypto::{CryptoError, MlsError};
use futures_util::TryFutureExt;
use js_sys::{Promise, Uint8Array};
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
use tls_codec::Deserialize;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use wasm_bindgen_futures::future_to_promise;

use crate::{
wasm::{context::CoreCryptoContext, E2eiConversationState},
Ciphersuite, CommitBundle, CoreCryptoError, CredentialType, CrlRegistration, E2eiDumpedPkiEnv, E2eiEnrollment,
InternalError, RotateBundle, WasmCryptoResult, WireIdentity,
};

#[wasm_bindgen]
impl CoreCryptoContext {
/// Returns: [`WasmCryptoResult<E2eiEnrollment>`]
Expand Down Expand Up @@ -183,7 +187,7 @@ impl CoreCryptoContext {
let nb_key_package = nb_key_package
.map(usize::try_from)
.transpose()
.map_err(CryptoError::from)?;
.expect("we never run corecrypto on systems with architectures narrower than 32 bits");

let crls = context
.e2ei_mls_init_only(
Expand Down Expand Up @@ -249,7 +253,9 @@ impl CoreCryptoContext {
future_to_promise(
async move {
let enrollment = std::sync::Arc::try_unwrap(enrollment.0)
.map_err(|_| CryptoError::LockPoisonError)?
.map_err(|_| {
InternalError::Other("enrollment had multiple strong refs and could not be unwrapped".into())
})?
.into_inner();
let handle = context.e2ei_enrollment_stash(enrollment).await?;
WasmCryptoResult::Ok(Uint8Array::from(handle.as_slice()).into())
Expand Down Expand Up @@ -365,9 +371,10 @@ impl CoreCryptoContext {
future_to_promise(
async move {
let group_info = VerifiableGroupInfo::tls_deserialize(&mut group_info.as_ref())
.map_err(MlsError::from)
.map_err(CryptoError::from)
.map_err(CoreCryptoError::from)?;
.map_err(core_crypto::mls::conversation::Error::tls_deserialize(
"verifiable group info",
))
.map_err(RecursiveError::mls_conversation("getting credential in use"))?;

let state: E2eiConversationState = context
.get_credential_in_use(group_info, credential_type.into())
Expand Down
55 changes: 26 additions & 29 deletions crypto-ffi/src/wasm/context/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use crate::wasm::{lower_ciphersuites, InternalError};
use crate::{
wasm::{lower_ciphersuites, InternalError},
BufferedDecryptedMessage, Ciphersuite, CommitBundle, ConversationConfiguration, ConversationInitBundle, CoreCrypto,
CoreCryptoError, CoreCryptoResult, CredentialType, CustomConfiguration, DecryptedMessage, FfiClientId,
MemberAddedMessages, ProposalBundle, WasmCryptoResult, WelcomeBundle,
};
use core_crypto::context::CentralContext;
use core_crypto::prelude::{
CiphersuiteName, ClientId, ClientIdentifier, ConversationId, KeyPackageIn, KeyPackageRef,
MlsConversationConfiguration, VerifiableGroupInfo,
use core_crypto::{
context::CentralContext,
prelude::{
CiphersuiteName, ClientId, ClientIdentifier, ConversationId, KeyPackageIn, KeyPackageRef,
MlsConversationConfiguration, VerifiableGroupInfo,
},
RecursiveError,
};
use core_crypto::{CryptoError, CryptoResult, MlsError};
use futures_util::TryFutureExt;
use js_sys::{Promise, Uint8Array};
use std::sync::Arc;
use tls_codec::{Deserialize, Serialize};
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use wasm_bindgen_futures::future_to_promise;

pub mod e2ei;
Expand Down Expand Up @@ -107,7 +108,7 @@ impl CoreCryptoContext {
let nb_key_package = nb_key_package
.map(usize::try_from)
.transpose()
.map_err(CryptoError::from)?;
.expect("we never run corecrypto on systems with architectures narrower than 32 bits");
context
.mls_init(
ClientIdentifier::Basic(client_id.clone().into()),
Expand Down Expand Up @@ -212,16 +213,13 @@ impl CoreCryptoContext {
credential_type.into(),
amount_requested as usize,
)
.await?
.await
.map_err(RecursiveError::mls_client("getting or creating client keypackage"))?
.into_iter()
.map(|kpb| {
kpb.tls_serialize_detached()
.map_err(MlsError::from)
.map_err(CryptoError::from)
.map(Into::into)
})
.collect::<CryptoResult<Vec<Vec<u8>>>>()
.map_err(CoreCryptoError::from)?;
.map(|kpb| kpb.tls_serialize_detached())
.collect::<Result<Vec<Vec<u8>>, _>>()
.map_err(core_crypto::mls::conversation::Error::tls_serialize("keypackages"))
.map_err(RecursiveError::mls_conversation("serializing client keypackages"))?;

let js_kps = js_sys::Array::from_iter(
kps.into_iter()
Expand All @@ -245,7 +243,7 @@ impl CoreCryptoContext {
let count = context
.client_valid_key_packages_count(ciphersuite.into(), credential_type.into())
.await
.map_err(CoreCryptoError::from)?;
.map_err(RecursiveError::mls_client("counting valid client keypackages"))?;
WasmCryptoResult::Ok(count.into())
}
.err_into(),
Expand All @@ -269,7 +267,7 @@ impl CoreCryptoContext {
context
.delete_keypackages(&refs[..])
.await
.map_err(CoreCryptoError::from)?;
.map_err(RecursiveError::mls_client("deleting keypackages"))?;
WasmCryptoResult::Ok(JsValue::UNDEFINED)
}
.err_into(),
Expand Down Expand Up @@ -564,9 +562,8 @@ impl CoreCryptoContext {
future_to_promise(
async move {
let kp = KeyPackageIn::tls_deserialize(&mut keypackage.as_ref())
.map_err(MlsError::from)
.map_err(CryptoError::from)
.map_err(CoreCryptoError::from)?;
.map_err(core_crypto::mls::conversation::Error::tls_deserialize("keypackage"))
.map_err(RecursiveError::mls_conversation("creating new add proposal"))?;

let proposal: ProposalBundle = context
.new_add_proposal(&conversation_id.to_vec(), kp.into())
Expand Down Expand Up @@ -640,9 +637,8 @@ impl CoreCryptoContext {
.map_err(CoreCryptoError::from)?
.to_bytes()
.map(|bytes| Uint8Array::from(bytes.as_slice()))
.map_err(MlsError::from)
.map_err(CryptoError::from)
.map_err(CoreCryptoError::from)?;
.map_err(core_crypto::MlsError::wrap("creating new external add proposal"))
.map_err(core_crypto::Error::Mls)?;

WasmCryptoResult::Ok(proposal_bytes.into())
}
Expand All @@ -664,9 +660,10 @@ impl CoreCryptoContext {
future_to_promise(
async move {
let group_info = VerifiableGroupInfo::tls_deserialize(&mut group_info.as_ref())
.map_err(MlsError::from)
.map_err(CryptoError::from)
.map_err(CoreCryptoError::from)?;
.map_err(core_crypto::mls::conversation::Error::tls_deserialize(
"verifiable group info",
))
.map_err(RecursiveError::mls_conversation("joining by external commit"))?;

let result: ConversationInitBundle = context
.join_by_external_commit(group_info, custom_configuration.into(), credential_type.into())
Expand Down
Loading

0 comments on commit 0971126

Please sign in to comment.