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