blob: 54e12a85faa94abcb80da6bb3577de76e6758b6b [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);
Doris Ling4a012832017-11-13 17:58:13 -080090 getActivity().setTitle(R.string.user_credentials);
Doris Ling03a3b512017-10-18 14:25:01 -070091 }
92
Robin Lee11fd5502016-05-16 15:42:34 +010093 protected void announceRemoval(String alias) {
Robin Leeccaf9c92017-03-24 14:50:05 +000094 if (!isAdded()) {
95 return;
Robin Lee11fd5502016-05-16 15:42:34 +010096 }
Robin Leeccaf9c92017-03-24 14:50:05 +000097 getListView().announceForAccessibility(getString(R.string.user_credential_removed, alias));
Robin Lee11fd5502016-05-16 15:42:34 +010098 }
99
Robin Lee04046a12016-01-19 11:42:57 +0000100 protected void refreshItems() {
101 if (isAdded()) {
102 new AliasLoader().execute();
103 }
104 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100105
Fan Zhang1e516282016-09-16 12:45:07 -0700106 public static class CredentialDialogFragment extends InstrumentedDialogFragment {
Robin Lee04046a12016-01-19 11:42:57 +0000107 private static final String TAG = "CredentialDialogFragment";
108 private static final String ARG_CREDENTIAL = "credential";
109
110 public static void show(Fragment target, Credential item) {
111 final Bundle args = new Bundle();
112 args.putParcelable(ARG_CREDENTIAL, item);
113
Robin Leef8e2dbf2016-04-07 13:17:24 +0100114 if (target.getFragmentManager().findFragmentByTag(TAG) == null) {
115 final DialogFragment frag = new CredentialDialogFragment();
116 frag.setTargetFragment(target, /* requestCode */ -1);
117 frag.setArguments(args);
118 frag.show(target.getFragmentManager(), TAG);
119 }
Robin Lee04046a12016-01-19 11:42:57 +0000120 }
121
122 @Override
123 public Dialog onCreateDialog(Bundle savedInstanceState) {
124 final Credential item = (Credential) getArguments().getParcelable(ARG_CREDENTIAL);
Robin Leee68d9572016-07-19 16:10:39 +0100125
Robin Lee04046a12016-01-19 11:42:57 +0000126 View root = getActivity().getLayoutInflater()
127 .inflate(R.layout.user_credential_dialog, null);
128 ViewGroup infoContainer = (ViewGroup) root.findViewById(R.id.credential_container);
Robin Leee68d9572016-07-19 16:10:39 +0100129 View contentView = getCredentialView(item, R.layout.user_credential, null,
130 infoContainer, /* expanded */ true);
131 infoContainer.addView(contentView);
Robin Leec421db72016-03-11 16:22:23 +0000132
133 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
Robin Lee04046a12016-01-19 11:42:57 +0000134 .setView(root)
135 .setTitle(R.string.user_credential_title)
Robin Leec421db72016-03-11 16:22:23 +0000136 .setPositiveButton(R.string.done, null);
137
138 final String restriction = UserManager.DISALLOW_CONFIG_CREDENTIALS;
139 final int myUserId = UserHandle.myUserId();
140 if (!RestrictedLockUtils.hasBaseUserRestriction(getContext(), restriction, myUserId)) {
141 DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
142 @Override public void onClick(DialogInterface dialog, int id) {
143 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(
144 getContext(), restriction, myUserId);
145 if (admin != null) {
146 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
147 admin);
148 } else {
Robin Leee68d9572016-07-19 16:10:39 +0100149 new RemoveCredentialsTask(getContext(), getTargetFragment())
150 .execute(item);
Robin Leec421db72016-03-11 16:22:23 +0000151 }
152 dialog.dismiss();
153 }
154 };
Robin Leee68d9572016-07-19 16:10:39 +0100155 if (item.isSystem()) {
156 // TODO: a safe means of clearing wifi certificates. Configs refer to aliases
157 // directly so deleting certs will break dependent access points.
158 builder.setNegativeButton(R.string.trusted_credentials_remove_label, listener);
159 }
Robin Leec421db72016-03-11 16:22:23 +0000160 }
161 return builder.create();
Robin Lee04046a12016-01-19 11:42:57 +0000162 }
163
Fan Zhang1e516282016-09-16 12:45:07 -0700164 @Override
165 public int getMetricsCategory() {
166 return MetricsEvent.DIALOG_USER_CREDENTIAL;
167 }
168
Robin Leee68d9572016-07-19 16:10:39 +0100169 /**
170 * Deletes all certificates and keys under a given alias.
171 *
172 * If the {@link Credential} is for a system alias, all active grants to the alias will be
173 * removed using {@link KeyChain}.
174 */
175 private class RemoveCredentialsTask extends AsyncTask<Credential, Void, Credential[]> {
176 private Context context;
Robin Leeda7bc512016-02-24 17:39:32 +0000177 private Fragment targetFragment;
178
Robin Leee68d9572016-07-19 16:10:39 +0100179 public RemoveCredentialsTask(Context context, Fragment targetFragment) {
180 this.context = context;
Robin Leeda7bc512016-02-24 17:39:32 +0000181 this.targetFragment = targetFragment;
Robin Lee04046a12016-01-19 11:42:57 +0000182 }
Robin Leeda7bc512016-02-24 17:39:32 +0000183
184 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100185 protected Credential[] doInBackground(Credential... credentials) {
186 for (final Credential credential : credentials) {
187 if (credential.isSystem()) {
188 removeGrantsAndDelete(credential);
189 continue;
Robin Leeda7bc512016-02-24 17:39:32 +0000190 }
Robin Leee68d9572016-07-19 16:10:39 +0100191 throw new UnsupportedOperationException(
192 "Not implemented for wifi certificates. This should not be reachable.");
Robin Leeda7bc512016-02-24 17:39:32 +0000193 }
Robin Leee68d9572016-07-19 16:10:39 +0100194 return credentials;
195 }
196
197 private void removeGrantsAndDelete(final Credential credential) {
198 final KeyChainConnection conn;
199 try {
200 conn = KeyChain.bind(getContext());
201 } catch (InterruptedException e) {
202 Log.w(TAG, "Connecting to KeyChain", e);
203 return;
204 }
205
206 try {
207 IKeyChainService keyChain = conn.getService();
208 keyChain.removeKeyPair(credential.alias);
209 } catch (RemoteException e) {
210 Log.w(TAG, "Removing credentials", e);
211 } finally {
212 conn.close();
213 }
Robin Leeda7bc512016-02-24 17:39:32 +0000214 }
215
216 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100217 protected void onPostExecute(Credential... credentials) {
Robin Lee11fd5502016-05-16 15:42:34 +0100218 if (targetFragment instanceof UserCredentialsSettings && targetFragment.isAdded()) {
219 final UserCredentialsSettings target = (UserCredentialsSettings) targetFragment;
Robin Leee68d9572016-07-19 16:10:39 +0100220 for (final Credential credential : credentials) {
221 target.announceRemoval(credential.alias);
Robin Lee11fd5502016-05-16 15:42:34 +0100222 }
223 target.refreshItems();
Robin Leeda7bc512016-02-24 17:39:32 +0000224 }
225 }
Robin Lee04046a12016-01-19 11:42:57 +0000226 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100227 }
228
229 /**
230 * Opens a background connection to KeyStore to list user credentials.
231 * The credentials are stored in a {@link CredentialAdapter} attached to the main
232 * {@link ListView} in the fragment.
233 */
Robin Leee68d9572016-07-19 16:10:39 +0100234 private class AliasLoader extends AsyncTask<Void, Void, List<Credential>> {
235 /**
236 * @return a list of credentials ordered:
237 * <ol>
238 * <li>first by purpose;</li>
239 * <li>then by alias.</li>
240 * </ol>
241 */
Robin Leebaefdcf2015-08-26 10:57:44 +0100242 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100243 protected List<Credential> doInBackground(Void... params) {
244 final KeyStore keyStore = KeyStore.getInstance();
245
246 // Certificates can be installed into SYSTEM_UID or WIFI_UID through CertInstaller.
247 final int myUserId = UserHandle.myUserId();
248 final int systemUid = UserHandle.getUid(myUserId, Process.SYSTEM_UID);
249 final int wifiUid = UserHandle.getUid(myUserId, Process.WIFI_UID);
250
251 List<Credential> credentials = new ArrayList<>();
252 credentials.addAll(getCredentialsForUid(keyStore, systemUid).values());
253 credentials.addAll(getCredentialsForUid(keyStore, wifiUid).values());
254 return credentials;
255 }
256
257 private SortedMap<String, Credential> getCredentialsForUid(KeyStore keyStore, int uid) {
258 final SortedMap<String, Credential> aliasMap = new TreeMap<>();
Robin Leebaefdcf2015-08-26 10:57:44 +0100259 for (final Credential.Type type : Credential.Type.values()) {
Robin Leee68d9572016-07-19 16:10:39 +0100260 for (final String alias : keyStore.list(type.prefix, uid)) {
Rubin Xu52221d82017-02-09 11:09:11 +0000261 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) {
262 // Do not show work profile keys in user credentials
263 if (alias.startsWith(LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT) ||
264 alias.startsWith(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT)) {
265 continue;
266 }
267 // Do not show synthetic password keys in user credential
268 if (alias.startsWith(LockPatternUtils.SYNTHETIC_PASSWORD_KEY_PREFIX)) {
269 continue;
270 }
Ricky Wai95792742016-05-24 19:28:53 +0100271 }
Robin Leee68d9572016-07-19 16:10:39 +0100272 Credential c = aliasMap.get(alias);
Robin Leebaefdcf2015-08-26 10:57:44 +0100273 if (c == null) {
Robin Leee68d9572016-07-19 16:10:39 +0100274 c = new Credential(alias, uid);
275 aliasMap.put(alias, c);
Robin Leebaefdcf2015-08-26 10:57:44 +0100276 }
277 c.storedTypes.add(type);
278 }
279 }
Robin Leee68d9572016-07-19 16:10:39 +0100280 return aliasMap;
Robin Leebaefdcf2015-08-26 10:57:44 +0100281 }
282
283 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100284 protected void onPostExecute(List<Credential> credentials) {
Robin Leeccaf9c92017-03-24 14:50:05 +0000285 if (!isAdded()) {
286 return;
287 }
288
289 if (credentials == null || credentials.size() == 0) {
290 // Create a "no credentials installed" message for the empty case.
291 TextView emptyTextView = (TextView) getActivity().findViewById(android.R.id.empty);
292 emptyTextView.setText(R.string.user_credential_none_installed);
293 setEmptyView(emptyTextView);
294 } else {
295 setEmptyView(null);
296 }
297
298 getListView().setAdapter(
299 new CredentialAdapter(credentials, UserCredentialsSettings.this));
Robin Leebaefdcf2015-08-26 10:57:44 +0100300 }
301 }
302
303 /**
304 * Helper class to display {@link Credential}s in a list.
305 */
Robin Leeccaf9c92017-03-24 14:50:05 +0000306 private static class CredentialAdapter extends RecyclerView.Adapter<ViewHolder> {
Robin Leee68d9572016-07-19 16:10:39 +0100307 private static final int LAYOUT_RESOURCE = R.layout.user_credential_preference;
308
Robin Leeccaf9c92017-03-24 14:50:05 +0000309 private final List<Credential> mItems;
310 private final View.OnClickListener mListener;
311
312 public CredentialAdapter(List<Credential> items, @Nullable View.OnClickListener listener) {
313 mItems = items;
314 mListener = listener;
Robin Leebaefdcf2015-08-26 10:57:44 +0100315 }
316
317 @Override
Robin Leeccaf9c92017-03-24 14:50:05 +0000318 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
319 final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
320 return new ViewHolder(inflater.inflate(LAYOUT_RESOURCE, parent, false));
321 }
322
323 @Override
324 public void onBindViewHolder(ViewHolder h, int position) {
325 getCredentialView(mItems.get(position), LAYOUT_RESOURCE, h.itemView, null, false);
326 h.itemView.setTag(mItems.get(position));
327 h.itemView.setOnClickListener(mListener);
328 }
329
330 @Override
331 public int getItemCount() {
332 return mItems.size();
333 }
334 }
335
336 private static class ViewHolder extends RecyclerView.ViewHolder {
337 public ViewHolder(View item) {
338 super(item);
Robin Leebaefdcf2015-08-26 10:57:44 +0100339 }
340 }
341
Robin Leee68d9572016-07-19 16:10:39 +0100342 /**
343 * Mapping from View IDs in {@link R} to the types of credentials they describe.
344 */
345 private static final SparseArray<Credential.Type> credentialViewTypes = new SparseArray<>();
346 static {
347 credentialViewTypes.put(R.id.contents_userkey, Credential.Type.USER_PRIVATE_KEY);
348 credentialViewTypes.put(R.id.contents_usercrt, Credential.Type.USER_CERTIFICATE);
349 credentialViewTypes.put(R.id.contents_cacrt, Credential.Type.CA_CERTIFICATE);
350 }
351
352 protected static View getCredentialView(Credential item, @LayoutRes int layoutResource,
353 @Nullable View view, ViewGroup parent, boolean expanded) {
354 if (view == null) {
355 view = LayoutInflater.from(parent.getContext()).inflate(layoutResource, parent, false);
356 }
357
358 ((TextView) view.findViewById(R.id.alias)).setText(item.alias);
359 ((TextView) view.findViewById(R.id.purpose)).setText(item.isSystem()
360 ? R.string.credential_for_vpn_and_apps
361 : R.string.credential_for_wifi);
362
363 view.findViewById(R.id.contents).setVisibility(expanded ? View.VISIBLE : View.GONE);
364 if (expanded) {
365 for (int i = 0; i < credentialViewTypes.size(); i++) {
366 final View detail = view.findViewById(credentialViewTypes.keyAt(i));
367 detail.setVisibility(item.storedTypes.contains(credentialViewTypes.valueAt(i))
368 ? View.VISIBLE : View.GONE);
369 }
370 }
371 return view;
372 }
373
374 static class AliasEntry {
375 public String alias;
376 public int uid;
377 }
378
Robin Leee2680422016-01-25 12:24:27 +0000379 static class Credential implements Parcelable {
380 static enum Type {
Robin Leebaefdcf2015-08-26 10:57:44 +0100381 CA_CERTIFICATE (Credentials.CA_CERTIFICATE),
382 USER_CERTIFICATE (Credentials.USER_CERTIFICATE),
383 USER_PRIVATE_KEY (Credentials.USER_PRIVATE_KEY),
384 USER_SECRET_KEY (Credentials.USER_SECRET_KEY);
385
386 final String prefix;
387
388 Type(String prefix) {
389 this.prefix = prefix;
390 }
391 }
392
393 /**
394 * Main part of the credential's alias. To fetch an item from KeyStore, prepend one of the
395 * prefixes from {@link CredentialItem.storedTypes}.
396 */
397 final String alias;
398
399 /**
Robin Leee68d9572016-07-19 16:10:39 +0100400 * UID under which this credential is stored. Typically {@link Process#SYSTEM_UID} but can
401 * also be {@link Process#WIFI_UID} for credentials installed as wifi certificates.
402 */
403 final int uid;
404
405 /**
Robin Leebaefdcf2015-08-26 10:57:44 +0100406 * Should contain some non-empty subset of:
407 * <ul>
408 * <li>{@link Credentials.CA_CERTIFICATE}</li>
409 * <li>{@link Credentials.USER_CERTIFICATE}</li>
410 * <li>{@link Credentials.USER_PRIVATE_KEY}</li>
411 * <li>{@link Credentials.USER_SECRET_KEY}</li>
412 * </ul>
413 */
Robin Lee04046a12016-01-19 11:42:57 +0000414 final EnumSet<Type> storedTypes = EnumSet.noneOf(Type.class);
Robin Leebaefdcf2015-08-26 10:57:44 +0100415
Robin Leee68d9572016-07-19 16:10:39 +0100416 Credential(final String alias, final int uid) {
Robin Leebaefdcf2015-08-26 10:57:44 +0100417 this.alias = alias;
Robin Leee68d9572016-07-19 16:10:39 +0100418 this.uid = uid;
Robin Leebaefdcf2015-08-26 10:57:44 +0100419 }
Robin Lee04046a12016-01-19 11:42:57 +0000420
421 Credential(Parcel in) {
Robin Leee68d9572016-07-19 16:10:39 +0100422 this(in.readString(), in.readInt());
Robin Lee04046a12016-01-19 11:42:57 +0000423
424 long typeBits = in.readLong();
425 for (Type i : Type.values()) {
426 if ((typeBits & (1L << i.ordinal())) != 0L) {
427 storedTypes.add(i);
428 }
429 }
430 }
431
432 public void writeToParcel(Parcel out, int flags) {
433 out.writeString(alias);
Robin Leee68d9572016-07-19 16:10:39 +0100434 out.writeInt(uid);
Robin Lee04046a12016-01-19 11:42:57 +0000435
436 long typeBits = 0;
437 for (Type i : storedTypes) {
438 typeBits |= 1L << i.ordinal();
439 }
440 out.writeLong(typeBits);
441 }
442
443 public int describeContents() {
444 return 0;
445 }
446
447 public static final Parcelable.Creator<Credential> CREATOR
448 = new Parcelable.Creator<Credential>() {
449 public Credential createFromParcel(Parcel in) {
450 return new Credential(in);
451 }
452
453 public Credential[] newArray(int size) {
454 return new Credential[size];
455 }
456 };
Robin Leee68d9572016-07-19 16:10:39 +0100457
458 public boolean isSystem() {
459 return UserHandle.getAppId(uid) == Process.SYSTEM_UID;
460 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100461 }
462}