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