Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.server.trust; |
| 18 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 19 | import android.Manifest; |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 20 | import android.annotation.UserIdInt; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 22 | import android.app.AlarmManager; |
| 23 | import android.app.AlarmManager.OnAlarmListener; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 24 | import android.app.admin.DevicePolicyManager; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 25 | import android.app.trust.ITrustListener; |
| 26 | import android.app.trust.ITrustManager; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 27 | import android.content.BroadcastReceiver; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 28 | import android.content.ComponentName; |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 29 | import android.content.ContentResolver; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 30 | import android.content.Context; |
| 31 | import android.content.Intent; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 32 | import android.content.IntentFilter; |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 33 | import android.content.pm.ApplicationInfo; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 34 | import android.content.pm.PackageManager; |
| 35 | import android.content.pm.ResolveInfo; |
| 36 | import android.content.pm.UserInfo; |
| 37 | import android.content.res.Resources; |
| 38 | import android.content.res.TypedArray; |
| 39 | import android.content.res.XmlResourceParser; |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 40 | import android.database.ContentObserver; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 41 | import android.graphics.drawable.Drawable; |
Dmitry Dementyev | 3857968 | 2019-01-22 13:41:34 -0800 | [diff] [blame] | 42 | import android.hardware.biometrics.BiometricSourceType; |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 43 | import android.net.Uri; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 44 | import android.os.Binder; |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 45 | import android.os.Build; |
Adrian Roos | a4ba56b | 2014-05-20 12:56:25 +0200 | [diff] [blame] | 46 | import android.os.DeadObjectException; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 47 | import android.os.Handler; |
| 48 | import android.os.IBinder; |
| 49 | import android.os.Message; |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 50 | import android.os.PersistableBundle; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 51 | import android.os.RemoteException; |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 52 | import android.os.SystemClock; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 53 | import android.os.UserHandle; |
| 54 | import android.os.UserManager; |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 55 | import android.provider.Settings; |
Rubin Xu | c398627 | 2019-11-05 10:15:36 +0000 | [diff] [blame^] | 56 | import android.security.KeyStore; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 57 | import android.service.trust.TrustAgentService; |
Zachary Iqbal | 666484d | 2017-03-31 19:10:09 -0700 | [diff] [blame] | 58 | import android.text.TextUtils; |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 59 | import android.util.ArrayMap; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 60 | import android.util.ArraySet; |
| 61 | import android.util.AttributeSet; |
Adrian Roos | 18ea893 | 2014-05-28 14:53:06 +0200 | [diff] [blame] | 62 | import android.util.Log; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 63 | import android.util.Slog; |
Adrian Roos | 7046bfd | 2014-05-16 21:20:54 +0200 | [diff] [blame] | 64 | import android.util.SparseBooleanArray; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 65 | import android.util.Xml; |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 66 | import android.view.IWindowManager; |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 67 | import android.view.WindowManagerGlobal; |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 68 | |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 69 | import com.android.internal.annotations.GuardedBy; |
| 70 | import com.android.internal.content.PackageMonitor; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 71 | import com.android.internal.util.DumpUtils; |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 72 | import com.android.internal.widget.LockPatternUtils; |
| 73 | import com.android.server.SystemService; |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 74 | |
Dmitry Dementyev | 3857968 | 2019-01-22 13:41:34 -0800 | [diff] [blame] | 75 | import org.xmlpull.v1.XmlPullParser; |
| 76 | import org.xmlpull.v1.XmlPullParserException; |
| 77 | |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 78 | import java.io.FileDescriptor; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 79 | import java.io.IOException; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 80 | import java.io.PrintWriter; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 81 | import java.util.ArrayList; |
| 82 | import java.util.List; |
Dmitry Dementyev | 3857968 | 2019-01-22 13:41:34 -0800 | [diff] [blame] | 83 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Manages trust agents and trust listeners. |
| 87 | * |
| 88 | * It is responsible for binding to the enabled {@link android.service.trust.TrustAgentService}s |
| 89 | * of each user and notifies them about events that are relevant to them. |
| 90 | * It start and stops them based on the value of |
| 91 | * {@link com.android.internal.widget.LockPatternUtils#getEnabledTrustAgents(int)}. |
| 92 | * |
| 93 | * It also keeps a set of {@link android.app.trust.ITrustListener}s that are notified whenever the |
| 94 | * trust state changes for any user. |
| 95 | * |
| 96 | * Trust state and the setting of enabled agents is kept per user and each user has its own |
| 97 | * instance of a {@link android.service.trust.TrustAgentService}. |
| 98 | */ |
| 99 | public class TrustManagerService extends SystemService { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 100 | private static final String TAG = "TrustManagerService"; |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 101 | static final boolean DEBUG = Build.IS_DEBUGGABLE && Log.isLoggable(TAG, Log.VERBOSE); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 102 | |
| 103 | private static final Intent TRUST_AGENT_INTENT = |
| 104 | new Intent(TrustAgentService.SERVICE_INTERFACE); |
Adrian Roos | 18ea893 | 2014-05-28 14:53:06 +0200 | [diff] [blame] | 105 | private static final String PERMISSION_PROVIDE_AGENT = Manifest.permission.PROVIDE_TRUST_AGENT; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 106 | |
| 107 | private static final int MSG_REGISTER_LISTENER = 1; |
| 108 | private static final int MSG_UNREGISTER_LISTENER = 2; |
| 109 | private static final int MSG_DISPATCH_UNLOCK_ATTEMPT = 3; |
| 110 | private static final int MSG_ENABLED_AGENTS_CHANGED = 4; |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 111 | private static final int MSG_KEYGUARD_SHOWING_CHANGED = 6; |
| 112 | private static final int MSG_START_USER = 7; |
| 113 | private static final int MSG_CLEANUP_USER = 8; |
| 114 | private static final int MSG_SWITCH_USER = 9; |
Rubin Xu | 83a15bc | 2016-08-10 10:53:24 +0100 | [diff] [blame] | 115 | private static final int MSG_FLUSH_TRUST_USUALLY_MANAGED = 10; |
| 116 | private static final int MSG_UNLOCK_USER = 11; |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 117 | private static final int MSG_STOP_USER = 12; |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 118 | private static final int MSG_DISPATCH_UNLOCK_LOCKOUT = 13; |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 119 | private static final int MSG_REFRESH_DEVICE_LOCKED_FOR_USER = 14; |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 120 | private static final int MSG_SCHEDULE_TRUST_TIMEOUT = 15; |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 121 | |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 122 | private static final int TRUST_USUALLY_MANAGED_FLUSH_DELAY = 2 * 60 * 1000; |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 123 | private static final String TRUST_TIMEOUT_ALARM_TAG = "TrustManagerService.trustTimeoutForUser"; |
Dmitry Dementyev | 3857968 | 2019-01-22 13:41:34 -0800 | [diff] [blame] | 124 | private static final long TRUST_TIMEOUT_IN_MILLIS = 4 * 60 * 60 * 1000; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 125 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 126 | private final ArraySet<AgentInfo> mActiveAgents = new ArraySet<>(); |
| 127 | private final ArrayList<ITrustListener> mTrustListeners = new ArrayList<>(); |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 128 | private final Receiver mReceiver = new Receiver(); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 129 | |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 130 | /* package */ final TrustArchive mArchive = new TrustArchive(); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 131 | private final Context mContext; |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 132 | private final LockPatternUtils mLockPatternUtils; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 133 | private final UserManager mUserManager; |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 134 | private final ActivityManager mActivityManager; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 135 | |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 136 | @GuardedBy("mUserIsTrusted") |
| 137 | private final SparseBooleanArray mUserIsTrusted = new SparseBooleanArray(); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 138 | |
Rubin Xu | c398627 | 2019-11-05 10:15:36 +0000 | [diff] [blame^] | 139 | /** |
| 140 | * Stores the locked state for users on the device. There are three different type of users |
| 141 | * which are handled slightly differently: |
| 142 | * <ul> |
| 143 | * <li> Users with real keyguard |
| 144 | * These are users who can be switched to ({@link UserInfo#supportsSwitchToByUser()}). Their |
| 145 | * locked state is derived by a combination of user secure state, keyguard state, trust agent |
| 146 | * decision and biometric authentication result. These are updated via |
| 147 | * {@link #refreshDeviceLockedForUser(int)} and result stored in {@link #mDeviceLockedForUser}. |
| 148 | * <li> Managed profiles with unified challenge |
| 149 | * Managed profile with unified challenge always shares the same locked state as their parent, |
| 150 | * so their locked state is not recorded in {@link #mDeviceLockedForUser}. Instead, |
| 151 | * {@link ITrustManager#isDeviceLocked(int)} always resolves their parent user handle and |
| 152 | * queries its locked state instead. |
| 153 | * <li> Managed profiles with separate challenge |
| 154 | * Locked state for profile with separate challenge is determined by other parts of the |
| 155 | * framework (mostly PowerManager) and pushed to TrustManagerService via |
| 156 | * {@link ITrustManager#setDeviceLockedForUser(int, boolean)}. Although in a corner case when |
| 157 | * the profile has a separate but empty challenge, setting its {@link #mDeviceLockedForUser} to |
| 158 | * {@code false} is actually done by {@link #refreshDeviceLockedForUser(int)}. |
| 159 | * </ul> |
| 160 | * TODO: Rename {@link ITrustManager#setDeviceLockedForUser(int, boolean)} to |
| 161 | * {@code setDeviceLockedForProfile} to better reflect its purpose. Unifying |
| 162 | * {@code setDeviceLockedForProfile} and {@link #setDeviceLockedForUser} would also be nice. |
| 163 | * At the moment they both update {@link #mDeviceLockedForUser} but have slightly different |
| 164 | * side-effects: one notifies trust agents while the other sends out a broadcast. |
| 165 | */ |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 166 | @GuardedBy("mDeviceLockedForUser") |
| 167 | private final SparseBooleanArray mDeviceLockedForUser = new SparseBooleanArray(); |
| 168 | |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 169 | @GuardedBy("mTrustUsuallyManagedForUser") |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 170 | private final SparseBooleanArray mTrustUsuallyManagedForUser = new SparseBooleanArray(); |
| 171 | |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 172 | // set to true only if user can skip bouncer |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 173 | @GuardedBy("mUsersUnlockedByBiometric") |
| 174 | private final SparseBooleanArray mUsersUnlockedByBiometric = new SparseBooleanArray(); |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 175 | |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 176 | private final ArrayMap<Integer, TrustTimeoutAlarmListener> mTrustTimeoutAlarmListenerForUser = |
| 177 | new ArrayMap<>(); |
| 178 | private AlarmManager mAlarmManager; |
| 179 | private final SettingsObserver mSettingsObserver; |
| 180 | |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 181 | private final StrongAuthTracker mStrongAuthTracker; |
| 182 | |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 183 | private boolean mTrustAgentsCanRun = false; |
Xiaohui Chen | 09e0291 | 2015-08-05 09:44:56 -0700 | [diff] [blame] | 184 | private int mCurrentUser = UserHandle.USER_SYSTEM; |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 185 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 186 | public TrustManagerService(Context context) { |
| 187 | super(context); |
| 188 | mContext = context; |
| 189 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 190 | mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 191 | mLockPatternUtils = new LockPatternUtils(context); |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 192 | mStrongAuthTracker = new StrongAuthTracker(context); |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 193 | mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| 194 | mSettingsObserver = new SettingsObserver(mHandler); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void onStart() { |
| 199 | publishBinderService(Context.TRUST_SERVICE, mService); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void onBootPhase(int phase) { |
Adrian Roos | 49d5345 | 2014-10-24 15:48:39 +0200 | [diff] [blame] | 204 | if (isSafeMode()) { |
| 205 | // No trust agents in safe mode. |
| 206 | return; |
| 207 | } |
| 208 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 209 | mPackageMonitor.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 210 | mReceiver.register(mContext); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 211 | mLockPatternUtils.registerStrongAuthTracker(mStrongAuthTracker); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 212 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 213 | mTrustAgentsCanRun = true; |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 214 | refreshAgentList(UserHandle.USER_ALL); |
Xiaohui Chen | 605733b | 2016-05-19 16:39:09 -0700 | [diff] [blame] | 215 | refreshDeviceLockedForUser(UserHandle.USER_ALL); |
Adrian Roos | 49d5345 | 2014-10-24 15:48:39 +0200 | [diff] [blame] | 216 | } else if (phase == SystemService.PHASE_BOOT_COMPLETED) { |
Xiaohui Chen | 09e0291 | 2015-08-05 09:44:56 -0700 | [diff] [blame] | 217 | maybeEnableFactoryTrustAgents(mLockPatternUtils, UserHandle.USER_SYSTEM); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 221 | // Extend unlock config and logic |
| 222 | |
| 223 | private final class SettingsObserver extends ContentObserver { |
| 224 | private final Uri TRUST_AGENTS_EXTEND_UNLOCK = |
| 225 | Settings.Secure.getUriFor(Settings.Secure.TRUST_AGENTS_EXTEND_UNLOCK); |
| 226 | |
| 227 | private final Uri LOCK_SCREEN_WHEN_TRUST_LOST = |
| 228 | Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_WHEN_TRUST_LOST); |
| 229 | |
Anthony Chen | 9128a5a | 2019-06-25 09:50:56 -0700 | [diff] [blame] | 230 | private final boolean mIsAutomotive; |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 231 | private final ContentResolver mContentResolver; |
| 232 | private boolean mTrustAgentsExtendUnlock; |
| 233 | private boolean mLockWhenTrustLost; |
| 234 | |
| 235 | /** |
| 236 | * Creates a settings observer |
| 237 | * |
| 238 | * @param handler The handler to run {@link #onChange} on, or null if none. |
| 239 | */ |
| 240 | SettingsObserver(Handler handler) { |
| 241 | super(handler); |
Anthony Chen | 9128a5a | 2019-06-25 09:50:56 -0700 | [diff] [blame] | 242 | |
| 243 | PackageManager packageManager = getContext().getPackageManager(); |
| 244 | mIsAutomotive = packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); |
| 245 | |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 246 | mContentResolver = getContext().getContentResolver(); |
| 247 | updateContentObserver(); |
| 248 | } |
| 249 | |
| 250 | void updateContentObserver() { |
| 251 | mContentResolver.unregisterContentObserver(this); |
| 252 | mContentResolver.registerContentObserver(TRUST_AGENTS_EXTEND_UNLOCK, |
| 253 | false /* notifyForDescendents */, |
| 254 | this /* observer */, |
| 255 | mCurrentUser); |
| 256 | mContentResolver.registerContentObserver(LOCK_SCREEN_WHEN_TRUST_LOST, |
| 257 | false /* notifyForDescendents */, |
| 258 | this /* observer */, |
| 259 | mCurrentUser); |
| 260 | |
| 261 | // Update the value immediately |
| 262 | onChange(true /* selfChange */, TRUST_AGENTS_EXTEND_UNLOCK); |
| 263 | onChange(true /* selfChange */, LOCK_SCREEN_WHEN_TRUST_LOST); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void onChange(boolean selfChange, Uri uri) { |
| 268 | if (TRUST_AGENTS_EXTEND_UNLOCK.equals(uri)) { |
Anthony Chen | 9128a5a | 2019-06-25 09:50:56 -0700 | [diff] [blame] | 269 | // Smart lock should only extend unlock. The only exception is for automotive, |
| 270 | // where it can actively unlock the head unit. |
| 271 | int defaultValue = mIsAutomotive ? 0 : 1; |
| 272 | |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 273 | mTrustAgentsExtendUnlock = |
| 274 | Settings.Secure.getIntForUser( |
| 275 | mContentResolver, |
| 276 | Settings.Secure.TRUST_AGENTS_EXTEND_UNLOCK, |
Anthony Chen | 9128a5a | 2019-06-25 09:50:56 -0700 | [diff] [blame] | 277 | defaultValue, |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 278 | mCurrentUser) != 0; |
| 279 | } else if (LOCK_SCREEN_WHEN_TRUST_LOST.equals(uri)) { |
| 280 | mLockWhenTrustLost = |
| 281 | Settings.Secure.getIntForUser( |
| 282 | mContentResolver, |
| 283 | Settings.Secure.LOCK_SCREEN_WHEN_TRUST_LOST, |
| 284 | 0 /* default */, |
| 285 | mCurrentUser) != 0; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | boolean getTrustAgentsExtendUnlock() { |
| 290 | return mTrustAgentsExtendUnlock; |
| 291 | } |
| 292 | |
| 293 | boolean getLockWhenTrustLost() { |
| 294 | return mLockWhenTrustLost; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | private void maybeLockScreen(int userId) { |
| 299 | if (userId != mCurrentUser) { |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | if (mSettingsObserver.getLockWhenTrustLost()) { |
| 304 | if (DEBUG) Slog.d(TAG, "Locking device because trust was lost"); |
| 305 | try { |
| 306 | WindowManagerGlobal.getWindowManagerService().lockNow(null); |
| 307 | } catch (RemoteException e) { |
| 308 | Slog.e(TAG, "Error locking screen when trust was lost"); |
| 309 | } |
| 310 | |
| 311 | // If active unlocking is not allowed, cancel any pending trust timeouts because the |
| 312 | // screen is already locked. |
| 313 | TrustTimeoutAlarmListener alarm = mTrustTimeoutAlarmListenerForUser.get(userId); |
| 314 | if (alarm != null && mSettingsObserver.getTrustAgentsExtendUnlock()) { |
| 315 | mAlarmManager.cancel(alarm); |
| 316 | alarm.setQueued(false /* isQueued */); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | private void scheduleTrustTimeout(int userId, boolean override) { |
| 322 | int shouldOverride = override ? 1 : 0; |
| 323 | if (override) { |
| 324 | shouldOverride = 1; |
| 325 | } |
| 326 | mHandler.obtainMessage(MSG_SCHEDULE_TRUST_TIMEOUT, userId, shouldOverride).sendToTarget(); |
| 327 | } |
| 328 | |
| 329 | private void handleScheduleTrustTimeout(int userId, int shouldOverride) { |
| 330 | long when = SystemClock.elapsedRealtime() + TRUST_TIMEOUT_IN_MILLIS; |
| 331 | userId = mCurrentUser; |
| 332 | TrustTimeoutAlarmListener alarm = mTrustTimeoutAlarmListenerForUser.get(userId); |
| 333 | |
| 334 | // Cancel existing trust timeouts for this user if needed. |
| 335 | if (alarm != null) { |
| 336 | if (shouldOverride == 0 && alarm.isQueued()) { |
| 337 | if (DEBUG) Slog.d(TAG, "Found existing trust timeout alarm. Skipping."); |
| 338 | return; |
| 339 | } |
| 340 | mAlarmManager.cancel(alarm); |
| 341 | } else { |
| 342 | alarm = new TrustTimeoutAlarmListener(userId); |
| 343 | mTrustTimeoutAlarmListenerForUser.put(userId, alarm); |
| 344 | } |
| 345 | |
| 346 | if (DEBUG) Slog.d(TAG, "\tSetting up trust timeout alarm"); |
| 347 | alarm.setQueued(true /* isQueued */); |
| 348 | mAlarmManager.setExact( |
| 349 | AlarmManager.ELAPSED_REALTIME_WAKEUP, when, TRUST_TIMEOUT_ALARM_TAG, alarm, |
| 350 | mHandler); |
| 351 | } |
| 352 | |
| 353 | // Agent management |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 354 | |
| 355 | private static final class AgentInfo { |
| 356 | CharSequence label; |
| 357 | Drawable icon; |
| 358 | ComponentName component; // service that implements ITrustAgent |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 359 | SettingsAttrs settings; // setting to launch to modify agent. |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 360 | TrustAgentWrapper agent; |
| 361 | int userId; |
| 362 | |
| 363 | @Override |
| 364 | public boolean equals(Object other) { |
| 365 | if (!(other instanceof AgentInfo)) { |
| 366 | return false; |
| 367 | } |
| 368 | AgentInfo o = (AgentInfo) other; |
| 369 | return component.equals(o.component) && userId == o.userId; |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public int hashCode() { |
| 374 | return component.hashCode() * 31 + userId; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | private void updateTrustAll() { |
| 379 | List<UserInfo> userInfos = mUserManager.getUsers(true /* excludeDying */); |
| 380 | for (UserInfo userInfo : userInfos) { |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 381 | updateTrust(userInfo.id, 0); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 385 | |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 386 | public void updateTrust(int userId, int flags) { |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 387 | updateTrust(userId, flags, false /* isFromUnlock */); |
| 388 | } |
| 389 | |
| 390 | private void updateTrust(int userId, int flags, boolean isFromUnlock) { |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 391 | boolean managed = aggregateIsTrustManaged(userId); |
| 392 | dispatchOnTrustManagedChanged(managed, userId); |
| 393 | if (mStrongAuthTracker.isTrustAllowedForUser(userId) |
| 394 | && isTrustUsuallyManagedInternal(userId) != managed) { |
| 395 | updateTrustUsuallyManaged(userId, managed); |
| 396 | } |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 397 | |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 398 | boolean trusted = aggregateIsTrusted(userId); |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 399 | IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); |
| 400 | boolean showingKeyguard = true; |
| 401 | try { |
| 402 | showingKeyguard = wm.isKeyguardLocked(); |
| 403 | } catch (RemoteException e) { |
| 404 | } |
| 405 | |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 406 | boolean changed; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 407 | synchronized (mUserIsTrusted) { |
Chad Brubaker | c4c5257 | 2019-02-17 13:34:02 -0800 | [diff] [blame] | 408 | if (mSettingsObserver.getTrustAgentsExtendUnlock()) { |
| 409 | // In extend unlock trust agents can only set the device to trusted if it already |
| 410 | // trusted or the device is unlocked. Attempting to set the device as trusted |
| 411 | // when the device is locked will be ignored. |
| 412 | changed = mUserIsTrusted.get(userId) != trusted; |
| 413 | trusted = trusted |
| 414 | && (!showingKeyguard || isFromUnlock || !changed) |
| 415 | && userId == mCurrentUser; |
| 416 | if (DEBUG) { |
| 417 | Slog.d(TAG, "Extend unlock setting trusted as " + Boolean.toString(trusted) |
| 418 | + " && " + Boolean.toString(!showingKeyguard) |
| 419 | + " && " + Boolean.toString(userId == mCurrentUser)); |
| 420 | } |
| 421 | } |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 422 | changed = mUserIsTrusted.get(userId) != trusted; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 423 | mUserIsTrusted.put(userId, trusted); |
| 424 | } |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 425 | dispatchOnTrustChanged(trusted, userId, flags); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 426 | if (changed) { |
| 427 | refreshDeviceLockedForUser(userId); |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 428 | if (!trusted) { |
| 429 | maybeLockScreen(userId); |
| 430 | } else { |
| 431 | scheduleTrustTimeout(userId, false /* override */); |
| 432 | } |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 433 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 436 | private void updateTrustUsuallyManaged(int userId, boolean managed) { |
| 437 | synchronized (mTrustUsuallyManagedForUser) { |
| 438 | mTrustUsuallyManagedForUser.put(userId, managed); |
| 439 | } |
| 440 | // Wait a few minutes before committing to flash, in case the trust agent is transiently not |
| 441 | // managing trust (crashed, needs to acknowledge DPM restrictions, etc). |
| 442 | mHandler.removeMessages(MSG_FLUSH_TRUST_USUALLY_MANAGED); |
| 443 | mHandler.sendMessageDelayed( |
| 444 | mHandler.obtainMessage(MSG_FLUSH_TRUST_USUALLY_MANAGED), |
| 445 | TRUST_USUALLY_MANAGED_FLUSH_DELAY); |
| 446 | } |
| 447 | |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 448 | public long addEscrowToken(byte[] token, int userId) { |
Ram Periathiruvadi | 32d5355 | 2019-02-19 13:25:46 -0800 | [diff] [blame] | 449 | return mLockPatternUtils.addEscrowToken(token, userId, |
| 450 | (long handle, int userid) -> { |
| 451 | dispatchEscrowTokenActivatedLocked(handle, userid); |
| 452 | }); |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | public boolean removeEscrowToken(long handle, int userId) { |
| 456 | return mLockPatternUtils.removeEscrowToken(handle, userId); |
| 457 | } |
| 458 | |
| 459 | public boolean isEscrowTokenActive(long handle, int userId) { |
| 460 | return mLockPatternUtils.isEscrowTokenActive(handle, userId); |
| 461 | } |
| 462 | |
| 463 | public void unlockUserWithToken(long handle, byte[] token, int userId) { |
| 464 | mLockPatternUtils.unlockUserWithToken(handle, token, userId); |
| 465 | } |
| 466 | |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 467 | void showKeyguardErrorMessage(CharSequence message) { |
| 468 | dispatchOnTrustError(message); |
| 469 | } |
| 470 | |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 471 | void refreshAgentList(int userIdOrAll) { |
| 472 | if (DEBUG) Slog.d(TAG, "refreshAgentList(" + userIdOrAll + ")"); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 473 | if (!mTrustAgentsCanRun) { |
Adrian Roos | 49d5345 | 2014-10-24 15:48:39 +0200 | [diff] [blame] | 474 | return; |
| 475 | } |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 476 | if (userIdOrAll != UserHandle.USER_ALL && userIdOrAll < UserHandle.USER_SYSTEM) { |
| 477 | Log.e(TAG, "refreshAgentList(userId=" + userIdOrAll + "): Invalid user handle," |
Adrian Roos | e681c27 | 2014-09-08 14:03:47 +0200 | [diff] [blame] | 478 | + " must be USER_ALL or a specific user.", new Throwable("here")); |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 479 | userIdOrAll = UserHandle.USER_ALL; |
Adrian Roos | e681c27 | 2014-09-08 14:03:47 +0200 | [diff] [blame] | 480 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 481 | PackageManager pm = mContext.getPackageManager(); |
| 482 | |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 483 | List<UserInfo> userInfos; |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 484 | if (userIdOrAll == UserHandle.USER_ALL) { |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 485 | userInfos = mUserManager.getUsers(true /* excludeDying */); |
| 486 | } else { |
| 487 | userInfos = new ArrayList<>(); |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 488 | userInfos.add(mUserManager.getUserInfo(userIdOrAll)); |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 489 | } |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 490 | LockPatternUtils lockPatternUtils = mLockPatternUtils; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 491 | |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 492 | ArraySet<AgentInfo> obsoleteAgents = new ArraySet<>(); |
| 493 | obsoleteAgents.addAll(mActiveAgents); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 494 | |
| 495 | for (UserInfo userInfo : userInfos) { |
Adrian Roos | fc29e0b | 2014-11-11 12:55:44 +0100 | [diff] [blame] | 496 | if (userInfo == null || userInfo.partial || !userInfo.isEnabled() |
| 497 | || userInfo.guestToRemove) continue; |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 498 | if (!userInfo.supportsSwitchToByUser()) { |
| 499 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id |
| 500 | + ": switchToByUser=false"); |
| 501 | continue; |
| 502 | } |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 503 | if (!mActivityManager.isUserRunning(userInfo.id)) { |
| 504 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id |
| 505 | + ": user not started"); |
| 506 | continue; |
| 507 | } |
| 508 | if (!lockPatternUtils.isSecure(userInfo.id)) { |
| 509 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id |
| 510 | + ": no secure credential"); |
| 511 | continue; |
| 512 | } |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 513 | |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 514 | DevicePolicyManager dpm = lockPatternUtils.getDevicePolicyManager(); |
| 515 | int disabledFeatures = dpm.getKeyguardDisabledFeatures(null, userInfo.id); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 516 | final boolean disableTrustAgents = |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 517 | (disabledFeatures & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0; |
| 518 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 519 | List<ComponentName> enabledAgents = lockPatternUtils.getEnabledTrustAgents(userInfo.id); |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 520 | if (enabledAgents == null) { |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 521 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id |
| 522 | + ": no agents enabled by user"); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 523 | continue; |
| 524 | } |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 525 | List<ResolveInfo> resolveInfos = resolveAllowedTrustAgents(pm, userInfo.id); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 526 | for (ResolveInfo resolveInfo : resolveInfos) { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 527 | ComponentName name = getComponentName(resolveInfo); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 528 | |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 529 | if (!enabledAgents.contains(name)) { |
| 530 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping " |
| 531 | + name.flattenToShortString() + " u"+ userInfo.id |
| 532 | + ": not enabled by user"); |
| 533 | continue; |
| 534 | } |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 535 | if (disableTrustAgents) { |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 536 | List<PersistableBundle> config = |
| 537 | dpm.getTrustAgentConfiguration(null /* admin */, name, userInfo.id); |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 538 | // Disable agent if no features are enabled. |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 539 | if (config == null || config.isEmpty()) { |
| 540 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping " |
| 541 | + name.flattenToShortString() + " u"+ userInfo.id |
| 542 | + ": not allowed by DPM"); |
| 543 | continue; |
| 544 | } |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 545 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 546 | AgentInfo agentInfo = new AgentInfo(); |
| 547 | agentInfo.component = name; |
| 548 | agentInfo.userId = userInfo.id; |
| 549 | if (!mActiveAgents.contains(agentInfo)) { |
| 550 | agentInfo.label = resolveInfo.loadLabel(pm); |
| 551 | agentInfo.icon = resolveInfo.loadIcon(pm); |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 552 | agentInfo.settings = getSettingsAttrs(pm, resolveInfo); |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 553 | } else { |
| 554 | int index = mActiveAgents.indexOf(agentInfo); |
| 555 | agentInfo = mActiveAgents.valueAt(index); |
| 556 | } |
| 557 | |
Vishwath Mohan | 60ea553 | 2019-05-25 12:23:02 -0700 | [diff] [blame] | 558 | boolean directUnlock = false; |
| 559 | if (agentInfo.settings != null) { |
| 560 | directUnlock = resolveInfo.serviceInfo.directBootAware |
| 561 | && agentInfo.settings.canUnlockProfile; |
| 562 | } |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 563 | |
| 564 | if (directUnlock) { |
| 565 | if (DEBUG) Slog.d(TAG, "refreshAgentList: trustagent " + name |
| 566 | + "of user " + userInfo.id + "can unlock user profile."); |
| 567 | } |
| 568 | |
Jeff Sharkey | a65e649 | 2017-06-21 13:45:11 -0600 | [diff] [blame] | 569 | if (!mUserManager.isUserUnlockingOrUnlocked(userInfo.id) |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 570 | && !directUnlock) { |
| 571 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 572 | + "'s trust agent " + name + ": FBE still locked and " |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 573 | + " the agent cannot unlock user profile."); |
| 574 | continue; |
| 575 | } |
| 576 | |
| 577 | if (!mStrongAuthTracker.canAgentsRunForUser(userInfo.id)) { |
| 578 | int flag = mStrongAuthTracker.getStrongAuthForUser(userInfo.id); |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 579 | if (flag != StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT) { |
| 580 | if (flag != StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT |
| 581 | || !directUnlock) { |
| 582 | if (DEBUG) |
| 583 | Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id |
| 584 | + ": prevented by StrongAuthTracker = 0x" |
| 585 | + Integer.toHexString(mStrongAuthTracker.getStrongAuthForUser( |
| 586 | userInfo.id))); |
| 587 | continue; |
| 588 | } |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
Adrian Roos | b884bb2 | 2017-05-03 11:33:51 -0700 | [diff] [blame] | 592 | if (agentInfo.agent == null) { |
| 593 | agentInfo.agent = new TrustAgentWrapper(mContext, this, |
| 594 | new Intent().setComponent(name), userInfo.getUserHandle()); |
| 595 | } |
| 596 | |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 597 | if (!mActiveAgents.contains(agentInfo)) { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 598 | mActiveAgents.add(agentInfo); |
| 599 | } else { |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 600 | obsoleteAgents.remove(agentInfo); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | boolean trustMayHaveChanged = false; |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 606 | for (int i = 0; i < obsoleteAgents.size(); i++) { |
| 607 | AgentInfo info = obsoleteAgents.valueAt(i); |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 608 | if (userIdOrAll == UserHandle.USER_ALL || userIdOrAll == info.userId) { |
Adrian Roos | e681c27 | 2014-09-08 14:03:47 +0200 | [diff] [blame] | 609 | if (info.agent.isManagingTrust()) { |
| 610 | trustMayHaveChanged = true; |
| 611 | } |
Adrian Roos | fc29e0b | 2014-11-11 12:55:44 +0100 | [diff] [blame] | 612 | info.agent.destroy(); |
Adrian Roos | e681c27 | 2014-09-08 14:03:47 +0200 | [diff] [blame] | 613 | mActiveAgents.remove(info); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 614 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | if (trustMayHaveChanged) { |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 618 | if (userIdOrAll == UserHandle.USER_ALL) { |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 619 | updateTrustAll(); |
| 620 | } else { |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 621 | updateTrust(userIdOrAll, 0); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 622 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 626 | boolean isDeviceLockedInner(int userId) { |
| 627 | synchronized (mDeviceLockedForUser) { |
| 628 | return mDeviceLockedForUser.get(userId, true); |
| 629 | } |
| 630 | } |
| 631 | |
Rubin Xu | c398627 | 2019-11-05 10:15:36 +0000 | [diff] [blame^] | 632 | /** |
| 633 | * Update the user's locked state. Only applicable to users with a real keyguard |
| 634 | * ({@link UserInfo#supportsSwitchToByUser}) and unsecured managed profiles. |
| 635 | */ |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 636 | private void refreshDeviceLockedForUser(int userId) { |
Xiaohui Chen | 09e0291 | 2015-08-05 09:44:56 -0700 | [diff] [blame] | 637 | if (userId != UserHandle.USER_ALL && userId < UserHandle.USER_SYSTEM) { |
Adrian Roos | 7e2e40e | 2014-11-21 15:27:12 +0100 | [diff] [blame] | 638 | Log.e(TAG, "refreshDeviceLockedForUser(userId=" + userId + "): Invalid user handle," |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 639 | + " must be USER_ALL or a specific user.", new Throwable("here")); |
| 640 | userId = UserHandle.USER_ALL; |
| 641 | } |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 642 | List<UserInfo> userInfos; |
| 643 | if (userId == UserHandle.USER_ALL) { |
| 644 | userInfos = mUserManager.getUsers(true /* excludeDying */); |
| 645 | } else { |
| 646 | userInfos = new ArrayList<>(); |
| 647 | userInfos.add(mUserManager.getUserInfo(userId)); |
| 648 | } |
| 649 | |
| 650 | IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); |
| 651 | |
| 652 | for (int i = 0; i < userInfos.size(); i++) { |
| 653 | UserInfo info = userInfos.get(i); |
| 654 | |
Pavel Grafov | 259ca36 | 2018-07-27 14:04:57 +0100 | [diff] [blame] | 655 | if (info == null || info.partial || !info.isEnabled() || info.guestToRemove) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 656 | continue; |
| 657 | } |
| 658 | |
| 659 | int id = info.id; |
| 660 | boolean secure = mLockPatternUtils.isSecure(id); |
Pavel Grafov | 259ca36 | 2018-07-27 14:04:57 +0100 | [diff] [blame] | 661 | |
| 662 | if (!info.supportsSwitchToByUser()) { |
| 663 | if (info.isManagedProfile() && !secure) { |
| 664 | setDeviceLockedForUser(id, false); |
| 665 | } |
| 666 | continue; |
| 667 | } |
| 668 | |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 669 | boolean trusted = aggregateIsTrusted(id); |
| 670 | boolean showingKeyguard = true; |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 671 | boolean biometricAuthenticated = false; |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 672 | |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 673 | if (mCurrentUser == id) { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 674 | synchronized(mUsersUnlockedByBiometric) { |
| 675 | biometricAuthenticated = mUsersUnlockedByBiometric.get(id, false); |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 676 | } |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 677 | try { |
| 678 | showingKeyguard = wm.isKeyguardLocked(); |
| 679 | } catch (RemoteException e) { |
| 680 | } |
| 681 | } |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 682 | boolean deviceLocked = secure && showingKeyguard && !trusted && |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 683 | !biometricAuthenticated; |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 684 | setDeviceLockedForUser(id, deviceLocked); |
| 685 | } |
| 686 | } |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 687 | |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 688 | private void setDeviceLockedForUser(@UserIdInt int userId, boolean locked) { |
| 689 | final boolean changed; |
| 690 | synchronized (mDeviceLockedForUser) { |
| 691 | changed = isDeviceLockedInner(userId) != locked; |
| 692 | mDeviceLockedForUser.put(userId, locked); |
| 693 | } |
| 694 | if (changed) { |
| 695 | dispatchDeviceLocked(userId, locked); |
Rubin Xu | c398627 | 2019-11-05 10:15:36 +0000 | [diff] [blame^] | 696 | |
| 697 | KeyStore.getInstance().onUserLockedStateChanged(userId, locked); |
| 698 | // Also update the user's profiles who have unified challenge, since they |
| 699 | // share the same unlocked state (see {@link #isDeviceLocked(int)}) |
| 700 | for (int profileHandle : mUserManager.getEnabledProfileIds(userId)) { |
| 701 | if (mLockPatternUtils.isManagedProfileWithUnifiedChallenge(profileHandle)) { |
| 702 | KeyStore.getInstance().onUserLockedStateChanged(profileHandle, locked); |
| 703 | } |
| 704 | } |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
| 708 | private void dispatchDeviceLocked(int userId, boolean isLocked) { |
| 709 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 710 | AgentInfo agent = mActiveAgents.valueAt(i); |
| 711 | if (agent.userId == userId) { |
| 712 | if (isLocked) { |
| 713 | agent.agent.onDeviceLocked(); |
| 714 | } else{ |
| 715 | agent.agent.onDeviceUnlocked(); |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
Ram Periathiruvadi | 32d5355 | 2019-02-19 13:25:46 -0800 | [diff] [blame] | 721 | private void dispatchEscrowTokenActivatedLocked(long handle, int userId) { |
| 722 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 723 | AgentInfo agent = mActiveAgents.valueAt(i); |
| 724 | if (agent.userId == userId) { |
| 725 | agent.agent.onEscrowTokenActivated(handle, userId); |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 730 | void updateDevicePolicyFeatures() { |
Adrian Roos | 9d6fc92 | 2016-08-10 17:09:55 -0700 | [diff] [blame] | 731 | boolean changed = false; |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 732 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 733 | AgentInfo info = mActiveAgents.valueAt(i); |
| 734 | if (info.agent.isConnected()) { |
| 735 | info.agent.updateDevicePolicyFeatures(); |
Adrian Roos | 9d6fc92 | 2016-08-10 17:09:55 -0700 | [diff] [blame] | 736 | changed = true; |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 737 | } |
| 738 | } |
Adrian Roos | 9d6fc92 | 2016-08-10 17:09:55 -0700 | [diff] [blame] | 739 | if (changed) { |
| 740 | mArchive.logDevicePolicyChanged(); |
| 741 | } |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 742 | } |
| 743 | |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 744 | private void removeAgentsOfPackage(String packageName) { |
| 745 | boolean trustMayHaveChanged = false; |
| 746 | for (int i = mActiveAgents.size() - 1; i >= 0; i--) { |
| 747 | AgentInfo info = mActiveAgents.valueAt(i); |
| 748 | if (packageName.equals(info.component.getPackageName())) { |
| 749 | Log.i(TAG, "Resetting agent " + info.component.flattenToShortString()); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 750 | if (info.agent.isManagingTrust()) { |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 751 | trustMayHaveChanged = true; |
| 752 | } |
Adrian Roos | fc29e0b | 2014-11-11 12:55:44 +0100 | [diff] [blame] | 753 | info.agent.destroy(); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 754 | mActiveAgents.removeAt(i); |
| 755 | } |
| 756 | } |
| 757 | if (trustMayHaveChanged) { |
| 758 | updateTrustAll(); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | public void resetAgent(ComponentName name, int userId) { |
| 763 | boolean trustMayHaveChanged = false; |
| 764 | for (int i = mActiveAgents.size() - 1; i >= 0; i--) { |
| 765 | AgentInfo info = mActiveAgents.valueAt(i); |
| 766 | if (name.equals(info.component) && userId == info.userId) { |
| 767 | Log.i(TAG, "Resetting agent " + info.component.flattenToShortString()); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 768 | if (info.agent.isManagingTrust()) { |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 769 | trustMayHaveChanged = true; |
| 770 | } |
Adrian Roos | fc29e0b | 2014-11-11 12:55:44 +0100 | [diff] [blame] | 771 | info.agent.destroy(); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 772 | mActiveAgents.removeAt(i); |
| 773 | } |
| 774 | } |
| 775 | if (trustMayHaveChanged) { |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 776 | updateTrust(userId, 0); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 777 | } |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 778 | refreshAgentList(userId); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 779 | } |
| 780 | |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 781 | private SettingsAttrs getSettingsAttrs(PackageManager pm, ResolveInfo resolveInfo) { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 782 | if (resolveInfo == null || resolveInfo.serviceInfo == null |
| 783 | || resolveInfo.serviceInfo.metaData == null) return null; |
| 784 | String cn = null; |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 785 | boolean canUnlockProfile = false; |
| 786 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 787 | XmlResourceParser parser = null; |
| 788 | Exception caughtException = null; |
| 789 | try { |
| 790 | parser = resolveInfo.serviceInfo.loadXmlMetaData(pm, |
| 791 | TrustAgentService.TRUST_AGENT_META_DATA); |
| 792 | if (parser == null) { |
| 793 | Slog.w(TAG, "Can't find " + TrustAgentService.TRUST_AGENT_META_DATA + " meta-data"); |
| 794 | return null; |
| 795 | } |
| 796 | Resources res = pm.getResourcesForApplication(resolveInfo.serviceInfo.applicationInfo); |
| 797 | AttributeSet attrs = Xml.asAttributeSet(parser); |
| 798 | int type; |
| 799 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 800 | && type != XmlPullParser.START_TAG) { |
| 801 | // Drain preamble. |
| 802 | } |
| 803 | String nodeName = parser.getName(); |
Adrian Roos | 7e03dfc | 2014-05-16 16:06:28 +0200 | [diff] [blame] | 804 | if (!"trust-agent".equals(nodeName)) { |
| 805 | Slog.w(TAG, "Meta-data does not start with trust-agent tag"); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 806 | return null; |
| 807 | } |
| 808 | TypedArray sa = res |
| 809 | .obtainAttributes(attrs, com.android.internal.R.styleable.TrustAgent); |
| 810 | cn = sa.getString(com.android.internal.R.styleable.TrustAgent_settingsActivity); |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 811 | canUnlockProfile = sa.getBoolean( |
| 812 | com.android.internal.R.styleable.TrustAgent_unlockProfile, false); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 813 | sa.recycle(); |
| 814 | } catch (PackageManager.NameNotFoundException e) { |
| 815 | caughtException = e; |
| 816 | } catch (IOException e) { |
| 817 | caughtException = e; |
| 818 | } catch (XmlPullParserException e) { |
| 819 | caughtException = e; |
| 820 | } finally { |
| 821 | if (parser != null) parser.close(); |
| 822 | } |
| 823 | if (caughtException != null) { |
| 824 | Slog.w(TAG, "Error parsing : " + resolveInfo.serviceInfo.packageName, caughtException); |
| 825 | return null; |
| 826 | } |
| 827 | if (cn == null) { |
| 828 | return null; |
| 829 | } |
| 830 | if (cn.indexOf('/') < 0) { |
| 831 | cn = resolveInfo.serviceInfo.packageName + "/" + cn; |
| 832 | } |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 833 | return new SettingsAttrs(ComponentName.unflattenFromString(cn), canUnlockProfile); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | private ComponentName getComponentName(ResolveInfo resolveInfo) { |
| 837 | if (resolveInfo == null || resolveInfo.serviceInfo == null) return null; |
| 838 | return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name); |
| 839 | } |
| 840 | |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 841 | private void maybeEnableFactoryTrustAgents(LockPatternUtils utils, int userId) { |
Adrian Roos | 2f19ad4 | 2017-07-05 16:09:22 +0200 | [diff] [blame] | 842 | if (0 != Settings.Secure.getIntForUser(mContext.getContentResolver(), |
| 843 | Settings.Secure.TRUST_AGENTS_INITIALIZED, 0, userId)) { |
| 844 | return; |
| 845 | } |
| 846 | PackageManager pm = mContext.getPackageManager(); |
| 847 | List<ResolveInfo> resolveInfos = resolveAllowedTrustAgents(pm, userId); |
Zachary Iqbal | 666484d | 2017-03-31 19:10:09 -0700 | [diff] [blame] | 848 | ComponentName defaultAgent = getDefaultFactoryTrustAgent(mContext); |
| 849 | boolean shouldUseDefaultAgent = defaultAgent != null; |
Adrian Roos | 2f19ad4 | 2017-07-05 16:09:22 +0200 | [diff] [blame] | 850 | ArraySet<ComponentName> discoveredAgents = new ArraySet<>(); |
Zachary Iqbal | 666484d | 2017-03-31 19:10:09 -0700 | [diff] [blame] | 851 | |
| 852 | if (shouldUseDefaultAgent) { |
Adrian Roos | 2f19ad4 | 2017-07-05 16:09:22 +0200 | [diff] [blame] | 853 | discoveredAgents.add(defaultAgent); |
Zachary Iqbal | 666484d | 2017-03-31 19:10:09 -0700 | [diff] [blame] | 854 | Log.i(TAG, "Enabling " + defaultAgent + " because it is a default agent."); |
| 855 | } else { // A default agent is not set; perform regular trust agent discovery |
| 856 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 857 | ComponentName componentName = getComponentName(resolveInfo); |
| 858 | int applicationInfoFlags = resolveInfo.serviceInfo.applicationInfo.flags; |
| 859 | if ((applicationInfoFlags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 860 | Log.i(TAG, "Leaving agent " + componentName + " disabled because package " |
| 861 | + "is not a system package."); |
| 862 | continue; |
| 863 | } |
| 864 | discoveredAgents.add(componentName); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 865 | } |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 866 | } |
Adrian Roos | 2f19ad4 | 2017-07-05 16:09:22 +0200 | [diff] [blame] | 867 | |
| 868 | List<ComponentName> previouslyEnabledAgents = utils.getEnabledTrustAgents(userId); |
| 869 | if (previouslyEnabledAgents != null) { |
| 870 | discoveredAgents.addAll(previouslyEnabledAgents); |
| 871 | } |
| 872 | utils.setEnabledTrustAgents(discoveredAgents, userId); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 873 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 874 | Settings.Secure.TRUST_AGENTS_INITIALIZED, 1, userId); |
| 875 | } |
| 876 | |
Zachary Iqbal | 666484d | 2017-03-31 19:10:09 -0700 | [diff] [blame] | 877 | /** |
| 878 | * Returns the {@link ComponentName} for the default trust agent, or {@code null} if there |
| 879 | * is no trust agent set. |
| 880 | */ |
| 881 | private static ComponentName getDefaultFactoryTrustAgent(Context context) { |
| 882 | String defaultTrustAgent = context.getResources() |
| 883 | .getString(com.android.internal.R.string.config_defaultTrustAgent); |
| 884 | if (TextUtils.isEmpty(defaultTrustAgent)) { |
| 885 | return null; |
| 886 | } |
| 887 | return ComponentName.unflattenFromString(defaultTrustAgent); |
| 888 | } |
| 889 | |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 890 | private List<ResolveInfo> resolveAllowedTrustAgents(PackageManager pm, int userId) { |
| 891 | List<ResolveInfo> resolveInfos = pm.queryIntentServicesAsUser(TRUST_AGENT_INTENT, |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 892 | PackageManager.GET_META_DATA | |
Adrian Roos | 68771eb | 2016-05-03 14:02:36 -0700 | [diff] [blame] | 893 | PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
| 894 | userId); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 895 | ArrayList<ResolveInfo> allowedAgents = new ArrayList<>(resolveInfos.size()); |
| 896 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 897 | if (resolveInfo.serviceInfo == null) continue; |
| 898 | if (resolveInfo.serviceInfo.applicationInfo == null) continue; |
| 899 | String packageName = resolveInfo.serviceInfo.packageName; |
| 900 | if (pm.checkPermission(PERMISSION_PROVIDE_AGENT, packageName) |
| 901 | != PackageManager.PERMISSION_GRANTED) { |
| 902 | ComponentName name = getComponentName(resolveInfo); |
| 903 | Log.w(TAG, "Skipping agent " + name + " because package does not have" |
| 904 | + " permission " + PERMISSION_PROVIDE_AGENT + "."); |
| 905 | continue; |
| 906 | } |
| 907 | allowedAgents.add(resolveInfo); |
| 908 | } |
| 909 | return allowedAgents; |
| 910 | } |
| 911 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 912 | // Agent dispatch and aggregation |
| 913 | |
| 914 | private boolean aggregateIsTrusted(int userId) { |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 915 | if (!mStrongAuthTracker.isTrustAllowedForUser(userId)) { |
Adrian Roos | 7046bfd | 2014-05-16 21:20:54 +0200 | [diff] [blame] | 916 | return false; |
| 917 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 918 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 919 | AgentInfo info = mActiveAgents.valueAt(i); |
| 920 | if (info.userId == userId) { |
| 921 | if (info.agent.isTrusted()) { |
| 922 | return true; |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | return false; |
| 927 | } |
| 928 | |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 929 | private boolean aggregateIsTrustManaged(int userId) { |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 930 | if (!mStrongAuthTracker.isTrustAllowedForUser(userId)) { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 931 | return false; |
| 932 | } |
| 933 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 934 | AgentInfo info = mActiveAgents.valueAt(i); |
| 935 | if (info.userId == userId) { |
| 936 | if (info.agent.isManagingTrust()) { |
| 937 | return true; |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | return false; |
| 942 | } |
| 943 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 944 | private void dispatchUnlockAttempt(boolean successful, int userId) { |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 945 | if (successful) { |
| 946 | mStrongAuthTracker.allowTrustFromUnlock(userId); |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 947 | // Allow the presence of trust on a successful unlock attempt to extend unlock. |
| 948 | updateTrust(userId, 0 /* flags */, true); |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 949 | } |
| 950 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 951 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 952 | AgentInfo info = mActiveAgents.valueAt(i); |
| 953 | if (info.userId == userId) { |
| 954 | info.agent.onUnlockAttempt(successful); |
| 955 | } |
| 956 | } |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 957 | } |
| 958 | |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 959 | private void dispatchUnlockLockout(int timeoutMs, int userId) { |
| 960 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 961 | AgentInfo info = mActiveAgents.valueAt(i); |
| 962 | if (info.userId == userId) { |
| 963 | info.agent.onUnlockLockout(timeoutMs); |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 968 | // Listeners |
| 969 | |
| 970 | private void addListener(ITrustListener listener) { |
| 971 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 972 | if (mTrustListeners.get(i).asBinder() == listener.asBinder()) { |
| 973 | return; |
| 974 | } |
| 975 | } |
| 976 | mTrustListeners.add(listener); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 977 | updateTrustAll(); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | private void removeListener(ITrustListener listener) { |
| 981 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 982 | if (mTrustListeners.get(i).asBinder() == listener.asBinder()) { |
Jay Civelli | 979a32e | 2014-07-18 16:47:36 -0700 | [diff] [blame] | 983 | mTrustListeners.remove(i); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 984 | return; |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 989 | private void dispatchOnTrustChanged(boolean enabled, int userId, int flags) { |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 990 | if (DEBUG) { |
| 991 | Log.i(TAG, "onTrustChanged(" + enabled + ", " + userId + ", 0x" |
| 992 | + Integer.toHexString(flags) + ")"); |
| 993 | } |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 994 | if (!enabled) flags = 0; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 995 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 996 | try { |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 997 | mTrustListeners.get(i).onTrustChanged(enabled, userId, flags); |
Adrian Roos | a4ba56b | 2014-05-20 12:56:25 +0200 | [diff] [blame] | 998 | } catch (DeadObjectException e) { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 999 | Slog.d(TAG, "Removing dead TrustListener."); |
| 1000 | mTrustListeners.remove(i); |
| 1001 | i--; |
| 1002 | } catch (RemoteException e) { |
| 1003 | Slog.e(TAG, "Exception while notifying TrustListener.", e); |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | private void dispatchOnTrustManagedChanged(boolean managed, int userId) { |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 1009 | if (DEBUG) { |
| 1010 | Log.i(TAG, "onTrustManagedChanged(" + managed + ", " + userId + ")"); |
| 1011 | } |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 1012 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 1013 | try { |
| 1014 | mTrustListeners.get(i).onTrustManagedChanged(managed, userId); |
| 1015 | } catch (DeadObjectException e) { |
| 1016 | Slog.d(TAG, "Removing dead TrustListener."); |
Adrian Roos | a4ba56b | 2014-05-20 12:56:25 +0200 | [diff] [blame] | 1017 | mTrustListeners.remove(i); |
| 1018 | i--; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1019 | } catch (RemoteException e) { |
Adrian Roos | a4ba56b | 2014-05-20 12:56:25 +0200 | [diff] [blame] | 1020 | Slog.e(TAG, "Exception while notifying TrustListener.", e); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 1025 | private void dispatchOnTrustError(CharSequence message) { |
| 1026 | if (DEBUG) { |
| 1027 | Log.i(TAG, "onTrustError(" + message + ")"); |
| 1028 | } |
| 1029 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 1030 | try { |
| 1031 | mTrustListeners.get(i).onTrustError(message); |
| 1032 | } catch (DeadObjectException e) { |
| 1033 | Slog.d(TAG, "Removing dead TrustListener."); |
| 1034 | mTrustListeners.remove(i); |
| 1035 | i--; |
| 1036 | } catch (RemoteException e) { |
| 1037 | Slog.e(TAG, "Exception while notifying TrustListener.", e); |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1042 | // User lifecycle |
| 1043 | |
| 1044 | @Override |
| 1045 | public void onStartUser(int userId) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1046 | mHandler.obtainMessage(MSG_START_USER, userId, 0, null).sendToTarget(); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | @Override |
| 1050 | public void onCleanupUser(int userId) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1051 | mHandler.obtainMessage(MSG_CLEANUP_USER, userId, 0, null).sendToTarget(); |
| 1052 | } |
| 1053 | |
| 1054 | @Override |
| 1055 | public void onSwitchUser(int userId) { |
| 1056 | mHandler.obtainMessage(MSG_SWITCH_USER, userId, 0, null).sendToTarget(); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1057 | } |
| 1058 | |
Adrian Roos | 68771eb | 2016-05-03 14:02:36 -0700 | [diff] [blame] | 1059 | @Override |
| 1060 | public void onUnlockUser(int userId) { |
| 1061 | mHandler.obtainMessage(MSG_UNLOCK_USER, userId, 0, null).sendToTarget(); |
| 1062 | } |
| 1063 | |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 1064 | @Override |
| 1065 | public void onStopUser(@UserIdInt int userId) { |
| 1066 | mHandler.obtainMessage(MSG_STOP_USER, userId, 0, null).sendToTarget(); |
| 1067 | } |
| 1068 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1069 | // Plumbing |
| 1070 | |
| 1071 | private final IBinder mService = new ITrustManager.Stub() { |
| 1072 | @Override |
| 1073 | public void reportUnlockAttempt(boolean authenticated, int userId) throws RemoteException { |
| 1074 | enforceReportPermission(); |
| 1075 | mHandler.obtainMessage(MSG_DISPATCH_UNLOCK_ATTEMPT, authenticated ? 1 : 0, userId) |
| 1076 | .sendToTarget(); |
| 1077 | } |
| 1078 | |
| 1079 | @Override |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 1080 | public void reportUnlockLockout(int timeoutMs, int userId) throws RemoteException { |
| 1081 | enforceReportPermission(); |
| 1082 | mHandler.obtainMessage(MSG_DISPATCH_UNLOCK_LOCKOUT, timeoutMs, userId) |
| 1083 | .sendToTarget(); |
| 1084 | } |
| 1085 | |
| 1086 | @Override |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1087 | public void reportEnabledTrustAgentsChanged(int userId) throws RemoteException { |
| 1088 | enforceReportPermission(); |
| 1089 | // coalesce refresh messages. |
| 1090 | mHandler.removeMessages(MSG_ENABLED_AGENTS_CHANGED); |
| 1091 | mHandler.sendEmptyMessage(MSG_ENABLED_AGENTS_CHANGED); |
| 1092 | } |
| 1093 | |
| 1094 | @Override |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1095 | public void reportKeyguardShowingChanged() throws RemoteException { |
| 1096 | enforceReportPermission(); |
| 1097 | // coalesce refresh messages. |
| 1098 | mHandler.removeMessages(MSG_KEYGUARD_SHOWING_CHANGED); |
| 1099 | mHandler.sendEmptyMessage(MSG_KEYGUARD_SHOWING_CHANGED); |
Jorim Jaggi | 5277dea | 2017-05-18 02:05:29 +0200 | [diff] [blame] | 1100 | |
| 1101 | // Make sure handler processes the message before returning, such that isDeviceLocked |
| 1102 | // after this call will retrieve the correct value. |
| 1103 | mHandler.runWithScissors(() -> {}, 0); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | @Override |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1107 | public void registerTrustListener(ITrustListener trustListener) throws RemoteException { |
| 1108 | enforceListenerPermission(); |
| 1109 | mHandler.obtainMessage(MSG_REGISTER_LISTENER, trustListener).sendToTarget(); |
| 1110 | } |
| 1111 | |
| 1112 | @Override |
| 1113 | public void unregisterTrustListener(ITrustListener trustListener) throws RemoteException { |
| 1114 | enforceListenerPermission(); |
| 1115 | mHandler.obtainMessage(MSG_UNREGISTER_LISTENER, trustListener).sendToTarget(); |
| 1116 | } |
| 1117 | |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 1118 | @Override |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 1119 | public boolean isDeviceLocked(int userId) throws RemoteException { |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 1120 | userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId, |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 1121 | false /* allowAll */, true /* requireFull */, "isDeviceLocked", null); |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 1122 | |
Clara Bayarri | 078e91b | 2016-01-15 16:57:35 +0000 | [diff] [blame] | 1123 | long token = Binder.clearCallingIdentity(); |
| 1124 | try { |
| 1125 | if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) { |
| 1126 | userId = resolveProfileParent(userId); |
| 1127 | } |
| 1128 | return isDeviceLockedInner(userId); |
| 1129 | } finally { |
| 1130 | Binder.restoreCallingIdentity(token); |
| 1131 | } |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 1134 | @Override |
| 1135 | public boolean isDeviceSecure(int userId) throws RemoteException { |
| 1136 | userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId, |
| 1137 | false /* allowAll */, true /* requireFull */, "isDeviceSecure", null); |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 1138 | |
| 1139 | long token = Binder.clearCallingIdentity(); |
| 1140 | try { |
Clara Bayarri | 8d35de8 | 2016-01-12 17:29:29 +0000 | [diff] [blame] | 1141 | if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) { |
| 1142 | userId = resolveProfileParent(userId); |
| 1143 | } |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 1144 | return mLockPatternUtils.isSecure(userId); |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 1145 | } finally { |
| 1146 | Binder.restoreCallingIdentity(token); |
| 1147 | } |
| 1148 | } |
| 1149 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1150 | private void enforceReportPermission() { |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 1151 | mContext.enforceCallingOrSelfPermission( |
| 1152 | Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE, "reporting trust events"); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | private void enforceListenerPermission() { |
| 1156 | mContext.enforceCallingPermission(Manifest.permission.TRUST_LISTENER, |
| 1157 | "register trust listener"); |
| 1158 | } |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 1159 | |
| 1160 | @Override |
| 1161 | protected void dump(FileDescriptor fd, final PrintWriter fout, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1162 | if (!DumpUtils.checkDumpPermission(mContext, TAG, fout)) return; |
Adrian Roos | 49d5345 | 2014-10-24 15:48:39 +0200 | [diff] [blame] | 1163 | if (isSafeMode()) { |
| 1164 | fout.println("disabled because the system is in safe mode."); |
| 1165 | return; |
| 1166 | } |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1167 | if (!mTrustAgentsCanRun) { |
| 1168 | fout.println("disabled because the third-party apps can't run yet."); |
| 1169 | return; |
| 1170 | } |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 1171 | final List<UserInfo> userInfos = mUserManager.getUsers(true /* excludeDying */); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 1172 | mHandler.runWithScissors(new Runnable() { |
| 1173 | @Override |
| 1174 | public void run() { |
| 1175 | fout.println("Trust manager state:"); |
| 1176 | for (UserInfo user : userInfos) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1177 | dumpUser(fout, user, user.id == mCurrentUser); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | }, 1500); |
| 1181 | } |
| 1182 | |
| 1183 | private void dumpUser(PrintWriter fout, UserInfo user, boolean isCurrent) { |
| 1184 | fout.printf(" User \"%s\" (id=%d, flags=%#x)", |
| 1185 | user.name, user.id, user.flags); |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 1186 | if (!user.supportsSwitchToByUser()) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1187 | fout.println("(managed profile)"); |
| 1188 | fout.println(" disabled because switching to this user is not possible."); |
| 1189 | return; |
| 1190 | } |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 1191 | if (isCurrent) { |
| 1192 | fout.print(" (current)"); |
| 1193 | } |
| 1194 | fout.print(": trusted=" + dumpBool(aggregateIsTrusted(user.id))); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 1195 | fout.print(", trustManaged=" + dumpBool(aggregateIsTrustManaged(user.id))); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1196 | fout.print(", deviceLocked=" + dumpBool(isDeviceLockedInner(user.id))); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1197 | fout.print(", strongAuthRequired=" + dumpHex( |
| 1198 | mStrongAuthTracker.getStrongAuthForUser(user.id))); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 1199 | fout.println(); |
| 1200 | fout.println(" Enabled agents:"); |
| 1201 | boolean duplicateSimpleNames = false; |
| 1202 | ArraySet<String> simpleNames = new ArraySet<String>(); |
| 1203 | for (AgentInfo info : mActiveAgents) { |
| 1204 | if (info.userId != user.id) { continue; } |
| 1205 | boolean trusted = info.agent.isTrusted(); |
| 1206 | fout.print(" "); fout.println(info.component.flattenToShortString()); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 1207 | fout.print(" bound=" + dumpBool(info.agent.isBound())); |
| 1208 | fout.print(", connected=" + dumpBool(info.agent.isConnected())); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 1209 | fout.print(", managingTrust=" + dumpBool(info.agent.isManagingTrust())); |
| 1210 | fout.print(", trusted=" + dumpBool(trusted)); |
| 1211 | fout.println(); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 1212 | if (trusted) { |
| 1213 | fout.println(" message=\"" + info.agent.getMessage() + "\""); |
| 1214 | } |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 1215 | if (!info.agent.isConnected()) { |
| 1216 | String restartTime = TrustArchive.formatDuration( |
| 1217 | info.agent.getScheduledRestartUptimeMillis() |
| 1218 | - SystemClock.uptimeMillis()); |
| 1219 | fout.println(" restartScheduledAt=" + restartTime); |
| 1220 | } |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 1221 | if (!simpleNames.add(TrustArchive.getSimpleName(info.component))) { |
| 1222 | duplicateSimpleNames = true; |
| 1223 | } |
| 1224 | } |
| 1225 | fout.println(" Events:"); |
| 1226 | mArchive.dump(fout, 50, user.id, " " /* linePrefix */, duplicateSimpleNames); |
| 1227 | fout.println(); |
| 1228 | } |
| 1229 | |
| 1230 | private String dumpBool(boolean b) { |
| 1231 | return b ? "1" : "0"; |
| 1232 | } |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1233 | |
| 1234 | private String dumpHex(int i) { |
| 1235 | return "0x" + Integer.toHexString(i); |
| 1236 | } |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 1237 | |
Rubin Xu | c398627 | 2019-11-05 10:15:36 +0000 | [diff] [blame^] | 1238 | /** |
| 1239 | * Changes the lock status for the given user. This is only applicable to managed profiles, |
| 1240 | * other users should be handled by Keyguard. |
| 1241 | */ |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 1242 | @Override |
Rubin Xu | 83a15bc | 2016-08-10 10:53:24 +0100 | [diff] [blame] | 1243 | public void setDeviceLockedForUser(int userId, boolean locked) { |
Clara Bayarri | 00a9b89 | 2016-01-13 16:17:09 +0000 | [diff] [blame] | 1244 | enforceReportPermission(); |
Rubin Xu | e078133 | 2016-08-19 11:20:34 +0100 | [diff] [blame] | 1245 | final long identity = Binder.clearCallingIdentity(); |
| 1246 | try { |
Pavel Grafov | 259ca36 | 2018-07-27 14:04:57 +0100 | [diff] [blame] | 1247 | if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userId) |
| 1248 | && mLockPatternUtils.isSecure(userId)) { |
Rubin Xu | e078133 | 2016-08-19 11:20:34 +0100 | [diff] [blame] | 1249 | synchronized (mDeviceLockedForUser) { |
| 1250 | mDeviceLockedForUser.put(userId, locked); |
| 1251 | } |
Rubin Xu | c398627 | 2019-11-05 10:15:36 +0000 | [diff] [blame^] | 1252 | |
| 1253 | KeyStore.getInstance().onUserLockedStateChanged(userId, locked); |
| 1254 | |
Rubin Xu | e078133 | 2016-08-19 11:20:34 +0100 | [diff] [blame] | 1255 | if (locked) { |
| 1256 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1257 | ActivityManager.getService().notifyLockedProfile(userId); |
Rubin Xu | e078133 | 2016-08-19 11:20:34 +0100 | [diff] [blame] | 1258 | } catch (RemoteException e) { |
| 1259 | } |
Rubin Xu | 83a15bc | 2016-08-10 10:53:24 +0100 | [diff] [blame] | 1260 | } |
Robin Lee | 92b83c6 | 2016-08-31 13:27:51 +0100 | [diff] [blame] | 1261 | final Intent lockIntent = new Intent(Intent.ACTION_DEVICE_LOCKED_CHANGED); |
| 1262 | lockIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 1263 | lockIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1264 | mContext.sendBroadcastAsUser(lockIntent, UserHandle.SYSTEM, |
| 1265 | Manifest.permission.TRUST_LISTENER, /* options */ null); |
Rubin Xu | 83a15bc | 2016-08-10 10:53:24 +0100 | [diff] [blame] | 1266 | } |
Rubin Xu | e078133 | 2016-08-19 11:20:34 +0100 | [diff] [blame] | 1267 | } finally { |
| 1268 | Binder.restoreCallingIdentity(identity); |
Rubin Xu | 83a15bc | 2016-08-10 10:53:24 +0100 | [diff] [blame] | 1269 | } |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 1270 | } |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 1271 | |
| 1272 | @Override |
| 1273 | public boolean isTrustUsuallyManaged(int userId) { |
| 1274 | mContext.enforceCallingPermission(Manifest.permission.TRUST_LISTENER, |
| 1275 | "query trust state"); |
| 1276 | return isTrustUsuallyManagedInternal(userId); |
| 1277 | } |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1278 | |
| 1279 | @Override |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1280 | public void unlockedByBiometricForUser(int userId, BiometricSourceType biometricSource) { |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1281 | enforceReportPermission(); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1282 | synchronized(mUsersUnlockedByBiometric) { |
| 1283 | mUsersUnlockedByBiometric.put(userId, true); |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1284 | } |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 1285 | // In extend unlock mode we need to refresh trust state here, which will call |
| 1286 | // refreshDeviceLockedForUser() |
| 1287 | int updateTrustOnUnlock = mSettingsObserver.getTrustAgentsExtendUnlock() ? 1 : 0; |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1288 | mHandler.obtainMessage(MSG_REFRESH_DEVICE_LOCKED_FOR_USER, userId, |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 1289 | updateTrustOnUnlock).sendToTarget(); |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | @Override |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1293 | public void clearAllBiometricRecognized(BiometricSourceType biometricSource) { |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1294 | enforceReportPermission(); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1295 | synchronized(mUsersUnlockedByBiometric) { |
| 1296 | mUsersUnlockedByBiometric.clear(); |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1297 | } |
| 1298 | mHandler.obtainMessage(MSG_REFRESH_DEVICE_LOCKED_FOR_USER, UserHandle.USER_ALL, |
| 1299 | 0 /* arg2 */).sendToTarget(); |
| 1300 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1301 | }; |
| 1302 | |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 1303 | private boolean isTrustUsuallyManagedInternal(int userId) { |
| 1304 | synchronized (mTrustUsuallyManagedForUser) { |
| 1305 | int i = mTrustUsuallyManagedForUser.indexOfKey(userId); |
| 1306 | if (i >= 0) { |
| 1307 | return mTrustUsuallyManagedForUser.valueAt(i); |
| 1308 | } |
| 1309 | } |
| 1310 | // It's not in memory yet, get the value from persisted storage instead |
| 1311 | boolean persistedValue = mLockPatternUtils.isTrustUsuallyManaged(userId); |
| 1312 | synchronized (mTrustUsuallyManagedForUser) { |
| 1313 | int i = mTrustUsuallyManagedForUser.indexOfKey(userId); |
| 1314 | if (i >= 0) { |
| 1315 | // Someone set the trust usually managed in the mean time. Better use that. |
| 1316 | return mTrustUsuallyManagedForUser.valueAt(i); |
| 1317 | } else { |
| 1318 | // .. otherwise it's safe to cache the fetched value now. |
| 1319 | mTrustUsuallyManagedForUser.put(userId, persistedValue); |
| 1320 | return persistedValue; |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 1325 | private int resolveProfileParent(int userId) { |
| 1326 | long identity = Binder.clearCallingIdentity(); |
| 1327 | try { |
| 1328 | UserInfo parent = mUserManager.getProfileParent(userId); |
| 1329 | if (parent != null) { |
| 1330 | return parent.getUserHandle().getIdentifier(); |
| 1331 | } |
| 1332 | return userId; |
| 1333 | } finally { |
| 1334 | Binder.restoreCallingIdentity(identity); |
| 1335 | } |
| 1336 | } |
| 1337 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1338 | private final Handler mHandler = new Handler() { |
| 1339 | @Override |
| 1340 | public void handleMessage(Message msg) { |
| 1341 | switch (msg.what) { |
| 1342 | case MSG_REGISTER_LISTENER: |
| 1343 | addListener((ITrustListener) msg.obj); |
| 1344 | break; |
| 1345 | case MSG_UNREGISTER_LISTENER: |
| 1346 | removeListener((ITrustListener) msg.obj); |
| 1347 | break; |
| 1348 | case MSG_DISPATCH_UNLOCK_ATTEMPT: |
| 1349 | dispatchUnlockAttempt(msg.arg1 != 0, msg.arg2); |
| 1350 | break; |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 1351 | case MSG_DISPATCH_UNLOCK_LOCKOUT: |
| 1352 | dispatchUnlockLockout(msg.arg1, msg.arg2); |
| 1353 | break; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1354 | case MSG_ENABLED_AGENTS_CHANGED: |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 1355 | refreshAgentList(UserHandle.USER_ALL); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1356 | // This is also called when the security mode of a user changes. |
| 1357 | refreshDeviceLockedForUser(UserHandle.USER_ALL); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1358 | break; |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1359 | case MSG_KEYGUARD_SHOWING_CHANGED: |
Adrian Roos | 7e2e40e | 2014-11-21 15:27:12 +0100 | [diff] [blame] | 1360 | refreshDeviceLockedForUser(mCurrentUser); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1361 | break; |
| 1362 | case MSG_START_USER: |
| 1363 | case MSG_CLEANUP_USER: |
Adrian Roos | 68771eb | 2016-05-03 14:02:36 -0700 | [diff] [blame] | 1364 | case MSG_UNLOCK_USER: |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1365 | refreshAgentList(msg.arg1); |
| 1366 | break; |
| 1367 | case MSG_SWITCH_USER: |
| 1368 | mCurrentUser = msg.arg1; |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 1369 | mSettingsObserver.updateContentObserver(); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1370 | refreshDeviceLockedForUser(UserHandle.USER_ALL); |
| 1371 | break; |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 1372 | case MSG_STOP_USER: |
| 1373 | setDeviceLockedForUser(msg.arg1, true); |
| 1374 | break; |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 1375 | case MSG_FLUSH_TRUST_USUALLY_MANAGED: |
| 1376 | SparseBooleanArray usuallyManaged; |
| 1377 | synchronized (mTrustUsuallyManagedForUser) { |
| 1378 | usuallyManaged = mTrustUsuallyManagedForUser.clone(); |
| 1379 | } |
| 1380 | |
| 1381 | for (int i = 0; i < usuallyManaged.size(); i++) { |
| 1382 | int userId = usuallyManaged.keyAt(i); |
| 1383 | boolean value = usuallyManaged.valueAt(i); |
| 1384 | if (value != mLockPatternUtils.isTrustUsuallyManaged(userId)) { |
| 1385 | mLockPatternUtils.setTrustUsuallyManaged(value, userId); |
| 1386 | } |
| 1387 | } |
Adrian Roos | 68771eb | 2016-05-03 14:02:36 -0700 | [diff] [blame] | 1388 | break; |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1389 | case MSG_REFRESH_DEVICE_LOCKED_FOR_USER: |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 1390 | if (msg.arg2 == 1) { |
| 1391 | updateTrust(msg.arg1, 0 /* flags */, true); |
| 1392 | } |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1393 | refreshDeviceLockedForUser(msg.arg1); |
| 1394 | break; |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 1395 | case MSG_SCHEDULE_TRUST_TIMEOUT: |
| 1396 | handleScheduleTrustTimeout(msg.arg1, msg.arg2); |
| 1397 | break; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | }; |
| 1401 | |
| 1402 | private final PackageMonitor mPackageMonitor = new PackageMonitor() { |
| 1403 | @Override |
| 1404 | public void onSomePackagesChanged() { |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 1405 | refreshAgentList(UserHandle.USER_ALL); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | @Override |
| 1409 | public boolean onPackageChanged(String packageName, int uid, String[] components) { |
| 1410 | // We're interested in all changes, even if just some components get enabled / disabled. |
| 1411 | return true; |
| 1412 | } |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 1413 | |
| 1414 | @Override |
| 1415 | public void onPackageDisappeared(String packageName, int reason) { |
| 1416 | removeAgentsOfPackage(packageName); |
| 1417 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1418 | }; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 1419 | |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 1420 | private static class SettingsAttrs { |
| 1421 | public ComponentName componentName; |
| 1422 | public boolean canUnlockProfile; |
| 1423 | |
| 1424 | public SettingsAttrs( |
| 1425 | ComponentName componentName, |
| 1426 | boolean canUnlockProfile) { |
| 1427 | this.componentName = componentName; |
| 1428 | this.canUnlockProfile = canUnlockProfile; |
| 1429 | } |
| 1430 | }; |
| 1431 | |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 1432 | private class Receiver extends BroadcastReceiver { |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 1433 | |
| 1434 | @Override |
| 1435 | public void onReceive(Context context, Intent intent) { |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 1436 | String action = intent.getAction(); |
| 1437 | if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED.equals(action)) { |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 1438 | refreshAgentList(getSendingUserId()); |
| 1439 | updateDevicePolicyFeatures(); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 1440 | } else if (Intent.ACTION_USER_ADDED.equals(action)) { |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1441 | int userId = getUserId(intent); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 1442 | if (userId > 0) { |
| 1443 | maybeEnableFactoryTrustAgents(mLockPatternUtils, userId); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 1444 | } |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1445 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 1446 | int userId = getUserId(intent); |
| 1447 | if (userId > 0) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1448 | synchronized (mUserIsTrusted) { |
| 1449 | mUserIsTrusted.delete(userId); |
| 1450 | } |
| 1451 | synchronized (mDeviceLockedForUser) { |
| 1452 | mDeviceLockedForUser.delete(userId); |
| 1453 | } |
Adrian Roos | ae02582 | 2016-11-03 14:46:13 -0700 | [diff] [blame] | 1454 | synchronized (mTrustUsuallyManagedForUser) { |
| 1455 | mTrustUsuallyManagedForUser.delete(userId); |
| 1456 | } |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1457 | synchronized (mUsersUnlockedByBiometric) { |
| 1458 | mUsersUnlockedByBiometric.delete(userId); |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1459 | } |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1460 | refreshAgentList(userId); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1461 | refreshDeviceLockedForUser(userId); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | private int getUserId(Intent intent) { |
| 1467 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -100); |
| 1468 | if (userId > 0) { |
| 1469 | return userId; |
| 1470 | } else { |
| 1471 | Slog.wtf(TAG, "EXTRA_USER_HANDLE missing or invalid, value=" + userId); |
| 1472 | return -100; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | public void register(Context context) { |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 1477 | IntentFilter filter = new IntentFilter(); |
| 1478 | filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 1479 | filter.addAction(Intent.ACTION_USER_ADDED); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1480 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 1481 | context.registerReceiverAsUser(this, |
| 1482 | UserHandle.ALL, |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 1483 | filter, |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 1484 | null /* permission */, |
| 1485 | null /* scheduler */); |
| 1486 | } |
| 1487 | } |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 1488 | |
| 1489 | private class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker { |
| 1490 | |
| 1491 | SparseBooleanArray mStartFromSuccessfulUnlock = new SparseBooleanArray(); |
| 1492 | |
| 1493 | public StrongAuthTracker(Context context) { |
| 1494 | super(context); |
| 1495 | } |
| 1496 | |
| 1497 | @Override |
| 1498 | public void onStrongAuthRequiredChanged(int userId) { |
| 1499 | mStartFromSuccessfulUnlock.delete(userId); |
| 1500 | |
| 1501 | if (DEBUG) { |
| 1502 | Log.i(TAG, "onStrongAuthRequiredChanged(" + userId + ") ->" |
| 1503 | + " trustAllowed=" + isTrustAllowedForUser(userId) |
| 1504 | + " agentsCanRun=" + canAgentsRunForUser(userId)); |
| 1505 | } |
| 1506 | |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 1507 | // Cancel pending alarms if we require some auth anyway. |
| 1508 | if (!isTrustAllowedForUser(userId)) { |
| 1509 | TrustTimeoutAlarmListener alarm = mTrustTimeoutAlarmListenerForUser.get(userId); |
| 1510 | if (alarm != null && alarm.isQueued()) { |
| 1511 | alarm.setQueued(false /* isQueued */); |
| 1512 | mAlarmManager.cancel(alarm); |
| 1513 | } |
| 1514 | } |
| 1515 | |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 1516 | refreshAgentList(userId); |
| 1517 | |
| 1518 | // The list of active trust agents may not have changed, if there was a previous call |
| 1519 | // to allowTrustFromUnlock, so we update the trust here too. |
| 1520 | updateTrust(userId, 0 /* flags */); |
| 1521 | } |
| 1522 | |
| 1523 | boolean canAgentsRunForUser(int userId) { |
| 1524 | return mStartFromSuccessfulUnlock.get(userId) |
| 1525 | || super.isTrustAllowedForUser(userId); |
| 1526 | } |
| 1527 | |
| 1528 | /** |
| 1529 | * Temporarily suppress strong auth requirements for {@param userId} until strong auth |
| 1530 | * changes again. Must only be called when we know about a successful unlock already |
| 1531 | * before the underlying StrongAuthTracker. |
| 1532 | * |
| 1533 | * Note that this only changes whether trust agents can be started, not the actual trusted |
| 1534 | * value. |
| 1535 | */ |
| 1536 | void allowTrustFromUnlock(int userId) { |
| 1537 | if (userId < UserHandle.USER_SYSTEM) { |
| 1538 | throw new IllegalArgumentException("userId must be a valid user: " + userId); |
| 1539 | } |
| 1540 | boolean previous = canAgentsRunForUser(userId); |
| 1541 | mStartFromSuccessfulUnlock.put(userId, true); |
| 1542 | |
| 1543 | if (DEBUG) { |
| 1544 | Log.i(TAG, "allowTrustFromUnlock(" + userId + ") ->" |
| 1545 | + " trustAllowed=" + isTrustAllowedForUser(userId) |
| 1546 | + " agentsCanRun=" + canAgentsRunForUser(userId)); |
| 1547 | } |
| 1548 | |
| 1549 | if (canAgentsRunForUser(userId) != previous) { |
| 1550 | refreshAgentList(userId); |
| 1551 | } |
| 1552 | } |
| 1553 | } |
Vishwath Mohan | 15f7dd0 | 2018-12-12 11:15:08 -0800 | [diff] [blame] | 1554 | |
| 1555 | private class TrustTimeoutAlarmListener implements OnAlarmListener { |
| 1556 | private final int mUserId; |
| 1557 | private boolean mIsQueued = false; |
| 1558 | |
| 1559 | TrustTimeoutAlarmListener(int userId) { |
| 1560 | mUserId = userId; |
| 1561 | } |
| 1562 | |
| 1563 | @Override |
| 1564 | public void onAlarm() { |
| 1565 | mIsQueued = false; |
| 1566 | int strongAuthState = mStrongAuthTracker.getStrongAuthForUser(mUserId); |
| 1567 | |
| 1568 | // Only fire if trust can unlock. |
| 1569 | if (mStrongAuthTracker.isTrustAllowedForUser(mUserId)) { |
| 1570 | if (DEBUG) Slog.d(TAG, "Revoking all trust because of trust timeout"); |
| 1571 | mLockPatternUtils.requireStrongAuth( |
| 1572 | mStrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST, mUserId); |
| 1573 | } |
| 1574 | maybeLockScreen(mUserId); |
| 1575 | } |
| 1576 | |
| 1577 | public void setQueued(boolean isQueued) { |
| 1578 | mIsQueued = isQueued; |
| 1579 | } |
| 1580 | |
| 1581 | public boolean isQueued() { |
| 1582 | return mIsQueued; |
| 1583 | } |
| 1584 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1585 | } |