Skip to content

Commit

Permalink
Set login_method when registering for tde (#711)
Browse files Browse the repository at this point in the history
We should set login_method when registering TDE keys
  • Loading branch information
Hinton authored Apr 15, 2024
1 parent 499e213 commit 6d45212
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 42 deletions.
21 changes: 21 additions & 0 deletions crates/bitwarden-crypto/src/keys/master_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ pub enum Kdf {
},
}

impl Default for Kdf {
fn default() -> Self {
Kdf::PBKDF2 {
iterations: default_pbkdf2_iterations(),
}
}
}

pub fn default_pbkdf2_iterations() -> NonZeroU32 {
NonZeroU32::new(600_000).expect("Non-zero number")
}
pub fn default_argon2_iterations() -> NonZeroU32 {
NonZeroU32::new(3).expect("Non-zero number")
}
pub fn default_argon2_memory() -> NonZeroU32 {
NonZeroU32::new(64).expect("Non-zero number")
}
pub fn default_argon2_parallelism() -> NonZeroU32 {
NonZeroU32::new(4).expect("Non-zero number")
}

#[derive(Copy, Clone, JsonSchema)]
#[cfg_attr(feature = "mobile", derive(uniffi::Enum))]
pub enum HashPurpose {
Expand Down
5 changes: 4 additions & 1 deletion crates/bitwarden-crypto/src/keys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
mod key_encryptable;
pub use key_encryptable::{CryptoKey, KeyDecryptable, KeyEncryptable};
mod master_key;
pub use master_key::{HashPurpose, Kdf, MasterKey};
pub use master_key::{
default_argon2_iterations, default_argon2_memory, default_argon2_parallelism,
default_pbkdf2_iterations, HashPurpose, Kdf, MasterKey,
};
mod shareable_key;
pub use shareable_key::derive_shareable_key;
mod symmetric_crypto_key;
Expand Down
13 changes: 6 additions & 7 deletions crates/bitwarden-uniffi/src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,15 @@ impl ClientAuth {
/// Generate keys needed for TDE process
pub async fn make_register_tde_keys(
&self,
email: String,
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)?)
Ok(self.0 .0.write().await.auth().make_register_tde_keys(
email,
org_public_key,
remember_device,
)?)
}

/// Validate the user password
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct IdentityTokenSuccessResponse {
#[serde(
rename = "kdfIterations",
alias = "KdfIterations",
default = "crate::util::default_pbkdf2_iterations"
default = "bitwarden_crypto::default_pbkdf2_iterations"
)]
kdf_iterations: NonZeroU32,

