From 8143ceec05bb3d693fab1650c9edc2aaf53970ad Mon Sep 17 00:00:00 2001 From: Odd Christer Brovig Date: Thu, 19 Sep 2019 19:42:52 +0200 Subject: [PATCH] Allow check for keys on pre M devices Copied the old plugin logic in and made it conditional in the same way as when creating a keypair --- src/android/RSA.java | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/android/RSA.java b/src/android/RSA.java index 2e66a9ad..749ea372 100644 --- a/src/android/RSA.java +++ b/src/android/RSA.java @@ -3,15 +3,15 @@ import android.annotation.TargetApi; import android.content.Context; import android.os.Build; +import android.security.KeyPairGeneratorSpec; +import android.security.keystore.KeyGenParameterSpec; import android.security.keystore.KeyInfo; +import android.security.keystore.KeyProperties; import android.security.keystore.UserNotAuthenticatedException; import android.util.Log; -import android.security.keystore.KeyGenParameterSpec; -import android.security.keystore.KeyProperties; -import android.security.KeyPairGeneratorSpec; - - +import javax.crypto.Cipher; +import javax.security.auth.x500.X500Principal; import java.math.BigInteger; import java.security.Key; import java.security.KeyFactory; @@ -21,11 +21,6 @@ import java.security.spec.RSAKeyGenParameterSpec; import java.util.Calendar; -import javax.crypto.Cipher; -import javax.security.auth.x500.X500Principal; - -import static org.apache.cordova.CordovaActivity.TAG; - public class RSA { private static final String KEYSTORE_PROVIDER = "AndroidKeyStore"; private static final Cipher CIPHER = getCipher(); @@ -55,7 +50,13 @@ public static void createKeyPair(Context ctx, String alias, Integer userAuthenti * @param alias * @return boolean */ - public static boolean encryptionKeysAvailable(String alias) { + static boolean encryptionKeysAvailable(String alias) { + return IS_API_23_AVAILABLE ? isEntryAvailable(alias) : isEntryAvailableLegacy(alias); + } + + + @TargetApi(Build.VERSION_CODES.M) + private static boolean isEntryAvailable(String alias) { try { Key privateKey = loadKey(Cipher.DECRYPT_MODE, alias); if (privateKey == null) { @@ -71,6 +72,15 @@ public static boolean encryptionKeysAvailable(String alias) { } } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + private static boolean isEntryAvailableLegacy(String alias) { + try { + return loadKey(Cipher.ENCRYPT_MODE, alias) != null; + } catch (Exception e) { + return false; + } + } /** * Check if we need to prompt for User's Credentials *