|
| 1 | +//! This example demonstrates how to securely protect keys with a password using the |
| 2 | +//! [PasswordProtectedKeyEnvelope]. |
| 3 | +
|
| 4 | +use bitwarden_crypto::{ |
| 5 | + key_ids, |
| 6 | + safe::{PasswordProtectedKeyEnvelope, PasswordProtectedKeyEnvelopeError}, |
| 7 | + KeyStore, KeyStoreContext, |
| 8 | +}; |
| 9 | + |
| 10 | +fn main() { |
| 11 | + let key_store = KeyStore::<ExampleIds>::default(); |
| 12 | + let mut ctx: KeyStoreContext<'_, ExampleIds> = key_store.context_mut(); |
| 13 | + let mut disk = MockDisk::new(); |
| 14 | + |
| 15 | + // Alice wants to protect a key with a password. |
| 16 | + // For example to: |
| 17 | + // - Protect her vault with a pin |
| 18 | + // - Protect her exported vault with a password |
| 19 | + // - Protect a send with a URL fragment secret |
| 20 | + // For this, the `PasswordProtectedKeyEnvelope` is used. |
| 21 | + |
| 22 | + // Alice has a vault protected with a symmetric key. She wants the symmetric key protected with |
| 23 | + // a PIN. |
| 24 | + let vault_key = ctx |
| 25 | + .generate_symmetric_key(ExampleSymmetricKey::VaultKey) |
| 26 | + .expect("Generating vault key should work"); |
| 27 | + |
| 28 | + // Seal the key with the PIN |
| 29 | + // The KDF settings are chosen for you, and do not need to be separately tracked or synced |
| 30 | + // Next, store this protected key envelope on disk. |
| 31 | + let pin = "1234"; |
| 32 | + let envelope = |
| 33 | + PasswordProtectedKeyEnvelope::seal(vault_key, pin, &ctx).expect("Sealing should work"); |
| 34 | + disk.save("vault_key_envelope", (&envelope).into()); |
| 35 | + |
| 36 | + // Wipe the context to simulate new session |
| 37 | + ctx.clear_local(); |
| 38 | + |
| 39 | + // Load the envelope from disk and unseal it with the PIN, and store it in the context. |
| 40 | + let deserialized: PasswordProtectedKeyEnvelope<ExampleIds> = |
| 41 | + PasswordProtectedKeyEnvelope::try_from( |
| 42 | + disk.load("vault_key_envelope") |
| 43 | + .expect("Loading from disk should work"), |
| 44 | + ) |
| 45 | + .expect("Deserializing envelope should work"); |
| 46 | + deserialized |
| 47 | + .unseal(ExampleSymmetricKey::VaultKey, pin, &mut ctx) |
| 48 | + .expect("Unsealing should work"); |
| 49 | + |
| 50 | + // Alice wants to change her password; also her KDF settings are below the minimums. |
| 51 | + // Re-sealing will update the password, and KDF settings. |
| 52 | + let envelope = envelope |
| 53 | + .reseal(pin, "0000") |
| 54 | + .expect("The password should be valid"); |
| 55 | + disk.save("vault_key_envelope", (&envelope).into()); |
| 56 | + |
| 57 | + // Alice wants to change the protected key. This requires creating a new envelope |
| 58 | + ctx.generate_symmetric_key(ExampleSymmetricKey::VaultKey) |
| 59 | + .expect("Generating vault key should work"); |
| 60 | + let envelope = PasswordProtectedKeyEnvelope::seal(ExampleSymmetricKey::VaultKey, "0000", &ctx) |
| 61 | + .expect("Sealing should work"); |
| 62 | + disk.save("vault_key_envelope", (&envelope).into()); |
| 63 | + |
| 64 | + // Alice tries the password but it is wrong |
| 65 | + assert!(matches!( |
| 66 | + envelope.unseal(ExampleSymmetricKey::VaultKey, "9999", &mut ctx), |
| 67 | + Err(PasswordProtectedKeyEnvelopeError::WrongPassword) |
| 68 | + )); |
| 69 | +} |
| 70 | + |
| 71 | +pub(crate) struct MockDisk { |
| 72 | + map: std::collections::HashMap<String, Vec<u8>>, |
| 73 | +} |
| 74 | + |
| 75 | +impl MockDisk { |
| 76 | + pub(crate) fn new() -> Self { |
| 77 | + MockDisk { |
| 78 | + map: std::collections::HashMap::new(), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + pub(crate) fn save(&mut self, key: &str, value: Vec<u8>) { |
| 83 | + self.map.insert(key.to_string(), value); |
| 84 | + } |
| 85 | + |
| 86 | + pub(crate) fn load(&self, key: &str) -> Option<&Vec<u8>> { |
| 87 | + self.map.get(key) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +key_ids! { |
| 92 | + #[symmetric] |
| 93 | + pub enum ExampleSymmetricKey { |
| 94 | + #[local] |
| 95 | + VaultKey |
| 96 | + } |
| 97 | + |
| 98 | + #[asymmetric] |
| 99 | + pub enum ExampleAsymmetricKey { |
| 100 | + Key(u8), |
| 101 | + } |
| 102 | + |
| 103 | + #[signing] |
| 104 | + pub enum ExampleSigningKey { |
| 105 | + Key(u8), |
| 106 | + } |
| 107 | + |
| 108 | + pub ExampleIds => ExampleSymmetricKey, ExampleAsymmetricKey, ExampleSigningKey; |
| 109 | +} |
0 commit comments