blob: 5f72ca58423f07f38256ef48f9869c79ed411c1b [file] [log] [blame]
Robin Leebaefdcf2015-08-26 10:57:44 +01001/*
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
17package com.android.settings;
18
Robin Leee68d9572016-07-19 16:10:39 +010019import android.annotation.LayoutRes;
20import android.annotation.Nullable;
Robin Lee04046a12016-01-19 11:42:57 +000021import android.app.Dialog;
Fan Zhang31b21002019-01-16 13:49:47 -080022import android.app.settings.SettingsEnums;
Robin Leebaefdcf2015-08-26 10:57:44 +010023import android.content.Context;
24import android.content.DialogInterface;
25import android.os.AsyncTask;
26import android.os.Bundle;
Robin Lee04046a12016-01-19 11:42:57 +000027import android.os.Parcel;
28import android.os.Parcelable;
Robin Leee68d9572016-07-19 16:10:39 +010029import android.os.Process;
Robin Leeda7bc512016-02-24 17:39:32 +000030import android.os.RemoteException;
Robin Leec421db72016-03-11 16:22:23 +000031import android.os.UserHandle;
32import android.os.UserManager;
Robin Leebaefdcf2015-08-26 10:57:44 +010033import android.security.Credentials;
Robin Leeda7bc512016-02-24 17:39:32 +000034import android.security.IKeyChainService;
35import android.security.KeyChain;
36import android.security.KeyChain.KeyChainConnection;
Janis Danisevskisa05bd652021-01-25 14:54:48 -080037import android.security.keystore.AndroidKeyStoreProvider;
38import android.security.keystore.KeyProperties;
39import android.security.keystore2.AndroidKeyStoreLoadStoreParameter;
Robin Leeda7bc512016-02-24 17:39:32 +000040import android.util.Log;
Robin Leee68d9572016-07-19 16:10:39 +010041import android.util.SparseArray;
Robin Leebaefdcf2015-08-26 10:57:44 +010042import android.view.LayoutInflater;
43import android.view.View;
44import android.view.ViewGroup;
Robin Leebaefdcf2015-08-26 10:57:44 +010045import android.widget.TextView;
46
Fan Zhang23f8d592018-08-28 15:11:40 -070047import androidx.appcompat.app.AlertDialog;
48import androidx.fragment.app.DialogFragment;
49import androidx.fragment.app.Fragment;
50import androidx.recyclerview.widget.RecyclerView;
51
Ricky Wai95792742016-05-24 19:28:53 +010052import com.android.internal.widget.LockPatternUtils;
Fan Zhang1e516282016-09-16 12:45:07 -070053import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Robin Leec421db72016-03-11 16:22:23 +000054import com.android.settingslib.RestrictedLockUtils;
55import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070056import com.android.settingslib.RestrictedLockUtilsInternal;
Fan Zhangc7162cd2018-06-18 15:21:41 -070057
Janis Danisevskisa05bd652021-01-25 14:54:48 -080058import java.security.Key;
59import java.security.KeyStore;
60import java.security.KeyStoreException;
61import java.security.NoSuchAlgorithmException;
Janis Danisevskisb705e1a2017-04-19 16:23:02 -070062import java.security.UnrecoverableKeyException;
Janis Danisevskisa05bd652021-01-25 14:54:48 -080063import java.security.cert.Certificate;
Robin Leee68d9572016-07-19 16:10:39 +010064import java.util.ArrayList;
Robin Leebaefdcf2015-08-26 10:57:44 +010065import java.util.EnumSet;
Janis Danisevskisa05bd652021-01-25 14:54:48 -080066import java.util.Enumeration;
Robin Leee68d9572016-07-19 16:10:39 +010067import java.util.List;
Robin Leebaefdcf2015-08-26 10:57:44 +010068import java.util.SortedMap;
69import java.util.TreeMap;
70
Janis Danisevskisa05bd652021-01-25 14:54:48 -080071import javax.crypto.SecretKey;
72
Robin Leeccaf9c92017-03-24 14:50:05 +000073public class UserCredentialsSettings extends SettingsPreferenceFragment
74 implements View.OnClickListener {
Robin Leebaefdcf2015-08-26 10:57:44 +010075 private static final String TAG = "UserCredentialsSettings";
76
Robin Leebaefdcf2015-08-26 10:57:44 +010077 @Override
Fan Zhang65076132016-08-08 10:25:13 -070078 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -080079 return SettingsEnums.USER_CREDENTIALS;
Robin Leebaefdcf2015-08-26 10:57:44 +010080 }
81
82 @Override
83 public void onResume() {
84 super.onResume();
Robin Lee04046a12016-01-19 11:42:57 +000085 refreshItems();
Robin Leebaefdcf2015-08-26 10:57:44 +010086 }
87
88 @Override
Robin Leeccaf9c92017-03-24 14:50:05 +000089 public void onClick(final View view) {
90 final Credential item = (Credential) view.getTag();
91 if (item != null) {
92 CredentialDialogFragment.show(this, item);
93 }
Robin Lee04046a12016-01-19 11:42:57 +000094 }
Robin Leebaefdcf2015-08-26 10:57:44 +010095
Doris Ling03a3b512017-10-18 14:25:01 -070096 @Override
Doris Linged4685f2017-10-25 14:08:57 -070097 public void onCreate(@Nullable Bundle savedInstanceState) {
98 super.onCreate(savedInstanceState);
Doris Ling4a012832017-11-13 17:58:13 -080099 getActivity().setTitle(R.string.user_credentials);
Doris Ling03a3b512017-10-18 14:25:01 -0700100 }
101
Robin Lee11fd5502016-05-16 15:42:34 +0100102 protected void announceRemoval(String alias) {
Robin Leeccaf9c92017-03-24 14:50:05 +0000103 if (!isAdded()) {
104 return;
Robin Lee11fd5502016-05-16 15:42:34 +0100105 }
Robin Leeccaf9c92017-03-24 14:50:05 +0000106 getListView().announceForAccessibility(getString(R.string.user_credential_removed, alias));
Robin Lee11fd5502016-05-16 15:42:34 +0100107 }
108
Robin Lee04046a12016-01-19 11:42:57 +0000109 protected void refreshItems() {
110 if (isAdded()) {
111 new AliasLoader().execute();
112 }
113 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100114
Fan Zhang1e516282016-09-16 12:45:07 -0700115 public static class CredentialDialogFragment extends InstrumentedDialogFragment {
Robin Lee04046a12016-01-19 11:42:57 +0000116 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 Leef8e2dbf2016-04-07 13:17:24 +0100123 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 Lee04046a12016-01-19 11:42:57 +0000129 }
130
131 @Override
132 public Dialog onCreateDialog(Bundle savedInstanceState) {
133 final Credential item = (Credential) getArguments().getParcelable(ARG_CREDENTIAL);
Robin Leee68d9572016-07-19 16:10:39 +0100134
Robin Lee04046a12016-01-19 11:42:57 +0000135 View root = getActivity().getLayoutInflater()
136 .inflate(R.layout.user_credential_dialog, null);
137 ViewGroup infoContainer = (ViewGroup) root.findViewById(R.id.credential_container);
Robin Leee68d9572016-07-19 16:10:39 +0100138 View contentView = getCredentialView(item, R.layout.user_credential, null,
139 infoContainer, /* expanded */ true);
140 infoContainer.addView(contentView);
Robin Leec421db72016-03-11 16:22:23 +0000141
142 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
Robin Lee04046a12016-01-19 11:42:57 +0000143 .setView(root)
144 .setTitle(R.string.user_credential_title)
Robin Leec421db72016-03-11 16:22:23 +0000145 .setPositiveButton(R.string.done, null);
146
147 final String restriction = UserManager.DISALLOW_CONFIG_CREDENTIALS;
148 final int myUserId = UserHandle.myUserId();
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700149 if (!RestrictedLockUtilsInternal.hasBaseUserRestriction(getContext(), restriction,
150 myUserId)) {
Robin Leec421db72016-03-11 16:22:23 +0000151 DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
152 @Override public void onClick(DialogInterface dialog, int id) {
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700153 final EnforcedAdmin admin = RestrictedLockUtilsInternal
154 .checkIfRestrictionEnforced(getContext(), restriction, myUserId);
Robin Leec421db72016-03-11 16:22:23 +0000155 if (admin != null) {
156 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
157 admin);
158 } else {
Robin Leee68d9572016-07-19 16:10:39 +0100159 new RemoveCredentialsTask(getContext(), getTargetFragment())
160 .execute(item);
Robin Leec421db72016-03-11 16:22:23 +0000161 }
162 dialog.dismiss();
163 }
164 };
Hai Shalom23666052019-03-07 15:54:25 -0800165 // 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 Leec421db72016-03-11 16:22:23 +0000174 }
175 return builder.create();
Robin Lee04046a12016-01-19 11:42:57 +0000176 }
177
Fan Zhang1e516282016-09-16 12:45:07 -0700178 @Override
179 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800180 return SettingsEnums.DIALOG_USER_CREDENTIAL;
Fan Zhang1e516282016-09-16 12:45:07 -0700181 }
182
Robin Leee68d9572016-07-19 16:10:39 +0100183 /**
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 Shalom23666052019-03-07 15:54:25 -0800187 * 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 Leee68d9572016-07-19 16:10:39 +0100189 */
190 private class RemoveCredentialsTask extends AsyncTask<Credential, Void, Credential[]> {
191 private Context context;
Robin Leeda7bc512016-02-24 17:39:32 +0000192 private Fragment targetFragment;
193
Robin Leee68d9572016-07-19 16:10:39 +0100194 public RemoveCredentialsTask(Context context, Fragment targetFragment) {
195 this.context = context;
Robin Leeda7bc512016-02-24 17:39:32 +0000196 this.targetFragment = targetFragment;
Robin Lee04046a12016-01-19 11:42:57 +0000197 }
Robin Leeda7bc512016-02-24 17:39:32 +0000198
199 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100200 protected Credential[] doInBackground(Credential... credentials) {
201 for (final Credential credential : credentials) {
202 if (credential.isSystem()) {
203 removeGrantsAndDelete(credential);
Hai Shalom23666052019-03-07 15:54:25 -0800204 } else {
205 deleteWifiCredential(credential);
Robin Leeda7bc512016-02-24 17:39:32 +0000206 }
Robin Leeda7bc512016-02-24 17:39:32 +0000207 }
Robin Leee68d9572016-07-19 16:10:39 +0100208 return credentials;
209 }
210
Hai Shalom23666052019-03-07 15:54:25 -0800211 private void deleteWifiCredential(final Credential credential) {
Janis Danisevskisa05bd652021-01-25 14:54:48 -0800212 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 Shalom23666052019-03-07 15:54:25 -0800225 }
226 }
227
Robin Leee68d9572016-07-19 16:10:39 +0100228 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 Leeda7bc512016-02-24 17:39:32 +0000245 }
246
247 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100248 protected void onPostExecute(Credential... credentials) {
Robin Lee11fd5502016-05-16 15:42:34 +0100249 if (targetFragment instanceof UserCredentialsSettings && targetFragment.isAdded()) {
250 final UserCredentialsSettings target = (UserCredentialsSettings) targetFragment;
Robin Leee68d9572016-07-19 16:10:39 +0100251 for (final Credential credential : credentials) {
252 target.announceRemoval(credential.alias);
Robin Lee11fd5502016-05-16 15:42:34 +0100253 }
254 target.refreshItems();
Robin Leeda7bc512016-02-24 17:39:32 +0000255 }
256 }
Robin Lee04046a12016-01-19 11:42:57 +0000257 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100258 }
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 Leee68d9572016-07-19 16:10:39 +0100265 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 Leebaefdcf2015-08-26 10:57:44 +0100273 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100274 protected List<Credential> doInBackground(Void... params) {
Robin Leee68d9572016-07-19 16:10:39 +0100275 // 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 Danisevskisa05bd652021-01-25 14:54:48 -0800280 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 Leee68d9572016-07-19 16:10:39 +0100294
Janis Danisevskisa05bd652021-01-25 14:54:48 -0800295 List<Credential> credentials = new ArrayList<>();
296 credentials.addAll(getCredentialsForUid(processKeystore, systemUid).values());
297 if (wifiKeystore != null) {
298 credentials.addAll(getCredentialsForUid(wifiKeystore, wifiUid).values());
Janis Danisevskisb705e1a2017-04-19 16:23:02 -0700299 }
Janis Danisevskisa05bd652021-01-25 14:54:48 -0800300 return credentials;
301 } catch (Exception e) {
302 throw new RuntimeException("Failed to load credentials from Keystore.", e);
303 }
Janis Danisevskisb705e1a2017-04-19 16:23:02 -0700304 }
305
Robin Leee68d9572016-07-19 16:10:39 +0100306 private SortedMap<String, Credential> getCredentialsForUid(KeyStore keyStore, int uid) {
Janis Danisevskisa05bd652021-01-25 14:54:48 -0800307 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 Danisevskisb705e1a2017-04-19 16:23:02 -0700328 // 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 Danisevskisa05bd652021-01-25 14:54:48 -0800334 // We should never reach this point because the synthetic password key
335 // is symmetric.
Janis Danisevskisb705e1a2017-04-19 16:23:02 -0700336 if (alias.startsWith(LockPatternUtils.SYNTHETIC_PASSWORD_KEY_PREFIX)) {
337 continue;
338 }
339 }
Janis Danisevskisa05bd652021-01-25 14:54:48 -0800340 // 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 Danisevskisb705e1a2017-04-19 16:23:02 -0700349 }
Rubin Xu52221d82017-02-09 11:09:11 +0000350 }
Janis Danisevskisa05bd652021-01-25 14:54:48 -0800351 } 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 Xu52221d82017-02-09 11:09:11 +0000364 }
Ricky Wai95792742016-05-24 19:28:53 +0100365 }
Janis Danisevskisa05bd652021-01-25 14:54:48 -0800366 aliasMap.put(alias, c);
Robin Leebaefdcf2015-08-26 10:57:44 +0100367 }
Janis Danisevskisa05bd652021-01-25 14:54:48 -0800368 return aliasMap;
369 } catch (KeyStoreException e) {
370 throw new RuntimeException("Failed to load credential from Android Keystore.", e);
Robin Leebaefdcf2015-08-26 10:57:44 +0100371 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100372 }
373
374 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100375 protected void onPostExecute(List<Credential> credentials) {
Robin Leeccaf9c92017-03-24 14:50:05 +0000376 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 Leebaefdcf2015-08-26 10:57:44 +0100391 }
392 }
393
394 /**
395 * Helper class to display {@link Credential}s in a list.
396 */
Robin Leeccaf9c92017-03-24 14:50:05 +0000397 private static class CredentialAdapter extends RecyclerView.Adapter<ViewHolder> {
Robin Leee68d9572016-07-19 16:10:39 +0100398 private static final int LAYOUT_RESOURCE = R.layout.user_credential_preference;
399
Robin Leeccaf9c92017-03-24 14:50:05 +0000400 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 Leebaefdcf2015-08-26 10:57:44 +0100406 }
407
408 @Override
Robin Leeccaf9c92017-03-24 14:50:05 +0000409 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 Leebaefdcf2015-08-26 10:57:44 +0100430 }
431 }
432
Robin Leee68d9572016-07-19 16:10:39 +0100433 /**
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 Danisevskisb705e1a2017-04-19 16:23:02 -0700438 credentialViewTypes.put(R.id.contents_userkey, Credential.Type.USER_KEY);
Robin Leee68d9572016-07-19 16:10:39 +0100439 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 Leee2680422016-01-25 12:24:27 +0000470 static class Credential implements Parcelable {
471 static enum Type {
Robin Leebaefdcf2015-08-26 10:57:44 +0100472 CA_CERTIFICATE (Credentials.CA_CERTIFICATE),
473 USER_CERTIFICATE (Credentials.USER_CERTIFICATE),
Janis Danisevskisb705e1a2017-04-19 16:23:02 -0700474 USER_KEY(Credentials.USER_PRIVATE_KEY, Credentials.USER_SECRET_KEY);
Robin Leebaefdcf2015-08-26 10:57:44 +0100475
Janis Danisevskisb705e1a2017-04-19 16:23:02 -0700476 final String[] prefix;
Robin Leebaefdcf2015-08-26 10:57:44 +0100477
Janis Danisevskisb705e1a2017-04-19 16:23:02 -0700478 Type(String... prefix) {
Robin Leebaefdcf2015-08-26 10:57:44 +0100479 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 Leee68d9572016-07-19 16:10:39 +0100490 * 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 Leebaefdcf2015-08-26 10:57:44 +0100496 * Should contain some non-empty subset of:
497 * <ul>
498 * <li>{@link Credentials.CA_CERTIFICATE}</li>
499 * <li>{@link Credentials.USER_CERTIFICATE}</li>
Janis Danisevskisb705e1a2017-04-19 16:23:02 -0700500 * <li>{@link Credentials.USER_KEY}</li>
Robin Leebaefdcf2015-08-26 10:57:44 +0100501 * </ul>
502 */
Robin Lee04046a12016-01-19 11:42:57 +0000503 final EnumSet<Type> storedTypes = EnumSet.noneOf(Type.class);
Robin Leebaefdcf2015-08-26 10:57:44 +0100504
Robin Leee68d9572016-07-19 16:10:39 +0100505 Credential(final String alias, final int uid) {
Robin Leebaefdcf2015-08-26 10:57:44 +0100506 this.alias = alias;
Robin Leee68d9572016-07-19 16:10:39 +0100507 this.uid = uid;
Robin Leebaefdcf2015-08-26 10:57:44 +0100508 }
Robin Lee04046a12016-01-19 11:42:57 +0000509
510 Credential(Parcel in) {
Robin Leee68d9572016-07-19 16:10:39 +0100511 this(in.readString(), in.readInt());
Robin Lee04046a12016-01-19 11:42:57 +0000512
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 Leee68d9572016-07-19 16:10:39 +0100523 out.writeInt(uid);
Robin Lee04046a12016-01-19 11:42:57 +0000524
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 Leee68d9572016-07-19 16:10:39 +0100546
547 public boolean isSystem() {
548 return UserHandle.getAppId(uid) == Process.SYSTEM_UID;
549 }
Hai Shalom23666052019-03-07 15:54:25 -0800550
551 public String getAlias() { return alias; }
552
553 public EnumSet<Type> getStoredTypes() {
554 return storedTypes;
555 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100556 }
557}