@@ -557,31 +557,46 @@ class VaultRepositoryImpl(
557
557
error = MissingPropertyException (" Biometric key" ),
558
558
)
559
559
val iv = authDiskSource.getUserBiometricInitVector(userId = userId)
560
+ val decryptedUserKey = iv
561
+ ?.let {
562
+ try {
563
+ cipher
564
+ .doFinal(biometricsKey.toByteArray(Charsets .ISO_8859_1 ))
565
+ .decodeToString()
566
+ } catch (e: GeneralSecurityException ) {
567
+ return VaultUnlockResult .BiometricDecodingError (error = e)
568
+ }
569
+ }
570
+ ? : biometricsKey
571
+ val encryptedBiometricsKey = if (iv == null ) {
572
+ // Attempting to setup an encrypted pin before unlocking, if this fails we send back
573
+ // the biometrics error and users will need to sign in another way and re-setup
574
+ // biometrics.
575
+ try {
576
+ cipher
577
+ .doFinal(biometricsKey.encodeToByteArray())
578
+ .toString(Charsets .ISO_8859_1 )
579
+ } catch (e: GeneralSecurityException ) {
580
+ return VaultUnlockResult .BiometricDecodingError (error = e)
581
+ }
582
+ } else {
583
+ null
584
+ }
560
585
return this
561
586
.unlockVaultForUser(
562
587
userId = userId,
563
588
initUserCryptoMethod = InitUserCryptoMethod .DecryptedKey (
564
- decryptedUserKey = iv
565
- ?.let {
566
- try {
567
- cipher
568
- .doFinal(biometricsKey.toByteArray(Charsets .ISO_8859_1 ))
569
- .decodeToString()
570
- } catch (e: GeneralSecurityException ) {
571
- return VaultUnlockResult .BiometricDecodingError (error = e)
572
- }
573
- }
574
- ? : biometricsKey,
589
+ decryptedUserKey = decryptedUserKey,
575
590
),
576
591
)
577
592
.also {
578
593
if (it is VaultUnlockResult .Success ) {
579
- if (iv == null ) {
594
+ encryptedBiometricsKey?.let {
595
+ // If this key is present, we store it and the associated IV for future use
596
+ // since we want to migrate the user to a more secure form of biometrics.
580
597
authDiskSource.storeUserBiometricUnlockKey(
581
598
userId = userId,
582
- biometricsKey = cipher
583
- .doFinal(biometricsKey.encodeToByteArray())
584
- .toString(Charsets .ISO_8859_1 ),
599
+ biometricsKey = it,
585
600
)
586
601
authDiskSource.storeUserBiometricInitVector(userId = userId, iv = cipher.iv)
587
602
}
0 commit comments