Commit c6e08b2
authored
[PM-5693] KeyStore implementation (#7)
## 🎟️ 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 1 parent c3962e5 commit c6e08b2
File tree
11 files changed
+1586
-0
lines changed- crates/bitwarden-crypto/src
- store
- backend
- implementation
- traits
11 files changed
+1586
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
26 | 30 | | |
27 | 31 | | |
28 | 32 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
83 | 87 | | |
84 | 88 | | |
85 | 89 | | |
| |||
Lines changed: 49 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
Lines changed: 28 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments