Skip to content

Commit cbb1373

Browse files
committed
Lift mobile crypto into key_management, and rename error
1 parent 1bb505d commit cbb1373

File tree

11 files changed

+33
-29
lines changed

11 files changed

+33
-29
lines changed

crates/bitwarden-core/src/auth/auth_request.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ mod tests {
140140
use bitwarden_crypto::{Kdf, MasterKey};
141141

142142
use super::*;
143-
use crate::{
144-
key_management::SymmetricKeyId,
145-
mobile::crypto::{AuthRequestMethod, InitUserCryptoMethod, InitUserCryptoRequest},
143+
use crate::key_management::{
144+
crypto::{AuthRequestMethod, InitUserCryptoMethod, InitUserCryptoRequest},
145+
SymmetricKeyId,
146146
};
147147

148148
#[test]

crates/bitwarden-core/src/auth/login/auth_request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
auth_request::new_auth_request,
1313
},
1414
client::{LoginMethod, UserLoginMethod},
15-
mobile::crypto::{AuthRequestMethod, InitUserCryptoMethod, InitUserCryptoRequest},
15+
key_management::crypto::{AuthRequestMethod, InitUserCryptoMethod, InitUserCryptoRequest},
1616
require, ApiError, Client,
1717
};
1818

crates/bitwarden-core/src/client/test_accounts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::HashMap;
44
use bitwarden_crypto::Kdf;
55

