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