From 0382744f7601fe65e68aa4e54abcfaba7c08e8d1 Mon Sep 17 00:00:00 2001 From: Andhika Yuana Date: Fri, 15 Nov 2024 22:09:50 +0700 Subject: [PATCH] fix: [#264] add null checking for BRKeyStore.removeAliasAndFiles (#270) --- .../tools/security/BRKeyStore.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/breadwallet/tools/security/BRKeyStore.java b/app/src/main/java/com/breadwallet/tools/security/BRKeyStore.java index ceb33ed9b..c8c7a7fcc 100644 --- a/app/src/main/java/com/breadwallet/tools/security/BRKeyStore.java +++ b/app/src/main/java/com/breadwallet/tools/security/BRKeyStore.java @@ -20,11 +20,9 @@ import com.breadwallet.R; import com.breadwallet.exceptions.BRKeystoreErrorException; import com.breadwallet.presenter.customviews.BRDialogView; -import com.breadwallet.tools.animation.BRAnimator; import com.breadwallet.tools.animation.BRDialog; import com.breadwallet.tools.manager.BRSharedPrefs; import com.breadwallet.tools.threads.BRExecutor; -import com.breadwallet.tools.util.BRConstants; import com.breadwallet.tools.util.BytesUtil; import com.breadwallet.tools.util.TypesConverter; import com.breadwallet.tools.util.Utils; @@ -333,7 +331,8 @@ private synchronized static byte[] _getData(final Context context, String alias, Timber.e(e); throw new RuntimeException(e.getMessage()); } - } catch (UnrecoverableKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException e) { + } 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()); @@ -715,8 +714,15 @@ public synchronized static boolean resetWalletKeyStore(Context context) { public synchronized static void removeAliasAndFiles(KeyStore keyStore, String alias, Context context) { try { keyStore.deleteEntry(alias); - boolean b1 = new File(getFilePath(aliasObjectMap.get(alias).datafileName, context)).delete(); - boolean b2 = new File(getFilePath(aliasObjectMap.get(alias).ivFileName, context)).delete(); + + AliasObject aliasObject = aliasObjectMap.get(alias); + if (aliasObject == null) { + Timber.w("aliasObject for alias: %s is null, skipping deletion", alias); + return; + } + + boolean b1 = new File(getFilePath(aliasObject.datafileName, context)).delete(); + boolean b2 = new File(getFilePath(aliasObject.ivFileName, context)).delete(); } catch (KeyStoreException e) { Timber.e(e); } @@ -930,7 +936,8 @@ public synchronized static byte[] _getOldData(final Context context, String alia /** keyStore.load(null) threw the Exception, meaning the keystore is unavailable */ Timber.e(e, "timber:_getOldData: keyStore.load(null) threw the Exception, meaning the keystore is unavailable"); return null; - } catch (UnrecoverableKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException e) { + } catch (UnrecoverableKeyException | NoSuchAlgorithmException | NoSuchPaddingException | + InvalidAlgorithmParameterException e) { /** if for any other reason the keystore fails, crash! */ Timber.e(e, "timber:getData: error"); return null;