-
Notifications
You must be signed in to change notification settings - Fork 12
[PM-5693] KeyStore implementation #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
Merged
Merged
Changes from all commits
Commits
Show all changes
79 commits
Select commit
Hold shift + click to select a range
d7c7c3e
Initial CryptoService impl
dani-garcia 6068e84
More work
dani-garcia 50dc1b4
Refactor keystore to also handle resizes
dani-garcia 216eb25
Merge branch 'main' into ps/secure-crypto-service
dani-garcia 4b846ed
Fix
dani-garcia 7a01168
Paralelization plus alternative to locatekey
dani-garcia c649bf0
WASM support
dani-garcia b901ef7
Merge branch 'main' into ps/secure-crypto-service
dani-garcia e2129ad
Remove unnecessary trait
dani-garcia f708fcc
Inline cryptoengine
dani-garcia 30854f7
Impl KeyStore in Slice struct directly
dani-garcia bed894a
Reset the values to None after munlock has zeroized them
dani-garcia 709dfce
Merge branch 'main' into ps/secure-crypto-service
dani-garcia f7eda88
Fmt
dani-garcia ce2343e
Add benchmark
dani-garcia bcd712f
Respect no-memory-hardening flag
dani-garcia 38343d2
Fix cfg flags, silence warnings
dani-garcia f34ce02
Export memfd correctly
dani-garcia cc27320
Fix memtest
dani-garcia 281619d
Merge branch 'main' into ps/secure-crypto-service
dani-garcia 32d298f
Try fat LTO to fix windows
dani-garcia c43aa08
Disable memsec on windows to fix it
dani-garcia dd37d1d
Incorrect optional dep
dani-garcia 45f3d32
Try updating windows in memsec
dani-garcia 1f70169
Remove unnecessary bound
dani-garcia 22a8b17
Move store impl around a bit
dani-garcia c63f656
Make KeyStore pub
dani-garcia db3f8d4
Merge branch 'main' into ps/secure-crypto-service-impl
dani-garcia eb81684
Some renames/simplifications
dani-garcia d279626
Add some missing implementations
dani-garcia d5f1ede
Rename
dani-garcia 8652d79
Introduce mutable context
dani-garcia fdb0263
Refactor keyref/encryptable locations
dani-garcia 6ea6267
Some additions needed to migrate the code
dani-garcia 32088c7
Merge branch 'main' into ps/secure-crypto-service
dani-garcia f882fb2
Remove bench
dani-garcia bec4786
Remove using
dani-garcia c2ffea6
Fix TODO
dani-garcia c0d2b63
Comment
dani-garcia cacf4db
Merge branch 'main' into ps/secure-crypto-service
dani-garcia f4ca816
Fmt
dani-garcia 748ba75
Merge branch 'main' into ps/secure-crypto-service
dani-garcia 713ec80
Merge branch 'main' into ps/secure-crypto-service
dani-garcia 8f8e378
Merge branch 'main' into ps/secure-crypto-service
dani-garcia 14485e8
Documentation improvements, renames and refactors
dani-garcia 84c0aca
Fmt
dani-garcia f38398e
Fix lints and tests
dani-garcia fada9c1
Search/replace was a bit too powerful hah
dani-garcia f9ed542
Fix doclink
dani-garcia e7bfdc3
Add some more comments about why we're doing the custom slice
dani-garcia 34b256e
Add context doc
dani-garcia fe6b28f
Add more tests
dani-garcia 45c9ee2
Add more docs
dani-garcia ad8ed59
Merge branch 'main' into ps/secure-crypto-service
dani-garcia 75b97fa
Fix wrong test
dani-garcia 33ec078
Merge branch 'main' into ps/secure-crypto-service
dani-garcia 275574f
Add ignore dep
dani-garcia 1d8911a
Merge branch 'main' into ps/secure-crypto-service
dani-garcia b969352
Split backend implementations to a separate PR
dani-garcia 8fddf86
MissingKey2 -> MissingKeyRef
dani-garcia f03ba9e
Change visibilities
dani-garcia f8c8427
Upsert + docs
dani-garcia fb34e6c
Fmt
dani-garcia 7a1dc2d
KeyRef->KeyId
dani-garcia 67c32fa
Reorganize traits and split encryptable
dani-garcia 2360396
UsesKey -> IdentifyKey
dani-garcia 4e690e6
Merge branch 'main' into ps/secure-crypto-service
dani-garcia 4e6b707
Add default impl for KeyStore
dani-garcia cc1c27e
Add decryptable/encryptable tests
dani-garcia a2afdf3
Fmt
dani-garcia 5fa459d
Fix test and add docs
dani-garcia 628e81b
Add context usage docs
dani-garcia 548ccc4
Merge branch 'main' into ps/secure-crypto-service
dani-garcia 9bd664b
Doc updates
dani-garcia 8a37052
Merge branch 'main' into ps/secure-crypto-service
dani-garcia 379c581
Mod docs
dani-garcia 24b5ed7
Make context clear local only
dani-garcia 7e6f7a2
clear -> clear_local
dani-garcia 65b7d4f
Merge branch 'main' into ps/secure-crypto-service
dani-garcia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
crates/bitwarden-crypto/src/store/backend/implementation/basic.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use zeroize::ZeroizeOnDrop; | ||
|
||
use crate::{store::backend::StoreBackend, KeyId}; | ||
|
||
/// This is a basic key store backend that stores keys in a HashMap memory. | ||
/// No protections are provided for the keys stored in this backend, beyond enforcing | ||
/// zeroization on drop. | ||
pub(crate) struct BasicBackend<Key: KeyId> { | ||
keys: std::collections::HashMap<Key, Key::KeyValue>, | ||
} | ||
|
||
impl<Key: KeyId> BasicBackend<Key> { | ||
pub fn new() -> Self { | ||
Self { | ||
keys: std::collections::HashMap::new(), | ||
} | ||
} | ||
} | ||
|
||
impl<Key: KeyId> StoreBackend<Key> for BasicBackend<Key> { | ||
fn upsert(&mut self, key_id: Key, key: <Key as KeyId>::KeyValue) { | ||
self.keys.insert(key_id, key); | ||
} | ||
|
||
fn get(&self, key_id: Key) -> Option<&<Key as KeyId>::KeyValue> { | ||
self.keys.get(&key_id) | ||
} | ||
|
||
fn remove(&mut self, key_id: Key) { | ||
self.keys.remove(&key_id); | ||
} | ||
|
||
fn clear(&mut self) { | ||
self.keys.clear(); | ||
} | ||
|
||
fn retain(&mut self, f: fn(Key) -> bool) { | ||
self.keys.retain(|k, _| f(*k)); | ||
} | ||
} | ||
|
||
/// [KeyId::KeyValue] already implements [ZeroizeOnDrop], | ||
/// so we only need to ensure the map is cleared on drop. | ||
impl<Key: KeyId> ZeroizeOnDrop for BasicBackend<Key> {} | ||
impl<Key: KeyId> Drop for BasicBackend<Key> { | ||
fn drop(&mut self) { | ||
self.clear(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
crates/bitwarden-crypto/src/store/backend/implementation/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use super::StoreBackend; | ||
use crate::store::KeyId; | ||
|
||
mod basic; | ||
|
||
/// Initializes a key store backend with the best available implementation for the current platform | ||
pub fn create_store<Key: KeyId>() -> Box<dyn StoreBackend<Key>> { | ||
Box::new(basic::BasicBackend::<Key>::new()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{traits::tests::TestSymmKey, SymmetricCryptoKey}; | ||
|
||
#[test] | ||
fn test_creates_a_valid_store() { | ||
let mut store = create_store::<TestSymmKey>(); | ||
|
||
let key = SymmetricCryptoKey::generate(rand::thread_rng()); | ||
store.upsert(TestSymmKey::A(0), key.clone()); | ||
|
||
assert_eq!( | ||
store.get(TestSymmKey::A(0)).unwrap().to_base64(), | ||
key.to_base64() | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use zeroize::ZeroizeOnDrop; | ||
|
||
use crate::store::KeyId; | ||
|
||
mod implementation; | ||
|
||
pub use implementation::create_store; | ||
|
||
/// This trait represents a platform that can store and return keys. If possible, | ||
/// it will try to enable as many security protections on the keys as it can. | ||
/// The keys themselves implement [ZeroizeOnDrop], so the store will only need to make sure | ||
/// that the keys are dropped when they are no longer needed. | ||
/// | ||
/// The default implementation is a basic in-memory store that does not provide any security | ||
/// guarantees. | ||
/// | ||
/// We have other implementations in testing using `mlock` and `memfd_secret` for protecting keys in | ||
/// memory. | ||
/// | ||
/// Other implementations could use secure enclaves, HSMs or OS provided keychains. | ||
pub trait StoreBackend<Key: KeyId>: ZeroizeOnDrop + Send + Sync { | ||
/// Inserts a key into the store. If the key already exists, it will be replaced. | ||
fn upsert(&mut self, key_id: Key, key: Key::KeyValue); | ||
|
||
/// Retrieves a key from the store. | ||
fn get(&self, key_id: Key) -> Option<&Key::KeyValue>; | ||
|
||
#[allow(unused)] | ||
/// Removes a key from the store. | ||
fn remove(&mut self, key_id: Key); | ||
|
||
/// Removes all keys from the store. | ||
fn clear(&mut self); | ||
|
||
/// Retains only the elements specified by the predicate. | ||
/// In other words, remove all keys for which `f` returns false. | ||
fn retain(&mut self, f: fn(Key) -> bool); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.