diff --git a/Cargo.lock b/Cargo.lock index 95ceea15..66248d6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -409,6 +409,7 @@ dependencies = [ "uniffi", "uuid", "wasm-bindgen", + "wasm-bindgen-futures", "wiremock", "zeroize", "zxcvbn", diff --git a/crates/bitwarden-core/Cargo.toml b/crates/bitwarden-core/Cargo.toml index 46490539..b2eb774e 100644 --- a/crates/bitwarden-core/Cargo.toml +++ b/crates/bitwarden-core/Cargo.toml @@ -24,6 +24,7 @@ uniffi = ["bitwarden-crypto/uniffi", "dep:uniffi"] # Uniffi bindings wasm = [ "bitwarden-error/wasm", "dep:wasm-bindgen", + "dep:wasm-bindgen-futures", "dep:tsify-next" ] # WASM support @@ -49,6 +50,7 @@ tsify-next = { workspace = true, optional = true } uniffi = { workspace = true, optional = true, features = ["tokio"] } uuid = { workspace = true } wasm-bindgen = { workspace = true, optional = true } +wasm-bindgen-futures = { workspace = true, optional = true } zeroize = { version = ">=1.7.0, <2.0", features = ["derive", "aarch64"] } zxcvbn = { version = ">=3.0.1, <4.0", optional = true } diff --git a/crates/bitwarden-core/src/mobile/crypto.rs b/crates/bitwarden-core/src/mobile/crypto.rs index 9f6765cc..f5ad5b18 100644 --- a/crates/bitwarden-core/src/mobile/crypto.rs +++ b/crates/bitwarden-core/src/mobile/crypto.rs @@ -11,6 +11,7 @@ use bitwarden_crypto::{ AsymmetricCryptoKey, CryptoError, EncString, Kdf, KeyDecryptable, KeyEncryptable, MasterKey, SymmetricCryptoKey, UnsignedSharedKey, UserKey, }; +use bitwarden_error::bitwarden_error; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; #[cfg(feature = "wasm")] @@ -24,6 +25,7 @@ use crate::{ /// Catch all error for mobile crypto operations. #[allow(missing_docs)] +#[bitwarden_error(flat)] #[derive(Debug, thiserror::Error)] pub enum MobileCryptoError { #[error(transparent)] @@ -255,6 +257,7 @@ pub(super) async fn get_user_encryption_key(client: &Client) -> Result Result { + make_key_pair(user_key) + } + + /// Verifies a user's asymmetric keys by decrypting the private key with the provided user + /// key. Returns if the private key is decryptable and if it is a valid matching key. + /// Crypto initialization not required. + pub fn verify_asymmetric_keys( + &self, + request: VerifyAsymmetricKeysRequest, + ) -> Result { + verify_asymmetric_keys(request) + } +} + +impl CryptoClient { /// Get the uses's decrypted encryption key. Note: It's very important /// to keep this key safe, as it can be used to decrypt all of the user's data pub async fn get_user_encryption_key(&self) -> Result { @@ -86,21 +108,6 @@ impl CryptoClient { ) -> Result { derive_key_connector(request) } - - /// Generates a new key pair and encrypts the private key with the provided user key. - pub fn make_key_pair(&self, user_key: String) -> Result { - make_key_pair(user_key) - } - - /// Verifies a user's asymmetric keys by decrypting the private key with the provided user - /// key. Returns if the private key is decryptable and if it is a valid matching key. - /// Crypto initialization not required. - pub fn verify_asymmetric_keys( - &self, - request: VerifyAsymmetricKeysRequest, - ) -> Result { - verify_asymmetric_keys(request) - } } impl Client { diff --git a/crates/bitwarden-wasm-internal/src/client.rs b/crates/bitwarden-wasm-internal/src/client.rs index 66fcfcc4..d8f56601 100644 --- a/crates/bitwarden-wasm-internal/src/client.rs +++ b/crates/bitwarden-wasm-internal/src/client.rs @@ -1,13 +1,13 @@ extern crate console_error_panic_hook; use std::fmt::Display; -use bitwarden_core::{Client, ClientSettings}; +use bitwarden_core::{mobile::CryptoClient, Client, ClientSettings}; use bitwarden_error::bitwarden_error; use bitwarden_exporters::ExporterClientExt; use bitwarden_vault::VaultClientExt; use wasm_bindgen::prelude::*; -use crate::{CryptoClient, GeneratorClient, VaultClient}; +use crate::{GeneratorClient, VaultClient}; #[wasm_bindgen] pub struct BitwardenClient(pub(crate) Client); @@ -41,7 +41,7 @@ impl BitwardenClient { } pub fn crypto(&self) -> CryptoClient { - CryptoClient::new(self.0.crypto()) + self.0.crypto() } pub fn vault(&self) -> VaultClient { diff --git a/crates/bitwarden-wasm-internal/src/crypto.rs b/crates/bitwarden-wasm-internal/src/crypto.rs deleted file mode 100644 index 044ffbce..00000000 --- a/crates/bitwarden-wasm-internal/src/crypto.rs +++ /dev/null @@ -1,55 +0,0 @@ -use bitwarden_core::{ - client::encryption_settings::EncryptionSettingsError, - mobile::crypto::{ - InitOrgCryptoRequest, InitUserCryptoRequest, MakeKeyPairResponse, - VerifyAsymmetricKeysRequest, VerifyAsymmetricKeysResponse, - }, -}; -use bitwarden_crypto::CryptoError; -use wasm_bindgen::prelude::*; - -#[wasm_bindgen] -pub struct CryptoClient(bitwarden_core::mobile::CryptoClient); - -impl CryptoClient { - pub fn new(client: bitwarden_core::mobile::CryptoClient) -> Self { - Self(client) - } -} - -#[wasm_bindgen] -impl CryptoClient { - /// Initialization method for the user crypto. Needs to be called before any other crypto - /// operations. - pub async fn initialize_user_crypto( - &self, - req: InitUserCryptoRequest, - ) -> Result<(), EncryptionSettingsError> { - self.0.initialize_user_crypto(req).await - } - - /// Initialization method for the organization crypto. Needs to be called after - /// `initialize_user_crypto` but before any other crypto operations. - pub async fn initialize_org_crypto( - &self, - req: InitOrgCryptoRequest, - ) -> Result<(), EncryptionSettingsError> { - self.0.initialize_org_crypto(req).await - } - - /// Generates a new key pair and encrypts the private key with the provided user key. - /// Crypto initialization not required. - pub fn make_key_pair(&self, user_key: String) -> Result { - self.0.make_key_pair(user_key) - } - - /// Verifies a user's asymmetric keys by decrypting the private key with the provided user - /// key. Returns if the private key is decryptable and if it is a valid matching key. - /// Crypto initialization not required. - pub fn verify_asymmetric_keys( - &self, - request: VerifyAsymmetricKeysRequest, - ) -> Result { - self.0.verify_asymmetric_keys(request) - } -} diff --git a/crates/bitwarden-wasm-internal/src/lib.rs b/crates/bitwarden-wasm-internal/src/lib.rs index 1e21f6ec..82f30ce0 100644 --- a/crates/bitwarden-wasm-internal/src/lib.rs +++ b/crates/bitwarden-wasm-internal/src/lib.rs @@ -1,7 +1,6 @@ #![doc = include_str!("../README.md")] mod client; -mod crypto; mod custom_types; mod generators; mod init; @@ -11,7 +10,6 @@ mod vault; pub use bitwarden_ipc::wasm::*; pub use client::BitwardenClient; -pub use crypto::CryptoClient; pub use generators::GeneratorClient; pub use init::init_sdk; pub use vault::{folders::FoldersClient, VaultClient};