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