diff --git a/android/src/main/java/com/oblador/keychain/KeychainModule.java b/android/src/main/java/com/oblador/keychain/KeychainModule.java index 7ed295e..08c122f 100644 --- a/android/src/main/java/com/oblador/keychain/KeychainModule.java +++ b/android/src/main/java/com/oblador/keychain/KeychainModule.java @@ -58,9 +58,6 @@ public class KeychainModule extends ReactContextBaseJavaModule { throw new EmptyParameterException("you passed empty or null username/password"); } CipherStorage currentCipherStorage = getCipherStorageForCurrentAPILevel(); - if (currentCipherStorage == null) { - throw new CryptoFailedException("Unsupported Android SDK " + Build.VERSION.SDK_INT); - } EncryptionResult result = currentCipherStorage.encrypt(service, username, password); prefsStorage.storeEncryptedEntry(service, result); @@ -79,9 +76,6 @@ public class KeychainModule extends ReactContextBaseJavaModule { public void getGenericPasswordForOptions(String service, Promise promise) { try { CipherStorage currentCipherStorage = getCipherStorageForCurrentAPILevel(); - if (currentCipherStorage == null) { - throw new CryptoFailedException("Unsupported Android SDK " + Build.VERSION.SDK_INT); - } final DecryptionResult decryptionResult; ResultSet resultSet = prefsStorage.getEncryptedEntry(service); @@ -161,18 +155,21 @@ public class KeychainModule extends ReactContextBaseJavaModule { } // The "Current" CipherStorage is the cipherStorage with the highest API level that is lower than or equal to the current API level - private CipherStorage getCipherStorageForCurrentAPILevel() { + private CipherStorage getCipherStorageForCurrentAPILevel() throws CryptoFailedException { int currentAPILevel = Build.VERSION.SDK_INT; CipherStorage currentCipherStorage = null; for (CipherStorage cipherStorage : cipherStorageMap.values()) { - int cipherStorageAPILevel = cipherStorage.getAPILevel(); + int cipherStorageAPILevel = cipherStorage.getMinSupportedApiLevel(); // Is the cipherStorage supported on the current API level? boolean isSupported = (cipherStorageAPILevel <= currentAPILevel); // Is the API level better than the one we previously selected (if any)? - if (isSupported && (currentCipherStorage == null || cipherStorageAPILevel > currentCipherStorage.getAPILevel())) { + if (isSupported && (currentCipherStorage == null || cipherStorageAPILevel > currentCipherStorage.getMinSupportedApiLevel())) { currentCipherStorage = cipherStorage; } } + if (currentCipherStorage == null) { + throw new CryptoFailedException("Unsupported Android SDK " + Build.VERSION.SDK_INT); + } return currentCipherStorage; } diff --git a/android/src/main/java/com/oblador/keychain/PrefsStorage.java b/android/src/main/java/com/oblador/keychain/PrefsStorage.java index 2dce843..e46206a 100644 --- a/android/src/main/java/com/oblador/keychain/PrefsStorage.java +++ b/android/src/main/java/com/oblador/keychain/PrefsStorage.java @@ -10,6 +10,7 @@ import com.oblador.keychain.cipherStorage.CipherStorageFacebookConceal; public class PrefsStorage { public static final String KEYCHAIN_DATA = "RN_KEYCHAIN"; + public static final String EMPTY_STRING = ""; static public class ResultSet { public final String cipherStorageName; @@ -30,6 +31,8 @@ public class PrefsStorage { } public ResultSet getEncryptedEntry(String service) { + service = service == null ? EMPTY_STRING : service; + byte[] bytesForUsername = getBytesForUsername(service); byte[] bytesForPassword = getBytesForPassword(service); String cipherStorageName = getCipherStorageName(service); @@ -44,6 +47,8 @@ public class PrefsStorage { } public void resetPassword(String service) { + service = service == null ? EMPTY_STRING : service; + String keyForUsername = getKeyForUsername(service); String keyForPassword = getKeyForPassword(service); String keyForCipherStorage = getKeyForCipherStorage(service); @@ -55,6 +60,8 @@ public class PrefsStorage { } public void storeEncryptedEntry(String service, EncryptionResult encryptionResult) { + service = service == null ? EMPTY_STRING : service; + String keyForUsername = getKeyForUsername(service); String keyForPassword = getKeyForPassword(service); String keyForCipherStorage = getKeyForCipherStorage(service); diff --git a/android/src/main/java/com/oblador/keychain/cipherStorage/CipherStorage.java b/android/src/main/java/com/oblador/keychain/cipherStorage/CipherStorage.java index bb76944..58a78ac 100644 --- a/android/src/main/java/com/oblador/keychain/cipherStorage/CipherStorage.java +++ b/android/src/main/java/com/oblador/keychain/cipherStorage/CipherStorage.java @@ -37,5 +37,5 @@ public interface CipherStorage { String getCipherStorageName(); - int getAPILevel(); + int getMinSupportedApiLevel(); } diff --git a/android/src/main/java/com/oblador/keychain/cipherStorage/CipherStorageFacebookConceal.java b/android/src/main/java/com/oblador/keychain/cipherStorage/CipherStorageFacebookConceal.java index 9a706d6..6cd6822 100644 --- a/android/src/main/java/com/oblador/keychain/cipherStorage/CipherStorageFacebookConceal.java +++ b/android/src/main/java/com/oblador/keychain/cipherStorage/CipherStorageFacebookConceal.java @@ -30,7 +30,7 @@ public class CipherStorageFacebookConceal implements CipherStorage { } @Override - public int getAPILevel() { + public int getMinSupportedApiLevel() { return Build.VERSION_CODES.JELLY_BEAN; } diff --git a/android/src/main/java/com/oblador/keychain/cipherStorage/CipherStorageKeystoreAESCBC.java b/android/src/main/java/com/oblador/keychain/cipherStorage/CipherStorageKeystoreAESCBC.java index bbe375f..8ce398a 100644 --- a/android/src/main/java/com/oblador/keychain/cipherStorage/CipherStorageKeystoreAESCBC.java +++ b/android/src/main/java/com/oblador/keychain/cipherStorage/CipherStorageKeystoreAESCBC.java @@ -47,7 +47,7 @@ public class CipherStorageKeystoreAESCBC implements CipherStorage { } @Override - public int getAPILevel() { + public int getMinSupportedApiLevel() { return Build.VERSION_CODES.M; } diff --git a/android/src/main/java/com/oblador/keychain/exceptions/KeyStoreAccessException.java b/android/src/main/java/com/oblador/keychain/exceptions/KeyStoreAccessException.java index c50314f..74585e1 100644 --- a/android/src/main/java/com/oblador/keychain/exceptions/KeyStoreAccessException.java +++ b/android/src/main/java/com/oblador/keychain/exceptions/KeyStoreAccessException.java @@ -1,10 +1,6 @@ package com.oblador.keychain.exceptions; public class KeyStoreAccessException extends Exception { - public KeyStoreAccessException(Throwable t) { - super(t); - } - public KeyStoreAccessException(String message, Throwable t) { super(message, t); }