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