Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 18 | |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 19 | import android.app.ActivityManagerNative; |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 20 | import android.app.KeyguardManager; |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 21 | import android.app.Notification; |
| 22 | import android.app.NotificationManager; |
| 23 | import android.app.PendingIntent; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 24 | import android.app.admin.DevicePolicyManager; |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 25 | import android.app.backup.BackupManager; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 26 | import android.app.trust.IStrongAuthTracker; |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 27 | import android.app.trust.TrustManager; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 29 | import android.content.ContentResolver; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 30 | import android.content.Context; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 31 | import android.content.Intent; |
| 32 | import android.content.IntentFilter; |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 33 | import android.content.pm.PackageManager; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 34 | import android.content.pm.UserInfo; |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 35 | import android.content.res.Resources; |
| 36 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 37 | import static android.Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE; |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 38 | import static android.content.Context.KEYGUARD_SERVICE; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 39 | import static android.content.Context.USER_SERVICE; |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 40 | import static android.Manifest.permission.READ_CONTACTS; |
Adrian Roos | 873010d | 2015-08-25 15:59:00 -0700 | [diff] [blame] | 41 | import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT; |
| 42 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 43 | import android.database.sqlite.SQLiteDatabase; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 44 | import android.os.Binder; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 45 | import android.os.Bundle; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 46 | import android.os.IBinder; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 47 | import android.os.IProgressListener; |
| 48 | import android.os.Parcel; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 49 | import android.os.RemoteException; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 50 | import android.os.storage.IMountService; |
| 51 | import android.os.ServiceManager; |
Amith Yamasani | d1645f8 | 2012-06-12 11:53:26 -0700 | [diff] [blame] | 52 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 53 | import android.os.UserHandle; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 54 | import android.os.UserManager; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 55 | import android.provider.Settings; |
| 56 | import android.provider.Settings.Secure; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 57 | import android.provider.Settings.SettingNotFoundException; |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 58 | import android.security.KeyStore; |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 59 | import android.service.gatekeeper.GateKeeperResponse; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 60 | import android.service.gatekeeper.IGateKeeperService; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 61 | import android.text.TextUtils; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 62 | import android.util.Log; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 63 | import android.util.Slog; |
| 64 | |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 65 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 66 | import com.android.internal.widget.ILockSettings; |
| 67 | import com.android.internal.widget.LockPatternUtils; |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 68 | import com.android.internal.widget.VerifyCredentialResponse; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 69 | import com.android.server.LockSettingsStorage.CredentialHash; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 70 | |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 71 | import java.nio.charset.StandardCharsets; |
| 72 | import java.security.MessageDigest; |
| 73 | import java.security.NoSuchAlgorithmException; |
| 74 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 75 | import java.util.Arrays; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 76 | import java.util.List; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 77 | import java.util.concurrent.CountDownLatch; |
| 78 | import java.util.concurrent.TimeUnit; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Keeps the lock pattern/password data and related settings for each user. |
| 82 | * Used by LockPatternUtils. Needs to be a service because Settings app also needs |
| 83 | * to be able to save lockscreen information for secondary users. |
| 84 | * @hide |
| 85 | */ |
| 86 | public class LockSettingsService extends ILockSettings.Stub { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 87 | private static final String TAG = "LockSettingsService"; |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 88 | private static final String PERMISSION = ACCESS_KEYGUARD_SECURE_STORAGE; |
| 89 | private static final Intent ACTION_NULL; // hack to ensure notification shows the bouncer |
| 90 | private static final int FBE_ENCRYPTED_NOTIFICATION = 0; |
| 91 | private static final boolean DEBUG = false; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 92 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 93 | private final Context mContext; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 94 | private final LockSettingsStorage mStorage; |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 95 | private final LockSettingsStrongAuth mStrongAuth; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 96 | |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 97 | private LockPatternUtils mLockPatternUtils; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 98 | private boolean mFirstCallToVold; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 99 | private IGateKeeperService mGateKeeperService; |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 100 | private NotificationManager mNotificationManager; |
| 101 | private UserManager mUserManager; |
| 102 | |
| 103 | static { |
| 104 | // Just launch the home screen, which happens anyway |
| 105 | ACTION_NULL = new Intent(Intent.ACTION_MAIN); |
| 106 | ACTION_NULL.addCategory(Intent.CATEGORY_HOME); |
| 107 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 108 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 109 | private interface CredentialUtil { |
| 110 | void setCredential(String credential, String savedCredential, int userId) |
| 111 | throws RemoteException; |
| 112 | byte[] toHash(String credential, int userId); |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 113 | String adjustForKeystore(String credential); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 116 | // This class manages life cycle events for encrypted users on File Based Encryption (FBE) |
| 117 | // devices. The most basic of these is to show/hide notifications about missing features until |
| 118 | // the user unlocks the account and credential-encrypted storage is available. |
| 119 | public static final class Lifecycle extends SystemService { |
| 120 | private LockSettingsService mLockSettingsService; |
| 121 | |
| 122 | public Lifecycle(Context context) { |
| 123 | super(context); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void onStart() { |
| 128 | mLockSettingsService = new LockSettingsService(getContext()); |
| 129 | publishBinderService("lock_settings", mLockSettingsService); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void onBootPhase(int phase) { |
| 134 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 135 | mLockSettingsService.maybeShowEncryptionNotifications(); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 136 | } else if (phase == SystemService.PHASE_BOOT_COMPLETED) { |
| 137 | // TODO |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void onUnlockUser(int userHandle) { |
| 143 | mLockSettingsService.onUnlockUser(userHandle); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void onCleanupUser(int userHandle) { |
| 148 | mLockSettingsService.onCleanupUser(userHandle); |
| 149 | } |
| 150 | } |
| 151 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 152 | public LockSettingsService(Context context) { |
| 153 | mContext = context; |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 154 | mStrongAuth = new LockSettingsStrongAuth(context); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 155 | // Open the database |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 156 | |
| 157 | mLockPatternUtils = new LockPatternUtils(context); |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 158 | mFirstCallToVold = true; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 159 | |
| 160 | IntentFilter filter = new IntentFilter(); |
| 161 | filter.addAction(Intent.ACTION_USER_ADDED); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 162 | filter.addAction(Intent.ACTION_USER_STARTING); |
Adrian Roos | db0f76e | 2015-01-07 22:19:38 +0100 | [diff] [blame] | 163 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 164 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 165 | mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 166 | |
| 167 | mStorage = new LockSettingsStorage(context, new LockSettingsStorage.Callback() { |
| 168 | @Override |
| 169 | public void initialize(SQLiteDatabase db) { |
| 170 | // Get the lockscreen default from a system property, if available |
| 171 | boolean lockScreenDisable = SystemProperties.getBoolean( |
| 172 | "ro.lockscreen.disable.default", false); |
| 173 | if (lockScreenDisable) { |
| 174 | mStorage.writeKeyValue(db, LockPatternUtils.DISABLE_LOCKSCREEN_KEY, "1", 0); |
| 175 | } |
| 176 | } |
| 177 | }); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 178 | mNotificationManager = (NotificationManager) |
| 179 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 180 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * If the account is credential-encrypted, show notification requesting the user to unlock |
| 185 | * the device. |
| 186 | */ |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 187 | private void maybeShowEncryptionNotifications() { |
| 188 | final List<UserInfo> users = mUserManager.getUsers(); |
| 189 | for (int i = 0; i < users.size(); i++) { |
| 190 | UserInfo user = users.get(i); |
| 191 | UserHandle userHandle = user.getUserHandle(); |
| 192 | if (!mUserManager.isUserUnlocked(userHandle)) { |
| 193 | if (!user.isManagedProfile()) { |
| 194 | showEncryptionNotification(userHandle); |
| 195 | } else { |
| 196 | UserInfo parent = mUserManager.getProfileParent(user.id); |
| 197 | if (parent != null && mUserManager.isUserUnlocked(parent.getUserHandle())) { |
| 198 | // Only show notifications for managed profiles once their parent |
| 199 | // user is unlocked. |
| 200 | showEncryptionNotificationForProfile(userHandle); |
| 201 | } |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 207 | private void showEncryptionNotificationForProfile(UserHandle user) { |
| 208 | Resources r = mContext.getResources(); |
| 209 | CharSequence title = r.getText( |
| 210 | com.android.internal.R.string.user_encrypted_title); |
| 211 | CharSequence message = r.getText( |
| 212 | com.android.internal.R.string.profile_encrypted_message); |
| 213 | CharSequence detail = r.getText( |
| 214 | com.android.internal.R.string.profile_encrypted_detail); |
| 215 | |
| 216 | final KeyguardManager km = (KeyguardManager) mContext.getSystemService(KEYGUARD_SERVICE); |
| 217 | final Intent unlockIntent = km.createConfirmDeviceCredentialIntent(null, null, user.getIdentifier()); |
| 218 | if (unlockIntent == null) { |
| 219 | return; |
| 220 | } |
| 221 | unlockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 222 | PendingIntent intent = PendingIntent.getActivity(mContext, 0, unlockIntent, |
| 223 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 224 | |
| 225 | showEncryptionNotification(user, title, message, detail, intent); |
| 226 | } |
| 227 | |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 228 | private void showEncryptionNotification(UserHandle user) { |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 229 | Resources r = mContext.getResources(); |
| 230 | CharSequence title = r.getText( |
| 231 | com.android.internal.R.string.user_encrypted_title); |
| 232 | CharSequence message = r.getText( |
| 233 | com.android.internal.R.string.user_encrypted_message); |
| 234 | CharSequence detail = r.getText( |
| 235 | com.android.internal.R.string.user_encrypted_detail); |
| 236 | |
| 237 | PendingIntent intent = PendingIntent.getBroadcast(mContext, 0, ACTION_NULL, |
| 238 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 239 | |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 240 | showEncryptionNotification(user, title, message, detail, intent); |
| 241 | } |
| 242 | |
| 243 | private void showEncryptionNotification(UserHandle user, CharSequence title, CharSequence message, |
| 244 | CharSequence detail, PendingIntent intent) { |
| 245 | if (DEBUG) Slog.v(TAG, "showing encryption notification, user: " + user.getIdentifier()); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 246 | Notification notification = new Notification.Builder(mContext) |
Jim Miller | b1135b7 | 2016-02-02 17:08:56 -0800 | [diff] [blame] | 247 | .setSmallIcon(com.android.internal.R.drawable.ic_user_secure) |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 248 | .setWhen(0) |
| 249 | .setOngoing(true) |
| 250 | .setTicker(title) |
| 251 | .setDefaults(0) // please be quiet |
| 252 | .setPriority(Notification.PRIORITY_MAX) |
| 253 | .setColor(mContext.getColor( |
| 254 | com.android.internal.R.color.system_notification_accent_color)) |
| 255 | .setContentTitle(title) |
| 256 | .setContentText(message) |
| 257 | .setContentInfo(detail) |
| 258 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 259 | .setContentIntent(intent) |
| 260 | .build(); |
| 261 | mNotificationManager.notifyAsUser(null, FBE_ENCRYPTED_NOTIFICATION, notification, user); |
| 262 | } |
| 263 | |
| 264 | public void hideEncryptionNotification(UserHandle userHandle) { |
| 265 | if (DEBUG) Slog.v(TAG, "hide encryption notification, user: "+ userHandle.getIdentifier()); |
| 266 | mNotificationManager.cancelAsUser(null, FBE_ENCRYPTED_NOTIFICATION, userHandle); |
| 267 | } |
| 268 | |
| 269 | public void onCleanupUser(int userId) { |
| 270 | hideEncryptionNotification(new UserHandle(userId)); |
| 271 | } |
| 272 | |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 273 | public void onUnlockUser(int userId) { |
| 274 | hideEncryptionNotification(new UserHandle(userId)); |
| 275 | |
| 276 | // Now we have unlocked the parent user we should show notifications |
| 277 | // about any profiles that exist. |
| 278 | List<UserInfo> profiles = mUserManager.getProfiles(userId); |
| 279 | for (int i = 0; i < profiles.size(); i++) { |
| 280 | UserInfo profile = profiles.get(i); |
| 281 | if (profile.isManagedProfile()) { |
| 282 | UserHandle userHandle = profile.getUserHandle(); |
| 283 | if (!mUserManager.isUserUnlocked(userHandle)) { |
| 284 | showEncryptionNotificationForProfile(userHandle); |
| 285 | } |
| 286 | } |
| 287 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 290 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 291 | @Override |
| 292 | public void onReceive(Context context, Intent intent) { |
Robin Lee | 1096cf8 | 2014-09-01 16:52:47 +0100 | [diff] [blame] | 293 | if (Intent.ACTION_USER_ADDED.equals(intent.getAction())) { |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 294 | // Notify keystore that a new user was added. |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 295 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
Robin Lee | 49d810c | 2014-09-23 13:50:22 +0100 | [diff] [blame] | 296 | final KeyStore ks = KeyStore.getInstance(); |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 297 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 298 | final UserInfo parentInfo = um.getProfileParent(userHandle); |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 299 | final int parentHandle = parentInfo != null ? parentInfo.id : -1; |
| 300 | ks.onUserAdded(userHandle, parentHandle); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 301 | } else if (Intent.ACTION_USER_STARTING.equals(intent.getAction())) { |
| 302 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 303 | mStorage.prefetchUser(userHandle); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 304 | } else if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) { |
| 305 | mStrongAuth.reportUnlock(getSendingUserId()); |
Adrian Roos | db0f76e | 2015-01-07 22:19:38 +0100 | [diff] [blame] | 306 | } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) { |
| 307 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 308 | if (userHandle > 0) { |
| 309 | removeUser(userHandle); |
| 310 | } |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | }; |
| 314 | |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 315 | @Override // binder interface |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 316 | public void systemReady() { |
| 317 | migrateOldData(); |
Andres Morales | 301ea44 | 2015-04-17 09:15:47 -0700 | [diff] [blame] | 318 | try { |
| 319 | getGateKeeperService(); |
| 320 | } catch (RemoteException e) { |
| 321 | Slog.e(TAG, "Failure retrieving IGateKeeperService", e); |
| 322 | } |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 323 | // TODO: maybe skip this for split system user mode. |
| 324 | mStorage.prefetchUser(UserHandle.USER_SYSTEM); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | private void migrateOldData() { |
| 328 | try { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 329 | // These Settings moved before multi-user was enabled, so we only have to do it for the |
| 330 | // root user. |
| 331 | if (getString("migrated", null, 0) == null) { |
| 332 | final ContentResolver cr = mContext.getContentResolver(); |
| 333 | for (String validSetting : VALID_SETTINGS) { |
| 334 | String value = Settings.Secure.getString(cr, validSetting); |
| 335 | if (value != null) { |
| 336 | setString(validSetting, value, 0); |
| 337 | } |
| 338 | } |
| 339 | // No need to move the password / pattern files. They're already in the right place. |
| 340 | setString("migrated", "true", 0); |
| 341 | Slog.i(TAG, "Migrated lock settings to new location"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 344 | // These Settings changed after multi-user was enabled, hence need to be moved per user. |
| 345 | if (getString("migrated_user_specific", null, 0) == null) { |
| 346 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 347 | final ContentResolver cr = mContext.getContentResolver(); |
| 348 | List<UserInfo> users = um.getUsers(); |
| 349 | for (int user = 0; user < users.size(); user++) { |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 350 | // Migrate owner info |
| 351 | final int userId = users.get(user).id; |
| 352 | final String OWNER_INFO = Secure.LOCK_SCREEN_OWNER_INFO; |
| 353 | String ownerInfo = Settings.Secure.getStringForUser(cr, OWNER_INFO, userId); |
Jim Miller | 325c567 | 2016-03-01 19:21:47 -0800 | [diff] [blame] | 354 | if (!TextUtils.isEmpty(ownerInfo)) { |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 355 | setString(OWNER_INFO, ownerInfo, userId); |
Jim Miller | 325c567 | 2016-03-01 19:21:47 -0800 | [diff] [blame] | 356 | Settings.Secure.putStringForUser(cr, OWNER_INFO, "", userId); |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 357 | } |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 358 | |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 359 | // Migrate owner info enabled. Note there was a bug where older platforms only |
| 360 | // stored this value if the checkbox was toggled at least once. The code detects |
| 361 | // this case by handling the exception. |
| 362 | final String OWNER_INFO_ENABLED = Secure.LOCK_SCREEN_OWNER_INFO_ENABLED; |
| 363 | boolean enabled; |
| 364 | try { |
| 365 | int ivalue = Settings.Secure.getIntForUser(cr, OWNER_INFO_ENABLED, userId); |
| 366 | enabled = ivalue != 0; |
| 367 | setLong(OWNER_INFO_ENABLED, enabled ? 1 : 0, userId); |
| 368 | } catch (SettingNotFoundException e) { |
| 369 | // Setting was never stored. Store it if the string is not empty. |
| 370 | if (!TextUtils.isEmpty(ownerInfo)) { |
| 371 | setLong(OWNER_INFO_ENABLED, 1, userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 374 | Settings.Secure.putIntForUser(cr, OWNER_INFO_ENABLED, 0, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 375 | } |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 376 | // No need to move the password / pattern files. They're already in the right place. |
| 377 | setString("migrated_user_specific", "true", 0); |
| 378 | Slog.i(TAG, "Migrated per-user lock settings to new location"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 379 | } |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 380 | |
| 381 | // Migrates biometric weak such that the fallback mechanism becomes the primary. |
| 382 | if (getString("migrated_biometric_weak", null, 0) == null) { |
| 383 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 384 | List<UserInfo> users = um.getUsers(); |
| 385 | for (int i = 0; i < users.size(); i++) { |
| 386 | int userId = users.get(i).id; |
| 387 | long type = getLong(LockPatternUtils.PASSWORD_TYPE_KEY, |
| 388 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 389 | userId); |
| 390 | long alternateType = getLong(LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 391 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 392 | userId); |
| 393 | if (type == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK) { |
| 394 | setLong(LockPatternUtils.PASSWORD_TYPE_KEY, |
| 395 | alternateType, |
| 396 | userId); |
| 397 | } |
| 398 | setLong(LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 399 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 400 | userId); |
| 401 | } |
| 402 | setString("migrated_biometric_weak", "true", 0); |
| 403 | Slog.i(TAG, "Migrated biometric weak to use the fallback instead"); |
| 404 | } |
Adrian Roos | 4383058 | 2015-04-21 16:04:43 -0700 | [diff] [blame] | 405 | |
| 406 | // Migrates lockscreen.disabled. Prior to M, the flag was ignored when more than one |
| 407 | // user was present on the system, so if we're upgrading to M and there is more than one |
| 408 | // user we disable the flag to remain consistent. |
| 409 | if (getString("migrated_lockscreen_disabled", null, 0) == null) { |
| 410 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 411 | |
| 412 | final List<UserInfo> users = um.getUsers(); |
| 413 | final int userCount = users.size(); |
| 414 | int switchableUsers = 0; |
| 415 | for (int i = 0; i < userCount; i++) { |
| 416 | if (users.get(i).supportsSwitchTo()) { |
| 417 | switchableUsers++; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (switchableUsers > 1) { |
| 422 | for (int i = 0; i < userCount; i++) { |
| 423 | int id = users.get(i).id; |
| 424 | |
| 425 | if (getBoolean(LockPatternUtils.DISABLE_LOCKSCREEN_KEY, false, id)) { |
| 426 | setBoolean(LockPatternUtils.DISABLE_LOCKSCREEN_KEY, false, id); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | setString("migrated_lockscreen_disabled", "true", 0); |
| 432 | Slog.i(TAG, "Migrated lockscreen disabled flag"); |
| 433 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 434 | } catch (RemoteException re) { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 435 | Slog.e(TAG, "Unable to migrate old data", re); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 439 | private final void checkWritePermission(int userId) { |
Jim Miller | 505329b | 2013-11-08 13:25:36 -0800 | [diff] [blame] | 440 | mContext.enforceCallingOrSelfPermission(PERMISSION, "LockSettingsWrite"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 443 | private final void checkPasswordReadPermission(int userId) { |
Jim Miller | 505329b | 2013-11-08 13:25:36 -0800 | [diff] [blame] | 444 | mContext.enforceCallingOrSelfPermission(PERMISSION, "LockSettingsRead"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 447 | private final void checkReadPermission(String requestedKey, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 448 | final int callingUid = Binder.getCallingUid(); |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 449 | |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 450 | for (int i = 0; i < READ_CONTACTS_PROTECTED_SETTINGS.length; i++) { |
| 451 | String key = READ_CONTACTS_PROTECTED_SETTINGS[i]; |
| 452 | if (key.equals(requestedKey) && mContext.checkCallingOrSelfPermission(READ_CONTACTS) |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 453 | != PackageManager.PERMISSION_GRANTED) { |
| 454 | throw new SecurityException("uid=" + callingUid |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 455 | + " needs permission " + READ_CONTACTS + " to read " |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 456 | + requestedKey + " for user " + userId); |
| 457 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 458 | } |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 459 | |
| 460 | for (int i = 0; i < READ_PASSWORD_PROTECTED_SETTINGS.length; i++) { |
| 461 | String key = READ_PASSWORD_PROTECTED_SETTINGS[i]; |
| 462 | if (key.equals(requestedKey) && mContext.checkCallingOrSelfPermission(PERMISSION) |
| 463 | != PackageManager.PERMISSION_GRANTED) { |
| 464 | throw new SecurityException("uid=" + callingUid |
| 465 | + " needs permission " + PERMISSION + " to read " |
| 466 | + requestedKey + " for user " + userId); |
| 467 | } |
| 468 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public void setBoolean(String key, boolean value, int userId) throws RemoteException { |
| 473 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 474 | setStringUnchecked(key, userId, value ? "1" : "0"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public void setLong(String key, long value, int userId) throws RemoteException { |
| 479 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 480 | setStringUnchecked(key, userId, Long.toString(value)); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public void setString(String key, String value, int userId) throws RemoteException { |
| 485 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 486 | setStringUnchecked(key, userId, value); |
| 487 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 488 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 489 | private void setStringUnchecked(String key, int userId, String value) { |
| 490 | mStorage.writeKeyValue(key, value, userId); |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 491 | if (ArrayUtils.contains(SETTINGS_TO_BACKUP, key)) { |
| 492 | BackupManager.dataChanged("com.android.providers.settings"); |
| 493 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | @Override |
| 497 | public boolean getBoolean(String key, boolean defaultValue, int userId) throws RemoteException { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 498 | checkReadPermission(key, userId); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 499 | String value = getStringUnchecked(key, null, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 500 | return TextUtils.isEmpty(value) ? |
| 501 | defaultValue : (value.equals("1") || value.equals("true")); |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public long getLong(String key, long defaultValue, int userId) throws RemoteException { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 506 | checkReadPermission(key, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 507 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 508 | String value = getStringUnchecked(key, null, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 509 | return TextUtils.isEmpty(value) ? defaultValue : Long.parseLong(value); |
| 510 | } |
| 511 | |
| 512 | @Override |
| 513 | public String getString(String key, String defaultValue, int userId) throws RemoteException { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 514 | checkReadPermission(key, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 515 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 516 | return getStringUnchecked(key, defaultValue, userId); |
| 517 | } |
| 518 | |
| 519 | public String getStringUnchecked(String key, String defaultValue, int userId) { |
| 520 | if (Settings.Secure.LOCK_PATTERN_ENABLED.equals(key)) { |
Adrian Roos | 7811d9f | 2015-07-27 15:10:13 -0700 | [diff] [blame] | 521 | long ident = Binder.clearCallingIdentity(); |
| 522 | try { |
| 523 | return mLockPatternUtils.isLockPatternEnabled(userId) ? "1" : "0"; |
| 524 | } finally { |
| 525 | Binder.restoreCallingIdentity(ident); |
| 526 | } |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Bryce Lee | 4614596 | 2015-12-14 14:39:10 -0800 | [diff] [blame] | 529 | if (LockPatternUtils.LEGACY_LOCK_PATTERN_ENABLED.equals(key)) { |
| 530 | key = Settings.Secure.LOCK_PATTERN_ENABLED; |
| 531 | } |
| 532 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 533 | return mStorage.readKeyValue(key, defaultValue, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Adrian Roos | 4f78845 | 2014-05-22 20:45:59 +0200 | [diff] [blame] | 536 | @Override |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 537 | public boolean havePassword(int userId) throws RemoteException { |
| 538 | // Do we need a permissions check here? |
| 539 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 540 | return mStorage.hasPassword(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public boolean havePattern(int userId) throws RemoteException { |
| 545 | // Do we need a permissions check here? |
| 546 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 547 | return mStorage.hasPattern(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 550 | private void setKeystorePassword(String password, int userHandle) { |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 551 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 552 | final KeyStore ks = KeyStore.getInstance(); |
| 553 | |
Clara Bayarri | 0a587d2 | 2016-02-23 14:49:41 -0800 | [diff] [blame] | 554 | if (um.getUserInfo(userHandle).isManagedProfile()) { |
| 555 | if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userHandle)) { |
| 556 | ks.onUserPasswordChanged(userHandle, password); |
| 557 | } else { |
| 558 | throw new RuntimeException("Can't set keystore password on a profile that " |
| 559 | + "doesn't have a profile challenge."); |
| 560 | } |
| 561 | } else { |
| 562 | final List<UserInfo> profiles = um.getProfiles(userHandle); |
| 563 | for (UserInfo pi : profiles) { |
| 564 | // Change password on the given user and all its profiles that don't have |
| 565 | // their own profile challenge enabled. |
| 566 | if (pi.id == userHandle || (pi.isManagedProfile() |
| 567 | && !mLockPatternUtils.isSeparateProfileChallengeEnabled(pi.id))) { |
| 568 | ks.onUserPasswordChanged(pi.id, password); |
| 569 | } |
| 570 | } |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | private void unlockKeystore(String password, int userHandle) { |
| 575 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 576 | final KeyStore ks = KeyStore.getInstance(); |
| 577 | |
Clara Bayarri | 0a587d2 | 2016-02-23 14:49:41 -0800 | [diff] [blame] | 578 | if (um.getUserInfo(userHandle).isManagedProfile()) { |
| 579 | if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userHandle)) { |
| 580 | ks.unlock(userHandle, password); |
| 581 | } else { |
| 582 | throw new RuntimeException("Can't unlock a profile explicitly if it " |
| 583 | + "doesn't have a profile challenge."); |
| 584 | } |
| 585 | } else { |
| 586 | final List<UserInfo> profiles = um.getProfiles(userHandle); |
| 587 | for (UserInfo pi : profiles) { |
| 588 | // Unlock the given user and all its profiles that don't have |
| 589 | // their own profile challenge enabled. |
| 590 | if (pi.id == userHandle || (pi.isManagedProfile() |
| 591 | && !mLockPatternUtils.isSeparateProfileChallengeEnabled(pi.id))) { |
| 592 | ks.unlock(pi.id, password); |
| 593 | } |
| 594 | } |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 598 | private void unlockUser(int userId, byte[] token, byte[] secret) { |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 599 | // TODO: make this method fully async so we can update UI with progress strings |
| 600 | final CountDownLatch latch = new CountDownLatch(1); |
| 601 | final IProgressListener listener = new IProgressListener.Stub() { |
| 602 | @Override |
| 603 | public void onStarted(int id, Bundle extras) throws RemoteException { |
| 604 | // Ignored |
| 605 | } |
| 606 | |
| 607 | @Override |
| 608 | public void onProgress(int id, int progress, Bundle extras) throws RemoteException { |
| 609 | // Ignored |
| 610 | } |
| 611 | |
| 612 | @Override |
| 613 | public void onFinished(int id, Bundle extras) throws RemoteException { |
| 614 | Log.d(TAG, "unlockUser finished!"); |
| 615 | latch.countDown(); |
| 616 | } |
| 617 | }; |
| 618 | |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 619 | try { |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 620 | ActivityManagerNative.getDefault().unlockUser(userId, token, secret, listener); |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 621 | } catch (RemoteException e) { |
| 622 | throw e.rethrowAsRuntimeException(); |
| 623 | } |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 624 | |
| 625 | try { |
| 626 | latch.await(15, TimeUnit.SECONDS); |
| 627 | } catch (InterruptedException e) { |
| 628 | Thread.currentThread().interrupt(); |
| 629 | } |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 630 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 631 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 632 | private byte[] getCurrentHandle(int userId) { |
| 633 | CredentialHash credential; |
| 634 | byte[] currentHandle; |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 635 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 636 | int currentHandleType = mStorage.getStoredCredentialType(userId); |
| 637 | switch (currentHandleType) { |
| 638 | case CredentialHash.TYPE_PATTERN: |
| 639 | credential = mStorage.readPatternHash(userId); |
| 640 | currentHandle = credential != null |
| 641 | ? credential.hash |
| 642 | : null; |
| 643 | break; |
| 644 | case CredentialHash.TYPE_PASSWORD: |
| 645 | credential = mStorage.readPasswordHash(userId); |
| 646 | currentHandle = credential != null |
| 647 | ? credential.hash |
| 648 | : null; |
| 649 | break; |
| 650 | case CredentialHash.TYPE_NONE: |
| 651 | default: |
| 652 | currentHandle = null; |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | // sanity check |
| 657 | if (currentHandleType != CredentialHash.TYPE_NONE && currentHandle == null) { |
| 658 | Slog.e(TAG, "Stored handle type [" + currentHandleType + "] but no handle available"); |
| 659 | } |
| 660 | |
| 661 | return currentHandle; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 664 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 665 | @Override |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 666 | public void setLockPattern(String pattern, String savedCredential, int userId) |
| 667 | throws RemoteException { |
| 668 | byte[] currentHandle = getCurrentHandle(userId); |
| 669 | |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 670 | if (pattern == null) { |
Andres Morales | cfb6160 | 2015-04-16 16:31:15 -0700 | [diff] [blame] | 671 | getGateKeeperService().clearSecureUserId(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 672 | mStorage.writePatternHash(null, userId); |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 673 | setKeystorePassword(null, userId); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 674 | clearUserKeyProtection(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 675 | return; |
| 676 | } |
| 677 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 678 | if (currentHandle == null) { |
| 679 | if (savedCredential != null) { |
| 680 | Slog.w(TAG, "Saved credential provided, but none stored"); |
| 681 | } |
| 682 | savedCredential = null; |
| 683 | } |
| 684 | |
| 685 | byte[] enrolledHandle = enrollCredential(currentHandle, savedCredential, pattern, userId); |
| 686 | if (enrolledHandle != null) { |
| 687 | mStorage.writePatternHash(enrolledHandle, userId); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 688 | setUserKeyProtection(userId, pattern, verifyPattern(pattern, 0, userId)); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 689 | } else { |
Andres Morales | 2c4a573 | 2015-07-09 16:11:00 -0700 | [diff] [blame] | 690 | throw new RemoteException("Failed to enroll pattern"); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
| 694 | |
| 695 | @Override |
| 696 | public void setLockPassword(String password, String savedCredential, int userId) |
| 697 | throws RemoteException { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 698 | byte[] currentHandle = getCurrentHandle(userId); |
| 699 | |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 700 | if (password == null) { |
Andres Morales | cfb6160 | 2015-04-16 16:31:15 -0700 | [diff] [blame] | 701 | getGateKeeperService().clearSecureUserId(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 702 | mStorage.writePasswordHash(null, userId); |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 703 | setKeystorePassword(null, userId); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 704 | clearUserKeyProtection(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 705 | return; |
| 706 | } |
| 707 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 708 | if (currentHandle == null) { |
| 709 | if (savedCredential != null) { |
| 710 | Slog.w(TAG, "Saved credential provided, but none stored"); |
| 711 | } |
| 712 | savedCredential = null; |
| 713 | } |
| 714 | |
| 715 | byte[] enrolledHandle = enrollCredential(currentHandle, savedCredential, password, userId); |
| 716 | if (enrolledHandle != null) { |
| 717 | mStorage.writePasswordHash(enrolledHandle, userId); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 718 | setUserKeyProtection(userId, password, verifyPassword(password, 0, userId)); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 719 | } else { |
Andres Morales | 2c4a573 | 2015-07-09 16:11:00 -0700 | [diff] [blame] | 720 | throw new RemoteException("Failed to enroll password"); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
| 724 | private byte[] enrollCredential(byte[] enrolledHandle, |
| 725 | String enrolledCredential, String toEnroll, int userId) |
| 726 | throws RemoteException { |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 727 | checkWritePermission(userId); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 728 | byte[] enrolledCredentialBytes = enrolledCredential == null |
| 729 | ? null |
| 730 | : enrolledCredential.getBytes(); |
| 731 | byte[] toEnrollBytes = toEnroll == null |
| 732 | ? null |
| 733 | : toEnroll.getBytes(); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 734 | GateKeeperResponse response = getGateKeeperService().enroll(userId, enrolledHandle, |
| 735 | enrolledCredentialBytes, toEnrollBytes); |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 736 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 737 | if (response == null) { |
| 738 | return null; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 739 | } |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 740 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 741 | byte[] hash = response.getPayload(); |
| 742 | if (hash != null) { |
| 743 | setKeystorePassword(toEnroll, userId); |
| 744 | } else { |
| 745 | // Should not happen |
| 746 | Slog.e(TAG, "Throttled while enrolling a password"); |
| 747 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 748 | return hash; |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 751 | private void setUserKeyProtection(int userId, String credential, VerifyCredentialResponse vcr) |
| 752 | throws RemoteException { |
| 753 | if (vcr == null) { |
| 754 | throw new RemoteException("Null response verifying a credential we just set"); |
| 755 | } |
| 756 | if (vcr.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK) { |
| 757 | throw new RemoteException("Non-OK response verifying a credential we just set: " |
| 758 | + vcr.getResponseCode()); |
| 759 | } |
| 760 | byte[] token = vcr.getPayload(); |
| 761 | if (token == null) { |
| 762 | throw new RemoteException("Empty payload verifying a credential we just set"); |
| 763 | } |
| 764 | changeUserKey(userId, token, secretFromCredential(credential)); |
| 765 | } |
| 766 | |
| 767 | private void clearUserKeyProtection(int userId) throws RemoteException { |
| 768 | changeUserKey(userId, null, null); |
| 769 | } |
| 770 | |
| 771 | private static byte[] secretFromCredential(String credential) throws RemoteException { |
| 772 | try { |
| 773 | MessageDigest digest = MessageDigest.getInstance("SHA-512"); |
| 774 | // Personalize the hash |
| 775 | byte[] personalization = "Android FBE credential hash" |
| 776 | .getBytes(StandardCharsets.UTF_8); |
| 777 | // Pad it to the block size of the hash function |
| 778 | personalization = Arrays.copyOf(personalization, 128); |
| 779 | digest.update(personalization); |
| 780 | digest.update(credential.getBytes(StandardCharsets.UTF_8)); |
| 781 | return digest.digest(); |
| 782 | } catch (NoSuchAlgorithmException e) { |
| 783 | throw new RuntimeException("NoSuchAlgorithmException for SHA-512"); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | private void changeUserKey(int userId, byte[] token, byte[] secret) |
| 788 | throws RemoteException { |
| 789 | final UserInfo userInfo = UserManager.get(mContext).getUserInfo(userId); |
| 790 | getMountService().changeUserKey(userId, userInfo.serialNumber, token, null, secret); |
| 791 | } |
| 792 | |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 793 | @Override |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 794 | public VerifyCredentialResponse checkPattern(String pattern, int userId) throws RemoteException { |
| 795 | return doVerifyPattern(pattern, false, 0, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 796 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 797 | |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 798 | @Override |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 799 | public VerifyCredentialResponse verifyPattern(String pattern, long challenge, int userId) |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 800 | throws RemoteException { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 801 | return doVerifyPattern(pattern, true, challenge, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 804 | private VerifyCredentialResponse doVerifyPattern(String pattern, boolean hasChallenge, |
| 805 | long challenge, int userId) throws RemoteException { |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 806 | checkPasswordReadPermission(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 807 | CredentialHash storedHash = mStorage.readPatternHash(userId); |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 808 | boolean shouldReEnrollBaseZero = storedHash != null && storedHash.isBaseZeroPattern; |
| 809 | |
| 810 | String patternToVerify; |
| 811 | if (shouldReEnrollBaseZero) { |
| 812 | patternToVerify = LockPatternUtils.patternStringToBaseZero(pattern); |
| 813 | } else { |
| 814 | patternToVerify = pattern; |
| 815 | } |
| 816 | |
| 817 | VerifyCredentialResponse response = verifyCredential(userId, storedHash, patternToVerify, |
| 818 | hasChallenge, challenge, |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 819 | new CredentialUtil() { |
| 820 | @Override |
| 821 | public void setCredential(String pattern, String oldPattern, int userId) |
| 822 | throws RemoteException { |
| 823 | setLockPattern(pattern, oldPattern, userId); |
| 824 | } |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 825 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 826 | @Override |
| 827 | public byte[] toHash(String pattern, int userId) { |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 828 | return LockPatternUtils.patternToHash( |
| 829 | LockPatternUtils.stringToPattern(pattern)); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 830 | } |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 831 | |
| 832 | @Override |
| 833 | public String adjustForKeystore(String pattern) { |
| 834 | return LockPatternUtils.patternStringToBaseZero(pattern); |
| 835 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 836 | } |
| 837 | ); |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 838 | |
| 839 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK |
| 840 | && shouldReEnrollBaseZero) { |
| 841 | setLockPattern(pattern, patternToVerify, userId); |
| 842 | } |
| 843 | |
| 844 | return response; |
| 845 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | @Override |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 849 | public VerifyCredentialResponse checkPassword(String password, int userId) |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 850 | throws RemoteException { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 851 | return doVerifyPassword(password, false, 0, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 854 | @Override |
| 855 | public VerifyCredentialResponse verifyPassword(String password, long challenge, int userId) |
| 856 | throws RemoteException { |
| 857 | return doVerifyPassword(password, true, challenge, userId); |
| 858 | } |
| 859 | |
| 860 | private VerifyCredentialResponse doVerifyPassword(String password, boolean hasChallenge, |
| 861 | long challenge, int userId) throws RemoteException { |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 862 | checkPasswordReadPermission(userId); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 863 | CredentialHash storedHash = mStorage.readPasswordHash(userId); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 864 | return verifyCredential(userId, storedHash, password, hasChallenge, challenge, |
| 865 | new CredentialUtil() { |
| 866 | @Override |
| 867 | public void setCredential(String password, String oldPassword, int userId) |
| 868 | throws RemoteException { |
| 869 | setLockPassword(password, oldPassword, userId); |
| 870 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 871 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 872 | @Override |
| 873 | public byte[] toHash(String password, int userId) { |
| 874 | return mLockPatternUtils.passwordToHash(password, userId); |
| 875 | } |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 876 | |
| 877 | @Override |
| 878 | public String adjustForKeystore(String password) { |
| 879 | return password; |
| 880 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 881 | } |
| 882 | ); |
| 883 | } |
| 884 | |
| 885 | private VerifyCredentialResponse verifyCredential(int userId, CredentialHash storedHash, |
| 886 | String credential, boolean hasChallenge, long challenge, CredentialUtil credentialUtil) |
| 887 | throws RemoteException { |
| 888 | if ((storedHash == null || storedHash.hash.length == 0) && TextUtils.isEmpty(credential)) { |
| 889 | // don't need to pass empty credentials to GateKeeper |
| 890 | return VerifyCredentialResponse.OK; |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 893 | if (TextUtils.isEmpty(credential)) { |
| 894 | return VerifyCredentialResponse.ERROR; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 895 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 896 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 897 | if (storedHash.version == CredentialHash.VERSION_LEGACY) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 898 | byte[] hash = credentialUtil.toHash(credential, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 899 | if (Arrays.equals(hash, storedHash.hash)) { |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 900 | unlockKeystore(credentialUtil.adjustForKeystore(credential), userId); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 901 | |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 902 | // Users with legacy credentials don't have credential-backed |
| 903 | // FBE keys, so just pass through a fake token/secret |
| 904 | Slog.i(TAG, "Unlocking user with fake token: " + userId); |
| 905 | final byte[] fakeToken = String.valueOf(userId).getBytes(); |
| 906 | unlockUser(userId, fakeToken, fakeToken); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 907 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 908 | // migrate credential to GateKeeper |
| 909 | credentialUtil.setCredential(credential, null, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 910 | if (!hasChallenge) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 911 | return VerifyCredentialResponse.OK; |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 912 | } |
| 913 | // Fall through to get the auth token. Technically this should never happen, |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 914 | // as a user that had a legacy credential would have to unlock their device |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 915 | // before getting to a flow with a challenge, but supporting for consistency. |
| 916 | } else { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 917 | return VerifyCredentialResponse.ERROR; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 918 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 921 | VerifyCredentialResponse response; |
Paul Crowley | 98e0a26 | 2016-02-04 09:41:53 +0000 | [diff] [blame] | 922 | boolean shouldReEnroll = false; |
| 923 | GateKeeperResponse gateKeeperResponse = getGateKeeperService() |
| 924 | .verifyChallenge(userId, challenge, storedHash.hash, credential.getBytes()); |
| 925 | int responseCode = gateKeeperResponse.getResponseCode(); |
| 926 | if (responseCode == GateKeeperResponse.RESPONSE_RETRY) { |
| 927 | response = new VerifyCredentialResponse(gateKeeperResponse.getTimeout()); |
| 928 | } else if (responseCode == GateKeeperResponse.RESPONSE_OK) { |
| 929 | byte[] token = gateKeeperResponse.getPayload(); |
| 930 | if (token == null) { |
| 931 | // something's wrong if there's no payload with a challenge |
| 932 | Slog.e(TAG, "verifyChallenge response had no associated payload"); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 933 | response = VerifyCredentialResponse.ERROR; |
Paul Crowley | 98e0a26 | 2016-02-04 09:41:53 +0000 | [diff] [blame] | 934 | } else { |
| 935 | shouldReEnroll = gateKeeperResponse.getShouldReEnroll(); |
| 936 | response = new VerifyCredentialResponse(token); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 937 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 938 | } else { |
Paul Crowley | 98e0a26 | 2016-02-04 09:41:53 +0000 | [diff] [blame] | 939 | response = VerifyCredentialResponse.ERROR; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 940 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 941 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 942 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 943 | // credential has matched |
| 944 | unlockKeystore(credential, userId); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 945 | |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 946 | Slog.i(TAG, "Unlocking user " + userId + |
| 947 | " with token length " + response.getPayload().length); |
| 948 | unlockUser(userId, response.getPayload(), secretFromCredential(credential)); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 949 | |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 950 | UserInfo info = UserManager.get(mContext).getUserInfo(userId); |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 951 | if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) { |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 952 | TrustManager trustManager = |
| 953 | (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE); |
| 954 | trustManager.setDeviceLockedForUser(userId, false); |
| 955 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 956 | if (shouldReEnroll) { |
| 957 | credentialUtil.setCredential(credential, credential, userId); |
| 958 | } |
Adrian Roos | 873010d | 2015-08-25 15:59:00 -0700 | [diff] [blame] | 959 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 960 | if (response.getTimeout() > 0) { |
| 961 | requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_LOCKOUT, userId); |
| 962 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 963 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 964 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 965 | return response; |
| 966 | } |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 967 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 968 | @Override |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 969 | public boolean checkVoldPassword(int userId) throws RemoteException { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 970 | if (!mFirstCallToVold) { |
| 971 | return false; |
| 972 | } |
| 973 | mFirstCallToVold = false; |
| 974 | |
| 975 | checkPasswordReadPermission(userId); |
| 976 | |
| 977 | // There's no guarantee that this will safely connect, but if it fails |
| 978 | // we will simply show the lock screen when we shouldn't, so relatively |
| 979 | // benign. There is an outside chance something nasty would happen if |
| 980 | // this service restarted before vold stales out the password in this |
| 981 | // case. The nastiness is limited to not showing the lock screen when |
| 982 | // we should, within the first minute of decrypting the phone if this |
| 983 | // service can't connect to vold, it restarts, and then the new instance |
| 984 | // does successfully connect. |
| 985 | final IMountService service = getMountService(); |
| 986 | String password = service.getPassword(); |
| 987 | service.clearPassword(); |
| 988 | if (password == null) { |
| 989 | return false; |
| 990 | } |
| 991 | |
| 992 | try { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 993 | if (mLockPatternUtils.isLockPatternEnabled(userId)) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 994 | if (checkPattern(password, userId).getResponseCode() |
| 995 | == GateKeeperResponse.RESPONSE_OK) { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 996 | return true; |
| 997 | } |
| 998 | } |
| 999 | } catch (Exception e) { |
| 1000 | } |
| 1001 | |
| 1002 | try { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1003 | if (mLockPatternUtils.isLockPasswordEnabled(userId)) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1004 | if (checkPassword(password, userId).getResponseCode() |
| 1005 | == GateKeeperResponse.RESPONSE_OK) { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1006 | return true; |
| 1007 | } |
| 1008 | } |
| 1009 | } catch (Exception e) { |
| 1010 | } |
| 1011 | |
| 1012 | return false; |
| 1013 | } |
| 1014 | |
Adrian Roos | db0f76e | 2015-01-07 22:19:38 +0100 | [diff] [blame] | 1015 | private void removeUser(int userId) { |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1016 | mStorage.removeUser(userId); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1017 | mStrongAuth.removeUser(userId); |
Robin Lee | 49d810c | 2014-09-23 13:50:22 +0100 | [diff] [blame] | 1018 | |
| 1019 | final KeyStore ks = KeyStore.getInstance(); |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 1020 | ks.onUserRemoved(userId); |
Andres Morales | 070fe63 | 2015-06-24 10:37:10 -0700 | [diff] [blame] | 1021 | |
| 1022 | try { |
| 1023 | final IGateKeeperService gk = getGateKeeperService(); |
| 1024 | if (gk != null) { |
| 1025 | gk.clearSecureUserId(userId); |
| 1026 | } |
| 1027 | } catch (RemoteException ex) { |
| 1028 | Slog.w(TAG, "unable to clear GK secure user id"); |
| 1029 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1032 | @Override |
| 1033 | public void registerStrongAuthTracker(IStrongAuthTracker tracker) { |
| 1034 | checkPasswordReadPermission(UserHandle.USER_ALL); |
| 1035 | mStrongAuth.registerStrongAuthTracker(tracker); |
| 1036 | } |
| 1037 | |
| 1038 | @Override |
| 1039 | public void unregisterStrongAuthTracker(IStrongAuthTracker tracker) { |
| 1040 | checkPasswordReadPermission(UserHandle.USER_ALL); |
| 1041 | mStrongAuth.unregisterStrongAuthTracker(tracker); |
| 1042 | } |
| 1043 | |
| 1044 | @Override |
| 1045 | public void requireStrongAuth(int strongAuthReason, int userId) { |
| 1046 | checkWritePermission(userId); |
| 1047 | mStrongAuth.requireStrongAuth(strongAuthReason, userId); |
| 1048 | } |
| 1049 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1050 | private static final String[] VALID_SETTINGS = new String[] { |
| 1051 | LockPatternUtils.LOCKOUT_PERMANENT_KEY, |
| 1052 | LockPatternUtils.LOCKOUT_ATTEMPT_DEADLINE, |
| 1053 | LockPatternUtils.PATTERN_EVER_CHOSEN_KEY, |
| 1054 | LockPatternUtils.PASSWORD_TYPE_KEY, |
| 1055 | LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 1056 | LockPatternUtils.LOCK_PASSWORD_SALT_KEY, |
| 1057 | LockPatternUtils.DISABLE_LOCKSCREEN_KEY, |
| 1058 | LockPatternUtils.LOCKSCREEN_OPTIONS, |
| 1059 | LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, |
| 1060 | LockPatternUtils.BIOMETRIC_WEAK_EVER_CHOSEN_KEY, |
| 1061 | LockPatternUtils.LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, |
| 1062 | LockPatternUtils.PASSWORD_HISTORY_KEY, |
| 1063 | Secure.LOCK_PATTERN_ENABLED, |
| 1064 | Secure.LOCK_BIOMETRIC_WEAK_FLAGS, |
| 1065 | Secure.LOCK_PATTERN_VISIBLE, |
| 1066 | Secure.LOCK_PATTERN_TACTILE_FEEDBACK_ENABLED |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 1067 | }; |
| 1068 | |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 1069 | // Reading these settings needs the contacts permission |
| 1070 | private static final String[] READ_CONTACTS_PROTECTED_SETTINGS = new String[] { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 1071 | Secure.LOCK_SCREEN_OWNER_INFO_ENABLED, |
| 1072 | Secure.LOCK_SCREEN_OWNER_INFO |
| 1073 | }; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1074 | |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 1075 | // Reading these settings needs the same permission as checking the password |
| 1076 | private static final String[] READ_PASSWORD_PROTECTED_SETTINGS = new String[] { |
| 1077 | LockPatternUtils.LOCK_PASSWORD_SALT_KEY, |
| 1078 | LockPatternUtils.PASSWORD_HISTORY_KEY, |
Adrian Roos | 855fa30 | 2015-04-02 16:01:12 +0200 | [diff] [blame] | 1079 | LockPatternUtils.PASSWORD_TYPE_KEY, |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 1080 | }; |
| 1081 | |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 1082 | private static final String[] SETTINGS_TO_BACKUP = new String[] { |
| 1083 | Secure.LOCK_SCREEN_OWNER_INFO_ENABLED, |
| 1084 | Secure.LOCK_SCREEN_OWNER_INFO |
| 1085 | }; |
| 1086 | |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1087 | private IMountService getMountService() { |
| 1088 | final IBinder service = ServiceManager.getService("mount"); |
| 1089 | if (service != null) { |
| 1090 | return IMountService.Stub.asInterface(service); |
| 1091 | } |
| 1092 | return null; |
| 1093 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1094 | |
Andres Morales | 301ea44 | 2015-04-17 09:15:47 -0700 | [diff] [blame] | 1095 | private class GateKeeperDiedRecipient implements IBinder.DeathRecipient { |
| 1096 | @Override |
| 1097 | public void binderDied() { |
| 1098 | mGateKeeperService.asBinder().unlinkToDeath(this, 0); |
| 1099 | mGateKeeperService = null; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | private synchronized IGateKeeperService getGateKeeperService() |
| 1104 | throws RemoteException { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1105 | if (mGateKeeperService != null) { |
| 1106 | return mGateKeeperService; |
| 1107 | } |
| 1108 | |
| 1109 | final IBinder service = |
| 1110 | ServiceManager.getService("android.service.gatekeeper.IGateKeeperService"); |
| 1111 | if (service != null) { |
Andres Morales | 301ea44 | 2015-04-17 09:15:47 -0700 | [diff] [blame] | 1112 | service.linkToDeath(new GateKeeperDiedRecipient(), 0); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1113 | mGateKeeperService = IGateKeeperService.Stub.asInterface(service); |
| 1114 | return mGateKeeperService; |
| 1115 | } |
| 1116 | |
| 1117 | Slog.e(TAG, "Unable to acquire GateKeeperService"); |
| 1118 | return null; |
| 1119 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1120 | } |