Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 44bb3b55 authored by Jack Yu's avatar Jack Yu
Browse files

Fixed ANR in PinStorage

Fixed ANR by moving keystore accessing into a separate thread
during boot up.

Test: atest PinStorageTest
Fix: 331549683
Flag: EXEMPT bug fix
Change-Id: Idd952730288b90ad5f444c7dd93ea63bdde1885f
parent de83bcf1
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@ import java.security.KeyStore;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.Executors;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
@@ -145,6 +146,7 @@ public class PinStorage extends Handler {
    private final int mBootCount;
    private final KeyStore mKeyStore;

    @Nullable
    private SecretKey mLongTermSecretKey;
    private SecretKey mShortTermSecretKey;

@@ -208,7 +210,9 @@ public class PinStorage extends Handler {
        // The short term key is retrieved later when needed.
        String alias = (!mIsDeviceSecure || mIsDeviceLocked)
                ? KEYSTORE_ALIAS_LONG_TERM_ALWAYS : KEYSTORE_ALIAS_LONG_TERM_USER_AUTH;
        mLongTermSecretKey = initializeSecretKey(alias, /*createIfAbsent=*/ true);
        // This is the main thread, so accessing keystore in a separate thread to prevent ANR.
        Executors.newSingleThreadExecutor().execute(() -> mLongTermSecretKey = initializeSecretKey(
                alias, /*createIfAbsent=*/ true));

        // If the device is not securee or is unlocked, we can start logic. Otherwise we need to
        // wait for the device to be unlocked and store any temporary PIN in RAM.
@@ -807,7 +811,7 @@ public class PinStorage extends Handler {
     * @return true if the operation was successful, false otherwise
     */
    private boolean savePinInformation(SharedPreferences.Editor editor, int slotId,
            StoredPin storedPin, String baseKey, SecretKey secretKey) {
            StoredPin storedPin, String baseKey, @Nullable SecretKey secretKey) {
        if (secretKey == null) {
            // Secret key for encryption is missing
            return false;
@@ -1150,7 +1154,7 @@ public class PinStorage extends Handler {
    }

    /** Returns the encrypted version of {@code input}, or an empty array in case of error. */
    private byte[] encrypt(SecretKey secretKey, byte[] input) {
    private byte[] encrypt(@Nullable SecretKey secretKey, byte[] input) {
        if (secretKey == null) {
            loge("Encrypt: Secret key is null");
            return new byte[0];