mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-05-14 14:02:49 +00:00
Fix a crash on KitKat when clearing the KeyStore
Apparently KitKat doesn't like KeyPermanentlyInvalidatedException
This commit is contained in:
parent
4365a693f2
commit
6d93b78f9a
1 changed files with 24 additions and 11 deletions
|
@ -21,6 +21,8 @@ import javax.crypto.KeyGenerator;
|
||||||
import javax.crypto.NoSuchPaddingException;
|
import javax.crypto.NoSuchPaddingException;
|
||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
|
|
||||||
|
import androidx.annotation.RequiresApi;
|
||||||
|
|
||||||
public class KeyStoreHandle {
|
public class KeyStoreHandle {
|
||||||
private final KeyStore _keyStore;
|
private final KeyStore _keyStore;
|
||||||
private static final String STORE_NAME = "AndroidKeyStore";
|
private static final String STORE_NAME = "AndroidKeyStore";
|
||||||
|
@ -77,22 +79,33 @@ public class KeyStoreHandle {
|
||||||
throw new KeyStoreHandleException(e);
|
throw new KeyStoreHandleException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to initialize a dummy cipher
|
if (isSupported() && isKeyPermanentlyInvalidated(key)) {
|
||||||
// and see if KeyPermanentlyInvalidatedException is thrown
|
return null;
|
||||||
if (isSupported()) {
|
|
||||||
try {
|
|
||||||
Cipher cipher = Cipher.getInstance(CryptoUtils.CRYPTO_AEAD);
|
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, key);
|
|
||||||
} catch (KeyPermanentlyInvalidatedException e) {
|
|
||||||
return null;
|
|
||||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||||
|
private static boolean isKeyPermanentlyInvalidated(SecretKey key) {
|
||||||
|
// try to initialize a dummy cipher
|
||||||
|
// and see if KeyPermanentlyInvalidatedException is thrown
|
||||||
|
try {
|
||||||
|
Cipher cipher = Cipher.getInstance(CryptoUtils.CRYPTO_AEAD);
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, key);
|
||||||
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
|
||||||
|
// apparently KitKat doesn't like KeyPermanentlyInvalidatedException, even when guarded with a version check
|
||||||
|
// it will throw a java.lang.VerifyError when its listed in a 'catch' statement
|
||||||
|
// so instead, check for it here
|
||||||
|
if (e instanceof KeyPermanentlyInvalidatedException) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public void deleteKey(String id) throws KeyStoreHandleException {
|
public void deleteKey(String id) throws KeyStoreHandleException {
|
||||||
try {
|
try {
|
||||||
_keyStore.deleteEntry(id);
|
_keyStore.deleteEntry(id);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue