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

Commit e8dbd005 authored by Peter Wang's avatar Peter Wang
Browse files

[Telephony Mainline] Removed usage of bouncycastle

Bug: 139169009
Test: Build and CarrierKeyDownloadMgrTest
Change-Id: I762e243050ca042da29a43aa97c8496e8d98205a
Merged-In: I762e243050ca042da29a43aa97c8496e8d98205a
parent 0f323f15
Loading
Loading
Loading
Loading
+17 −16
Original line number Diff line number Diff line
@@ -43,7 +43,6 @@ import android.util.Log;
import android.util.Pair;

import com.android.internal.annotations.VisibleForTesting;
import com.android.org.bouncycastle.util.io.pem.PemReader;

import org.json.JSONArray;
import org.json.JSONException;
@@ -55,7 +54,6 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.security.PublicKey;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
@@ -72,6 +70,10 @@ public class CarrierKeyDownloadManager extends Handler {

    private static final String MCC_MNC_PREF_TAG = "CARRIER_KEY_DM_MCC_MNC";

    private static final String CERT_BEGIN_STRING = "-----BEGIN CERTIFICATE-----";

    private static final String CERT_END_STRING = "-----END CERTIFICATE-----";

    private static final int DAY_IN_MILLIS = 24 * 3600 * 1000;

    // Create a window prior to the key expiration, during which the cert will be
@@ -436,7 +438,6 @@ public class CarrierKeyDownloadManager extends Handler {
            Log.e(LOG_TAG, "jsonStr or mcc, mnc: is empty");
            return;
        }
        PemReader reader = null;
        try {
            String mcc = "";
            String mnc = "";
@@ -470,26 +471,14 @@ public class CarrierKeyDownloadManager extends Handler {
                    }
                }
                String identifier = key.getString(JSON_IDENTIFIER);
                ByteArrayInputStream inStream = new ByteArrayInputStream(cert.getBytes());
                Reader fReader = new BufferedReader(new InputStreamReader(inStream));
                reader = new PemReader(fReader);
                Pair<PublicKey, Long> keyInfo =
                        getKeyInformation(reader.readPemObject().getContent());
                reader.close();
                        getKeyInformation(cleanCertString(cert).getBytes());
                savePublicKey(keyInfo.first, type, identifier, keyInfo.second, mcc, mnc);
            }
        } catch (final JSONException e) {
            Log.e(LOG_TAG, "Json parsing error: " + e.getMessage());
        } catch (final Exception e) {
            Log.e(LOG_TAG, "Exception getting certificate: " + e);
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (final Exception e) {
                Log.e(LOG_TAG, "Exception getting certificate: " + e);
            }
        }
    }

@@ -597,4 +586,16 @@ public class CarrierKeyDownloadManager extends Handler {
                publicKey, new Date(expirationDate));
        mPhone.setCarrierInfoForImsiEncryption(imsiEncryptionInfo);
    }

    /**
     * Remove potential extraneous text in a certificate string
     * @param cert certificate string
     * @return Cleaned up version of the certificate string
     */
    @VisibleForTesting
    public static String cleanCertString(String cert) {
        return cert.substring(
                cert.indexOf(CERT_BEGIN_STRING),
                cert.indexOf(CERT_END_STRING) + CERT_END_STRING.length());
    }
}
+13 −18
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ package com.android.internal.telephony;

import static android.preference.PreferenceManager.getDefaultSharedPreferences;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -39,8 +40,6 @@ import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.util.Pair;

import com.android.org.bouncycastle.util.io.pem.PemReader;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -48,10 +47,6 @@ import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.MockitoAnnotations;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.security.PublicKey;
import java.text.SimpleDateFormat;
import java.util.Calendar;
@@ -148,12 +143,9 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
    @Test
    @SmallTest
    public void testParseJson() {
        ByteArrayInputStream certBytes = new ByteArrayInputStream(CERT.getBytes());
        Reader fRd = new BufferedReader(new InputStreamReader(certBytes));
        PemReader reader = new PemReader(fRd);
        Pair<PublicKey, Long> keyInfo = null;
        try {
            keyInfo = mCarrierKeyDM.getKeyInformation(reader.readPemObject().getContent());
            keyInfo = mCarrierKeyDM.getKeyInformation(CERT.getBytes());
        } catch (Exception e) {
            fail(LOG_TAG + "exception creating public key");
        }
@@ -172,12 +164,9 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
    @Test
    @SmallTest
    public void testParseJsonPublicKey() {
        ByteArrayInputStream certBytes = new ByteArrayInputStream(CERT.getBytes());
        Reader fRd = new BufferedReader(new InputStreamReader(certBytes));
        PemReader reader = new PemReader(fRd);
        Pair<PublicKey, Long> keyInfo = null;
        try {
            keyInfo = mCarrierKeyDM.getKeyInformation(reader.readPemObject().getContent());
            keyInfo = mCarrierKeyDM.getKeyInformation(CERT.getBytes());
        } catch (Exception e) {
            fail(LOG_TAG + "exception creating public key");
        }
@@ -324,12 +313,9 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
    @Test
    @SmallTest
    public void testParseJson3GppFormat() {
        ByteArrayInputStream certBytes = new ByteArrayInputStream(CERT.getBytes());
        Reader fRd = new BufferedReader(new InputStreamReader(certBytes));
        PemReader reader = new PemReader(fRd);
        Pair<PublicKey, Long> keyInfo = null;
        try {
            keyInfo = mCarrierKeyDM.getKeyInformation(reader.readPemObject().getContent());
            keyInfo = mCarrierKeyDM.getKeyInformation(CERT.getBytes());
        } catch (Exception e) {
            fail(LOG_TAG + "exception creating public key");
        }
@@ -342,4 +328,13 @@ public class CarrierKeyDownloadMgrTest extends TelephonyTest {
                (Matchers.refEq(imsiEncryptionInfo)));
    }

    /**
     * Checks if certificate string cleaning is working correctly
     */
    @Test
    @SmallTest
    public void testCleanCertString() {
        assertEquals(CarrierKeyDownloadManager
                .cleanCertString("Comments before" + CERT + "Comments after"), CERT);
    }
}