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

Commit cffde72f authored by arunvoddu's avatar arunvoddu
Browse files

Refactor the ImsiEncryption key download scenarios.

1. Retry the download in fail scenarios like device screen is locked or Internet is not avilable.
2. Do not delete the Key from the DB when a sim is disabled/removed where same operetor sim is active in another slot.
3. Optimize the logic not to execute on every carrierConfig change call.

Bug: 320955199
Test: Verified manually also atest tests/telephonytests/src/com/android/internal/telephony/CarrierKeyDownloadMgrTest.java
Change-Id: Ic77c8f923cda1728e81db9e64a15ca86b73d2069
parent fb9fa4b8
Loading
Loading
Loading
Loading
+26 −4
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;

import com.android.internal.telephony.flags.Flags;
import com.android.internal.telephony.metrics.TelephonyMetrics;

import java.security.PublicKey;
@@ -297,7 +298,12 @@ public class CarrierInfoManager {
        final TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class)
                .createForSubscriptionId(subId);
        int carrierId = telephonyManager.getSimCarrierId();
        if (Flags.imsiKeyRetryDownloadOnPhoneUnlock()) {
            String simOperator = telephonyManager.getSimOperator();
            deleteCarrierInfoForImsiEncryption(context, subId, carrierId, simOperator);
        } else {
            deleteCarrierInfoForImsiEncryption(context, subId, carrierId);
        }
        Intent resetIntent = new Intent(TelephonyIntents.ACTION_CARRIER_CERTIFICATE_DOWNLOAD);
        SubscriptionManager.putPhoneIdAndSubIdExtra(resetIntent, mPhoneId);
        context.sendBroadcastAsUser(resetIntent, UserHandle.ALL);
