Skip to content

Commit

Permalink
Allow check for keys on pre M devices
Browse files Browse the repository at this point in the history
Copied the old plugin logic in and made it conditional
in the same way as when creating a keypair
  • Loading branch information
oddcb committed Sep 19, 2019
1 parent c2669ed commit 8143cee
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/android/RSA.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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
*
Expand Down

0 comments on commit 8143cee

Please sign in to comment.