11use bitwarden_crypto:: { EncString , PinKey , safe:: PasswordProtectedKeyEnvelope } ;
2+ use tracing:: info;
23
34use 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.
4547pub ( 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}
0 commit comments