-
Notifications
You must be signed in to change notification settings - Fork 9
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] KeyStore implementation #7
Conversation
Otherwise some Drop implementations can crash
Split the secure storage implementations to #125 so this is easier to review and merge. |
crates/bitwarden-crypto/src/store/backend/implementation/basic.rs
Outdated
Show resolved
Hide resolved
impl<Ids: KeyIds> Encryptable<Ids, Ids::Symmetric, EncString> for &str { | ||
fn encrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Ids>, | ||
key: Ids::Symmetric, | ||
) -> Result<EncString, CryptoError> { | ||
self.as_bytes().encrypt(ctx, key) | ||
} | ||
} | ||
|
||
impl<Ids: KeyIds> Encryptable<Ids, Ids::Asymmetric, AsymmetricEncString> for &str { | ||
fn encrypt( | ||
&self, | ||
ctx: &mut KeyStoreContext<Ids>, | ||
key: Ids::Asymmetric, | ||
) -> Result<AsymmetricEncString, CryptoError> { | ||
self.as_bytes().encrypt(ctx, key) | ||
} | ||
} |
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.
I believe we can generalize this code slightly.
impl<Ids: KeyIds> Encryptable<Ids, Ids::Symmetric, EncString> for &str { | |
fn encrypt( | |
&self, | |
ctx: &mut KeyStoreContext<Ids>, | |
key: Ids::Symmetric, | |
) -> Result<EncString, CryptoError> { | |
self.as_bytes().encrypt(ctx, key) | |
} | |
} | |
impl<Ids: KeyIds> Encryptable<Ids, Ids::Asymmetric, AsymmetricEncString> for &str { | |
fn encrypt( | |
&self, | |
ctx: &mut KeyStoreContext<Ids>, | |
key: Ids::Asymmetric, | |
) -> Result<AsymmetricEncString, CryptoError> { | |
self.as_bytes().encrypt(ctx, key) | |
} | |
} | |
impl<Ids, K, E> Encryptable<Ids, K, E> for &str | |
where | |
Ids: KeyIds, | |
K: KeyId, | |
E: EncStringType, | |
[u8]: Encryptable<Ids, K, E>, | |
{ | |
fn encrypt(&self, ctx: &mut KeyStoreContext<Ids>, key: K) -> Result<E, CryptoError> { | |
self.as_bytes().encrypt(ctx, key) | |
} | |
} |
And define a common trait for EncString
.
pub trait EncStringType {}
impl EncStringType for EncString {}
impl EncStringType for AsymmetricEncString {}
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.
This is something Matt also brought up when trying to do authenticated data. I think the best path forward would be to separate encryptable/decrytable into encryptable/decryptable and encodable/decodable, something like this.
impl <T: Encodable<Vec<u8>> Encryptable<EncString> for T
impl <T: Encodable<Vec<u8>> Encryptable< AsymmetricEncString> for T
impl Encodable<Vec<u8>> for &str
impl Encodable<Vec<u8>> for String
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.
Can we add some quick module docs? I.e. //!
/// * `new_key_id` - The key id where the decrypted key will be stored. If it already exists, it | ||
/// will be overwritten | ||
/// * `encrypted_key` - The key to decrypt | ||
pub fn decrypt_symmetric_key_with_symmetric_key( |
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.
I wonder if the naming here is slightly misleading. We're not decrypting it we're adding it to the context?
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.
Well, we're doing both: We decrypt the key and add it to the context. Originally i was planning to make it a generic function like decrypt_key_into_store
, but I couldn't get the typing to do what I wanted, which explains the long names. (And calling it decrypt_symmetric_key_into_store_using_symmetric_key
is just way too long)
## ๐๏ธ Tracking https://bitwarden.atlassian.net/browse/PM-5693 ## ๐ Objective Migrated from bitwarden/sdk-sm#1117 Migrate the codebase to the new KeyStore introduced in #7. EncryptionSettings was removed from Client, though it is still used to initialize the KeyStore. Ideally we'd move that initialization code over directly into the crypto crate but this PR is big enough as it is. There are still some things using keys directly and KeyEncryptable/KeyDecryptable, like MasterKey, PinKey, DeviceKey, private key fingerprint. Those would need to be migrated over on a separate PR. We need to remove the rest of the uses of SymmetricCryptoKey from the client crates, then we can remove the internal boxing they are doing, as the keys would be protected by the KeyStore instead. Secrets Manager code should be moved to using the Encryptable/Decryptable interface, as it's encrypting fields one by one, I'll look into doing that on a separate PR. ## โฐ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## ๐ฆฎ Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - ๐ (`:+1:`) or similar for great changes - ๐ (`:memo:`) or โน๏ธ (`:information_source:`) for notes or general info - โ (`:question:`) for questions - ๐ค (`:thinking:`) or ๐ญ (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - ๐จ (`:art:`) for suggestions / improvements - โ (`:x:`) orโ ๏ธ (`:warning:`) for more significant problems or concerns needing attention - ๐ฑ (`:seedling:`) or โป๏ธ (`:recycle:`) for future improvements or indications of technical debt - โ (`:pick:`) for minor or nitpick changes
๐๏ธ Tracking
https://bitwarden.atlassian.net/browse/PM-5693
๐ Objective
Migrated from bitwarden/sdk-sm#979
I've been looking into using
memfd_secret
to protect keys in memory on Linux.The
memfd_secret
API is similar to malloc in that we get some memory range allocated for us, but this memory is only visible to the process that has access to the file descriptor, which should protect us from any memory dumping from anything other than a kernel driver.https://man.archlinux.org/man/memfd_secret.2.en
Using this requires changing how we store keys, so this is the perfect moment to go through removing the raw keys from the API surface.
Changes
KeyRef
trait andSymmetricKeyRef
/AsymmetricKeyRef
subtraits to represent the key types. Also a small macro to quickly implement them.KeyRef
implementations are user defined types that implementsEq+Hash+Copy
, and don't contain any key materialKeyEncryptable/KeyDecryptable
are nowDecryptable/Encryptable
, and instead of taking the key as parameter, they take aCryptoServiceContext
and aKeyRef
. This way keys are never exposed to the clients.KeyLocator
trait is replaced byUsesKey
. This trait only returns theKeyRef
of the key required to decrypt it, instead of fetching the key itself.KeyStore
trait, with some simple CRUD methods. We have two implementations, one based onmemfd_secret
for Linux, and another one based on a Rust boxed slice, that also appliesmlock
if possible.mlock
andmemfd_secret
apply their protection over a specified memory area, we need a Rust data structure that is laid out linearly and also doesn't reallocate by itself. Also becausememfd_secret
allocates the memory for us, we can't use a std type like Vec (reason is the Vec must be allocated by the Rust allocator [ref]). This basically only leaves us with a Rust slice, on top of which we'd need to implement insert/get/delete. To allow for reuse and easier testing I've createdSliceKeyStore
, which implements the CRUD methods from theKeyStore
trait on top of a plain slice. Then the actualmlock
andmemfd_secret
implementations just need to implement a trait casting their data to a slice. The data is stored as a slice ofOption<(KeyRef, KeyMaterial)>
, and the keys are unique and sorted in the slice for easier lookups.CryptoService
, which contains theKeyStore
s and some encrypt/decrypt utility functions. From aCryptoService
you can also initialize aCryptoServiceContext
CryptoServiceContext
contains a read only view of the keys inside theCryptoService
, plus a read-write ephemeral key store, for use byDecryptable/Encryptable
implementations when they need to temporarily store some keys (Cipher keys, attachment keys, send keys...).Migrated the codebase to use these changes in a separate PR: bitwarden/sdk-sm#1117
๐ธ Screenshots
โฐ Reminders before review
team
๐ฆฎ Reviewer guidelines
:+1:
) or similar for great changes:memo:
) or โน๏ธ (:information_source:
) for notes or general info:question:
) for questions:thinking:
) or ๐ญ (:thought_balloon:
) for more open inquiry that's not quite a confirmedissue and could potentially benefit from discussion
:art:
) for suggestions / improvements:x:
) or:warning:
) for more significant problems or concerns needing attention:seedling:
) or โป๏ธ (:recycle:
) for future improvements or indications of technical debt:pick:
) for minor or nitpick changes