66
use crate::{
7-
mobile::crypto::{
7+
key_management::crypto::{
88
initialize_org_crypto, initialize_user_crypto, InitOrgCryptoRequest, InitUserCryptoMethod,
99
InitUserCryptoRequest,
1010
},

crates/bitwarden-core/src/mobile/crypto.rs renamed to crates/bitwarden-core/src/key_management/crypto.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{
2727
#[allow(missing_docs)]
2828
#[bitwarden_error(flat)]
2929
#[derive(Debug, thiserror::Error)]
30-
pub enum MobileCryptoError {
30+
pub enum CryptoClientError {
3131
#[error(transparent)]
3232
NotAuthenticated(#[from] NotAuthenticatedError),
3333
#[error(transparent)]
@@ -248,7 +248,7 @@ pub(crate) async fn initialize_org_crypto(
248248
Ok(())
249249
}
250250

251-
pub(super) async fn get_user_encryption_key(client: &Client) -> Result<String, MobileCryptoError> {
251+
pub(super) async fn get_user_encryption_key(client: &Client) -> Result<String, CryptoClientError> {
252252
let key_store = client.internal.get_key_store();
253253
let ctx = key_store.context();
254254
// This is needed because the mobile clients need access to the user encryption key
@@ -273,7 +273,7 @@ pub struct UpdatePasswordResponse {
273273
pub(super) fn update_password(
274274
client: &Client,
275275
new_password: String,
276-
) -> Result<UpdatePasswordResponse, MobileCryptoError> {
276+
) -> Result<UpdatePasswordResponse, CryptoClientError> {
277277
let key_store = client.internal.get_key_store();
278278
let ctx = key_store.context();
279279
// FIXME: [PM-18099] Once MasterKey deals with KeyIds, this should be updated
@@ -323,7 +323,7 @@ pub struct DerivePinKeyResponse {
323323
pub(super) fn derive_pin_key(
324324
client: &Client,
325325
pin: String,
326-
) -> Result<DerivePinKeyResponse, MobileCryptoError> {
326+
) -> Result<DerivePinKeyResponse, CryptoClientError> {
327327
let key_store = client.internal.get_key_store();
328328
let ctx = key_store.context();
329329
// FIXME: [PM-18099] Once PinKey deals with KeyIds, this should be updated
@@ -346,7 +346,7 @@ pub(super) fn derive_pin_key(
346346
pub(super) fn derive_pin_user_key(
347347
client: &Client,
348348
encrypted_pin: EncString,
349-
) -> Result<EncString, MobileCryptoError> {
349+
) -> Result<EncString, CryptoClientError> {
350350
let key_store = client.internal.get_key_store();
351351
let ctx = key_store.context();
352352
// FIXME: [PM-18099] Once PinKey deals with KeyIds, this should be updated
@@ -366,7 +366,7 @@ fn derive_pin_protected_user_key(
366366
pin: &str,
367367
login_method: &LoginMethod,
368368
user_key: &SymmetricCryptoKey,
369-
) -> Result<EncString, MobileCryptoError> {
369+
) -> Result<EncString, CryptoClientError> {
370370
use bitwarden_crypto::PinKey;
371371

372372
let derived_key = match login_method {

crates/bitwarden-core/src/mobile/crypto_client.rs renamed to crates/bitwarden-core/src/key_management/crypto_client.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use bitwarden_crypto::{EncString, UnsignedSharedKey};
55
use wasm_bindgen::prelude::*;
66

77
use super::crypto::{
8-
derive_key_connector, make_key_pair, verify_asymmetric_keys, DeriveKeyConnectorError,
9-
DeriveKeyConnectorRequest, EnrollAdminPasswordResetError, MakeKeyPairResponse,
10-
MobileCryptoError, VerifyAsymmetricKeysRequest, VerifyAsymmetricKeysResponse,
8+
derive_key_connector, make_key_pair, verify_asymmetric_keys, CryptoClientError,
9+
DeriveKeyConnectorError, DeriveKeyConnectorRequest, EnrollAdminPasswordResetError,
10+
MakeKeyPairResponse, VerifyAsymmetricKeysRequest, VerifyAsymmetricKeysResponse,
1111
};
1212
#[cfg(feature = "internal")]
13-
use crate::mobile::crypto::{
13+
use crate::key_management::crypto::{
1414
derive_pin_key, derive_pin_user_key, enroll_admin_password_reset, get_user_encryption_key,
1515
initialize_org_crypto, initialize_user_crypto, update_password, DerivePinKeyResponse,
1616
InitOrgCryptoRequest, InitUserCryptoRequest, UpdatePasswordResponse,
@@ -63,7 +63,7 @@ impl CryptoClient {
6363
impl CryptoClient {
6464
/// Get the uses's decrypted encryption key. Note: It's very important
6565
/// to keep this key safe, as it can be used to decrypt all of the user's data
66-
pub async fn get_user_encryption_key(&self) -> Result<String, MobileCryptoError> {
66+
pub async fn get_user_encryption_key(&self) -> Result<String, CryptoClientError> {
6767
get_user_encryption_key(&self.client).await
6868
}
6969

@@ -72,14 +72,14 @@ impl CryptoClient {
7272
pub fn update_password(
7373
&self,
7474
new_password: String,
75-
) -> Result<UpdatePasswordResponse, MobileCryptoError> {
75+
) -> Result<UpdatePasswordResponse, CryptoClientError> {
7676
update_password(&self.client, new_password)
7777
}
7878

7979
/// Generates a PIN protected user key from the provided PIN. The result can be stored and later
8080
/// used to initialize another client instance by using the PIN and the PIN key with
8181
/// `initialize_user_crypto`.
82-
pub fn derive_pin_key(&self, pin: String) -> Result<DerivePinKeyResponse, MobileCryptoError> {
82+
pub fn derive_pin_key(&self, pin: String) -> Result<DerivePinKeyResponse, CryptoClientError> {
8383
derive_pin_key(&self.client, pin)
8484
}
8585

@@ -88,7 +88,7 @@ impl CryptoClient {
8888
pub fn derive_pin_user_key(
8989
&self,
9090
encrypted_pin: EncString,
91-
) -> Result<EncString, MobileCryptoError> {
91+
) -> Result<EncString, CryptoClientError> {
9292
derive_pin_user_key(&self.client, encrypted_pin)
9393
}
9494

crates/bitwarden-core/src/key_management/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
//! [Encryptable](bitwarden_crypto::Encryptable) and [Decryptable](bitwarden_crypto::Encryptable).
1010
use bitwarden_crypto::{key_ids, KeyStore, SymmetricCryptoKey};
1111

12+
pub mod crypto;
13+
mod crypto_client;
14+
15+
pub use crypto_client::CryptoClient;
16+
1217
key_ids! {
1318
#[symmetric]
1419
pub enum SymmetricKeyId {

crates/bitwarden-core/src/mobile/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
//! This module consists of stop-gap functionality for the mobile clients until the SDK owns it's
44
//! own state.
55
6-
pub mod crypto;
76
mod kdf;
87

98
mod client_kdf;
10-
mod crypto_client;
119

1210
pub use client_kdf::KdfClient;
13-
pub use crypto_client::CryptoClient;

crates/bitwarden-core/tests/register.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async fn test_register_initialize_crypto() {
77
use std::num::NonZeroU32;
88

99
use bitwarden_core::{
10-
mobile::crypto::{InitUserCryptoMethod, InitUserCryptoRequest},
10+
key_management::crypto::{InitUserCryptoMethod, InitUserCryptoRequest},
1111
Client,
1212
};
1313
use bitwarden_crypto::Kdf;

crates/bitwarden-uniffi/src/crypto.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bitwarden_core::mobile::crypto::{
1+
use bitwarden_core::key_management::crypto::{
22
DeriveKeyConnectorRequest, DerivePinKeyResponse, InitOrgCryptoRequest, InitUserCryptoRequest,
33
UpdatePasswordResponse,
44
};
@@ -7,7 +7,7 @@ use bitwarden_crypto::{EncString, UnsignedSharedKey};
77
use crate::error::{Error, Result};
88

99
#[derive(uniffi::Object)]
10-
pub struct CryptoClient(pub(crate) bitwarden_core::mobile::CryptoClient);
10+
pub struct CryptoClient(pub(crate) bitwarden_core::key_management::CryptoClient);
1111

1212
#[uniffi::export(async_runtime = "tokio")]
1313
impl CryptoClient {

crates/bitwarden-uniffi/src/error.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ pub enum Error {
4040
#[error(transparent)]
4141
Api(#[from] bitwarden_core::ApiError),
4242
#[error(transparent)]
43-
DeriveKeyConnector(#[from] bitwarden_core::mobile::crypto::DeriveKeyConnectorError),
43+
DeriveKeyConnector(#[from] bitwarden_core::key_management::crypto::DeriveKeyConnectorError),
4444
#[error(transparent)]
4545
EncryptionSettings(
4646
#[from] bitwarden_core::client::encryption_settings::EncryptionSettingsError,
4747
),
4848
#[error(transparent)]
49-
EnrollAdminPasswordReset(#[from] bitwarden_core::mobile::crypto::EnrollAdminPasswordResetError),
49+
EnrollAdminPasswordReset(
50+
#[from] bitwarden_core::key_management::crypto::EnrollAdminPasswordResetError,
51+
),
5052
#[error(transparent)]
51-
MobileCrypto(#[from] bitwarden_core::mobile::crypto::MobileCryptoError),
53+
MobileCrypto(#[from] bitwarden_core::key_management::crypto::CryptoClientError),
5254
#[error(transparent)]
5355
AuthValidate(#[from] bitwarden_core::auth::AuthValidateError),
5456
#[error(transparent)]

0 commit comments

Comments
 (0)