Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.settings; |
| 18 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 19 | import android.annotation.LayoutRes; |
| 20 | import android.annotation.Nullable; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 21 | import android.app.AlertDialog; |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 22 | import android.app.Dialog; |
| 23 | import android.app.DialogFragment; |
| 24 | import android.app.Fragment; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.DialogInterface; |
| 27 | import android.os.AsyncTask; |
| 28 | import android.os.Bundle; |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 29 | import android.os.Parcel; |
| 30 | import android.os.Parcelable; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 31 | import android.os.Process; |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 32 | import android.os.RemoteException; |
Robin Lee | c421db7 | 2016-03-11 16:22:23 +0000 | [diff] [blame] | 33 | import android.os.UserHandle; |
| 34 | import android.os.UserManager; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 35 | import android.security.Credentials; |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 36 | import android.security.IKeyChainService; |
| 37 | import android.security.KeyChain; |
| 38 | import android.security.KeyChain.KeyChainConnection; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 39 | import android.security.KeyStore; |
Janis Danisevskis | b705e1a | 2017-04-19 16:23:02 -0700 | [diff] [blame] | 40 | import android.security.keymaster.KeyCharacteristics; |
| 41 | import android.security.keymaster.KeymasterDefs; |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 42 | import android.util.Log; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 43 | import android.util.SparseArray; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 44 | import android.view.LayoutInflater; |
| 45 | import android.view.View; |
| 46 | import android.view.ViewGroup; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 47 | import android.widget.TextView; |
| 48 | |
Tamas Berghammer | 265d3c2 | 2016-06-22 15:34:45 +0100 | [diff] [blame] | 49 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Ricky Wai | 9579274 | 2016-05-24 19:28:53 +0100 | [diff] [blame] | 50 | import com.android.internal.widget.LockPatternUtils; |
Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame] | 51 | import com.android.settings.core.instrumentation.InstrumentedDialogFragment; |
Robin Lee | c421db7 | 2016-03-11 16:22:23 +0000 | [diff] [blame] | 52 | import com.android.settingslib.RestrictedLockUtils; |
| 53 | import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame^] | 54 | |
Janis Danisevskis | b705e1a | 2017-04-19 16:23:02 -0700 | [diff] [blame] | 55 | import java.security.UnrecoverableKeyException; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 56 | import java.util.ArrayList; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 57 | import java.util.EnumSet; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 58 | import java.util.List; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 59 | import java.util.SortedMap; |
| 60 | import java.util.TreeMap; |
| 61 | |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame^] | 62 | import androidx.recyclerview.widget.RecyclerView; |
| 63 | |
Robin Lee | ccaf9c9 | 2017-03-24 14:50:05 +0000 | [diff] [blame] | 64 | public class UserCredentialsSettings extends SettingsPreferenceFragment |
| 65 | implements View.OnClickListener { |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 66 | private static final String TAG = "UserCredentialsSettings"; |
| 67 | |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 68 | @Override |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 69 | public int getMetricsCategory() { |
Robin Lee | b70b3d8 | 2016-02-01 12:52:16 +0000 | [diff] [blame] | 70 | return MetricsEvent.USER_CREDENTIALS; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void onResume() { |
| 75 | super.onResume(); |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 76 | refreshItems(); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
Robin Lee | ccaf9c9 | 2017-03-24 14:50:05 +0000 | [diff] [blame] | 80 | public void onClick(final View view) { |
| 81 | final Credential item = (Credential) view.getTag(); |
| 82 | if (item != null) { |
| 83 | CredentialDialogFragment.show(this, item); |
| 84 | } |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 85 | } |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 86 | |
Doris Ling | 03a3b51 | 2017-10-18 14:25:01 -0700 | [diff] [blame] | 87 | @Override |
Doris Ling | ed4685f | 2017-10-25 14:08:57 -0700 | [diff] [blame] | 88 | public void onCreate(@Nullable Bundle savedInstanceState) { |
| 89 | super.onCreate(savedInstanceState); |
Doris Ling | 4a01283 | 2017-11-13 17:58:13 -0800 | [diff] [blame] | 90 | getActivity().setTitle(R.string.user_credentials); |
Doris Ling | 03a3b51 | 2017-10-18 14:25:01 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Robin Lee | 11fd550 | 2016-05-16 15:42:34 +0100 | [diff] [blame] | 93 | protected void announceRemoval(String alias) { |
Robin Lee | ccaf9c9 | 2017-03-24 14:50:05 +0000 | [diff] [blame] | 94 | if (!isAdded()) { |
| 95 | return; |
Robin Lee | 11fd550 | 2016-05-16 15:42:34 +0100 | [diff] [blame] | 96 | } |
Robin Lee | ccaf9c9 | 2017-03-24 14:50:05 +0000 | [diff] [blame] | 97 | getListView().announceForAccessibility(getString(R.string.user_credential_removed, alias)); |
Robin Lee | 11fd550 | 2016-05-16 15:42:34 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 100 | protected void refreshItems() { |
| 101 | if (isAdded()) { |
| 102 | new AliasLoader().execute(); |
| 103 | } |
| 104 | } |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 105 | |
Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame] | 106 | public static class CredentialDialogFragment extends InstrumentedDialogFragment { |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 107 | private static final String TAG = "CredentialDialogFragment"; |
| 108 | private static final String ARG_CREDENTIAL = "credential"; |
| 109 | |
| 110 | public static void show(Fragment target, Credential item) { |
| 111 | final Bundle args = new Bundle(); |
| 112 | args.putParcelable(ARG_CREDENTIAL, item); |
| 113 | |
Robin Lee | f8e2dbf | 2016-04-07 13:17:24 +0100 | [diff] [blame] | 114 | if (target.getFragmentManager().findFragmentByTag(TAG) == null) { |
| 115 | final DialogFragment frag = new CredentialDialogFragment(); |
| 116 | frag.setTargetFragment(target, /* requestCode */ -1); |
| 117 | frag.setArguments(args); |
| 118 | frag.show(target.getFragmentManager(), TAG); |
| 119 | } |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 124 | final Credential item = (Credential) getArguments().getParcelable(ARG_CREDENTIAL); |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 125 | |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 126 | View root = getActivity().getLayoutInflater() |
| 127 | .inflate(R.layout.user_credential_dialog, null); |
| 128 | ViewGroup infoContainer = (ViewGroup) root.findViewById(R.id.credential_container); |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 129 | View contentView = getCredentialView(item, R.layout.user_credential, null, |
| 130 | infoContainer, /* expanded */ true); |
| 131 | infoContainer.addView(contentView); |
Robin Lee | c421db7 | 2016-03-11 16:22:23 +0000 | [diff] [blame] | 132 | |
| 133 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 134 | .setView(root) |
| 135 | .setTitle(R.string.user_credential_title) |
Robin Lee | c421db7 | 2016-03-11 16:22:23 +0000 | [diff] [blame] | 136 | .setPositiveButton(R.string.done, null); |
| 137 | |
| 138 | final String restriction = UserManager.DISALLOW_CONFIG_CREDENTIALS; |
| 139 | final int myUserId = UserHandle.myUserId(); |
| 140 | if (!RestrictedLockUtils.hasBaseUserRestriction(getContext(), restriction, myUserId)) { |
| 141 | DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { |
| 142 | @Override public void onClick(DialogInterface dialog, int id) { |
| 143 | final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced( |
| 144 | getContext(), restriction, myUserId); |
| 145 | if (admin != null) { |
| 146 | RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(), |
| 147 | admin); |
| 148 | } else { |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 149 | new RemoveCredentialsTask(getContext(), getTargetFragment()) |
| 150 | .execute(item); |
Robin Lee | c421db7 | 2016-03-11 16:22:23 +0000 | [diff] [blame] | 151 | } |
| 152 | dialog.dismiss(); |
| 153 | } |
| 154 | }; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 155 | if (item.isSystem()) { |
| 156 | // TODO: a safe means of clearing wifi certificates. Configs refer to aliases |
| 157 | // directly so deleting certs will break dependent access points. |
| 158 | builder.setNegativeButton(R.string.trusted_credentials_remove_label, listener); |
| 159 | } |
Robin Lee | c421db7 | 2016-03-11 16:22:23 +0000 | [diff] [blame] | 160 | } |
| 161 | return builder.create(); |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Fan Zhang | 1e51628 | 2016-09-16 12:45:07 -0700 | [diff] [blame] | 164 | @Override |
| 165 | public int getMetricsCategory() { |
| 166 | return MetricsEvent.DIALOG_USER_CREDENTIAL; |
| 167 | } |
| 168 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 169 | /** |
| 170 | * Deletes all certificates and keys under a given alias. |
| 171 | * |
| 172 | * If the {@link Credential} is for a system alias, all active grants to the alias will be |
| 173 | * removed using {@link KeyChain}. |
| 174 | */ |
| 175 | private class RemoveCredentialsTask extends AsyncTask<Credential, Void, Credential[]> { |
| 176 | private Context context; |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 177 | private Fragment targetFragment; |
| 178 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 179 | public RemoveCredentialsTask(Context context, Fragment targetFragment) { |
| 180 | this.context = context; |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 181 | this.targetFragment = targetFragment; |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 182 | } |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 183 | |
| 184 | @Override |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 185 | protected Credential[] doInBackground(Credential... credentials) { |
| 186 | for (final Credential credential : credentials) { |
| 187 | if (credential.isSystem()) { |
| 188 | removeGrantsAndDelete(credential); |
| 189 | continue; |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 190 | } |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 191 | throw new UnsupportedOperationException( |
| 192 | "Not implemented for wifi certificates. This should not be reachable."); |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 193 | } |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 194 | return credentials; |
| 195 | } |
| 196 | |
| 197 | private void removeGrantsAndDelete(final Credential credential) { |
| 198 | final KeyChainConnection conn; |
| 199 | try { |
| 200 | conn = KeyChain.bind(getContext()); |
| 201 | } catch (InterruptedException e) { |
| 202 | Log.w(TAG, "Connecting to KeyChain", e); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | try { |
| 207 | IKeyChainService keyChain = conn.getService(); |
| 208 | keyChain.removeKeyPair(credential.alias); |
| 209 | } catch (RemoteException e) { |
| 210 | Log.w(TAG, "Removing credentials", e); |
| 211 | } finally { |
| 212 | conn.close(); |
| 213 | } |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 217 | protected void onPostExecute(Credential... credentials) { |
Robin Lee | 11fd550 | 2016-05-16 15:42:34 +0100 | [diff] [blame] | 218 | if (targetFragment instanceof UserCredentialsSettings && targetFragment.isAdded()) { |
| 219 | final UserCredentialsSettings target = (UserCredentialsSettings) targetFragment; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 220 | for (final Credential credential : credentials) { |
| 221 | target.announceRemoval(credential.alias); |
Robin Lee | 11fd550 | 2016-05-16 15:42:34 +0100 | [diff] [blame] | 222 | } |
| 223 | target.refreshItems(); |
Robin Lee | da7bc51 | 2016-02-24 17:39:32 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 226 | } |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Opens a background connection to KeyStore to list user credentials. |
| 231 | * The credentials are stored in a {@link CredentialAdapter} attached to the main |
| 232 | * {@link ListView} in the fragment. |
| 233 | */ |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 234 | private class AliasLoader extends AsyncTask<Void, Void, List<Credential>> { |
| 235 | /** |
| 236 | * @return a list of credentials ordered: |
| 237 | * <ol> |
| 238 | * <li>first by purpose;</li> |
| 239 | * <li>then by alias.</li> |
| 240 | * </ol> |
| 241 | */ |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 242 | @Override |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 243 | protected List<Credential> doInBackground(Void... params) { |
| 244 | final KeyStore keyStore = KeyStore.getInstance(); |
| 245 | |
| 246 | // Certificates can be installed into SYSTEM_UID or WIFI_UID through CertInstaller. |
| 247 | final int myUserId = UserHandle.myUserId(); |
| 248 | final int systemUid = UserHandle.getUid(myUserId, Process.SYSTEM_UID); |
| 249 | final int wifiUid = UserHandle.getUid(myUserId, Process.WIFI_UID); |
| 250 | |
| 251 | List<Credential> credentials = new ArrayList<>(); |
| 252 | credentials.addAll(getCredentialsForUid(keyStore, systemUid).values()); |
| 253 | credentials.addAll(getCredentialsForUid(keyStore, wifiUid).values()); |
| 254 | return credentials; |
| 255 | } |
| 256 | |
Janis Danisevskis | b705e1a | 2017-04-19 16:23:02 -0700 | [diff] [blame] | 257 | private boolean isAsymmetric(KeyStore keyStore, String alias, int uid) |
| 258 | throws UnrecoverableKeyException { |
| 259 | KeyCharacteristics keyCharacteristics = new KeyCharacteristics(); |
| 260 | int errorCode = keyStore.getKeyCharacteristics(alias, null, null, uid, |
| 261 | keyCharacteristics); |
| 262 | if (errorCode != KeyStore.NO_ERROR) { |
| 263 | throw (UnrecoverableKeyException) |
| 264 | new UnrecoverableKeyException("Failed to obtain information about key") |
| 265 | .initCause(KeyStore.getKeyStoreException(errorCode)); |
| 266 | } |
| 267 | Integer keymasterAlgorithm = keyCharacteristics.getEnum( |
| 268 | KeymasterDefs.KM_TAG_ALGORITHM); |
| 269 | if (keymasterAlgorithm == null) { |
| 270 | throw new UnrecoverableKeyException("Key algorithm unknown"); |
| 271 | } |
| 272 | return keymasterAlgorithm == KeymasterDefs.KM_ALGORITHM_RSA || |
| 273 | keymasterAlgorithm == KeymasterDefs.KM_ALGORITHM_EC; |
| 274 | } |
| 275 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 276 | private SortedMap<String, Credential> getCredentialsForUid(KeyStore keyStore, int uid) { |
| 277 | final SortedMap<String, Credential> aliasMap = new TreeMap<>(); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 278 | for (final Credential.Type type : Credential.Type.values()) { |
Janis Danisevskis | b705e1a | 2017-04-19 16:23:02 -0700 | [diff] [blame] | 279 | for (final String prefix : type.prefix) { |
| 280 | for (final String alias : keyStore.list(prefix, uid)) { |
| 281 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) { |
| 282 | // Do not show work profile keys in user credentials |
| 283 | if (alias.startsWith(LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT) || |
| 284 | alias.startsWith(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT)) { |
| 285 | continue; |
| 286 | } |
| 287 | // Do not show synthetic password keys in user credential |
| 288 | if (alias.startsWith(LockPatternUtils.SYNTHETIC_PASSWORD_KEY_PREFIX)) { |
| 289 | continue; |
| 290 | } |
| 291 | } |
| 292 | try { |
| 293 | if (type == Credential.Type.USER_KEY && |
| 294 | !isAsymmetric(keyStore, prefix + alias, uid)) { |
| 295 | continue; |
| 296 | } |
| 297 | } catch (UnrecoverableKeyException e) { |
| 298 | Log.e(TAG, "Unable to determine algorithm of key: " + prefix + alias, e); |
Rubin Xu | 52221d8 | 2017-02-09 11:09:11 +0000 | [diff] [blame] | 299 | continue; |
| 300 | } |
Janis Danisevskis | b705e1a | 2017-04-19 16:23:02 -0700 | [diff] [blame] | 301 | Credential c = aliasMap.get(alias); |
| 302 | if (c == null) { |
| 303 | c = new Credential(alias, uid); |
| 304 | aliasMap.put(alias, c); |
Rubin Xu | 52221d8 | 2017-02-09 11:09:11 +0000 | [diff] [blame] | 305 | } |
Janis Danisevskis | b705e1a | 2017-04-19 16:23:02 -0700 | [diff] [blame] | 306 | c.storedTypes.add(type); |
Ricky Wai | 9579274 | 2016-05-24 19:28:53 +0100 | [diff] [blame] | 307 | } |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 308 | } |
| 309 | } |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 310 | return aliasMap; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | @Override |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 314 | protected void onPostExecute(List<Credential> credentials) { |
Robin Lee | ccaf9c9 | 2017-03-24 14:50:05 +0000 | [diff] [blame] | 315 | if (!isAdded()) { |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | if (credentials == null || credentials.size() == 0) { |
| 320 | // Create a "no credentials installed" message for the empty case. |
| 321 | TextView emptyTextView = (TextView) getActivity().findViewById(android.R.id.empty); |
| 322 | emptyTextView.setText(R.string.user_credential_none_installed); |
| 323 | setEmptyView(emptyTextView); |
| 324 | } else { |
| 325 | setEmptyView(null); |
| 326 | } |
| 327 | |
| 328 | getListView().setAdapter( |
| 329 | new CredentialAdapter(credentials, UserCredentialsSettings.this)); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Helper class to display {@link Credential}s in a list. |
| 335 | */ |
Robin Lee | ccaf9c9 | 2017-03-24 14:50:05 +0000 | [diff] [blame] | 336 | private static class CredentialAdapter extends RecyclerView.Adapter<ViewHolder> { |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 337 | private static final int LAYOUT_RESOURCE = R.layout.user_credential_preference; |
| 338 | |
Robin Lee | ccaf9c9 | 2017-03-24 14:50:05 +0000 | [diff] [blame] | 339 | private final List<Credential> mItems; |
| 340 | private final View.OnClickListener mListener; |
| 341 | |
| 342 | public CredentialAdapter(List<Credential> items, @Nullable View.OnClickListener listener) { |
| 343 | mItems = items; |
| 344 | mListener = listener; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | @Override |
Robin Lee | ccaf9c9 | 2017-03-24 14:50:05 +0000 | [diff] [blame] | 348 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 349 | final LayoutInflater inflater = LayoutInflater.from(parent.getContext()); |
| 350 | return new ViewHolder(inflater.inflate(LAYOUT_RESOURCE, parent, false)); |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public void onBindViewHolder(ViewHolder h, int position) { |
| 355 | getCredentialView(mItems.get(position), LAYOUT_RESOURCE, h.itemView, null, false); |
| 356 | h.itemView.setTag(mItems.get(position)); |
| 357 | h.itemView.setOnClickListener(mListener); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public int getItemCount() { |
| 362 | return mItems.size(); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | private static class ViewHolder extends RecyclerView.ViewHolder { |
| 367 | public ViewHolder(View item) { |
| 368 | super(item); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 372 | /** |
| 373 | * Mapping from View IDs in {@link R} to the types of credentials they describe. |
| 374 | */ |
| 375 | private static final SparseArray<Credential.Type> credentialViewTypes = new SparseArray<>(); |
| 376 | static { |
Janis Danisevskis | b705e1a | 2017-04-19 16:23:02 -0700 | [diff] [blame] | 377 | credentialViewTypes.put(R.id.contents_userkey, Credential.Type.USER_KEY); |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 378 | credentialViewTypes.put(R.id.contents_usercrt, Credential.Type.USER_CERTIFICATE); |
| 379 | credentialViewTypes.put(R.id.contents_cacrt, Credential.Type.CA_CERTIFICATE); |
| 380 | } |
| 381 | |
| 382 | protected static View getCredentialView(Credential item, @LayoutRes int layoutResource, |
| 383 | @Nullable View view, ViewGroup parent, boolean expanded) { |
| 384 | if (view == null) { |
| 385 | view = LayoutInflater.from(parent.getContext()).inflate(layoutResource, parent, false); |
| 386 | } |
| 387 | |
| 388 | ((TextView) view.findViewById(R.id.alias)).setText(item.alias); |
| 389 | ((TextView) view.findViewById(R.id.purpose)).setText(item.isSystem() |
| 390 | ? R.string.credential_for_vpn_and_apps |
| 391 | : R.string.credential_for_wifi); |
| 392 | |
| 393 | view.findViewById(R.id.contents).setVisibility(expanded ? View.VISIBLE : View.GONE); |
| 394 | if (expanded) { |
| 395 | for (int i = 0; i < credentialViewTypes.size(); i++) { |
| 396 | final View detail = view.findViewById(credentialViewTypes.keyAt(i)); |
| 397 | detail.setVisibility(item.storedTypes.contains(credentialViewTypes.valueAt(i)) |
| 398 | ? View.VISIBLE : View.GONE); |
| 399 | } |
| 400 | } |
| 401 | return view; |
| 402 | } |
| 403 | |
| 404 | static class AliasEntry { |
| 405 | public String alias; |
| 406 | public int uid; |
| 407 | } |
| 408 | |
Robin Lee | e268042 | 2016-01-25 12:24:27 +0000 | [diff] [blame] | 409 | static class Credential implements Parcelable { |
| 410 | static enum Type { |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 411 | CA_CERTIFICATE (Credentials.CA_CERTIFICATE), |
| 412 | USER_CERTIFICATE (Credentials.USER_CERTIFICATE), |
Janis Danisevskis | b705e1a | 2017-04-19 16:23:02 -0700 | [diff] [blame] | 413 | USER_KEY(Credentials.USER_PRIVATE_KEY, Credentials.USER_SECRET_KEY); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 414 | |
Janis Danisevskis | b705e1a | 2017-04-19 16:23:02 -0700 | [diff] [blame] | 415 | final String[] prefix; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 416 | |
Janis Danisevskis | b705e1a | 2017-04-19 16:23:02 -0700 | [diff] [blame] | 417 | Type(String... prefix) { |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 418 | this.prefix = prefix; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Main part of the credential's alias. To fetch an item from KeyStore, prepend one of the |
| 424 | * prefixes from {@link CredentialItem.storedTypes}. |
| 425 | */ |
| 426 | final String alias; |
| 427 | |
| 428 | /** |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 429 | * UID under which this credential is stored. Typically {@link Process#SYSTEM_UID} but can |
| 430 | * also be {@link Process#WIFI_UID} for credentials installed as wifi certificates. |
| 431 | */ |
| 432 | final int uid; |
| 433 | |
| 434 | /** |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 435 | * Should contain some non-empty subset of: |
| 436 | * <ul> |
| 437 | * <li>{@link Credentials.CA_CERTIFICATE}</li> |
| 438 | * <li>{@link Credentials.USER_CERTIFICATE}</li> |
Janis Danisevskis | b705e1a | 2017-04-19 16:23:02 -0700 | [diff] [blame] | 439 | * <li>{@link Credentials.USER_KEY}</li> |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 440 | * </ul> |
| 441 | */ |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 442 | final EnumSet<Type> storedTypes = EnumSet.noneOf(Type.class); |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 443 | |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 444 | Credential(final String alias, final int uid) { |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 445 | this.alias = alias; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 446 | this.uid = uid; |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 447 | } |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 448 | |
| 449 | Credential(Parcel in) { |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 450 | this(in.readString(), in.readInt()); |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 451 | |
| 452 | long typeBits = in.readLong(); |
| 453 | for (Type i : Type.values()) { |
| 454 | if ((typeBits & (1L << i.ordinal())) != 0L) { |
| 455 | storedTypes.add(i); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | public void writeToParcel(Parcel out, int flags) { |
| 461 | out.writeString(alias); |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 462 | out.writeInt(uid); |
Robin Lee | 04046a1 | 2016-01-19 11:42:57 +0000 | [diff] [blame] | 463 | |
| 464 | long typeBits = 0; |
| 465 | for (Type i : storedTypes) { |
| 466 | typeBits |= 1L << i.ordinal(); |
| 467 | } |
| 468 | out.writeLong(typeBits); |
| 469 | } |
| 470 | |
| 471 | public int describeContents() { |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | public static final Parcelable.Creator<Credential> CREATOR |
| 476 | = new Parcelable.Creator<Credential>() { |
| 477 | public Credential createFromParcel(Parcel in) { |
| 478 | return new Credential(in); |
| 479 | } |
| 480 | |
| 481 | public Credential[] newArray(int size) { |
| 482 | return new Credential[size]; |
| 483 | } |
| 484 | }; |
Robin Lee | e68d957 | 2016-07-19 16:10:39 +0100 | [diff] [blame] | 485 | |
| 486 | public boolean isSystem() { |
| 487 | return UserHandle.getAppId(uid) == Process.SYSTEM_UID; |
| 488 | } |
Robin Lee | baefdcf | 2015-08-26 10:57:44 +0100 | [diff] [blame] | 489 | } |
| 490 | } |