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