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