Skip to content

Commit

Permalink
Handle failure to init biometrics crypto more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
bpellin committed Feb 14, 2020
1 parent 601976b commit 18e9de9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,18 @@ public boolean isFingerprintInitialized() {
return initOk;
}

public void initEncryptData() {
public boolean initEncryptData() {
cryptoInitOk = false;

if (!isFingerprintInitialized()) {
if (biometricCallback != null) {
biometricCallback.onException();
}
return;
return false;
}
try {
initEncryptKey(false);
return true;
} catch (final InvalidKeyException invalidKeyException) {
try {
biometricCallback.onKeyInvalidated();
Expand All @@ -142,6 +143,7 @@ public void initEncryptData() {
biometricCallback.onException();
}

return false;
}

private void initEncryptKey(
Expand Down Expand Up @@ -183,12 +185,13 @@ public Cipher getCipher() {
return cipher;
}

public void initDecryptData(
public boolean initDecryptData(
final String ivSpecValue) {

cryptoInitOk = false;
try {
initDecryptKey(ivSpecValue,false);
return true;
} catch (final InvalidKeyException invalidKeyException) {
// Key was invalidated (maybe all registered fingerprints were changed)
// Retry with new key
Expand All @@ -203,6 +206,8 @@ public void initDecryptData(
} catch (final Exception e) {
biometricCallback.onException();
}

return false;
}

private void initDecryptKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
biometricOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initDecryptData();
if (!initDecryptData()) {
return;
}
Cipher cipher = biometricHelper.getCipher();
// if cipher == null
biometricOpenPrompt.authenticate(loadPrompt, new BiometricPrompt.CryptoObject(cipher));
}
});
Expand Down Expand Up @@ -326,10 +327,10 @@ private boolean canceledBiometricAuth(int errorCode) {
}


private void initDecryptData() {
private boolean initDecryptData() {
final String ivSpecValue = prefsNoBackup.getString(getPreferenceKeyIvSpec(), null);

biometricHelper.initDecryptData(ivSpecValue);
return biometricHelper.initDecryptData(ivSpecValue);
}


Expand Down Expand Up @@ -742,7 +743,9 @@ public void onClick(
});
} else if (mSuccess) {
if (biometricCheck.isChecked()) {
biometricHelper.initEncryptData();
if (!biometricHelper.initEncryptData()) {
return;
}
Cipher cipher = biometricHelper.getCipher();

biometricSavePrompt.authenticate(savePrompt, new BiometricPrompt.CryptoObject(cipher));
Expand Down

0 comments on commit 18e9de9

Please sign in to comment.