Test app for IMSI privacy.

Create test app to download IMIS certificates, and parse the JSON.
The test app will also persist the certificates using TelephonyManager.

Bug: 35606429
Test: Manual
Change-Id: Icef53ffbd790f05c11a37c40d63f3252c8d7301c
diff --git a/testapps/AndroidManifest.xml b/testapps/AndroidManifest.xml
index 592145b..83f021f 100644
--- a/testapps/AndroidManifest.xml
+++ b/testapps/AndroidManifest.xml
@@ -25,6 +25,7 @@
     <uses-permission android:name="android.permission.CAMERA" />
     <uses-permission android:name="android.permission.CALL_PHONE" />
     <uses-permission android:name="android.permission.CONTROL_INCALL_EXPERIENCE" />
+    <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />
     <uses-permission android:name="android.permission.READ_CALL_LOG" />
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
@@ -178,6 +179,16 @@
             </intent-filter>
           </activity>
 
+        <activity android:name="com.android.server.telecom.testapps.TestCertActivity"
+                android:label="@string/KeyUiAppLabel"
+                android:launchMode="singleInstance">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+          </activity>
+
         <activity android:name="com.android.server.telecom.testapps.SelfManagedCallingActivity"
                   android:label="@string/selfManagedCallingActivityLabel"
                   android:process="com.android.server.telecom.testapps.SelfMangingCallingApp"
diff --git a/testapps/res/layout/key_list.xml b/testapps/res/layout/key_list.xml
new file mode 100644
index 0000000..f56836c
--- /dev/null
+++ b/testapps/res/layout/key_list.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2014 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+      android:layout_width="fill_parent"
+      android:layout_height="wrap_content"/>
diff --git a/testapps/res/layout/testcert_main.xml b/testapps/res/layout/testcert_main.xml
new file mode 100644
index 0000000..84ed1e8
--- /dev/null
+++ b/testapps/res/layout/testcert_main.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2014 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical" >
+    <EditText
+        android:id="@+id/text"
+        android:inputType="text"
+        android:layout_width="200dp"
+        android:layout_height="wrap_content" />
+    <Button
+        android:id="@+id/get_key_button"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="@string/getKeyButton" />
+     <ListView
+        android:id="@+id/keylist"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content" />
+</LinearLayout>
diff --git a/testapps/res/values/donottranslate_strings.xml b/testapps/res/values/donottranslate_strings.xml
index bfe7550..a0485d0 100644
--- a/testapps/res/values/donottranslate_strings.xml
+++ b/testapps/res/values/donottranslate_strings.xml
@@ -64,6 +64,10 @@
 
     <string name="placeUssdButton">Send USSD</string>
 
+    <string name="KeyUiAppLabel">Get Key UI</string>
+
+    <string name="getKeyButton">Get Key Json</string>
+
     <!-- String for button in SelfManagedCallingActivity. -->
     <string name="checkIfPermittedBeforeCallingButton">Check if calls permitted before calling</string>
 
