Skip to content

Commit fabee16

Browse files
committed
Fix clippy errors
1 parent 690c6df commit fabee16

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

crates/bitwarden-crypto/examples/protect_key_with_password.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,52 @@ fn main() {
2222
// Alice has a vault protected with a symmetric key. She wants this protected with a PIN.
2323
let vault_key = ctx
2424
.generate_symmetric_key(ExampleSymmetricKey::VaultKey)
25-
.unwrap();
25+
.expect("Generating vault key should work");
2626

2727
// Seal the key with the PIN
2828
// The KDF settings are chosen for you, and do not need to be separately tracked or synced
2929
// Next, story this protected key envelope on disk.
3030
let pin = "1234";
3131
let envelope =
3232
PasswordProtectedKeyEnvelope::seal(vault_key, pin, &ctx).expect("Sealing should work");
33-
disk.save("vault_key_envelope", (&envelope).try_into().unwrap());
33+
disk.save(
34+
"vault_key_envelope",
35+
(&envelope).try_into().expect("Saving envelope should work"),
36+
);
3437

3538
// Wipe the context to simulate new session
3639
ctx.clear_local();
3740

3841
// Load the envelope from disk and unseal it with the PIN, and store it in the context.
3942
let deserialized: PasswordProtectedKeyEnvelope<ExampleIds> =
40-
PasswordProtectedKeyEnvelope::try_from(disk.load("vault_key_envelope").unwrap()).unwrap();
43+
PasswordProtectedKeyEnvelope::try_from(
44+
disk.load("vault_key_envelope")
45+
.expect("Loading from disk should work"),
46+
)
47+
.expect("Deserializing envelope should work");
4148
deserialized
4249
.unseal(ExampleSymmetricKey::VaultKey, pin, &mut ctx)
43-
.unwrap();
50+
.expect("Unsealing should work");
4451

4552
// Alice wants to change her password; also her KDF settings are below the minimums.
4653
// Re-sealing will update the password, and KDF settings.
4754
let envelope = envelope
4855
.reseal(pin, "0000")
4956
.expect("The password should be valid");
50-
disk.save("vault_key_envelope", (&envelope).try_into().unwrap());
57+
disk.save(
58+
"vault_key_envelope",
59+
(&envelope).try_into().expect("Saving envelope should work"),
60+
);
5161

5262
// Alice wants to change the protected key. This requires creating a new envelope
5363
ctx.generate_symmetric_key(ExampleSymmetricKey::VaultKey)
54-
.unwrap();
64+
.expect("Generating vault key should work");
5565
let envelope = PasswordProtectedKeyEnvelope::seal(ExampleSymmetricKey::VaultKey, "0000", &ctx)
5666
.expect("Sealing should work");
57-
disk.save("vault_key_envelope", (&envelope).try_into().unwrap());
67+
disk.save(
68+
"vault_key_envelope",
69+
(&envelope).try_into().expect("Saving envelope should work"),
70+
);
5871

5972
// Alice tries the password but it is wrong
6073
assert!(matches!(

crates/bitwarden-crypto/src/safe/password_protected_key_envelope.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ impl<Ids: KeyIds> PasswordProtectedKeyEnvelope<Ids> {
123123
) -> Result<Ids::Symmetric, PasswordProtectedKeyEnvelopeError> {
124124
let key = self.unseal_ref(password)?;
125125
#[allow(deprecated)]
126-
ctx.set_symmetric_key(target_keyslot, key).unwrap();
126+
ctx.set_symmetric_key(target_keyslot, key)
127+
.map_err(|_| PasswordProtectedKeyEnvelopeError::KeyStoreError)?;
127128
Ok(target_keyslot)
128129
}
129130

@@ -156,16 +157,20 @@ impl<Ids: KeyIds> PasswordProtectedKeyEnvelope<Ids> {
156157
})?;
157158
let envelope_key = derive_key(&kdf_settings, password)
158159
.map_err(|_| PasswordProtectedKeyEnvelopeError::KdfError)?;
160+
let nonce: [u8; 24] = self
161+
.cose_encrypt
162+
.unprotected
163+
.iv
164+
.clone()
165+
.try_into()
166+
.map_err(|_| {
167+
PasswordProtectedKeyEnvelopeError::ParsingError("Invalid IV".to_string())
168+
})?;
159169

160170
let key_bytes = self
161171
.cose_encrypt
162172
.decrypt(&[], |data, aad| {
163-
xchacha20::decrypt_xchacha20_poly1305(
164-
&self.cose_encrypt.unprotected.iv.clone().try_into().unwrap(),
165-
&envelope_key,
166-
data,
167-
aad,
168-
)
173+
xchacha20::decrypt_xchacha20_poly1305(&nonce, &envelope_key, data, aad)
169174
})
170175
// If decryption fails, the envelope-key is incorrect and thus the password is incorrect
171176
// since the KDF parameters & salt are guaranteed to be correct
@@ -263,13 +268,16 @@ impl Argon2RawSettings {
263268
}
264269
}
265270

266-
impl Into<Header> for &Argon2RawSettings {
267-
fn into(self) -> Header {
271+
impl From<&Argon2RawSettings> for Header {
272+
fn from(settings: &Argon2RawSettings) -> Header {
268273
let builder = HeaderBuilder::new()
269-
.value(ARGON2_ITERATIONS, Integer::from(self.iterations).into())
270-
.value(ARGON2_MEMORY, Integer::from(self.memory).into())
271-
.value(ARGON2_PARALLELISM, Integer::from(self.parallelism).into())
272-
.value(ARGON2_SALT, Value::from(self.salt.to_vec()));
274+
.value(ARGON2_ITERATIONS, Integer::from(settings.iterations).into())
275+
.value(ARGON2_MEMORY, Integer::from(settings.memory).into())
276+
.value(
277+
ARGON2_PARALLELISM,
278+
Integer::from(settings.parallelism).into(),
279+
)
280+
.value(ARGON2_SALT, Value::from(settings.salt.to_vec()));
273281

274282
let mut header = builder.build();
275283
header.alg = Some(coset::Algorithm::PrivateUse(ALG_ARGON2ID13));
@@ -345,6 +353,9 @@ pub enum PasswordProtectedKeyEnvelopeError {
345353
/// There is no key for the provided key id in the key store
346354
#[error("Key missing error")]
347355
KeyMissingError,
356+
/// The key store could not be written to, for example due to being read-only
357+
#[error("Could not write to key store")]
358+
KeyStoreError,
348359
}
349360

350361
impl From<CoseExtractError> for PasswordProtectedKeyEnvelopeError {

0 commit comments

Comments
 (0)