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