-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PM-5693] CryptoService using memfd_secret on Linux #7
base: main
Are you sure you want to change the base?
Changes from all commits
d7c7c3e
6068e84
50dc1b4
216eb25
4b846ed
7a01168
c649bf0
b901ef7
e2129ad
f708fcc
30854f7
bed894a
709dfce
f7eda88
ce2343e
bcd712f
38343d2
f34ce02
cc27320
281619d
32d298f
c43aa08
dd37d1d
45f3d32
1f70169
22a8b17
c63f656
db3f8d4
eb81684
d279626
d5f1ede
8652d79
fdb0263
6ea6267
32088c7
f882fb2
bec4786
c2ffea6
c0d2b63
cacf4db
f4ca816
748ba75
713ec80
8f8e378
14485e8
84c0aca
f38398e
fada9c1
f9ed542
e7bfdc3
34b256e
fe6b28f
45c9ee2
ad8ed59
75b97fa
33ec078
275574f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
use crate::{store::KeyStoreContext, AsymmetricEncString, CryptoError, EncString, KeyRef, KeyRefs}; | ||
|
||
/// Types implementing [UsesKey] are capable of knowing which cryptographic key is | ||
/// needed to encrypt/decrypt them. | ||
pub trait UsesKey<Key: KeyRef> { | ||
fn uses_key(&self) -> Key; | ||
} | ||
|
||
/// An encryption operation that takes the input value and encrypts it into the output value. | ||
/// Implementations should generally consist of calling [Encryptable::encrypt] for all the fields of | ||
/// the type. | ||
pub trait Encryptable<Refs: KeyRefs, Key: KeyRef, Output> { | ||
fn encrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Key, | ||
) -> Result<Output, crate::CryptoError>; | ||
} | ||
|
||
/// A decryption operation that takes the input value and decrypts it into the output value. | ||
/// Implementations should generally consist of calling [Decryptable::decrypt] for all the fields of | ||
/// the type. | ||
pub trait Decryptable<Refs: KeyRefs, Key: KeyRef, Output> { | ||
fn decrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Key, | ||
) -> Result<Output, crate::CryptoError>; | ||
} | ||
|
||
// Basic Encryptable/Decryptable implementations to and from bytes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these be doc comments? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've mostly added these to split up and organize all the Encryptable/Decryptable implementations. They all look very similar and when I didn't have these comments is was quite annoying to find one in particular. Maybe what that is telling us is that we should move those impls into separate modules or separate files. |
||
|
||
impl<Refs: KeyRefs> Decryptable<Refs, Refs::Symmetric, Vec<u8>> for EncString { | ||
fn decrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Refs::Symmetric, | ||
) -> Result<Vec<u8>, crate::CryptoError> { | ||
ctx.decrypt_data_with_symmetric_key(key, self) | ||
} | ||
} | ||
|
||
impl<Refs: KeyRefs> Decryptable<Refs, Refs::Asymmetric, Vec<u8>> for AsymmetricEncString { | ||
fn decrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Refs::Asymmetric, | ||
) -> Result<Vec<u8>, crate::CryptoError> { | ||
ctx.decrypt_data_with_asymmetric_key(key, self) | ||
} | ||
} | ||
|
||
impl<Refs: KeyRefs> Encryptable<Refs, Refs::Symmetric, EncString> for &[u8] { | ||
fn encrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Refs::Symmetric, | ||
) -> Result<EncString, crate::CryptoError> { | ||
ctx.encrypt_data_with_symmetric_key(key, self) | ||
} | ||
} | ||
|
||
impl<Refs: KeyRefs> Encryptable<Refs, Refs::Asymmetric, AsymmetricEncString> for &[u8] { | ||
fn encrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Refs::Asymmetric, | ||
) -> Result<AsymmetricEncString, crate::CryptoError> { | ||
ctx.encrypt_data_with_asymmetric_key(key, self) | ||
} | ||
} | ||
|
||
// Encryptable/Decryptable implementations to and from strings | ||
|
||
impl<Refs: KeyRefs> Decryptable<Refs, Refs::Symmetric, String> for EncString { | ||
fn decrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Refs::Symmetric, | ||
) -> Result<String, crate::CryptoError> { | ||
let bytes: Vec<u8> = self.decrypt(ctx, key)?; | ||
String::from_utf8(bytes).map_err(|_| CryptoError::InvalidUtf8String) | ||
} | ||
} | ||
|
||
impl<Refs: KeyRefs> Decryptable<Refs, Refs::Asymmetric, String> for AsymmetricEncString { | ||
fn decrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Refs::Asymmetric, | ||
) -> Result<String, crate::CryptoError> { | ||
let bytes: Vec<u8> = self.decrypt(ctx, key)?; | ||
String::from_utf8(bytes).map_err(|_| CryptoError::InvalidUtf8String) | ||
} | ||
} | ||
|
||
impl<Refs: KeyRefs> Encryptable<Refs, Refs::Symmetric, EncString> for &str { | ||
fn encrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Refs::Symmetric, | ||
) -> Result<EncString, crate::CryptoError> { | ||
self.as_bytes().encrypt(ctx, key) | ||
} | ||
} | ||
|
||
impl<Refs: KeyRefs> Encryptable<Refs, Refs::Asymmetric, AsymmetricEncString> for &str { | ||
fn encrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Refs::Asymmetric, | ||
) -> Result<AsymmetricEncString, crate::CryptoError> { | ||
self.as_bytes().encrypt(ctx, key) | ||
} | ||
} | ||
|
||
impl<Refs: KeyRefs> Encryptable<Refs, Refs::Symmetric, EncString> for String { | ||
fn encrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Refs::Symmetric, | ||
) -> Result<EncString, crate::CryptoError> { | ||
self.as_bytes().encrypt(ctx, key) | ||
} | ||
} | ||
|
||
impl<Refs: KeyRefs> Encryptable<Refs, Refs::Asymmetric, AsymmetricEncString> for String { | ||
fn encrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Refs::Asymmetric, | ||
) -> Result<AsymmetricEncString, crate::CryptoError> { | ||
self.as_bytes().encrypt(ctx, key) | ||
} | ||
} | ||
|
||
// Generic implementations for Optional values | ||
|
||
impl<Refs: KeyRefs, Key: KeyRef, T: Encryptable<Refs, Key, Output>, Output> | ||
Encryptable<Refs, Key, Option<Output>> for Option<T> | ||
{ | ||
fn encrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Key, | ||
) -> Result<Option<Output>, crate::CryptoError> { | ||
self.as_ref() | ||
.map(|value| value.encrypt(ctx, key)) | ||
.transpose() | ||
} | ||
} | ||
|
||
impl<Refs: KeyRefs, Key: KeyRef, T: Decryptable<Refs, Key, Output>, Output> | ||
Decryptable<Refs, Key, Option<Output>> for Option<T> | ||
{ | ||
fn decrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Key, | ||
) -> Result<Option<Output>, crate::CryptoError> { | ||
self.as_ref() | ||
.map(|value| value.decrypt(ctx, key)) | ||
.transpose() | ||
} | ||
} | ||
|
||
// Generic implementations for Vec values | ||
|
||
impl<Refs: KeyRefs, Key: KeyRef, T: Encryptable<Refs, Key, Output>, Output> | ||
Encryptable<Refs, Key, Vec<Output>> for Vec<T> | ||
{ | ||
fn encrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Key, | ||
) -> Result<Vec<Output>, crate::CryptoError> { | ||
self.iter().map(|value| value.encrypt(ctx, key)).collect() | ||
} | ||
} | ||
|
||
impl<Refs: KeyRefs, Key: KeyRef, T: Decryptable<Refs, Key, Output>, Output> | ||
Decryptable<Refs, Key, Vec<Output>> for Vec<T> | ||
{ | ||
fn decrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Refs>, | ||
key: Key, | ||
) -> Result<Vec<Output>, crate::CryptoError> { | ||
self.iter().map(|value| value.decrypt(ctx, key)).collect() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MissingKey2
๐ญ Will this be removed once we remove all usages of the old interfaces?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's removed in the other PR that starts using this code in the rest of the SDK:
https://github.com/bitwarden/sdk-internal/pull/8/files#diff-3a39fb8ed55a85cd232b4ac7681c85bb81462727f6d311b1a4c33be7c5b7f4ed
I didn't want to update it here to avoid needing to touch up code which was going to need to be updated in the other PR anyway