-
Notifications
You must be signed in to change notification settings - Fork 14
[PM-24127] Implement password-protected key envelope #335
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
Open
quexten
wants to merge
142
commits into
main
Choose a base branch
from
km/beeep/safe-password-protected-key-envelope
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+799
โ3
Open
Changes from all commits
Commits
Show all changes
142 commits
Select commit
Hold shift + click to select a range
bc5900b
Implement cose content format
quexten 2e905c4
Cargo fmt
quexten b41f5ef
Fix docs
quexten eb70dfc
Fix formatting
quexten 1e2e95c
Merge main
quexten 68be6e6
Fix formatting
quexten d6a18a4
Fix comment
quexten 6e9e526
Cleanup
quexten 50d8f70
Switch from pass by ref to pass by value for enum
quexten e5bd251
Add CompositeEncryptable trait
quexten b92010b
Cleanup
quexten 9635098
Merge branch 'main' into km/cose-content-format
quexten b8056c2
Fix typo
quexten 9440825
Typed encryptable
quexten f8cb804
Apply cargo fmt
quexten e6939e6
Fix build
quexten 2a202c1
Remove unused imports
quexten ba9e2c3
Rename to primitiveencryptablewithcontenttype
quexten a42b1e7
Set correct content type for pkcs8
quexten 5447cbb
Add cose keywrap
quexten 66c345f
Fix keywrap
quexten 9b2f6c9
Run cargo fmt
quexten 88b96b7
Fix encryptable
quexten 7e69b7b
Run cargo fmt
quexten b7d2bc8
Cleanup
quexten acb6d12
Rename to PrimitiveEncryptableWithoutContentFormat
quexten 723ec63
Rename to PrimitiveEncryptable
quexten 36d8de9
Merge branch 'main' into km/cose-content-format
quexten 33da07b
Add documentation for the encryptable traits
quexten 2861a9a
Merge branch 'km/cose-content-format' of github.com:bitwarden/sdk-intโฆ
quexten 1681df9
Fix clippy errors
quexten 0cd85fa
Fix docs
quexten f91f0b8
Cargo fmt
quexten 34ee00e
Fix docs
quexten 485b6d8
Fix docs
quexten f122ff0
Fix docs
quexten f22531c
Update crates/bitwarden-crypto/src/keys/utils.rs
quexten ba10631
Update crates/bitwarden-crypto/src/keys/utils.rs
quexten 69d8c57
Update crates/bitwarden-crypto/src/keys/utils.rs
quexten 8833146
Merge branch 'main' into km/cose-content-format
quexten 47ced5a
Merge branch 'main' into km/cose-content-format
quexten 42dccb0
Add docs
quexten 9d0ea55
Merge branch 'km/cose-content-format' of github.com:bitwarden/sdk-intโฆ
quexten a3ed9d6
Merge branch 'main' into km/cose-content-format
quexten 9eceb32
Cargo fmt
quexten f30c3ce
Apply fixes
quexten 5f4dc3a
Cleanup
quexten 7b17ca3
Cleanup
quexten 38c8945
Cleanup
quexten b5dd862
Apply more fixes
quexten 47c7764
Apply fixes
quexten b009c81
Apply fixes
quexten c9f6111
Update test vector to include content type
quexten b9b0f6e
Add bitwarden legacy content type
quexten 67dd5e9
Move content format to separate file
quexten 7635cd0
Remove unused import
quexten ec14e51
Fix missing parsing for content type
quexten 75be6db
Merge branch 'main' into km/cose-content-format
quexten 6d8bb8f
Fix doc error
quexten 38b8958
Typed byte arrays
quexten 9f334fb
Fix readme
quexten 02cc7b3
Clippy cleanup
quexten f145eb3
Fix clippy errors
quexten 99d909c
Fix clippy errors
quexten d47e8c0
Switch bitwarden symmetric crypto key bytes to serialized bytes generic
quexten 1920a49
Rename to bytes
quexten 625b830
Cargo fmt
quexten 24f1431
Simplify encoded symmetric key
quexten 9eb4ff8
Merge branch 'main' into km/cose-content-format
quexten 916a46e
Remove unwrap in example
quexten 8722077
Merge branch 'km/cose-content-format' of github.com:bitwarden/sdk-intโฆ
quexten 670c6a7
Cleanup
quexten 6da4a0b
Fix docs
quexten 4e5510b
Merge branch 'main' into km/cose-content-format
quexten b00f48b
Make content format trait sealed and add type aliases
quexten bf9f8c4
Merge branch 'km/cose-content-format' of github.com:bitwarden/sdk-intโฆ
quexten 235f3bc
Apply cargo fmt
quexten 1953ba3
Apply fixes
quexten 41bb1ad
Replace non type aliased Bytes references with type aliases
quexten c41e513
Apply clippy fixes
quexten 90d2295
Update crates/bitwarden-crypto/src/enc_string/symmetric.rs
quexten de8f957
Update crates/bitwarden-crypto/src/keys/signed_public_key.rs
quexten f6ad513
Update crates/bitwarden-crypto/src/enc_string/symmetric.rs
quexten 3c2984b
Update crates/bitwarden-crypto/src/fingerprint.rs
quexten bdc90b3
Update crates/bitwarden-crypto/src/signing/signed_object.rs
quexten 80dde40
Update crates/bitwarden-crypto/src/signing/signed_object.rs
quexten 1f30896
Update crates/bitwarden-crypto/README.md
quexten 1817ae0
Update crates/bitwarden-core/src/key_management/crypto.rs
quexten 416dbf5
Update crates/bitwarden-core/src/client/encryption_settings.rs
quexten aaad503
Fix build and cleanup
quexten 2e21906
Remove to_vec from VerifyingKey usages
quexten dbdee15
Undo take
quexten 375fd0d
Unapply allow missing docs
quexten fd22ded
Clean up KeyEncryptable or pin key
quexten 7ee0278
Cleanup
quexten 9b3549a
Apply cleanup
quexten a5abaae
Undo changes to crypto init
quexten 81138ea
Apply allow private interfaces to content format
quexten 4445aec
Cleanup
quexten 25d1907
Typesafe base64 handling
quexten 0d0f59b
Cleanup
quexten e3ee279
Cleanup cose sign1 types
quexten bf7bc82
Revert "Typesafe base64 handling"
quexten 1da0173
Revert "Cleanup"
quexten 87f87a6
Cleanup
quexten 15ffca9
Clippy fix
quexten 144620f
Move use under internal flag
quexten 53aeeee
Merge branch 'main' into km/cose-content-format
quexten 81b5a15
Update crates/bitwarden-core/src/client/encryption_settings.rs
quexten 9427634
Update crates/bitwarden-core/src/client/encryption_settings.rs
quexten d1f8029
Move cose content format trait higher
quexten 7f52fb0
Add docs
quexten 53c528b
Update crates/bitwarden-crypto/src/keys/symmetric_crypto_key.rs
quexten c51e779
Update crates/bitwarden-crypto/src/keys/device_key.rs
quexten 6c8092a
Update crates/bitwarden-crypto/src/keys/symmetric_crypto_key.rs
quexten 954a6a5
Clean up symmetric crypto key
quexten 948baa5
Merge branch 'km/cose-content-format' of github.com:bitwarden/sdk-intโฆ
quexten b2f5211
Fix encryptable docs
quexten f0b6ec5
Cleanup
quexten 390463c
Small cleanup
quexten a2e243e
Implement password-protected key envelope
quexten 616786e
Add cfg(test) to function
quexten b913762
Cargo format
quexten 120f8c6
Fix clippy errors
quexten b1a615a
Merge branch 'main' into km/beeep/safe-password-protected-key-envelope
quexten 25700ef
Fix private const
quexten 690c6df
Cleanup and add tests
quexten fabee16
Fix clippy errors
quexten b81de59
Merge branch 'main' into km/beeep/safe-password-protected-key-envelope
quexten 1fef06f
Update crates/bitwarden-crypto/examples/protect_key_with_password.rs
quexten b85806a
Update crates/bitwarden-crypto/examples/protect_key_with_password.rs
quexten 7848fa6
Update crates/bitwarden-crypto/examples/protect_key_with_password.rs
quexten 32aacd7
Update crates/bitwarden-crypto/src/safe/password_protected_key_enveloโฆ
quexten 6fd28c2
Update crates/bitwarden-crypto/src/safe/password_protected_key_enveloโฆ
quexten d601b0c
Update crates/bitwarden-crypto/src/safe/README.md
quexten 6b11472
Update crates/bitwarden-crypto/src/safe/README.md
quexten b8198bf
Fix typo
quexten 0892687
Re-generate test vectors
quexten 3e03ec3
Cargo fmt
quexten 9345408
Prettier fix formatting
quexten 6fb4e97
Fix link formatting
quexten f685b35
Merge branch 'main' into km/beeep/safe-password-protected-key-envelope
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"encryptable", | ||
"Hkdf", | ||
"Hmac", | ||
"keyslot", | ||
"Maybeable", | ||
"Oaep", | ||
"Pbkdf", | ||
|
118 changes: 118 additions & 0 deletions
118
crates/bitwarden-crypto/examples/protect_key_with_password.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,118 @@ | ||
//! This example demonstrates how to securely protect keys with a password using the | ||
//! [PasswordProtectedKeyEnvelope]. | ||
|
||
use bitwarden_crypto::{ | ||
key_ids, | ||
safe::{PasswordProtectedKeyEnvelope, PasswordProtectedKeyEnvelopeError}, | ||
KeyStore, KeyStoreContext, | ||
}; | ||
|
||
fn main() { | ||
let key_store = KeyStore::<ExampleIds>::default(); | ||
let mut ctx: KeyStoreContext<'_, ExampleIds> = key_store.context_mut(); | ||
let mut disk = MockDisk::new(); | ||
|
||
// Alice wants to protect a key with a password. | ||
// For example to: | ||
// - Protect her vault with a pin | ||
// - Protect her exported vault with a password | ||
// - Protect a send with a URL fragment secret | ||
// For this, the `PasswordProtectedKeyEnvelope` is used. | ||
|
||
// Alice has a vault protected with a symmetric key. She wants the symmetric key protected with | ||
// a PIN. | ||
let vault_key = ctx | ||
.generate_symmetric_key(ExampleSymmetricKey::VaultKey) | ||
.expect("Generating vault key should work"); | ||
|
||
// Seal the key with the PIN | ||
// The KDF settings are chosen for you, and do not need to be separately tracked or synced | ||
// Next, store this protected key envelope on disk. | ||
let pin = "1234"; | ||
let envelope = | ||
PasswordProtectedKeyEnvelope::seal(vault_key, pin, &ctx).expect("Sealing should work"); | ||
disk.save( | ||
"vault_key_envelope", | ||
(&envelope).try_into().expect("Saving envelope should work"), | ||
); | ||
|
||
// Wipe the context to simulate new session | ||
ctx.clear_local(); | ||
|
||
// Load the envelope from disk and unseal it with the PIN, and store it in the context. | ||
let deserialized: PasswordProtectedKeyEnvelope<ExampleIds> = | ||
PasswordProtectedKeyEnvelope::try_from( | ||
disk.load("vault_key_envelope") | ||
.expect("Loading from disk should work"), | ||
) | ||
.expect("Deserializing envelope should work"); | ||
deserialized | ||
.unseal(ExampleSymmetricKey::VaultKey, pin, &mut ctx) | ||
.expect("Unsealing should work"); | ||
|
||
// Alice wants to change her password; also her KDF settings are below the minimums. | ||
// Re-sealing will update the password, and KDF settings. | ||
let envelope = envelope | ||
.reseal(pin, "0000") | ||
.expect("The password should be valid"); | ||
disk.save( | ||
"vault_key_envelope", | ||
(&envelope).try_into().expect("Saving envelope should work"), | ||
); | ||
|
||
// Alice wants to change the protected key. This requires creating a new envelope | ||
ctx.generate_symmetric_key(ExampleSymmetricKey::VaultKey) | ||
.expect("Generating vault key should work"); | ||
let envelope = PasswordProtectedKeyEnvelope::seal(ExampleSymmetricKey::VaultKey, "0000", &ctx) | ||
Thomas-Avery marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.expect("Sealing should work"); | ||
disk.save( | ||
"vault_key_envelope", | ||
(&envelope).try_into().expect("Saving envelope should work"), | ||
); | ||
|
||
// Alice tries the password but it is wrong | ||
assert!(matches!( | ||
envelope.unseal(ExampleSymmetricKey::VaultKey, "9999", &mut ctx), | ||
Err(PasswordProtectedKeyEnvelopeError::WrongPassword) | ||
)); | ||
} | ||
|
||
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] | ||
VaultKey | ||
} | ||
|
||
#[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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Bitwarden-crypto safe module | ||
|
||
The safe module provides high-level cryptographic tools for building secure protocols and features. | ||
When developing new features, use this module first before considering lower-level primitives from | ||
other parts of `bitwarden-crypto`. | ||
|
||
## Password-protected key envelope | ||
|
||
Use the password protected key envelope to protect a symmetric key with a password. Examples | ||
include: | ||
|
||
- locking a vault with a PIN/Password | ||
- protecting exports with a password | ||
|
||
Internally, the module uses a KDF to protect against brute-forcing, but it does not expose this to | ||
the consumer. The consumer only provides a password and key. |
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,4 @@ | ||
#![doc = include_str!("./README.md")] | ||
|
||
mod password_protected_key_envelope; | ||
pub use password_protected_key_envelope::*; |
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.