Expand All @@ -41,6 +41,8 @@ pub struct IdentityTokenSuccessResponse {

#[cfg(test)]
mod test {
use bitwarden_crypto::default_pbkdf2_iterations;

use super::*;

impl Default for IdentityTokenSuccessResponse {
Expand All @@ -54,7 +56,7 @@ mod test {
key: Default::default(),
two_factor_token: Default::default(),
kdf: KdfType::default(),
kdf_iterations: crate::util::default_pbkdf2_iterations(),
kdf_iterations: default_pbkdf2_iterations(),
reset_master_password: Default::default(),
force_password_reset: Default::default(),
api_use_key_connector: Default::default(),
Expand Down
3 changes: 2 additions & 1 deletion crates/bitwarden/src/auth/client_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ impl<'a> ClientAuth<'a> {

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

pub async fn register(&mut self, input: &RegisterRequest) -> Result<()> {
Expand Down
6 changes: 1 addition & 5 deletions crates/bitwarden/src/auth/login/auth_request.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::num::NonZeroU32;

use bitwarden_api_api::{
apis::auth_requests_api::{auth_requests_id_response_get, auth_requests_post},
models::{AuthRequestCreateRequestModel, AuthRequestType},
Expand Down Expand Up @@ -86,9 +84,7 @@ pub(crate) async fn complete_auth_request(
.await?;

if let IdentityTokenResponse::Authenticated(r) = response {
let kdf = Kdf::PBKDF2 {
iterations: NonZeroU32::new(600_000).expect("Non-zero number"),
};
let kdf = Kdf::default();

client.set_tokens(
r.access_token.clone(),
Expand Down
3 changes: 1 addition & 2 deletions crates/bitwarden/src/auth/login/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ pub(crate) fn parse_prelogin(response: PreloginResponseModel) -> Result<Kdf> {
use std::num::NonZeroU32;

use bitwarden_api_identity::models::KdfType;

use crate::util::{
use bitwarden_crypto::{
default_argon2_iterations, default_argon2_memory, default_argon2_parallelism,
default_pbkdf2_iterations,
};
Expand Down
8 changes: 3 additions & 5 deletions crates/bitwarden/src/auth/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use bitwarden_api_identity::{
apis::accounts_api::accounts_register_post,
models::{KeysRequestModel, RegisterRequestModel},
};
use bitwarden_crypto::{HashPurpose, MasterKey, RsaKeyPair};
use bitwarden_crypto::{default_pbkdf2_iterations, HashPurpose, MasterKey, RsaKeyPair};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{client::Kdf, error::Result, util::default_pbkdf2_iterations, Client};
use crate::{client::Kdf, error::Result, Client};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
Expand All @@ -21,9 +21,7 @@ pub struct RegisterRequest {
pub(super) async fn register(client: &mut Client, req: &RegisterRequest) -> Result<()> {
let config = client.get_api_configurations().await;

let kdf = Kdf::PBKDF2 {
iterations: default_pbkdf2_iterations(),
};
let kdf = Kdf::default();

let keys = make_register_keys(req.email.to_owned(), req.password.to_owned(), kdf)?;

Expand Down
10 changes: 9 additions & 1 deletion crates/bitwarden/src/auth/tde.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use base64::{engine::general_purpose::STANDARD, Engine};
use bitwarden_crypto::{
AsymmetricEncString, AsymmetricPublicCryptoKey, DeviceKey, EncString, SymmetricCryptoKey,
AsymmetricEncString, AsymmetricPublicCryptoKey, DeviceKey, EncString, Kdf, SymmetricCryptoKey,
TrustDeviceResponse, UserKey,
};

Expand All @@ -11,6 +11,7 @@ use crate::{error::Result, Client};
/// password reset. If remember_device is true, it also generates a device key.
pub(super) fn make_register_tde_keys(
client: &mut Client,
email: String,
org_public_key: String,
remember_device: bool,
) -> Result<RegisterTdeKeyResponse> {
Expand All @@ -30,6 +31,13 @@ pub(super) fn make_register_tde_keys(
None
};

client.set_login_method(crate::client::LoginMethod::User(
crate::client::UserLoginMethod::Username {
client_id: "".to_owned(),
email,
kdf: Kdf::default(),
},
));
client.initialize_user_crypto_decrypted_key(user_key.0, key_pair.private.clone())?;

Ok(RegisterTdeKeyResponse {
Expand Down
18 changes: 0 additions & 18 deletions crates/bitwarden/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
use std::num::NonZeroU32;

use base64::{
alphabet,
engine::{DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig},
};

pub fn default_pbkdf2_iterations() -> NonZeroU32 {
NonZeroU32::new(600_000).expect("Non-zero number")
}
#[cfg(feature = "internal")]
pub fn default_argon2_iterations() -> NonZeroU32 {
NonZeroU32::new(3).expect("Non-zero number")
}
#[cfg(feature = "internal")]
pub fn default_argon2_memory() -> NonZeroU32 {
NonZeroU32::new(64).expect("Non-zero number")
}
#[cfg(feature = "internal")]
pub fn default_argon2_parallelism() -> NonZeroU32 {
NonZeroU32::new(4).expect("Non-zero number")
}

const INDIFFERENT: GeneralPurposeConfig =
GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent);

Expand Down

0 comments on commit 6d45212

Please sign in to comment.