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_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/// A temporary bridge to access KM-related state from within the SDK.
67#[cfg(feature = "internal")]
68pub mod state_bridge;
69
70use crate::{OrganizationId, UserId};
71
72/// Represents the local user data key, wrapped by user key.
73/// This key is used to encrypt local user data (e.g., password generator history).
74#[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
111/// This is a helper function to create a test KeyStore with a single user key.
112/// While this function is not marked as #[cfg(test)], it should only be used for testing purposes.
113/// It's only public so that other crates can make use of it in their own tests.
114pub 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
126/// This is a helper function to create a test KeyStore with a single user key and an organization
127/// key using the provided organization uuid. While this function is not marked as #[cfg(test)], it
128/// should only be used for testing purposes. It's only public so that other crates can make use of
129/// it in their own tests.
130pub 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}