Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.keychain; |
| 18 | |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Robin Lee | 8eb453b | 2015-02-17 14:13:36 +0000 | [diff] [blame^] | 20 | import android.app.ActivityManagerNative; |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 21 | import android.app.admin.IDevicePolicyManager; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 22 | import android.app.AlertDialog; |
| 23 | import android.app.Dialog; |
| 24 | import android.app.PendingIntent; |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 25 | import android.content.Context; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 26 | import android.content.DialogInterface; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 27 | import android.content.Intent; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.res.Resources; |
| 30 | import android.os.AsyncTask; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 31 | import android.os.Bundle; |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 32 | import android.os.IBinder; |
| 33 | import android.os.RemoteException; |
| 34 | import android.os.ServiceManager; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 35 | import android.security.Credentials; |
Brian Carlstrom | f5b50a4 | 2011-06-09 16:05:09 -0700 | [diff] [blame] | 36 | import android.security.IKeyChainAliasCallback; |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 37 | import android.security.KeyChain; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 38 | import android.security.KeyStore; |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 39 | import android.util.Log; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 40 | import android.view.LayoutInflater; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 41 | import android.view.View; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 42 | import android.view.ViewGroup; |
Selim Gurun | 9c2b71c | 2012-03-22 15:51:14 -0700 | [diff] [blame] | 43 | import android.widget.AdapterView; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 44 | import android.widget.BaseAdapter; |
| 45 | import android.widget.Button; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 46 | import android.widget.ListView; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 47 | import android.widget.RadioButton; |
| 48 | import android.widget.TextView; |
| 49 | import com.android.org.bouncycastle.asn1.x509.X509Name; |
| 50 | import java.io.ByteArrayInputStream; |
| 51 | import java.io.InputStream; |
| 52 | import java.security.cert.CertificateException; |
| 53 | import java.security.cert.CertificateFactory; |
| 54 | import java.security.cert.X509Certificate; |
| 55 | import java.util.ArrayList; |
| 56 | import java.util.Arrays; |
| 57 | import java.util.Collections; |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 58 | import java.util.concurrent.ExecutionException; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 59 | import java.util.List; |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 60 | |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 61 | import javax.security.auth.x500.X500Principal; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 62 | |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 63 | public class KeyChainActivity extends Activity { |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 64 | private static final String TAG = "KeyChain"; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 65 | |
| 66 | private static String KEY_STATE = "state"; |
| 67 | |
| 68 | private static final int REQUEST_UNLOCK = 1; |
| 69 | |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 70 | private int mSenderUid; |
| 71 | |
| 72 | private PendingIntent mSender; |
| 73 | |
Kenny Root | 6ba32f2 | 2014-07-31 14:03:16 -0700 | [diff] [blame] | 74 | private static enum State { INITIAL, UNLOCK_REQUESTED, UNLOCK_CANCELED }; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 75 | |
| 76 | private State mState; |
| 77 | |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 78 | // beware that some of these KeyStore operations such as saw and |
| 79 | // get do file I/O in the remote keystore process and while they |
| 80 | // do not cause StrictMode violations, they logically should not |
| 81 | // be done on the UI thread. |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 82 | private KeyStore mKeyStore = KeyStore.getInstance(); |
| 83 | |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 84 | @Override public void onCreate(Bundle savedState) { |
| 85 | super.onCreate(savedState); |
| 86 | if (savedState == null) { |
| 87 | mState = State.INITIAL; |
| 88 | } else { |
| 89 | mState = (State) savedState.getSerializable(KEY_STATE); |
| 90 | if (mState == null) { |
| 91 | mState = State.INITIAL; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | @Override public void onResume() { |
| 97 | super.onResume(); |
| 98 | |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 99 | mSender = getIntent().getParcelableExtra(KeyChain.EXTRA_SENDER); |
| 100 | if (mSender == null) { |
| 101 | // if no sender, bail, we need to identify the app to the user securely. |
| 102 | finish(null); |
| 103 | return; |
| 104 | } |
| 105 | try { |
| 106 | mSenderUid = getPackageManager().getPackageInfo( |
| 107 | mSender.getIntentSender().getTargetPackage(), 0).applicationInfo.uid; |
| 108 | } catch (PackageManager.NameNotFoundException e) { |
| 109 | // if unable to find the sender package info bail, |
| 110 | // we need to identify the app to the user securely. |
| 111 | finish(null); |
| 112 | return; |
| 113 | } |
| 114 | |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 115 | // see if KeyStore has been unlocked, if not start activity to do so |
| 116 | switch (mState) { |
| 117 | case INITIAL: |
Kenny Root | 4ff2296 | 2013-02-14 10:17:06 -0800 | [diff] [blame] | 118 | if (!mKeyStore.isUnlocked()) { |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 119 | mState = State.UNLOCK_REQUESTED; |
| 120 | this.startActivityForResult(new Intent(Credentials.UNLOCK_ACTION), |
| 121 | REQUEST_UNLOCK); |
| 122 | // Note that Credentials.unlock will start an |
| 123 | // Activity and we will be paused but then resumed |
| 124 | // when the unlock Activity completes and our |
| 125 | // onActivityResult is called with REQUEST_UNLOCK |
| 126 | return; |
| 127 | } |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 128 | chooseCertificate(); |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 129 | return; |
| 130 | case UNLOCK_REQUESTED: |
| 131 | // we've already asked, but have not heard back, probably just rotated. |
| 132 | // wait to hear back via onActivityResult |
| 133 | return; |
Kenny Root | 6ba32f2 | 2014-07-31 14:03:16 -0700 | [diff] [blame] | 134 | case UNLOCK_CANCELED: |
| 135 | // User wanted to cancel the request, so exit. |
| 136 | mState = State.INITIAL; |
Kenny Root | 13e867c | 2014-11-14 08:34:57 -0800 | [diff] [blame] | 137 | finish(null); |
Kenny Root | 6ba32f2 | 2014-07-31 14:03:16 -0700 | [diff] [blame] | 138 | return; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 139 | default: |
| 140 | throw new AssertionError(); |
| 141 | } |
| 142 | } |
| 143 | |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 144 | private void chooseCertificate() { |
| 145 | // Start loading the set of certs to choose from now- if device policy doesn't return an |
| 146 | // alias, having aliases loading already will save some time waiting for UI to start. |
| 147 | final AliasLoader loader = new AliasLoader(); |
| 148 | loader.execute(); |
| 149 | |
| 150 | final IKeyChainAliasCallback.Stub callback = new IKeyChainAliasCallback.Stub() { |
| 151 | @Override public void alias(String alias) { |
| 152 | // Use policy-suggested alias if provided |
| 153 | if (alias != null) { |
| 154 | finish(alias); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | // No suggested alias - instead finish loading and show UI to pick one |
| 159 | final CertificateAdapter certAdapter; |
| 160 | try { |
| 161 | certAdapter = loader.get(); |
| 162 | } catch (InterruptedException | ExecutionException e) { |
| 163 | Log.e(TAG, "Loading certificate aliases interrupted", e); |
| 164 | finish(null); |
| 165 | return; |
| 166 | } |
| 167 | runOnUiThread(new Runnable() { |
| 168 | @Override public void run() { |
| 169 | displayCertChooserDialog(certAdapter); |
| 170 | } |
| 171 | }); |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | // Give a profile or device owner the chance to intercept the request, if a private key |
| 176 | // access listener is registered with the DevicePolicyManagerService. |
| 177 | IDevicePolicyManager devicePolicyManager = IDevicePolicyManager.Stub.asInterface( |
| 178 | ServiceManager.getService(Context.DEVICE_POLICY_SERVICE)); |
| 179 | |
| 180 | String host = getIntent().getStringExtra(KeyChain.EXTRA_HOST); |
| 181 | int port = getIntent().getIntExtra(KeyChain.EXTRA_PORT, -1); |
| 182 | String url = getIntent().getStringExtra(KeyChain.EXTRA_URL); |
| 183 | String alias = getIntent().getStringExtra(KeyChain.EXTRA_ALIAS); |
| 184 | |
| 185 | try { |
Robin Lee | 8eb453b | 2015-02-17 14:13:36 +0000 | [diff] [blame^] | 186 | int uid = ActivityManagerNative.getDefault().getLaunchedFromUid(getActivityToken()); |
| 187 | devicePolicyManager.choosePrivateKeyAlias(uid, host, port, url, alias, callback); |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 188 | } catch (RemoteException e) { |
Robin Lee | 8eb453b | 2015-02-17 14:13:36 +0000 | [diff] [blame^] | 189 | Log.e(TAG, "Unable to request alias from DevicePolicyManager", e); |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 190 | // Proceed without a suggested alias. |
| 191 | try { |
| 192 | callback.alias(null); |
| 193 | } catch (RemoteException shouldNeverHappen) { |
| 194 | finish(null); |
| 195 | } |
| 196 | } |
Brian Carlstrom | 91940e7 | 2011-06-28 20:37:31 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 199 | private class AliasLoader extends AsyncTask<Void, Void, CertificateAdapter> { |
| 200 | @Override protected CertificateAdapter doInBackground(Void... params) { |
| 201 | String[] aliasArray = mKeyStore.saw(Credentials.USER_PRIVATE_KEY); |
| 202 | List<String> aliasList = ((aliasArray == null) |
| 203 | ? Collections.<String>emptyList() |
| 204 | : Arrays.asList(aliasArray)); |
| 205 | Collections.sort(aliasList); |
| 206 | return new CertificateAdapter(aliasList); |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 207 | } |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 208 | } |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 209 | |
Brian Carlstrom | 91940e7 | 2011-06-28 20:37:31 -0700 | [diff] [blame] | 210 | private void displayCertChooserDialog(final CertificateAdapter adapter) { |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 211 | AlertDialog.Builder builder = new AlertDialog.Builder(this); |
Brian Carlstrom | df17230 | 2011-06-26 17:13:54 -0700 | [diff] [blame] | 212 | |
Brian Carlstrom | 4bf9e1a | 2011-06-28 21:45:31 -0700 | [diff] [blame] | 213 | TextView contextView = (TextView) View.inflate(this, R.layout.cert_chooser_header, null); |
| 214 | View footer = View.inflate(this, R.layout.cert_chooser_footer, null); |
| 215 | |
| 216 | final ListView lv = (ListView) View.inflate(this, R.layout.cert_chooser, null); |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 217 | lv.addHeaderView(contextView, null, false); |
| 218 | lv.addFooterView(footer, null, false); |
Brian Carlstrom | 4bf9e1a | 2011-06-28 21:45:31 -0700 | [diff] [blame] | 219 | lv.setAdapter(adapter); |
| 220 | builder.setView(lv); |
Brian Carlstrom | df17230 | 2011-06-26 17:13:54 -0700 | [diff] [blame] | 221 | |
Selim Gurun | 9c2b71c | 2012-03-22 15:51:14 -0700 | [diff] [blame] | 222 | lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 223 | |
| 224 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 225 | lv.setItemChecked(position, true); |
| 226 | } |
| 227 | }); |
| 228 | |
Brian Carlstrom | 91940e7 | 2011-06-28 20:37:31 -0700 | [diff] [blame] | 229 | boolean empty = adapter.mAliases.isEmpty(); |
Brian Carlstrom | df17230 | 2011-06-26 17:13:54 -0700 | [diff] [blame] | 230 | int negativeLabel = empty ? android.R.string.cancel : R.string.deny_button; |
| 231 | builder.setNegativeButton(negativeLabel, new DialogInterface.OnClickListener() { |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 232 | @Override public void onClick(DialogInterface dialog, int id) { |
| 233 | dialog.cancel(); // will cause OnDismissListener to be called |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 234 | } |
| 235 | }); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 236 | |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 237 | String title; |
Brian Carlstrom | df17230 | 2011-06-26 17:13:54 -0700 | [diff] [blame] | 238 | Resources res = getResources(); |
| 239 | if (empty) { |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 240 | title = res.getString(R.string.title_no_certs); |
| 241 | } else { |
| 242 | title = res.getString(R.string.title_select_cert); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 243 | String alias = getIntent().getStringExtra(KeyChain.EXTRA_ALIAS); |
| 244 | if (alias != null) { |
Brian Carlstrom | 6231655 | 2011-07-10 12:26:12 -0700 | [diff] [blame] | 245 | // if alias was requested, set it if found |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 246 | int adapterPosition = adapter.mAliases.indexOf(alias); |
| 247 | if (adapterPosition != -1) { |
| 248 | int listViewPosition = adapterPosition+1; |
| 249 | lv.setItemChecked(listViewPosition, true); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 250 | } |
Brian Carlstrom | 6231655 | 2011-07-10 12:26:12 -0700 | [diff] [blame] | 251 | } else if (adapter.mAliases.size() == 1) { |
| 252 | // if only one choice, preselect it |
| 253 | int adapterPosition = 0; |
| 254 | int listViewPosition = adapterPosition+1; |
| 255 | lv.setItemChecked(listViewPosition, true); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | builder.setPositiveButton(R.string.allow_button, new DialogInterface.OnClickListener() { |
| 259 | @Override public void onClick(DialogInterface dialog, int id) { |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 260 | int listViewPosition = lv.getCheckedItemPosition(); |
| 261 | int adapterPosition = listViewPosition-1; |
| 262 | String alias = ((adapterPosition >= 0) |
| 263 | ? adapter.getItem(adapterPosition) |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 264 | : null); |
| 265 | finish(alias); |
| 266 | } |
| 267 | }); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 268 | } |
| 269 | builder.setTitle(title); |
Brian Carlstrom | 91940e7 | 2011-06-28 20:37:31 -0700 | [diff] [blame] | 270 | final Dialog dialog = builder.create(); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 271 | |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 272 | |
| 273 | // getTargetPackage guarantees that the returned string is |
| 274 | // supplied by the system, so that an application can not |
| 275 | // spoof its package. |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 276 | String pkg = mSender.getIntentSender().getTargetPackage(); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 277 | PackageManager pm = getPackageManager(); |
| 278 | CharSequence applicationLabel; |
| 279 | try { |
| 280 | applicationLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString(); |
| 281 | } catch (PackageManager.NameNotFoundException e) { |
| 282 | applicationLabel = pkg; |
| 283 | } |
| 284 | String appMessage = String.format(res.getString(R.string.requesting_application), |
| 285 | applicationLabel); |
| 286 | |
| 287 | String contextMessage = appMessage; |
| 288 | String host = getIntent().getStringExtra(KeyChain.EXTRA_HOST); |
| 289 | if (host != null) { |
| 290 | String hostString = host; |
| 291 | int port = getIntent().getIntExtra(KeyChain.EXTRA_PORT, -1); |
| 292 | if (port != -1) { |
| 293 | hostString += ":" + port; |
| 294 | } |
| 295 | String hostMessage = String.format(res.getString(R.string.requesting_server), |
| 296 | hostString); |
| 297 | if (contextMessage == null) { |
| 298 | contextMessage = hostMessage; |
| 299 | } else { |
| 300 | contextMessage += " " + hostMessage; |
| 301 | } |
| 302 | } |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 303 | contextView.setText(contextMessage); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 304 | |
| 305 | String installMessage = String.format(res.getString(R.string.install_new_cert_message), |
| 306 | Credentials.EXTENSION_PFX, Credentials.EXTENSION_P12); |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 307 | TextView installText = (TextView) footer.findViewById(R.id.cert_chooser_install_message); |
| 308 | installText.setText(installMessage); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 309 | |
Brian Carlstrom | 4bf9e1a | 2011-06-28 21:45:31 -0700 | [diff] [blame] | 310 | Button installButton = (Button) footer.findViewById(R.id.cert_chooser_install_button); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 311 | installButton.setOnClickListener(new View.OnClickListener() { |
| 312 | @Override public void onClick(View v) { |
| 313 | // remove dialog so that we will recreate with |
| 314 | // possibly new content after install returns |
Brian Carlstrom | 91940e7 | 2011-06-28 20:37:31 -0700 | [diff] [blame] | 315 | dialog.dismiss(); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 316 | Credentials.getInstance().install(KeyChainActivity.this); |
| 317 | } |
| 318 | }); |
| 319 | |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 320 | dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { |
| 321 | @Override public void onCancel(DialogInterface dialog) { |
| 322 | finish(null); |
| 323 | } |
| 324 | }); |
Brian Carlstrom | 91940e7 | 2011-06-28 20:37:31 -0700 | [diff] [blame] | 325 | dialog.show(); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | private class CertificateAdapter extends BaseAdapter { |
| 329 | private final List<String> mAliases; |
| 330 | private final List<String> mSubjects = new ArrayList<String>(); |
| 331 | private CertificateAdapter(List<String> aliases) { |
| 332 | mAliases = aliases; |
| 333 | mSubjects.addAll(Collections.nCopies(aliases.size(), (String) null)); |
| 334 | } |
| 335 | @Override public int getCount() { |
| 336 | return mAliases.size(); |
| 337 | } |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 338 | @Override public String getItem(int adapterPosition) { |
| 339 | return mAliases.get(adapterPosition); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 340 | } |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 341 | @Override public long getItemId(int adapterPosition) { |
| 342 | return adapterPosition; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 343 | } |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 344 | @Override public View getView(final int adapterPosition, View view, ViewGroup parent) { |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 345 | ViewHolder holder; |
| 346 | if (view == null) { |
| 347 | LayoutInflater inflater = LayoutInflater.from(KeyChainActivity.this); |
| 348 | view = inflater.inflate(R.layout.cert_item, parent, false); |
| 349 | holder = new ViewHolder(); |
| 350 | holder.mAliasTextView = (TextView) view.findViewById(R.id.cert_item_alias); |
| 351 | holder.mSubjectTextView = (TextView) view.findViewById(R.id.cert_item_subject); |
| 352 | holder.mRadioButton = (RadioButton) view.findViewById(R.id.cert_item_selected); |
| 353 | view.setTag(holder); |
| 354 | } else { |
| 355 | holder = (ViewHolder) view.getTag(); |
| 356 | } |
| 357 | |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 358 | String alias = mAliases.get(adapterPosition); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 359 | |
| 360 | holder.mAliasTextView.setText(alias); |
| 361 | |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 362 | String subject = mSubjects.get(adapterPosition); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 363 | if (subject == null) { |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 364 | new CertLoader(adapterPosition, holder.mSubjectTextView).execute(); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 365 | } else { |
| 366 | holder.mSubjectTextView.setText(subject); |
| 367 | } |
| 368 | |
| 369 | ListView lv = (ListView)parent; |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 370 | int listViewCheckedItemPosition = lv.getCheckedItemPosition(); |
| 371 | int adapterCheckedItemPosition = listViewCheckedItemPosition-1; |
| 372 | holder.mRadioButton.setChecked(adapterPosition == adapterCheckedItemPosition); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 373 | return view; |
| 374 | } |
| 375 | |
| 376 | private class CertLoader extends AsyncTask<Void, Void, String> { |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 377 | private final int mAdapterPosition; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 378 | private final TextView mSubjectView; |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 379 | private CertLoader(int adapterPosition, TextView subjectView) { |
| 380 | mAdapterPosition = adapterPosition; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 381 | mSubjectView = subjectView; |
| 382 | } |
| 383 | @Override protected String doInBackground(Void... params) { |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 384 | String alias = mAliases.get(mAdapterPosition); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 385 | byte[] bytes = mKeyStore.get(Credentials.USER_CERTIFICATE + alias); |
| 386 | if (bytes == null) { |
| 387 | return null; |
| 388 | } |
| 389 | InputStream in = new ByteArrayInputStream(bytes); |
| 390 | X509Certificate cert; |
| 391 | try { |
| 392 | CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 393 | cert = (X509Certificate)cf.generateCertificate(in); |
| 394 | } catch (CertificateException ignored) { |
| 395 | return null; |
| 396 | } |
| 397 | // bouncycastle can handle the emailAddress OID of 1.2.840.113549.1.9.1 |
| 398 | X500Principal subjectPrincipal = cert.getSubjectX500Principal(); |
| 399 | X509Name subjectName = X509Name.getInstance(subjectPrincipal.getEncoded()); |
| 400 | String subjectString = subjectName.toString(true, X509Name.DefaultSymbols); |
| 401 | return subjectString; |
| 402 | } |
| 403 | @Override protected void onPostExecute(String subjectString) { |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 404 | mSubjects.set(mAdapterPosition, subjectString); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 405 | mSubjectView.setText(subjectString); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | private static class ViewHolder { |
| 411 | TextView mAliasTextView; |
| 412 | TextView mSubjectTextView; |
| 413 | RadioButton mRadioButton; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 417 | switch (requestCode) { |
| 418 | case REQUEST_UNLOCK: |
Kenny Root | 4ff2296 | 2013-02-14 10:17:06 -0800 | [diff] [blame] | 419 | if (mKeyStore.isUnlocked()) { |
Kenny Root | 6ba32f2 | 2014-07-31 14:03:16 -0700 | [diff] [blame] | 420 | mState = State.INITIAL; |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 421 | chooseCertificate(); |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 422 | } else { |
| 423 | // user must have canceled unlock, give up |
Kenny Root | 6ba32f2 | 2014-07-31 14:03:16 -0700 | [diff] [blame] | 424 | mState = State.UNLOCK_CANCELED; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 425 | } |
| 426 | return; |
| 427 | default: |
| 428 | throw new AssertionError(); |
| 429 | } |
| 430 | } |
| 431 | |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 432 | private void finish(String alias) { |
| 433 | if (alias == null) { |
| 434 | setResult(RESULT_CANCELED); |
| 435 | } else { |
| 436 | Intent result = new Intent(); |
| 437 | result.putExtra(Intent.EXTRA_TEXT, alias); |
| 438 | setResult(RESULT_OK, result); |
| 439 | } |
Brian Carlstrom | f5b50a4 | 2011-06-09 16:05:09 -0700 | [diff] [blame] | 440 | IKeyChainAliasCallback keyChainAliasResponse |
| 441 | = IKeyChainAliasCallback.Stub.asInterface( |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 442 | getIntent().getIBinderExtra(KeyChain.EXTRA_RESPONSE)); |
| 443 | if (keyChainAliasResponse != null) { |
Brian Carlstrom | 7d9aa75 | 2011-07-07 11:52:27 -0700 | [diff] [blame] | 444 | new ResponseSender(keyChainAliasResponse, alias).execute(); |
| 445 | return; |
| 446 | } |
| 447 | finish(); |
| 448 | } |
| 449 | |
| 450 | private class ResponseSender extends AsyncTask<Void, Void, Void> { |
| 451 | private IKeyChainAliasCallback mKeyChainAliasResponse; |
| 452 | private String mAlias; |
| 453 | private ResponseSender(IKeyChainAliasCallback keyChainAliasResponse, String alias) { |
| 454 | mKeyChainAliasResponse = keyChainAliasResponse; |
| 455 | mAlias = alias; |
| 456 | } |
| 457 | @Override protected Void doInBackground(Void... unused) { |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 458 | try { |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 459 | if (mAlias != null) { |
| 460 | KeyChain.KeyChainConnection connection = KeyChain.bind(KeyChainActivity.this); |
| 461 | try { |
| 462 | connection.getService().setGrant(mSenderUid, mAlias, true); |
| 463 | } finally { |
| 464 | connection.close(); |
| 465 | } |
| 466 | } |
Brian Carlstrom | 7d9aa75 | 2011-07-07 11:52:27 -0700 | [diff] [blame] | 467 | mKeyChainAliasResponse.alias(mAlias); |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 468 | } catch (InterruptedException ignored) { |
| 469 | Thread.currentThread().interrupt(); |
| 470 | Log.d(TAG, "interrupted while granting access", ignored); |
Brian Carlstrom | 2a85883 | 2011-05-26 09:30:26 -0700 | [diff] [blame] | 471 | } catch (Exception ignored) { |
| 472 | // don't just catch RemoteException, caller could |
| 473 | // throw back a RuntimeException across processes |
| 474 | // which we should protect against. |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 475 | Log.e(TAG, "error while granting access", ignored); |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 476 | } |
Brian Carlstrom | 7d9aa75 | 2011-07-07 11:52:27 -0700 | [diff] [blame] | 477 | return null; |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 478 | } |
Brian Carlstrom | 7d9aa75 | 2011-07-07 11:52:27 -0700 | [diff] [blame] | 479 | @Override protected void onPostExecute(Void unused) { |
| 480 | finish(); |
| 481 | } |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Brian Carlstrom | 9e606df | 2011-06-07 12:03:08 -0700 | [diff] [blame] | 484 | @Override public void onBackPressed() { |
| 485 | finish(null); |
| 486 | } |
| 487 | |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 488 | @Override protected void onSaveInstanceState(Bundle savedState) { |
| 489 | super.onSaveInstanceState(savedState); |
| 490 | if (mState != State.INITIAL) { |
| 491 | savedState.putSerializable(KEY_STATE, mState); |
| 492 | } |
| 493 | } |
| 494 | } |