Skip to content

Commit

Permalink
Implement make_register_tde_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Mar 27, 2024
1 parent 27cf054 commit 1231c37
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/bitwarden-uniffi/src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use bitwarden::auth::{
password::MasterPasswordPolicyOptions, AuthRequestResponse, RegisterKeyResponse,
RegisterTdeKeyResponse,
};
use bitwarden_crypto::{AsymmetricEncString, HashPurpose, Kdf, TrustDeviceResponse};

Expand Down Expand Up @@ -78,6 +79,21 @@ impl ClientAuth {
.make_register_keys(email, password, kdf)?)
}

/// Generate keys needed for TDE process
pub async fn make_register_tde_keys(
&self,
org_public_key: String,
remember_device: bool,
) -> Result<RegisterTdeKeyResponse> {
Ok(self
.0
.0
.write()
.await
.auth()
.make_register_tde_keys(org_public_key, remember_device)?)
}

Check warning on line 95 in crates/bitwarden-uniffi/src/auth/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/auth/mod.rs#L83-L95

Added lines #L83 - L95 were not covered by tests

/// Validate the user password
///
/// To retrieve the user's password hash, use [`ClientAuth::hash_password`] with
Expand Down
9 changes: 9 additions & 0 deletions crates/bitwarden/src/auth/client_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
MasterPasswordPolicyOptions,
},
register::{make_register_keys, register},
tde::{make_register_tde_keys, RegisterTdeKeyResponse},
AuthRequestResponse, RegisterKeyResponse, RegisterRequest,
},
client::Kdf,
Expand Down Expand Up @@ -73,6 +74,14 @@ impl<'a> ClientAuth<'a> {
make_register_keys(email, password, kdf)
}

pub fn make_register_tde_keys(
&mut self,
org_public_key: String,
remember_device: bool,
) -> Result<RegisterTdeKeyResponse> {
make_register_tde_keys(self.client, org_public_key, remember_device)
}

Check warning on line 83 in crates/bitwarden/src/auth/client_auth.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/client_auth.rs#L77-L83

Added lines #L77 - L83 were not covered by tests

pub async fn register(&mut self, input: &RegisterRequest) -> Result<()> {
register(self.client, input).await
}
Expand Down
4 changes: 4 additions & 0 deletions crates/bitwarden/src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ mod auth_request;
pub use auth_request::AuthRequestResponse;
#[cfg(feature = "mobile")]
pub(crate) use auth_request::{auth_request_decrypt_master_key, auth_request_decrypt_user_key};
#[cfg(feature = "mobile")]
mod tde;
#[cfg(feature = "mobile")]
pub use tde::RegisterTdeKeyResponse;

#[cfg(feature = "internal")]
use crate::{client::Kdf, error::Result};
Expand Down
52 changes: 52 additions & 0 deletions crates/bitwarden/src/auth/tde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use base64::{engine::general_purpose::STANDARD, Engine};
use bitwarden_crypto::{
AsymmetricEncString, AsymmetricPublicCryptoKey, DeviceKey, EncString, SymmetricCryptoKey,
TrustDeviceResponse, UserKey,
};

use crate::{error::Result, Client};

pub(super) fn make_register_tde_keys(
client: &mut Client,
org_public_key: String,
remember_device: bool,
) -> Result<RegisterTdeKeyResponse> {
let public_key = AsymmetricPublicCryptoKey::from_der(&STANDARD.decode(org_public_key)?)?;

Check warning on line 14 in crates/bitwarden/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/tde.rs#L9-L14

Added lines #L9 - L14 were not covered by tests

let mut rng = rand::thread_rng();

// Generate a new user key and key pair, and encrypt the user key with the org public key for
// admin password reset
let user_key = UserKey::new(SymmetricCryptoKey::generate(&mut rng));
let key_pair = user_key.make_key_pair()?;

Check warning on line 21 in crates/bitwarden/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/tde.rs#L16-L21

Added lines #L16 - L21 were not covered by tests

let admin_reset =
AsymmetricEncString::encrypt_rsa2048_oaep_sha1(&user_key.0.to_vec(), &public_key)?;

Check warning on line 24 in crates/bitwarden/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/tde.rs#L23-L24

Added lines #L23 - L24 were not covered by tests

let device_key = if remember_device {
Some(DeviceKey::trust_device(&user_key.0)?)

Check warning on line 27 in crates/bitwarden/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/tde.rs#L26-L27

Added lines #L26 - L27 were not covered by tests
} else {
None

Check warning on line 29 in crates/bitwarden/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/tde.rs#L29

Added line #L29 was not covered by tests
};

// Initialize the crypto with the generated user key, this way it doesn't need to leave the
// client
client.initialize_user_crypto_decrypted_key(user_key.0, key_pair.private.clone())?;

Check warning on line 34 in crates/bitwarden/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/tde.rs#L34

Added line #L34 was not covered by tests

Ok(RegisterTdeKeyResponse {
private_key: key_pair.private,
public_key: key_pair.public,

admin_reset,
device_key,
})
}

Check warning on line 43 in crates/bitwarden/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/tde.rs#L36-L43

Added lines #L36 - L43 were not covered by tests

#[cfg_attr(feature = "mobile", derive(uniffi::Record))]
pub struct RegisterTdeKeyResponse {
pub private_key: EncString,
pub public_key: String,

pub admin_reset: AsymmetricEncString,
pub device_key: Option<TrustDeviceResponse>,
}

0 comments on commit 1231c37

Please sign in to comment.