bitwarden_core/key_management/
mod.rs1use bitwarden_crypto::{
14 EncString, KeyStore, SymmetricCryptoKey, key_slot_ids, safe::PasswordProtectedKeyEnvelope,
15};
16
17#[cfg(feature = "internal")]
18pub mod account_cryptographic_state;
19#[cfg(feature = "internal")]
20pub mod crypto;
21#[cfg(feature = "internal")]
22mod crypto_client;
23use bitwarden_encoding::B64;
24#[cfg(feature = "internal")]
25pub use crypto_client::CryptoClient;
26
27#[cfg(feature = "internal")]
28mod master_password;
29#[cfg(feature = "internal")]
30pub use master_password::{
31 MasterPasswordAuthenticationData, MasterPasswordError, MasterPasswordUnlockData,
32};
33#[cfg(feature = "internal")]
34mod security_state;
35#[cfg(feature = "internal")]
36pub use security_state::{
37 BLOB_SECURITY_VERSION, MINIMUM_ENFORCE_ICON_URI_HASH_VERSION, SecurityState,
38 SignedSecurityState,
39};
40#[cfg(feature = "internal")]
41mod user_decryption;
42use serde::{Deserialize, Serialize};
43#[cfg(feature = "wasm")]
44use tsify::Tsify;
45#[cfg(feature = "internal")]
46pub use user_decryption::UserDecryptionData;
47#[cfg(feature = "internal")]
48mod v2_upgrade_token;
49#[cfg(feature = "internal")]
50pub use v2_upgrade_token::{V2UpgradeToken, V2UpgradeTokenError};
51
52#[cfg(all(feature = "internal", feature = "wasm"))]
53mod wasm_unlock_state;
54
55#[cfg(feature = "internal")]
56mod pin_lock_system;
57#[cfg(feature = "internal")]
58pub use pin_lock_system::{PinLockSystem, PinLockType, PinUnlockStatus};
59
60#[cfg(feature = "internal")]
61mod local_user_data_key;
62#[cfg(feature = "internal")]
63mod local_user_data_key_state;
64
65#[cfg(feature = "internal")]
67pub mod state_bridge;
68
69use crate::{OrganizationId, UserId};
70
71#[derive(Serialize, Deserialize, Debug, Clone)]
74#[repr(transparent)]
75#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
76#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
77pub struct UserKeyState {
78 decrypted_user_key: B64,
79}
80
81bitwarden_state::register_repository_item!(String => UserKeyState, "UserKey");
82
83#[derive(Serialize, Deserialize, Debug, Clone)]
86#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
87#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
88pub struct LocalUserDataKeyState {
89 wrapped_key: EncString,
90}
91
92bitwarden_state::register_repository_item!(UserId => LocalUserDataKeyState, "LocalUserDataKey");
93
94#[derive(Serialize, Deserialize, Debug, Clone)]
96#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
97#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
98pub struct EphemeralPinEnvelopeState {
99 pin_envelope: PasswordProtectedKeyEnvelope,
100}
101
102bitwarden_state::register_repository_item!(String => EphemeralPinEnvelopeState, "EphemeralPinEnvelope");
103
104key_slot_ids! {
105 #[symmetric]
106 pub enum SymmetricKeySlotId {
107 Master,
108 User,
109 Organization(OrganizationId),
110 LocalUserData,
111 #[local]
112 Local(LocalId),
113 }
114
115 #[private]
116 pub enum PrivateKeySlotId {
117 UserPrivateKey,
118 #[local]
119 Local(LocalId),
120 }
121
122 #[signing]
123 pub enum SigningKeySlotId {
124 UserSigningKey,
125 #[local]
126 Local(LocalId),
127 }
128
129 pub KeySlotIds => SymmetricKeySlotId, PrivateKeySlotId, SigningKeySlotId;
130}
131
132pub fn create_test_crypto_with_user_key(key: SymmetricCryptoKey) -> KeyStore<KeySlotIds> {
136 let store = KeyStore::default();
137
138 #[allow(deprecated)]
139 store
140 .context_mut()
141 .set_symmetric_key(SymmetricKeySlotId::User, key.clone())
142 .expect("Mutable context");
143
144 store
145}
146
147pub fn create_test_crypto_with_user_and_org_key(
152 key: SymmetricCryptoKey,
153 org_id: OrganizationId,
154 org_key: SymmetricCryptoKey,
155) -> KeyStore<KeySlotIds> {
156 let store = KeyStore::default();
157
158 #[allow(deprecated)]
159 store
160 .context_mut()
161 .set_symmetric_key(SymmetricKeySlotId::User, key.clone())
162 .expect("Mutable context");
163
164 #[allow(deprecated)]
165 store
166 .context_mut()
167 .set_symmetric_key(SymmetricKeySlotId::Organization(org_id), org_key.clone())
168 .expect("Mutable context");
169
170 store
171}