Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-7051] Implement make_register_tde_keys #685

Merged
merged 2 commits into from
Mar 28, 2024
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
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 bitwarden::auth::{
password::MasterPasswordPolicyOptions, AuthRequestResponse, RegisterKeyResponse,
RegisterTdeKeyResponse,
};
use bitwarden_crypto::{AsymmetricEncString, HashPurpose, Kdf, TrustDeviceResponse};

Expand Down Expand Up @@ -78,6 +79,21 @@
.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 @@
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 @@
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 = "internal")]
mod tde;
#[cfg(feature = "internal")]
pub use tde::RegisterTdeKeyResponse;

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

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

/// This function generates a new user key and key pair, initializes the client's crypto with the
/// generated user key, and encrypts the user key with the organization public key for admin
/// password reset. If remember_device is true, it also generates a device key.
pub(super) fn make_register_tde_keys(
Hinton marked this conversation as resolved.
Show resolved Hide resolved
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 17 in crates/bitwarden/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/tde.rs#L12-L17

Added lines #L12 - L17 were not covered by tests

let mut rng = rand::thread_rng();

let user_key = UserKey::new(SymmetricCryptoKey::generate(&mut rng));
let key_pair = user_key.make_key_pair()?;

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

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/tde.rs#L19-L22

Added lines #L19 - L22 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L24 - L25 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L30 was not covered by tests
};

client.initialize_user_crypto_decrypted_key(user_key.0, key_pair.private.clone())?;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L33 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 42 in crates/bitwarden/src/auth/tde.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/tde.rs#L35-L42

Added lines #L35 - L42 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>,
}
2 changes: 1 addition & 1 deletion crates/bitwarden/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl Client {
Ok(self.encryption_settings.as_ref().unwrap())
}

#[cfg(feature = "mobile")]
#[cfg(feature = "internal")]
pub(crate) fn initialize_user_crypto_decrypted_key(
&mut self,
user_key: SymmetricCryptoKey,
Expand Down
Loading