Skip to content
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

Merged
merged 79 commits into from
Feb 3, 2025
Merged

Conversation

dani-garcia
Copy link
Member

๐ŸŽŸ๏ธ 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

  • Added a KeyRef trait and SymmetricKeyRef/AsymmetricKeyRef subtraits to represent the key types. Also a small macro to quickly implement them. KeyRef implementations are user defined types that implements Eq+Hash+Copy, and don't contain any key material
  • KeyEncryptable/KeyDecryptable are now Decryptable/Encryptable, and instead of taking the key as parameter, they take a CryptoServiceContext and a KeyRef. This way keys are never exposed to the clients.
  • KeyLocator trait is replaced by UsesKey. This trait only returns the KeyRef of the key required to decrypt it, instead of fetching the key itself.
  • Created a KeyStore trait, with some simple CRUD methods. We have two implementations, one based on memfd_secret for Linux, and another one based on a Rust boxed slice, that also applies mlock if possible.
  • Because both mlock and memfd_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 because memfd_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 created SliceKeyStore, which implements the CRUD methods from the KeyStore trait on top of a plain slice. Then the actual mlock and memfd_secret implementations just need to implement a trait casting their data to a slice. The data is stored as a slice of Option<(KeyRef, KeyMaterial)>, and the keys are unique and sorted in the slice for easier lookups.
  • Created CryptoService, which contains the KeyStores and some encrypt/decrypt utility functions. From a CryptoService you can also initialize a CryptoServiceContext
  • A CryptoServiceContext contains a read only view of the keys inside the CryptoService, plus a read-write ephemeral key store, for use by Decryptable/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

  • 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

  • ๐Ÿ‘ (:+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

@dani-garcia
Copy link
Member Author

Split the secure storage implementations to #125 so this is easier to review and merge.

@dani-garcia dani-garcia requested a review from Hinton January 22, 2025 14:43
Comment on lines +50 to +68
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)
}
}
Copy link
Member

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.

Suggested change
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 {}

Copy link
Member Author

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

Copy link
Member

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(
Copy link
Member

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?

Copy link
Member Author

@dani-garcia dani-garcia Jan 28, 2025

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)

@dani-garcia dani-garcia requested a review from Hinton February 3, 2025 13:17
@dani-garcia dani-garcia merged commit c6e08b2 into main Feb 3, 2025
40 checks passed
@dani-garcia dani-garcia deleted the ps/secure-crypto-service branch February 3, 2025 17:55
@dani-garcia dani-garcia changed the title [PM-5693] CryptoService using memfd_secret on Linux [PM-5693] KeyStore implementation Feb 3, 2025
dani-garcia added a commit that referenced this pull request Feb 7, 2025
## ๐ŸŽŸ๏ธ 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants