bitwarden_crypto/traits/key_id.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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
use std::{fmt::Debug, hash::Hash};
use zeroize::ZeroizeOnDrop;
use crate::{AsymmetricCryptoKey, CryptoKey, SymmetricCryptoKey};
/// Represents a key identifier that can be used to identify cryptographic keys in the
/// key store. It is used to avoid exposing the key material directly in the public API.
///
/// This trait is user-implemented, and the recommended implementation is using enums with variants
/// for each expected key purpose. We provide a macro ([crate::key_ids]) that simplifies the trait
/// implementation
///
/// To implement it manually, note that you need a few types:
/// - One implementing [KeyId<KeyValue = SymmetricCryptoKey>]
/// - One implementing [KeyId<KeyValue = AsymmetricCryptoKey>]
/// - One implementing [KeyIds]
pub trait KeyId:
Debug + Clone + Copy + Hash + Eq + PartialEq + Ord + PartialOrd + Send + Sync + 'static
{
type KeyValue: CryptoKey + Send + Sync + ZeroizeOnDrop;
/// Returns whether the key is local to the current context or shared globally by the
/// key store. See [crate::store::KeyStoreContext] for more information.
fn is_local(&self) -> bool;
}
/// Represents a set of all the key identifiers that need to be defined to use a key store.
/// At the moment it's just symmetric and asymmetric keys.
pub trait KeyIds {
type Symmetric: KeyId<KeyValue = SymmetricCryptoKey>;
type Asymmetric: KeyId<KeyValue = AsymmetricCryptoKey>;
}
/// Just a small derive_like macro that can be used to generate the key identifier enums.
/// Example usage:
/// ```rust
/// use bitwarden_crypto::key_ids;
/// key_ids! {
/// #[symmetric]
/// pub enum SymmKeyId {
/// User,
/// Org(uuid::Uuid),
/// #[local]
/// Local(&'static str),
/// }
///
/// #[asymmetric]
/// pub enum AsymmKeyId {
/// PrivateKey,
/// }
/// pub Ids => SymmKeyId, AsymmKeyId;
/// }
#[macro_export]
macro_rules! key_ids {
( $(
#[$meta_type:tt]
$vis:vis enum $name:ident {
$(
$( #[$variant_tag:tt] )?
$variant:ident $( ( $inner:ty ) )?
),*
$(,)?
}
)+
$ids_vis:vis $ids_name:ident => $symm_name:ident, $asymm_name:ident;
) => {
$(
#[derive(std::fmt::Debug, Clone, Copy, std::hash::Hash, Eq, PartialEq, Ord, PartialOrd)]
$vis enum $name { $(
$variant $( ($inner) )?,
)* }
impl $crate::KeyId for $name {
type KeyValue = key_ids!(@key_type $meta_type);
fn is_local(&self) -> bool {
use $name::*;
match self { $(
key_ids!(@variant_match $variant $( ( $inner ) )?) =>
key_ids!(@variant_value $( $variant_tag )? ),
)* }
}
}
)+
$ids_vis struct $ids_name;
impl $crate::KeyIds for $ids_name {
type Symmetric = $symm_name;
type Asymmetric = $asymm_name;
}
};
( @key_type symmetric ) => { $crate::SymmetricCryptoKey };
( @key_type asymmetric ) => { $crate::AsymmetricCryptoKey };
( @variant_match $variant:ident ( $inner:ty ) ) => { $variant (_) };
( @variant_match $variant:ident ) => { $variant };
( @variant_value local ) => { true };
( @variant_value ) => { false };
}
#[cfg(test)]
pub(crate) mod tests {
use crate::{
traits::tests::{TestAsymmKey, TestSymmKey},
KeyId,
};
#[test]
fn test_local() {
assert!(!TestSymmKey::A(0).is_local());
assert!(!TestSymmKey::B((4, 10)).is_local());
assert!(TestSymmKey::C(8).is_local());
assert!(!TestAsymmKey::A(0).is_local());
assert!(!TestAsymmKey::B.is_local());
assert!(TestAsymmKey::C("test").is_local());
}
}