blob: 6cf1ae0a54c687c4e38f4dc2a564add930d5a736 [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
Robin Lee11fd5502016-05-16 15:42:34 +010087 protected void announceRemoval(String alias) {
Robin Leeccaf9c92017-03-24 14:50:05 +000088 if (!isAdded()) {
89 return;
Robin Lee11fd5502016-05-16 15:42:34 +010090 }
Robin Leeccaf9c92017-03-24 14:50:05 +000091 getListView().announceForAccessibility(getString(R.string.user_credential_removed, alias));
Robin Lee11fd5502016-05-16 15:42:34 +010092 }
93
Robin Lee04046a12016-01-19 11:42:57 +000094 protected void refreshItems() {
95 if (isAdded()) {
96 new AliasLoader().execute();
97 }
98 }
Robin Leebaefdcf2015-08-26 10:57:44 +010099
Fan Zhang1e516282016-09-16 12:45:07 -0700100 public static class CredentialDialogFragment extends InstrumentedDialogFragment {
Robin Lee04046a12016-01-19 11:42:57 +0000101 private static final String TAG = "CredentialDialogFragment";
102 private static final String ARG_CREDENTIAL = "credential";
103
104 public static void show(Fragment target, Credential item) {
105 final Bundle args = new Bundle();
106 args.putParcelable(ARG_CREDENTIAL, item);
107
Robin Leef8e2dbf2016-04-07 13:17:24 +0100108 if (target.getFragmentManager().findFragmentByTag(TAG) == null) {
109 final DialogFragment frag = new CredentialDialogFragment();
110 frag.setTargetFragment(target, /* requestCode */ -1);
111 frag.setArguments(args);
112 frag.show(target.getFragmentManager(), TAG);
113 }
Robin Lee04046a12016-01-19 11:42:57 +0000114 }
115
116 @Override
117 public Dialog onCreateDialog(Bundle savedInstanceState) {
118 final Credential item = (Credential) getArguments().getParcelable(ARG_CREDENTIAL);
Robin Leee68d9572016-07-19 16:10:39 +0100119
Robin Lee04046a12016-01-19 11:42:57 +0000120 View root = getActivity().getLayoutInflater()
121 .inflate(R.layout.user_credential_dialog, null);
122 ViewGroup infoContainer = (ViewGroup) root.findViewById(R.id.credential_container);
Robin Leee68d9572016-07-19 16:10:39 +0100123 View contentView = getCredentialView(item, R.layout.user_credential, null,
124 infoContainer, /* expanded */ true);
125 infoContainer.addView(contentView);
Robin Leec421db72016-03-11 16:22:23 +0000126
127 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
Robin Lee04046a12016-01-19 11:42:57 +0000128 .setView(root)
129 .setTitle(R.string.user_credential_title)
Robin Leec421db72016-03-11 16:22:23 +0000130 .setPositiveButton(R.string.done, null);
131
132 final String restriction = UserManager.DISALLOW_CONFIG_CREDENTIALS;
133 final int myUserId = UserHandle.myUserId();
134 if (!RestrictedLockUtils.hasBaseUserRestriction(getContext(), restriction, myUserId)) {
135 DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
136 @Override public void onClick(DialogInterface dialog, int id) {
137 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(
138 getContext(), restriction, myUserId);
139 if (admin != null) {
140 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
141 admin);
142 } else {
Robin Leee68d9572016-07-19 16:10:39 +0100143 new RemoveCredentialsTask(getContext(), getTargetFragment())
144 .execute(item);
Robin Leec421db72016-03-11 16:22:23 +0000145 }
146 dialog.dismiss();
147 }
148 };
Robin Leee68d9572016-07-19 16:10:39 +0100149 if (item.isSystem()) {
150 // TODO: a safe means of clearing wifi certificates. Configs refer to aliases
151 // directly so deleting certs will break dependent access points.
152 builder.setNegativeButton(R.string.trusted_credentials_remove_label, listener);
153 }
Robin Leec421db72016-03-11 16:22:23 +0000154 }
155 return builder.create();
Robin Lee04046a12016-01-19 11:42:57 +0000156 }
157
Fan Zhang1e516282016-09-16 12:45:07 -0700158 @Override
159 public int getMetricsCategory() {
160 return MetricsEvent.DIALOG_USER_CREDENTIAL;
161 }
162
Robin Leee68d9572016-07-19 16:10:39 +0100163 /**
164 * Deletes all certificates and keys under a given alias.
165 *
166 * If the {@link Credential} is for a system alias, all active grants to the alias will be
167 * removed using {@link KeyChain}.
168 */
169 private class RemoveCredentialsTask extends AsyncTask<Credential, Void, Credential[]> {
170 private Context context;
Robin Leeda7bc512016-02-24 17:39:32 +0000171 private Fragment targetFragment;
172
Robin Leee68d9572016-07-19 16:10:39 +0100173 public RemoveCredentialsTask(Context context, Fragment targetFragment) {
174 this.context = context;
Robin Leeda7bc512016-02-24 17:39:32 +0000175 this.targetFragment = targetFragment;
Robin Lee04046a12016-01-19 11:42:57 +0000176 }
Robin Leeda7bc512016-02-24 17:39:32 +0000177
178 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100179 protected Credential[] doInBackground(Credential... credentials) {
180 for (final Credential credential : credentials) {
181 if (credential.isSystem()) {
182 removeGrantsAndDelete(credential);
183 continue;
Robin Leeda7bc512016-02-24 17:39:32 +0000184 }
Robin Leee68d9572016-07-19 16:10:39 +0100185 throw new UnsupportedOperationException(
186 "Not implemented for wifi certificates. This should not be reachable.");
Robin Leeda7bc512016-02-24 17:39:32 +0000187 }
Robin Leee68d9572016-07-19 16:10:39 +0100188 return credentials;
189 }
190
191 private void removeGrantsAndDelete(final Credential credential) {
192 final KeyChainConnection conn;
193 try {
194 conn = KeyChain.bind(getContext());
195 } catch (InterruptedException e) {
196 Log.w(TAG, "Connecting to KeyChain", e);
197 return;
198 }
199
200 try {
201 IKeyChainService keyChain = conn.getService();
202 keyChain.removeKeyPair(credential.alias);
203 } catch (RemoteException e) {
204 Log.w(TAG, "Removing credentials", e);
205 } finally {
206 conn.close();
207 }
Robin Leeda7bc512016-02-24 17:39:32 +0000208 }
209
210 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100211 protected void onPostExecute(Credential... credentials) {
Robin Lee11fd5502016-05-16 15:42:34 +0100212 if (targetFragment instanceof UserCredentialsSettings && targetFragment.isAdded()) {
213 final UserCredentialsSettings target = (UserCredentialsSettings) targetFragment;
Robin Leee68d9572016-07-19 16:10:39 +0100214 for (final Credential credential : credentials) {
215 target.announceRemoval(credential.alias);
Robin Lee11fd5502016-05-16 15:42:34 +0100216 }
217 target.refreshItems();
Robin Leeda7bc512016-02-24 17:39:32 +0000218 }
219 }
Robin Lee04046a12016-01-19 11:42:57 +0000220 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100221 }
222
223 /**
224 * Opens a background connection to KeyStore to list user credentials.
225 * The credentials are stored in a {@link CredentialAdapter} attached to the main
226 * {@link ListView} in the fragment.
227 */
Robin Leee68d9572016-07-19 16:10:39 +0100228 private class AliasLoader extends AsyncTask<Void, Void, List<Credential>> {
229 /**
230 * @return a list of credentials ordered:
231 * <ol>
232 * <li>first by purpose;</li>
233 * <li>then by alias.</li>
234 * </ol>
235 */
Robin Leebaefdcf2015-08-26 10:57:44 +0100236 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100237 protected List<Credential> doInBackground(Void... params) {
238 final KeyStore keyStore = KeyStore.getInstance();
239
240 // Certificates can be installed into SYSTEM_UID or WIFI_UID through CertInstaller.
241 final int myUserId = UserHandle.myUserId();
242 final int systemUid = UserHandle.getUid(myUserId, Process.SYSTEM_UID);
243 final int wifiUid = UserHandle.getUid(myUserId, Process.WIFI_UID);
244
245 List<Credential> credentials = new ArrayList<>();
246 credentials.addAll(getCredentialsForUid(keyStore, systemUid).values());
247 credentials.addAll(getCredentialsForUid(keyStore, wifiUid).values());
248 return credentials;
249 }
250
251 private SortedMap<String, Credential> getCredentialsForUid(KeyStore keyStore, int uid) {
252 final SortedMap<String, Credential> aliasMap = new TreeMap<>();
Robin Leebaefdcf2015-08-26 10:57:44 +0100253 for (final Credential.Type type : Credential.Type.values()) {
Robin Leee68d9572016-07-19 16:10:39 +0100254 for (final String alias : keyStore.list(type.prefix, uid)) {
Rubin Xu52221d82017-02-09 11:09:11 +0000255 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) {
256 // Do not show work profile keys in user credentials
257 if (alias.startsWith(LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT) ||
258 alias.startsWith(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT)) {
259 continue;
260 }
261 // Do not show synthetic password keys in user credential
262 if (alias.startsWith(LockPatternUtils.SYNTHETIC_PASSWORD_KEY_PREFIX)) {
263 continue;
264 }
Ricky Wai95792742016-05-24 19:28:53 +0100265 }
Robin Leee68d9572016-07-19 16:10:39 +0100266 Credential c = aliasMap.get(alias);
Robin Leebaefdcf2015-08-26 10:57:44 +0100267 if (c == null) {
Robin Leee68d9572016-07-19 16:10:39 +0100268 c = new Credential(alias, uid);
269 aliasMap.put(alias, c);
Robin Leebaefdcf2015-08-26 10:57:44 +0100270 }
271 c.storedTypes.add(type);
272 }
273 }
Robin Leee68d9572016-07-19 16:10:39 +0100274 return aliasMap;
Robin Leebaefdcf2015-08-26 10:57:44 +0100275 }
276
277 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100278 protected void onPostExecute(List<Credential> credentials) {
Robin Leeccaf9c92017-03-24 14:50:05 +0000279 if (!isAdded()) {
280 return;
281 }
282
283 if (credentials == null || credentials.size() == 0) {
284 // Create a "no credentials installed" message for the empty case.
285 TextView emptyTextView = (TextView) getActivity().findViewById(android.R.id.empty);
286 emptyTextView.setText(R.string.user_credential_none_installed);
287 setEmptyView(emptyTextView);
288 } else {
289 setEmptyView(null);
290 }
291
292 getListView().setAdapter(
293 new CredentialAdapter(credentials, UserCredentialsSettings.this));
Robin Leebaefdcf2015-08-26 10:57:44 +0100294 }
295 }
296
297 /**
298 * Helper class to display {@link Credential}s in a list.
299 */
Robin Leeccaf9c92017-03-24 14:50:05 +0000300 private static class CredentialAdapter extends RecyclerView.Adapter<ViewHolder> {
Robin Leee68d9572016-07-19 16:10:39 +0100301 private static final int LAYOUT_RESOURCE = R.layout.user_credential_preference;
302
Robin Leeccaf9c92017-03-24 14:50:05 +0000303 private final List<Credential> mItems;
304 private final View.OnClickListener mListener;
305
306 public CredentialAdapter(List<Credential> items, @Nullable View.OnClickListener listener) {
307 mItems = items;
308 mListener = listener;
Robin Leebaefdcf2015-08-26 10:57:44 +0100309 }
310
311 @Override
Robin Leeccaf9c92017-03-24 14:50:05 +0000312 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
313 final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
314 return new ViewHolder(inflater.inflate(LAYOUT_RESOURCE, parent, false));
315 }
316
317 @Override
318 public void onBindViewHolder(ViewHolder h, int position) {
319 getCredentialView(mItems.get(position), LAYOUT_RESOURCE, h.itemView, null, false);
320 h.itemView.setTag(mItems.get(position));
321 h.itemView.setOnClickListener(mListener);
322 }
323
324 @Override
325 public int getItemCount() {
326 return mItems.size();
327 }
328 }
329
330 private static class ViewHolder extends RecyclerView.ViewHolder {
331 public ViewHolder(View item) {
332 super(item);
Robin Leebaefdcf2015-08-26 10:57:44 +0100333 }
334 }
335
Robin Leee68d9572016-07-19 16:10:39 +0100336 /**
337 * Mapping from View IDs in {@link R} to the types of credentials they describe.
338 */
339 private static final SparseArray<Credential.Type> credentialViewTypes = new SparseArray<>();
340 static {
341 credentialViewTypes.put(R.id.contents_userkey, Credential.Type.USER_PRIVATE_KEY);
342 credentialViewTypes.put(R.id.contents_usercrt, Credential.Type.USER_CERTIFICATE);
343 credentialViewTypes.put(R.id.contents_cacrt, Credential.Type.CA_CERTIFICATE);
344 }
345
346 protected static View getCredentialView(Credential item, @LayoutRes int layoutResource,
347 @Nullable View view, ViewGroup parent, boolean expanded) {
348 if (view == null) {
349 view = LayoutInflater.from(parent.getContext()).inflate(layoutResource, parent, false);
350 }
351
352 ((TextView) view.findViewById(R.id.alias)).setText(item.alias);
353 ((TextView) view.findViewById(R.id.purpose)).setText(item.isSystem()
354 ? R.string.credential_for_vpn_and_apps
355 : R.string.credential_for_wifi);
356
357 view.findViewById(R.id.contents).setVisibility(expanded ? View.VISIBLE : View.GONE);
358 if (expanded) {
359 for (int i = 0; i < credentialViewTypes.size(); i++) {
360 final View detail = view.findViewById(credentialViewTypes.keyAt(i));
361 detail.setVisibility(item.storedTypes.contains(credentialViewTypes.valueAt(i))
362 ? View.VISIBLE : View.GONE);
363 }
364 }
365 return view;
366 }
367
368 static class AliasEntry {
369 public String alias;
370 public int uid;
371 }
372
Robin Leee2680422016-01-25 12:24:27 +0000373 static class Credential implements Parcelable {
374 static enum Type {
Robin Leebaefdcf2015-08-26 10:57:44 +0100375 CA_CERTIFICATE (Credentials.CA_CERTIFICATE),
376 USER_CERTIFICATE (Credentials.USER_CERTIFICATE),
377 USER_PRIVATE_KEY (Credentials.USER_PRIVATE_KEY),
378 USER_SECRET_KEY (Credentials.USER_SECRET_KEY);
379
380 final String prefix;
381
382 Type(String prefix) {
383 this.prefix = prefix;
384 }
385 }
386
387 /**
388 * Main part of the credential's alias. To fetch an item from KeyStore, prepend one of the
389 * prefixes from {@link CredentialItem.storedTypes}.
390 */
391 final String alias;
392
393 /**
Robin Leee68d9572016-07-19 16:10:39 +0100394 * UID under which this credential is stored. Typically {@link Process#SYSTEM_UID} but can
395 * also be {@link Process#WIFI_UID} for credentials installed as wifi certificates.
396 */
397 final int uid;
398
399 /**
Robin Leebaefdcf2015-08-26 10:57:44 +0100400 * Should contain some non-empty subset of:
401 * <ul>
402 * <li>{@link Credentials.CA_CERTIFICATE}</li>
403 * <li>{@link Credentials.USER_CERTIFICATE}</li>
404 * <li>{@link Credentials.USER_PRIVATE_KEY}</li>
405 * <li>{@link Credentials.USER_SECRET_KEY}</li>
406 * </ul>
407 */
Robin Lee04046a12016-01-19 11:42:57 +0000408 final EnumSet<Type> storedTypes = EnumSet.noneOf(Type.class);
Robin Leebaefdcf2015-08-26 10:57:44 +0100409
Robin Leee68d9572016-07-19 16:10:39 +0100410 Credential(final String alias, final int uid) {
Robin Leebaefdcf2015-08-26 10:57:44 +0100411 this.alias = alias;
Robin Leee68d9572016-07-19 16:10:39 +0100412 this.uid = uid;
Robin Leebaefdcf2015-08-26 10:57:44 +0100413 }
Robin Lee04046a12016-01-19 11:42:57 +0000414
415 Credential(Parcel in) {
Robin Leee68d9572016-07-19 16:10:39 +0100416 this(in.readString(), in.readInt());
Robin Lee04046a12016-01-19 11:42:57 +0000417
418 long typeBits = in.readLong();
419 for (Type i : Type.values()) {
420 if ((typeBits & (1L << i.ordinal())) != 0L) {
421 storedTypes.add(i);
422 }
423 }
424 }
425
426 public void writeToParcel(Parcel out, int flags) {
427 out.writeString(alias);
Robin Leee68d9572016-07-19 16:10:39 +0100428 out.writeInt(uid);
Robin Lee04046a12016-01-19 11:42:57 +0000429
430 long typeBits = 0;
431 for (Type i : storedTypes) {
432 typeBits |= 1L << i.ordinal();
433 }
434 out.writeLong(typeBits);
435 }
436
437 public int describeContents() {
438 return 0;
439 }
440
441 public static final Parcelable.Creator<Credential> CREATOR
442 = new Parcelable.Creator<Credential>() {
443 public Credential createFromParcel(Parcel in) {
444 return new Credential(in);
445 }
446
447 public Credential[] newArray(int size) {
448 return new Credential[size];
449 }
450 };
Robin Leee68d9572016-07-19 16:10:39 +0100451
452 public boolean isSystem() {
453 return UserHandle.getAppId(uid) == Process.SYSTEM_UID;
454 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100455 }
456}