use bitwarden_crypto::{
DeviceKey, EncString, Kdf, PublicKey, SpkiPublicKeyBytes, SymmetricCryptoKey,
TrustDeviceResponse, UnsignedSharedKey, UserKey,
};
use bitwarden_encoding::B64;
use crate::{
Client, client::encryption_settings::EncryptionSettingsError,
key_management::account_cryptographic_state::WrappedAccountCryptographicState,
};
pub(super) async fn make_register_tde_keys(
client: &Client,
email: String,
org_public_key: B64,
remember_device: bool,
) -> Result<RegisterTdeKeyResponse, EncryptionSettingsError> {
let public_key = PublicKey::from_der(&SpkiPublicKeyBytes::from(&org_public_key))?;
let user_key = UserKey::new(SymmetricCryptoKey::make_aes256_cbc_hmac_key());
let key_pair = user_key.make_key_pair()?;
#[expect(deprecated)]
let admin_reset = UnsignedSharedKey::encapsulate_key_unsigned(&user_key.0, &public_key)?;
let device_key = if remember_device {
Some(DeviceKey::trust_device(&user_key.0)?)
} else {
None
};
client.internal.initialize_user_crypto_decrypted_key(
user_key.0,
WrappedAccountCryptographicState::V1 {
private_key: key_pair.private.clone(),
},
&None,
)?;
client
.internal
.set_login_method(crate::client::LoginMethod::User(
crate::client::UserLoginMethod::Username {
client_id: "".to_owned(),
email,
kdf: Kdf::default_pbkdf2(),
},
))
.await;
Ok(RegisterTdeKeyResponse {
private_key: key_pair.private,
public_key: key_pair.public,
admin_reset,
device_key,
})
}
#[allow(missing_docs)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct RegisterTdeKeyResponse {
pub private_key: EncString,
pub public_key: B64,
pub admin_reset: UnsignedSharedKey,
pub device_key: Option<TrustDeviceResponse>,
}