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