bitwarden_core/auth/
tde.rs1use bitwarden_crypto::{
2 DeviceKey, EncString, Kdf, PublicKey, SpkiPublicKeyBytes, SymmetricCryptoKey,
3 TrustDeviceResponse, UnsignedSharedKey, UserKey,
4};
5use bitwarden_encoding::B64;
6
7use crate::{
8 Client, client::encryption_settings::EncryptionSettingsError,
9 key_management::account_cryptographic_state::WrappedAccountCryptographicState,
10};
11
12pub(super) fn make_register_tde_keys(
16 client: &Client,
17 email: String,
18 org_public_key: B64,
19 remember_device: bool,
20) -> Result<RegisterTdeKeyResponse, EncryptionSettingsError> {
21 let public_key = PublicKey::from_der(&SpkiPublicKeyBytes::from(&org_public_key))?;
22
23 let user_key = UserKey::new(SymmetricCryptoKey::make_aes256_cbc_hmac_key());
24 let key_pair = user_key.make_key_pair()?;
25
26 #[expect(deprecated)]
27 let admin_reset = UnsignedSharedKey::encapsulate_key_unsigned(&user_key.0, &public_key)?;
28
29 let device_key = if remember_device {
30 Some(DeviceKey::trust_device(&user_key.0)?)
31 } else {
32 None
33 };
34
35 client.internal.initialize_user_crypto_decrypted_key(
36 user_key.0,
37 WrappedAccountCryptographicState::V1 {
40 private_key: key_pair.private.clone(),
41 },
42 &None,
43 )?;
44
45 client
46 .internal
47 .set_login_method(crate::client::LoginMethod::User(
48 crate::client::UserLoginMethod::Username {
49 client_id: "".to_owned(),
50 email,
51 kdf: Kdf::default_pbkdf2(),
52 },
53 ));
54
55 Ok(RegisterTdeKeyResponse {
56 private_key: key_pair.private,
57 public_key: key_pair.public,
58
59 admin_reset,
60 device_key,
61 })
62}
63
64#[allow(missing_docs)]
65#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
66pub struct RegisterTdeKeyResponse {
67 pub private_key: EncString,
68 pub public_key: B64,
69
70 pub admin_reset: UnsignedSharedKey,
71 pub device_key: Option<TrustDeviceResponse>,
72}