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 | |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 19 | import android.app.admin.DevicePolicyManager; |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 20 | import android.app.backup.BackupManager; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 21 | import android.app.trust.IStrongAuthTracker; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 28 | import android.content.pm.UserInfo; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 29 | import static android.Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 30 | import static android.content.Context.USER_SERVICE; |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 31 | import static android.Manifest.permission.READ_CONTACTS; |
Adrian Roos | 873010d | 2015-08-25 15:59:00 -0700 | [diff] [blame] | 32 | import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT; |
| 33 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 34 | import android.database.sqlite.SQLiteDatabase; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 35 | import android.os.Binder; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 36 | import android.os.IBinder; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 37 | import android.os.RemoteException; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 38 | import android.os.storage.IMountService; |
| 39 | import android.os.ServiceManager; |
Amith Yamasani | d1645f8 | 2012-06-12 11:53:26 -0700 | [diff] [blame] | 40 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 41 | import android.os.UserHandle; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 42 | import android.os.UserManager; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 43 | import android.provider.Settings; |
| 44 | import android.provider.Settings.Secure; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 45 | import android.provider.Settings.SettingNotFoundException; |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 46 | import android.security.KeyStore; |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 47 | import android.service.gatekeeper.GateKeeperResponse; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 48 | import android.service.gatekeeper.IGateKeeperService; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 49 | import android.text.TextUtils; |
| 50 | import android.util.Slog; |
| 51 | |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 52 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 53 | import com.android.internal.widget.ILockSettings; |
| 54 | import com.android.internal.widget.LockPatternUtils; |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 55 | import com.android.internal.widget.VerifyCredentialResponse; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 56 | import com.android.server.LockSettingsStorage.CredentialHash; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 57 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 58 | import java.util.Arrays; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 59 | import java.util.List; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Keeps the lock pattern/password data and related settings for each user. |
| 63 | * Used by LockPatternUtils. Needs to be a service because Settings app also needs |
| 64 | * to be able to save lockscreen information for secondary users. |
| 65 | * @hide |
| 66 | */ |
| 67 | public class LockSettingsService extends ILockSettings.Stub { |
| 68 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 69 | private static final String PERMISSION = ACCESS_KEYGUARD_SECURE_STORAGE; |
Adrian Roos | 4f78845 | 2014-05-22 20:45:59 +0200 | [diff] [blame] | 70 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 71 | private static final String TAG = "LockSettingsService"; |
| 72 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 73 | private final Context mContext; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 74 | |
| 75 | private final LockSettingsStorage mStorage; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 76 | private final LockSettingsStrongAuth mStrongAuth = new LockSettingsStrongAuth(); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 77 | |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 78 | private LockPatternUtils mLockPatternUtils; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 79 | private boolean mFirstCallToVold; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 80 | private IGateKeeperService mGateKeeperService; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 81 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 82 | private interface CredentialUtil { |
| 83 | void setCredential(String credential, String savedCredential, int userId) |
| 84 | throws RemoteException; |
| 85 | byte[] toHash(String credential, int userId); |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 86 | String adjustForKeystore(String credential); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 89 | public LockSettingsService(Context context) { |
| 90 | mContext = context; |
| 91 | // Open the database |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 92 | |
| 93 | mLockPatternUtils = new LockPatternUtils(context); |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 94 | mFirstCallToVold = true; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 95 | |
| 96 | IntentFilter filter = new IntentFilter(); |
| 97 | filter.addAction(Intent.ACTION_USER_ADDED); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 98 | filter.addAction(Intent.ACTION_USER_STARTING); |
Adrian Roos | db0f76e | 2015-01-07 22:19:38 +0100 | [diff] [blame] | 99 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 100 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 101 | mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 102 | |
| 103 | mStorage = new LockSettingsStorage(context, new LockSettingsStorage.Callback() { |
| 104 | @Override |
| 105 | public void initialize(SQLiteDatabase db) { |
| 106 | // Get the lockscreen default from a system property, if available |
| 107 | boolean lockScreenDisable = SystemProperties.getBoolean( |
| 108 | "ro.lockscreen.disable.default", false); |
| 109 | if (lockScreenDisable) { |
| 110 | mStorage.writeKeyValue(db, LockPatternUtils.DISABLE_LOCKSCREEN_KEY, "1", 0); |
| 111 | } |
| 112 | } |
| 113 | }); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 116 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 117 | @Override |
| 118 | public void onReceive(Context context, Intent intent) { |
Robin Lee | 1096cf8 | 2014-09-01 16:52:47 +0100 | [diff] [blame] | 119 | if (Intent.ACTION_USER_ADDED.equals(intent.getAction())) { |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 120 | // Notify keystore that a new user was added. |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 121 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
Robin Lee | 49d810c | 2014-09-23 13:50:22 +0100 | [diff] [blame] | 122 | final KeyStore ks = KeyStore.getInstance(); |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 123 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 124 | final UserInfo parentInfo = um.getProfileParent(userHandle); |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 125 | final int parentHandle = parentInfo != null ? parentInfo.id : -1; |
| 126 | ks.onUserAdded(userHandle, parentHandle); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 127 | } else if (Intent.ACTION_USER_STARTING.equals(intent.getAction())) { |
| 128 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 129 | mStorage.prefetchUser(userHandle); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 130 | } else if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) { |
| 131 | mStrongAuth.reportUnlock(getSendingUserId()); |
Adrian Roos | db0f76e | 2015-01-07 22:19:38 +0100 | [diff] [blame] | 132 | } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) { |
| 133 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 134 | if (userHandle > 0) { |
| 135 | removeUser(userHandle); |
| 136 | } |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | }; |
| 140 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 141 | public void systemReady() { |
| 142 | migrateOldData(); |
Andres Morales | 301ea44 | 2015-04-17 09:15:47 -0700 | [diff] [blame] | 143 | try { |
| 144 | getGateKeeperService(); |
| 145 | } catch (RemoteException e) { |
| 146 | Slog.e(TAG, "Failure retrieving IGateKeeperService", e); |
| 147 | } |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 148 | // TODO: maybe skip this for split system user mode. |
| 149 | mStorage.prefetchUser(UserHandle.USER_SYSTEM); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | private void migrateOldData() { |
| 153 | try { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 154 | // These Settings moved before multi-user was enabled, so we only have to do it for the |
| 155 | // root user. |
| 156 | if (getString("migrated", null, 0) == null) { |
| 157 | final ContentResolver cr = mContext.getContentResolver(); |
| 158 | for (String validSetting : VALID_SETTINGS) { |
| 159 | String value = Settings.Secure.getString(cr, validSetting); |
| 160 | if (value != null) { |
| 161 | setString(validSetting, value, 0); |
| 162 | } |
| 163 | } |
| 164 | // No need to move the password / pattern files. They're already in the right place. |
| 165 | setString("migrated", "true", 0); |
| 166 | Slog.i(TAG, "Migrated lock settings to new location"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 169 | // These Settings changed after multi-user was enabled, hence need to be moved per user. |
| 170 | if (getString("migrated_user_specific", null, 0) == null) { |
| 171 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 172 | final ContentResolver cr = mContext.getContentResolver(); |
| 173 | List<UserInfo> users = um.getUsers(); |
| 174 | for (int user = 0; user < users.size(); user++) { |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 175 | // Migrate owner info |
| 176 | final int userId = users.get(user).id; |
| 177 | final String OWNER_INFO = Secure.LOCK_SCREEN_OWNER_INFO; |
| 178 | String ownerInfo = Settings.Secure.getStringForUser(cr, OWNER_INFO, userId); |
| 179 | if (ownerInfo != null) { |
| 180 | setString(OWNER_INFO, ownerInfo, userId); |
| 181 | Settings.Secure.putStringForUser(cr, ownerInfo, "", userId); |
| 182 | } |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 183 | |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 184 | // Migrate owner info enabled. Note there was a bug where older platforms only |
| 185 | // stored this value if the checkbox was toggled at least once. The code detects |
| 186 | // this case by handling the exception. |
| 187 | final String OWNER_INFO_ENABLED = Secure.LOCK_SCREEN_OWNER_INFO_ENABLED; |
| 188 | boolean enabled; |
| 189 | try { |
| 190 | int ivalue = Settings.Secure.getIntForUser(cr, OWNER_INFO_ENABLED, userId); |
| 191 | enabled = ivalue != 0; |
| 192 | setLong(OWNER_INFO_ENABLED, enabled ? 1 : 0, userId); |
| 193 | } catch (SettingNotFoundException e) { |
| 194 | // Setting was never stored. Store it if the string is not empty. |
| 195 | if (!TextUtils.isEmpty(ownerInfo)) { |
| 196 | setLong(OWNER_INFO_ENABLED, 1, userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 199 | Settings.Secure.putIntForUser(cr, OWNER_INFO_ENABLED, 0, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 200 | } |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 201 | // No need to move the password / pattern files. They're already in the right place. |
| 202 | setString("migrated_user_specific", "true", 0); |
| 203 | Slog.i(TAG, "Migrated per-user lock settings to new location"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 204 | } |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 205 | |
| 206 | // Migrates biometric weak such that the fallback mechanism becomes the primary. |
| 207 | if (getString("migrated_biometric_weak", null, 0) == null) { |
| 208 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 209 | List<UserInfo> users = um.getUsers(); |
| 210 | for (int i = 0; i < users.size(); i++) { |
| 211 | int userId = users.get(i).id; |
| 212 | long type = getLong(LockPatternUtils.PASSWORD_TYPE_KEY, |
| 213 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 214 | userId); |
| 215 | long alternateType = getLong(LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 216 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 217 | userId); |
| 218 | if (type == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK) { |
| 219 | setLong(LockPatternUtils.PASSWORD_TYPE_KEY, |
| 220 | alternateType, |
| 221 | userId); |
| 222 | } |
| 223 | setLong(LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 224 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 225 | userId); |
| 226 | } |
| 227 | setString("migrated_biometric_weak", "true", 0); |
| 228 | Slog.i(TAG, "Migrated biometric weak to use the fallback instead"); |
| 229 | } |
Adrian Roos | 4383058 | 2015-04-21 16:04:43 -0700 | [diff] [blame] | 230 | |
| 231 | // Migrates lockscreen.disabled. Prior to M, the flag was ignored when more than one |
| 232 | // user was present on the system, so if we're upgrading to M and there is more than one |
| 233 | // user we disable the flag to remain consistent. |
| 234 | if (getString("migrated_lockscreen_disabled", null, 0) == null) { |
| 235 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 236 | |
| 237 | final List<UserInfo> users = um.getUsers(); |
| 238 | final int userCount = users.size(); |
| 239 | int switchableUsers = 0; |
| 240 | for (int i = 0; i < userCount; i++) { |
| 241 | if (users.get(i).supportsSwitchTo()) { |
| 242 | switchableUsers++; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (switchableUsers > 1) { |
| 247 | for (int i = 0; i < userCount; i++) { |
| 248 | int id = users.get(i).id; |
| 249 | |
| 250 | if (getBoolean(LockPatternUtils.DISABLE_LOCKSCREEN_KEY, false, id)) { |
| 251 | setBoolean(LockPatternUtils.DISABLE_LOCKSCREEN_KEY, false, id); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | setString("migrated_lockscreen_disabled", "true", 0); |
| 257 | Slog.i(TAG, "Migrated lockscreen disabled flag"); |
| 258 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 259 | } catch (RemoteException re) { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 260 | Slog.e(TAG, "Unable to migrate old data", re); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 264 | private final void checkWritePermission(int userId) { |
Jim Miller | 505329b | 2013-11-08 13:25:36 -0800 | [diff] [blame] | 265 | mContext.enforceCallingOrSelfPermission(PERMISSION, "LockSettingsWrite"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 268 | private final void checkPasswordReadPermission(int userId) { |
Jim Miller | 505329b | 2013-11-08 13:25:36 -0800 | [diff] [blame] | 269 | mContext.enforceCallingOrSelfPermission(PERMISSION, "LockSettingsRead"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 272 | private final void checkReadPermission(String requestedKey, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 273 | final int callingUid = Binder.getCallingUid(); |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 274 | |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 275 | for (int i = 0; i < READ_CONTACTS_PROTECTED_SETTINGS.length; i++) { |
| 276 | String key = READ_CONTACTS_PROTECTED_SETTINGS[i]; |
| 277 | if (key.equals(requestedKey) && mContext.checkCallingOrSelfPermission(READ_CONTACTS) |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 278 | != PackageManager.PERMISSION_GRANTED) { |
| 279 | throw new SecurityException("uid=" + callingUid |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 280 | + " needs permission " + READ_CONTACTS + " to read " |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 281 | + requestedKey + " for user " + userId); |
| 282 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 283 | } |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 284 | |
| 285 | for (int i = 0; i < READ_PASSWORD_PROTECTED_SETTINGS.length; i++) { |
| 286 | String key = READ_PASSWORD_PROTECTED_SETTINGS[i]; |
| 287 | if (key.equals(requestedKey) && mContext.checkCallingOrSelfPermission(PERMISSION) |
| 288 | != PackageManager.PERMISSION_GRANTED) { |
| 289 | throw new SecurityException("uid=" + callingUid |
| 290 | + " needs permission " + PERMISSION + " to read " |
| 291 | + requestedKey + " for user " + userId); |
| 292 | } |
| 293 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public void setBoolean(String key, boolean value, int userId) throws RemoteException { |
| 298 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 299 | setStringUnchecked(key, userId, value ? "1" : "0"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void setLong(String key, long value, int userId) throws RemoteException { |
| 304 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 305 | setStringUnchecked(key, userId, Long.toString(value)); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public void setString(String key, String value, int userId) throws RemoteException { |
| 310 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 311 | setStringUnchecked(key, userId, value); |
| 312 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 313 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 314 | private void setStringUnchecked(String key, int userId, String value) { |
| 315 | mStorage.writeKeyValue(key, value, userId); |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 316 | if (ArrayUtils.contains(SETTINGS_TO_BACKUP, key)) { |
| 317 | BackupManager.dataChanged("com.android.providers.settings"); |
| 318 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public boolean getBoolean(String key, boolean defaultValue, int userId) throws RemoteException { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 323 | checkReadPermission(key, userId); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 324 | String value = getStringUnchecked(key, null, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 325 | return TextUtils.isEmpty(value) ? |
| 326 | defaultValue : (value.equals("1") || value.equals("true")); |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public long getLong(String key, long defaultValue, int userId) throws RemoteException { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 331 | checkReadPermission(key, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 332 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 333 | String value = getStringUnchecked(key, null, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 334 | return TextUtils.isEmpty(value) ? defaultValue : Long.parseLong(value); |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public String getString(String key, String defaultValue, int userId) throws RemoteException { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 339 | checkReadPermission(key, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 340 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 341 | return getStringUnchecked(key, defaultValue, userId); |
| 342 | } |
| 343 | |
| 344 | public String getStringUnchecked(String key, String defaultValue, int userId) { |
| 345 | if (Settings.Secure.LOCK_PATTERN_ENABLED.equals(key)) { |
Adrian Roos | 7811d9f | 2015-07-27 15:10:13 -0700 | [diff] [blame] | 346 | long ident = Binder.clearCallingIdentity(); |
| 347 | try { |
| 348 | return mLockPatternUtils.isLockPatternEnabled(userId) ? "1" : "0"; |
| 349 | } finally { |
| 350 | Binder.restoreCallingIdentity(ident); |
| 351 | } |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 352 | } |
| 353 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 354 | return mStorage.readKeyValue(key, defaultValue, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Adrian Roos | 4f78845 | 2014-05-22 20:45:59 +0200 | [diff] [blame] | 357 | @Override |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 358 | public boolean havePassword(int userId) throws RemoteException { |
| 359 | // Do we need a permissions check here? |
| 360 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 361 | return mStorage.hasPassword(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public boolean havePattern(int userId) throws RemoteException { |
| 366 | // Do we need a permissions check here? |
| 367 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 368 | return mStorage.hasPattern(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 371 | private void setKeystorePassword(String password, int userHandle) { |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 372 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 373 | final KeyStore ks = KeyStore.getInstance(); |
| 374 | |
| 375 | final List<UserInfo> profiles = um.getProfiles(userHandle); |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 376 | for (UserInfo pi : profiles) { |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 377 | ks.onUserPasswordChanged(pi.id, password); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | private void unlockKeystore(String password, int userHandle) { |
| 382 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 383 | final KeyStore ks = KeyStore.getInstance(); |
| 384 | |
| 385 | final List<UserInfo> profiles = um.getProfiles(userHandle); |
| 386 | for (UserInfo pi : profiles) { |
| 387 | ks.unlock(pi.id, password); |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 391 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 392 | private byte[] getCurrentHandle(int userId) { |
| 393 | CredentialHash credential; |
| 394 | byte[] currentHandle; |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 395 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 396 | int currentHandleType = mStorage.getStoredCredentialType(userId); |
| 397 | switch (currentHandleType) { |
| 398 | case CredentialHash.TYPE_PATTERN: |
| 399 | credential = mStorage.readPatternHash(userId); |
| 400 | currentHandle = credential != null |
| 401 | ? credential.hash |
| 402 | : null; |
| 403 | break; |
| 404 | case CredentialHash.TYPE_PASSWORD: |
| 405 | credential = mStorage.readPasswordHash(userId); |
| 406 | currentHandle = credential != null |
| 407 | ? credential.hash |
| 408 | : null; |
| 409 | break; |
| 410 | case CredentialHash.TYPE_NONE: |
| 411 | default: |
| 412 | currentHandle = null; |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | // sanity check |
| 417 | if (currentHandleType != CredentialHash.TYPE_NONE && currentHandle == null) { |
| 418 | Slog.e(TAG, "Stored handle type [" + currentHandleType + "] but no handle available"); |
| 419 | } |
| 420 | |
| 421 | return currentHandle; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 424 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 425 | @Override |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 426 | public void setLockPattern(String pattern, String savedCredential, int userId) |
| 427 | throws RemoteException { |
| 428 | byte[] currentHandle = getCurrentHandle(userId); |
| 429 | |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 430 | if (pattern == null) { |
Andres Morales | cfb6160 | 2015-04-16 16:31:15 -0700 | [diff] [blame] | 431 | getGateKeeperService().clearSecureUserId(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 432 | mStorage.writePatternHash(null, userId); |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 433 | setKeystorePassword(null, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 437 | if (currentHandle == null) { |
| 438 | if (savedCredential != null) { |
| 439 | Slog.w(TAG, "Saved credential provided, but none stored"); |
| 440 | } |
| 441 | savedCredential = null; |
| 442 | } |
| 443 | |
| 444 | byte[] enrolledHandle = enrollCredential(currentHandle, savedCredential, pattern, userId); |
| 445 | if (enrolledHandle != null) { |
| 446 | mStorage.writePatternHash(enrolledHandle, userId); |
| 447 | } else { |
Andres Morales | 2c4a573 | 2015-07-09 16:11:00 -0700 | [diff] [blame] | 448 | throw new RemoteException("Failed to enroll pattern"); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | @Override |
| 454 | public void setLockPassword(String password, String savedCredential, int userId) |
| 455 | throws RemoteException { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 456 | byte[] currentHandle = getCurrentHandle(userId); |
| 457 | |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 458 | if (password == null) { |
Andres Morales | cfb6160 | 2015-04-16 16:31:15 -0700 | [diff] [blame] | 459 | getGateKeeperService().clearSecureUserId(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 460 | mStorage.writePasswordHash(null, userId); |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 461 | setKeystorePassword(null, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 462 | return; |
| 463 | } |
| 464 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 465 | if (currentHandle == null) { |
| 466 | if (savedCredential != null) { |
| 467 | Slog.w(TAG, "Saved credential provided, but none stored"); |
| 468 | } |
| 469 | savedCredential = null; |
| 470 | } |
| 471 | |
| 472 | byte[] enrolledHandle = enrollCredential(currentHandle, savedCredential, password, userId); |
| 473 | if (enrolledHandle != null) { |
| 474 | mStorage.writePasswordHash(enrolledHandle, userId); |
| 475 | } else { |
Andres Morales | 2c4a573 | 2015-07-09 16:11:00 -0700 | [diff] [blame] | 476 | throw new RemoteException("Failed to enroll password"); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | private byte[] enrollCredential(byte[] enrolledHandle, |
| 481 | String enrolledCredential, String toEnroll, int userId) |
| 482 | throws RemoteException { |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 483 | checkWritePermission(userId); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 484 | byte[] enrolledCredentialBytes = enrolledCredential == null |
| 485 | ? null |
| 486 | : enrolledCredential.getBytes(); |
| 487 | byte[] toEnrollBytes = toEnroll == null |
| 488 | ? null |
| 489 | : toEnroll.getBytes(); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 490 | GateKeeperResponse response = getGateKeeperService().enroll(userId, enrolledHandle, |
| 491 | enrolledCredentialBytes, toEnrollBytes); |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 492 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 493 | if (response == null) { |
| 494 | return null; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 495 | } |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 496 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 497 | byte[] hash = response.getPayload(); |
| 498 | if (hash != null) { |
| 499 | setKeystorePassword(toEnroll, userId); |
| 500 | } else { |
| 501 | // Should not happen |
| 502 | Slog.e(TAG, "Throttled while enrolling a password"); |
| 503 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 504 | return hash; |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | @Override |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 508 | public VerifyCredentialResponse checkPattern(String pattern, int userId) throws RemoteException { |
| 509 | return doVerifyPattern(pattern, false, 0, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 510 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 511 | |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 512 | @Override |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 513 | public VerifyCredentialResponse verifyPattern(String pattern, long challenge, int userId) |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 514 | throws RemoteException { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 515 | return doVerifyPattern(pattern, true, challenge, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 518 | private VerifyCredentialResponse doVerifyPattern(String pattern, boolean hasChallenge, |
| 519 | long challenge, int userId) throws RemoteException { |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 520 | checkPasswordReadPermission(userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 521 | CredentialHash storedHash = mStorage.readPatternHash(userId); |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 522 | boolean shouldReEnrollBaseZero = storedHash != null && storedHash.isBaseZeroPattern; |
| 523 | |
| 524 | String patternToVerify; |
| 525 | if (shouldReEnrollBaseZero) { |
| 526 | patternToVerify = LockPatternUtils.patternStringToBaseZero(pattern); |
| 527 | } else { |
| 528 | patternToVerify = pattern; |
| 529 | } |
| 530 | |
| 531 | VerifyCredentialResponse response = verifyCredential(userId, storedHash, patternToVerify, |
| 532 | hasChallenge, challenge, |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 533 | new CredentialUtil() { |
| 534 | @Override |
| 535 | public void setCredential(String pattern, String oldPattern, int userId) |
| 536 | throws RemoteException { |
| 537 | setLockPattern(pattern, oldPattern, userId); |
| 538 | } |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 539 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 540 | @Override |
| 541 | public byte[] toHash(String pattern, int userId) { |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 542 | return LockPatternUtils.patternToHash( |
| 543 | LockPatternUtils.stringToPattern(pattern)); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 544 | } |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 545 | |
| 546 | @Override |
| 547 | public String adjustForKeystore(String pattern) { |
| 548 | return LockPatternUtils.patternStringToBaseZero(pattern); |
| 549 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 550 | } |
| 551 | ); |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 552 | |
| 553 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK |
| 554 | && shouldReEnrollBaseZero) { |
| 555 | setLockPattern(pattern, patternToVerify, userId); |
| 556 | } |
| 557 | |
| 558 | return response; |
| 559 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | @Override |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 563 | public VerifyCredentialResponse checkPassword(String password, int userId) |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 564 | throws RemoteException { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 565 | return doVerifyPassword(password, false, 0, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 568 | @Override |
| 569 | public VerifyCredentialResponse verifyPassword(String password, long challenge, int userId) |
| 570 | throws RemoteException { |
| 571 | return doVerifyPassword(password, true, challenge, userId); |
| 572 | } |
| 573 | |
| 574 | private VerifyCredentialResponse doVerifyPassword(String password, boolean hasChallenge, |
| 575 | long challenge, int userId) throws RemoteException { |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 576 | checkPasswordReadPermission(userId); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 577 | CredentialHash storedHash = mStorage.readPasswordHash(userId); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 578 | return verifyCredential(userId, storedHash, password, hasChallenge, challenge, |
| 579 | new CredentialUtil() { |
| 580 | @Override |
| 581 | public void setCredential(String password, String oldPassword, int userId) |
| 582 | throws RemoteException { |
| 583 | setLockPassword(password, oldPassword, userId); |
| 584 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 585 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 586 | @Override |
| 587 | public byte[] toHash(String password, int userId) { |
| 588 | return mLockPatternUtils.passwordToHash(password, userId); |
| 589 | } |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 590 | |
| 591 | @Override |
| 592 | public String adjustForKeystore(String password) { |
| 593 | return password; |
| 594 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 595 | } |
| 596 | ); |
| 597 | } |
| 598 | |
| 599 | private VerifyCredentialResponse verifyCredential(int userId, CredentialHash storedHash, |
| 600 | String credential, boolean hasChallenge, long challenge, CredentialUtil credentialUtil) |
| 601 | throws RemoteException { |
| 602 | if ((storedHash == null || storedHash.hash.length == 0) && TextUtils.isEmpty(credential)) { |
| 603 | // don't need to pass empty credentials to GateKeeper |
| 604 | return VerifyCredentialResponse.OK; |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 607 | if (TextUtils.isEmpty(credential)) { |
| 608 | return VerifyCredentialResponse.ERROR; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 609 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 610 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 611 | if (storedHash.version == CredentialHash.VERSION_LEGACY) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 612 | byte[] hash = credentialUtil.toHash(credential, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 613 | if (Arrays.equals(hash, storedHash.hash)) { |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 614 | unlockKeystore(credentialUtil.adjustForKeystore(credential), userId); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 615 | // migrate credential to GateKeeper |
| 616 | credentialUtil.setCredential(credential, null, userId); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 617 | if (!hasChallenge) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 618 | return VerifyCredentialResponse.OK; |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 619 | } |
| 620 | // Fall through to get the auth token. Technically this should never happen, |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 621 | // 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] | 622 | // before getting to a flow with a challenge, but supporting for consistency. |
| 623 | } else { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 624 | return VerifyCredentialResponse.ERROR; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 625 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 628 | VerifyCredentialResponse response; |
| 629 | boolean shouldReEnroll = false;; |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 630 | if (hasChallenge) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 631 | byte[] token = null; |
| 632 | GateKeeperResponse gateKeeperResponse = getGateKeeperService() |
| 633 | .verifyChallenge(userId, challenge, storedHash.hash, credential.getBytes()); |
| 634 | int responseCode = gateKeeperResponse.getResponseCode(); |
| 635 | if (responseCode == GateKeeperResponse.RESPONSE_RETRY) { |
| 636 | response = new VerifyCredentialResponse(gateKeeperResponse.getTimeout()); |
| 637 | } else if (responseCode == GateKeeperResponse.RESPONSE_OK) { |
| 638 | token = gateKeeperResponse.getPayload(); |
| 639 | if (token == null) { |
| 640 | // something's wrong if there's no payload with a challenge |
| 641 | Slog.e(TAG, "verifyChallenge response had no associated payload"); |
| 642 | response = VerifyCredentialResponse.ERROR; |
| 643 | } else { |
| 644 | shouldReEnroll = gateKeeperResponse.getShouldReEnroll(); |
| 645 | response = new VerifyCredentialResponse(token); |
| 646 | } |
| 647 | } else { |
| 648 | response = VerifyCredentialResponse.ERROR; |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 649 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 650 | } else { |
| 651 | GateKeeperResponse gateKeeperResponse = getGateKeeperService().verify( |
| 652 | userId, storedHash.hash, credential.getBytes()); |
| 653 | int responseCode = gateKeeperResponse.getResponseCode(); |
| 654 | if (responseCode == GateKeeperResponse.RESPONSE_RETRY) { |
| 655 | response = new VerifyCredentialResponse(gateKeeperResponse.getTimeout()); |
| 656 | } else if (responseCode == GateKeeperResponse.RESPONSE_OK) { |
| 657 | shouldReEnroll = gateKeeperResponse.getShouldReEnroll(); |
| 658 | response = VerifyCredentialResponse.OK; |
| 659 | } else { |
| 660 | response = VerifyCredentialResponse.ERROR; |
| 661 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 662 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 663 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 664 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 665 | // credential has matched |
| 666 | unlockKeystore(credential, userId); |
| 667 | if (shouldReEnroll) { |
| 668 | credentialUtil.setCredential(credential, credential, userId); |
| 669 | } |
Adrian Roos | 873010d | 2015-08-25 15:59:00 -0700 | [diff] [blame] | 670 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 671 | if (response.getTimeout() > 0) { |
| 672 | requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_LOCKOUT, userId); |
| 673 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 674 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 675 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 676 | return response; |
| 677 | } |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 678 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 679 | @Override |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 680 | public boolean checkVoldPassword(int userId) throws RemoteException { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 681 | if (!mFirstCallToVold) { |
| 682 | return false; |
| 683 | } |
| 684 | mFirstCallToVold = false; |
| 685 | |
| 686 | checkPasswordReadPermission(userId); |
| 687 | |
| 688 | // There's no guarantee that this will safely connect, but if it fails |
| 689 | // we will simply show the lock screen when we shouldn't, so relatively |
| 690 | // benign. There is an outside chance something nasty would happen if |
| 691 | // this service restarted before vold stales out the password in this |
| 692 | // case. The nastiness is limited to not showing the lock screen when |
| 693 | // we should, within the first minute of decrypting the phone if this |
| 694 | // service can't connect to vold, it restarts, and then the new instance |
| 695 | // does successfully connect. |
| 696 | final IMountService service = getMountService(); |
| 697 | String password = service.getPassword(); |
| 698 | service.clearPassword(); |
| 699 | if (password == null) { |
| 700 | return false; |
| 701 | } |
| 702 | |
| 703 | try { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 704 | if (mLockPatternUtils.isLockPatternEnabled(userId)) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 705 | if (checkPattern(password, userId).getResponseCode() |
| 706 | == GateKeeperResponse.RESPONSE_OK) { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 707 | return true; |
| 708 | } |
| 709 | } |
| 710 | } catch (Exception e) { |
| 711 | } |
| 712 | |
| 713 | try { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 714 | if (mLockPatternUtils.isLockPasswordEnabled(userId)) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 715 | if (checkPassword(password, userId).getResponseCode() |
| 716 | == GateKeeperResponse.RESPONSE_OK) { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 717 | return true; |
| 718 | } |
| 719 | } |
| 720 | } catch (Exception e) { |
| 721 | } |
| 722 | |
| 723 | return false; |
| 724 | } |
| 725 | |
Adrian Roos | db0f76e | 2015-01-07 22:19:38 +0100 | [diff] [blame] | 726 | private void removeUser(int userId) { |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 727 | mStorage.removeUser(userId); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 728 | mStrongAuth.removeUser(userId); |
Robin Lee | 49d810c | 2014-09-23 13:50:22 +0100 | [diff] [blame] | 729 | |
| 730 | final KeyStore ks = KeyStore.getInstance(); |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 731 | ks.onUserRemoved(userId); |
Andres Morales | 070fe63 | 2015-06-24 10:37:10 -0700 | [diff] [blame] | 732 | |
| 733 | try { |
| 734 | final IGateKeeperService gk = getGateKeeperService(); |
| 735 | if (gk != null) { |
| 736 | gk.clearSecureUserId(userId); |
| 737 | } |
| 738 | } catch (RemoteException ex) { |
| 739 | Slog.w(TAG, "unable to clear GK secure user id"); |
| 740 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 743 | @Override |
| 744 | public void registerStrongAuthTracker(IStrongAuthTracker tracker) { |
| 745 | checkPasswordReadPermission(UserHandle.USER_ALL); |
| 746 | mStrongAuth.registerStrongAuthTracker(tracker); |
| 747 | } |
| 748 | |
| 749 | @Override |
| 750 | public void unregisterStrongAuthTracker(IStrongAuthTracker tracker) { |
| 751 | checkPasswordReadPermission(UserHandle.USER_ALL); |
| 752 | mStrongAuth.unregisterStrongAuthTracker(tracker); |
| 753 | } |
| 754 | |
| 755 | @Override |
| 756 | public void requireStrongAuth(int strongAuthReason, int userId) { |
| 757 | checkWritePermission(userId); |
| 758 | mStrongAuth.requireStrongAuth(strongAuthReason, userId); |
| 759 | } |
| 760 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 761 | private static final String[] VALID_SETTINGS = new String[] { |
| 762 | LockPatternUtils.LOCKOUT_PERMANENT_KEY, |
| 763 | LockPatternUtils.LOCKOUT_ATTEMPT_DEADLINE, |
| 764 | LockPatternUtils.PATTERN_EVER_CHOSEN_KEY, |
| 765 | LockPatternUtils.PASSWORD_TYPE_KEY, |
| 766 | LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 767 | LockPatternUtils.LOCK_PASSWORD_SALT_KEY, |
| 768 | LockPatternUtils.DISABLE_LOCKSCREEN_KEY, |
| 769 | LockPatternUtils.LOCKSCREEN_OPTIONS, |
| 770 | LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, |
| 771 | LockPatternUtils.BIOMETRIC_WEAK_EVER_CHOSEN_KEY, |
| 772 | LockPatternUtils.LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, |
| 773 | LockPatternUtils.PASSWORD_HISTORY_KEY, |
| 774 | Secure.LOCK_PATTERN_ENABLED, |
| 775 | Secure.LOCK_BIOMETRIC_WEAK_FLAGS, |
| 776 | Secure.LOCK_PATTERN_VISIBLE, |
| 777 | Secure.LOCK_PATTERN_TACTILE_FEEDBACK_ENABLED |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 778 | }; |
| 779 | |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 780 | // Reading these settings needs the contacts permission |
| 781 | private static final String[] READ_CONTACTS_PROTECTED_SETTINGS = new String[] { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 782 | Secure.LOCK_SCREEN_OWNER_INFO_ENABLED, |
| 783 | Secure.LOCK_SCREEN_OWNER_INFO |
| 784 | }; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 785 | |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 786 | // Reading these settings needs the same permission as checking the password |
| 787 | private static final String[] READ_PASSWORD_PROTECTED_SETTINGS = new String[] { |
| 788 | LockPatternUtils.LOCK_PASSWORD_SALT_KEY, |
| 789 | LockPatternUtils.PASSWORD_HISTORY_KEY, |
Adrian Roos | 855fa30 | 2015-04-02 16:01:12 +0200 | [diff] [blame] | 790 | LockPatternUtils.PASSWORD_TYPE_KEY, |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 791 | }; |
| 792 | |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 793 | private static final String[] SETTINGS_TO_BACKUP = new String[] { |
| 794 | Secure.LOCK_SCREEN_OWNER_INFO_ENABLED, |
| 795 | Secure.LOCK_SCREEN_OWNER_INFO |
| 796 | }; |
| 797 | |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 798 | private IMountService getMountService() { |
| 799 | final IBinder service = ServiceManager.getService("mount"); |
| 800 | if (service != null) { |
| 801 | return IMountService.Stub.asInterface(service); |
| 802 | } |
| 803 | return null; |
| 804 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 805 | |
Andres Morales | 301ea44 | 2015-04-17 09:15:47 -0700 | [diff] [blame] | 806 | private class GateKeeperDiedRecipient implements IBinder.DeathRecipient { |
| 807 | @Override |
| 808 | public void binderDied() { |
| 809 | mGateKeeperService.asBinder().unlinkToDeath(this, 0); |
| 810 | mGateKeeperService = null; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | private synchronized IGateKeeperService getGateKeeperService() |
| 815 | throws RemoteException { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 816 | if (mGateKeeperService != null) { |
| 817 | return mGateKeeperService; |
| 818 | } |
| 819 | |
| 820 | final IBinder service = |
| 821 | ServiceManager.getService("android.service.gatekeeper.IGateKeeperService"); |
| 822 | if (service != null) { |
Andres Morales | 301ea44 | 2015-04-17 09:15:47 -0700 | [diff] [blame] | 823 | service.linkToDeath(new GateKeeperDiedRecipient(), 0); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 824 | mGateKeeperService = IGateKeeperService.Stub.asInterface(service); |
| 825 | return mGateKeeperService; |
| 826 | } |
| 827 | |
| 828 | Slog.e(TAG, "Unable to acquire GateKeeperService"); |
| 829 | return null; |
| 830 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 831 | } |