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