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