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_encrypted_migrations_grace_period_start;
46#[cfg(feature = "internal")]
47pub use v2_encrypted_migrations_grace_period_start::V2EncryptedMigrationsGracePeriodStart;
48#[cfg(feature = "internal")]
49mod v2_upgrade_token;
50#[cfg(feature = "internal")]
51pub use v2_upgrade_token::{V2UpgradeToken, V2UpgradeTokenError};
52#[cfg(feature = "internal")]
53mod webauthn_prf;
54#[cfg(feature = "internal")]
55pub use webauthn_prf::{WebAuthnPrfError, WebAuthnPrfUnlockData, WebAuthnPrfUnlockOption};
56
57#[cfg(all(feature = "internal", feature = "wasm"))]
58mod wasm_unlock_state;
59
60#[cfg(feature = "internal")]
61mod pin_lock_system;
62#[cfg(feature = "internal")]
63pub use pin_lock_system::{PinLockSystem, PinLockType, PinUnlockStatus};
64
65#[cfg(feature = "internal")]
66mod local_user_data_key;
67#[cfg(feature = "internal")]
68mod local_user_data_key_state;
69
70#[cfg(feature = "internal")]
72pub mod state_bridge;
73
74use crate::{OrganizationId, UserId};
75
76#[derive(Serialize, Deserialize, Debug, Clone)]
79#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
80#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
81pub struct LocalUserDataKeyState {
82 wrapped_key: EncString,
83}
84
85bitwarden_state::register_repository_item!(UserId => LocalUserDataKeyState, "LocalUserDataKey");
86
87key_slot_ids! {
88 #[symmetric]
89 pub enum SymmetricKeySlotId {
90 Master,
91 User,
92 Organization(OrganizationId),
93 LocalUserData,
94 #[local]
95 Local(LocalId),
96 }
97
98 #[private]
99 pub enum PrivateKeySlotId {
100 UserPrivateKey,
101 #[local]
102 Local(LocalId),
103 }
104
105 #[signing]
106 pub enum SigningKeySlotId {
107 UserSigningKey,
108 #[local]
109 Local(LocalId),
110 }
111
112 pub KeySlotIds => SymmetricKeySlotId, PrivateKeySlotId, SigningKeySlotId;
113}
114
115pub fn create_test_crypto_with_user_key(key: SymmetricCryptoKey) -> KeyStore<KeySlotIds> {
119 let store = KeyStore::default();
120
121 #[allow(deprecated)]
122 store
123 .context_mut()
124 .set_symmetric_key(SymmetricKeySlotId::User, key.clone())
125 .expect("Mutable context");
126
127 store
128}
129
130pub fn create_test_crypto_with_user_and_org_key(
135 key: SymmetricCryptoKey,
136 org_id: OrganizationId,
137 org_key: SymmetricCryptoKey,
138) -> KeyStore<KeySlotIds> {
139 let store = KeyStore::default();
140
141 #[allow(deprecated)]
142 store
143 .context_mut()
144 .set_symmetric_key(SymmetricKeySlotId::User, key.clone())
145 .expect("Mutable context");
146
147 #[allow(deprecated)]
148 store
149 .context_mut()
150 .set_symmetric_key(SymmetricKeySlotId::Organization(org_id), org_key.clone())
151 .expect("Mutable context");
152
153 store
154}