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//! - [SymmetricKeyId] is used to identify symmetric keys.
5//! - [AsymmetricKeyId] is used to identify asymmetric keys.
6//! - [KeyIds] is a helper type that combines both symmetric and asymmetric key identifiers. This is
7//!   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::{KeyStore, SymmetricCryptoKey, key_ids};
14
15#[cfg(feature = "internal")]
16pub mod crypto;
17#[cfg(feature = "internal")]
18mod crypto_client;
19#[cfg(feature = "internal")]
20pub use crypto_client::CryptoClient;
21
22#[cfg(feature = "internal")]
23mod master_password;
24#[cfg(feature = "internal")]
25pub use master_password::MasterPasswordError;
26#[cfg(feature = "internal")]
27pub(crate) use master_password::{MasterPasswordAuthenticationData, MasterPasswordUnlockData};
28#[cfg(feature = "internal")]
29mod non_generic_wrappers;
30#[cfg(feature = "internal")]
31pub(crate) use non_generic_wrappers::*;
32#[cfg(feature = "internal")]
33mod security_state;
34#[cfg(feature = "internal")]
35pub use security_state::{SecurityState, SignedSecurityState};
36#[cfg(feature = "internal")]
37mod user_decryption;
38#[cfg(feature = "internal")]
39pub use user_decryption::UserDecryptionData;
40
41use crate::OrganizationId;
42
43key_ids! {
44    #[symmetric]
45    pub enum SymmetricKeyId {
46        Master,
47        User,
48        Organization(OrganizationId),
49        #[local]
50        Local(&'static str),
51    }
52
53    #[asymmetric]
54    pub enum AsymmetricKeyId {
55        UserPrivateKey,
56        #[local]
57        Local(&'static str),
58    }
59
60    #[signing]
61    pub enum SigningKeyId {
62        UserSigningKey,
63        #[local]
64        Local(&'static str),
65    }
66
67    pub KeyIds => SymmetricKeyId, AsymmetricKeyId, SigningKeyId;
68}
69
70/// This is a helper function to create a test KeyStore with a single user key.
71/// While this function is not marked as #[cfg(test)], it should only be used for testing purposes.
72/// It's only public so that other crates can make use of it in their own tests.
73pub fn create_test_crypto_with_user_key(key: SymmetricCryptoKey) -> KeyStore<KeyIds> {
74    let store = KeyStore::default();
75
76    #[allow(deprecated)]
77    store
78        .context_mut()
79        .set_symmetric_key(SymmetricKeyId::User, key.clone())
80        .expect("Mutable context");
81
82    store
83}
84
85/// This is a helper function to create a test KeyStore with a single user key and an organization
86/// key using the provided organization uuid. While this function is not marked as #[cfg(test)], it
87/// should only be used for testing purposes. It's only public so that other crates can make use of
88/// it in their own tests.
89pub fn create_test_crypto_with_user_and_org_key(
90    key: SymmetricCryptoKey,
91    org_id: OrganizationId,
92    org_key: SymmetricCryptoKey,
93) -> KeyStore<KeyIds> {
94    let store = KeyStore::default();
95
96    #[allow(deprecated)]
97    store
98        .context_mut()
99        .set_symmetric_key(SymmetricKeyId::User, key.clone())
100        .expect("Mutable context");
101
102    #[allow(deprecated)]
103    store
104        .context_mut()
105        .set_symmetric_key(SymmetricKeyId::Organization(org_id), org_key.clone())
106        .expect("Mutable context");
107
108    store
109}