Skip to content

Commit 7a162ed

Browse files
committed
simpler pin validation
1 parent 89f2c54 commit 7a162ed

File tree

3 files changed

+37
-41
lines changed

3 files changed

+37
-41
lines changed

crates/bitwarden-core/src/auth/auth_client.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,15 @@ impl AuthClient {
162162
}
163163

164164
/// Validates a PIN against a PIN-protected user key envelope.
165-
/// Returns `false` if the PIN fails to decrypt the envelope.
166-
/// Requires the user key to be present in the client, otherwise returns
167-
/// [`AuthValidateError::NotAuthenticated`].
165+
///
166+
/// Returns `false` if validation fails for any reason:
167+
/// - The PIN is incorrect
168+
/// - The envelope is corrupted or malformed
168169
pub fn validate_pin_protected_user_key_envelope(
169170
&self,
170171
pin: String,
171172
pin_protected_user_key_envelope: PasswordProtectedKeyEnvelope,
172-
) -> Result<bool, AuthValidateError> {
173+
) -> bool {
173174
validate_pin_protected_user_key_envelope(&self.client, pin, pin_protected_user_key_envelope)
174175
}
175176

crates/bitwarden-core/src/auth/pin.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use bitwarden_crypto::{EncString, PinKey, safe::PasswordProtectedKeyEnvelope};
2+
use tracing::info;
23

34
use crate::{
45
Client, NotAuthenticatedError,
@@ -42,28 +43,20 @@ pub(crate) fn validate_pin(
4243
}
4344
}
4445

46+
/// Validates a PIN-protected user key envelope by attempting to unseal it with the provided PIN.
4547
pub(crate) fn validate_pin_protected_user_key_envelope(
4648
client: &Client,
4749
pin: String,
4850
pin_protected_user_key_envelope: PasswordProtectedKeyEnvelope,
49-
) -> Result<bool, AuthValidateError> {
51+
) -> bool {
5052
let key_store = client.internal.get_key_store();
5153
let mut ctx = key_store.context();
5254

53-
if let Ok(decrypted_key_id) = pin_protected_user_key_envelope.unseal(pin.as_str(), &mut ctx) {
54-
#[allow(deprecated)]
55-
let Ok(decrypted_user_key) = ctx.dangerous_get_symmetric_key(decrypted_key_id) else {
56-
return Ok(false);
57-
};
58-
59-
#[allow(deprecated)]
60-
let user_key = ctx
61-
.dangerous_get_symmetric_key(SymmetricKeyId::User)
62-
.map_err(|_| NotAuthenticatedError)?;
63-
64-
Ok(*user_key == *decrypted_user_key)
55+
if let Err(e) = pin_protected_user_key_envelope.unseal(pin.as_str(), &mut ctx) {
56+
info!("Validating PIN-protected user key envelope failed: {e:?}");
57+
false
6558
} else {
66-
Ok(false)
59+
true
6760
}
6861
}
6962

@@ -136,7 +129,7 @@ mod tests {
136129
}
137130

138131
#[test]
139-
fn test_validate_pin_protected_user_key_envelope_valid() {
132+
fn test_validate_pin_protected_user_key_envelope_valid_pin() {
140133
let pin = "1234";
141134
let client = init_client();
142135

@@ -147,8 +140,7 @@ mod tests {
147140

148141
// Validate with the correct PIN
149142
let result = validate_pin_protected_user_key_envelope(&client, pin.to_string(), envelope);
150-
assert!(result.is_ok());
151-
assert!(result.unwrap());
143+
assert!(result);
152144
}
153145

154146
#[test]
@@ -166,28 +158,31 @@ mod tests {
166158
// Validate with the wrong PIN
167159
let result =
168160
validate_pin_protected_user_key_envelope(&client, wrong_pin.to_string(), envelope);
169-
assert!(result.is_ok());
170-
assert!(!result.unwrap());
161+
assert!(!result);
171162
}
172163

173164
#[test]
174-
fn test_validate_pin_protected_user_key_envelope_not_authenticated() {
165+
fn test_validate_pin_protected_user_key_malformed_envelope() {
175166
let pin = "1234";
176167

177-
// Create an envelope from a properly initialized client
178-
let initialized_client = init_client();
179-
let key_store = initialized_client.internal.get_key_store();
168+
let client = init_client();
169+
170+
// Create a PIN-protected envelope with the correct PIN
171+
let key_store = client.internal.get_key_store();
180172
let ctx = key_store.context();
181173
let envelope = PasswordProtectedKeyEnvelope::seal(SymmetricKeyId::User, pin, &ctx).unwrap();
182174

175+
let mut envelope_bytes: Vec<u8> = (&envelope).into();
176+
// Corrupt some bytes
177+
envelope_bytes[50] ^= 0xFF;
178+
179+
let envelope: PasswordProtectedKeyEnvelope =
180+
PasswordProtectedKeyEnvelope::try_from(&envelope_bytes).unwrap();
181+
183182
let client = Client::new(None);
184183

185184
// Validate should fail because no user key is present in this client
186185
let result = validate_pin_protected_user_key_envelope(&client, pin.to_string(), envelope);
187-
assert!(result.is_err());
188-
assert!(matches!(
189-
result.unwrap_err(),
190-
AuthValidateError::NotAuthenticated(_)
191-
));
186+
assert!(!result);
192187
}
193188
}

crates/bitwarden-uniffi/src/auth/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,22 @@ impl AuthClient {
117117
Ok(self.0.auth().validate_pin(pin, pin_protected_user_key)?)
118118
}
119119

120-
/// Validate the user PIN
120+
/// Validates a PIN against a PIN-protected user key envelope.
121121
///
122-
/// To validate the user PIN, you need to have the user's `pin_protected_user_key_envelope`.
123-
/// This key is obtained when enabling PIN unlock on the account with the `enroll_pin` method.
122+
/// The `pin_protected_user_key_envelope` key is obtained when enabling PIN unlock on the
123+
/// account with the [bitwarden_core::key_management::CryptoClient::enroll_pin] method.
124124
///
125-
/// This works by comparing the decrypted user key with the current user key, so the client must
126-
/// be unlocked.
125+
/// Returns `false` if validation fails for any reason:
126+
/// - The PIN is incorrect
127+
/// - The envelope is corrupted or malformed
127128
pub fn validate_pin_protected_user_key_envelope(
128129
&self,
129130
pin: String,
130131
pin_protected_user_key_envelope: PasswordProtectedKeyEnvelope,
131-
) -> Result<bool> {
132-
Ok(self
133-
.0
132+
) -> bool {
133+
self.0
134134
.auth()
135-
.validate_pin_protected_user_key_envelope(pin, pin_protected_user_key_envelope)?)
135+
.validate_pin_protected_user_key_envelope(pin, pin_protected_user_key_envelope)
136136
}
137137

138138
/// Initialize a new auth request

0 commit comments

Comments
 (0)