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