From 6e68d798163a8a44f55d90ee220e3b3ebb4cc6ec Mon Sep 17 00:00:00 2001 From: Alexander Bakker Date: Tue, 13 Feb 2018 19:27:40 +0100 Subject: [PATCH] Fix a crash on Kitkat. It doesn't like the KeyStoreHandle class for some reason --- .../main/java/me/impy/aegis/SlotHolder.java | 16 +++++++---- .../me/impy/aegis/SlotManagerActivity.java | 21 ++++++++------ .../me/impy/aegis/crypto/KeyStoreHandle.java | 28 +++++++++++++------ .../aegis/crypto/KeyStoreHandleException.java | 7 +++++ .../impy/aegis/helpers/FingerprintHelper.java | 14 ++++++---- 5 files changed, 57 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/me/impy/aegis/crypto/KeyStoreHandleException.java diff --git a/app/src/main/java/me/impy/aegis/SlotHolder.java b/app/src/main/java/me/impy/aegis/SlotHolder.java index 55fe2cdf..b2b9492b 100644 --- a/app/src/main/java/me/impy/aegis/SlotHolder.java +++ b/app/src/main/java/me/impy/aegis/SlotHolder.java @@ -7,10 +7,12 @@ import android.widget.LinearLayout; import android.widget.TextView; import me.impy.aegis.crypto.KeyStoreHandle; +import me.impy.aegis.crypto.KeyStoreHandleException; import me.impy.aegis.crypto.slots.FingerprintSlot; import me.impy.aegis.crypto.slots.PasswordSlot; import me.impy.aegis.crypto.slots.RawSlot; import me.impy.aegis.crypto.slots.Slot; +import me.impy.aegis.helpers.FingerprintHelper; public class SlotHolder extends RecyclerView.ViewHolder { private TextView _slotUsed; @@ -35,12 +37,14 @@ public class SlotHolder extends RecyclerView.ViewHolder { } else if (slot instanceof FingerprintSlot) { _slotName.setText("Finger"); _slotImg.setImageResource(R.drawable.ic_fingerprint_black_24dp); - try { - KeyStoreHandle keyStore = new KeyStoreHandle(); - if (keyStore.containsKey(slot.getID())) { - _slotUsed.setVisibility(View.VISIBLE); - } - } catch (Exception e) { } + if (FingerprintHelper.isSupported()) { + try { + KeyStoreHandle keyStore = new KeyStoreHandle(); + if (keyStore.containsKey(slot.getID())) { + _slotUsed.setVisibility(View.VISIBLE); + } + } catch (KeyStoreHandleException e) { } + } } else if (slot instanceof RawSlot) { _slotName.setText("Raw"); _slotImg.setImageResource(R.drawable.ic_vpn_key_black_24dp); diff --git a/app/src/main/java/me/impy/aegis/SlotManagerActivity.java b/app/src/main/java/me/impy/aegis/SlotManagerActivity.java index e09937bc..3793c8a7 100644 --- a/app/src/main/java/me/impy/aegis/SlotManagerActivity.java +++ b/app/src/main/java/me/impy/aegis/SlotManagerActivity.java @@ -1,6 +1,7 @@ package me.impy.aegis; import android.content.Intent; +import android.os.Build; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; @@ -9,12 +10,12 @@ import android.support.v7.widget.RecyclerView; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.widget.EditText; import android.widget.Toast; import javax.crypto.Cipher; import me.impy.aegis.crypto.KeyStoreHandle; +import me.impy.aegis.crypto.KeyStoreHandleException; import me.impy.aegis.crypto.MasterKey; import me.impy.aegis.crypto.slots.FingerprintSlot; import me.impy.aegis.crypto.slots.PasswordSlot; @@ -70,15 +71,19 @@ public class SlotManagerActivity extends AegisActivity implements SlotAdapter.Li // only show the fingerprint option if we can get an instance of the fingerprint manager // and if none of the slots in the collection has a matching alias in the keystore int visibility = View.VISIBLE; - try { - KeyStoreHandle keyStore = new KeyStoreHandle(); - for (FingerprintSlot slot : _slots.findAll(FingerprintSlot.class)) { - if (keyStore.containsKey(slot.getID()) && FingerprintHelper.getManager(this) != null) { - visibility = View.GONE; - break; + if (FingerprintHelper.isSupported()) { + try { + KeyStoreHandle keyStore = new KeyStoreHandle(); + for (FingerprintSlot slot : _slots.findAll(FingerprintSlot.class)) { + if (keyStore.containsKey(slot.getID()) && FingerprintHelper.getManager(this) != null) { + visibility = View.GONE; + break; + } } + } catch (KeyStoreHandleException e) { + visibility = View.GONE; } - } catch (Exception e) { + } else { visibility = View.GONE; } findViewById(R.id.button_add_fingerprint).setVisibility(visibility); diff --git a/app/src/main/java/me/impy/aegis/crypto/KeyStoreHandle.java b/app/src/main/java/me/impy/aegis/crypto/KeyStoreHandle.java index 7341ffc0..dbba7298 100644 --- a/app/src/main/java/me/impy/aegis/crypto/KeyStoreHandle.java +++ b/app/src/main/java/me/impy/aegis/crypto/KeyStoreHandle.java @@ -19,23 +19,29 @@ import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; -import me.impy.aegis.crypto.slots.FingerprintSlot; - public class KeyStoreHandle { private final KeyStore _keyStore; private static final String STORE_NAME = "AndroidKeyStore"; - public KeyStoreHandle() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException { - _keyStore = KeyStore.getInstance(STORE_NAME); - _keyStore.load(null); + public KeyStoreHandle() throws KeyStoreHandleException { + try { + _keyStore = KeyStore.getInstance(STORE_NAME); + _keyStore.load(null); + } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) { + throw new KeyStoreHandleException(e); + } } - public boolean containsKey(String id) throws KeyStoreException { - return _keyStore.containsAlias(id); + public boolean containsKey(String id) throws KeyStoreHandleException { + try { + return _keyStore.containsAlias(id); + } catch (KeyStoreException e) { + throw new KeyStoreHandleException(e); + } } public SecretKey generateKey(String id) throws Exception { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (isSupported()) { KeyGenerator generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, STORE_NAME); generator.init(new KeyGenParameterSpec.Builder(id, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) @@ -58,7 +64,7 @@ public class KeyStoreHandle { // try to initialize a dummy cipher // and see if KeyPermanentlyInvalidatedException is thrown - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (isSupported()) { try { @SuppressLint("GetInstance") Cipher cipher = Cipher.getInstance(CryptoUtils.CRYPTO_CIPHER_RAW); @@ -76,4 +82,8 @@ public class KeyStoreHandle { public void deleteKey(String id) throws KeyStoreException { _keyStore.deleteEntry(id); } + + public static boolean isSupported() { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; + } } diff --git a/app/src/main/java/me/impy/aegis/crypto/KeyStoreHandleException.java b/app/src/main/java/me/impy/aegis/crypto/KeyStoreHandleException.java new file mode 100644 index 00000000..424f4533 --- /dev/null +++ b/app/src/main/java/me/impy/aegis/crypto/KeyStoreHandleException.java @@ -0,0 +1,7 @@ +package me.impy.aegis.crypto; + +public class KeyStoreHandleException extends Exception { + public KeyStoreHandleException(Throwable cause) { + super(cause); + } +} diff --git a/app/src/main/java/me/impy/aegis/helpers/FingerprintHelper.java b/app/src/main/java/me/impy/aegis/helpers/FingerprintHelper.java index 20fa7fab..12cd200e 100644 --- a/app/src/main/java/me/impy/aegis/helpers/FingerprintHelper.java +++ b/app/src/main/java/me/impy/aegis/helpers/FingerprintHelper.java @@ -11,14 +11,16 @@ public class FingerprintHelper { } public static FingerprintManager getManager(Context context) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (PermissionHelper.granted(context, Manifest.permission.USE_FINGERPRINT)) { - FingerprintManager manager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE); - if (manager != null && manager.isHardwareDetected() && manager.hasEnrolledFingerprints()) { - return manager; - } + if (isSupported() && PermissionHelper.granted(context, Manifest.permission.USE_FINGERPRINT)) { + FingerprintManager manager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE); + if (manager != null && manager.isHardwareDetected() && manager.hasEnrolledFingerprints()) { + return manager; } } return null; } + + public static boolean isSupported() { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; + } }