Skip to content

Commit

Permalink
chore: [#126] simplify exception handling at BRKeyStore._getData and …
Browse files Browse the repository at this point in the history
…record exception to Firebase Crashlytics (#283)
  • Loading branch information
andhikayuana authored and kcw-grunt committed Nov 30, 2024
1 parent 933f520 commit 8bf8fca
Showing 1 changed file with 28 additions and 39 deletions.
67 changes: 28 additions & 39 deletions app/src/main/java/com/breadwallet/tools/security/BRKeyStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.breadwallet.tools.util.TypesConverter;
import com.breadwallet.tools.util.Utils;
import com.breadwallet.wallet.BRWalletManager;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
import com.platform.entities.WalletInfo;
import com.platform.tools.KVStoreManager;

Expand All @@ -36,6 +37,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyStore;
Expand Down Expand Up @@ -239,8 +241,11 @@ private synchronized static byte[] _getData(final Context context, String alias,
if (encryptedData != null) {
//new format data is present, good
byte[] iv = retrieveEncryptedData(context, alias_iv);
if (iv == null)
throw new NullPointerException("iv is missing when data isn't: " + alias);
if (iv == null) {
NullPointerException exception = new NullPointerException("iv is missing when data isn't: " + alias);
FirebaseCrashlytics.getInstance().recordException(exception);
return null;
}
Cipher outCipher;

outCipher = Cipher.getInstance(NEW_CIPHER_ALGORITHM);
Expand All @@ -251,8 +256,9 @@ private synchronized static byte[] _getData(final Context context, String alias,
return decryptedData;
}
} catch (IllegalBlockSizeException | BadPaddingException e) {
Timber.e(e);
throw new RuntimeException("failed to decrypt data: " + e.getMessage());
Timber.e(e, "failed to decrypt data: " + alias);
FirebaseCrashlytics.getInstance().recordException(e);
return null;
}
}
//no new format data, get the old one and migrate it to the new format
Expand All @@ -264,7 +270,9 @@ private synchronized static byte[] _getData(final Context context, String alias,
if (!fileExists) {
return null;/* file also not there, fine then */
}
Timber.e(new BRKeystoreErrorException("file is present but the key is gone: " + alias));
BRKeystoreErrorException exception = new BRKeystoreErrorException("file is present but the key is gone: " + alias);
Timber.e(exception);
FirebaseCrashlytics.getInstance().recordException(exception);
return null;
}

Expand All @@ -275,12 +283,15 @@ private synchronized static byte[] _getData(final Context context, String alias,
removeAliasAndFiles(keyStore, alias, context);
//report it if one exists and not the other.
if (ivExists != aliasExists) {
Timber.e(new BRKeystoreErrorException("alias or iv isn't on the disk: " + alias + ", aliasExists:" + aliasExists));
return null;
BRKeystoreErrorException exception = new BRKeystoreErrorException("alias or iv isn't on the disk: " + alias + ", aliasExists:" + aliasExists);
Timber.e(exception);
FirebaseCrashlytics.getInstance().recordException(exception);
} else {
Timber.e(new BRKeystoreErrorException("!ivExists && !aliasExists: " + alias));
return null;
BRKeystoreErrorException exception = new BRKeystoreErrorException("!ivExists && !aliasExists: " + alias);
Timber.e(exception);
FirebaseCrashlytics.getInstance().recordException(exception);
}
return null;
}

byte[] iv = readBytesFromFile(getFilePath(alias_iv, context));
Expand Down Expand Up @@ -309,36 +320,14 @@ private synchronized static byte[] _getData(final Context context, String alias,
storeEncryptedData(context, encryptedData, alias);
return result;

} catch (InvalidKeyException e) {
if (e instanceof UserNotAuthenticatedException) {
/** user not authenticated, ask the system for authentication */
Timber.e(e, "timber:_getData: showAuthenticationScreen: %s", alias);
showAuthenticationScreen(context, request_code, alias);
throw (UserNotAuthenticatedException) e;
} else {
Timber.e(e, "timber:_getData: InvalidKeyException");
if (e instanceof KeyPermanentlyInvalidatedException)
showKeyInvalidated(context);
throw new UserNotAuthenticatedException(); //just to not go any further
}
} catch (IOException | CertificateException | KeyStoreException e) {
/** keyStore.load(null) threw the Exception, meaning the keystore is unavailable */
Timber.d(e, "_getData: keyStore.load(null) threw the Exception, meaning the keystore is unavailable");
if (e instanceof FileNotFoundException) {
Timber.e(new RuntimeException("the key is present but the phrase on the disk no", e), "_getData: File not found exception");
throw new RuntimeException(e.getMessage());
} else {
Timber.e(e);
throw new RuntimeException(e.getMessage());
}
} catch (UnrecoverableKeyException | NoSuchAlgorithmException | NoSuchPaddingException |
InvalidAlgorithmParameterException e) {
/** if for any other reason the keystore fails, crash! */
Timber.e(e, "timber:getData: error");
throw new RuntimeException(e.getMessage());
} catch (BadPaddingException | IllegalBlockSizeException | NoSuchProviderException e) {
Timber.e(e);
throw new RuntimeException(e.getMessage());
} catch (UserNotAuthenticatedException e) {
Timber.e(e, "timber:_getData: showAuthenticationScreen: %s", alias);
showAuthenticationScreen(context, request_code, alias);
throw e;
} catch (GeneralSecurityException | IOException e) {
Timber.e(e, "timber:getData: error retrieving");
FirebaseCrashlytics.getInstance().recordException(e);
throw new IllegalStateException(e);
} finally {
lock.unlock();
}
Expand Down

0 comments on commit 8bf8fca

Please sign in to comment.