-
Notifications
You must be signed in to change notification settings - Fork 25
[PM-26459] Implement data envelope #336
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 4 commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
e2bb9c1
Make context local Ids autogenerated
dani-garcia 7218aba
Update docs
dani-garcia 81c7439
Merge branch 'main' into ps/context-local-uuids
dani-garcia ab25b95
Add comments
dani-garcia d9707e0
Fix tests
dani-garcia 175dbce
Merge branch 'main' into ps/context-local-uuids
dani-garcia 237cb20
Add signing keys
dani-garcia d892b90
Merge branch 'main' into ps/context-local-uuids
dani-garcia 7b2b49f
Introduce opaque local ids
dani-garcia 817cf6e
Fmt
dani-garcia c4cf8ec
Missing docs
dani-garcia f0c31ed
Merge branch 'main' into ps/context-local-uuids
dani-garcia b67aaca
Merge branch 'main' into ps/context-local-uuids
dani-garcia 16369db
Implement data envelope
quexten 5153a99
Cargo fmt
quexten 4dc80a2
Fix non wasm build
quexten 3387d2b
Fix clippy issues
quexten fcc2d9c
Clean up docs and fix wasm
quexten 5a8a4ec
Add versioned example
quexten 72f224f
Clean-up and key opts
quexten 1c7ee43
Verify key ops and add dataenvelope test vector
quexten 1c5b17f
Add comment to key opts
quexten a92fa6f
Add newline
quexten ca989d6
Merge branch 'main' into km/beeep/safe-data-envelope
quexten b244735
Move namespace to trait implementation
quexten 8ce366c
Merge branch 'km/beeep/safe-data-envelope' of github.com:bitwarden/sd…
quexten e59b40f
Require serialize and deserialize
quexten 11f8ee2
Merge branch 'main' into ps/context-local-uuids
quexten d37361b
Remove unused imports
quexten 476e6b3
Fix build
quexten 831a005
Generate versioned sealable enum via macro
quexten bcfef62
Add padding
quexten 217f649
Fix clippy issue
quexten 0b58542
Merge branch 'ps/context-local-uuids' into km/data-envelope-follow-up
quexten 99fbb45
Add convenience functions and fix build
quexten d704375
Fix clippy
quexten 2e78546
Fix build
quexten 8f57bec
Fix test
quexten da90552
Merge branch 'main' into km/beeep/safe-data-envelope
quexten 0ff2877
Merge branch 'main' into km/beeep/safe-data-envelope
quexten b23fcbf
Merge branch 'km/data-envelope-follow-up' into km/beeep/safe-data-env…
quexten 6c45d79
Merge branch 'km/beeep/safe-data-envelope' of github.com:bitwarden/sd…
quexten 6865a2f
Fix build
quexten 18ff1de
Fix build
quexten 8c8568b
Fix build
quexten 7322188
Remove crypto error variant for data envelope
quexten e8a9b2b
Add comment to vault item namespace
quexten 334f084
Pass through b64 error and derive clone
quexten 8b45178
Rename to supported_operations
quexten cbc84a3
Add jira ticket
quexten 8f0ea2b
Update crates/bitwarden-crypto/src/safe/data_envelope.rs
quexten 5ebdc2f
Update crates/bitwarden-crypto/src/safe/README.md
quexten 74cb980
Address feedback
quexten 6a73765
Merge branch 'main' into km/beeep/safe-data-envelope
quexten cfa3f4f
Fix formatting
quexten d044cf7
Apply feedback
quexten 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| //! This example demonstrates how to seal a piece of data. | ||
| //! | ||
| //! If there is a struct that should be kept secret, in can be sealed with a `DataEnvelope`. This | ||
| //! will automatically create a content-encryption-key. This is useful because the key is stored | ||
| //! separately. Rotating the encrypting key now only requires re-uploading the | ||
| //! content-encryption-key instead of the entire data. Further, server-side tampering (swapping of | ||
| //! individual fields encrypted by the same key) is prevented. | ||
| //! | ||
| //! In general, if a struct of data should be protected, the `DataEnvelope` should be used. | ||
|
|
||
| use bitwarden_crypto::{key_ids, safe::SealableData}; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| #[derive(Serialize, Deserialize)] | ||
| struct MyItem { | ||
| a: u64, | ||
| b: String, | ||
| } | ||
| impl SealableData for MyItem {} | ||
|
|
||
| fn main() { | ||
| let store = bitwarden_crypto::KeyStore::<ExampleIds>::default(); | ||
| let mut ctx: bitwarden_crypto::KeyStoreContext<'_, ExampleIds> = store.context_mut(); | ||
| let mut disk = MockDisk::new(); | ||
|
|
||
| let my_item = MyItem { | ||
| a: 42, | ||
| b: "Hello, World!".to_string(), | ||
| }; | ||
| // Seal the item into an encrypted blob, and store the content-encryption-key in the context. | ||
| let sealed_item = bitwarden_crypto::safe::DataEnvelope::seal( | ||
| my_item, | ||
| &bitwarden_crypto::safe::DataEnvelopeNamespace::VaultItem, | ||
| ExampleSymmetricKey::ItemKey, | ||
| &mut ctx, | ||
| ) | ||
| .expect("Sealing should work"); | ||
|
|
||
| // Store the sealed item on disk | ||
| disk.save("sealed_item", (&sealed_item).into()); | ||
| let sealed_item = disk | ||
| .load("sealed_item") | ||
| .expect("Failed to load sealed item") | ||
| .clone(); | ||
| let sealed_item: bitwarden_crypto::safe::DataEnvelope = | ||
| bitwarden_crypto::safe::DataEnvelope::from(sealed_item); | ||
|
|
||
| let my_item: MyItem = sealed_item | ||
| .unseal( | ||
| &bitwarden_crypto::safe::DataEnvelopeNamespace::VaultItem, | ||
| ExampleSymmetricKey::ItemKey, | ||
| &mut ctx, | ||
| ) | ||
| .expect("Unsealing should work"); | ||
| assert!(my_item.a == 42); | ||
| assert!(my_item.b == "Hello, World!"); | ||
| } | ||
|
|
||
| pub(crate) struct MockDisk { | ||
| map: std::collections::HashMap<String, Vec<u8>>, | ||
| } | ||
|
|
||
| impl MockDisk { | ||
| pub(crate) fn new() -> Self { | ||
| MockDisk { | ||
| map: std::collections::HashMap::new(), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn save(&mut self, key: &str, value: Vec<u8>) { | ||
| self.map.insert(key.to_string(), value); | ||
| } | ||
|
|
||
| pub(crate) fn load(&self, key: &str) -> Option<&Vec<u8>> { | ||
| self.map.get(key) | ||
| } | ||
| } | ||
|
|
||
| key_ids! { | ||
| #[symmetric] | ||
| pub enum ExampleSymmetricKey { | ||
| #[local] | ||
| ItemKey | ||
| } | ||
|
|
||
| #[asymmetric] | ||
| pub enum ExampleAsymmetricKey { | ||
| Key(u8), | ||
| } | ||
|
|
||
| #[signing] | ||
| pub enum ExampleSigningKey { | ||
| Key(u8), | ||
| } | ||
| pub ExampleIds => ExampleSymmetricKey, ExampleAsymmetricKey, ExampleSigningKey; | ||
| } |
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
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
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
Oops, something went wrong.
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.