blob: 8a75905a8216031a8d07699c932c32e6feb2b344 [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 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.AdapterView;
46import android.widget.AdapterView.OnItemClickListener;
47import android.widget.ArrayAdapter;
Robin Leebaefdcf2015-08-26 10:57:44 +010048import android.widget.ListView;
49import android.widget.TextView;
50
Robin Leeb70b3d82016-02-01 12:52:16 +000051import com.android.internal.logging.MetricsProto.MetricsEvent;
Ricky Wai95792742016-05-24 19:28:53 +010052import com.android.internal.widget.LockPatternUtils;
Robin Leec421db72016-03-11 16:22:23 +000053import com.android.settingslib.RestrictedLockUtils;
54import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
Robin Leeb70b3d82016-02-01 12:52:16 +000055
Robin Leee68d9572016-07-19 16:10:39 +010056import java.util.ArrayList;
Robin Leebaefdcf2015-08-26 10:57:44 +010057import java.util.EnumSet;
Robin Leee68d9572016-07-19 16:10:39 +010058import java.util.List;
Robin Leebaefdcf2015-08-26 10:57:44 +010059import java.util.SortedMap;
60import java.util.TreeMap;
61
Robin Leebaefdcf2015-08-26 10:57:44 +010062import static android.view.View.GONE;
Jason Monk39b46742015-09-10 15:52:51 -040063import static android.view.View.VISIBLE;
Robin Leebaefdcf2015-08-26 10:57:44 +010064
Udam Saini0708d9e2016-03-28 16:35:13 -070065public class UserCredentialsSettings extends OptionsMenuFragment implements OnItemClickListener {
Robin Leebaefdcf2015-08-26 10:57:44 +010066 private static final String TAG = "UserCredentialsSettings";
67
Robin Leebaefdcf2015-08-26 10:57:44 +010068 private ListView mListView;
69
70 @Override
Fan Zhang65076132016-08-08 10:25:13 -070071 public int getMetricsCategory() {
Robin Leeb70b3d82016-02-01 12:52:16 +000072 return MetricsEvent.USER_CREDENTIALS;
Robin Leebaefdcf2015-08-26 10:57:44 +010073 }
74
75 @Override
76 public void onResume() {
77 super.onResume();
Robin Lee04046a12016-01-19 11:42:57 +000078 refreshItems();
Robin Leebaefdcf2015-08-26 10:57:44 +010079 }
80
81 @Override
82 public View onCreateView(
83 LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
Robin Leee68d9572016-07-19 16:10:39 +010084 final View rootView = inflater.inflate(R.layout.user_credentials, parent, false);
Robin Leebaefdcf2015-08-26 10:57:44 +010085
86 // Set up an OnItemClickListener for the credential list.
Robin Leee68d9572016-07-19 16:10:39 +010087 mListView = (ListView) rootView.findViewById(R.id.credential_list);
Robin Leebaefdcf2015-08-26 10:57:44 +010088 mListView.setOnItemClickListener(this);
89
Robin Leee68d9572016-07-19 16:10:39 +010090 return rootView;
Robin Leebaefdcf2015-08-26 10:57:44 +010091 }
92
93 @Override
94 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
95 final Credential item = (Credential) parent.getItemAtPosition(position);
Robin Lee04046a12016-01-19 11:42:57 +000096 CredentialDialogFragment.show(this, item);
97 }
Robin Leebaefdcf2015-08-26 10:57:44 +010098
Robin Lee11fd5502016-05-16 15:42:34 +010099 protected void announceRemoval(String alias) {
100 if (isAdded()) {
101 mListView.announceForAccessibility(getString(R.string.user_credential_removed, alias));
102 }
103 }
104
Robin Lee04046a12016-01-19 11:42:57 +0000105 protected void refreshItems() {
106 if (isAdded()) {
107 new AliasLoader().execute();
108 }
109 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100110
Robin Lee04046a12016-01-19 11:42:57 +0000111 public static class CredentialDialogFragment extends DialogFragment {
112 private static final String TAG = "CredentialDialogFragment";
113 private static final String ARG_CREDENTIAL = "credential";
114
115 public static void show(Fragment target, Credential item) {
116 final Bundle args = new Bundle();
117 args.putParcelable(ARG_CREDENTIAL, item);
118
Robin Leef8e2dbf2016-04-07 13:17:24 +0100119 if (target.getFragmentManager().findFragmentByTag(TAG) == null) {
120 final DialogFragment frag = new CredentialDialogFragment();
121 frag.setTargetFragment(target, /* requestCode */ -1);
122 frag.setArguments(args);
123 frag.show(target.getFragmentManager(), TAG);
124 }
Robin Lee04046a12016-01-19 11:42:57 +0000125 }
126
127 @Override
128 public Dialog onCreateDialog(Bundle savedInstanceState) {
129 final Credential item = (Credential) getArguments().getParcelable(ARG_CREDENTIAL);
Robin Leee68d9572016-07-19 16:10:39 +0100130
Robin Lee04046a12016-01-19 11:42:57 +0000131 View root = getActivity().getLayoutInflater()
132 .inflate(R.layout.user_credential_dialog, null);
133 ViewGroup infoContainer = (ViewGroup) root.findViewById(R.id.credential_container);
Robin Leee68d9572016-07-19 16:10:39 +0100134 View contentView = getCredentialView(item, R.layout.user_credential, null,
135 infoContainer, /* expanded */ true);
136 infoContainer.addView(contentView);
Robin Leec421db72016-03-11 16:22:23 +0000137
138 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
Robin Lee04046a12016-01-19 11:42:57 +0000139 .setView(root)
140 .setTitle(R.string.user_credential_title)
Robin Leec421db72016-03-11 16:22:23 +0000141 .setPositiveButton(R.string.done, null);
142
143 final String restriction = UserManager.DISALLOW_CONFIG_CREDENTIALS;
144 final int myUserId = UserHandle.myUserId();
145 if (!RestrictedLockUtils.hasBaseUserRestriction(getContext(), restriction, myUserId)) {
146 DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
147 @Override public void onClick(DialogInterface dialog, int id) {
148 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(
149 getContext(), restriction, myUserId);
150 if (admin != null) {
151 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
152 admin);
153 } else {
Robin Leee68d9572016-07-19 16:10:39 +0100154 new RemoveCredentialsTask(getContext(), getTargetFragment())
155 .execute(item);
Robin Leec421db72016-03-11 16:22:23 +0000156 }
157 dialog.dismiss();
158 }
159 };
Robin Leee68d9572016-07-19 16:10:39 +0100160 if (item.isSystem()) {
161 // TODO: a safe means of clearing wifi certificates. Configs refer to aliases
162 // directly so deleting certs will break dependent access points.
163 builder.setNegativeButton(R.string.trusted_credentials_remove_label, listener);
164 }
Robin Leec421db72016-03-11 16:22:23 +0000165 }
166 return builder.create();
Robin Lee04046a12016-01-19 11:42:57 +0000167 }
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)) {
Ricky Wai95792742016-05-24 19:28:53 +0100261 // Do not show work profile keys in user credentials
262 if (alias.startsWith(LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT) ||
263 alias.startsWith(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT)) {
264 continue;
265 }
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) {
279 final Credential[] credentialArray = credentials.toArray(new Credential[0]);
280 mListView.setAdapter(new CredentialAdapter(getContext(), credentialArray));
Robin Leebaefdcf2015-08-26 10:57:44 +0100281 }
282 }
283
284 /**
285 * Helper class to display {@link Credential}s in a list.
286 */
287 private static class CredentialAdapter extends ArrayAdapter<Credential> {
Robin Leee68d9572016-07-19 16:10:39 +0100288 private static final int LAYOUT_RESOURCE = R.layout.user_credential_preference;
289
290 public CredentialAdapter(Context context, final Credential[] objects) {
291 super(context, LAYOUT_RESOURCE, objects);
Robin Leebaefdcf2015-08-26 10:57:44 +0100292 }
293
294 @Override
Robin Leee68d9572016-07-19 16:10:39 +0100295 public View getView(int position, @Nullable View view, ViewGroup parent) {
296 return getCredentialView(getItem(position), LAYOUT_RESOURCE, view, parent,
297 /* expanded */ false);
Robin Leebaefdcf2015-08-26 10:57:44 +0100298 }
299 }
300
Robin Leee68d9572016-07-19 16:10:39 +0100301 /**
302 * Mapping from View IDs in {@link R} to the types of credentials they describe.
303 */
304 private static final SparseArray<Credential.Type> credentialViewTypes = new SparseArray<>();
305 static {
306 credentialViewTypes.put(R.id.contents_userkey, Credential.Type.USER_PRIVATE_KEY);
307 credentialViewTypes.put(R.id.contents_usercrt, Credential.Type.USER_CERTIFICATE);
308 credentialViewTypes.put(R.id.contents_cacrt, Credential.Type.CA_CERTIFICATE);
309 }
310
311 protected static View getCredentialView(Credential item, @LayoutRes int layoutResource,
312 @Nullable View view, ViewGroup parent, boolean expanded) {
313 if (view == null) {
314 view = LayoutInflater.from(parent.getContext()).inflate(layoutResource, parent, false);
315 }
316
317 ((TextView) view.findViewById(R.id.alias)).setText(item.alias);
318 ((TextView) view.findViewById(R.id.purpose)).setText(item.isSystem()
319 ? R.string.credential_for_vpn_and_apps
320 : R.string.credential_for_wifi);
321
322 view.findViewById(R.id.contents).setVisibility(expanded ? View.VISIBLE : View.GONE);
323 if (expanded) {
324 for (int i = 0; i < credentialViewTypes.size(); i++) {
325 final View detail = view.findViewById(credentialViewTypes.keyAt(i));
326 detail.setVisibility(item.storedTypes.contains(credentialViewTypes.valueAt(i))
327 ? View.VISIBLE : View.GONE);
328 }
329 }
330 return view;
331 }
332
333 static class AliasEntry {
334 public String alias;
335 public int uid;
336 }
337
Robin Leee2680422016-01-25 12:24:27 +0000338 static class Credential implements Parcelable {
339 static enum Type {
Robin Leebaefdcf2015-08-26 10:57:44 +0100340 CA_CERTIFICATE (Credentials.CA_CERTIFICATE),
341 USER_CERTIFICATE (Credentials.USER_CERTIFICATE),
342 USER_PRIVATE_KEY (Credentials.USER_PRIVATE_KEY),
343 USER_SECRET_KEY (Credentials.USER_SECRET_KEY);
344
345 final String prefix;
346
347 Type(String prefix) {
348 this.prefix = prefix;
349 }
350 }
351
352 /**
353 * Main part of the credential's alias. To fetch an item from KeyStore, prepend one of the
354 * prefixes from {@link CredentialItem.storedTypes}.
355 */
356 final String alias;
357
358 /**
Robin Leee68d9572016-07-19 16:10:39 +0100359 * UID under which this credential is stored. Typically {@link Process#SYSTEM_UID} but can
360 * also be {@link Process#WIFI_UID} for credentials installed as wifi certificates.
361 */
362 final int uid;
363
364 /**
Robin Leebaefdcf2015-08-26 10:57:44 +0100365 * Should contain some non-empty subset of:
366 * <ul>
367 * <li>{@link Credentials.CA_CERTIFICATE}</li>
368 * <li>{@link Credentials.USER_CERTIFICATE}</li>
369 * <li>{@link Credentials.USER_PRIVATE_KEY}</li>
370 * <li>{@link Credentials.USER_SECRET_KEY}</li>
371 * </ul>
372 */
Robin Lee04046a12016-01-19 11:42:57 +0000373 final EnumSet<Type> storedTypes = EnumSet.noneOf(Type.class);
Robin Leebaefdcf2015-08-26 10:57:44 +0100374
Robin Leee68d9572016-07-19 16:10:39 +0100375 Credential(final String alias, final int uid) {
Robin Leebaefdcf2015-08-26 10:57:44 +0100376 this.alias = alias;
Robin Leee68d9572016-07-19 16:10:39 +0100377 this.uid = uid;
Robin Leebaefdcf2015-08-26 10:57:44 +0100378 }
Robin Lee04046a12016-01-19 11:42:57 +0000379
380 Credential(Parcel in) {
Robin Leee68d9572016-07-19 16:10:39 +0100381 this(in.readString(), in.readInt());
Robin Lee04046a12016-01-19 11:42:57 +0000382
383 long typeBits = in.readLong();
384 for (Type i : Type.values()) {
385 if ((typeBits & (1L << i.ordinal())) != 0L) {
386 storedTypes.add(i);
387 }
388 }
389 }
390
391 public void writeToParcel(Parcel out, int flags) {
392 out.writeString(alias);
Robin Leee68d9572016-07-19 16:10:39 +0100393 out.writeInt(uid);
Robin Lee04046a12016-01-19 11:42:57 +0000394
395 long typeBits = 0;
396 for (Type i : storedTypes) {
397 typeBits |= 1L << i.ordinal();
398 }
399 out.writeLong(typeBits);
400 }
401
402 public int describeContents() {
403 return 0;
404 }
405
406 public static final Parcelable.Creator<Credential> CREATOR
407 = new Parcelable.Creator<Credential>() {
408 public Credential createFromParcel(Parcel in) {
409 return new Credential(in);
410 }
411
412 public Credential[] newArray(int size) {
413 return new Credential[size];
414 }
415 };
Robin Leee68d9572016-07-19 16:10:39 +0100416
417 public boolean isSystem() {
418 return UserHandle.getAppId(uid) == Process.SYSTEM_UID;
419 }
Robin Leebaefdcf2015-08-26 10:57:44 +0100420 }
421}