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

Commit 462d62a8 authored by Jayachandran C's avatar Jayachandran C Committed by Gerrit Code Review
Browse files

Merge "Added carrier_id support to the carrier_key table in CarrierInformation...

Merge "Added carrier_id support to the carrier_key table in CarrierInformation DB Also Updated the related test cases"
parents fe3bcab4 51f9e9a4
Loading
Loading
Loading
Loading
+59 −14
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ public class CarrierInfoManager {
    public static ImsiEncryptionInfo getCarrierInfoForImsiEncryption(int keyType,
                                                                     Context context,
                                                                     String operatorNumeric,
                                                                     int carrierId,
                                                                     boolean fallback,
                                                                     int subId) {
        String mcc = "";
@@ -88,7 +89,8 @@ public class CarrierInfoManager {
            ContentResolver mContentResolver = context.getContentResolver();
            String[] columns = {Telephony.CarrierColumns.PUBLIC_KEY,
                    Telephony.CarrierColumns.EXPIRATION_TIME,
                    Telephony.CarrierColumns.KEY_IDENTIFIER};
                    Telephony.CarrierColumns.KEY_IDENTIFIER,
                    Telephony.CarrierColumns.CARRIER_ID};
            findCursor = mContentResolver.query(Telephony.CarrierColumns.CONTENT_URI, columns,
                    "mcc=? and mnc=? and key_type=?",
                    new String[]{mcc, mnc, String.valueOf(keyType)}, null);
@@ -141,16 +143,29 @@ public class CarrierInfoManager {
                Pair<PublicKey, Long> keyInfo =
                        CarrierKeyDownloadManager.getKeyInformation(keyString.getBytes());
                return new ImsiEncryptionInfo(mcc, mnc, keyType, keyId,
                        keyInfo.first, new Date(keyInfo.second));
                        keyInfo.first, new Date(keyInfo.second), carrierId);
            }
            if (findCursor.getCount() > 1) {
                Log.e(LOG_TAG, "More than 1 row found for the keyType: " + keyType);
                // Lookup for the carrier_id
                String carrierIdStr = "";
                while (findCursor.moveToNext()) {
                    carrierIdStr = findCursor.getString(3);
                    int cursorCarrierId = (TextUtils.isEmpty(carrierIdStr))
                            ? TelephonyManager.UNKNOWN_CARRIER_ID : Integer.parseInt(
                            carrierIdStr);
                    if (cursorCarrierId != TelephonyManager.UNKNOWN_CARRIER_ID
                            && cursorCarrierId == carrierId) {
                        return getImsiEncryptionInfo(findCursor, mcc, mnc, keyType,
                                cursorCarrierId);
                    }
            byte[] carrier_key = findCursor.getBlob(0);
            Date expirationTime = new Date(findCursor.getLong(1));
            String keyIdentifier = findCursor.getString(2);
            return new ImsiEncryptionInfo(mcc, mnc, keyType, keyIdentifier, carrier_key,
                    expirationTime);
                }
                findCursor.moveToFirst();
            }
            String carrierIdStr = findCursor.getString(3);
            int cursorCarrierId = (TextUtils.isEmpty(carrierIdStr))
                    ? TelephonyManager.UNKNOWN_CARRIER_ID : Integer.parseInt(carrierIdStr);
            return getImsiEncryptionInfo(findCursor, mcc, mnc, keyType, cursorCarrierId);
        } catch (IllegalArgumentException e) {
            Log.e(LOG_TAG, "Bad arguments:" + e);
        } catch (Exception e) {
@@ -163,6 +178,22 @@ public class CarrierInfoManager {
        return null;
    }

    private static ImsiEncryptionInfo getImsiEncryptionInfo(Cursor findCursor, String mcc,
            String mnc, int keyType, int carrierId) {
        byte[] carrier_key = findCursor.getBlob(0);
        Date expirationTime = new Date(findCursor.getLong(1));
        String keyIdentifier = findCursor.getString(2);
        ImsiEncryptionInfo imsiEncryptionInfo = null;
        try {
            imsiEncryptionInfo = new ImsiEncryptionInfo(mcc, mnc,
                    keyType, keyIdentifier, carrier_key,
                    expirationTime, carrierId);
        } catch (Exception exp) {
            Log.e(LOG_TAG, "Exception = " + exp.getMessage());
        }
        return imsiEncryptionInfo;
    }

    /**
     * Inserts or update the Carrier Key in the database
     * @param imsiEncryptionInfo ImsiEncryptionInfo object.
@@ -178,6 +209,7 @@ public class CarrierInfoManager {
        ContentValues contentValues = new ContentValues();
        contentValues.put(Telephony.CarrierColumns.MCC, imsiEncryptionInfo.getMcc());
        contentValues.put(Telephony.CarrierColumns.MNC, imsiEncryptionInfo.getMnc());
        contentValues.put(Telephony.CarrierColumns.CARRIER_ID, imsiEncryptionInfo.getCarrierId());
        contentValues.put(Telephony.CarrierColumns.KEY_TYPE,
                imsiEncryptionInfo.getKeyType());
        contentValues.put(Telephony.CarrierColumns.KEY_IDENTIFIER,
@@ -200,10 +232,11 @@ public class CarrierInfoManager {
            try {
                int nRows = mContentResolver.update(Telephony.CarrierColumns.CONTENT_URI,
                        updatedValues,
                        "mcc=? and mnc=? and key_type=?", new String[]{
                        "mcc=? and mnc=? and key_type=? and carrier_id=?", new String[]{
                                imsiEncryptionInfo.getMcc(),
                                imsiEncryptionInfo.getMnc(),
                                String.valueOf(imsiEncryptionInfo.getKeyType())});
                                String.valueOf(imsiEncryptionInfo.getKeyType()),
                                String.valueOf(imsiEncryptionInfo.getCarrierId())});
                if (nRows == 0) {
                    Log.d(LOG_TAG, "Error updating values:" + imsiEncryptionInfo);
                    downloadSuccessfull = false;
@@ -260,7 +293,10 @@ public class CarrierInfoManager {
            Log.e(LOG_TAG, "Could not reset carrier keys, subscription for mPhoneId=" + mPhoneId);
            return;
        }
        deleteCarrierInfoForImsiEncryption(context, subIds[0]);
        final TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class)
                .createForSubscriptionId(subIds[0]);
        int carrierId = telephonyManager.getSimCarrierId();
        deleteCarrierInfoForImsiEncryption(context, subIds[0], carrierId);
        Intent resetIntent = new Intent(TelephonyIntents.ACTION_CARRIER_CERTIFICATE_DOWNLOAD);
        SubscriptionManager.putPhoneIdAndSubIdExtra(resetIntent, mPhoneId);
        context.sendBroadcastAsUser(resetIntent, UserHandle.ALL);
@@ -269,11 +305,16 @@ public class CarrierInfoManager {
    /**
     * Deletes all the keys for a given Carrier from the device keystore.
     * @param context Context
     * @param subId
     * @param carrierId delete the key which matches the carrierId
     *
     */
    public static void deleteCarrierInfoForImsiEncryption(Context context, int subId) {
    public static void deleteCarrierInfoForImsiEncryption(Context context, int subId,
            int carrierId) {
        Log.i(LOG_TAG, "deleting carrier key from db for subId=" + subId);
        String mcc = "";
        String mnc = "";

        final TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class)
                .createForSubscriptionId(subId);
        String simOperator = telephonyManager.getSimOperator();
@@ -284,11 +325,15 @@ public class CarrierInfoManager {
            Log.e(LOG_TAG, "Invalid networkOperator: " + simOperator);
            return;
        }
        String carriedIdStr = String.valueOf(carrierId);
        ContentResolver mContentResolver = context.getContentResolver();
        try {
            String whereClause = "mcc=? and mnc=?";
            String[] whereArgs = new String[] { mcc, mnc };
            mContentResolver.delete(Telephony.CarrierColumns.CONTENT_URI, whereClause, whereArgs);
            String whereClause = "mcc=? and mnc=? and carrier_id=?";
            String[] whereArgs = new String[] { mcc, mnc, carriedIdStr };
            int count = mContentResolver.delete(Telephony.CarrierColumns.CONTENT_URI, whereClause,
                    whereArgs);
            Log.i(LOG_TAG, "Deleting the number of entries = " + count + "   for carrierId = "
                    + carriedIdStr);
        } catch (Exception e) {
            Log.e(LOG_TAG, "Delete failed" + e);
        }
+54 −26
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.provider.Telephony;
import android.telephony.CarrierConfigManager;
import android.telephony.ImsiEncryptionInfo;
import android.telephony.SubscriptionManager;
@@ -108,9 +109,12 @@ public class CarrierKeyDownloadManager extends Handler {
    public final DownloadManager mDownloadManager;
    private String mURL;
    private boolean mAllowedOverMeteredNetwork = false;
    private boolean mDeleteOldKeyAfterDownload = false;
    private TelephonyManager mTelephonyManager;

    @VisibleForTesting
    public String mMccMncForDownload;
    public int mCarrierId;
    @VisibleForTesting
    public long mDownloadId;

@@ -123,6 +127,8 @@ public class CarrierKeyDownloadManager extends Handler {
        filter.addAction(TelephonyIntents.ACTION_CARRIER_CERTIFICATE_DOWNLOAD);
        mContext.registerReceiver(mBroadcastReceiver, filter, null, phone);
        mDownloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
        mTelephonyManager = mContext.getSystemService(TelephonyManager.class)
                .createForSubscriptionId(mPhone.getSubId());
    }

    private final BroadcastReceiver mDownloadReceiver = new BroadcastReceiver() {
@@ -174,8 +180,9 @@ public class CarrierKeyDownloadManager extends Handler {
            case EVENT_DOWNLOAD_COMPLETE:
                long carrierKeyDownloadIdentifier = (long) msg.obj;
                String currentMccMnc = getSimOperator();
                if (isValidDownload(currentMccMnc, carrierKeyDownloadIdentifier)) {
                    onDownloadComplete(carrierKeyDownloadIdentifier, currentMccMnc);
                int carrierId = getSimCarrierId();
                if (isValidDownload(currentMccMnc, carrierKeyDownloadIdentifier, carrierId)) {
                    onDownloadComplete(carrierKeyDownloadIdentifier, currentMccMnc, carrierId);
                    onPostDownloadProcessing(carrierKeyDownloadIdentifier);
                }
                break;
@@ -206,7 +213,7 @@ public class CarrierKeyDownloadManager extends Handler {
        } else {
            // delete any existing alarms.
            cleanupRenewalAlarms();
            mPhone.deleteCarrierInfoForImsiEncryption();
            mPhone.deleteCarrierInfoForImsiEncryption(getSimCarrierId());
        }
    }

@@ -214,7 +221,7 @@ public class CarrierKeyDownloadManager extends Handler {
        Log.d(LOG_TAG, "Cleaning up download info");
        mDownloadId = -1;
        mMccMncForDownload = null;

        mCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
    }

    private void cleanupRenewalAlarms() {
@@ -295,9 +302,15 @@ public class CarrierKeyDownloadManager extends Handler {
     **/
    @VisibleForTesting
    public String getSimOperator() {
        final TelephonyManager telephonyManager =
                (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager.getSimOperator(mPhone.getSubId());
        return mTelephonyManager.getSimOperator(mPhone.getSubId());
    }

    /**
     * Returns the sim operator.
     **/
    @VisibleForTesting
    public int getSimCarrierId() {
        return mTelephonyManager.getSimCarrierId();
    }

    /**
@@ -306,7 +319,7 @@ public class CarrierKeyDownloadManager extends Handler {
     *  instance of the phone.
     **/
    @VisibleForTesting
    public boolean isValidDownload(String currentMccMnc, long currentDownloadId) {
    public boolean isValidDownload(String currentMccMnc, long currentDownloadId, int carrierId) {
        if (currentDownloadId != mDownloadId) {
            Log.e(LOG_TAG, "download ID=" + currentDownloadId
                    + " for completed download does not match stored id=" + mDownloadId);
@@ -314,19 +327,23 @@ public class CarrierKeyDownloadManager extends Handler {
        }

        if (TextUtils.isEmpty(currentMccMnc) || TextUtils.isEmpty(mMccMncForDownload)
                || !TextUtils.equals(currentMccMnc, mMccMncForDownload)) {
            Log.e(LOG_TAG, "currentMccMnc=" + currentMccMnc + " stored=" + mMccMncForDownload);
                || !TextUtils.equals(currentMccMnc, mMccMncForDownload)
                || mCarrierId != carrierId) {
            Log.e(LOG_TAG, "currentMccMnc=" + currentMccMnc + " storedMccMnc =" + mMccMncForDownload
                    + "currentCarrierId = " + carrierId + "  storedCarrierId = " + mCarrierId);
            return false;
        }

        Log.d(LOG_TAG, "Matched MccMnc, downloadId: " + currentMccMnc + "," + currentDownloadId);
        Log.d(LOG_TAG, "Matched MccMnc =  " + currentMccMnc + ", carrierId = " + carrierId
                + ", downloadId: " + currentDownloadId);
        return true;
    }

    /**
     * This method will try to parse the downloaded information, and persist it in the database.
     **/
    private void onDownloadComplete(long carrierKeyDownloadIdentifier, String mccMnc) {
    private void onDownloadComplete(long carrierKeyDownloadIdentifier, String mccMnc,
            int carrierId) {
        Log.d(LOG_TAG, "onDownloadComplete: " + carrierKeyDownloadIdentifier);
        String jsonStr;
        DownloadManager.Query query = new DownloadManager.Query();
@@ -346,7 +363,7 @@ public class CarrierKeyDownloadManager extends Handler {
                        jsonStr = convertToStringNoGZip(mDownloadManager,
                                carrierKeyDownloadIdentifier);
                    }
                    parseJsonAndPersistKey(jsonStr, mccMnc);
                    parseJsonAndPersistKey(jsonStr, mccMnc, carrierId);
                } catch (Exception e) {
                    Log.e(LOG_TAG, "Error in download:" + carrierKeyDownloadIdentifier
                            + ". " + e);
@@ -448,9 +465,10 @@ public class CarrierKeyDownloadManager extends Handler {
     * @param mccMnc contains the mcc, mnc.
     */
    @VisibleForTesting
    public void parseJsonAndPersistKey(String jsonStr, String mccMnc) {
        if (TextUtils.isEmpty(jsonStr) || TextUtils.isEmpty(mccMnc)) {
            Log.e(LOG_TAG, "jsonStr or mcc, mnc: is empty");
    public void parseJsonAndPersistKey(String jsonStr, String mccMnc, int carrierId) {
        if (TextUtils.isEmpty(jsonStr) || TextUtils.isEmpty(mccMnc)
                || carrierId == TelephonyManager.UNKNOWN_CARRIER_ID) {
            Log.e(LOG_TAG, "jsonStr or mcc, mnc: is empty or carrierId is UNKNOWN_CARRIER_ID");
            return;
        }
        try {
@@ -481,7 +499,11 @@ public class CarrierKeyDownloadManager extends Handler {
                String identifier = key.getString(JSON_IDENTIFIER);
                Pair<PublicKey, Long> keyInfo =
                        getKeyInformation(cleanCertString(cert).getBytes());
                savePublicKey(keyInfo.first, type, identifier, keyInfo.second, mcc, mnc);
                if (mDeleteOldKeyAfterDownload) {
                    mPhone.deleteCarrierInfoForImsiEncryption(TelephonyManager.UNKNOWN_CARRIER_ID);
                    mDeleteOldKeyAfterDownload = false;
                }
                savePublicKey(keyInfo.first, type, identifier, keyInfo.second, mcc, mnc, carrierId);
            }
        } catch (final JSONException e) {
            Log.e(LOG_TAG, "Json parsing error: " + e.getMessage());
@@ -528,6 +550,10 @@ public class CarrierKeyDownloadManager extends Handler {
            if (imsiEncryptionInfo == null) {
                Log.d(LOG_TAG, "Key not found for: " + key_type);
                return true;
            } else if (imsiEncryptionInfo.getCarrierId() == TelephonyManager.UNKNOWN_CARRIER_ID) {
                Log.d(LOG_TAG, "carrier key is unknown carrier, so prefer to reDownload");
                mDeleteOldKeyAfterDownload = true;
                return true;
            }
            Date imsiDate = imsiEncryptionInfo.getExpirationTime();
            long timeToExpire = imsiDate.getTime() - System.currentTimeMillis();
@@ -539,11 +565,12 @@ public class CarrierKeyDownloadManager extends Handler {
    private boolean downloadKey() {
        Log.d(LOG_TAG, "starting download from: " + mURL);
        String mccMnc = getSimOperator();

        if (!TextUtils.isEmpty(mccMnc)) {
            Log.d(LOG_TAG, "downloading key for mccmnc: " + mccMnc);
        int carrierId = getSimCarrierId();
        if (!TextUtils.isEmpty(mccMnc) || carrierId != TelephonyManager.UNKNOWN_CARRIER_ID) {
            Log.d(LOG_TAG, "downloading key for mccmnc : " + mccMnc + ", carrierId : "
                    + carrierId);
        } else {
            Log.e(LOG_TAG, "mccmnc: is empty");
            Log.e(LOG_TAG, "mccmnc or carrierId is UnKnown");
            return false;
        }
        try {
@@ -561,9 +588,10 @@ public class CarrierKeyDownloadManager extends Handler {
            request.addRequestHeader("Accept-Encoding", "gzip");
            Long carrierKeyDownloadRequestId = mDownloadManager.enqueue(request);

            Log.d(LOG_TAG, "saving values mccmnc, downloadId: " + mccMnc
                    + ", " + carrierKeyDownloadRequestId);
            Log.d(LOG_TAG, "saving values mccmnc: " + mccMnc + ", downloadId: "
                    + carrierKeyDownloadRequestId + ", carrierId: " + carrierId);
            mMccMncForDownload = mccMnc;
            mCarrierId = carrierId;
            mDownloadId = carrierKeyDownloadRequestId;
        } catch (Exception e) {
            Log.e(LOG_TAG, "exception trying to download key from url: " + mURL);
@@ -598,9 +626,9 @@ public class CarrierKeyDownloadManager extends Handler {
     **/
    @VisibleForTesting
    public void savePublicKey(PublicKey publicKey, int type, String identifier, long expirationDate,
                               String mcc, String mnc) {
        ImsiEncryptionInfo imsiEncryptionInfo = new ImsiEncryptionInfo(mcc, mnc, type, identifier,
                publicKey, new Date(expirationDate));
            String mcc, String mnc, int carrierId) {
        ImsiEncryptionInfo imsiEncryptionInfo = new ImsiEncryptionInfo(mcc, mnc,
                type, identifier, publicKey, new Date(expirationDate), carrierId);
        mPhone.setCarrierInfoForImsiEncryption(imsiEncryptionInfo);
    }

+8 −5
Original line number Diff line number Diff line
@@ -1951,10 +1951,12 @@ public class GsmCdmaPhone extends Phone {

    @Override
    public ImsiEncryptionInfo getCarrierInfoForImsiEncryption(int keyType, boolean fallback) {
        String operatorNumeric = TelephonyManager.from(mContext)
                .getSimOperatorNumericForPhone(mPhoneId);
        final TelephonyManager telephonyManager = mContext.getSystemService(TelephonyManager.class)
                .createForSubscriptionId(getSubId());
        String operatorNumeric = telephonyManager.getSimOperator();
        int carrierId = telephonyManager.getSimCarrierId();
        return CarrierInfoManager.getCarrierInfoForImsiEncryption(keyType,
                mContext, operatorNumeric, fallback, getSubId());
                mContext, operatorNumeric, carrierId, fallback, getSubId());
    }

    @Override
@@ -1964,8 +1966,9 @@ public class GsmCdmaPhone extends Phone {
    }

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

    @Override
+2 −1
Original line number Diff line number Diff line
@@ -4019,8 +4019,9 @@ public abstract class Phone extends Handler implements PhoneInternalInterface {

    /**
     * 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
     */
    public void deleteCarrierInfoForImsiEncryption() {
    public void deleteCarrierInfoForImsiEncryption(int carrierId) {
        return;
    }

+82 −31

File changed.

Preview size limit exceeded, changes collapsed.

Loading