1 /*
2  * Copyright (C) 2017 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 
17 package com.android.settings.security;
18 
19 import android.content.Context;
20 import android.os.UserManager;
21 import android.security.keystore.KeyProperties;
22 import android.security.keystore2.AndroidKeyStoreLoadStoreParameter;
23 
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settingslib.RestrictedPreference;
27 import com.android.settingslib.core.lifecycle.Lifecycle;
28 import com.android.settingslib.core.lifecycle.LifecycleObserver;
29 import com.android.settingslib.core.lifecycle.events.OnResume;
30 
31 import java.security.KeyStore;
32 import java.security.KeyStoreException;
33 
34 public class ResetCredentialsPreferenceController extends RestrictedEncryptionPreferenceController
35         implements LifecycleObserver, OnResume {
36 
37     private static final String KEY_RESET_CREDENTIALS = "credentials_reset";
38 
39     private final KeyStore mKeyStore;
40     private final KeyStore mWifiKeyStore;
41 
42     private RestrictedPreference mPreference;
43 
ResetCredentialsPreferenceController(Context context, Lifecycle lifecycle)44     public ResetCredentialsPreferenceController(Context context, Lifecycle lifecycle) {
45         super(context, UserManager.DISALLOW_CONFIG_CREDENTIALS);
46         KeyStore keyStore = null;
47         try {
48             keyStore = KeyStore.getInstance("AndroidKeyStore");
49             keyStore.load(null);
50         } catch (Exception e) {
51             keyStore = null;
52         }
53         mKeyStore = keyStore;
54         keyStore = null;
55         if (context.getUser().isSystem()) {
56             try {
57                 keyStore = KeyStore.getInstance("AndroidKeyStore");
58                 keyStore.load(new AndroidKeyStoreLoadStoreParameter(KeyProperties.NAMESPACE_WIFI));
59             } catch (Exception e) {
60                 keyStore = null;
61             }
62         }
63         mWifiKeyStore = keyStore;
64         if (lifecycle != null) {
65             lifecycle.addObserver(this);
66         }
67     }
68 
69     @Override
getPreferenceKey()70     public String getPreferenceKey() {
71         return KEY_RESET_CREDENTIALS;
72     }
73 
74     @Override
displayPreference(PreferenceScreen screen)75     public void displayPreference(PreferenceScreen screen) {
76         super.displayPreference(screen);
77         mPreference = screen.findPreference(getPreferenceKey());
78     }
79 
80     @Override
onResume()81     public void onResume() {
82         if (mPreference != null && !mPreference.isDisabledByAdmin()) {
83             boolean isEnabled = false;
84             try {
85                 isEnabled = (mKeyStore != null
86                         && mKeyStore.aliases().hasMoreElements())
87                         || (mWifiKeyStore != null
88                         && mWifiKeyStore.aliases().hasMoreElements());
89 
90             } catch (KeyStoreException e) {
91                 // If access to keystore fails, treat as disabled.
92             }
93             mPreference.setEnabled(isEnabled);
94         }
95     }
96 }
97