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; |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 59 | import android.security.keystore.AndroidKeyStoreProvider; |
| 60 | import android.security.keystore.KeyProperties; |
| 61 | import android.security.keystore.KeyProtection; |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 62 | import android.service.gatekeeper.GateKeeperResponse; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 63 | import android.service.gatekeeper.IGateKeeperService; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 64 | import android.text.TextUtils; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 65 | import android.util.Log; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 66 | import android.util.Slog; |
| 67 | |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 68 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 69 | import com.android.internal.widget.ILockSettings; |
| 70 | import com.android.internal.widget.LockPatternUtils; |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 71 | import com.android.internal.widget.VerifyCredentialResponse; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 72 | import com.android.server.LockSettingsStorage.CredentialHash; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 73 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 74 | import libcore.util.HexEncoding; |
| 75 | |
| 76 | import java.io.ByteArrayOutputStream; |
| 77 | import java.io.FileNotFoundException; |
| 78 | import java.io.IOException; |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 79 | import java.nio.charset.StandardCharsets; |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 80 | import java.security.InvalidAlgorithmParameterException; |
| 81 | import java.security.InvalidKeyException; |
| 82 | import java.security.KeyStoreException; |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 83 | import java.security.MessageDigest; |
| 84 | import java.security.NoSuchAlgorithmException; |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 85 | import java.security.SecureRandom; |
| 86 | import java.security.UnrecoverableKeyException; |
| 87 | import java.security.cert.CertificateException; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 88 | import java.util.Arrays; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 89 | import java.util.List; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 90 | import java.util.concurrent.CountDownLatch; |
| 91 | import java.util.concurrent.TimeUnit; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 92 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 93 | import javax.crypto.BadPaddingException; |
| 94 | import javax.crypto.Cipher; |
| 95 | import javax.crypto.IllegalBlockSizeException; |
| 96 | import javax.crypto.KeyGenerator; |
| 97 | import javax.crypto.NoSuchPaddingException; |
| 98 | import javax.crypto.SecretKey; |
| 99 | import javax.crypto.spec.GCMParameterSpec; |
| 100 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Keeps the lock pattern/password data and related settings for each user. |
| 103 | * Used by LockPatternUtils. Needs to be a service because Settings app also needs |
| 104 | * to be able to save lockscreen information for secondary users. |
| 105 | * @hide |
| 106 | */ |
| 107 | public class LockSettingsService extends ILockSettings.Stub { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 108 | private static final String TAG = "LockSettingsService"; |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 109 | private static final String PERMISSION = ACCESS_KEYGUARD_SECURE_STORAGE; |
| 110 | private static final Intent ACTION_NULL; // hack to ensure notification shows the bouncer |
| 111 | private static final int FBE_ENCRYPTED_NOTIFICATION = 0; |
| 112 | private static final boolean DEBUG = false; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 113 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 114 | private static final String PROFILE_KEY_NAME_ENCRYPT = "profile_key_name_encrypt_"; |
| 115 | private static final String PROFILE_KEY_NAME_DECRYPT = "profile_key_name_decrypt_"; |
| 116 | private static final int PROFILE_KEY_IV_SIZE = 12; |
| 117 | private static final String SEPARATE_PROFILE_CHALLENGE_KEY = "lockscreen.profilechallenge"; |
| 118 | private final Object mSeparateChallengeLock = new Object(); |
| 119 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 120 | private final Context mContext; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 121 | private final LockSettingsStorage mStorage; |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 122 | private final LockSettingsStrongAuth mStrongAuth; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 123 | |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 124 | private LockPatternUtils mLockPatternUtils; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 125 | private boolean mFirstCallToVold; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 126 | private IGateKeeperService mGateKeeperService; |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 127 | private NotificationManager mNotificationManager; |
| 128 | private UserManager mUserManager; |
| 129 | |
| 130 | static { |
| 131 | // Just launch the home screen, which happens anyway |
| 132 | ACTION_NULL = new Intent(Intent.ACTION_MAIN); |
| 133 | ACTION_NULL.addCategory(Intent.CATEGORY_HOME); |
| 134 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 135 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 136 | private interface CredentialUtil { |
| 137 | void setCredential(String credential, String savedCredential, int userId) |
| 138 | throws RemoteException; |
| 139 | byte[] toHash(String credential, int userId); |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 140 | String adjustForKeystore(String credential); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 143 | // This class manages life cycle events for encrypted users on File Based Encryption (FBE) |
| 144 | // devices. The most basic of these is to show/hide notifications about missing features until |
| 145 | // the user unlocks the account and credential-encrypted storage is available. |
| 146 | public static final class Lifecycle extends SystemService { |
| 147 | private LockSettingsService mLockSettingsService; |
| 148 | |
| 149 | public Lifecycle(Context context) { |
| 150 | super(context); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void onStart() { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 155 | AndroidKeyStoreProvider.install(); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 156 | mLockSettingsService = new LockSettingsService(getContext()); |
| 157 | publishBinderService("lock_settings", mLockSettingsService); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void onBootPhase(int phase) { |
| 162 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 163 | mLockSettingsService.maybeShowEncryptionNotifications(); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 164 | } else if (phase == SystemService.PHASE_BOOT_COMPLETED) { |
| 165 | // TODO |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void onUnlockUser(int userHandle) { |
| 171 | mLockSettingsService.onUnlockUser(userHandle); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void onCleanupUser(int userHandle) { |
| 176 | mLockSettingsService.onCleanupUser(userHandle); |
| 177 | } |
| 178 | } |
| 179 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 180 | /** |
| 181 | * Tie managed profile to primary profile if it is in unified mode and not tied before. |
| 182 | * |
| 183 | * @param managedUserId Managed profile user Id |
| 184 | * @param managedUserPassword Managed profile original password (when it has separated lock). |
| 185 | * NULL when it does not have a separated lock before. |
| 186 | */ |
| 187 | public void tieManagedProfileLockIfNecessary(int managedUserId, String managedUserPassword) { |
| 188 | if (DEBUG) Slog.v(TAG, "Check child profile lock for user: " + managedUserId); |
| 189 | // Only for managed profile |
| 190 | if (!UserManager.get(mContext).getUserInfo(managedUserId).isManagedProfile()) { |
| 191 | return; |
| 192 | } |
| 193 | // Do not tie managed profile when work challenge is enabled |
| 194 | if (mLockPatternUtils.isSeparateProfileChallengeEnabled(managedUserId)) { |
| 195 | return; |
| 196 | } |
| 197 | // Do not tie managed profile to parent when it's done already |
| 198 | if (mStorage.hasChildProfileLock(managedUserId)) { |
| 199 | return; |
| 200 | } |
| 201 | // Do not tie it to parent when parent does not have a screen lock |
| 202 | final int parentId = mUserManager.getProfileParent(managedUserId).id; |
| 203 | if (!mStorage.hasPassword(parentId) && !mStorage.hasPattern(parentId)) { |
| 204 | if (DEBUG) Slog.v(TAG, "Parent does not have a screen lock"); |
| 205 | return; |
| 206 | } |
| 207 | if (DEBUG) Slog.v(TAG, "Tie managed profile to parent now!"); |
| 208 | byte[] randomLockSeed = new byte[] {}; |
| 209 | try { |
| 210 | randomLockSeed = SecureRandom.getInstance("SHA1PRNG").generateSeed(40); |
| 211 | String newPassword = String.valueOf(HexEncoding.encode(randomLockSeed)); |
| 212 | setLockPasswordInternal(newPassword, managedUserPassword, managedUserId); |
| 213 | tieProfileLockToParent(managedUserId, newPassword); |
| 214 | } catch (NoSuchAlgorithmException | RemoteException e) { |
| 215 | Slog.e(TAG, "Fail to tie managed profile", e); |
| 216 | // Nothing client can do to fix this issue, so we do not throw exception out |
| 217 | } |
| 218 | } |
| 219 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 220 | public LockSettingsService(Context context) { |
| 221 | mContext = context; |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 222 | mStrongAuth = new LockSettingsStrongAuth(context); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 223 | // Open the database |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 224 | |
| 225 | mLockPatternUtils = new LockPatternUtils(context); |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 226 | mFirstCallToVold = true; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 227 | |
| 228 | IntentFilter filter = new IntentFilter(); |
| 229 | filter.addAction(Intent.ACTION_USER_ADDED); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 230 | filter.addAction(Intent.ACTION_USER_STARTING); |
Adrian Roos | db0f76e | 2015-01-07 22:19:38 +0100 | [diff] [blame] | 231 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 232 | mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 233 | |
| 234 | mStorage = new LockSettingsStorage(context, new LockSettingsStorage.Callback() { |
| 235 | @Override |
| 236 | public void initialize(SQLiteDatabase db) { |
| 237 | // Get the lockscreen default from a system property, if available |
| 238 | boolean lockScreenDisable = SystemProperties.getBoolean( |
| 239 | "ro.lockscreen.disable.default", false); |
| 240 | if (lockScreenDisable) { |
| 241 | mStorage.writeKeyValue(db, LockPatternUtils.DISABLE_LOCKSCREEN_KEY, "1", 0); |
| 242 | } |
| 243 | } |
| 244 | }); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 245 | mNotificationManager = (NotificationManager) |
| 246 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 247 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * If the account is credential-encrypted, show notification requesting the user to unlock |
| 252 | * the device. |
| 253 | */ |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 254 | private void maybeShowEncryptionNotifications() { |
| 255 | final List<UserInfo> users = mUserManager.getUsers(); |
| 256 | for (int i = 0; i < users.size(); i++) { |
| 257 | UserInfo user = users.get(i); |
| 258 | UserHandle userHandle = user.getUserHandle(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 259 | if (!mUserManager.isUserUnlockingOrUnlocked(userHandle)) { |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 260 | if (!user.isManagedProfile()) { |
| 261 | showEncryptionNotification(userHandle); |
| 262 | } else { |
| 263 | UserInfo parent = mUserManager.getProfileParent(user.id); |
Benjamin Franz | f52709c | 2016-04-01 15:49:13 +0100 | [diff] [blame] | 264 | if (parent != null && |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 265 | mUserManager.isUserUnlockingOrUnlocked(parent.getUserHandle()) && |
Benjamin Franz | f52709c | 2016-04-01 15:49:13 +0100 | [diff] [blame] | 266 | !mUserManager.isQuietModeEnabled(userHandle)) { |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 267 | // Only show notifications for managed profiles once their parent |
| 268 | // user is unlocked. |
| 269 | showEncryptionNotificationForProfile(userHandle); |
| 270 | } |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 271 | } |
| 272 | } |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 276 | private void showEncryptionNotificationForProfile(UserHandle user) { |
| 277 | Resources r = mContext.getResources(); |
| 278 | CharSequence title = r.getText( |
| 279 | com.android.internal.R.string.user_encrypted_title); |
| 280 | CharSequence message = r.getText( |
| 281 | com.android.internal.R.string.profile_encrypted_message); |
| 282 | CharSequence detail = r.getText( |
| 283 | com.android.internal.R.string.profile_encrypted_detail); |
| 284 | |
| 285 | final KeyguardManager km = (KeyguardManager) mContext.getSystemService(KEYGUARD_SERVICE); |
| 286 | final Intent unlockIntent = km.createConfirmDeviceCredentialIntent(null, null, user.getIdentifier()); |
| 287 | if (unlockIntent == null) { |
| 288 | return; |
| 289 | } |
| 290 | unlockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 291 | PendingIntent intent = PendingIntent.getActivity(mContext, 0, unlockIntent, |
| 292 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 293 | |
| 294 | showEncryptionNotification(user, title, message, detail, intent); |
| 295 | } |
| 296 | |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 297 | private void showEncryptionNotification(UserHandle user) { |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 298 | Resources r = mContext.getResources(); |
| 299 | CharSequence title = r.getText( |
| 300 | com.android.internal.R.string.user_encrypted_title); |
| 301 | CharSequence message = r.getText( |
| 302 | com.android.internal.R.string.user_encrypted_message); |
| 303 | CharSequence detail = r.getText( |
| 304 | com.android.internal.R.string.user_encrypted_detail); |
| 305 | |
| 306 | PendingIntent intent = PendingIntent.getBroadcast(mContext, 0, ACTION_NULL, |
| 307 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 308 | |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 309 | showEncryptionNotification(user, title, message, detail, intent); |
| 310 | } |
| 311 | |
| 312 | private void showEncryptionNotification(UserHandle user, CharSequence title, CharSequence message, |
| 313 | CharSequence detail, PendingIntent intent) { |
| 314 | if (DEBUG) Slog.v(TAG, "showing encryption notification, user: " + user.getIdentifier()); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 315 | Notification notification = new Notification.Builder(mContext) |
Jim Miller | b1135b7 | 2016-02-02 17:08:56 -0800 | [diff] [blame] | 316 | .setSmallIcon(com.android.internal.R.drawable.ic_user_secure) |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 317 | .setWhen(0) |
| 318 | .setOngoing(true) |
| 319 | .setTicker(title) |
| 320 | .setDefaults(0) // please be quiet |
| 321 | .setPriority(Notification.PRIORITY_MAX) |
| 322 | .setColor(mContext.getColor( |
| 323 | com.android.internal.R.color.system_notification_accent_color)) |
| 324 | .setContentTitle(title) |
| 325 | .setContentText(message) |
Selim Cinek | 0f9dd1e | 2016-04-05 17:03:40 -0700 | [diff] [blame] | 326 | .setSubText(detail) |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 327 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 328 | .setContentIntent(intent) |
| 329 | .build(); |
| 330 | mNotificationManager.notifyAsUser(null, FBE_ENCRYPTED_NOTIFICATION, notification, user); |
| 331 | } |
| 332 | |
| 333 | public void hideEncryptionNotification(UserHandle userHandle) { |
| 334 | if (DEBUG) Slog.v(TAG, "hide encryption notification, user: "+ userHandle.getIdentifier()); |
| 335 | mNotificationManager.cancelAsUser(null, FBE_ENCRYPTED_NOTIFICATION, userHandle); |
| 336 | } |
| 337 | |
| 338 | public void onCleanupUser(int userId) { |
| 339 | hideEncryptionNotification(new UserHandle(userId)); |
| 340 | } |
| 341 | |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 342 | public void onUnlockUser(int userId) { |
Ricky Wai | 8481258 | 2016-05-10 20:11:59 +0100 | [diff] [blame] | 343 | // Hide notification first, as tie managed profile lock takes time |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 344 | hideEncryptionNotification(new UserHandle(userId)); |
Ricky Wai | 8481258 | 2016-05-10 20:11:59 +0100 | [diff] [blame] | 345 | tieManagedProfileLockIfNecessary(userId, null); |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 346 | |
| 347 | // Now we have unlocked the parent user we should show notifications |
| 348 | // about any profiles that exist. |
| 349 | List<UserInfo> profiles = mUserManager.getProfiles(userId); |
| 350 | for (int i = 0; i < profiles.size(); i++) { |
| 351 | UserInfo profile = profiles.get(i); |
| 352 | if (profile.isManagedProfile()) { |
| 353 | UserHandle userHandle = profile.getUserHandle(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 354 | if (!mUserManager.isUserUnlockingOrUnlocked(userHandle) && |
Benjamin Franz | f52709c | 2016-04-01 15:49:13 +0100 | [diff] [blame] | 355 | !mUserManager.isQuietModeEnabled(userHandle)) { |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 356 | showEncryptionNotificationForProfile(userHandle); |
| 357 | } |
| 358 | } |
| 359 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 362 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 363 | @Override |
| 364 | public void onReceive(Context context, Intent intent) { |
Robin Lee | 1096cf8 | 2014-09-01 16:52:47 +0100 | [diff] [blame] | 365 | if (Intent.ACTION_USER_ADDED.equals(intent.getAction())) { |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 366 | // Notify keystore that a new user was added. |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 367 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
Robin Lee | 49d810c | 2014-09-23 13:50:22 +0100 | [diff] [blame] | 368 | final KeyStore ks = KeyStore.getInstance(); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 369 | final UserInfo parentInfo = mUserManager.getProfileParent(userHandle); |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 370 | final int parentHandle = parentInfo != null ? parentInfo.id : -1; |
| 371 | ks.onUserAdded(userHandle, parentHandle); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 372 | } else if (Intent.ACTION_USER_STARTING.equals(intent.getAction())) { |
| 373 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 374 | mStorage.prefetchUser(userHandle); |
Adrian Roos | db0f76e | 2015-01-07 22:19:38 +0100 | [diff] [blame] | 375 | } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) { |
| 376 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 377 | if (userHandle > 0) { |
| 378 | removeUser(userHandle); |
| 379 | } |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | }; |
| 383 | |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 384 | @Override // binder interface |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 385 | public void systemReady() { |
| 386 | migrateOldData(); |
Andres Morales | 301ea44 | 2015-04-17 09:15:47 -0700 | [diff] [blame] | 387 | try { |
| 388 | getGateKeeperService(); |
| 389 | } catch (RemoteException e) { |
| 390 | Slog.e(TAG, "Failure retrieving IGateKeeperService", e); |
| 391 | } |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 392 | // TODO: maybe skip this for split system user mode. |
| 393 | mStorage.prefetchUser(UserHandle.USER_SYSTEM); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | private void migrateOldData() { |
| 397 | try { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 398 | // These Settings moved before multi-user was enabled, so we only have to do it for the |
| 399 | // root user. |
| 400 | if (getString("migrated", null, 0) == null) { |
| 401 | final ContentResolver cr = mContext.getContentResolver(); |
| 402 | for (String validSetting : VALID_SETTINGS) { |
| 403 | String value = Settings.Secure.getString(cr, validSetting); |
| 404 | if (value != null) { |
| 405 | setString(validSetting, value, 0); |
| 406 | } |
| 407 | } |
| 408 | // No need to move the password / pattern files. They're already in the right place. |
| 409 | setString("migrated", "true", 0); |
| 410 | Slog.i(TAG, "Migrated lock settings to new location"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 413 | // These Settings changed after multi-user was enabled, hence need to be moved per user. |
| 414 | if (getString("migrated_user_specific", null, 0) == null) { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 415 | final ContentResolver cr = mContext.getContentResolver(); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 416 | List<UserInfo> users = mUserManager.getUsers(); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 417 | for (int user = 0; user < users.size(); user++) { |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 418 | // Migrate owner info |
| 419 | final int userId = users.get(user).id; |
| 420 | final String OWNER_INFO = Secure.LOCK_SCREEN_OWNER_INFO; |
| 421 | String ownerInfo = Settings.Secure.getStringForUser(cr, OWNER_INFO, userId); |
Jim Miller | 325c567 | 2016-03-01 19:21:47 -0800 | [diff] [blame] | 422 | if (!TextUtils.isEmpty(ownerInfo)) { |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 423 | setString(OWNER_INFO, ownerInfo, userId); |
Jim Miller | 325c567 | 2016-03-01 19:21:47 -0800 | [diff] [blame] | 424 | Settings.Secure.putStringForUser(cr, OWNER_INFO, "", userId); |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 425 | } |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 426 | |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 427 | // Migrate owner info enabled. Note there was a bug where older platforms only |
| 428 | // stored this value if the checkbox was toggled at least once. The code detects |
| 429 | // this case by handling the exception. |
| 430 | final String OWNER_INFO_ENABLED = Secure.LOCK_SCREEN_OWNER_INFO_ENABLED; |
| 431 | boolean enabled; |
| 432 | try { |
| 433 | int ivalue = Settings.Secure.getIntForUser(cr, OWNER_INFO_ENABLED, userId); |
| 434 | enabled = ivalue != 0; |
| 435 | setLong(OWNER_INFO_ENABLED, enabled ? 1 : 0, userId); |
| 436 | } catch (SettingNotFoundException e) { |
| 437 | // Setting was never stored. Store it if the string is not empty. |
| 438 | if (!TextUtils.isEmpty(ownerInfo)) { |
| 439 | setLong(OWNER_INFO_ENABLED, 1, userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 442 | Settings.Secure.putIntForUser(cr, OWNER_INFO_ENABLED, 0, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 443 | } |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 444 | // No need to move the password / pattern files. They're already in the right place. |
| 445 | setString("migrated_user_specific", "true", 0); |
| 446 | Slog.i(TAG, "Migrated per-user lock settings to new location"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 447 | } |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 448 | |
| 449 | // Migrates biometric weak such that the fallback mechanism becomes the primary. |
| 450 | if (getString("migrated_biometric_weak", null, 0) == null) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 451 | List<UserInfo> users = mUserManager.getUsers(); |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 452 | for (int i = 0; i < users.size(); i++) { |
| 453 | int userId = users.get(i).id; |
| 454 | long type = getLong(LockPatternUtils.PASSWORD_TYPE_KEY, |
| 455 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 456 | userId); |
| 457 | long alternateType = getLong(LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 458 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 459 | userId); |
| 460 | if (type == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK) { |
| 461 | setLong(LockPatternUtils.PASSWORD_TYPE_KEY, |
| 462 | alternateType, |
| 463 | userId); |
| 464 | } |
| 465 | setLong(LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 466 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 467 | userId); |
| 468 | } |
| 469 | setString("migrated_biometric_weak", "true", 0); |
| 470 | Slog.i(TAG, "Migrated biometric weak to use the fallback instead"); |
| 471 | } |
Adrian Roos | 4383058 | 2015-04-21 16:04:43 -0700 | [diff] [blame] | 472 | |
| 473 | // Migrates lockscreen.disabled. Prior to M, the flag was ignored when more than one |
| 474 | // user was present on the system, so if we're upgrading to M and there is more than one |
| 475 | // user we disable the flag to remain consistent. |
| 476 | if (getString("migrated_lockscreen_disabled", null, 0) == null) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 477 | final List<UserInfo> users = mUserManager.getUsers(); |
Adrian Roos | 4383058 | 2015-04-21 16:04:43 -0700 | [diff] [blame] | 478 | final int userCount = users.size(); |
| 479 | int switchableUsers = 0; |
| 480 | for (int i = 0; i < userCount; i++) { |
| 481 | if (users.get(i).supportsSwitchTo()) { |
| 482 | switchableUsers++; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (switchableUsers > 1) { |
| 487 | for (int i = 0; i < userCount; i++) { |
| 488 | int id = users.get(i).id; |
| 489 | |
| 490 | if (getBoolean(LockPatternUtils.DISABLE_LOCKSCREEN_KEY, false, id)) { |
| 491 | setBoolean(LockPatternUtils.DISABLE_LOCKSCREEN_KEY, false, id); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | setString("migrated_lockscreen_disabled", "true", 0); |
| 497 | Slog.i(TAG, "Migrated lockscreen disabled flag"); |
| 498 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 499 | } catch (RemoteException re) { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 500 | Slog.e(TAG, "Unable to migrate old data", re); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 504 | private final void checkWritePermission(int userId) { |
Jim Miller | 505329b | 2013-11-08 13:25:36 -0800 | [diff] [blame] | 505 | mContext.enforceCallingOrSelfPermission(PERMISSION, "LockSettingsWrite"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 508 | private final void checkPasswordReadPermission(int userId) { |
Jim Miller | 505329b | 2013-11-08 13:25:36 -0800 | [diff] [blame] | 509 | mContext.enforceCallingOrSelfPermission(PERMISSION, "LockSettingsRead"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 512 | private final void checkReadPermission(String requestedKey, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 513 | final int callingUid = Binder.getCallingUid(); |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 514 | |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 515 | for (int i = 0; i < READ_CONTACTS_PROTECTED_SETTINGS.length; i++) { |
| 516 | String key = READ_CONTACTS_PROTECTED_SETTINGS[i]; |
| 517 | if (key.equals(requestedKey) && mContext.checkCallingOrSelfPermission(READ_CONTACTS) |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 518 | != PackageManager.PERMISSION_GRANTED) { |
| 519 | throw new SecurityException("uid=" + callingUid |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 520 | + " needs permission " + READ_CONTACTS + " to read " |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 521 | + requestedKey + " for user " + userId); |
| 522 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 523 | } |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 524 | |
| 525 | for (int i = 0; i < READ_PASSWORD_PROTECTED_SETTINGS.length; i++) { |
| 526 | String key = READ_PASSWORD_PROTECTED_SETTINGS[i]; |
| 527 | if (key.equals(requestedKey) && mContext.checkCallingOrSelfPermission(PERMISSION) |
| 528 | != PackageManager.PERMISSION_GRANTED) { |
| 529 | throw new SecurityException("uid=" + callingUid |
| 530 | + " needs permission " + PERMISSION + " to read " |
| 531 | + requestedKey + " for user " + userId); |
| 532 | } |
| 533 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | @Override |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 537 | public boolean getSeparateProfileChallengeEnabled(int userId) throws RemoteException { |
| 538 | synchronized (mSeparateChallengeLock) { |
| 539 | return getBoolean(SEPARATE_PROFILE_CHALLENGE_KEY, false, userId); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public void setSeparateProfileChallengeEnabled(int userId, boolean enabled, |
| 545 | String managedUserPassword) throws RemoteException { |
| 546 | synchronized (mSeparateChallengeLock) { |
| 547 | setBoolean(SEPARATE_PROFILE_CHALLENGE_KEY, enabled, userId); |
| 548 | if (enabled) { |
| 549 | mStorage.removeChildProfileLock(userId); |
| 550 | removeKeystoreProfileKey(userId); |
| 551 | } else { |
| 552 | tieManagedProfileLockIfNecessary(userId, managedUserPassword); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | @Override |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 558 | public void setBoolean(String key, boolean value, int userId) throws RemoteException { |
| 559 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 560 | setStringUnchecked(key, userId, value ? "1" : "0"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | @Override |
| 564 | public void setLong(String key, long value, int userId) throws RemoteException { |
| 565 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 566 | setStringUnchecked(key, userId, Long.toString(value)); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | @Override |
| 570 | public void setString(String key, String value, int userId) throws RemoteException { |
| 571 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 572 | setStringUnchecked(key, userId, value); |
| 573 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 574 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 575 | private void setStringUnchecked(String key, int userId, String value) { |
| 576 | mStorage.writeKeyValue(key, value, userId); |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 577 | if (ArrayUtils.contains(SETTINGS_TO_BACKUP, key)) { |
| 578 | BackupManager.dataChanged("com.android.providers.settings"); |
| 579 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | @Override |
| 583 | public boolean getBoolean(String key, boolean defaultValue, int userId) throws RemoteException { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 584 | checkReadPermission(key, userId); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 585 | String value = getStringUnchecked(key, null, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 586 | return TextUtils.isEmpty(value) ? |
| 587 | defaultValue : (value.equals("1") || value.equals("true")); |
| 588 | } |
| 589 | |
| 590 | @Override |
| 591 | public long getLong(String key, long defaultValue, int userId) throws RemoteException { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 592 | checkReadPermission(key, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 593 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 594 | String value = getStringUnchecked(key, null, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 595 | return TextUtils.isEmpty(value) ? defaultValue : Long.parseLong(value); |
| 596 | } |
| 597 | |
| 598 | @Override |
| 599 | public String getString(String key, String defaultValue, int userId) throws RemoteException { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 600 | checkReadPermission(key, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 601 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 602 | return getStringUnchecked(key, defaultValue, userId); |
| 603 | } |
| 604 | |
| 605 | public String getStringUnchecked(String key, String defaultValue, int userId) { |
| 606 | if (Settings.Secure.LOCK_PATTERN_ENABLED.equals(key)) { |
Adrian Roos | 7811d9f | 2015-07-27 15:10:13 -0700 | [diff] [blame] | 607 | long ident = Binder.clearCallingIdentity(); |
| 608 | try { |
| 609 | return mLockPatternUtils.isLockPatternEnabled(userId) ? "1" : "0"; |
| 610 | } finally { |
| 611 | Binder.restoreCallingIdentity(ident); |
| 612 | } |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 613 | } |
| 614 | |
Bryce Lee | 4614596 | 2015-12-14 14:39:10 -0800 | [diff] [blame] | 615 | if (LockPatternUtils.LEGACY_LOCK_PATTERN_ENABLED.equals(key)) { |
| 616 | key = Settings.Secure.LOCK_PATTERN_ENABLED; |
| 617 | } |
| 618 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 619 | return mStorage.readKeyValue(key, defaultValue, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Adrian Roos | 4f78845 | 2014-05-22 20:45:59 +0200 | [diff] [blame] | 622 | @Override |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 623 | public boolean havePassword(int userId) throws RemoteException { |
| 624 | // Do we need a permissions check here? |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 625 | return mStorage.hasPassword(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | @Override |
| 629 | public boolean havePattern(int userId) throws RemoteException { |
| 630 | // Do we need a permissions check here? |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 631 | return mStorage.hasPattern(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 634 | private void setKeystorePassword(String password, int userHandle) { |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 635 | final KeyStore ks = KeyStore.getInstance(); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 636 | ks.onUserPasswordChanged(userHandle, password); |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | private void unlockKeystore(String password, int userHandle) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 640 | if (DEBUG) Slog.v(TAG, "Unlock keystore for user: " + userHandle); |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 641 | final KeyStore ks = KeyStore.getInstance(); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 642 | ks.unlock(userHandle, password); |
| 643 | } |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 644 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 645 | private String getDecryptedPasswordForTiedProfile(int userId) |
| 646 | throws KeyStoreException, UnrecoverableKeyException, |
| 647 | NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, |
| 648 | InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, |
| 649 | CertificateException, IOException { |
| 650 | if (DEBUG) Slog.v(TAG, "Unlock keystore for child profile"); |
| 651 | byte[] storedData = mStorage.readChildProfileLock(userId); |
| 652 | if (storedData == null) { |
| 653 | throw new FileNotFoundException("Child profile lock file not found"); |
| 654 | } |
| 655 | byte[] iv = Arrays.copyOfRange(storedData, 0, PROFILE_KEY_IV_SIZE); |
| 656 | byte[] encryptedPassword = Arrays.copyOfRange(storedData, PROFILE_KEY_IV_SIZE, |
| 657 | storedData.length); |
| 658 | byte[] decryptionResult; |
| 659 | java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore"); |
| 660 | keyStore.load(null); |
| 661 | SecretKey decryptionKey = (SecretKey) keyStore.getKey( |
| 662 | PROFILE_KEY_NAME_DECRYPT + userId, null); |
| 663 | |
| 664 | Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" |
| 665 | + KeyProperties.BLOCK_MODE_GCM + "/" + KeyProperties.ENCRYPTION_PADDING_NONE); |
| 666 | |
| 667 | cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new GCMParameterSpec(128, iv)); |
| 668 | decryptionResult = cipher.doFinal(encryptedPassword); |
| 669 | return new String(decryptionResult, StandardCharsets.UTF_8); |
| 670 | } |
| 671 | |
| 672 | private void unlockChildProfile(int profileHandle) throws RemoteException { |
| 673 | try { |
| 674 | doVerifyPassword(getDecryptedPasswordForTiedProfile(profileHandle), false, |
| 675 | 0 /* no challenge */, profileHandle); |
| 676 | } catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException |
| 677 | | NoSuchAlgorithmException | NoSuchPaddingException |
| 678 | | InvalidAlgorithmParameterException | IllegalBlockSizeException |
| 679 | | BadPaddingException | CertificateException | IOException e) { |
| 680 | if (e instanceof FileNotFoundException) { |
| 681 | Slog.i(TAG, "Child profile key not found"); |
Clara Bayarri | 0a587d2 | 2016-02-23 14:49:41 -0800 | [diff] [blame] | 682 | } else { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 683 | Slog.e(TAG, "Failed to decrypt child profile key", e); |
Clara Bayarri | 0a587d2 | 2016-02-23 14:49:41 -0800 | [diff] [blame] | 684 | } |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 688 | private void unlockUser(int userId, byte[] token, byte[] secret) { |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 689 | // TODO: make this method fully async so we can update UI with progress strings |
| 690 | final CountDownLatch latch = new CountDownLatch(1); |
| 691 | final IProgressListener listener = new IProgressListener.Stub() { |
| 692 | @Override |
| 693 | public void onStarted(int id, Bundle extras) throws RemoteException { |
Jeff Sharkey | fd24108 | 2016-04-19 15:58:24 -0600 | [diff] [blame] | 694 | Log.d(TAG, "unlockUser started"); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | @Override |
| 698 | public void onProgress(int id, int progress, Bundle extras) throws RemoteException { |
Jeff Sharkey | fd24108 | 2016-04-19 15:58:24 -0600 | [diff] [blame] | 699 | Log.d(TAG, "unlockUser progress " + progress); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | @Override |
| 703 | public void onFinished(int id, Bundle extras) throws RemoteException { |
Jeff Sharkey | fd24108 | 2016-04-19 15:58:24 -0600 | [diff] [blame] | 704 | Log.d(TAG, "unlockUser finished"); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 705 | latch.countDown(); |
| 706 | } |
| 707 | }; |
| 708 | |
Ricky Wai | 7881cf8 | 2016-04-15 17:20:12 +0100 | [diff] [blame] | 709 | // Turn off quite mode if it's enabled, only managed profile can return true for now, it |
| 710 | // will return false if it is not a managed profile. |
| 711 | if (mUserManager.isQuietModeEnabled(new UserHandle(userId))) { |
Benjamin Franz | f02420c | 2016-04-04 18:52:21 +0100 | [diff] [blame] | 712 | mUserManager.setQuietModeEnabled(userId, false); |
| 713 | } |
| 714 | |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 715 | try { |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 716 | ActivityManagerNative.getDefault().unlockUser(userId, token, secret, listener); |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 717 | } catch (RemoteException e) { |
| 718 | throw e.rethrowAsRuntimeException(); |
| 719 | } |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 720 | |
| 721 | try { |
| 722 | latch.await(15, TimeUnit.SECONDS); |
| 723 | } catch (InterruptedException e) { |
| 724 | Thread.currentThread().interrupt(); |
| 725 | } |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 726 | try { |
| 727 | if (!mUserManager.getUserInfo(userId).isManagedProfile()) { |
| 728 | final List<UserInfo> profiles = mUserManager.getProfiles(userId); |
| 729 | for (UserInfo pi : profiles) { |
| 730 | // Unlock managed profile with unified lock |
| 731 | if (pi.isManagedProfile() |
| 732 | && !mLockPatternUtils.isSeparateProfileChallengeEnabled(pi.id) |
Ricky Wai | 7881cf8 | 2016-04-15 17:20:12 +0100 | [diff] [blame] | 733 | && !pi.isQuietModeEnabled() |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 734 | && mStorage.hasChildProfileLock(pi.id)) { |
| 735 | unlockChildProfile(pi.id); |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | } catch (RemoteException e) { |
| 740 | Log.d(TAG, "Failed to unlock child profile", e); |
| 741 | } |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 742 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 743 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 744 | private byte[] getCurrentHandle(int userId) { |
| 745 | CredentialHash credential; |
| 746 | byte[] currentHandle; |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 747 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 748 | int currentHandleType = mStorage.getStoredCredentialType(userId); |
| 749 | switch (currentHandleType) { |
| 750 | case CredentialHash.TYPE_PATTERN: |
| 751 | credential = mStorage.readPatternHash(userId); |
| 752 | currentHandle = credential != null |
| 753 | ? credential.hash |
| 754 | : null; |
| 755 | break; |
| 756 | case CredentialHash.TYPE_PASSWORD: |
| 757 | credential = mStorage.readPasswordHash(userId); |
| 758 | currentHandle = credential != null |
| 759 | ? credential.hash |
| 760 | : null; |
| 761 | break; |
| 762 | case CredentialHash.TYPE_NONE: |
| 763 | default: |
| 764 | currentHandle = null; |
| 765 | break; |
| 766 | } |
| 767 | |
| 768 | // sanity check |
| 769 | if (currentHandleType != CredentialHash.TYPE_NONE && currentHandle == null) { |
| 770 | Slog.e(TAG, "Stored handle type [" + currentHandleType + "] but no handle available"); |
| 771 | } |
| 772 | |
| 773 | return currentHandle; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 776 | private void onUserLockChanged(int userId) throws RemoteException { |
| 777 | if (mUserManager.getUserInfo(userId).isManagedProfile()) { |
| 778 | return; |
| 779 | } |
| 780 | final boolean isSecure = mStorage.hasPassword(userId) || mStorage.hasPattern(userId); |
| 781 | final List<UserInfo> profiles = mUserManager.getProfiles(userId); |
| 782 | final int size = profiles.size(); |
| 783 | for (int i = 0; i < size; i++) { |
| 784 | final UserInfo profile = profiles.get(i); |
| 785 | if (profile.isManagedProfile()) { |
| 786 | final int managedUserId = profile.id; |
| 787 | if (mLockPatternUtils.isSeparateProfileChallengeEnabled(managedUserId)) { |
| 788 | continue; |
| 789 | } |
| 790 | if (isSecure) { |
| 791 | tieManagedProfileLockIfNecessary(managedUserId, null); |
| 792 | } else { |
| 793 | getGateKeeperService().clearSecureUserId(managedUserId); |
| 794 | mStorage.writePatternHash(null, managedUserId); |
| 795 | setKeystorePassword(null, managedUserId); |
Paul Crowley | a1eb750 | 2016-05-10 20:36:56 +0000 | [diff] [blame] | 796 | clearUserKeyProtection(managedUserId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 797 | mStorage.removeChildProfileLock(managedUserId); |
| 798 | removeKeystoreProfileKey(managedUserId); |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 803 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 804 | private boolean isManagedProfileWithUnifiedLock(int userId) { |
| 805 | return mUserManager.getUserInfo(userId).isManagedProfile() |
| 806 | && !mLockPatternUtils.isSeparateProfileChallengeEnabled(userId); |
| 807 | } |
| 808 | |
| 809 | private boolean isManagedProfileWithSeparatedLock(int userId) { |
| 810 | return mUserManager.getUserInfo(userId).isManagedProfile() |
| 811 | && mLockPatternUtils.isSeparateProfileChallengeEnabled(userId); |
| 812 | } |
| 813 | |
| 814 | // This method should be called by LockPatternUtil only, all internal methods in this class |
| 815 | // should call setLockPatternInternal. |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 816 | @Override |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 817 | public void setLockPattern(String pattern, String savedCredential, int userId) |
| 818 | throws RemoteException { |
Jim Miller | e484eaf | 2016-04-13 16:35:36 -0700 | [diff] [blame] | 819 | checkWritePermission(userId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 820 | synchronized (mSeparateChallengeLock) { |
| 821 | setLockPatternInternal(pattern, savedCredential, userId); |
| 822 | setSeparateProfileChallengeEnabled(userId, true, null); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | public void setLockPatternInternal(String pattern, String savedCredential, int userId) |
| 827 | throws RemoteException { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 828 | byte[] currentHandle = getCurrentHandle(userId); |
| 829 | |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 830 | if (pattern == null) { |
Andres Morales | cfb6160 | 2015-04-16 16:31:15 -0700 | [diff] [blame] | 831 | getGateKeeperService().clearSecureUserId(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 832 | mStorage.writePatternHash(null, userId); |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 833 | setKeystorePassword(null, userId); |
Paul Crowley | a1eb750 | 2016-05-10 20:36:56 +0000 | [diff] [blame] | 834 | clearUserKeyProtection(userId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 835 | onUserLockChanged(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 836 | return; |
| 837 | } |
| 838 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 839 | if (isManagedProfileWithUnifiedLock(userId)) { |
| 840 | // get credential from keystore when managed profile has unified lock |
| 841 | try { |
| 842 | savedCredential = getDecryptedPasswordForTiedProfile(userId); |
| 843 | } catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException |
| 844 | | NoSuchAlgorithmException | NoSuchPaddingException |
| 845 | | InvalidAlgorithmParameterException | IllegalBlockSizeException |
| 846 | | BadPaddingException | CertificateException | IOException e) { |
| 847 | if (e instanceof FileNotFoundException) { |
| 848 | Slog.i(TAG, "Child profile key not found"); |
| 849 | } else { |
| 850 | Slog.e(TAG, "Failed to decrypt child profile key", e); |
| 851 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 852 | } |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 853 | } else { |
| 854 | if (currentHandle == null) { |
| 855 | if (savedCredential != null) { |
| 856 | Slog.w(TAG, "Saved credential provided, but none stored"); |
| 857 | } |
| 858 | savedCredential = null; |
| 859 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | byte[] enrolledHandle = enrollCredential(currentHandle, savedCredential, pattern, userId); |
| 863 | if (enrolledHandle != null) { |
| 864 | mStorage.writePatternHash(enrolledHandle, userId); |
Paul Crowley | a1eb750 | 2016-05-10 20:36:56 +0000 | [diff] [blame] | 865 | setUserKeyProtection(userId, pattern, verifyPattern(pattern, 0, userId)); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 866 | onUserLockChanged(userId); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 867 | } else { |
Andres Morales | 2c4a573 | 2015-07-09 16:11:00 -0700 | [diff] [blame] | 868 | throw new RemoteException("Failed to enroll pattern"); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 872 | // This method should be called by LockPatternUtil only, all internal methods in this class |
| 873 | // should call setLockPasswordInternal. |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 874 | @Override |
| 875 | public void setLockPassword(String password, String savedCredential, int userId) |
| 876 | throws RemoteException { |
Jim Miller | e484eaf | 2016-04-13 16:35:36 -0700 | [diff] [blame] | 877 | checkWritePermission(userId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 878 | synchronized (mSeparateChallengeLock) { |
| 879 | setLockPasswordInternal(password, savedCredential, userId); |
| 880 | setSeparateProfileChallengeEnabled(userId, true, null); |
| 881 | } |
| 882 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 883 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 884 | public void setLockPasswordInternal(String password, String savedCredential, int userId) |
| 885 | throws RemoteException { |
| 886 | byte[] currentHandle = getCurrentHandle(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 887 | if (password == null) { |
Andres Morales | cfb6160 | 2015-04-16 16:31:15 -0700 | [diff] [blame] | 888 | getGateKeeperService().clearSecureUserId(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 889 | mStorage.writePasswordHash(null, userId); |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 890 | setKeystorePassword(null, userId); |
Paul Crowley | a1eb750 | 2016-05-10 20:36:56 +0000 | [diff] [blame] | 891 | clearUserKeyProtection(userId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 892 | onUserLockChanged(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 893 | return; |
| 894 | } |
| 895 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 896 | if (isManagedProfileWithUnifiedLock(userId)) { |
| 897 | // get credential from keystore when managed profile has unified lock |
| 898 | try { |
| 899 | savedCredential = getDecryptedPasswordForTiedProfile(userId); |
Ricky Wai | de3a068 | 2016-05-03 14:50:03 +0100 | [diff] [blame] | 900 | } catch (FileNotFoundException e) { |
| 901 | Slog.i(TAG, "Child profile key not found"); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 902 | } catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException |
| 903 | | NoSuchAlgorithmException | NoSuchPaddingException |
| 904 | | InvalidAlgorithmParameterException | IllegalBlockSizeException |
| 905 | | BadPaddingException | CertificateException | IOException e) { |
Ricky Wai | de3a068 | 2016-05-03 14:50:03 +0100 | [diff] [blame] | 906 | Slog.e(TAG, "Failed to decrypt child profile key", e); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 907 | } |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 908 | } else { |
| 909 | if (currentHandle == null) { |
| 910 | if (savedCredential != null) { |
| 911 | Slog.w(TAG, "Saved credential provided, but none stored"); |
| 912 | } |
| 913 | savedCredential = null; |
| 914 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | byte[] enrolledHandle = enrollCredential(currentHandle, savedCredential, password, userId); |
| 918 | if (enrolledHandle != null) { |
| 919 | mStorage.writePasswordHash(enrolledHandle, userId); |
Paul Crowley | a1eb750 | 2016-05-10 20:36:56 +0000 | [diff] [blame] | 920 | setUserKeyProtection(userId, password, verifyPassword(password, 0, userId)); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 921 | onUserLockChanged(userId); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 922 | } else { |
Andres Morales | 2c4a573 | 2015-07-09 16:11:00 -0700 | [diff] [blame] | 923 | throw new RemoteException("Failed to enroll password"); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 927 | private void tieProfileLockToParent(int userId, String password) { |
| 928 | if (DEBUG) Slog.v(TAG, "tieProfileLockToParent for user: " + userId); |
| 929 | byte[] randomLockSeed = password.getBytes(StandardCharsets.UTF_8); |
| 930 | byte[] encryptionResult; |
| 931 | byte[] iv; |
| 932 | try { |
| 933 | KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES); |
| 934 | keyGenerator.init(new SecureRandom()); |
| 935 | SecretKey secretKey = keyGenerator.generateKey(); |
| 936 | |
| 937 | java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore"); |
| 938 | keyStore.load(null); |
| 939 | keyStore.setEntry( |
| 940 | PROFILE_KEY_NAME_ENCRYPT + userId, |
| 941 | new java.security.KeyStore.SecretKeyEntry(secretKey), |
| 942 | new KeyProtection.Builder(KeyProperties.PURPOSE_ENCRYPT) |
| 943 | .setBlockModes(KeyProperties.BLOCK_MODE_GCM) |
| 944 | .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) |
| 945 | .build()); |
| 946 | keyStore.setEntry( |
| 947 | PROFILE_KEY_NAME_DECRYPT + userId, |
| 948 | new java.security.KeyStore.SecretKeyEntry(secretKey), |
| 949 | new KeyProtection.Builder(KeyProperties.PURPOSE_DECRYPT) |
| 950 | .setBlockModes(KeyProperties.BLOCK_MODE_GCM) |
| 951 | .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) |
| 952 | .setUserAuthenticationRequired(true) |
| 953 | .setUserAuthenticationValidityDurationSeconds(30) |
| 954 | .build()); |
| 955 | |
| 956 | // Key imported, obtain a reference to it. |
| 957 | SecretKey keyStoreEncryptionKey = (SecretKey) keyStore.getKey( |
| 958 | PROFILE_KEY_NAME_ENCRYPT + userId, null); |
| 959 | // The original key can now be discarded. |
| 960 | |
| 961 | Cipher cipher = Cipher.getInstance( |
| 962 | KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_GCM + "/" |
| 963 | + KeyProperties.ENCRYPTION_PADDING_NONE); |
| 964 | cipher.init(Cipher.ENCRYPT_MODE, keyStoreEncryptionKey); |
| 965 | encryptionResult = cipher.doFinal(randomLockSeed); |
| 966 | iv = cipher.getIV(); |
| 967 | } catch (CertificateException | UnrecoverableKeyException |
| 968 | | IOException | BadPaddingException | IllegalBlockSizeException | KeyStoreException |
| 969 | | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException e) { |
| 970 | throw new RuntimeException("Failed to encrypt key", e); |
| 971 | } |
| 972 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 973 | try { |
| 974 | if (iv.length != PROFILE_KEY_IV_SIZE) { |
| 975 | throw new RuntimeException("Invalid iv length: " + iv.length); |
| 976 | } |
| 977 | outputStream.write(iv); |
| 978 | outputStream.write(encryptionResult); |
| 979 | } catch (IOException e) { |
| 980 | throw new RuntimeException("Failed to concatenate byte arrays", e); |
| 981 | } |
| 982 | mStorage.writeChildProfileLock(userId, outputStream.toByteArray()); |
| 983 | } |
| 984 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 985 | private byte[] enrollCredential(byte[] enrolledHandle, |
| 986 | String enrolledCredential, String toEnroll, int userId) |
| 987 | throws RemoteException { |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 988 | checkWritePermission(userId); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 989 | byte[] enrolledCredentialBytes = enrolledCredential == null |
| 990 | ? null |
| 991 | : enrolledCredential.getBytes(); |
| 992 | byte[] toEnrollBytes = toEnroll == null |
| 993 | ? null |
| 994 | : toEnroll.getBytes(); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 995 | GateKeeperResponse response = getGateKeeperService().enroll(userId, enrolledHandle, |
| 996 | enrolledCredentialBytes, toEnrollBytes); |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 997 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 998 | if (response == null) { |
| 999 | return null; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1000 | } |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 1001 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1002 | byte[] hash = response.getPayload(); |
| 1003 | if (hash != null) { |
| 1004 | setKeystorePassword(toEnroll, userId); |
| 1005 | } else { |
| 1006 | // Should not happen |
| 1007 | Slog.e(TAG, "Throttled while enrolling a password"); |
| 1008 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1009 | return hash; |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1012 | private void setUserKeyProtection(int userId, String credential, VerifyCredentialResponse vcr) |
| 1013 | throws RemoteException { |
| 1014 | if (vcr == null) { |
| 1015 | throw new RemoteException("Null response verifying a credential we just set"); |
| 1016 | } |
| 1017 | if (vcr.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK) { |
| 1018 | throw new RemoteException("Non-OK response verifying a credential we just set: " |
| 1019 | + vcr.getResponseCode()); |
| 1020 | } |
| 1021 | byte[] token = vcr.getPayload(); |
| 1022 | if (token == null) { |
| 1023 | throw new RemoteException("Empty payload verifying a credential we just set"); |
| 1024 | } |
Paul Crowley | a1eb750 | 2016-05-10 20:36:56 +0000 | [diff] [blame] | 1025 | changeUserKey(userId, token, secretFromCredential(credential)); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | private void clearUserKeyProtection(int userId) throws RemoteException { |
Paul Crowley | a1eb750 | 2016-05-10 20:36:56 +0000 | [diff] [blame] | 1029 | changeUserKey(userId, null, null); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | private static byte[] secretFromCredential(String credential) throws RemoteException { |
| 1033 | try { |
| 1034 | MessageDigest digest = MessageDigest.getInstance("SHA-512"); |
| 1035 | // Personalize the hash |
| 1036 | byte[] personalization = "Android FBE credential hash" |
| 1037 | .getBytes(StandardCharsets.UTF_8); |
| 1038 | // Pad it to the block size of the hash function |
| 1039 | personalization = Arrays.copyOf(personalization, 128); |
| 1040 | digest.update(personalization); |
| 1041 | digest.update(credential.getBytes(StandardCharsets.UTF_8)); |
| 1042 | return digest.digest(); |
| 1043 | } catch (NoSuchAlgorithmException e) { |
| 1044 | throw new RuntimeException("NoSuchAlgorithmException for SHA-512"); |
| 1045 | } |
| 1046 | } |
| 1047 | |
Paul Crowley | a1eb750 | 2016-05-10 20:36:56 +0000 | [diff] [blame] | 1048 | private void changeUserKey(int userId, byte[] token, byte[] secret) |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1049 | throws RemoteException { |
| 1050 | final UserInfo userInfo = UserManager.get(mContext).getUserInfo(userId); |
Paul Crowley | 815036f | 2016-03-29 14:14:48 -0700 | [diff] [blame] | 1051 | final IMountService mountService = getMountService(); |
| 1052 | final long callingId = Binder.clearCallingIdentity(); |
| 1053 | try { |
Paul Crowley | a1eb750 | 2016-05-10 20:36:56 +0000 | [diff] [blame] | 1054 | mountService.changeUserKey(userId, userInfo.serialNumber, token, null, secret); |
Paul Crowley | 815036f | 2016-03-29 14:14:48 -0700 | [diff] [blame] | 1055 | } finally { |
| 1056 | Binder.restoreCallingIdentity(callingId); |
| 1057 | } |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 1060 | @Override |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1061 | public VerifyCredentialResponse checkPattern(String pattern, int userId) throws RemoteException { |
| 1062 | return doVerifyPattern(pattern, false, 0, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1063 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1064 | |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1065 | @Override |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1066 | public VerifyCredentialResponse verifyPattern(String pattern, long challenge, int userId) |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1067 | throws RemoteException { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1068 | return doVerifyPattern(pattern, true, challenge, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 1071 | private VerifyCredentialResponse doVerifyPattern(String pattern, boolean hasChallenge, |
| 1072 | long challenge, int userId) throws RemoteException { |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1073 | checkPasswordReadPermission(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1074 | CredentialHash storedHash = mStorage.readPatternHash(userId); |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 1075 | boolean shouldReEnrollBaseZero = storedHash != null && storedHash.isBaseZeroPattern; |
| 1076 | |
| 1077 | String patternToVerify; |
| 1078 | if (shouldReEnrollBaseZero) { |
| 1079 | patternToVerify = LockPatternUtils.patternStringToBaseZero(pattern); |
| 1080 | } else { |
| 1081 | patternToVerify = pattern; |
| 1082 | } |
| 1083 | |
| 1084 | VerifyCredentialResponse response = verifyCredential(userId, storedHash, patternToVerify, |
| 1085 | hasChallenge, challenge, |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1086 | new CredentialUtil() { |
| 1087 | @Override |
| 1088 | public void setCredential(String pattern, String oldPattern, int userId) |
| 1089 | throws RemoteException { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1090 | setLockPatternInternal(pattern, oldPattern, userId); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1091 | } |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1092 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1093 | @Override |
| 1094 | public byte[] toHash(String pattern, int userId) { |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 1095 | return LockPatternUtils.patternToHash( |
| 1096 | LockPatternUtils.stringToPattern(pattern)); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1097 | } |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 1098 | |
| 1099 | @Override |
| 1100 | public String adjustForKeystore(String pattern) { |
| 1101 | return LockPatternUtils.patternStringToBaseZero(pattern); |
| 1102 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1103 | } |
| 1104 | ); |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 1105 | |
| 1106 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK |
| 1107 | && shouldReEnrollBaseZero) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1108 | setLockPatternInternal(pattern, patternToVerify, userId); |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | return response; |
Paul Crowley | a1eb750 | 2016-05-10 20:36:56 +0000 | [diff] [blame] | 1112 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | @Override |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1116 | public VerifyCredentialResponse checkPassword(String password, int userId) |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1117 | throws RemoteException { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1118 | return doVerifyPassword(password, false, 0, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1121 | @Override |
| 1122 | public VerifyCredentialResponse verifyPassword(String password, long challenge, int userId) |
| 1123 | throws RemoteException { |
| 1124 | return doVerifyPassword(password, true, challenge, userId); |
| 1125 | } |
| 1126 | |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 1127 | @Override |
| 1128 | public VerifyCredentialResponse verifyTiedProfileChallenge(String password, boolean isPattern, |
| 1129 | long challenge, int userId) throws RemoteException { |
| 1130 | checkPasswordReadPermission(userId); |
| 1131 | if (!isManagedProfileWithUnifiedLock(userId)) { |
| 1132 | throw new RemoteException("User id must be managed profile with unified lock"); |
| 1133 | } |
| 1134 | final int parentProfileId = mUserManager.getProfileParent(userId).id; |
| 1135 | // Unlock parent by using parent's challenge |
| 1136 | final VerifyCredentialResponse parentResponse = isPattern |
| 1137 | ? doVerifyPattern(password, true, challenge, parentProfileId) |
| 1138 | : doVerifyPassword(password, true, challenge, parentProfileId); |
| 1139 | if (parentResponse.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK) { |
| 1140 | // Failed, just return parent's response |
| 1141 | return parentResponse; |
| 1142 | } |
| 1143 | |
| 1144 | try { |
| 1145 | // Unlock work profile, and work profile with unified lock must use password only |
| 1146 | return doVerifyPassword(getDecryptedPasswordForTiedProfile(userId), true, |
| 1147 | challenge, |
| 1148 | userId); |
| 1149 | } catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException |
| 1150 | | NoSuchAlgorithmException | NoSuchPaddingException |
| 1151 | | InvalidAlgorithmParameterException | IllegalBlockSizeException |
| 1152 | | BadPaddingException | CertificateException | IOException e) { |
| 1153 | Slog.e(TAG, "Failed to decrypt child profile key", e); |
| 1154 | throw new RemoteException("Unable to get tied profile token"); |
| 1155 | } |
| 1156 | } |
| 1157 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1158 | private VerifyCredentialResponse doVerifyPassword(String password, boolean hasChallenge, |
| 1159 | long challenge, int userId) throws RemoteException { |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1160 | checkPasswordReadPermission(userId); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1161 | CredentialHash storedHash = mStorage.readPasswordHash(userId); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1162 | return verifyCredential(userId, storedHash, password, hasChallenge, challenge, |
| 1163 | new CredentialUtil() { |
| 1164 | @Override |
| 1165 | public void setCredential(String password, String oldPassword, int userId) |
| 1166 | throws RemoteException { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1167 | setLockPasswordInternal(password, oldPassword, userId); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1168 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1169 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1170 | @Override |
| 1171 | public byte[] toHash(String password, int userId) { |
| 1172 | return mLockPatternUtils.passwordToHash(password, userId); |
| 1173 | } |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 1174 | |
| 1175 | @Override |
| 1176 | public String adjustForKeystore(String password) { |
| 1177 | return password; |
| 1178 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1179 | } |
| 1180 | ); |
| 1181 | } |
| 1182 | |
| 1183 | private VerifyCredentialResponse verifyCredential(int userId, CredentialHash storedHash, |
| 1184 | String credential, boolean hasChallenge, long challenge, CredentialUtil credentialUtil) |
| 1185 | throws RemoteException { |
| 1186 | if ((storedHash == null || storedHash.hash.length == 0) && TextUtils.isEmpty(credential)) { |
| 1187 | // don't need to pass empty credentials to GateKeeper |
| 1188 | return VerifyCredentialResponse.OK; |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1191 | if (TextUtils.isEmpty(credential)) { |
| 1192 | return VerifyCredentialResponse.ERROR; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1193 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1194 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1195 | if (storedHash.version == CredentialHash.VERSION_LEGACY) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1196 | byte[] hash = credentialUtil.toHash(credential, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1197 | if (Arrays.equals(hash, storedHash.hash)) { |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 1198 | unlockKeystore(credentialUtil.adjustForKeystore(credential), userId); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 1199 | |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1200 | // Users with legacy credentials don't have credential-backed |
| 1201 | // FBE keys, so just pass through a fake token/secret |
| 1202 | Slog.i(TAG, "Unlocking user with fake token: " + userId); |
| 1203 | final byte[] fakeToken = String.valueOf(userId).getBytes(); |
| 1204 | unlockUser(userId, fakeToken, fakeToken); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 1205 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1206 | // migrate credential to GateKeeper |
| 1207 | credentialUtil.setCredential(credential, null, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1208 | if (!hasChallenge) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1209 | return VerifyCredentialResponse.OK; |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1210 | } |
| 1211 | // Fall through to get the auth token. Technically this should never happen, |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1212 | // 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] | 1213 | // before getting to a flow with a challenge, but supporting for consistency. |
| 1214 | } else { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1215 | return VerifyCredentialResponse.ERROR; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1216 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1219 | VerifyCredentialResponse response; |
Paul Crowley | 98e0a26 | 2016-02-04 09:41:53 +0000 | [diff] [blame] | 1220 | boolean shouldReEnroll = false; |
| 1221 | GateKeeperResponse gateKeeperResponse = getGateKeeperService() |
| 1222 | .verifyChallenge(userId, challenge, storedHash.hash, credential.getBytes()); |
| 1223 | int responseCode = gateKeeperResponse.getResponseCode(); |
| 1224 | if (responseCode == GateKeeperResponse.RESPONSE_RETRY) { |
| 1225 | response = new VerifyCredentialResponse(gateKeeperResponse.getTimeout()); |
| 1226 | } else if (responseCode == GateKeeperResponse.RESPONSE_OK) { |
| 1227 | byte[] token = gateKeeperResponse.getPayload(); |
| 1228 | if (token == null) { |
| 1229 | // something's wrong if there's no payload with a challenge |
| 1230 | Slog.e(TAG, "verifyChallenge response had no associated payload"); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1231 | response = VerifyCredentialResponse.ERROR; |
Paul Crowley | 98e0a26 | 2016-02-04 09:41:53 +0000 | [diff] [blame] | 1232 | } else { |
| 1233 | shouldReEnroll = gateKeeperResponse.getShouldReEnroll(); |
| 1234 | response = new VerifyCredentialResponse(token); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1235 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1236 | } else { |
Paul Crowley | 98e0a26 | 2016-02-04 09:41:53 +0000 | [diff] [blame] | 1237 | response = VerifyCredentialResponse.ERROR; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1238 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1239 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1240 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 1241 | // credential has matched |
| 1242 | unlockKeystore(credential, userId); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 1243 | |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1244 | Slog.i(TAG, "Unlocking user " + userId + |
| 1245 | " with token length " + response.getPayload().length); |
| 1246 | unlockUser(userId, response.getPayload(), secretFromCredential(credential)); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 1247 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1248 | if (isManagedProfileWithSeparatedLock(userId)) { |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 1249 | TrustManager trustManager = |
| 1250 | (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE); |
| 1251 | trustManager.setDeviceLockedForUser(userId, false); |
| 1252 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1253 | if (shouldReEnroll) { |
| 1254 | credentialUtil.setCredential(credential, credential, userId); |
| 1255 | } |
Adrian Roos | 873010d | 2015-08-25 15:59:00 -0700 | [diff] [blame] | 1256 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 1257 | if (response.getTimeout() > 0) { |
| 1258 | requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_LOCKOUT, userId); |
| 1259 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1260 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1261 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1262 | return response; |
| 1263 | } |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1264 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1265 | @Override |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1266 | public boolean checkVoldPassword(int userId) throws RemoteException { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1267 | if (!mFirstCallToVold) { |
| 1268 | return false; |
| 1269 | } |
| 1270 | mFirstCallToVold = false; |
| 1271 | |
| 1272 | checkPasswordReadPermission(userId); |
| 1273 | |
| 1274 | // There's no guarantee that this will safely connect, but if it fails |
| 1275 | // we will simply show the lock screen when we shouldn't, so relatively |
| 1276 | // benign. There is an outside chance something nasty would happen if |
| 1277 | // this service restarted before vold stales out the password in this |
| 1278 | // case. The nastiness is limited to not showing the lock screen when |
| 1279 | // we should, within the first minute of decrypting the phone if this |
| 1280 | // service can't connect to vold, it restarts, and then the new instance |
| 1281 | // does successfully connect. |
| 1282 | final IMountService service = getMountService(); |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 1283 | String password; |
| 1284 | long identity = Binder.clearCallingIdentity(); |
| 1285 | try { |
| 1286 | password = service.getPassword(); |
| 1287 | service.clearPassword(); |
| 1288 | } finally { |
| 1289 | Binder.restoreCallingIdentity(identity); |
| 1290 | } |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1291 | if (password == null) { |
| 1292 | return false; |
| 1293 | } |
| 1294 | |
| 1295 | try { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1296 | if (mLockPatternUtils.isLockPatternEnabled(userId)) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1297 | if (checkPattern(password, userId).getResponseCode() |
| 1298 | == GateKeeperResponse.RESPONSE_OK) { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1299 | return true; |
| 1300 | } |
| 1301 | } |
| 1302 | } catch (Exception e) { |
| 1303 | } |
| 1304 | |
| 1305 | try { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1306 | if (mLockPatternUtils.isLockPasswordEnabled(userId)) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1307 | if (checkPassword(password, userId).getResponseCode() |
| 1308 | == GateKeeperResponse.RESPONSE_OK) { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1309 | return true; |
| 1310 | } |
| 1311 | } |
| 1312 | } catch (Exception e) { |
| 1313 | } |
| 1314 | |
| 1315 | return false; |
| 1316 | } |
| 1317 | |
Adrian Roos | db0f76e | 2015-01-07 22:19:38 +0100 | [diff] [blame] | 1318 | private void removeUser(int userId) { |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1319 | mStorage.removeUser(userId); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1320 | mStrongAuth.removeUser(userId); |
Robin Lee | 49d810c | 2014-09-23 13:50:22 +0100 | [diff] [blame] | 1321 | |
| 1322 | final KeyStore ks = KeyStore.getInstance(); |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 1323 | ks.onUserRemoved(userId); |
Andres Morales | 070fe63 | 2015-06-24 10:37:10 -0700 | [diff] [blame] | 1324 | |
| 1325 | try { |
| 1326 | final IGateKeeperService gk = getGateKeeperService(); |
| 1327 | if (gk != null) { |
| 1328 | gk.clearSecureUserId(userId); |
| 1329 | } |
| 1330 | } catch (RemoteException ex) { |
| 1331 | Slog.w(TAG, "unable to clear GK secure user id"); |
| 1332 | } |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1333 | if (mUserManager.getUserInfo(userId).isManagedProfile()) { |
| 1334 | removeKeystoreProfileKey(userId); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | private void removeKeystoreProfileKey(int targetUserId) { |
| 1339 | if (DEBUG) Slog.v(TAG, "Remove keystore profile key for user: " + targetUserId); |
| 1340 | try { |
| 1341 | java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore"); |
| 1342 | keyStore.load(null); |
| 1343 | keyStore.deleteEntry(PROFILE_KEY_NAME_ENCRYPT + targetUserId); |
| 1344 | keyStore.deleteEntry(PROFILE_KEY_NAME_DECRYPT + targetUserId); |
| 1345 | } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException |
| 1346 | | IOException e) { |
| 1347 | // We have tried our best to remove all keys |
| 1348 | Slog.e(TAG, "Unable to remove keystore profile key for user:" + targetUserId, e); |
| 1349 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1352 | @Override |
| 1353 | public void registerStrongAuthTracker(IStrongAuthTracker tracker) { |
| 1354 | checkPasswordReadPermission(UserHandle.USER_ALL); |
| 1355 | mStrongAuth.registerStrongAuthTracker(tracker); |
| 1356 | } |
| 1357 | |
| 1358 | @Override |
| 1359 | public void unregisterStrongAuthTracker(IStrongAuthTracker tracker) { |
| 1360 | checkPasswordReadPermission(UserHandle.USER_ALL); |
| 1361 | mStrongAuth.unregisterStrongAuthTracker(tracker); |
| 1362 | } |
| 1363 | |
| 1364 | @Override |
| 1365 | public void requireStrongAuth(int strongAuthReason, int userId) { |
| 1366 | checkWritePermission(userId); |
| 1367 | mStrongAuth.requireStrongAuth(strongAuthReason, userId); |
| 1368 | } |
| 1369 | |
Adrian Roos | 4ab7e59 | 2016-04-13 15:38:13 -0700 | [diff] [blame] | 1370 | @Override |
| 1371 | public void userPresent(int userId) { |
| 1372 | checkWritePermission(userId); |
| 1373 | mStrongAuth.reportUnlock(userId); |
| 1374 | } |
| 1375 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1376 | private static final String[] VALID_SETTINGS = new String[] { |
| 1377 | LockPatternUtils.LOCKOUT_PERMANENT_KEY, |
| 1378 | LockPatternUtils.LOCKOUT_ATTEMPT_DEADLINE, |
| 1379 | LockPatternUtils.PATTERN_EVER_CHOSEN_KEY, |
| 1380 | LockPatternUtils.PASSWORD_TYPE_KEY, |
| 1381 | LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 1382 | LockPatternUtils.LOCK_PASSWORD_SALT_KEY, |
| 1383 | LockPatternUtils.DISABLE_LOCKSCREEN_KEY, |
| 1384 | LockPatternUtils.LOCKSCREEN_OPTIONS, |
| 1385 | LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, |
| 1386 | LockPatternUtils.BIOMETRIC_WEAK_EVER_CHOSEN_KEY, |
| 1387 | LockPatternUtils.LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, |
| 1388 | LockPatternUtils.PASSWORD_HISTORY_KEY, |
| 1389 | Secure.LOCK_PATTERN_ENABLED, |
| 1390 | Secure.LOCK_BIOMETRIC_WEAK_FLAGS, |
| 1391 | Secure.LOCK_PATTERN_VISIBLE, |
| 1392 | Secure.LOCK_PATTERN_TACTILE_FEEDBACK_ENABLED |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 1393 | }; |
| 1394 | |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 1395 | // Reading these settings needs the contacts permission |
| 1396 | private static final String[] READ_CONTACTS_PROTECTED_SETTINGS = new String[] { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 1397 | Secure.LOCK_SCREEN_OWNER_INFO_ENABLED, |
| 1398 | Secure.LOCK_SCREEN_OWNER_INFO |
| 1399 | }; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1400 | |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 1401 | // Reading these settings needs the same permission as checking the password |
| 1402 | private static final String[] READ_PASSWORD_PROTECTED_SETTINGS = new String[] { |
| 1403 | LockPatternUtils.LOCK_PASSWORD_SALT_KEY, |
| 1404 | LockPatternUtils.PASSWORD_HISTORY_KEY, |
Adrian Roos | 855fa30 | 2015-04-02 16:01:12 +0200 | [diff] [blame] | 1405 | LockPatternUtils.PASSWORD_TYPE_KEY, |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 1406 | }; |
| 1407 | |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 1408 | private static final String[] SETTINGS_TO_BACKUP = new String[] { |
| 1409 | Secure.LOCK_SCREEN_OWNER_INFO_ENABLED, |
| 1410 | Secure.LOCK_SCREEN_OWNER_INFO |
| 1411 | }; |
| 1412 | |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 1413 | private IMountService getMountService() { |
| 1414 | final IBinder service = ServiceManager.getService("mount"); |
| 1415 | if (service != null) { |
| 1416 | return IMountService.Stub.asInterface(service); |
| 1417 | } |
| 1418 | return null; |
| 1419 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1420 | |
Andres Morales | 301ea44 | 2015-04-17 09:15:47 -0700 | [diff] [blame] | 1421 | private class GateKeeperDiedRecipient implements IBinder.DeathRecipient { |
| 1422 | @Override |
| 1423 | public void binderDied() { |
| 1424 | mGateKeeperService.asBinder().unlinkToDeath(this, 0); |
| 1425 | mGateKeeperService = null; |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | private synchronized IGateKeeperService getGateKeeperService() |
| 1430 | throws RemoteException { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1431 | if (mGateKeeperService != null) { |
| 1432 | return mGateKeeperService; |
| 1433 | } |
| 1434 | |
| 1435 | final IBinder service = |
| 1436 | ServiceManager.getService("android.service.gatekeeper.IGateKeeperService"); |
| 1437 | if (service != null) { |
Andres Morales | 301ea44 | 2015-04-17 09:15:47 -0700 | [diff] [blame] | 1438 | service.linkToDeath(new GateKeeperDiedRecipient(), 0); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1439 | mGateKeeperService = IGateKeeperService.Stub.asInterface(service); |
| 1440 | return mGateKeeperService; |
| 1441 | } |
| 1442 | |
| 1443 | Slog.e(TAG, "Unable to acquire GateKeeperService"); |
| 1444 | return null; |
| 1445 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1446 | } |