Skip to main content

bitwarden_core/key_management/
mod.rs

1//! This module contains the definition for the key identifiers used by the rest of the crates.
2//! Any code that needs to interact with the [KeyStore] should use these types.
3//!
4//! - [SymmetricKeySlotId] is used to identify symmetric keys.
5//! - [PrivateKeySlotId] is used to identify private keys.
6//! - [KeySlotIds] is a helper type that combines both symmetric and private key identifiers. This
7//!   is usually used in the type bounds of [KeyStore],
8//!   [KeyStoreContext](bitwarden_crypto::KeyStoreContext),
9//!   [PrimitiveEncryptable](bitwarden_crypto::PrimitiveEncryptable),
10//!   [CompositeEncryptable](bitwarden_crypto::CompositeEncryptable), and
11//!   [Decryptable](bitwarden_crypto::Decryptable).
12
13use 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/// A temporary bridge to access KM-related state from within the SDK.
71#[cfg(feature = "internal")]
72pub mod state_bridge;
73
74use crate::{OrganizationId, UserId};
75
76/// Represents the local user data key, wrapped by user key.
77/// This key is used to encrypt local user data (e.g., password generator history).
78#[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
115/// This is a helper function to create a test KeyStore with a single user key.
116/// While this function is not marked as #[cfg(test)], it should only be used for testing purposes.
117/// It's only public so that other crates can make use of it in their own tests.
118pub 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
130/// This is a helper function to create a test KeyStore with a single user key and an organization
131/// key using the provided organization uuid. While this function is not marked as #[cfg(test)], it
132/// should only be used for testing purposes. It's only public so that other crates can make use of
133/// it in their own tests.
134pub 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}