diff --git a/testapps/src/com/android/server/telecom/testapps/TestCertActivity.java b/testapps/src/com/android/server/telecom/testapps/TestCertActivity.java
new file mode 100644
index 0000000..0df836c
--- /dev/null
+++ b/testapps/src/com/android/server/telecom/testapps/TestCertActivity.java
@@ -0,0 +1,240 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.telecom.testapps;
+
+import android.app.Activity;
+import android.content.Context;
+import android.os.Bundle;
+import android.telephony.ImsiEncryptionInfo;
+import android.text.TextUtils;
+import android.util.Log;
+import android.telephony.TelephonyManager;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.ArrayAdapter;
+import android.widget.EditText;
+import android.widget.Toast;
+
+import android.app.ProgressDialog;
+import android.os.AsyncTask;
+import android.util.Log;
+import android.widget.ListView;
+import android.widget.Toast;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.security.KeyFactory;
+import java.security.NoSuchAlgorithmException;
+import java.security.PublicKey;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.X509EncodedKeySpec;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.ProtocolException;
+import java.net.URL;
+import java.util.ArrayList;
+
+import android.util.Base64;
+
+public class TestCertActivity extends Activity {
+
+    private EditText mCertUrlView;
+    public static final String LOG_TAG = "TestCertActivity";
+
+    private ProgressDialog progressDialog;
+    private ArrayList<String> keyList = new ArrayList<String>();
+
+    // URL to get the json
+    private String mURL = "";
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.testcert_main);
+        findViewById(R.id.get_key_button).setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                new GetKeys().execute();
+            }
+        });
+
+        mCertUrlView = (EditText) findViewById(R.id.text);
+        mCertUrlView.setText(mURL);
+    }
+
+    public static PublicKey makeKeyObject(byte[] publicKeyBytes) {
+        try {
+            X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKeyBytes);
+            return KeyFactory.getInstance("RSA").generatePublic(pubKeySpec);
+        } catch (InvalidKeySpecException | NoSuchAlgorithmException ex) {
+            Log.e(LOG_TAG, "Error makeKeyObject: unable to convert into PublicKey", ex);
+        }
+        return null;
+    }
+
+    /**
+     * Class to get json by making HTTP call
+     */
+    private class GetKeys extends AsyncTask<Void, Void, Void> {
+
+        @Override
+        protected void onPreExecute() {
+            super.onPreExecute();
+            progressDialog = new ProgressDialog(TestCertActivity.this);
+            progressDialog.setMessage("Downloading...");
+            progressDialog.setCancelable(false);
+            progressDialog.show();
+        }
+
+        public String getCertificateList() {
+            String response = null;
+            String mURL = mCertUrlView.getText().toString();
+            try {
+                URL url = new URL(mURL);
+                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+                conn.setRequestMethod("GET");
+                // read the response
+                InputStream in = new BufferedInputStream(conn.getInputStream());
+                response = convertToString(in);
+            } catch (ProtocolException e) {
+                Log.e(LOG_TAG, "ProtocolException: " + e.getMessage());
+            } catch (MalformedURLException e) {
+                Log.e(LOG_TAG, "MalformedURLException: " + e.getMessage());
+            } catch (IOException e) {
+                Log.e(LOG_TAG, "IOException: " + e.getMessage());
+            } catch (Exception e) {
+                Log.e(LOG_TAG, "Exception: " + e.getMessage());
+            }
+            return response;
+        }
+
+        private String convertToString(InputStream is) {
+            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+            StringBuilder sb = new StringBuilder();
+
+            String line;
+            try {
+                while ((line = reader.readLine()) != null) {
+                    sb.append(line).append('\n');
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            } finally {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            return sb.toString();
+        }
+
+        private void savePublicKey(String key, int type, String identifier) {
+            byte[] keyBytes = Base64.decode(key.getBytes(), Base64.DEFAULT);
+            PublicKey publicKey = makeKeyObject(keyBytes);
+            Log.i(LOG_TAG, "generated public key: " + publicKey);
+            final TelephonyManager telephonyManager =
+                    (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
+
+            String mcc = "";
+            String mnc = "";
+            String networkOperator = telephonyManager.getNetworkOperator();
+
+            if (!TextUtils.isEmpty(networkOperator)) {
+                mcc = networkOperator.substring(0, 3);
+                mnc = networkOperator.substring(3);
+                Log.i(LOG_TAG, "using values for mnc, mcc: " + mnc + "," + mcc);
+            }
+
+            ImsiEncryptionInfo imsiEncryptionInfo = new ImsiEncryptionInfo(mcc,
+                    mnc, type, identifier, publicKey);
+            telephonyManager.setCarrierInfoForImsiEncryption(imsiEncryptionInfo);
+            keyList.add(imsiEncryptionInfo.getKeyType() + "," +
+                    imsiEncryptionInfo.getKeyIdentifier());
+            Log.i(LOG_TAG,"calling telephonymanager complete");
+        }
+
+        @Override
+        protected Void doInBackground(Void... arg0) {
+            // Making a request to url and getting response
+            String jsonStr = getCertificateList();
+            Log.d(LOG_TAG, "Response from url: " + jsonStr);
+
+            if (jsonStr != null) {
+                try {
+                    JSONObject jsonObj = new JSONObject(jsonStr);
+                    // Getting JSON Array node
+                    JSONArray certificates = jsonObj.getJSONArray("certificates");
+
+                    // looping through the certificates
+                    for (int i = 0; i < certificates.length(); i++) {
+                        JSONObject cert = certificates.getJSONObject(i);
+                        String key = cert.getString("key");
+                        int type = cert.getInt("type");
+                        String identifier = cert.getString("identifier");
+                        savePublicKey(key, type, identifier);
+                    }
+                } catch (final JSONException e) {
+                    Log.e(LOG_TAG, "Json parsing error: " + e.getMessage());
+                    runOnUiThread(new Runnable() {
+                        @Override
+                        public void run() {
+                            Toast.makeText(getApplicationContext(),
+                                    "Json parsing error: " + e.getMessage(),
+                                    Toast.LENGTH_LONG)
+                                    .show();
+                        }
+                    });
+                }
+            } else {
+                Log.e(LOG_TAG, "Unable to get JSON from server " + mURL);
+                runOnUiThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        Toast.makeText(getApplicationContext(),
+                                "Unable to get JSON from server!",
+                                Toast.LENGTH_LONG)
+                                .show();
+                    }
+                });
+            }
+            return null;
+        }
+
+        @Override
+        protected void onPostExecute(Void result) {
+
+            super.onPostExecute(result);
+            if (progressDialog.isShowing()) {
+                progressDialog.dismiss();
+            }
+            ListView listView = (ListView) findViewById(R.id.keylist);
+            ArrayAdapter arrayAdapter =
+                    new ArrayAdapter(TestCertActivity.this, R.layout.key_list, keyList);
+            listView.setAdapter(arrayAdapter);
+        }
+    }
+}
+
+