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; |
| 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. |
Eran Messeri | 96235f6 | 2017-10-20 10:27:28 +0100 | [diff] [blame^] | 147 | final AliasLoader loader = new AliasLoader(mKeyStore, this); |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 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 | |
Robin Lee | 606d9d0 | 2015-05-05 15:57:13 +0100 | [diff] [blame] | 180 | Uri uri = getIntent().getParcelableExtra(KeyChain.EXTRA_URI); |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 181 | String alias = getIntent().getStringExtra(KeyChain.EXTRA_ALIAS); |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 182 | try { |
Robin Lee | 9bfaabb | 2015-07-31 14:52:54 +0100 | [diff] [blame] | 183 | devicePolicyManager.choosePrivateKeyAlias(mSenderUid, uri, alias, callback); |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 184 | } catch (RemoteException e) { |
Robin Lee | 8eb453b | 2015-02-17 14:13:36 +0000 | [diff] [blame] | 185 | Log.e(TAG, "Unable to request alias from DevicePolicyManager", e); |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 186 | // Proceed without a suggested alias. |
| 187 | try { |
| 188 | callback.alias(null); |
| 189 | } catch (RemoteException shouldNeverHappen) { |
| 190 | finish(null); |
| 191 | } |
| 192 | } |
Brian Carlstrom | 91940e7 | 2011-06-28 20:37:31 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Eran Messeri | 96235f6 | 2017-10-20 10:27:28 +0100 | [diff] [blame^] | 195 | private static class AliasLoader extends AsyncTask<Void, Void, CertificateAdapter> { |
| 196 | private final KeyStore mKeyStore; |
| 197 | private final Context mContext; |
| 198 | public AliasLoader(KeyStore keyStore, Context context) { |
| 199 | mKeyStore = keyStore; |
| 200 | mContext = context; |
| 201 | } |
| 202 | |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 203 | @Override protected CertificateAdapter doInBackground(Void... params) { |
Alex Klyubin | 44c777b | 2015-06-08 09:46:15 -0700 | [diff] [blame] | 204 | String[] aliasArray = mKeyStore.list(Credentials.USER_PRIVATE_KEY); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 205 | List<String> aliasList = ((aliasArray == null) |
| 206 | ? Collections.<String>emptyList() |
| 207 | : Arrays.asList(aliasArray)); |
| 208 | Collections.sort(aliasList); |
Eran Messeri | 96235f6 | 2017-10-20 10:27:28 +0100 | [diff] [blame^] | 209 | return new CertificateAdapter(mKeyStore, mContext, aliasList); |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 210 | } |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 211 | } |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 212 | |
Brian Carlstrom | 91940e7 | 2011-06-28 20:37:31 -0700 | [diff] [blame] | 213 | private void displayCertChooserDialog(final CertificateAdapter adapter) { |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 214 | AlertDialog.Builder builder = new AlertDialog.Builder(this); |
Brian Carlstrom | df17230 | 2011-06-26 17:13:54 -0700 | [diff] [blame] | 215 | |
Brian Carlstrom | 91940e7 | 2011-06-28 20:37:31 -0700 | [diff] [blame] | 216 | boolean empty = adapter.mAliases.isEmpty(); |
Brian Carlstrom | df17230 | 2011-06-26 17:13:54 -0700 | [diff] [blame] | 217 | int negativeLabel = empty ? android.R.string.cancel : R.string.deny_button; |
Robin Lee | 128549b | 2016-07-11 19:34:59 +0100 | [diff] [blame] | 218 | builder.setNegativeButton(negativeLabel, new DialogInterface.OnClickListener() { |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 219 | @Override public void onClick(DialogInterface dialog, int id) { |
| 220 | dialog.cancel(); // will cause OnDismissListener to be called |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 221 | } |
| 222 | }); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 223 | |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 224 | String title; |
Robin Lee | 8c9d433 | 2016-04-26 12:49:24 +0100 | [diff] [blame] | 225 | int selectedItem = -1; |
Brian Carlstrom | df17230 | 2011-06-26 17:13:54 -0700 | [diff] [blame] | 226 | Resources res = getResources(); |
| 227 | if (empty) { |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 228 | title = res.getString(R.string.title_no_certs); |
| 229 | } else { |
| 230 | title = res.getString(R.string.title_select_cert); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 231 | String alias = getIntent().getStringExtra(KeyChain.EXTRA_ALIAS); |
Robin Lee | 8c9d433 | 2016-04-26 12:49:24 +0100 | [diff] [blame] | 232 | |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 233 | if (alias != null) { |
Brian Carlstrom | 6231655 | 2011-07-10 12:26:12 -0700 | [diff] [blame] | 234 | // if alias was requested, set it if found |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 235 | int adapterPosition = adapter.mAliases.indexOf(alias); |
| 236 | if (adapterPosition != -1) { |
Robin Lee | 8c9d433 | 2016-04-26 12:49:24 +0100 | [diff] [blame] | 237 | // increase by 1 to account for item 0 being the header. |
| 238 | selectedItem = adapterPosition + 1; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 239 | } |
Brian Carlstrom | 6231655 | 2011-07-10 12:26:12 -0700 | [diff] [blame] | 240 | } else if (adapter.mAliases.size() == 1) { |
| 241 | // if only one choice, preselect it |
Robin Lee | 8c9d433 | 2016-04-26 12:49:24 +0100 | [diff] [blame] | 242 | selectedItem = 1; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | builder.setPositiveButton(R.string.allow_button, new DialogInterface.OnClickListener() { |
| 246 | @Override public void onClick(DialogInterface dialog, int id) { |
Robin Lee | 8c9d433 | 2016-04-26 12:49:24 +0100 | [diff] [blame] | 247 | if (dialog instanceof AlertDialog) { |
| 248 | ListView lv = ((AlertDialog) dialog).getListView(); |
| 249 | int listViewPosition = lv.getCheckedItemPosition(); |
| 250 | int adapterPosition = listViewPosition-1; |
| 251 | String alias = ((adapterPosition >= 0) |
| 252 | ? adapter.getItem(adapterPosition) |
| 253 | : null); |
| 254 | finish(alias); |
| 255 | } else { |
| 256 | Log.wtf(TAG, "Expected AlertDialog, got " + dialog, new Exception()); |
| 257 | finish(null); |
| 258 | } |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 259 | } |
| 260 | }); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 261 | } |
| 262 | builder.setTitle(title); |
Robin Lee | 8c9d433 | 2016-04-26 12:49:24 +0100 | [diff] [blame] | 263 | builder.setSingleChoiceItems(adapter, selectedItem, null); |
Robin Lee | 8c9d433 | 2016-04-26 12:49:24 +0100 | [diff] [blame] | 264 | final AlertDialog dialog = builder.create(); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 265 | |
Robin Lee | 128549b | 2016-07-11 19:34:59 +0100 | [diff] [blame] | 266 | // Show text above and below the list to explain what the certificate will be used for, |
| 267 | // and how to install another one, respectively. |
Robin Lee | 8c9d433 | 2016-04-26 12:49:24 +0100 | [diff] [blame] | 268 | TextView contextView = (TextView) View.inflate(this, R.layout.cert_chooser_header, null); |
Robin Lee | 8c9d433 | 2016-04-26 12:49:24 +0100 | [diff] [blame] | 269 | |
| 270 | final ListView lv = dialog.getListView(); |
| 271 | lv.addHeaderView(contextView, null, false); |
Robin Lee | 128549b | 2016-07-11 19:34:59 +0100 | [diff] [blame] | 272 | lv.addFooterView(View.inflate(this, R.layout.cert_install, null)); |
Robin Lee | 8c9d433 | 2016-04-26 12:49:24 +0100 | [diff] [blame] | 273 | lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 274 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Robin Lee | 128549b | 2016-07-11 19:34:59 +0100 | [diff] [blame] | 275 | if (position == 0) { |
| 276 | // Header. Just text; ignore clicks. |
| 277 | return; |
| 278 | } else if (position == adapter.getCount() + 1) { |
| 279 | // Footer. Remove dialog so that we will recreate with possibly new content |
| 280 | // after install returns. |
| 281 | dialog.dismiss(); |
| 282 | Credentials.getInstance().install(KeyChainActivity.this); |
| 283 | } else { |
| 284 | dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true); |
| 285 | lv.setItemChecked(position, true); |
| 286 | adapter.notifyDataSetChanged(); |
| 287 | } |
Robin Lee | 8c9d433 | 2016-04-26 12:49:24 +0100 | [diff] [blame] | 288 | } |
| 289 | }); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 290 | |
| 291 | // getTargetPackage guarantees that the returned string is |
| 292 | // supplied by the system, so that an application can not |
| 293 | // spoof its package. |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 294 | String pkg = mSender.getIntentSender().getTargetPackage(); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 295 | PackageManager pm = getPackageManager(); |
| 296 | CharSequence applicationLabel; |
| 297 | try { |
| 298 | applicationLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString(); |
| 299 | } catch (PackageManager.NameNotFoundException e) { |
| 300 | applicationLabel = pkg; |
| 301 | } |
| 302 | String appMessage = String.format(res.getString(R.string.requesting_application), |
| 303 | applicationLabel); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 304 | String contextMessage = appMessage; |
Robin Lee | 606d9d0 | 2015-05-05 15:57:13 +0100 | [diff] [blame] | 305 | Uri uri = getIntent().getParcelableExtra(KeyChain.EXTRA_URI); |
| 306 | if (uri != null) { |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 307 | String hostMessage = String.format(res.getString(R.string.requesting_server), |
Robin Lee | 606d9d0 | 2015-05-05 15:57:13 +0100 | [diff] [blame] | 308 | uri.getAuthority()); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 309 | if (contextMessage == null) { |
| 310 | contextMessage = hostMessage; |
| 311 | } else { |
| 312 | contextMessage += " " + hostMessage; |
| 313 | } |
| 314 | } |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 315 | contextView.setText(contextMessage); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 316 | |
Robin Lee | 128549b | 2016-07-11 19:34:59 +0100 | [diff] [blame] | 317 | if (selectedItem == -1) { |
| 318 | dialog.setOnShowListener(new DialogInterface.OnShowListener() { |
| 319 | @Override |
| 320 | public void onShow(DialogInterface dialogInterface) { |
| 321 | dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false); |
| 322 | } |
| 323 | }); |
| 324 | } |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 325 | dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { |
| 326 | @Override public void onCancel(DialogInterface dialog) { |
| 327 | finish(null); |
| 328 | } |
| 329 | }); |
Brian Carlstrom | 91940e7 | 2011-06-28 20:37:31 -0700 | [diff] [blame] | 330 | dialog.show(); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Eran Messeri | 96235f6 | 2017-10-20 10:27:28 +0100 | [diff] [blame^] | 333 | private static class CertificateAdapter extends BaseAdapter { |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 334 | private final List<String> mAliases; |
| 335 | private final List<String> mSubjects = new ArrayList<String>(); |
Eran Messeri | 96235f6 | 2017-10-20 10:27:28 +0100 | [diff] [blame^] | 336 | private final KeyStore mKeyStore; |
| 337 | private final Context mContext; |
| 338 | |
| 339 | private CertificateAdapter(KeyStore keyStore, Context context, List<String> aliases) { |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 340 | mAliases = aliases; |
| 341 | mSubjects.addAll(Collections.nCopies(aliases.size(), (String) null)); |
Eran Messeri | 96235f6 | 2017-10-20 10:27:28 +0100 | [diff] [blame^] | 342 | mKeyStore = keyStore; |
| 343 | mContext = context; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 344 | } |
| 345 | @Override public int getCount() { |
| 346 | return mAliases.size(); |
| 347 | } |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 348 | @Override public String getItem(int adapterPosition) { |
| 349 | return mAliases.get(adapterPosition); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 350 | } |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 351 | @Override public long getItemId(int adapterPosition) { |
| 352 | return adapterPosition; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 353 | } |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 354 | @Override public View getView(final int adapterPosition, View view, ViewGroup parent) { |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 355 | ViewHolder holder; |
| 356 | if (view == null) { |
Eran Messeri | 96235f6 | 2017-10-20 10:27:28 +0100 | [diff] [blame^] | 357 | LayoutInflater inflater = LayoutInflater.from(mContext); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 358 | view = inflater.inflate(R.layout.cert_item, parent, false); |
| 359 | holder = new ViewHolder(); |
| 360 | holder.mAliasTextView = (TextView) view.findViewById(R.id.cert_item_alias); |
| 361 | holder.mSubjectTextView = (TextView) view.findViewById(R.id.cert_item_subject); |
| 362 | holder.mRadioButton = (RadioButton) view.findViewById(R.id.cert_item_selected); |
| 363 | view.setTag(holder); |
| 364 | } else { |
| 365 | holder = (ViewHolder) view.getTag(); |
| 366 | } |
| 367 | |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 368 | String alias = mAliases.get(adapterPosition); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 369 | |
| 370 | holder.mAliasTextView.setText(alias); |
| 371 | |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 372 | String subject = mSubjects.get(adapterPosition); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 373 | if (subject == null) { |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 374 | new CertLoader(adapterPosition, holder.mSubjectTextView).execute(); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 375 | } else { |
| 376 | holder.mSubjectTextView.setText(subject); |
| 377 | } |
| 378 | |
| 379 | ListView lv = (ListView)parent; |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 380 | int listViewCheckedItemPosition = lv.getCheckedItemPosition(); |
| 381 | int adapterCheckedItemPosition = listViewCheckedItemPosition-1; |
| 382 | holder.mRadioButton.setChecked(adapterPosition == adapterCheckedItemPosition); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 383 | return view; |
| 384 | } |
| 385 | |
| 386 | private class CertLoader extends AsyncTask<Void, Void, String> { |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 387 | private final int mAdapterPosition; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 388 | private final TextView mSubjectView; |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 389 | private CertLoader(int adapterPosition, TextView subjectView) { |
| 390 | mAdapterPosition = adapterPosition; |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 391 | mSubjectView = subjectView; |
| 392 | } |
| 393 | @Override protected String doInBackground(Void... params) { |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 394 | String alias = mAliases.get(mAdapterPosition); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 395 | byte[] bytes = mKeyStore.get(Credentials.USER_CERTIFICATE + alias); |
| 396 | if (bytes == null) { |
| 397 | return null; |
| 398 | } |
| 399 | InputStream in = new ByteArrayInputStream(bytes); |
| 400 | X509Certificate cert; |
| 401 | try { |
| 402 | CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 403 | cert = (X509Certificate)cf.generateCertificate(in); |
| 404 | } catch (CertificateException ignored) { |
| 405 | return null; |
| 406 | } |
| 407 | // bouncycastle can handle the emailAddress OID of 1.2.840.113549.1.9.1 |
| 408 | X500Principal subjectPrincipal = cert.getSubjectX500Principal(); |
| 409 | X509Name subjectName = X509Name.getInstance(subjectPrincipal.getEncoded()); |
| 410 | String subjectString = subjectName.toString(true, X509Name.DefaultSymbols); |
| 411 | return subjectString; |
| 412 | } |
| 413 | @Override protected void onPostExecute(String subjectString) { |
Brian Carlstrom | 5dd41f6 | 2011-06-29 18:51:34 -0700 | [diff] [blame] | 414 | mSubjects.set(mAdapterPosition, subjectString); |
Brian Carlstrom | 65e649e | 2011-06-24 02:13:28 -0700 | [diff] [blame] | 415 | mSubjectView.setText(subjectString); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | private static class ViewHolder { |
| 421 | TextView mAliasTextView; |
| 422 | TextView mSubjectTextView; |
| 423 | RadioButton mRadioButton; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 427 | switch (requestCode) { |
| 428 | case REQUEST_UNLOCK: |
Kenny Root | 4ff2296 | 2013-02-14 10:17:06 -0800 | [diff] [blame] | 429 | if (mKeyStore.isUnlocked()) { |
Kenny Root | 6ba32f2 | 2014-07-31 14:03:16 -0700 | [diff] [blame] | 430 | mState = State.INITIAL; |
Robin Lee | b46e493 | 2015-02-03 18:17:39 +0000 | [diff] [blame] | 431 | chooseCertificate(); |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 432 | } else { |
| 433 | // user must have canceled unlock, give up |
Kenny Root | 6ba32f2 | 2014-07-31 14:03:16 -0700 | [diff] [blame] | 434 | mState = State.UNLOCK_CANCELED; |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 435 | } |
| 436 | return; |
| 437 | default: |
| 438 | throw new AssertionError(); |
| 439 | } |
| 440 | } |
| 441 | |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 442 | private void finish(String alias) { |
| 443 | if (alias == null) { |
| 444 | setResult(RESULT_CANCELED); |
| 445 | } else { |
| 446 | Intent result = new Intent(); |
| 447 | result.putExtra(Intent.EXTRA_TEXT, alias); |
| 448 | setResult(RESULT_OK, result); |
| 449 | } |
Brian Carlstrom | f5b50a4 | 2011-06-09 16:05:09 -0700 | [diff] [blame] | 450 | IKeyChainAliasCallback keyChainAliasResponse |
| 451 | = IKeyChainAliasCallback.Stub.asInterface( |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 452 | getIntent().getIBinderExtra(KeyChain.EXTRA_RESPONSE)); |
| 453 | if (keyChainAliasResponse != null) { |
Brian Carlstrom | 7d9aa75 | 2011-07-07 11:52:27 -0700 | [diff] [blame] | 454 | new ResponseSender(keyChainAliasResponse, alias).execute(); |
| 455 | return; |
| 456 | } |
| 457 | finish(); |
| 458 | } |
| 459 | |
| 460 | private class ResponseSender extends AsyncTask<Void, Void, Void> { |
| 461 | private IKeyChainAliasCallback mKeyChainAliasResponse; |
| 462 | private String mAlias; |
| 463 | private ResponseSender(IKeyChainAliasCallback keyChainAliasResponse, String alias) { |
| 464 | mKeyChainAliasResponse = keyChainAliasResponse; |
| 465 | mAlias = alias; |
| 466 | } |
| 467 | @Override protected Void doInBackground(Void... unused) { |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 468 | try { |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 469 | if (mAlias != null) { |
| 470 | KeyChain.KeyChainConnection connection = KeyChain.bind(KeyChainActivity.this); |
| 471 | try { |
| 472 | connection.getService().setGrant(mSenderUid, mAlias, true); |
| 473 | } finally { |
| 474 | connection.close(); |
| 475 | } |
| 476 | } |
Brian Carlstrom | 7d9aa75 | 2011-07-07 11:52:27 -0700 | [diff] [blame] | 477 | mKeyChainAliasResponse.alias(mAlias); |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 478 | } catch (InterruptedException ignored) { |
| 479 | Thread.currentThread().interrupt(); |
| 480 | Log.d(TAG, "interrupted while granting access", ignored); |
Brian Carlstrom | 2a85883 | 2011-05-26 09:30:26 -0700 | [diff] [blame] | 481 | } catch (Exception ignored) { |
| 482 | // don't just catch RemoteException, caller could |
| 483 | // throw back a RuntimeException across processes |
| 484 | // which we should protect against. |
Fred Quintana | fb2e18e | 2011-07-13 14:54:05 -0700 | [diff] [blame] | 485 | Log.e(TAG, "error while granting access", ignored); |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 486 | } |
Brian Carlstrom | 7d9aa75 | 2011-07-07 11:52:27 -0700 | [diff] [blame] | 487 | return null; |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 488 | } |
Brian Carlstrom | 7d9aa75 | 2011-07-07 11:52:27 -0700 | [diff] [blame] | 489 | @Override protected void onPostExecute(Void unused) { |
| 490 | finish(); |
| 491 | } |
Brian Carlstrom | bb04f70 | 2011-05-24 21:54:51 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Brian Carlstrom | 9e606df | 2011-06-07 12:03:08 -0700 | [diff] [blame] | 494 | @Override public void onBackPressed() { |
| 495 | finish(null); |
| 496 | } |
| 497 | |
Brian Carlstrom | 3e6251d | 2011-04-11 09:05:06 -0700 | [diff] [blame] | 498 | @Override protected void onSaveInstanceState(Bundle savedState) { |
| 499 | super.onSaveInstanceState(savedState); |
| 500 | if (mState != State.INITIAL) { |
| 501 | savedState.putSerializable(KEY_STATE, mState); |
| 502 | } |
| 503 | } |
| 504 | } |