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 = "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 crypto_settings;
60#[cfg(feature = "internal")]
61pub(crate) use crypto_settings::CryptoSettings;
62
63#[cfg(feature = "internal")]
64mod pin_lock_system;
65#[cfg(feature = "internal")]
66pub use pin_lock_system::{PinLockSystem, PinLockType, PinUnlockStatus};
67
68#[cfg(feature = "internal")]
69mod local_user_data_key;
70#[cfg(feature = "internal")]
71mod local_user_data_key_state;
72
73/// A temporary bridge to access KM-related state from within the SDK.
74#[cfg(feature = "internal")]
75pub mod state_bridge;
76
77use crate::{OrganizationId, UserId};
78
79/// Represents the local user data key, wrapped by user key.
80/// This key is used to encrypt local user data (e.g., password generator history).
81#[derive(Serialize, Deserialize, Debug, Clone)]
82#[bitwarden_ffi::wasm_record]
83#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
84pub struct LocalUserDataKeyState {
85    wrapped_key: EncString,
86}
87
88bitwarden_state::register_repository_item!(UserId => LocalUserDataKeyState, "LocalUserDataKey");
89
90key_slot_ids! {
91    #[symmetric]
92    pub enum SymmetricKeySlotId {
93        Master,
94        User,
95        Organization(OrganizationId),
96        LocalUserData,
97        #[local]
98        Local(LocalId),
99    }
100
101    #[private]
102    pub enum PrivateKeySlotId {
103        Organization(OrganizationId),
104        UserPrivateKey,
105        #[local]
106        Local(LocalId),
107    }
108
109    #[signing]
110    pub enum SigningKeySlotId {
111        UserSigningKey,
112        #[local]
113        Local(LocalId),
114    }
115
116    pub KeySlotIds => SymmetricKeySlotId, PrivateKeySlotId, SigningKeySlotId;
117}
118
119/// This is a helper function to create a test KeyStore with a single user key.
120/// While this function is not marked as #[cfg(test)], it should only be used for testing purposes.
121/// It's only public so that other crates can make use of it in their own tests.
122pub fn create_test_crypto_with_user_key(key: SymmetricCryptoKey) -> KeyStore<KeySlotIds> {
123    let store = KeyStore::default();
124
125    #[allow(deprecated)]
126    store
127        .context_mut()
128        .set_symmetric_key(SymmetricKeySlotId::User, key.clone())
129        .expect("Mutable context");
130
131    store
132}
133
134/// This is a helper function to create a test KeyStore with a single user key and an organization
135/// key using the provided organization uuid. While this function is not marked as #[cfg(test)], it
136/// should only be used for testing purposes. It's only public so that other crates can make use of
137/// it in their own tests.
138pub fn create_test_crypto_with_user_and_org_key(
139    key: SymmetricCryptoKey,
140    org_id: OrganizationId,
141    org_key: SymmetricCryptoKey,
142) -> KeyStore<KeySlotIds> {
143    let store = KeyStore::default();
144
145    #[allow(deprecated)]
146    store
147        .context_mut()
148        .set_symmetric_key(SymmetricKeySlotId::User, key.clone())
149        .expect("Mutable context");
150
151    #[allow(deprecated)]
152    store
153        .context_mut()
154        .set_symmetric_key(SymmetricKeySlotId::Organization(org_id), org_key.clone())
155        .expect("Mutable context");
156
157    store
158}