blob: f8966a59a4184c89f6709e5ea676be70c465d0d9 [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 Leebaefdcf2015-08-26 10:57:44 +010021import android.app.AlertDialog;
Robin Lee04046a12016-01-19 11:42:57 +000022import android.app.Dialog;
23import android.app.DialogFragment;
24import android.app.Fragment;
Robin Leebaefdcf2015-08-26 10:57:44 +010025import android.content.Context;
26import android.content.DialogInterface;
27import android.os.AsyncTask;
28import android.os.Bundle;
Robin Lee04046a12016-01-19 11:42:57 +000029import android.os.Parcel;
30import android.os.Parcelable;
Robin Leee68d9572016-07-19 16:10:39 +010031import android.os.Process;
Robin Leeda7bc512016-02-24 17:39:32 +000032import android.os.RemoteException;
Robin Leec421db72016-03-11 16:22:23 +000033import android.os.UserHandle;
34import android.os.UserManager;
Robin Leebaefdcf2015-08-26 10:57:44 +010035import android.security.Credentials;
Robin Leeda7bc512016-02-24 17:39:32 +000036import android.security.IKeyChainService;
37import android.security.KeyChain;
38import android.security.KeyChain.KeyChainConnection;
Robin Leebaefdcf2015-08-26 10:57:44 +010039import android.security.KeyStore;
Robin Leeccaf9c92017-03-24 14:50:05 +000040import android.support.v7.widget.RecyclerView;
Robin Leeda7bc512016-02-24 17:39:32 +000041import android.util.Log;
Robin Leee68d9572016-07-19 16:10:39 +010042import android.util.SparseArray;
Robin Leebaefdcf2015-08-26 10:57:44 +010043import android.view.LayoutInflater;
44import android.view.View;
45import android.view.ViewGroup;
Robin Leebaefdcf2015-08-26 10:57:44 +010046import android.widget.TextView;
47
Tamas Berghammer265d3c22016-06-22 15:34:45 +010048import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Ricky Wai95792742016-05-24 19:28:53 +010049import com.android.internal.widget.LockPatternUtils;
Fan Zhang1e516282016-09-16 12:45:07 -070050import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Robin Leeccaf9c92017-03-24 14:50:05 +000051import com.android.settings.SettingsPreferenceFragment;
Robin Leec421db72016-03-11 16:22:23 +000052import com.android.settingslib.RestrictedLockUtils;
53import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
Robin Leeb70b3d82016-02-01 12:52:16 +000054
Robin Leee68d9572016-07-19 16:10:39 +010055import java.util.ArrayList;
Robin Leebaefdcf2015-08-26 10:57:44 +010056import java.util.EnumSet;
Robin Leee68d9572016-07-19 16:10:39 +010057import java.util.List;
Robin Leebaefdcf2015-08-26 10:57:44 +010058import java.util.SortedMap;
59import java.util.TreeMap;
60
Robin Leebaefdcf2015-08-26 10:57:44 +010061import static android.view.View.GONE;
Jason Monk39b46742015-09-10 15:52:51 -040062import static android.view.View.VISIBLE;
Robin Leebaefdcf2015-08-26 10:57:44 +010063
Robin Leeccaf9c92017-03-24 14:50:05 +000064public class UserCredentialsSettings extends SettingsPreferenceFragment
65 implements View.OnClickListener {
Robin Leebaefdcf2015-08-26 10:57:44 +010066 private static final String TAG = "UserCredentialsSettings";
67
Robin Leebaefdcf2015-08-26 10:57:44 +010068 @Override
Fan Zhang65076132016-08-08 10:25:13 -070069 public int getMetricsCategory() {
Robin Leeb70b3d82016-02-01 12:52:16 +000070 return MetricsEvent.USER_CREDENTIALS;
Robin Leebaefdcf2015-08-26 10:57:44 +010071 }
72
73 @Override
74 public void onResume() {
75 super.onResume();
Robin Lee04046a12016-01-19 11:42:57 +000076 refreshItems();
Robin Leebaefdcf2015-08-26 10:57:44 +010077 }
78
79 @Override
Robin Leeccaf9c92017-03-24 14:50:05 +000080 public void onClick(final View view) {
81 final Credential item = (Credential) view.getTag();
82 if (item != null) {
83 CredentialDialogFragment.show(this, item);
84 }
Robin Lee04046a12016-01-19 11:42:57 +000085 }
Robin Leebaefdcf2015-08-26 10:57:44 +010086
Doris Ling03a3b512017-10-18 14:25:01 -070087 @Override
Doris Linged4685f2017-10-25 14:08:57 -070088 public void onCreate(@Nullable Bundle savedInstanceState) {
89 super.onCreate(savedInstanceState);
90 if (usePreferenceScreenTitle()) {
91 getActivity().setTitle(R.string.user_credentials);
92 }
Doris Ling03a3b512017-10-18 14:25:01 -070093 }
94
Robin Lee11fd5502016-05-16 15:42:34 +010095 protected void announceRemoval(String alias) {
Robin Leeccaf9c92017-03-24 14:50:05 +000096 if (!isAdded()) {
97 return;
Robin Lee11fd5502016-05-16 15:42:34 +010098 }
Robin Leeccaf9c92017-03-24 14:50:05 +000099 getListView().announceForAccessibility(getString(R.string.user_credential_removed, alias));
Robin Lee11fd5502016-05-16 15:42:34 +0100100 }
101
Robin Lee04046a12016-01-19 11:42:57 +0000102 protected void refreshItems() {
103 if (isAdded()) {
104 new AliasLoader().execute();
105 }
106 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100107
Fan Zhang1e516282016-09-16 12:45:07 -0700108 public static class CredentialDialogFragment extends InstrumentedDialogFragment {
Robin Lee04046a12016-01-19 11:42:57 +0000109 private static final String TAG = "CredentialDialogFragment";
110 private static final String ARG_CREDENTIAL = "credential";
111
112 public static void show(Fragment target, Credential item) {
113 final Bundle args = new Bundle();
114 args.putParcelable(ARG_CREDENTIAL, item);
115
Robin Leef8e2dbf2016-04-07 13:17:24 +0100116 if (target.getFragmentManager().findFragmentByTag(TAG) == null) {
117 final DialogFragment frag = new CredentialDialogFragment();
118 frag.setTargetFragment(target, /* requestCode */ -1);
119 frag.setArguments(args);
120 frag.show(target.getFragmentManager(), TAG);
121 }
Robin Lee04046a12016-01-19 11:42:57 +0000122 }
123
124 @Override
125 public Dialog onCreateDialog(Bundle savedInstanceState) {
126 final Credential item = (Credential) getArguments().getParcelable(ARG_CREDENTIAL);
Robin Leee68d9572016-07-19 16:10:39 +0100127
Robin Lee04046a12016-01-19 11:42:57 +0000128 View root = getActivity().getLayoutInflater()
129 .inflate(R.layout.user_credential_dialog, null);
130 ViewGroup infoContainer = (ViewGroup) root.findViewById(R.id.credential_container);
Robin Leee68d9572016-07-19 16:10:39 +0100131 View contentView = getCredentialView(item, R.layout.user_credential, null,
132 infoContainer, /* expanded */ true);
133 infoContainer.addView(contentView);
Robin Leec421db72016-03-11 16:22:23 +0000134
135 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
Robin Lee04046a12016-01-19 11:42:57 +0000136 .setView(root)
137 .setTitle(R.string.user_credential_title)
Robin Leec421db72016-03-11 16:22:23 +0000138 .setPositiveButton(R.string.done, null);
139
140 final String restriction = UserManager.DISALLOW_CONFIG_CREDENTIALS;
141 final int myUserId = UserHandle.myUserId();
142 if (!RestrictedLockUtils.hasBaseUserRestriction(getContext(), restriction, myUserId)) {
143 DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
144 @Override public void onClick(DialogInterface dialog, int id) {
145 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(
146 getContext(), restriction, myUserId);
147 if (admin != null) {
148 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
149 admin);
150 } else {
Robin Leee68d9572016-07-19 16:10:39 +0100151 new RemoveCredentialsTask(getContext(), getTargetFragment())
152 .execute(item);
Robin Leec421db72016-03-11 16:22:23 +0000153 }
154 dialog.dismiss();
155 }
156 };
Robin Leee68d9572016-07-19 16:10:39 +0100157 if (item.isSystem()) {
158 // TODO: a safe means of clearing wifi certificates. Configs refer to aliases
159 // directly so deleting certs will break dependent access points.
160 builder.setNegativeButton(R.string.trusted_credentials_remove_label, listener);
161 }
Robin Leec421db72016-03-11 16:22:23 +0000162 }
163 return builder.create();
Robin Lee04046a12016-01-19 11:42:57 +0000164 }
165
Fan Zhang1e516282016-09-16 12:45:07 -0700166 @Override
167 public int getMetricsCategory() {
168 return MetricsEvent.DIALOG_USER_CREDENTIAL;
169 }
170
Robin Leee68d9572016-07-19 16:10:39 +0100171 /**
172 * Deletes all certificates and keys under a given alias.
173 *
174 * If the {@link Credential} is for a system alias, all active grants to the alias will be
175 * removed using {@link KeyChain}.
176 */
177 private class RemoveCredentialsTask extends AsyncTask<Credential, Void, Credential[]> {
178 private Context context;
Robin Leeda7bc512016-02-24 17:39:32 +0000179 private Fragment targetFragment;
180
Robin Leee68d9572016-07-19 16:10:39 +0100181 public RemoveCredentialsTask(Context context, Fragment targetFragment) {
182 this.context = context;
Robin Leeda7bc512016-02-24 17:39:32 +0000183 this.targetFragment = targetFragment;
Robin Lee04046a12016-01-19 11:42:57 +0000184 }
Robin Leeda7bc512016-02-24 17:39:32 +0000185
186 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100187 protected Credential[] doInBackground(Credential... credentials) {
188 for (final Credential credential : credentials) {
189 if (credential.isSystem()) {
190 removeGrantsAndDelete(credential);
191 continue;
Robin Leeda7bc512016-02-24 17:39:32 +0000192 }
Robin Leee68d9572016-07-19 16:10:39 +0100193 throw new UnsupportedOperationException(
194 "Not implemented for wifi certificates. This should not be reachable.");
Robin Leeda7bc512016-02-24 17:39:32 +0000195 }
Robin Leee68d9572016-07-19 16:10:39 +0100196 return credentials;
197 }
198
199 private void removeGrantsAndDelete(final Credential credential) {
200 final KeyChainConnection conn;
201 try {
202 conn = KeyChain.bind(getContext());
203 } catch (InterruptedException e) {
204 Log.w(TAG, "Connecting to KeyChain", e);
205 return;
206 }
207
208 try {
209 IKeyChainService keyChain = conn.getService();
210 keyChain.removeKeyPair(credential.alias);
211 } catch (RemoteException e) {
212 Log.w(TAG, "Removing credentials", e);
213 } finally {
214 conn.close();
215 }
Robin Leeda7bc512016-02-24 17:39:32 +0000216 }
217
218 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100219 protected void onPostExecute(Credential... credentials) {
Robin Lee11fd5502016-05-16 15:42:34 +0100220 if (targetFragment instanceof UserCredentialsSettings && targetFragment.isAdded()) {
221 final UserCredentialsSettings target = (UserCredentialsSettings) targetFragment;
Robin Leee68d9572016-07-19 16:10:39 +0100222 for (final Credential credential : credentials) {
223 target.announceRemoval(credential.alias);
Robin Lee11fd5502016-05-16 15:42:34 +0100224 }
225 target.refreshItems();
Robin Leeda7bc512016-02-24 17:39:32 +0000226 }
227 }
Robin Lee04046a12016-01-19 11:42:57 +0000228 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100229 }
230
231 /**
232 * Opens a background connection to KeyStore to list user credentials.
233 * The credentials are stored in a {@link CredentialAdapter} attached to the main
234 * {@link ListView} in the fragment.
235 */
Robin Leee68d9572016-07-19 16:10:39 +0100236 private class AliasLoader extends AsyncTask<Void, Void, List<Credential>> {
237 /**
238 * @return a list of credentials ordered:
239 * <ol>
240 * <li>first by purpose;</li>
241 * <li>then by alias.</li>
242 * </ol>
243 */
Robin Leebaefdcf2015-08-26 10:57:44 +0100244 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100245 protected List<Credential> doInBackground(Void... params) {
246 final KeyStore keyStore = KeyStore.getInstance();
247
248 // Certificates can be installed into SYSTEM_UID or WIFI_UID through CertInstaller.
249 final int myUserId = UserHandle.myUserId();
250 final int systemUid = UserHandle.getUid(myUserId, Process.SYSTEM_UID);
251 final int wifiUid = UserHandle.getUid(myUserId, Process.WIFI_UID);
252
253 List<Credential> credentials = new ArrayList<>();
254 credentials.addAll(getCredentialsForUid(keyStore, systemUid).values());
255 credentials.addAll(getCredentialsForUid(keyStore, wifiUid).values());
256 return credentials;
257 }
258
259 private SortedMap<String, Credential> getCredentialsForUid(KeyStore keyStore, int uid) {
260 final SortedMap<String, Credential> aliasMap = new TreeMap<>();
Robin Leebaefdcf2015-08-26 10:57:44 +0100261 for (final Credential.Type type : Credential.Type.values()) {
Robin Leee68d9572016-07-19 16:10:39 +0100262 for (final String alias : keyStore.list(type.prefix, uid)) {
Rubin Xu52221d82017-02-09 11:09:11 +0000263 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) {
264 // Do not show work profile keys in user credentials
265 if (alias.startsWith(LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT) ||
266 alias.startsWith(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT)) {
267 continue;
268 }
269 // Do not show synthetic password keys in user credential
270 if (alias.startsWith(LockPatternUtils.SYNTHETIC_PASSWORD_KEY_PREFIX)) {
271 continue;
272 }
Ricky Wai95792742016-05-24 19:28:53 +0100273 }
Robin Leee68d9572016-07-19 16:10:39 +0100274 Credential c = aliasMap.get(alias);
Robin Leebaefdcf2015-08-26 10:57:44 +0100275 if (c == null) {
Robin Leee68d9572016-07-19 16:10:39 +0100276 c = new Credential(alias, uid);
277 aliasMap.put(alias, c);
Robin Leebaefdcf2015-08-26 10:57:44 +0100278 }
279 c.storedTypes.add(type);
280 }
281 }
Robin Leee68d9572016-07-19 16:10:39 +0100282 return aliasMap;
Robin Leebaefdcf2015-08-26 10:57:44 +0100283 }
284
285 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100286 protected void onPostExecute(List<Credential> credentials) {
Robin Leeccaf9c92017-03-24 14:50:05 +0000287 if (!isAdded()) {
288 return;
289 }
290
291 if (credentials == null || credentials.size() == 0) {
292 // Create a "no credentials installed" message for the empty case.
293 TextView emptyTextView = (TextView) getActivity().findViewById(android.R.id.empty);
294 emptyTextView.setText(R.string.user_credential_none_installed);
295 setEmptyView(emptyTextView);
296 } else {
297 setEmptyView(null);
298 }
299
300 getListView().setAdapter(
301 new CredentialAdapter(credentials, UserCredentialsSettings.this));
Robin Leebaefdcf2015-08-26 10:57:44 +0100302 }
303 }
304
305 /**
306 * Helper class to display {@link Credential}s in a list.
307 */
Robin Leeccaf9c92017-03-24 14:50:05 +0000308 private static class CredentialAdapter extends RecyclerView.Adapter<ViewHolder> {
Robin Leee68d9572016-07-19 16:10:39 +0100309 private static final int LAYOUT_RESOURCE = R.layout.user_credential_preference;
310
Robin Leeccaf9c92017-03-24 14:50:05 +0000311 private final List<Credential> mItems;
312 private final View.OnClickListener mListener;
313
314 public CredentialAdapter(List<Credential> items, @Nullable View.OnClickListener listener) {
315 mItems = items;
316 mListener = listener;
Robin Leebaefdcf2015-08-26 10:57:44 +0100317 }
318
319 @Override
Robin Leeccaf9c92017-03-24 14:50:05 +0000320 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
321 final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
322 return new ViewHolder(inflater.inflate(LAYOUT_RESOURCE, parent, false));
323 }
324
325 @Override
326 public void onBindViewHolder(ViewHolder h, int position) {
327 getCredentialView(mItems.get(position), LAYOUT_RESOURCE, h.itemView, null, false);
328 h.itemView.setTag(mItems.get(position));
329 h.itemView.setOnClickListener(mListener);
330 }
331
332 @Override
333 public int getItemCount() {
334 return mItems.size();
335 }
336 }
337
338 private static class ViewHolder extends RecyclerView.ViewHolder {
339 public ViewHolder(View item) {
340 super(item);
Robin Leebaefdcf2015-08-26 10:57:44 +0100341 }
342 }
343
Robin Leee68d9572016-07-19 16:10:39 +0100344 /**
345 * Mapping from View IDs in {@link R} to the types of credentials they describe.
346 */
347 private static final SparseArray<Credential.Type> credentialViewTypes = new SparseArray<>();
348 static {
349 credentialViewTypes.put(R.id.contents_userkey, Credential.Type.USER_PRIVATE_KEY);
350 credentialViewTypes.put(R.id.contents_usercrt, Credential.Type.USER_CERTIFICATE);
351 credentialViewTypes.put(R.id.contents_cacrt, Credential.Type.CA_CERTIFICATE);
352 }
353
354 protected static View getCredentialView(Credential item, @LayoutRes int layoutResource,
355 @Nullable View view, ViewGroup parent, boolean expanded) {
356 if (view == null) {
357 view = LayoutInflater.from(parent.getContext()).inflate(layoutResource, parent, false);
358 }
359
360 ((TextView) view.findViewById(R.id.alias)).setText(item.alias);
361 ((TextView) view.findViewById(R.id.purpose)).setText(item.isSystem()
362 ? R.string.credential_for_vpn_and_apps
363 : R.string.credential_for_wifi);
364
365 view.findViewById(R.id.contents).setVisibility(expanded ? View.VISIBLE : View.GONE);
366 if (expanded) {
367 for (int i = 0; i < credentialViewTypes.size(); i++) {
368 final View detail = view.findViewById(credentialViewTypes.keyAt(i));
369 detail.setVisibility(item.storedTypes.contains(credentialViewTypes.valueAt(i))
370 ? View.VISIBLE : View.GONE);
371 }
372 }
373 return view;
374 }
375
376 static class AliasEntry {
377 public String alias;
378 public int uid;
379 }
380
Robin Leee2680422016-01-25 12:24:27 +0000381 static class Credential implements Parcelable {
382 static enum Type {
Robin Leebaefdcf2015-08-26 10:57:44 +0100383 CA_CERTIFICATE (Credentials.CA_CERTIFICATE),
384 USER_CERTIFICATE (Credentials.USER_CERTIFICATE),
385 USER_PRIVATE_KEY (Credentials.USER_PRIVATE_KEY),
386 USER_SECRET_KEY (Credentials.USER_SECRET_KEY);
387
388 final String prefix;
389
390 Type(String prefix) {
391 this.prefix = prefix;
392 }
393 }
394
395 /**
396 * Main part of the credential's alias. To fetch an item from KeyStore, prepend one of the
397 * prefixes from {@link CredentialItem.storedTypes}.
398 */
399 final String alias;
400
401 /**
Robin Leee68d9572016-07-19 16:10:39 +0100402 * UID under which this credential is stored. Typically {@link Process#SYSTEM_UID} but can
403 * also be {@link Process#WIFI_UID} for credentials installed as wifi certificates.
404 */
405 final int uid;
406
407 /**
Robin Leebaefdcf2015-08-26 10:57:44 +0100408 * Should contain some non-empty subset of:
409 * <ul>
410 * <li>{@link Credentials.CA_CERTIFICATE}</li>
411 * <li>{@link Credentials.USER_CERTIFICATE}</li>
412 * <li>{@link Credentials.USER_PRIVATE_KEY}</li>
413 * <li>{@link Credentials.USER_SECRET_KEY}</li>
414 * </ul>
415 */
Robin Lee04046a12016-01-19 11:42:57 +0000416 final EnumSet<Type> storedTypes = EnumSet.noneOf(Type.class);
Robin Leebaefdcf2015-08-26 10:57:44 +0100417
Robin Leee68d9572016-07-19 16:10:39 +0100418 Credential(final String alias, final int uid) {
Robin Leebaefdcf2015-08-26 10:57:44 +0100419 this.alias = alias;
Robin Leee68d9572016-07-19 16:10:39 +0100420 this.uid = uid;
Robin Leebaefdcf2015-08-26 10:57:44 +0100421 }
Robin Lee04046a12016-01-19 11:42:57 +0000422
423 Credential(Parcel in) {
Robin Leee68d9572016-07-19 16:10:39 +0100424 this(in.readString(), in.readInt());
Robin Lee04046a12016-01-19 11:42:57 +0000425
426 long typeBits = in.readLong();
427 for (Type i : Type.values()) {
428 if ((typeBits & (1L << i.ordinal())) != 0L) {
429 storedTypes.add(i);
430 }
431 }
432 }
433
434 public void writeToParcel(Parcel out, int flags) {
435 out.writeString(alias);
Robin Leee68d9572016-07-19 16:10:39 +0100436 out.writeInt(uid);
Robin Lee04046a12016-01-19 11:42:57 +0000437
438 long typeBits = 0;
439 for (Type i : storedTypes) {
440 typeBits |= 1L << i.ordinal();
441 }
442 out.writeLong(typeBits);
443 }
444
445 public int describeContents() {
446 return 0;
447 }
448
449 public static final Parcelable.Creator<Credential> CREATOR
450 = new Parcelable.Creator<Credential>() {
451 public Credential createFromParcel(Parcel in) {
452 return new Credential(in);
453 }
454
455 public Credential[] newArray(int size) {
456 return new Credential[size];
457 }
458 };
Robin Leee68d9572016-07-19 16:10:39 +0100459
460 public boolean isSystem() {
461 return UserHandle.getAppId(uid) == Process.SYSTEM_UID;
462 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100463 }
464}