Loading src/java/com/android/internal/telephony/CarrierKeyDownloadManager.java +17 −16 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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 Loading Loading @@ -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 = ""; Loading Loading @@ -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); } } } Loading Loading @@ -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()); } } tests/telephonytests/src/com/android/internal/telephony/CarrierKeyDownloadMgrTest.java +13 −18 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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; Loading Loading @@ -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"); } Loading @@ -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"); } Loading Loading @@ -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"); } Loading @@ -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); } } Loading
src/java/com/android/internal/telephony/CarrierKeyDownloadManager.java +17 −16 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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 Loading Loading @@ -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 = ""; Loading Loading @@ -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); } } } Loading Loading @@ -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()); } }
tests/telephonytests/src/com/android/internal/telephony/CarrierKeyDownloadMgrTest.java +13 −18 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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; Loading Loading @@ -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"); } Loading @@ -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"); } Loading Loading @@ -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"); } Loading @@ -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); } }