@@ -312,13 +318,29 @@ public class CarrierInfoManager {
     */
    public static void deleteCarrierInfoForImsiEncryption(Context context, int subId,
            int carrierId) {
        deleteCarrierInfoForImsiEncryption(context, subId, carrierId, null);
    }

    /**
     * Deletes all the keys for a given Carrier from the device keystore.
     * @param context Context
     * @param subId SubscriptionId
     * @param carrierId delete the key which matches the carrierId
     * @param simOperator delete the key which matches the MCCMNC
     *
     */
    public static void deleteCarrierInfoForImsiEncryption(Context context, int subId,
            int carrierId, String simOperator) {
        Log.i(LOG_TAG, "deleting carrier key from db for subId=" + subId);
        String mcc = "";
        String mnc = "";

        final TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class)
        if (TextUtils.isEmpty(simOperator)) {
            final TelephonyManager telephonyManager = context.getSystemService(
                            TelephonyManager.class)
                    .createForSubscriptionId(subId);
        String simOperator = telephonyManager.getSimOperator();
            simOperator = telephonyManager.getSimOperator();
        }
        if (!TextUtils.isEmpty(simOperator)) {
            mcc = simOperator.substring(0, 3);
            mnc = simOperator.substring(3);
+309 −118

File changed.

Preview size limit exceeded, changes collapsed.

+7 −2
Original line number Diff line number Diff line
@@ -539,7 +539,7 @@ public class GsmCdmaPhone extends Phone {
        mContext.registerReceiver(mBroadcastReceiver, filter,
                android.Manifest.permission.MODIFY_PHONE_STATE, null, Context.RECEIVER_EXPORTED);

        mCDM = new CarrierKeyDownloadManager(this, mFeatureFlags);
        mCDM = new CarrierKeyDownloadManager(this);
        mCIM = new CarrierInfoManager();

        mCi.registerForImeiMappingChanged(this, EVENT_IMEI_MAPPING_CHANGED, null);
@@ -2124,8 +2124,13 @@ public class GsmCdmaPhone extends Phone {

    @Override
    public void deleteCarrierInfoForImsiEncryption(int carrierId) {
        CarrierInfoManager.deleteCarrierInfoForImsiEncryption(mContext, getSubId(), carrierId);
    }

    @Override
    public void deleteCarrierInfoForImsiEncryption(int carrierId, String simOperator) {
        CarrierInfoManager.deleteCarrierInfoForImsiEncryption(mContext, getSubId(),
                carrierId);
                carrierId, simOperator);
    }

    @Override
+9 −0
Original line number Diff line number Diff line
@@ -4025,6 +4025,15 @@ public abstract class Phone extends Handler implements PhoneInternalInterface {
        return;
    }

    /**
     * Deletes all the keys for a given Carrier from the device keystore.
     * @param carrierId : the carrier ID which needs to be matched in the delete query
     * @param simOperator : MccMnc which needs to be matched in the delete query.
     */
    public void deleteCarrierInfoForImsiEncryption(int carrierId, String simOperator) {

    }

    public int getCarrierId() {
        return TelephonyManager.UNKNOWN_CARRIER_ID;
    }
+35 −26
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.times;
@@ -31,6 +32,8 @@ import static org.mockito.Mockito.when;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.ImsiEncryptionInfo;
@@ -38,18 +41,18 @@ import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.text.TextUtils;
import android.util.Pair;

import androidx.test.filters.SmallTest;

import com.android.internal.telephony.flags.FeatureFlags;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;

import java.security.PublicKey;
@@ -91,7 +94,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
                    + "\"public-key\": \"" + CERT + "\"}]}";

    private CarrierConfigManager.CarrierConfigChangeListener mCarrierConfigChangeListener;
    private FeatureFlags mFeatureFlags;

    @Before
    public void setUp() throws Exception {
        logd("CarrierActionAgentTest +Setup!");
@@ -99,15 +102,18 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
        mBundle = mContextFixture.getCarrierConfigBundle();
        when(mCarrierConfigManager.getConfigForSubId(anyInt(), any())).thenReturn(mBundle);
        when(mUserManager.isUserUnlocked()).thenReturn(true);
        when(mKeyguardManager.isDeviceLocked()).thenReturn(false);
        // Capture listener to emulate the carrier config change notification used later
        ArgumentCaptor<CarrierConfigManager.CarrierConfigChangeListener> listenerArgumentCaptor =
                ArgumentCaptor.forClass(CarrierConfigManager.CarrierConfigChangeListener.class);
        mFeatureFlags = Mockito.mock(FeatureFlags.class);
        mCarrierKeyDM = new CarrierKeyDownloadManager(mPhone, mFeatureFlags);
        mCarrierKeyDM = new CarrierKeyDownloadManager(mPhone);
        verify(mCarrierConfigManager).registerCarrierConfigChangeListener(any(),
                listenerArgumentCaptor.capture());
        mCarrierConfigChangeListener = listenerArgumentCaptor.getAllValues().get(0);

        mConnectivityManager = (ConnectivityManager) mPhone.getContext().getSystemService(
                Context.CONNECTIVITY_SERVICE);
        Network network = Mockito.mock(Network.class);
        when(mConnectivityManager.getActiveNetwork()).thenReturn(network);
        processAllMessages();
        logd("CarrierActionAgentTest -Setup!");
    }
@@ -331,8 +337,8 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
        expectedCal.add(Calendar.DATE, 1);
        String dateExpected = dt.format(expectedCal.getTime());

        when(mTelephonyManager.getSimOperator(anyInt())).thenReturn("310260");
        when(mTelephonyManager.getSimCarrierId()).thenReturn(1);
        when(mPhone.getOperatorNumeric()).thenReturn("310260");
        when(mPhone.getCarrierId()).thenReturn(1);
        Intent mIntent = new Intent(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        mIntent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, downloadId);
        mContext.sendBroadcast(mIntent);
@@ -355,11 +361,11 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
        bundle.putInt(CarrierConfigManager.IMSI_KEY_AVAILABILITY_INT, 3);
        bundle.putString(CarrierConfigManager.IMSI_KEY_DOWNLOAD_URL_STRING, mURL);

        when(mTelephonyManager.getSimOperator(anyInt())).thenReturn("310260");
        when(mTelephonyManager.getSimCarrierId()).thenReturn(1);
        when(mPhone.getOperatorNumeric()).thenReturn("310260");
        when(mPhone.getCarrierId()).thenReturn(1);
        mCarrierConfigChangeListener.onCarrierConfigChanged(0 /* slotIndex */,
                SubscriptionManager.INVALID_SUBSCRIPTION_ID,
                TelephonyManager.UNKNOWN_CARRIER_ID, TelephonyManager.UNKNOWN_CARRIER_ID);
                1, TelephonyManager.UNKNOWN_CARRIER_ID);
        processAllMessages();
        assertEquals("310260", mCarrierKeyDM.mMccMncForDownload);
        assertEquals(1, mCarrierKeyDM.mCarrierId);
@@ -369,6 +375,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
    @SmallTest
    public void testCarrierConfigChangedWithUserLocked() {
        when(mUserManager.isUserUnlocked()).thenReturn(false);
        when(mKeyguardManager.isDeviceLocked()).thenReturn(true);
        CarrierConfigManager carrierConfigManager = (CarrierConfigManager)
                mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
        int slotId = mPhone.getPhoneId();
@@ -376,14 +383,14 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
        bundle.putInt(CarrierConfigManager.IMSI_KEY_AVAILABILITY_INT, 3);
        bundle.putString(CarrierConfigManager.IMSI_KEY_DOWNLOAD_URL_STRING, mURL);

        when(mTelephonyManager.getSimOperator(anyInt())).thenReturn("310260");
        when(mTelephonyManager.getSimCarrierId()).thenReturn(1);
        when(mPhone.getOperatorNumeric()).thenReturn("310260");
        when(mPhone.getCarrierId()).thenReturn(1);
        mCarrierConfigChangeListener.onCarrierConfigChanged(0 /* slotIndex */,
                SubscriptionManager.INVALID_SUBSCRIPTION_ID,
                TelephonyManager.UNKNOWN_CARRIER_ID, TelephonyManager.UNKNOWN_CARRIER_ID);
                1, TelephonyManager.UNKNOWN_CARRIER_ID);
        processAllMessages();
        assertNull(mCarrierKeyDM.mMccMncForDownload);
        assertEquals(0, mCarrierKeyDM.mCarrierId);
        assertEquals("310260", mCarrierKeyDM.mMccMncForDownload);
        assertEquals(1, mCarrierKeyDM.mCarrierId);
    }

    @Test
@@ -391,6 +398,7 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
    public void testUserLockedAfterCarrierConfigChanged() {
        // User is locked at beginning
        when(mUserManager.isUserUnlocked()).thenReturn(false);
        when(mKeyguardManager.isDeviceLocked()).thenReturn(true);
        CarrierConfigManager carrierConfigManager = (CarrierConfigManager)
                mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
        int slotId = mPhone.getPhoneId();
@@ -399,17 +407,18 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
        bundle.putString(CarrierConfigManager.IMSI_KEY_DOWNLOAD_URL_STRING, mURL);

        // Carrier config change received
        when(mTelephonyManager.getSimOperator(anyInt())).thenReturn("310260");
        when(mTelephonyManager.getSimCarrierId()).thenReturn(1);
        when(mPhone.getOperatorNumeric()).thenReturn("310260");
        when(mPhone.getCarrierId()).thenReturn(1);
        mCarrierConfigChangeListener.onCarrierConfigChanged(0 /* slotIndex */,
                SubscriptionManager.INVALID_SUBSCRIPTION_ID,
                TelephonyManager.UNKNOWN_CARRIER_ID, TelephonyManager.UNKNOWN_CARRIER_ID);
                1, TelephonyManager.UNKNOWN_CARRIER_ID);
        processAllMessages();

        // User unlocked event received
        Intent mIntent = new Intent(Intent.ACTION_USER_UNLOCKED);
        Intent mIntent = new Intent(Intent.ACTION_USER_PRESENT);
        mContext.sendBroadcast(mIntent);
        when(mUserManager.isUserUnlocked()).thenReturn(true);
        when(mKeyguardManager.isDeviceLocked()).thenReturn(false);
        processAllMessages();

        assertEquals("310260", mCarrierKeyDM.mMccMncForDownload);
@@ -432,11 +441,11 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {

        mCarrierConfigChangeListener.onCarrierConfigChanged(0 /* slotIndex */,
                SubscriptionManager.INVALID_SUBSCRIPTION_ID,
                TelephonyManager.UNKNOWN_CARRIER_ID, TelephonyManager.UNKNOWN_CARRIER_ID);
                1, TelephonyManager.UNKNOWN_CARRIER_ID);
        processAllMessages();
        assertNull(mCarrierKeyDM.mMccMncForDownload);
        assertTrue(TextUtils.isEmpty(mCarrierKeyDM.mMccMncForDownload));

        verify(mPhone).deleteCarrierInfoForImsiEncryption(0);
        verify(mPhone).deleteCarrierInfoForImsiEncryption(1, "");
    }

    /**
@@ -453,8 +462,8 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
        bundle.putInt(CarrierConfigManager.IMSI_KEY_AVAILABILITY_INT, 3);
        bundle.putString(CarrierConfigManager.IMSI_KEY_DOWNLOAD_URL_STRING, mURL);

        when(mTelephonyManager.getSimOperator(anyInt())).thenReturn("310260");
        when(mTelephonyManager.getSimCarrierId()).thenReturn(1);
        when(mPhone.getOperatorNumeric()).thenReturn("310260");
        when(mPhone.getCarrierId()).thenReturn(1);
        Intent mIntent = new Intent("com.android.internal.telephony.carrier_key_download_alarm");
        mIntent.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, slotIndex);
        mContext.sendBroadcast(mIntent);