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] Migrate SDK to CryptoService #1117

Closed

Conversation

dani-garcia
Copy link
Member

@dani-garcia dani-garcia commented Oct 7, 2024

๐ŸŽŸ๏ธ Tracking

https://bitwarden.atlassian.net/browse/PM-5693

๐Ÿ“” Objective

Migrate the codebase to the new CryptoService introduced in #979.

EncryptedSettings was removed from Client, though it is still used to initialize the CryptoService. Ideally we'd move that initialization code over directly into the service 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.

โฐ 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 dani-garcia force-pushed the ps/secure-crypto-service-impl branch from 0dc0aa8 to 9a9a3ca Compare October 7, 2024 14:59
Copy link
Contributor

github-actions bot commented Oct 7, 2024

Logo
Checkmarx One โ€“ Scan Summary & Details โ€“ 41c0a2c4-7ed5-4cb2-bcf5-145f51b80c51

No New Or Fixed Issues Found

Copy link

codecov bot commented Oct 7, 2024

Codecov Report

Attention: Patch coverage is 59.19153% with 424 lines in your changes missing coverage. Please review.

Please upload report for BASE (ps/secure-crypto-service@f4ca816). Learn more about missing BASE report.

Files with missing lines Patch % Lines
crates/bitwarden-vault/src/cipher/login.rs 51.00% 49 Missing โš ๏ธ
crates/bitwarden-vault/src/cipher/identity.rs 0.00% 46 Missing โš ๏ธ
crates/bitwarden-vault/src/cipher/card.rs 0.00% 22 Missing โš ๏ธ
crates/bitwarden-fido/src/authenticator.rs 0.00% 20 Missing โš ๏ธ
crates/bitwarden-send/src/send.rs 79.38% 20 Missing โš ๏ธ
crates/bitwarden-vault/src/folder.rs 0.00% 18 Missing โš ๏ธ
crates/bitwarden-sm/src/secrets/create.rs 0.00% 17 Missing โš ๏ธ
crates/bitwarden-sm/src/secrets/update.rs 0.00% 17 Missing โš ๏ธ
crates/bitwarden-vault/src/cipher/cipher.rs 93.25% 17 Missing โš ๏ธ
crates/bitwarden-send/src/client_sends.rs 0.00% 16 Missing โš ๏ธ
... and 30 more
Additional details and impacted files
@@                     Coverage Diff                     @@
##             ps/secure-crypto-service    #1117   +/-   ##
===========================================================
  Coverage                            ?   59.88%           
===========================================================
  Files                               ?      201           
  Lines                               ?    14803           
  Branches                            ?        0           
===========================================================
  Hits                                ?     8865           
  Misses                              ?     5938           
  Partials                            ?        0           

โ˜” View full report in Codecov by Sentry.
๐Ÿ“ข Have feedback on the report? Share it here.

@dani-garcia dani-garcia force-pushed the ps/secure-crypto-service-impl branch 2 times, most recently from 16774b6 to a77c69e Compare October 8, 2024 10:45
# Conflicts:
#	crates/bitwarden-crypto/src/error.rs
#	crates/bitwarden-crypto/src/service/context.rs
@dani-garcia dani-garcia force-pushed the ps/secure-crypto-service-impl branch from a77c69e to 0c74d80 Compare October 8, 2024 10:48
@dani-garcia dani-garcia changed the title Migrate SDK to CryptoService [PM-5693] Migrate SDK to CryptoService Oct 8, 2024
@dani-garcia dani-garcia marked this pull request as ready for review October 8, 2024 10:57
@dani-garcia dani-garcia requested a review from a team as a code owner October 8, 2024 10:57
@dani-garcia dani-garcia requested review from Hinton and removed request for a team October 8, 2024 11:01
@dani-garcia dani-garcia deleted the ps/secure-crypto-service-impl branch October 22, 2024 16:14
dani-garcia added a commit to bitwarden/sdk-internal that referenced this pull request Feb 3, 2025
## ๐ŸŽŸ๏ธ 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](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.from_raw_parts)]).
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 `KeyStore`s 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

<!-- Required for any UI changes; delete if not applicable. Use fixed
width images for better display. -->

## โฐ 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
dani-garcia added a commit to bitwarden/sdk-internal 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.

1 participant