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 | |
Andrew Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 17 | package com.android.server.locksettings; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 18 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 19 | import static android.Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE; |
| 20 | import static android.Manifest.permission.READ_CONTACTS; |
| 21 | import static android.content.Context.KEYGUARD_SERVICE; |
Adrian Roos | b953e18 | 2017-08-17 17:58:26 +0200 | [diff] [blame] | 22 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 23 | |
Pavel Grafov | 19a4fb3 | 2019-03-15 12:55:12 +0000 | [diff] [blame] | 24 | import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_NONE; |
| 25 | import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PASSWORD; |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 26 | import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PASSWORD_OR_PIN; |
Pavel Grafov | 19a4fb3 | 2019-03-15 12:55:12 +0000 | [diff] [blame] | 27 | import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PATTERN; |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 28 | import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PIN; |
Ram Periathiruvadi | 32d5355 | 2019-02-19 13:25:46 -0800 | [diff] [blame] | 29 | import static com.android.internal.widget.LockPatternUtils.EscrowTokenStateChangeCallback; |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 30 | import static com.android.internal.widget.LockPatternUtils.SYNTHETIC_PASSWORD_ENABLED_BY_DEFAULT; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 31 | import static com.android.internal.widget.LockPatternUtils.SYNTHETIC_PASSWORD_ENABLED_KEY; |
| 32 | import static com.android.internal.widget.LockPatternUtils.SYNTHETIC_PASSWORD_HANDLE_KEY; |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 33 | import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 34 | import static com.android.internal.widget.LockPatternUtils.USER_FRP; |
| 35 | import static com.android.internal.widget.LockPatternUtils.frpCredentialEnabled; |
Adrian Roos | 454f53f | 2017-08-08 14:56:42 +0200 | [diff] [blame] | 36 | import static com.android.internal.widget.LockPatternUtils.userOwnsFrpCredential; |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 37 | |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 38 | import android.annotation.IntDef; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 39 | import android.annotation.NonNull; |
| 40 | import android.annotation.Nullable; |
Andrew Scull | 5daf273 | 2016-11-14 15:02:45 +0000 | [diff] [blame] | 41 | import android.annotation.UserIdInt; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 42 | import android.app.ActivityManager; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 43 | import android.app.IActivityManager; |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 44 | import android.app.KeyguardManager; |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 45 | import android.app.Notification; |
| 46 | import android.app.NotificationManager; |
| 47 | import android.app.PendingIntent; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 48 | import android.app.admin.DevicePolicyManager; |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 49 | import android.app.admin.DevicePolicyManagerInternal; |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 50 | import android.app.admin.DeviceStateCache; |
Andrew Scull | 5daf273 | 2016-11-14 15:02:45 +0000 | [diff] [blame] | 51 | import android.app.admin.PasswordMetrics; |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 52 | import android.app.backup.BackupManager; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 53 | import android.app.trust.IStrongAuthTracker; |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 54 | import android.app.trust.TrustManager; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 55 | import android.content.BroadcastReceiver; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 56 | import android.content.ContentResolver; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 57 | import android.content.Context; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 58 | import android.content.Intent; |
| 59 | import android.content.IntentFilter; |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 60 | import android.content.pm.PackageManager; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 61 | import android.content.pm.UserInfo; |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 62 | import android.content.res.Resources; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 63 | import android.database.ContentObserver; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 64 | import android.database.sqlite.SQLiteDatabase; |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 65 | import android.hardware.authsecret.V1_0.IAuthSecret; |
Kevin Chyn | a38653c | 2019-02-11 17:46:21 -0800 | [diff] [blame] | 66 | import android.hardware.biometrics.BiometricManager; |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 67 | import android.hardware.face.Face; |
Kevin Chyn | a38653c | 2019-02-11 17:46:21 -0800 | [diff] [blame] | 68 | import android.hardware.face.FaceManager; |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 69 | import android.hardware.fingerprint.Fingerprint; |
| 70 | import android.hardware.fingerprint.FingerprintManager; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 71 | import android.net.Uri; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 72 | import android.os.Binder; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 73 | import android.os.Bundle; |
Ricky Wai | b0cdf38 | 2016-05-16 17:28:04 +0100 | [diff] [blame] | 74 | import android.os.Handler; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 75 | import android.os.IBinder; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 76 | import android.os.IProgressListener; |
Ricky Wai | 4613fe4 | 2016-05-24 11:11:42 +0100 | [diff] [blame] | 77 | import android.os.Process; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 78 | import android.os.RemoteException; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 79 | import android.os.ResultReceiver; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 80 | import android.os.ServiceManager; |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 81 | import android.os.ShellCallback; |
Jeff Sharkey | eddf518 | 2016-08-09 16:36:08 -0600 | [diff] [blame] | 82 | import android.os.StrictMode; |
Amith Yamasani | d1645f8 | 2012-06-12 11:53:26 -0700 | [diff] [blame] | 83 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 84 | import android.os.UserHandle; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 85 | import android.os.UserManager; |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 86 | import android.os.UserManagerInternal; |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 87 | import android.os.storage.IStorageManager; |
| 88 | import android.os.storage.StorageManager; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 89 | import android.provider.Settings; |
| 90 | import android.provider.Settings.Secure; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 91 | import android.provider.Settings.SettingNotFoundException; |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 92 | import android.security.KeyStore; |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 93 | import android.security.keystore.AndroidKeyStoreProvider; |
| 94 | import android.security.keystore.KeyProperties; |
| 95 | import android.security.keystore.KeyProtection; |
Pavel Grafov | 0acc4bf | 2017-08-23 12:20:54 +0100 | [diff] [blame] | 96 | import android.security.keystore.UserNotAuthenticatedException; |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 97 | import android.security.keystore.recovery.KeyChainProtectionParams; |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 98 | import android.security.keystore.recovery.KeyChainSnapshot; |
Bo Zhu | 7c1972f | 2018-02-22 21:43:52 -0800 | [diff] [blame] | 99 | import android.security.keystore.recovery.RecoveryCertPath; |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 100 | import android.security.keystore.recovery.WrappedApplicationKey; |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 101 | import android.service.gatekeeper.GateKeeperResponse; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 102 | import android.service.gatekeeper.IGateKeeperService; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 103 | import android.text.TextUtils; |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 104 | import android.util.ArrayMap; |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 105 | import android.util.ArraySet; |
Adrian Roos | b953e18 | 2017-08-17 17:58:26 +0200 | [diff] [blame] | 106 | import android.util.EventLog; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 107 | import android.util.Slog; |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 108 | import android.util.SparseArray; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 109 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 110 | import com.android.internal.annotations.GuardedBy; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 111 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 112 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 113 | import com.android.internal.notification.SystemNotificationChannels; |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 114 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 115 | import com.android.internal.util.DumpUtils; |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 116 | import com.android.internal.util.IndentingPrintWriter; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 117 | import com.android.internal.util.Preconditions; |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 118 | import com.android.internal.widget.ICheckCredentialProgressCallback; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 119 | import com.android.internal.widget.ILockSettings; |
| 120 | import com.android.internal.widget.LockPatternUtils; |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 121 | import com.android.internal.widget.LockSettingsInternal; |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 122 | import com.android.internal.widget.LockscreenCredential; |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 123 | import com.android.internal.widget.VerifyCredentialResponse; |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 124 | import com.android.server.LocalServices; |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 125 | import com.android.server.ServiceThread; |
Andrew Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 126 | import com.android.server.SystemService; |
| 127 | import com.android.server.locksettings.LockSettingsStorage.CredentialHash; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 128 | import com.android.server.locksettings.LockSettingsStorage.PersistentData; |
Andrew Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 129 | import com.android.server.locksettings.SyntheticPasswordManager.AuthenticationResult; |
| 130 | import com.android.server.locksettings.SyntheticPasswordManager.AuthenticationToken; |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 131 | import com.android.server.locksettings.recoverablekeystore.RecoverableKeyStoreManager; |
Adrian Roos | 1c8e3c0 | 2018-11-20 20:07:55 +0100 | [diff] [blame] | 132 | import com.android.server.wm.WindowManagerInternal; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 133 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 134 | import libcore.util.HexEncoding; |
| 135 | |
| 136 | import java.io.ByteArrayOutputStream; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 137 | import java.io.FileDescriptor; |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 138 | import java.io.FileNotFoundException; |
| 139 | import java.io.IOException; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 140 | import java.io.PrintWriter; |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 141 | import java.security.InvalidAlgorithmParameterException; |
| 142 | import java.security.InvalidKeyException; |
| 143 | import java.security.KeyStoreException; |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 144 | import java.security.MessageDigest; |
| 145 | import java.security.NoSuchAlgorithmException; |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 146 | import java.security.SecureRandom; |
| 147 | import java.security.UnrecoverableKeyException; |
| 148 | import java.security.cert.CertificateException; |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 149 | import java.text.SimpleDateFormat; |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 150 | import java.util.ArrayList; |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 151 | import java.util.Arrays; |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 152 | import java.util.Date; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 153 | import java.util.List; |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 154 | import java.util.Map; |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 155 | import java.util.NoSuchElementException; |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 156 | import java.util.Set; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 157 | import java.util.concurrent.CountDownLatch; |
| 158 | import java.util.concurrent.TimeUnit; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 159 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 160 | import javax.crypto.BadPaddingException; |
| 161 | import javax.crypto.Cipher; |
| 162 | import javax.crypto.IllegalBlockSizeException; |
| 163 | import javax.crypto.KeyGenerator; |
| 164 | import javax.crypto.NoSuchPaddingException; |
| 165 | import javax.crypto.SecretKey; |
| 166 | import javax.crypto.spec.GCMParameterSpec; |
| 167 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 168 | /** |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 169 | * Keeps the lock pattern/password data and related settings for each user. Used by |
| 170 | * LockPatternUtils. Needs to be a service because Settings app also needs to be able to save |
| 171 | * lockscreen information for secondary users. |
| 172 | * |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 173 | * @hide |
| 174 | */ |
| 175 | public class LockSettingsService extends ILockSettings.Stub { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 176 | private static final String TAG = "LockSettingsService"; |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 177 | private static final String PERMISSION = ACCESS_KEYGUARD_SECURE_STORAGE; |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 178 | private static final boolean DEBUG = false; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 179 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 180 | private static final int PROFILE_KEY_IV_SIZE = 12; |
| 181 | private static final String SEPARATE_PROFILE_CHALLENGE_KEY = "lockscreen.profilechallenge"; |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 182 | private static final String PREV_SYNTHETIC_PASSWORD_HANDLE_KEY = "prev-sp-handle"; |
| 183 | private static final String SYNTHETIC_PASSWORD_UPDATE_TIME_KEY = "sp-handle-ts"; |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 184 | |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 185 | // No challenge provided |
| 186 | private static final int CHALLENGE_NONE = 0; |
| 187 | // Challenge was provided from the external caller (non-LockSettingsService) |
| 188 | private static final int CHALLENGE_FROM_CALLER = 1; |
| 189 | // Challenge was generated from within LockSettingsService, for resetLockout. When challenge |
| 190 | // type is set to internal, LSS will revokeChallenge after all profiles for that user are |
| 191 | // unlocked. |
| 192 | private static final int CHALLENGE_INTERNAL = 2; |
| 193 | |
| 194 | @IntDef({CHALLENGE_NONE, |
| 195 | CHALLENGE_FROM_CALLER, |
| 196 | CHALLENGE_INTERNAL}) |
Rubin Xu | a758727 | 2019-07-30 17:14:28 +0100 | [diff] [blame] | 197 | @interface ChallengeType {} |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 198 | |
Rubin Xu | a0a0d35 | 2017-05-15 16:18:01 +0000 | [diff] [blame] | 199 | // Order of holding lock: mSeparateChallengeLock -> mSpManager -> this |
Rubin Xu | e94a770 | 2017-06-20 17:29:57 +0100 | [diff] [blame] | 200 | // Do not call into ActivityManager while holding mSpManager lock. |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 201 | private final Object mSeparateChallengeLock = new Object(); |
| 202 | |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 203 | private final DeviceProvisionedObserver mDeviceProvisionedObserver = |
| 204 | new DeviceProvisionedObserver(); |
| 205 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 206 | private final Injector mInjector; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 207 | private final Context mContext; |
Rubin Xu | 7cf4509 | 2017-08-28 11:47:35 +0100 | [diff] [blame] | 208 | @VisibleForTesting |
| 209 | protected final Handler mHandler; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 210 | @VisibleForTesting |
| 211 | protected final LockSettingsStorage mStorage; |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 212 | private final LockSettingsStrongAuth mStrongAuth; |
Victor Chang | a0940d3 | 2016-05-16 19:36:08 +0100 | [diff] [blame] | 213 | private final SynchronizedStrongAuthTracker mStrongAuthTracker; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 214 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 215 | private final LockPatternUtils mLockPatternUtils; |
| 216 | private final NotificationManager mNotificationManager; |
| 217 | private final UserManager mUserManager; |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 218 | private final IStorageManager mStorageManager; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 219 | private final IActivityManager mActivityManager; |
Andrew Scull | 8fc2ec8 | 2017-05-19 10:50:36 +0100 | [diff] [blame] | 220 | private final SyntheticPasswordManager mSpManager; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 221 | |
| 222 | private final KeyStore mKeyStore; |
| 223 | |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 224 | private final RecoverableKeyStoreManager mRecoverableKeyStoreManager; |
| 225 | |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 226 | private boolean mFirstCallToVold; |
Rubin Xu | 1985486 | 2019-08-15 16:37:23 +0100 | [diff] [blame] | 227 | // Current password metric for all users on the device. Updated when user unlocks |
| 228 | // the device or changes password. Removed when user is stopped. |
| 229 | @GuardedBy("this") |
| 230 | final SparseArray<PasswordMetrics> mUserPasswordMetrics = new SparseArray<>(); |
| 231 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 232 | protected IGateKeeperService mGateKeeperService; |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 233 | protected IAuthSecret mAuthSecretService; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 234 | |
David Anderson | 6ebc25b | 2019-02-12 16:25:56 -0800 | [diff] [blame] | 235 | private static final String GSI_RUNNING_PROP = "ro.gsid.image_running"; |
| 236 | |
Ricky Wai | 4613fe4 | 2016-05-24 11:11:42 +0100 | [diff] [blame] | 237 | /** |
| 238 | * The UIDs that are used for system credential storage in keystore. |
| 239 | */ |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 240 | private static final int[] SYSTEM_CREDENTIAL_UIDS = { |
| 241 | Process.WIFI_UID, Process.VPN_UID, |
Rubin Xu | 24b89b1 | 2017-04-26 19:44:16 +0100 | [diff] [blame] | 242 | Process.ROOT_UID, Process.SYSTEM_UID }; |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 243 | |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 244 | // This class manages life cycle events for encrypted users on File Based Encryption (FBE) |
| 245 | // devices. The most basic of these is to show/hide notifications about missing features until |
| 246 | // the user unlocks the account and credential-encrypted storage is available. |
| 247 | public static final class Lifecycle extends SystemService { |
| 248 | private LockSettingsService mLockSettingsService; |
| 249 | |
| 250 | public Lifecycle(Context context) { |
| 251 | super(context); |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void onStart() { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 256 | AndroidKeyStoreProvider.install(); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 257 | mLockSettingsService = new LockSettingsService(getContext()); |
| 258 | publishBinderService("lock_settings", mLockSettingsService); |
| 259 | } |
| 260 | |
| 261 | @Override |
Adrian Roos | 60dcbbf | 2017-08-08 16:19:33 +0200 | [diff] [blame] | 262 | public void onBootPhase(int phase) { |
| 263 | super.onBootPhase(phase); |
| 264 | if (phase == PHASE_ACTIVITY_MANAGER_READY) { |
| 265 | mLockSettingsService.migrateOldDataAfterSystemReady(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | @Override |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 270 | public void onStartUser(int userHandle) { |
| 271 | mLockSettingsService.onStartUser(userHandle); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public void onUnlockUser(int userHandle) { |
| 276 | mLockSettingsService.onUnlockUser(userHandle); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void onCleanupUser(int userHandle) { |
| 281 | mLockSettingsService.onCleanupUser(userHandle); |
| 282 | } |
| 283 | } |
| 284 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 285 | @VisibleForTesting |
| 286 | protected static class SynchronizedStrongAuthTracker |
| 287 | extends LockPatternUtils.StrongAuthTracker { |
Victor Chang | a0940d3 | 2016-05-16 19:36:08 +0100 | [diff] [blame] | 288 | public SynchronizedStrongAuthTracker(Context context) { |
| 289 | super(context); |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | protected void handleStrongAuthRequiredChanged(int strongAuthFlags, int userId) { |
| 294 | synchronized (this) { |
| 295 | super.handleStrongAuthRequiredChanged(strongAuthFlags, userId); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public int getStrongAuthForUser(int userId) { |
| 301 | synchronized (this) { |
| 302 | return super.getStrongAuthForUser(userId); |
| 303 | } |
| 304 | } |
| 305 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 306 | void register(LockSettingsStrongAuth strongAuth) { |
| 307 | strongAuth.registerStrongAuthTracker(this.mStub); |
Victor Chang | a0940d3 | 2016-05-16 19:36:08 +0100 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 311 | private class PendingResetLockout { |
| 312 | final int mUserId; |
| 313 | final byte[] mHAT; |
| 314 | PendingResetLockout(int userId, byte[] hat) { |
| 315 | mUserId = userId; |
| 316 | mHAT = hat; |
| 317 | } |
| 318 | } |
| 319 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 320 | private LockscreenCredential generateRandomProfilePassword() { |
| 321 | byte[] randomLockSeed = new byte[] {}; |
| 322 | try { |
| 323 | randomLockSeed = SecureRandom.getInstance("SHA1PRNG").generateSeed(40); |
| 324 | char[] newPasswordChars = HexEncoding.encode(randomLockSeed); |
| 325 | byte[] newPassword = new byte[newPasswordChars.length]; |
| 326 | for (int i = 0; i < newPasswordChars.length; i++) { |
| 327 | newPassword[i] = (byte) newPasswordChars[i]; |
| 328 | } |
| 329 | LockscreenCredential credential = |
| 330 | LockscreenCredential.createManagedPassword(newPassword); |
| 331 | Arrays.fill(newPasswordChars, '\u0000'); |
| 332 | Arrays.fill(newPassword, (byte) 0); |
| 333 | Arrays.fill(randomLockSeed, (byte) 0); |
| 334 | return credential; |
| 335 | } catch (NoSuchAlgorithmException e) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 336 | throw new IllegalStateException("Fail to generate profile password", e); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 340 | /** |
| 341 | * Tie managed profile to primary profile if it is in unified mode and not tied before. |
| 342 | * |
| 343 | * @param managedUserId Managed profile user Id |
| 344 | * @param managedUserPassword Managed profile original password (when it has separated lock). |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 345 | */ |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 346 | public void tieManagedProfileLockIfNecessary(int managedUserId, |
| 347 | LockscreenCredential managedUserPassword) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 348 | if (DEBUG) Slog.v(TAG, "Check child profile lock for user: " + managedUserId); |
| 349 | // Only for managed profile |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 350 | if (!mUserManager.getUserInfo(managedUserId).isManagedProfile()) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | // Do not tie managed profile when work challenge is enabled |
| 354 | if (mLockPatternUtils.isSeparateProfileChallengeEnabled(managedUserId)) { |
| 355 | return; |
| 356 | } |
| 357 | // Do not tie managed profile to parent when it's done already |
| 358 | if (mStorage.hasChildProfileLock(managedUserId)) { |
| 359 | return; |
| 360 | } |
| 361 | // Do not tie it to parent when parent does not have a screen lock |
| 362 | final int parentId = mUserManager.getProfileParent(managedUserId).id; |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 363 | if (!isUserSecure(parentId)) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 364 | if (DEBUG) Slog.v(TAG, "Parent does not have a screen lock"); |
| 365 | return; |
| 366 | } |
Rubin Xu | bfc7faaf | 2016-11-22 15:18:32 +0000 | [diff] [blame] | 367 | // Do not tie when the parent has no SID (but does have a screen lock). |
| 368 | // This can only happen during an upgrade path where SID is yet to be |
| 369 | // generated when the user unlocks for the first time. |
| 370 | try { |
| 371 | if (getGateKeeperService().getSecureUserId(parentId) == 0) { |
| 372 | return; |
| 373 | } |
| 374 | } catch (RemoteException e) { |
| 375 | Slog.e(TAG, "Failed to talk to GateKeeper service", e); |
| 376 | return; |
| 377 | } |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 378 | if (DEBUG) Slog.v(TAG, "Tie managed profile to parent now!"); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 379 | try (LockscreenCredential unifiedProfilePassword = generateRandomProfilePassword()) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 380 | setLockCredentialInternal(unifiedProfilePassword, managedUserPassword, managedUserId, |
| 381 | false, /* isLockTiedToParent= */ true); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 382 | tieProfileLockToParent(managedUserId, unifiedProfilePassword); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 386 | static class Injector { |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 387 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 388 | protected Context mContext; |
| 389 | |
| 390 | public Injector(Context context) { |
| 391 | mContext = context; |
| 392 | } |
| 393 | |
| 394 | public Context getContext() { |
| 395 | return mContext; |
| 396 | } |
| 397 | |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 398 | public ServiceThread getServiceThread() { |
| 399 | ServiceThread handlerThread = new ServiceThread(TAG, Process.THREAD_PRIORITY_BACKGROUND, |
| 400 | true /*allowIo*/); |
| 401 | handlerThread.start(); |
| 402 | return handlerThread; |
| 403 | } |
| 404 | |
| 405 | public Handler getHandler(ServiceThread handlerThread) { |
| 406 | return new Handler(handlerThread.getLooper()); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | public LockSettingsStorage getStorage() { |
| 410 | final LockSettingsStorage storage = new LockSettingsStorage(mContext); |
| 411 | storage.setDatabaseOnCreateCallback(new LockSettingsStorage.Callback() { |
| 412 | @Override |
| 413 | public void initialize(SQLiteDatabase db) { |
| 414 | // Get the lockscreen default from a system property, if available |
| 415 | boolean lockScreenDisable = SystemProperties.getBoolean( |
| 416 | "ro.lockscreen.disable.default", false); |
| 417 | if (lockScreenDisable) { |
| 418 | storage.writeKeyValue(db, LockPatternUtils.DISABLE_LOCKSCREEN_KEY, "1", 0); |
| 419 | } |
| 420 | } |
| 421 | }); |
| 422 | return storage; |
| 423 | } |
| 424 | |
| 425 | public LockSettingsStrongAuth getStrongAuth() { |
| 426 | return new LockSettingsStrongAuth(mContext); |
| 427 | } |
| 428 | |
| 429 | public SynchronizedStrongAuthTracker getStrongAuthTracker() { |
| 430 | return new SynchronizedStrongAuthTracker(mContext); |
| 431 | } |
| 432 | |
| 433 | public IActivityManager getActivityManager() { |
| 434 | return ActivityManager.getService(); |
| 435 | } |
| 436 | |
| 437 | public LockPatternUtils getLockPatternUtils() { |
| 438 | return new LockPatternUtils(mContext); |
| 439 | } |
| 440 | |
| 441 | public NotificationManager getNotificationManager() { |
| 442 | return (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 443 | } |
| 444 | |
| 445 | public UserManager getUserManager() { |
| 446 | return (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 447 | } |
| 448 | |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 449 | public UserManagerInternal getUserManagerInternal() { |
| 450 | return LocalServices.getService(UserManagerInternal.class); |
| 451 | } |
| 452 | |
Rubin Xu | 1985486 | 2019-08-15 16:37:23 +0100 | [diff] [blame] | 453 | /** |
| 454 | * Return the {@link DevicePolicyManager} object. |
| 455 | * |
| 456 | * Since LockSettingsService is considered a lower-level component than DevicePolicyManager, |
| 457 | * do NOT hold any lock in this class while calling into DevicePolicyManager to prevent |
| 458 | * the risk of deadlock. |
| 459 | */ |
Rubin Xu | 8b30ec3 | 2017-03-05 00:47:09 +0000 | [diff] [blame] | 460 | public DevicePolicyManager getDevicePolicyManager() { |
| 461 | return (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 462 | } |
| 463 | |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 464 | public DeviceStateCache getDeviceStateCache() { |
| 465 | return DeviceStateCache.getInstance(); |
| 466 | } |
| 467 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 468 | public KeyStore getKeyStore() { |
| 469 | return KeyStore.getInstance(); |
| 470 | } |
| 471 | |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 472 | public RecoverableKeyStoreManager getRecoverableKeyStoreManager(KeyStore keyStore) { |
| 473 | return RecoverableKeyStoreManager.getInstance(mContext, keyStore); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 476 | public IStorageManager getStorageManager() { |
| 477 | final IBinder service = ServiceManager.getService("mount"); |
| 478 | if (service != null) { |
| 479 | return IStorageManager.Stub.asInterface(service); |
| 480 | } |
| 481 | return null; |
| 482 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 483 | |
| 484 | public SyntheticPasswordManager getSyntheticPasswordManager(LockSettingsStorage storage) { |
David Anderson | 28dea68 | 2019-02-20 13:37:51 -0800 | [diff] [blame] | 485 | return new SyntheticPasswordManager(getContext(), storage, getUserManager(), |
| 486 | new PasswordSlotManager()); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Kevin Chyn | 28623b6 | 2019-06-11 11:43:33 -0700 | [diff] [blame] | 489 | public boolean hasEnrolledBiometrics(int userId) { |
Kevin Chyn | 71db85f | 2019-05-14 15:32:47 -0700 | [diff] [blame] | 490 | BiometricManager bm = mContext.getSystemService(BiometricManager.class); |
Kevin Chyn | 27f9231 | 2019-07-26 12:41:33 -0700 | [diff] [blame] | 491 | return bm.hasEnrolledBiometrics(userId); |
Kevin Chyn | cf1a62e | 2019-02-21 11:11:59 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 494 | public int binderGetCallingUid() { |
| 495 | return Binder.getCallingUid(); |
| 496 | } |
David Anderson | 6ebc25b | 2019-02-12 16:25:56 -0800 | [diff] [blame] | 497 | |
| 498 | public boolean isGsiRunning() { |
| 499 | return SystemProperties.getInt(GSI_RUNNING_PROP, 0) > 0; |
| 500 | } |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 501 | |
| 502 | public FingerprintManager getFingerprintManager() { |
| 503 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) { |
| 504 | return (FingerprintManager) mContext.getSystemService(Context.FINGERPRINT_SERVICE); |
| 505 | } else { |
| 506 | return null; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | public FaceManager getFaceManager() { |
| 511 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FACE)) { |
| 512 | return (FaceManager) mContext.getSystemService(Context.FACE_SERVICE); |
| 513 | } else { |
| 514 | return null; |
| 515 | } |
| 516 | } |
| 517 | |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 518 | public int settingsGlobalGetInt(ContentResolver contentResolver, String keyName, |
| 519 | int defaultValue) { |
| 520 | return Settings.Global.getInt(contentResolver, keyName, defaultValue); |
| 521 | } |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | public LockSettingsService(Context context) { |
| 525 | this(new Injector(context)); |
| 526 | } |
| 527 | |
| 528 | @VisibleForTesting |
| 529 | protected LockSettingsService(Injector injector) { |
| 530 | mInjector = injector; |
| 531 | mContext = injector.getContext(); |
| 532 | mKeyStore = injector.getKeyStore(); |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 533 | mRecoverableKeyStoreManager = injector.getRecoverableKeyStoreManager(mKeyStore); |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 534 | mHandler = injector.getHandler(injector.getServiceThread()); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 535 | mStrongAuth = injector.getStrongAuth(); |
| 536 | mActivityManager = injector.getActivityManager(); |
| 537 | |
| 538 | mLockPatternUtils = injector.getLockPatternUtils(); |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 539 | mFirstCallToVold = true; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 540 | |
| 541 | IntentFilter filter = new IntentFilter(); |
| 542 | filter.addAction(Intent.ACTION_USER_ADDED); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 543 | filter.addAction(Intent.ACTION_USER_STARTING); |
Adrian Roos | db0f76e | 2015-01-07 22:19:38 +0100 | [diff] [blame] | 544 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 545 | injector.getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, |
| 546 | null, null); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 547 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 548 | mStorage = injector.getStorage(); |
| 549 | mNotificationManager = injector.getNotificationManager(); |
| 550 | mUserManager = injector.getUserManager(); |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 551 | mStorageManager = injector.getStorageManager(); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 552 | mStrongAuthTracker = injector.getStrongAuthTracker(); |
| 553 | mStrongAuthTracker.register(mStrongAuth); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 554 | |
| 555 | mSpManager = injector.getSyntheticPasswordManager(mStorage); |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 556 | |
| 557 | LocalServices.addService(LockSettingsInternal.class, new LocalService()); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /** |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 561 | * If the account is credential-encrypted, show notification requesting the user to unlock the |
| 562 | * device. |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 563 | */ |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 564 | private void maybeShowEncryptionNotificationForUser(@UserIdInt int userId) { |
| 565 | final UserInfo user = mUserManager.getUserInfo(userId); |
| 566 | if (!user.isManagedProfile()) { |
| 567 | // When the user is locked, we communicate it loud-and-clear |
| 568 | // on the lockscreen; we only show a notification below for |
| 569 | // locked managed profiles. |
| 570 | return; |
| 571 | } |
| 572 | |
Irina Dumitrescu | e13ce66 | 2019-05-09 17:50:50 +0100 | [diff] [blame] | 573 | if (isUserKeyUnlocked(userId)) { |
| 574 | // If storage is not locked, the user will be automatically unlocked so there is |
| 575 | // no need to show the notification. |
| 576 | return; |
| 577 | } |
| 578 | |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 579 | final UserHandle userHandle = user.getUserHandle(); |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 580 | final boolean isSecure = isUserSecure(userId); |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 581 | if (isSecure && !mUserManager.isUserUnlockingOrUnlocked(userHandle)) { |
| 582 | UserInfo parent = mUserManager.getProfileParent(userId); |
| 583 | if (parent != null && |
| 584 | mUserManager.isUserUnlockingOrUnlocked(parent.getUserHandle()) && |
| 585 | !mUserManager.isQuietModeEnabled(userHandle)) { |
| 586 | // Only show notifications for managed profiles once their parent |
| 587 | // user is unlocked. |
| 588 | showEncryptionNotificationForProfile(userHandle); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 589 | } |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 593 | private void showEncryptionNotificationForProfile(UserHandle user) { |
| 594 | Resources r = mContext.getResources(); |
| 595 | CharSequence title = r.getText( |
Rubin Xu | 897750a | 2019-04-30 17:15:03 +0100 | [diff] [blame] | 596 | com.android.internal.R.string.profile_encrypted_title); |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 597 | CharSequence message = r.getText( |
| 598 | com.android.internal.R.string.profile_encrypted_message); |
| 599 | CharSequence detail = r.getText( |
| 600 | com.android.internal.R.string.profile_encrypted_detail); |
| 601 | |
| 602 | final KeyguardManager km = (KeyguardManager) mContext.getSystemService(KEYGUARD_SERVICE); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 603 | final Intent unlockIntent = km.createConfirmDeviceCredentialIntent(null, null, |
| 604 | user.getIdentifier()); |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 605 | if (unlockIntent == null) { |
| 606 | return; |
| 607 | } |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 608 | unlockIntent.setFlags( |
| 609 | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 610 | PendingIntent intent = PendingIntent.getActivity(mContext, 0, unlockIntent, |
| 611 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 612 | |
| 613 | showEncryptionNotification(user, title, message, detail, intent); |
| 614 | } |
| 615 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 616 | private void showEncryptionNotification(UserHandle user, CharSequence title, |
| 617 | CharSequence message, CharSequence detail, PendingIntent intent) { |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 618 | if (DEBUG) Slog.v(TAG, "showing encryption notification, user: " + user.getIdentifier()); |
Jeff Sharkey | 4a39936 | 2016-05-26 09:47:43 -0600 | [diff] [blame] | 619 | |
| 620 | // Suppress all notifications on non-FBE devices for now |
| 621 | if (!StorageManager.isFileEncryptedNativeOrEmulated()) return; |
| 622 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 623 | Notification notification = |
Rubin Xu | 897750a | 2019-04-30 17:15:03 +0100 | [diff] [blame] | 624 | new Notification.Builder(mContext, SystemNotificationChannels.DEVICE_ADMIN) |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 625 | .setSmallIcon(com.android.internal.R.drawable.ic_user_secure) |
| 626 | .setWhen(0) |
| 627 | .setOngoing(true) |
| 628 | .setTicker(title) |
| 629 | .setColor(mContext.getColor( |
| 630 | com.android.internal.R.color.system_notification_accent_color)) |
| 631 | .setContentTitle(title) |
| 632 | .setContentText(message) |
| 633 | .setSubText(detail) |
| 634 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 635 | .setContentIntent(intent) |
| 636 | .build(); |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 637 | mNotificationManager.notifyAsUser(null, SystemMessage.NOTE_FBE_ENCRYPTED_NOTIFICATION, |
| 638 | notification, user); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 641 | private void hideEncryptionNotification(UserHandle userHandle) { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 642 | if (DEBUG) Slog.v(TAG, "hide encryption notification, user: " + userHandle.getIdentifier()); |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 643 | mNotificationManager.cancelAsUser(null, SystemMessage.NOTE_FBE_ENCRYPTED_NOTIFICATION, |
| 644 | userHandle); |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | public void onCleanupUser(int userId) { |
| 648 | hideEncryptionNotification(new UserHandle(userId)); |
Erin Yan | 324d75e | 2019-05-16 17:22:21 -0700 | [diff] [blame] | 649 | // User is stopped with its CE key evicted. Restore strong auth requirement to the default |
| 650 | // flags after boot since stopping and restarting a user later is equivalent to rebooting |
| 651 | // the device. |
| 652 | int strongAuthRequired = LockPatternUtils.StrongAuthTracker.getDefaultFlags(mContext); |
| 653 | requireStrongAuth(strongAuthRequired, userId); |
Rubin Xu | 1985486 | 2019-08-15 16:37:23 +0100 | [diff] [blame] | 654 | synchronized (this) { |
| 655 | mUserPasswordMetrics.remove(userId); |
| 656 | } |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 657 | } |
| 658 | |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 659 | public void onStartUser(final int userId) { |
| 660 | maybeShowEncryptionNotificationForUser(userId); |
| 661 | } |
| 662 | |
Pavel Grafov | 0acc4bf | 2017-08-23 12:20:54 +0100 | [diff] [blame] | 663 | /** |
| 664 | * Check if profile got unlocked but the keystore is still locked. This happens on full disk |
| 665 | * encryption devices since the profile may not yet be running when we consider unlocking it |
| 666 | * during the normal flow. In this case unlock the keystore for the profile. |
| 667 | */ |
| 668 | private void ensureProfileKeystoreUnlocked(int userId) { |
| 669 | final KeyStore ks = KeyStore.getInstance(); |
| 670 | if (ks.state(userId) == KeyStore.State.LOCKED |
| 671 | && tiedManagedProfileReadyToUnlock(mUserManager.getUserInfo(userId))) { |
| 672 | Slog.i(TAG, "Managed profile got unlocked, will unlock its keystore"); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 673 | // If boot took too long and the password in vold got expired, parent keystore will |
| 674 | // be still locked, we ignore this case since the user will be prompted to unlock |
| 675 | // the device after boot. |
| 676 | unlockChildProfile(userId, true /* ignoreUserNotAuthenticated */, |
| 677 | CHALLENGE_NONE, 0 /* challenge */, null /* resetLockouts */); |
Pavel Grafov | 0acc4bf | 2017-08-23 12:20:54 +0100 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Ricky Wai | b0cdf38 | 2016-05-16 17:28:04 +0100 | [diff] [blame] | 681 | public void onUnlockUser(final int userId) { |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 682 | // Perform tasks which require locks in LSS on a handler, as we are callbacks from |
| 683 | // ActivityManager.unlockUser() |
| 684 | mHandler.post(new Runnable() { |
| 685 | @Override |
| 686 | public void run() { |
Pavel Grafov | 0acc4bf | 2017-08-23 12:20:54 +0100 | [diff] [blame] | 687 | ensureProfileKeystoreUnlocked(userId); |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 688 | // Hide notification first, as tie managed profile lock takes time |
| 689 | hideEncryptionNotification(new UserHandle(userId)); |
Ricky Wai | b0cdf38 | 2016-05-16 17:28:04 +0100 | [diff] [blame] | 690 | |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 691 | if (mUserManager.getUserInfo(userId).isManagedProfile()) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 692 | tieManagedProfileLockIfNecessary(userId, LockscreenCredential.createNone()); |
Ricky Wai | b0cdf38 | 2016-05-16 17:28:04 +0100 | [diff] [blame] | 693 | } |
Andrew Scull | f49794b | 2018-04-13 12:01:25 +0100 | [diff] [blame] | 694 | |
| 695 | // If the user doesn't have a credential, try and derive their secret for the |
| 696 | // AuthSecret HAL. The secret will have been enrolled if the user previously set a |
| 697 | // credential and still needs to be passed to the HAL once that credential is |
| 698 | // removed. |
| 699 | if (mUserManager.getUserInfo(userId).isPrimary() && !isUserSecure(userId)) { |
| 700 | tryDeriveAuthTokenForUnsecuredPrimaryUser(userId); |
| 701 | } |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 702 | } |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 703 | }); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Andrew Scull | f49794b | 2018-04-13 12:01:25 +0100 | [diff] [blame] | 706 | private void tryDeriveAuthTokenForUnsecuredPrimaryUser(@UserIdInt int userId) { |
| 707 | synchronized (mSpManager) { |
| 708 | // Make sure the user has a synthetic password to derive |
| 709 | if (!isSyntheticPasswordBasedCredentialLocked(userId)) { |
| 710 | return; |
| 711 | } |
| 712 | |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 713 | final long handle = getSyntheticPasswordHandleLocked(userId); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 714 | AuthenticationResult result = |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 715 | mSpManager.unwrapPasswordBasedSyntheticPassword(getGateKeeperService(), |
| 716 | handle, LockscreenCredential.createNone(), userId, null); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 717 | if (result.authToken != null) { |
| 718 | Slog.i(TAG, "Retrieved auth token for user " + userId); |
| 719 | onAuthTokenKnownForUser(userId, result.authToken); |
| 720 | } else { |
| 721 | Slog.e(TAG, "Auth token not available for user " + userId); |
Andrew Scull | f49794b | 2018-04-13 12:01:25 +0100 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 726 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 727 | @Override |
| 728 | public void onReceive(Context context, Intent intent) { |
Robin Lee | 1096cf8 | 2014-09-01 16:52:47 +0100 | [diff] [blame] | 729 | if (Intent.ACTION_USER_ADDED.equals(intent.getAction())) { |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 730 | // Notify keystore that a new user was added. |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 731 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
Amith Yamasani | f11a574 | 2016-06-16 08:20:07 -0700 | [diff] [blame] | 732 | if (userHandle > UserHandle.USER_SYSTEM) { |
| 733 | removeUser(userHandle, /* unknownUser= */ true); |
| 734 | } |
Robin Lee | 49d810c | 2014-09-23 13:50:22 +0100 | [diff] [blame] | 735 | final KeyStore ks = KeyStore.getInstance(); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 736 | final UserInfo parentInfo = mUserManager.getProfileParent(userHandle); |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 737 | final int parentHandle = parentInfo != null ? parentInfo.id : -1; |
| 738 | ks.onUserAdded(userHandle, parentHandle); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 739 | } else if (Intent.ACTION_USER_STARTING.equals(intent.getAction())) { |
| 740 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 741 | mStorage.prefetchUser(userHandle); |
Adrian Roos | db0f76e | 2015-01-07 22:19:38 +0100 | [diff] [blame] | 742 | } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) { |
| 743 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 744 | if (userHandle > 0) { |
Amith Yamasani | f11a574 | 2016-06-16 08:20:07 -0700 | [diff] [blame] | 745 | removeUser(userHandle, /* unknownUser= */ false); |
Adrian Roos | db0f76e | 2015-01-07 22:19:38 +0100 | [diff] [blame] | 746 | } |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | }; |
| 750 | |
Jim Miller | 4f93c58 | 2016-01-27 19:05:43 -0800 | [diff] [blame] | 751 | @Override // binder interface |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 752 | public void systemReady() { |
Adrian Roos | b953e18 | 2017-08-17 17:58:26 +0200 | [diff] [blame] | 753 | if (mContext.checkCallingOrSelfPermission(PERMISSION) != PERMISSION_GRANTED) { |
| 754 | EventLog.writeEvent(0x534e4554, "28251513", getCallingUid(), ""); // SafetyNet |
| 755 | } |
| 756 | checkWritePermission(UserHandle.USER_SYSTEM); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 757 | migrateOldData(); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 758 | getGateKeeperService(); |
| 759 | mSpManager.initWeaverService(); |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 760 | // Find the AuthSecret HAL |
| 761 | try { |
| 762 | mAuthSecretService = IAuthSecret.getService(); |
| 763 | } catch (NoSuchElementException e) { |
| 764 | Slog.i(TAG, "Device doesn't implement AuthSecret HAL"); |
| 765 | } catch (RemoteException e) { |
| 766 | Slog.w(TAG, "Failed to get AuthSecret HAL", e); |
| 767 | } |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 768 | mDeviceProvisionedObserver.onSystemReady(); |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 769 | // TODO: maybe skip this for split system user mode. |
| 770 | mStorage.prefetchUser(UserHandle.USER_SYSTEM); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | private void migrateOldData() { |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 774 | // These Settings moved before multi-user was enabled, so we only have to do it for the |
| 775 | // root user. |
| 776 | if (getString("migrated", null, 0) == null) { |
| 777 | final ContentResolver cr = mContext.getContentResolver(); |
| 778 | for (String validSetting : VALID_SETTINGS) { |
| 779 | String value = Settings.Secure.getString(cr, validSetting); |
| 780 | if (value != null) { |
| 781 | setString(validSetting, value, 0); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 782 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 783 | } |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 784 | // No need to move the password / pattern files. They're already in the right place. |
| 785 | setString("migrated", "true", 0); |
| 786 | Slog.i(TAG, "Migrated lock settings to new location"); |
| 787 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 788 | |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 789 | // These Settings changed after multi-user was enabled, hence need to be moved per user. |
| 790 | if (getString("migrated_user_specific", null, 0) == null) { |
| 791 | final ContentResolver cr = mContext.getContentResolver(); |
| 792 | List<UserInfo> users = mUserManager.getUsers(); |
| 793 | for (int user = 0; user < users.size(); user++) { |
| 794 | // Migrate owner info |
| 795 | final int userId = users.get(user).id; |
| 796 | final String OWNER_INFO = Secure.LOCK_SCREEN_OWNER_INFO; |
| 797 | String ownerInfo = Settings.Secure.getStringForUser(cr, OWNER_INFO, userId); |
| 798 | if (!TextUtils.isEmpty(ownerInfo)) { |
| 799 | setString(OWNER_INFO, ownerInfo, userId); |
| 800 | Settings.Secure.putStringForUser(cr, OWNER_INFO, "", userId); |
Adrian Roos | 4383058 | 2015-04-21 16:04:43 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 803 | // Migrate owner info enabled. Note there was a bug where older platforms only |
| 804 | // stored this value if the checkbox was toggled at least once. The code detects |
| 805 | // this case by handling the exception. |
| 806 | final String OWNER_INFO_ENABLED = Secure.LOCK_SCREEN_OWNER_INFO_ENABLED; |
| 807 | boolean enabled; |
Ricky Wai | 97c8f8d | 2016-07-13 17:57:45 +0100 | [diff] [blame] | 808 | try { |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 809 | int ivalue = Settings.Secure.getIntForUser(cr, OWNER_INFO_ENABLED, userId); |
| 810 | enabled = ivalue != 0; |
| 811 | setLong(OWNER_INFO_ENABLED, enabled ? 1 : 0, userId); |
| 812 | } catch (SettingNotFoundException e) { |
| 813 | // Setting was never stored. Store it if the string is not empty. |
| 814 | if (!TextUtils.isEmpty(ownerInfo)) { |
| 815 | setLong(OWNER_INFO_ENABLED, 1, userId); |
Ricky Wai | 97c8f8d | 2016-07-13 17:57:45 +0100 | [diff] [blame] | 816 | } |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 817 | } |
| 818 | Settings.Secure.putIntForUser(cr, OWNER_INFO_ENABLED, 0, userId); |
| 819 | } |
| 820 | // No need to move the password / pattern files. They're already in the right place. |
| 821 | setString("migrated_user_specific", "true", 0); |
| 822 | Slog.i(TAG, "Migrated per-user lock settings to new location"); |
| 823 | } |
| 824 | |
| 825 | // Migrates biometric weak such that the fallback mechanism becomes the primary. |
| 826 | if (getString("migrated_biometric_weak", null, 0) == null) { |
| 827 | List<UserInfo> users = mUserManager.getUsers(); |
| 828 | for (int i = 0; i < users.size(); i++) { |
| 829 | int userId = users.get(i).id; |
| 830 | long type = getLong(LockPatternUtils.PASSWORD_TYPE_KEY, |
| 831 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 832 | userId); |
| 833 | long alternateType = getLong(LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 834 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 835 | userId); |
| 836 | if (type == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK) { |
| 837 | setLong(LockPatternUtils.PASSWORD_TYPE_KEY, |
| 838 | alternateType, |
| 839 | userId); |
| 840 | } |
| 841 | setLong(LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 842 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 843 | userId); |
| 844 | } |
| 845 | setString("migrated_biometric_weak", "true", 0); |
| 846 | Slog.i(TAG, "Migrated biometric weak to use the fallback instead"); |
| 847 | } |
| 848 | |
| 849 | // Migrates lockscreen.disabled. Prior to M, the flag was ignored when more than one |
| 850 | // user was present on the system, so if we're upgrading to M and there is more than one |
| 851 | // user we disable the flag to remain consistent. |
| 852 | if (getString("migrated_lockscreen_disabled", null, 0) == null) { |
| 853 | final List<UserInfo> users = mUserManager.getUsers(); |
| 854 | final int userCount = users.size(); |
| 855 | int switchableUsers = 0; |
| 856 | for (int i = 0; i < userCount; i++) { |
| 857 | if (users.get(i).supportsSwitchTo()) { |
| 858 | switchableUsers++; |
Ricky Wai | 97c8f8d | 2016-07-13 17:57:45 +0100 | [diff] [blame] | 859 | } |
Ricky Wai | 7b9eb41 | 2016-05-12 17:29:12 +0100 | [diff] [blame] | 860 | } |
Greg Plesur | eb2e453 | 2016-11-02 17:10:27 -0400 | [diff] [blame] | 861 | |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 862 | if (switchableUsers > 1) { |
Greg Plesur | eb2e453 | 2016-11-02 17:10:27 -0400 | [diff] [blame] | 863 | for (int i = 0; i < userCount; i++) { |
| 864 | int id = users.get(i).id; |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 865 | |
| 866 | if (getBoolean(LockPatternUtils.DISABLE_LOCKSCREEN_KEY, false, id)) { |
| 867 | setBoolean(LockPatternUtils.DISABLE_LOCKSCREEN_KEY, false, id); |
| 868 | } |
Greg Plesur | eb2e453 | 2016-11-02 17:10:27 -0400 | [diff] [blame] | 869 | } |
Greg Plesur | eb2e453 | 2016-11-02 17:10:27 -0400 | [diff] [blame] | 870 | } |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 871 | |
| 872 | setString("migrated_lockscreen_disabled", "true", 0); |
| 873 | Slog.i(TAG, "Migrated lockscreen disabled flag"); |
| 874 | } |
| 875 | |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 876 | boolean isWatch = mContext.getPackageManager().hasSystemFeature( |
| 877 | PackageManager.FEATURE_WATCH); |
| 878 | // Wear used to set DISABLE_LOCKSCREEN to 'true', but because Wear now allows accounts |
| 879 | // and device management the lockscreen must be re-enabled now for users that upgrade. |
| 880 | if (isWatch && getString("migrated_wear_lockscreen_disabled", null, 0) == null) { |
Rubin Xu | 7b76f33 | 2019-11-08 12:10:12 +0000 | [diff] [blame] | 881 | final List<UserInfo> users = mUserManager.getUsers(); |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 882 | final int userCount = users.size(); |
| 883 | for (int i = 0; i < userCount; i++) { |
| 884 | int id = users.get(i).id; |
| 885 | setBoolean(LockPatternUtils.DISABLE_LOCKSCREEN_KEY, false, id); |
| 886 | } |
| 887 | setString("migrated_wear_lockscreen_disabled", "true", 0); |
| 888 | Slog.i(TAG, "Migrated lockscreen_disabled for Wear devices"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
Adrian Roos | 60dcbbf | 2017-08-08 16:19:33 +0200 | [diff] [blame] | 892 | private void migrateOldDataAfterSystemReady() { |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 893 | // Migrate the FRP credential to the persistent data block |
| 894 | if (LockPatternUtils.frpCredentialEnabled(mContext) |
| 895 | && !getBoolean("migrated_frp", false, 0)) { |
| 896 | migrateFrpCredential(); |
| 897 | setBoolean("migrated_frp", true, 0); |
| 898 | Slog.i(TAG, "Migrated migrated_frp."); |
Adrian Roos | 60dcbbf | 2017-08-08 16:19:33 +0200 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * Migrate the credential for the FRP credential owner user if the following are satisfied: |
| 904 | * - the user has a secure credential |
| 905 | * - the FRP credential is not set up |
| 906 | * - the credential is based on a synthetic password. |
| 907 | */ |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 908 | private void migrateFrpCredential() { |
Adrian Roos | 60dcbbf | 2017-08-08 16:19:33 +0200 | [diff] [blame] | 909 | if (mStorage.readPersistentDataBlock() != PersistentData.NONE) { |
| 910 | return; |
| 911 | } |
| 912 | for (UserInfo userInfo : mUserManager.getUsers()) { |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 913 | if (userOwnsFrpCredential(mContext, userInfo) && isUserSecure(userInfo.id)) { |
Adrian Roos | 60dcbbf | 2017-08-08 16:19:33 +0200 | [diff] [blame] | 914 | synchronized (mSpManager) { |
| 915 | if (isSyntheticPasswordBasedCredentialLocked(userInfo.id)) { |
| 916 | int actualQuality = (int) getLong(LockPatternUtils.PASSWORD_TYPE_KEY, |
| 917 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, userInfo.id); |
| 918 | |
| 919 | mSpManager.migrateFrpPasswordLocked( |
| 920 | getSyntheticPasswordHandleLocked(userInfo.id), |
| 921 | userInfo, |
| 922 | redactActualQualityToMostLenientEquivalentQuality(actualQuality)); |
| 923 | } |
| 924 | } |
| 925 | return; |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | /** |
| 931 | * Returns the lowest password quality that still presents the same UI for entering it. |
| 932 | * |
| 933 | * For the FRP credential, we do not want to leak the actual quality of the password, only what |
| 934 | * kind of UI it requires. However, when migrating, we only know the actual quality, not the |
| 935 | * originally requested quality; since this is only used to determine what input variant to |
| 936 | * present to the user, we just assume the lowest possible quality was requested. |
| 937 | */ |
| 938 | private int redactActualQualityToMostLenientEquivalentQuality(int quality) { |
| 939 | switch (quality) { |
| 940 | case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC: |
| 941 | case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC: |
| 942 | case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX: |
| 943 | return DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC; |
| 944 | case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC: |
| 945 | case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX: |
| 946 | return DevicePolicyManager.PASSWORD_QUALITY_NUMERIC; |
| 947 | case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED: |
| 948 | case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING: |
| 949 | case DevicePolicyManager.PASSWORD_QUALITY_MANAGED: |
| 950 | case DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK: |
| 951 | default: |
| 952 | return quality; |
| 953 | } |
| 954 | } |
| 955 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 956 | private final void checkWritePermission(int userId) { |
Jim Miller | 505329b | 2013-11-08 13:25:36 -0800 | [diff] [blame] | 957 | mContext.enforceCallingOrSelfPermission(PERMISSION, "LockSettingsWrite"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 960 | private final void checkPasswordReadPermission(int userId) { |
Jim Miller | 505329b | 2013-11-08 13:25:36 -0800 | [diff] [blame] | 961 | mContext.enforceCallingOrSelfPermission(PERMISSION, "LockSettingsRead"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Adrian Roos | b953e18 | 2017-08-17 17:58:26 +0200 | [diff] [blame] | 964 | private final void checkPasswordHavePermission(int userId) { |
| 965 | if (mContext.checkCallingOrSelfPermission(PERMISSION) != PERMISSION_GRANTED) { |
| 966 | EventLog.writeEvent(0x534e4554, "28251513", getCallingUid(), ""); // SafetyNet |
| 967 | } |
| 968 | mContext.enforceCallingOrSelfPermission(PERMISSION, "LockSettingsHave"); |
| 969 | } |
| 970 | |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 971 | private final void checkReadPermission(String requestedKey, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 972 | final int callingUid = Binder.getCallingUid(); |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 973 | |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 974 | for (int i = 0; i < READ_CONTACTS_PROTECTED_SETTINGS.length; i++) { |
| 975 | String key = READ_CONTACTS_PROTECTED_SETTINGS[i]; |
| 976 | if (key.equals(requestedKey) && mContext.checkCallingOrSelfPermission(READ_CONTACTS) |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 977 | != PackageManager.PERMISSION_GRANTED) { |
| 978 | throw new SecurityException("uid=" + callingUid |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 979 | + " needs permission " + READ_CONTACTS + " to read " |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 980 | + requestedKey + " for user " + userId); |
| 981 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 982 | } |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 983 | |
| 984 | for (int i = 0; i < READ_PASSWORD_PROTECTED_SETTINGS.length; i++) { |
| 985 | String key = READ_PASSWORD_PROTECTED_SETTINGS[i]; |
| 986 | if (key.equals(requestedKey) && mContext.checkCallingOrSelfPermission(PERMISSION) |
| 987 | != PackageManager.PERMISSION_GRANTED) { |
| 988 | throw new SecurityException("uid=" + callingUid |
| 989 | + " needs permission " + PERMISSION + " to read " |
| 990 | + requestedKey + " for user " + userId); |
| 991 | } |
| 992 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | @Override |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 996 | public boolean getSeparateProfileChallengeEnabled(int userId) { |
Ricky Wai | 7f405f1 | 2016-05-31 12:05:05 +0100 | [diff] [blame] | 997 | checkReadPermission(SEPARATE_PROFILE_CHALLENGE_KEY, userId); |
Rubin Xu | ba6fef5 | 2019-10-25 11:10:43 +0100 | [diff] [blame] | 998 | return getSeparateProfileChallengeEnabledInternal(userId); |
| 999 | } |
| 1000 | |
| 1001 | private boolean getSeparateProfileChallengeEnabledInternal(int userId) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1002 | synchronized (mSeparateChallengeLock) { |
Rubin Xu | ba6fef5 | 2019-10-25 11:10:43 +0100 | [diff] [blame] | 1003 | return getBooleanUnchecked(SEPARATE_PROFILE_CHALLENGE_KEY, false, userId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | @Override |
| 1008 | public void setSeparateProfileChallengeEnabled(int userId, boolean enabled, |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1009 | LockscreenCredential managedUserPassword) { |
Ricky Wai | 7f405f1 | 2016-05-31 12:05:05 +0100 | [diff] [blame] | 1010 | checkWritePermission(userId); |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 1011 | if (!mLockPatternUtils.hasSecureLockScreen()) { |
| 1012 | throw new UnsupportedOperationException( |
| 1013 | "This operation requires secure lock screen feature."); |
| 1014 | } |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1015 | synchronized (mSeparateChallengeLock) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1016 | setSeparateProfileChallengeEnabledLocked(userId, enabled, managedUserPassword != null |
| 1017 | ? managedUserPassword : LockscreenCredential.createNone()); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1018 | } |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1019 | notifySeparateProfileChallengeChanged(userId); |
| 1020 | } |
| 1021 | |
| 1022 | @GuardedBy("mSeparateChallengeLock") |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1023 | private void setSeparateProfileChallengeEnabledLocked(@UserIdInt int userId, |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1024 | boolean enabled, LockscreenCredential managedUserPassword) { |
Pavel Grafov | f10ae7e | 2018-06-25 12:13:38 +0100 | [diff] [blame] | 1025 | final boolean old = getBoolean(SEPARATE_PROFILE_CHALLENGE_KEY, false, userId); |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1026 | setBoolean(SEPARATE_PROFILE_CHALLENGE_KEY, enabled, userId); |
Pavel Grafov | f10ae7e | 2018-06-25 12:13:38 +0100 | [diff] [blame] | 1027 | try { |
| 1028 | if (enabled) { |
| 1029 | mStorage.removeChildProfileLock(userId); |
| 1030 | removeKeystoreProfileKey(userId); |
| 1031 | } else { |
| 1032 | tieManagedProfileLockIfNecessary(userId, managedUserPassword); |
| 1033 | } |
| 1034 | } catch (IllegalStateException e) { |
| 1035 | setBoolean(SEPARATE_PROFILE_CHALLENGE_KEY, old, userId); |
| 1036 | throw e; |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | private void notifySeparateProfileChallengeChanged(int userId) { |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 1041 | // LSS cannot call into DPM directly, otherwise it will cause deadlock. |
| 1042 | // In this case, calling DPM on a handler thread is OK since DPM doesn't |
| 1043 | // expect reportSeparateProfileChallengeChanged() to happen synchronously. |
| 1044 | mHandler.post(() -> { |
| 1045 | final DevicePolicyManagerInternal dpmi = LocalServices.getService( |
| 1046 | DevicePolicyManagerInternal.class); |
| 1047 | if (dpmi != null) { |
| 1048 | dpmi.reportSeparateProfileChallengeChanged(userId); |
| 1049 | } |
| 1050 | }); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | @Override |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 1054 | public void setBoolean(String key, boolean value, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1055 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1056 | setStringUnchecked(key, userId, value ? "1" : "0"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | @Override |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 1060 | public void setLong(String key, long value, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1061 | checkWritePermission(userId); |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1062 | setLongUnchecked(key, value, userId); |
| 1063 | } |
| 1064 | |
| 1065 | private void setLongUnchecked(String key, long value, int userId) { |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1066 | setStringUnchecked(key, userId, Long.toString(value)); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | @Override |
Rubin Xu | fe35447 | 2017-11-21 12:05:06 +0000 | [diff] [blame] | 1070 | public void setString(String key, String value, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1071 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1072 | setStringUnchecked(key, userId, value); |
| 1073 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1074 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1075 | private void setStringUnchecked(String key, int userId, String value) { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1076 | Preconditions.checkArgument(userId != USER_FRP, "cannot store lock settings for FRP user"); |
| 1077 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1078 | mStorage.writeKeyValue(key, value, userId); |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 1079 | if (ArrayUtils.contains(SETTINGS_TO_BACKUP, key)) { |
| 1080 | BackupManager.dataChanged("com.android.providers.settings"); |
| 1081 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | @Override |
Adrian Roos | 60dcbbf | 2017-08-08 16:19:33 +0200 | [diff] [blame] | 1085 | public boolean getBoolean(String key, boolean defaultValue, int userId) { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 1086 | checkReadPermission(key, userId); |
Rubin Xu | ba6fef5 | 2019-10-25 11:10:43 +0100 | [diff] [blame] | 1087 | return getBooleanUnchecked(key, defaultValue, userId); |
| 1088 | } |
| 1089 | |
| 1090 | private boolean getBooleanUnchecked(String key, boolean defaultValue, int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1091 | String value = getStringUnchecked(key, null, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1092 | return TextUtils.isEmpty(value) ? |
| 1093 | defaultValue : (value.equals("1") || value.equals("true")); |
| 1094 | } |
| 1095 | |
| 1096 | @Override |
Adrian Roos | 60dcbbf | 2017-08-08 16:19:33 +0200 | [diff] [blame] | 1097 | public long getLong(String key, long defaultValue, int userId) { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 1098 | checkReadPermission(key, userId); |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1099 | return getLongUnchecked(key, defaultValue, userId); |
| 1100 | } |
| 1101 | |
| 1102 | private long getLongUnchecked(String key, long defaultValue, int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1103 | String value = getStringUnchecked(key, null, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1104 | return TextUtils.isEmpty(value) ? defaultValue : Long.parseLong(value); |
| 1105 | } |
| 1106 | |
| 1107 | @Override |
Adrian Roos | 60dcbbf | 2017-08-08 16:19:33 +0200 | [diff] [blame] | 1108 | public String getString(String key, String defaultValue, int userId) { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 1109 | checkReadPermission(key, userId); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1110 | return getStringUnchecked(key, defaultValue, userId); |
| 1111 | } |
| 1112 | |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1113 | private String getStringUnchecked(String key, String defaultValue, int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1114 | if (Settings.Secure.LOCK_PATTERN_ENABLED.equals(key)) { |
Adrian Roos | 7811d9f | 2015-07-27 15:10:13 -0700 | [diff] [blame] | 1115 | long ident = Binder.clearCallingIdentity(); |
| 1116 | try { |
| 1117 | return mLockPatternUtils.isLockPatternEnabled(userId) ? "1" : "0"; |
| 1118 | } finally { |
| 1119 | Binder.restoreCallingIdentity(ident); |
| 1120 | } |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1121 | } |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1122 | if (userId == USER_FRP) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1123 | return null; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Bryce Lee | 4614596 | 2015-12-14 14:39:10 -0800 | [diff] [blame] | 1126 | if (LockPatternUtils.LEGACY_LOCK_PATTERN_ENABLED.equals(key)) { |
| 1127 | key = Settings.Secure.LOCK_PATTERN_ENABLED; |
| 1128 | } |
| 1129 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1130 | return mStorage.readKeyValue(key, defaultValue, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1133 | private void setKeyguardStoredQuality(int quality, int userId) { |
| 1134 | if (DEBUG) Slog.d(TAG, "setKeyguardStoredQuality: user=" + userId + " quality=" + quality); |
| 1135 | setLongUnchecked(LockPatternUtils.PASSWORD_TYPE_KEY, quality, userId); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1138 | private int getKeyguardStoredQuality(int userId) { |
| 1139 | return (int) getLongUnchecked(LockPatternUtils.PASSWORD_TYPE_KEY, |
| 1140 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, userId); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Adrian Roos | 4f78845 | 2014-05-22 20:45:59 +0200 | [diff] [blame] | 1143 | @Override |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1144 | public int getCredentialType(int userId) { |
Adrian Roos | b953e18 | 2017-08-17 17:58:26 +0200 | [diff] [blame] | 1145 | checkPasswordHavePermission(userId); |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1146 | return getCredentialTypeInternal(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1149 | // TODO: this is a hot path, can we optimize it? |
| 1150 | /** |
| 1151 | * Returns the credential type of the user, can be one of {@link #CREDENTIAL_TYPE_NONE}, |
| 1152 | * {@link #CREDENTIAL_TYPE_PATTERN}, {@link #CREDENTIAL_TYPE_PIN} and |
| 1153 | * {@link #CREDENTIAL_TYPE_PASSWORD} |
| 1154 | */ |
| 1155 | public int getCredentialTypeInternal(int userId) { |
| 1156 | if (userId == USER_FRP) { |
| 1157 | return getFrpCredentialType(); |
| 1158 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1159 | synchronized (mSpManager) { |
| 1160 | if (isSyntheticPasswordBasedCredentialLocked(userId)) { |
Pavel Grafov | 19a4fb3 | 2019-03-15 12:55:12 +0000 | [diff] [blame] | 1161 | final long handle = getSyntheticPasswordHandleLocked(userId); |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1162 | int rawType = mSpManager.getCredentialType(handle, userId); |
| 1163 | if (rawType != CREDENTIAL_TYPE_PASSWORD_OR_PIN) { |
| 1164 | return rawType; |
| 1165 | } |
| 1166 | return pinOrPasswordQualityToCredentialType(getKeyguardStoredQuality(userId)); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1169 | // Intentional duplication of the getKeyguardStoredQuality() call above since this is a |
| 1170 | // unlikely code path (device with pre-synthetic password credential). We want to skip |
| 1171 | // calling getKeyguardStoredQuality whenever possible. |
| 1172 | final int savedQuality = getKeyguardStoredQuality(userId); |
| 1173 | if (savedQuality == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING |
| 1174 | && mStorage.hasPattern(userId)) { |
| 1175 | return CREDENTIAL_TYPE_PATTERN; |
| 1176 | } |
| 1177 | if (savedQuality >= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC |
| 1178 | && mStorage.hasPassword(userId)) { |
| 1179 | return pinOrPasswordQualityToCredentialType(savedQuality); |
| 1180 | } |
| 1181 | return CREDENTIAL_TYPE_NONE; |
| 1182 | } |
| 1183 | |
| 1184 | private int getFrpCredentialType() { |
| 1185 | PersistentData data = mStorage.readPersistentDataBlock(); |
| 1186 | if (data.type != PersistentData.TYPE_SP && data.type != PersistentData.TYPE_SP_WEAVER) { |
| 1187 | return CREDENTIAL_TYPE_NONE; |
| 1188 | } |
| 1189 | int credentialType = SyntheticPasswordManager.getFrpCredentialType(data.payload); |
| 1190 | if (credentialType != CREDENTIAL_TYPE_PASSWORD_OR_PIN) { |
| 1191 | return credentialType; |
| 1192 | } |
| 1193 | return pinOrPasswordQualityToCredentialType(data.qualityForUi); |
| 1194 | } |
| 1195 | |
| 1196 | private static int pinOrPasswordQualityToCredentialType(int quality) { |
| 1197 | if (LockPatternUtils.isQualityAlphabeticPassword(quality)) { |
| 1198 | return CREDENTIAL_TYPE_PASSWORD; |
| 1199 | } |
| 1200 | if (LockPatternUtils.isQualityNumericPin(quality)) { |
| 1201 | return CREDENTIAL_TYPE_PIN; |
| 1202 | } |
| 1203 | throw new IllegalArgumentException("Quality is neither Pin nor password: " + quality); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1206 | private boolean isUserSecure(int userId) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1207 | return getCredentialTypeInternal(userId) != CREDENTIAL_TYPE_NONE; |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1210 | private void setKeystorePassword(byte[] password, int userHandle) { |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 1211 | final KeyStore ks = KeyStore.getInstance(); |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1212 | // TODO(b/120484642): Update keystore to accept byte[] passwords |
| 1213 | String passwordString = password == null ? null : new String(password); |
| 1214 | ks.onUserPasswordChanged(userHandle, passwordString); |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1217 | private void unlockKeystore(byte[] password, int userHandle) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1218 | if (DEBUG) Slog.v(TAG, "Unlock keystore for user: " + userHandle); |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1219 | // TODO(b/120484642): Update keystore to accept byte[] passwords |
| 1220 | String passwordString = password == null ? null : new String(password); |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 1221 | final KeyStore ks = KeyStore.getInstance(); |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1222 | ks.unlock(userHandle, passwordString); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1223 | } |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 1224 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1225 | @VisibleForTesting /** Note: this method is overridden in unit tests */ |
| 1226 | protected LockscreenCredential getDecryptedPasswordForTiedProfile(int userId) |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1227 | throws KeyStoreException, UnrecoverableKeyException, |
| 1228 | NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, |
| 1229 | InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, |
| 1230 | CertificateException, IOException { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1231 | if (DEBUG) Slog.v(TAG, "Get child profile decrypted key"); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1232 | byte[] storedData = mStorage.readChildProfileLock(userId); |
| 1233 | if (storedData == null) { |
| 1234 | throw new FileNotFoundException("Child profile lock file not found"); |
| 1235 | } |
| 1236 | byte[] iv = Arrays.copyOfRange(storedData, 0, PROFILE_KEY_IV_SIZE); |
| 1237 | byte[] encryptedPassword = Arrays.copyOfRange(storedData, PROFILE_KEY_IV_SIZE, |
| 1238 | storedData.length); |
| 1239 | byte[] decryptionResult; |
| 1240 | java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore"); |
| 1241 | keyStore.load(null); |
| 1242 | SecretKey decryptionKey = (SecretKey) keyStore.getKey( |
Ricky Wai | d398244 | 2016-05-24 19:27:08 +0100 | [diff] [blame] | 1243 | LockPatternUtils.PROFILE_KEY_NAME_DECRYPT + userId, null); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1244 | |
| 1245 | Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" |
| 1246 | + KeyProperties.BLOCK_MODE_GCM + "/" + KeyProperties.ENCRYPTION_PADDING_NONE); |
| 1247 | |
| 1248 | cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new GCMParameterSpec(128, iv)); |
| 1249 | decryptionResult = cipher.doFinal(encryptedPassword); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1250 | LockscreenCredential credential = LockscreenCredential.createManagedPassword( |
| 1251 | decryptionResult); |
| 1252 | Arrays.fill(decryptionResult, (byte) 0); |
| 1253 | return credential; |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Kevin Chyn | 97d0a5b | 2019-06-11 12:52:17 -0700 | [diff] [blame] | 1256 | private void unlockChildProfile(int profileHandle, boolean ignoreUserNotAuthenticated, |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1257 | @ChallengeType int challengeType, long challenge, |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1258 | @Nullable ArrayList<PendingResetLockout> resetLockouts) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1259 | try { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1260 | doVerifyCredential(getDecryptedPasswordForTiedProfile(profileHandle), |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1261 | challengeType, challenge, profileHandle, null /* progressCallback */, |
| 1262 | resetLockouts); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1263 | } catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException |
| 1264 | | NoSuchAlgorithmException | NoSuchPaddingException |
| 1265 | | InvalidAlgorithmParameterException | IllegalBlockSizeException |
| 1266 | | BadPaddingException | CertificateException | IOException e) { |
| 1267 | if (e instanceof FileNotFoundException) { |
| 1268 | Slog.i(TAG, "Child profile key not found"); |
Pavel Grafov | 0acc4bf | 2017-08-23 12:20:54 +0100 | [diff] [blame] | 1269 | } else if (ignoreUserNotAuthenticated && e instanceof UserNotAuthenticatedException) { |
| 1270 | Slog.i(TAG, "Parent keystore seems locked, ignoring"); |
Clara Bayarri | 0a587d2 | 2016-02-23 14:49:41 -0800 | [diff] [blame] | 1271 | } else { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1272 | Slog.e(TAG, "Failed to decrypt child profile key", e); |
Clara Bayarri | 0a587d2 | 2016-02-23 14:49:41 -0800 | [diff] [blame] | 1273 | } |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
Kevin Chyn | 97d0a5b | 2019-06-11 12:52:17 -0700 | [diff] [blame] | 1277 | private void unlockUser(int userId, byte[] token, byte[] secret) { |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1278 | unlockUser(userId, token, secret, CHALLENGE_NONE, 0 /* challenge */, null); |
Kevin Chyn | 97d0a5b | 2019-06-11 12:52:17 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 1281 | /** |
| 1282 | * Unlock the user (both storage and user state) and its associated managed profiles |
| 1283 | * synchronously. |
| 1284 | * |
| 1285 | * <em>Be very careful about the risk of deadlock here: ActivityManager.unlockUser() |
| 1286 | * can end up calling into other system services to process user unlock request (via |
| 1287 | * {@link com.android.server.SystemServiceManager#unlockUser} </em> |
| 1288 | */ |
Kevin Chyn | 97d0a5b | 2019-06-11 12:52:17 -0700 | [diff] [blame] | 1289 | private void unlockUser(int userId, byte[] token, byte[] secret, |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1290 | @ChallengeType int challengeType, long challenge, |
| 1291 | @Nullable ArrayList<PendingResetLockout> resetLockouts) { |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 1292 | // TODO: make this method fully async so we can update UI with progress strings |
Pavel Grafov | b9b2c69 | 2019-05-24 14:07:55 +0100 | [diff] [blame] | 1293 | final boolean alreadyUnlocked = mUserManager.isUserUnlockingOrUnlocked(userId); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 1294 | final CountDownLatch latch = new CountDownLatch(1); |
| 1295 | final IProgressListener listener = new IProgressListener.Stub() { |
| 1296 | @Override |
| 1297 | public void onStarted(int id, Bundle extras) throws RemoteException { |
Rubin Xu | e1beaf0 | 2019-10-22 11:36:51 +0100 | [diff] [blame] | 1298 | Slog.d(TAG, "unlockUser started"); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | @Override |
| 1302 | public void onProgress(int id, int progress, Bundle extras) throws RemoteException { |
Rubin Xu | e1beaf0 | 2019-10-22 11:36:51 +0100 | [diff] [blame] | 1303 | Slog.d(TAG, "unlockUser progress " + progress); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | @Override |
| 1307 | public void onFinished(int id, Bundle extras) throws RemoteException { |
Rubin Xu | e1beaf0 | 2019-10-22 11:36:51 +0100 | [diff] [blame] | 1308 | Slog.d(TAG, "unlockUser finished"); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 1309 | latch.countDown(); |
| 1310 | } |
| 1311 | }; |
| 1312 | |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 1313 | try { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 1314 | mActivityManager.unlockUser(userId, token, secret, listener); |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 1315 | } catch (RemoteException e) { |
| 1316 | throw e.rethrowAsRuntimeException(); |
| 1317 | } |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 1318 | |
| 1319 | try { |
| 1320 | latch.await(15, TimeUnit.SECONDS); |
| 1321 | } catch (InterruptedException e) { |
| 1322 | Thread.currentThread().interrupt(); |
| 1323 | } |
Pavel Grafov | b9b2c69 | 2019-05-24 14:07:55 +0100 | [diff] [blame] | 1324 | |
| 1325 | if (mUserManager.getUserInfo(userId).isManagedProfile()) { |
| 1326 | return; |
| 1327 | } |
| 1328 | |
| 1329 | for (UserInfo profile : mUserManager.getProfiles(userId)) { |
| 1330 | // Unlock managed profile with unified lock |
| 1331 | if (tiedManagedProfileReadyToUnlock(profile)) { |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1332 | // Must pass the challenge on for resetLockout, so it's not over-written, which |
| 1333 | // causes LockSettingsService to revokeChallenge inappropriately. |
| 1334 | unlockChildProfile(profile.id, false /* ignoreUserNotAuthenticated */, |
| 1335 | challengeType, challenge, resetLockouts); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1336 | } |
Pavel Grafov | b9b2c69 | 2019-05-24 14:07:55 +0100 | [diff] [blame] | 1337 | // Now we have unlocked the parent user and attempted to unlock the profile we should |
| 1338 | // show notifications if the profile is still locked. |
| 1339 | if (!alreadyUnlocked) { |
| 1340 | long ident = clearCallingIdentity(); |
| 1341 | try { |
| 1342 | maybeShowEncryptionNotificationForUser(profile.id); |
| 1343 | } finally { |
| 1344 | restoreCallingIdentity(ident); |
| 1345 | } |
| 1346 | } |
| 1347 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1348 | } |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1349 | |
| 1350 | if (resetLockouts != null && !resetLockouts.isEmpty()) { |
| 1351 | mHandler.post(() -> { |
| 1352 | final BiometricManager bm = mContext.getSystemService(BiometricManager.class); |
| 1353 | final PackageManager pm = mContext.getPackageManager(); |
| 1354 | for (int i = 0; i < resetLockouts.size(); i++) { |
Kevin Chyn | 91c70b6 | 2019-06-13 13:20:41 -0700 | [diff] [blame] | 1355 | bm.setActiveUser(resetLockouts.get(i).mUserId); |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1356 | bm.resetLockout(resetLockouts.get(i).mHAT); |
| 1357 | } |
| 1358 | if (challengeType == CHALLENGE_INTERNAL |
| 1359 | && pm.hasSystemFeature(PackageManager.FEATURE_FACE)) { |
| 1360 | mContext.getSystemService(FaceManager.class).revokeChallenge(); |
| 1361 | } |
| 1362 | }); |
| 1363 | } |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 1364 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1365 | |
Pavel Grafov | 0acc4bf | 2017-08-23 12:20:54 +0100 | [diff] [blame] | 1366 | private boolean tiedManagedProfileReadyToUnlock(UserInfo userInfo) { |
| 1367 | return userInfo.isManagedProfile() |
| 1368 | && !mLockPatternUtils.isSeparateProfileChallengeEnabled(userInfo.id) |
| 1369 | && mStorage.hasChildProfileLock(userInfo.id) |
| 1370 | && mUserManager.isUserRunning(userInfo.id); |
| 1371 | } |
| 1372 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1373 | private Map<Integer, LockscreenCredential> getDecryptedPasswordsForAllTiedProfiles(int userId) { |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1374 | if (mUserManager.getUserInfo(userId).isManagedProfile()) { |
| 1375 | return null; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1376 | } |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1377 | Map<Integer, LockscreenCredential> result = new ArrayMap<>(); |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1378 | final List<UserInfo> profiles = mUserManager.getProfiles(userId); |
| 1379 | final int size = profiles.size(); |
| 1380 | for (int i = 0; i < size; i++) { |
| 1381 | final UserInfo profile = profiles.get(i); |
| 1382 | if (!profile.isManagedProfile()) { |
| 1383 | continue; |
| 1384 | } |
| 1385 | final int managedUserId = profile.id; |
| 1386 | if (mLockPatternUtils.isSeparateProfileChallengeEnabled(managedUserId)) { |
| 1387 | continue; |
| 1388 | } |
| 1389 | try { |
Rubin Xu | 4f988c9 | 2017-09-12 16:25:26 +0100 | [diff] [blame] | 1390 | result.put(managedUserId, getDecryptedPasswordForTiedProfile(managedUserId)); |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1391 | } catch (KeyStoreException | UnrecoverableKeyException | NoSuchAlgorithmException |
| 1392 | | NoSuchPaddingException | InvalidKeyException |
| 1393 | | InvalidAlgorithmParameterException | IllegalBlockSizeException |
| 1394 | | BadPaddingException | CertificateException | IOException e) { |
Rubin Xu | 4f988c9 | 2017-09-12 16:25:26 +0100 | [diff] [blame] | 1395 | Slog.e(TAG, "getDecryptedPasswordsForAllTiedProfiles failed for user " + |
| 1396 | managedUserId, e); |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | return result; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1402 | /** |
| 1403 | * Synchronize all profile's work challenge of the given user if it's unified: tie or clear them |
| 1404 | * depending on the parent user's secure state. |
| 1405 | * |
| 1406 | * When clearing tied work challenges, a pre-computed password table for profiles are required, |
| 1407 | * since changing password for profiles requires existing password, and existing passwords can |
| 1408 | * only be computed before the parent user's password is cleared. |
| 1409 | * |
| 1410 | * Strictly this is a recursive function, since setLockCredentialInternal ends up calling this |
| 1411 | * method again on profiles. However the recursion is guaranteed to terminate as this method |
| 1412 | * terminates when the user is a managed profile. |
| 1413 | */ |
| 1414 | private void synchronizeUnifiedWorkChallengeForProfiles(int userId, |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1415 | Map<Integer, LockscreenCredential> profilePasswordMap) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1416 | if (mUserManager.getUserInfo(userId).isManagedProfile()) { |
| 1417 | return; |
| 1418 | } |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1419 | final boolean isSecure = isUserSecure(userId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1420 | final List<UserInfo> profiles = mUserManager.getProfiles(userId); |
| 1421 | final int size = profiles.size(); |
| 1422 | for (int i = 0; i < size; i++) { |
| 1423 | final UserInfo profile = profiles.get(i); |
| 1424 | if (profile.isManagedProfile()) { |
| 1425 | final int managedUserId = profile.id; |
| 1426 | if (mLockPatternUtils.isSeparateProfileChallengeEnabled(managedUserId)) { |
| 1427 | continue; |
| 1428 | } |
| 1429 | if (isSecure) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1430 | tieManagedProfileLockIfNecessary(managedUserId, |
| 1431 | LockscreenCredential.createNone()); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1432 | } else { |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1433 | // We use cached work profile password computed before clearing the parent's |
| 1434 | // credential, otherwise they get lost |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1435 | if (profilePasswordMap != null |
| 1436 | && profilePasswordMap.containsKey(managedUserId)) { |
| 1437 | setLockCredentialInternal(LockscreenCredential.createNone(), |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1438 | profilePasswordMap.get(managedUserId), |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1439 | managedUserId, |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 1440 | false, /* isLockTiedToParent= */ true); |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1441 | } else { |
| 1442 | Slog.wtf(TAG, "clear tied profile challenges, but no password supplied."); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1443 | // Attempt an untrusted reset by supplying an empty credential. |
| 1444 | setLockCredentialInternal(LockscreenCredential.createNone(), |
| 1445 | LockscreenCredential.createNone(), |
| 1446 | managedUserId, |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 1447 | true, /* isLockTiedToParent= */ true); |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1448 | } |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1449 | mStorage.removeChildProfileLock(managedUserId); |
| 1450 | removeKeystoreProfileKey(managedUserId); |
| 1451 | } |
| 1452 | } |
| 1453 | } |
| 1454 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1455 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1456 | private boolean isManagedProfileWithUnifiedLock(int userId) { |
| 1457 | return mUserManager.getUserInfo(userId).isManagedProfile() |
| 1458 | && !mLockPatternUtils.isSeparateProfileChallengeEnabled(userId); |
| 1459 | } |
| 1460 | |
| 1461 | private boolean isManagedProfileWithSeparatedLock(int userId) { |
| 1462 | return mUserManager.getUserInfo(userId).isManagedProfile() |
| 1463 | && mLockPatternUtils.isSeparateProfileChallengeEnabled(userId); |
| 1464 | } |
| 1465 | |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 1466 | /** |
| 1467 | * Send credentials for user {@code userId} to {@link RecoverableKeyStoreManager} during an |
| 1468 | * unlock operation. |
| 1469 | */ |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1470 | private void sendCredentialsOnUnlockIfRequired(LockscreenCredential credential, int userId) { |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 1471 | // Don't send credentials during the factory reset protection flow. |
| 1472 | if (userId == USER_FRP) { |
| 1473 | return; |
| 1474 | } |
| 1475 | |
| 1476 | // A profile with a unified lock screen stores a randomly generated credential, so skip it. |
| 1477 | // Its parent will send credentials for the profile, as it stores the unified lock |
| 1478 | // credential. |
| 1479 | if (isManagedProfileWithUnifiedLock(userId)) { |
| 1480 | return; |
| 1481 | } |
| 1482 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1483 | // RecoverableKeyStoreManager expects null for empty credential. |
| 1484 | final byte[] secret = credential.isNone() ? null : credential.getCredential(); |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 1485 | // Send credentials for the user and any child profiles that share its lock screen. |
| 1486 | for (int profileId : getProfilesWithSameLockScreen(userId)) { |
| 1487 | mRecoverableKeyStoreManager.lockScreenSecretAvailable( |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1488 | credential.getType(), secret, profileId); |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | /** |
| 1493 | * Send credentials for user {@code userId} to {@link RecoverableKeyStoreManager} when its |
| 1494 | * credentials are set/changed. |
| 1495 | */ |
| 1496 | private void sendCredentialsOnChangeIfRequired( |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1497 | LockscreenCredential credential, int userId, boolean isLockTiedToParent) { |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 1498 | // A profile whose lock screen is being tied to its parent's will either have a randomly |
| 1499 | // generated credential (creation) or null (removal). We rely on the parent to send its |
| 1500 | // credentials for the profile in both cases as it stores the unified lock credential. |
| 1501 | if (isLockTiedToParent) { |
| 1502 | return; |
| 1503 | } |
| 1504 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1505 | // RecoverableKeyStoreManager expects null for empty credential. |
| 1506 | final byte[] secret = credential.isNone() ? null : credential.getCredential(); |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 1507 | // Send credentials for the user and any child profiles that share its lock screen. |
| 1508 | for (int profileId : getProfilesWithSameLockScreen(userId)) { |
| 1509 | mRecoverableKeyStoreManager.lockScreenSecretChanged( |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1510 | credential.getType(), secret, profileId); |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | /** |
| 1515 | * Returns all profiles of {@code userId}, including itself, that have the same lock screen |
| 1516 | * challenge. |
| 1517 | */ |
| 1518 | private Set<Integer> getProfilesWithSameLockScreen(int userId) { |
| 1519 | Set<Integer> profiles = new ArraySet<>(); |
| 1520 | for (UserInfo profile : mUserManager.getProfiles(userId)) { |
| 1521 | if (profile.id == userId |
| 1522 | || (profile.profileGroupId == userId |
| 1523 | && isManagedProfileWithUnifiedLock(profile.id))) { |
| 1524 | profiles.add(profile.id); |
| 1525 | } |
| 1526 | } |
| 1527 | return profiles; |
| 1528 | } |
| 1529 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1530 | // This method should be called by LockPatternUtil only, all internal methods in this class |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1531 | // should call setLockCredentialInternal. |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1532 | @Override |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1533 | public boolean setLockCredential(LockscreenCredential credential, |
| 1534 | LockscreenCredential savedCredential, int userId, boolean allowUntrustedChange) { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1535 | |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 1536 | if (!mLockPatternUtils.hasSecureLockScreen()) { |
| 1537 | throw new UnsupportedOperationException( |
| 1538 | "This operation requires secure lock screen feature"); |
| 1539 | } |
Jim Miller | e484eaf | 2016-04-13 16:35:36 -0700 | [diff] [blame] | 1540 | checkWritePermission(userId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1541 | synchronized (mSeparateChallengeLock) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1542 | if (!setLockCredentialInternal(credential, savedCredential, |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 1543 | userId, allowUntrustedChange, /* isLockTiedToParent= */ false)) { |
| 1544 | return false; |
| 1545 | } |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1546 | setSeparateProfileChallengeEnabledLocked(userId, true, /* unused */ null); |
Andrew Scull | 5daf273 | 2016-11-14 15:02:45 +0000 | [diff] [blame] | 1547 | notifyPasswordChanged(userId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1548 | } |
Pavel Grafov | a8da697 | 2019-04-03 13:58:58 +0100 | [diff] [blame] | 1549 | if (mUserManager.getUserInfo(userId).isManagedProfile()) { |
| 1550 | // Make sure the profile doesn't get locked straight after setting work challenge. |
| 1551 | setDeviceUnlockedForUser(userId); |
| 1552 | } |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1553 | notifySeparateProfileChallengeChanged(userId); |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 1554 | return true; |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 1557 | /** |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1558 | * @param savedCredential if the user is a managed profile with unified challenge and |
| 1559 | * savedCredential is empty, LSS will try to re-derive the profile password internally. |
| 1560 | * TODO (b/80170828): Fix this so profile password is always passed in. |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 1561 | * @param isLockTiedToParent is {@code true} if {@code userId} is a profile and its new |
| 1562 | * credentials are being tied to its parent's credentials. |
| 1563 | */ |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1564 | private boolean setLockCredentialInternal(LockscreenCredential credential, |
| 1565 | LockscreenCredential savedCredential, int userId, |
| 1566 | boolean allowUntrustedChange, boolean isLockTiedToParent) { |
| 1567 | Preconditions.checkNotNull(credential); |
| 1568 | Preconditions.checkNotNull(savedCredential); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1569 | synchronized (mSpManager) { |
| 1570 | if (isSyntheticPasswordBasedCredentialLocked(userId)) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1571 | return spBasedSetLockCredentialInternalLocked(credential, savedCredential, userId, |
| 1572 | allowUntrustedChange, isLockTiedToParent); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1573 | } |
| 1574 | } |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1575 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1576 | if (credential.isNone()) { |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 1577 | clearUserKeyProtection(userId); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1578 | gateKeeperClearSecureUserId(userId); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1579 | mStorage.writeCredentialHash(CredentialHash.createEmptyHash(), userId); |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1580 | // Still update PASSWORD_TYPE_KEY if we are running in pre-synthetic password code path, |
| 1581 | // since it forms part of the state that determines the credential type |
| 1582 | // @see getCredentialTypeInternal |
| 1583 | setKeyguardStoredQuality(DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, userId); |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 1584 | setKeystorePassword(null, userId); |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 1585 | fixateNewestUserKeyAuth(userId); |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1586 | synchronizeUnifiedWorkChallengeForProfiles(userId, null); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1587 | setUserPasswordMetrics(LockscreenCredential.createNone(), userId); |
| 1588 | sendCredentialsOnChangeIfRequired(credential, userId, isLockTiedToParent); |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 1589 | return true; |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1590 | } |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1591 | |
| 1592 | CredentialHash currentHandle = mStorage.readCredentialHash(userId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1593 | if (isManagedProfileWithUnifiedLock(userId)) { |
| 1594 | // get credential from keystore when managed profile has unified lock |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1595 | if (savedCredential.isNone()) { |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1596 | try { |
| 1597 | savedCredential = getDecryptedPasswordForTiedProfile(userId); |
| 1598 | } catch (FileNotFoundException e) { |
| 1599 | Slog.i(TAG, "Child profile key not found"); |
| 1600 | } catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException |
| 1601 | | NoSuchAlgorithmException | NoSuchPaddingException |
| 1602 | | InvalidAlgorithmParameterException | IllegalBlockSizeException |
| 1603 | | BadPaddingException | CertificateException | IOException e) { |
| 1604 | Slog.e(TAG, "Failed to decrypt child profile key", e); |
| 1605 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1606 | } |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1607 | } else { |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1608 | if (currentHandle.hash == null) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1609 | if (!savedCredential.isNone()) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1610 | Slog.w(TAG, "Saved credential provided, but none stored"); |
| 1611 | } |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1612 | savedCredential.close(); |
| 1613 | savedCredential = LockscreenCredential.createNone(); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1614 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1615 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1616 | synchronized (mSpManager) { |
| 1617 | if (shouldMigrateToSyntheticPasswordLocked(userId)) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1618 | initializeSyntheticPasswordLocked(currentHandle.hash, savedCredential, userId); |
| 1619 | return spBasedSetLockCredentialInternalLocked(credential, savedCredential, userId, |
| 1620 | allowUntrustedChange, isLockTiedToParent); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | if (DEBUG) Slog.d(TAG, "setLockCredentialInternal: user=" + userId); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1624 | byte[] enrolledHandle = enrollCredential(currentHandle.hash, |
| 1625 | savedCredential.getCredential(), credential.getCredential(), userId); |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 1626 | if (enrolledHandle == null) { |
| 1627 | Slog.w(TAG, String.format("Failed to enroll %s: incorrect credential", |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1628 | credential.isPattern() ? "pattern" : "password")); |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 1629 | return false; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1630 | } |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1631 | CredentialHash willStore = CredentialHash.create(enrolledHandle, credential.getType()); |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 1632 | mStorage.writeCredentialHash(willStore, userId); |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1633 | // Still update PASSWORD_TYPE_KEY if we are running in pre-synthetic password code path, |
| 1634 | // since it forms part of the state that determines the credential type |
| 1635 | // @see getCredentialTypeInternal |
| 1636 | setKeyguardStoredQuality( |
| 1637 | LockPatternUtils.credentialTypeToPasswordQuality(credential.getType()), userId); |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 1638 | // push new secret and auth token to vold |
| 1639 | GateKeeperResponse gkResponse; |
| 1640 | try { |
| 1641 | gkResponse = getGateKeeperService().verifyChallenge(userId, 0, willStore.hash, |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1642 | credential.getCredential()); |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 1643 | } catch (RemoteException e) { |
| 1644 | throw new IllegalStateException("Failed to verify current credential", e); |
| 1645 | } |
| 1646 | setUserKeyProtection(userId, credential, convertResponse(gkResponse)); |
| 1647 | fixateNewestUserKeyAuth(userId); |
| 1648 | // Refresh the auth token |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1649 | doVerifyCredential(credential, CHALLENGE_FROM_CALLER, 0, userId, |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 1650 | null /* progressCallback */); |
| 1651 | synchronizeUnifiedWorkChallengeForProfiles(userId, null); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1652 | sendCredentialsOnChangeIfRequired(credential, userId, isLockTiedToParent); |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 1653 | return true; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1654 | } |
| 1655 | |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1656 | private VerifyCredentialResponse convertResponse(GateKeeperResponse gateKeeperResponse) { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1657 | return VerifyCredentialResponse.fromGateKeeperResponse(gateKeeperResponse); |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1660 | @VisibleForTesting /** Note: this method is overridden in unit tests */ |
| 1661 | protected void tieProfileLockToParent(int userId, LockscreenCredential password) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1662 | if (DEBUG) Slog.v(TAG, "tieProfileLockToParent for user: " + userId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1663 | byte[] encryptionResult; |
| 1664 | byte[] iv; |
| 1665 | try { |
| 1666 | KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES); |
| 1667 | keyGenerator.init(new SecureRandom()); |
| 1668 | SecretKey secretKey = keyGenerator.generateKey(); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1669 | java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore"); |
| 1670 | keyStore.load(null); |
Ricky Wai | 97c8f8d | 2016-07-13 17:57:45 +0100 | [diff] [blame] | 1671 | try { |
| 1672 | keyStore.setEntry( |
| 1673 | LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT + userId, |
| 1674 | new java.security.KeyStore.SecretKeyEntry(secretKey), |
| 1675 | new KeyProtection.Builder(KeyProperties.PURPOSE_ENCRYPT) |
| 1676 | .setBlockModes(KeyProperties.BLOCK_MODE_GCM) |
| 1677 | .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) |
| 1678 | .build()); |
| 1679 | keyStore.setEntry( |
| 1680 | LockPatternUtils.PROFILE_KEY_NAME_DECRYPT + userId, |
| 1681 | new java.security.KeyStore.SecretKeyEntry(secretKey), |
| 1682 | new KeyProtection.Builder(KeyProperties.PURPOSE_DECRYPT) |
| 1683 | .setBlockModes(KeyProperties.BLOCK_MODE_GCM) |
| 1684 | .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) |
| 1685 | .setUserAuthenticationRequired(true) |
| 1686 | .setUserAuthenticationValidityDurationSeconds(30) |
Pavel Grafov | e053c1e | 2017-08-08 16:53:32 +0100 | [diff] [blame] | 1687 | .setCriticalToDeviceEncryption(true) |
Ricky Wai | 97c8f8d | 2016-07-13 17:57:45 +0100 | [diff] [blame] | 1688 | .build()); |
| 1689 | // Key imported, obtain a reference to it. |
| 1690 | SecretKey keyStoreEncryptionKey = (SecretKey) keyStore.getKey( |
| 1691 | LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT + userId, null); |
| 1692 | Cipher cipher = Cipher.getInstance( |
| 1693 | KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_GCM + "/" |
| 1694 | + KeyProperties.ENCRYPTION_PADDING_NONE); |
| 1695 | cipher.init(Cipher.ENCRYPT_MODE, keyStoreEncryptionKey); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1696 | encryptionResult = cipher.doFinal(password.getCredential()); |
Ricky Wai | 97c8f8d | 2016-07-13 17:57:45 +0100 | [diff] [blame] | 1697 | iv = cipher.getIV(); |
| 1698 | } finally { |
| 1699 | // The original key can now be discarded. |
| 1700 | keyStore.deleteEntry(LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT + userId); |
| 1701 | } |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1702 | } catch (CertificateException | UnrecoverableKeyException |
Zach Jang | e61672a | 2016-11-22 17:47:18 +0000 | [diff] [blame] | 1703 | | IOException | BadPaddingException | IllegalBlockSizeException | KeyStoreException |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1704 | | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException e) { |
Rubin Xu | 0f9c2ff | 2019-08-14 16:25:57 +0100 | [diff] [blame] | 1705 | throw new IllegalStateException("Failed to encrypt key", e); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1706 | } |
| 1707 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 1708 | try { |
| 1709 | if (iv.length != PROFILE_KEY_IV_SIZE) { |
Rubin Xu | 0f9c2ff | 2019-08-14 16:25:57 +0100 | [diff] [blame] | 1710 | throw new IllegalArgumentException("Invalid iv length: " + iv.length); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1711 | } |
| 1712 | outputStream.write(iv); |
| 1713 | outputStream.write(encryptionResult); |
| 1714 | } catch (IOException e) { |
Rubin Xu | 0f9c2ff | 2019-08-14 16:25:57 +0100 | [diff] [blame] | 1715 | throw new IllegalStateException("Failed to concatenate byte arrays", e); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 1716 | } |
| 1717 | mStorage.writeChildProfileLock(userId, outputStream.toByteArray()); |
| 1718 | } |
| 1719 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1720 | private byte[] enrollCredential(byte[] enrolledHandle, |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1721 | byte[] enrolledCredential, byte[] toEnroll, int userId) { |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 1722 | checkWritePermission(userId); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1723 | GateKeeperResponse response; |
| 1724 | try { |
| 1725 | response = getGateKeeperService().enroll(userId, enrolledHandle, |
| 1726 | enrolledCredential, toEnroll); |
| 1727 | } catch (RemoteException e) { |
| 1728 | Slog.e(TAG, "Failed to enroll credential", e); |
| 1729 | return null; |
| 1730 | } |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 1731 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1732 | if (response == null) { |
| 1733 | return null; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1734 | } |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 1735 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1736 | byte[] hash = response.getPayload(); |
| 1737 | if (hash != null) { |
| 1738 | setKeystorePassword(toEnroll, userId); |
| 1739 | } else { |
| 1740 | // Should not happen |
| 1741 | Slog.e(TAG, "Throttled while enrolling a password"); |
| 1742 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 1743 | return hash; |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1746 | private void setAuthlessUserKeyProtection(int userId, byte[] key) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1747 | if (DEBUG) Slog.d(TAG, "setAuthlessUserKeyProtectiond: user=" + userId); |
| 1748 | addUserKeyAuth(userId, null, key); |
| 1749 | } |
| 1750 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1751 | private void setUserKeyProtection(int userId, LockscreenCredential credential, |
| 1752 | VerifyCredentialResponse vcr) { |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1753 | if (DEBUG) Slog.d(TAG, "setUserKeyProtection: user=" + userId); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1754 | if (vcr == null) { |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1755 | throw new IllegalArgumentException("Null response verifying a credential we just set"); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1756 | } |
| 1757 | if (vcr.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK) { |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1758 | throw new IllegalArgumentException("Non-OK response verifying a credential we just set " |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1759 | + vcr.getResponseCode()); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1760 | } |
| 1761 | byte[] token = vcr.getPayload(); |
| 1762 | if (token == null) { |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1763 | throw new IllegalArgumentException("Empty payload verifying a credential we just set"); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1764 | } |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 1765 | addUserKeyAuth(userId, token, secretFromCredential(credential)); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1768 | private void clearUserKeyProtection(int userId) { |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1769 | if (DEBUG) Slog.d(TAG, "clearUserKeyProtection user=" + userId); |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 1770 | addUserKeyAuth(userId, null, null); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1773 | private static byte[] secretFromCredential(LockscreenCredential credential) { |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1774 | try { |
| 1775 | MessageDigest digest = MessageDigest.getInstance("SHA-512"); |
| 1776 | // Personalize the hash |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1777 | byte[] personalization = "Android FBE credential hash".getBytes(); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1778 | // Pad it to the block size of the hash function |
| 1779 | personalization = Arrays.copyOf(personalization, 128); |
| 1780 | digest.update(personalization); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1781 | digest.update(credential.getCredential()); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1782 | return digest.digest(); |
| 1783 | } catch (NoSuchAlgorithmException e) { |
Rubin Xu | 0f9c2ff | 2019-08-14 16:25:57 +0100 | [diff] [blame] | 1784 | throw new IllegalStateException("NoSuchAlgorithmException for SHA-512"); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1785 | } |
| 1786 | } |
| 1787 | |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 1788 | private boolean isUserKeyUnlocked(int userId) { |
| 1789 | try { |
| 1790 | return mStorageManager.isUserKeyUnlocked(userId); |
| 1791 | } catch (RemoteException e) { |
Rubin Xu | e1beaf0 | 2019-10-22 11:36:51 +0100 | [diff] [blame] | 1792 | Slog.e(TAG, "failed to check user key locked state", e); |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 1793 | return false; |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | /** Unlock disk encryption */ |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1798 | private void unlockUserKey(int userId, byte[] token, byte[] secret) { |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 1799 | final UserInfo userInfo = mUserManager.getUserInfo(userId); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1800 | try { |
| 1801 | mStorageManager.unlockUserKey(userId, userInfo.serialNumber, token, secret); |
| 1802 | } catch (RemoteException e) { |
| 1803 | throw new IllegalStateException("Failed to unlock user key " + userId, e); |
| 1804 | |
| 1805 | } |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1808 | private void addUserKeyAuth(int userId, byte[] token, byte[] secret) { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 1809 | final UserInfo userInfo = mUserManager.getUserInfo(userId); |
Paul Crowley | 815036f | 2016-03-29 14:14:48 -0700 | [diff] [blame] | 1810 | final long callingId = Binder.clearCallingIdentity(); |
| 1811 | try { |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 1812 | mStorageManager.addUserKeyAuth(userId, userInfo.serialNumber, token, secret); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1813 | } catch (RemoteException e) { |
| 1814 | throw new IllegalStateException("Failed to add new key to vold " + userId, e); |
Paul Crowley | 815036f | 2016-03-29 14:14:48 -0700 | [diff] [blame] | 1815 | } finally { |
| 1816 | Binder.restoreCallingIdentity(callingId); |
| 1817 | } |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1820 | private void fixateNewestUserKeyAuth(int userId) { |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 1821 | if (DEBUG) Slog.d(TAG, "fixateNewestUserKeyAuth: user=" + userId); |
Toni Barzic | 6c81872 | 2016-05-27 09:18:39 -0700 | [diff] [blame] | 1822 | final long callingId = Binder.clearCallingIdentity(); |
| 1823 | try { |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 1824 | mStorageManager.fixateNewestUserKeyAuth(userId); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1825 | } catch (RemoteException e) { |
| 1826 | // OK to ignore the exception as vold would just accept both old and new |
| 1827 | // keys if this call fails, and will fix itself during the next boot |
| 1828 | Slog.w(TAG, "fixateNewestUserKeyAuth failed", e); |
Toni Barzic | 6c81872 | 2016-05-27 09:18:39 -0700 | [diff] [blame] | 1829 | } finally { |
| 1830 | Binder.restoreCallingIdentity(callingId); |
| 1831 | } |
Paul Crowley | cc70155 | 2016-05-17 14:18:49 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 1834 | @Override |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1835 | public void resetKeyStore(int userId) { |
Ricky Wai | 7f405f1 | 2016-05-31 12:05:05 +0100 | [diff] [blame] | 1836 | checkWritePermission(userId); |
Ricky Wai | 4613fe4 | 2016-05-24 11:11:42 +0100 | [diff] [blame] | 1837 | if (DEBUG) Slog.v(TAG, "Reset keystore for user: " + userId); |
| 1838 | int managedUserId = -1; |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1839 | LockscreenCredential managedUserDecryptedPassword = null; |
Ricky Wai | 4613fe4 | 2016-05-24 11:11:42 +0100 | [diff] [blame] | 1840 | final List<UserInfo> profiles = mUserManager.getProfiles(userId); |
| 1841 | for (UserInfo pi : profiles) { |
| 1842 | // Unlock managed profile with unified lock |
| 1843 | if (pi.isManagedProfile() |
| 1844 | && !mLockPatternUtils.isSeparateProfileChallengeEnabled(pi.id) |
| 1845 | && mStorage.hasChildProfileLock(pi.id)) { |
| 1846 | try { |
| 1847 | if (managedUserId == -1) { |
| 1848 | managedUserDecryptedPassword = getDecryptedPasswordForTiedProfile(pi.id); |
| 1849 | managedUserId = pi.id; |
| 1850 | } else { |
| 1851 | // Should not happen |
| 1852 | Slog.e(TAG, "More than one managed profile, uid1:" + managedUserId |
| 1853 | + ", uid2:" + pi.id); |
| 1854 | } |
| 1855 | } catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException |
| 1856 | | NoSuchAlgorithmException | NoSuchPaddingException |
| 1857 | | InvalidAlgorithmParameterException | IllegalBlockSizeException |
| 1858 | | BadPaddingException | CertificateException | IOException e) { |
| 1859 | Slog.e(TAG, "Failed to decrypt child profile key", e); |
| 1860 | } |
| 1861 | } |
| 1862 | } |
| 1863 | try { |
| 1864 | // Clear all the users credentials could have been installed in for this user. |
| 1865 | for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) { |
| 1866 | for (int uid : SYSTEM_CREDENTIAL_UIDS) { |
| 1867 | mKeyStore.clearUid(UserHandle.getUid(profileId, uid)); |
| 1868 | } |
| 1869 | } |
| 1870 | } finally { |
| 1871 | if (managedUserId != -1 && managedUserDecryptedPassword != null) { |
| 1872 | if (DEBUG) Slog.v(TAG, "Restore tied profile lock"); |
Zach Jang | e61672a | 2016-11-22 17:47:18 +0000 | [diff] [blame] | 1873 | tieProfileLockToParent(managedUserId, managedUserDecryptedPassword); |
Ricky Wai | 4613fe4 | 2016-05-24 11:11:42 +0100 | [diff] [blame] | 1874 | } |
| 1875 | } |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1876 | if (managedUserDecryptedPassword != null) { |
| 1877 | managedUserDecryptedPassword.zeroize(); |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1878 | } |
Ricky Wai | 4613fe4 | 2016-05-24 11:11:42 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | @Override |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1882 | public VerifyCredentialResponse checkCredential(LockscreenCredential credential, int userId, |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1883 | ICheckCredentialProgressCallback progressCallback) { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1884 | checkPasswordReadPermission(userId); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1885 | return doVerifyCredential(credential, CHALLENGE_NONE, 0, userId, progressCallback); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1886 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1887 | |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1888 | @Override |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1889 | public VerifyCredentialResponse verifyCredential(LockscreenCredential credential, |
| 1890 | long challenge, int userId) { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1891 | checkPasswordReadPermission(userId); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1892 | return doVerifyCredential(credential, CHALLENGE_FROM_CALLER, challenge, userId, |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1893 | null /* progressCallback */); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1896 | private VerifyCredentialResponse doVerifyCredential(LockscreenCredential credential, |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1897 | @ChallengeType int challengeType, long challenge, int userId, |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1898 | ICheckCredentialProgressCallback progressCallback) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1899 | return doVerifyCredential(credential, challengeType, challenge, userId, |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1900 | progressCallback, null /* resetLockouts */); |
| 1901 | } |
| 1902 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1903 | /** |
| 1904 | * Verify user credential and unlock the user. Fix pattern bug by deprecating the old base zero |
| 1905 | * format. |
| 1906 | */ |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1907 | private VerifyCredentialResponse doVerifyCredential(LockscreenCredential credential, |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1908 | @ChallengeType int challengeType, long challenge, int userId, |
| 1909 | ICheckCredentialProgressCallback progressCallback, |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1910 | @Nullable ArrayList<PendingResetLockout> resetLockouts) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1911 | if (credential == null || credential.isNone()) { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1912 | throw new IllegalArgumentException("Credential can't be null or empty"); |
| 1913 | } |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1914 | if (userId == USER_FRP && mInjector.settingsGlobalGetInt(mContext.getContentResolver(), |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1915 | Settings.Global.DEVICE_PROVISIONED, 0) != 0) { |
| 1916 | Slog.e(TAG, "FRP credential can only be verified prior to provisioning."); |
| 1917 | return VerifyCredentialResponse.ERROR; |
| 1918 | } |
Rubin Xu | e94a770 | 2017-06-20 17:29:57 +0100 | [diff] [blame] | 1919 | VerifyCredentialResponse response = null; |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1920 | response = spBasedDoVerifyCredential(credential, challengeType, challenge, |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1921 | userId, progressCallback, resetLockouts); |
Rubin Xu | e94a770 | 2017-06-20 17:29:57 +0100 | [diff] [blame] | 1922 | // The user employs synthetic password based credential. |
| 1923 | if (response != null) { |
Robert Berry | 40386df | 2018-01-22 21:16:58 +0000 | [diff] [blame] | 1924 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1925 | sendCredentialsOnUnlockIfRequired(credential, userId); |
Robert Berry | 40386df | 2018-01-22 21:16:58 +0000 | [diff] [blame] | 1926 | } |
Rubin Xu | e94a770 | 2017-06-20 17:29:57 +0100 | [diff] [blame] | 1927 | return response; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1928 | } |
Rubin Xu | e94a770 | 2017-06-20 17:29:57 +0100 | [diff] [blame] | 1929 | |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1930 | if (userId == USER_FRP) { |
Andrew Scull | 971f294 | 2017-07-12 15:09:45 +0100 | [diff] [blame] | 1931 | Slog.wtf(TAG, "Unexpected FRP credential type, should be SP based."); |
| 1932 | return VerifyCredentialResponse.ERROR; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1933 | } |
| 1934 | |
Andrew Scull | 971f294 | 2017-07-12 15:09:45 +0100 | [diff] [blame] | 1935 | final CredentialHash storedHash = mStorage.readCredentialHash(userId); |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1936 | if (!credential.checkAgainstStoredType(storedHash.type)) { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1937 | Slog.wtf(TAG, "doVerifyCredential type mismatch with stored credential??" |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1938 | + " stored: " + storedHash.type + " passed in: " + credential.getType()); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1939 | return VerifyCredentialResponse.ERROR; |
| 1940 | } |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 1941 | |
Rubin Xu | 7959e2a | 2019-07-31 11:26:53 +0100 | [diff] [blame] | 1942 | response = verifyCredential(userId, storedHash, credential, |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1943 | challengeType, challenge, progressCallback); |
Andres Morales | 59ef126 | 2015-06-26 13:56:39 -0700 | [diff] [blame] | 1944 | |
Michal Karpinski | e71f583 | 2017-01-13 18:18:49 +0000 | [diff] [blame] | 1945 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 1946 | mStrongAuth.reportSuccessfulStrongAuthUnlock(userId); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1947 | } |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 1948 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1949 | return response; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | @Override |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1953 | public VerifyCredentialResponse verifyTiedProfileChallenge(LockscreenCredential credential, |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1954 | long challenge, int userId) { |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 1955 | checkPasswordReadPermission(userId); |
| 1956 | if (!isManagedProfileWithUnifiedLock(userId)) { |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1957 | throw new IllegalArgumentException("User id must be managed profile with unified lock"); |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 1958 | } |
| 1959 | final int parentProfileId = mUserManager.getProfileParent(userId).id; |
| 1960 | // Unlock parent by using parent's challenge |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1961 | final VerifyCredentialResponse parentResponse = doVerifyCredential( |
| 1962 | credential, |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1963 | CHALLENGE_FROM_CALLER, |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1964 | challenge, |
| 1965 | parentProfileId, |
| 1966 | null /* progressCallback */); |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 1967 | if (parentResponse.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK) { |
| 1968 | // Failed, just return parent's response |
| 1969 | return parentResponse; |
| 1970 | } |
| 1971 | |
| 1972 | try { |
| 1973 | // Unlock work profile, and work profile with unified lock must use password only |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1974 | return doVerifyCredential(getDecryptedPasswordForTiedProfile(userId), |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 1975 | CHALLENGE_FROM_CALLER, |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 1976 | challenge, |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 1977 | userId, null /* progressCallback */); |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 1978 | } catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException |
| 1979 | | NoSuchAlgorithmException | NoSuchPaddingException |
| 1980 | | InvalidAlgorithmParameterException | IllegalBlockSizeException |
| 1981 | | BadPaddingException | CertificateException | IOException e) { |
| 1982 | Slog.e(TAG, "Failed to decrypt child profile key", e); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1983 | throw new IllegalStateException("Unable to get tied profile token"); |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 1984 | } |
| 1985 | } |
| 1986 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 1987 | /** |
| 1988 | * Lowest-level credential verification routine that talks to GateKeeper. If verification |
| 1989 | * passes, unlock the corresponding user and keystore. Also handles the migration from legacy |
| 1990 | * hash to GK. |
| 1991 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1992 | private VerifyCredentialResponse verifyCredential(int userId, CredentialHash storedHash, |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1993 | LockscreenCredential credential, @ChallengeType int challengeType, long challenge, |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 1994 | ICheckCredentialProgressCallback progressCallback) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1995 | if ((storedHash == null || storedHash.hash.length == 0) && credential.isNone()) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1996 | // don't need to pass empty credentials to GateKeeper |
| 1997 | return VerifyCredentialResponse.OK; |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 1998 | } |
| 1999 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2000 | if (storedHash == null || storedHash.hash.length == 0 || credential.isNone()) { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 2001 | return VerifyCredentialResponse.ERROR; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 2002 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 2003 | |
Jeff Sharkey | eddf518 | 2016-08-09 16:36:08 -0600 | [diff] [blame] | 2004 | // We're potentially going to be doing a bunch of disk I/O below as part |
| 2005 | // of unlocking the user, so yell if calling from the main thread. |
| 2006 | StrictMode.noteDiskRead(); |
| 2007 | |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2008 | GateKeeperResponse gateKeeperResponse; |
| 2009 | try { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2010 | gateKeeperResponse = getGateKeeperService().verifyChallenge( |
| 2011 | userId, challenge, storedHash.hash, credential.getCredential()); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2012 | } catch (RemoteException e) { |
| 2013 | Slog.e(TAG, "gatekeeper verify failed", e); |
| 2014 | gateKeeperResponse = GateKeeperResponse.ERROR; |
| 2015 | } |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 2016 | VerifyCredentialResponse response = convertResponse(gateKeeperResponse); |
| 2017 | boolean shouldReEnroll = gateKeeperResponse.getShouldReEnroll(); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 2018 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 2019 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
Jorim Jaggi | e31f6b8 | 2016-07-01 16:15:09 -0700 | [diff] [blame] | 2020 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 2021 | // credential has matched |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 2022 | |
| 2023 | if (progressCallback != null) { |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2024 | try { |
| 2025 | progressCallback.onCredentialVerified(); |
| 2026 | } catch (RemoteException e) { |
Rubin Xu | e1beaf0 | 2019-10-22 11:36:51 +0100 | [diff] [blame] | 2027 | Slog.w(TAG, "progressCallback throws exception", e); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2028 | } |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 2029 | } |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2030 | setUserPasswordMetrics(credential, userId); |
| 2031 | unlockKeystore(credential.getCredential(), userId); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 2032 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2033 | Slog.i(TAG, "Unlocking user " + userId + " with token length " |
| 2034 | + response.getPayload().length); |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 2035 | unlockUser(userId, response.getPayload(), secretFromCredential(credential)); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 2036 | |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 2037 | if (isManagedProfileWithSeparatedLock(userId)) { |
Pavel Grafov | a8da697 | 2019-04-03 13:58:58 +0100 | [diff] [blame] | 2038 | setDeviceUnlockedForUser(userId); |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 2039 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 2040 | if (shouldReEnroll) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2041 | setLockCredentialInternal(credential, credential, |
| 2042 | userId, false, /* isLockTiedToParent= */ false); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2043 | } else { |
| 2044 | // Now that we've cleared of all required GK migration, let's do the final |
| 2045 | // migration to synthetic password. |
| 2046 | synchronized (mSpManager) { |
| 2047 | if (shouldMigrateToSyntheticPasswordLocked(userId)) { |
Rubin Xu | 128180b | 2017-04-12 18:02:44 +0100 | [diff] [blame] | 2048 | AuthenticationToken auth = initializeSyntheticPasswordLocked( |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2049 | storedHash.hash, credential, userId); |
Rubin Xu | 128180b | 2017-04-12 18:02:44 +0100 | [diff] [blame] | 2050 | activateEscrowTokens(auth, userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2051 | } |
| 2052 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 2053 | } |
Dmitry Dementyev | 6a509e4 | 2017-12-19 14:47:26 -0800 | [diff] [blame] | 2054 | // Use credentials to create recoverable keystore snapshot. |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2055 | sendCredentialsOnUnlockIfRequired(credential, userId); |
Dmitry Dementyev | 6a509e4 | 2017-12-19 14:47:26 -0800 | [diff] [blame] | 2056 | |
Adrian Roos | 873010d | 2015-08-25 15:59:00 -0700 | [diff] [blame] | 2057 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 2058 | if (response.getTimeout() > 0) { |
| 2059 | requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_LOCKOUT, userId); |
| 2060 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 2061 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 2062 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 2063 | return response; |
| 2064 | } |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 2065 | |
Rubin Xu | 7cf4509 | 2017-08-28 11:47:35 +0100 | [diff] [blame] | 2066 | /** |
Rubin Xu | 1985486 | 2019-08-15 16:37:23 +0100 | [diff] [blame] | 2067 | * Keep track of the given user's latest password metric. This should be called |
| 2068 | * when the user is authenticating or when a new password is being set. In comparison, |
| 2069 | * {@link #notifyPasswordChanged} only needs to be called when the user changes the password. |
Rubin Xu | 7cf4509 | 2017-08-28 11:47:35 +0100 | [diff] [blame] | 2070 | */ |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2071 | private void setUserPasswordMetrics(LockscreenCredential password, @UserIdInt int userHandle) { |
Rubin Xu | 1985486 | 2019-08-15 16:37:23 +0100 | [diff] [blame] | 2072 | synchronized (this) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2073 | mUserPasswordMetrics.put(userHandle, PasswordMetrics.computeForCredential(password)); |
Rubin Xu | 1985486 | 2019-08-15 16:37:23 +0100 | [diff] [blame] | 2074 | } |
| 2075 | } |
Andrew Scull | 5daf273 | 2016-11-14 15:02:45 +0000 | [diff] [blame] | 2076 | |
Rubin Xu | 1985486 | 2019-08-15 16:37:23 +0100 | [diff] [blame] | 2077 | @VisibleForTesting |
| 2078 | PasswordMetrics getUserPasswordMetrics(int userHandle) { |
| 2079 | if (!isUserSecure(userHandle)) { |
| 2080 | // for users without password, mUserPasswordMetrics might not be initialized |
| 2081 | // since the user never unlock the device manually. In this case, always |
| 2082 | // return a default metrics object. This is to distinguish this case from |
| 2083 | // the case where during boot user password is unknown yet (returning null here) |
Pavel Grafov | 6f33484 | 2019-08-06 14:37:06 +0100 | [diff] [blame] | 2084 | return new PasswordMetrics(CREDENTIAL_TYPE_NONE); |
Rubin Xu | 1985486 | 2019-08-15 16:37:23 +0100 | [diff] [blame] | 2085 | } |
| 2086 | synchronized (this) { |
| 2087 | return mUserPasswordMetrics.get(userHandle); |
| 2088 | } |
Andrew Scull | 5daf273 | 2016-11-14 15:02:45 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | /** |
Rubin Xu | 1985486 | 2019-08-15 16:37:23 +0100 | [diff] [blame] | 2092 | * Call after {@link #setUserPasswordMetrics} so metrics are updated before |
Andrew Scull | 5daf273 | 2016-11-14 15:02:45 +0000 | [diff] [blame] | 2093 | * reporting the password changed. |
| 2094 | */ |
| 2095 | private void notifyPasswordChanged(@UserIdInt int userId) { |
Andrew Scull | 5daf273 | 2016-11-14 15:02:45 +0000 | [diff] [blame] | 2096 | mHandler.post(() -> { |
Rubin Xu | 1985486 | 2019-08-15 16:37:23 +0100 | [diff] [blame] | 2097 | mInjector.getDevicePolicyManager().reportPasswordChanged(userId); |
Adrian Roos | 1c8e3c0 | 2018-11-20 20:07:55 +0100 | [diff] [blame] | 2098 | LocalServices.getService(WindowManagerInternal.class).reportPasswordChanged(userId); |
Andrew Scull | 5daf273 | 2016-11-14 15:02:45 +0000 | [diff] [blame] | 2099 | }); |
| 2100 | } |
| 2101 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2102 | private LockscreenCredential createPattern(String patternString) { |
| 2103 | final byte[] patternBytes = patternString.getBytes(); |
| 2104 | LockscreenCredential pattern = LockscreenCredential.createPattern( |
| 2105 | LockPatternUtils.byteArrayToPattern(patternBytes)); |
| 2106 | Arrays.fill(patternBytes, (byte) 0); |
| 2107 | return pattern; |
| 2108 | } |
| 2109 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 2110 | @Override |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2111 | public boolean checkVoldPassword(int userId) { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2112 | if (!mFirstCallToVold) { |
| 2113 | return false; |
| 2114 | } |
| 2115 | mFirstCallToVold = false; |
| 2116 | |
| 2117 | checkPasswordReadPermission(userId); |
| 2118 | |
| 2119 | // There's no guarantee that this will safely connect, but if it fails |
| 2120 | // we will simply show the lock screen when we shouldn't, so relatively |
| 2121 | // benign. There is an outside chance something nasty would happen if |
| 2122 | // this service restarted before vold stales out the password in this |
| 2123 | // case. The nastiness is limited to not showing the lock screen when |
| 2124 | // we should, within the first minute of decrypting the phone if this |
| 2125 | // service can't connect to vold, it restarts, and then the new instance |
| 2126 | // does successfully connect. |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 2127 | final IStorageManager service = mInjector.getStorageManager(); |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 2128 | // TODO(b/120484642): Update vold to return a password as a byte array |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 2129 | String password; |
| 2130 | long identity = Binder.clearCallingIdentity(); |
| 2131 | try { |
| 2132 | password = service.getPassword(); |
| 2133 | service.clearPassword(); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2134 | } catch (RemoteException e) { |
| 2135 | Slog.w(TAG, "vold getPassword() failed", e); |
| 2136 | return false; |
Paul Lawrence | 0bbd108 | 2016-04-26 15:21:02 -0700 | [diff] [blame] | 2137 | } finally { |
| 2138 | Binder.restoreCallingIdentity(identity); |
| 2139 | } |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 2140 | if (TextUtils.isEmpty(password)) { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2141 | return false; |
| 2142 | } |
| 2143 | |
| 2144 | try { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 2145 | final LockscreenCredential credential; |
| 2146 | switch (getCredentialTypeInternal(userId)) { |
| 2147 | case CREDENTIAL_TYPE_PATTERN: |
| 2148 | credential = createPattern(password); |
| 2149 | break; |
| 2150 | case CREDENTIAL_TYPE_PIN: |
| 2151 | credential = LockscreenCredential.createPin(password); |
| 2152 | break; |
| 2153 | case CREDENTIAL_TYPE_PASSWORD: |
| 2154 | credential = LockscreenCredential.createPassword(password); |
| 2155 | break; |
| 2156 | default: |
| 2157 | credential = null; |
| 2158 | Slog.e(TAG, "Unknown credential type"); |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2159 | } |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2160 | |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 2161 | if (credential != null |
| 2162 | && checkCredential(credential, userId, null /* progressCallback */) |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 2163 | .getResponseCode() == GateKeeperResponse.RESPONSE_OK) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 2164 | return true; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2165 | } |
| 2166 | } catch (Exception e) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 2167 | Slog.e(TAG, "checkVoldPassword failed: ", e); |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | return false; |
| 2171 | } |
| 2172 | |
Amith Yamasani | f11a574 | 2016-06-16 08:20:07 -0700 | [diff] [blame] | 2173 | private void removeUser(int userId, boolean unknownUser) { |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 2174 | mSpManager.removeUser(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 2175 | mStorage.removeUser(userId); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 2176 | mStrongAuth.removeUser(userId); |
Andrew Scull | ede482d | 2018-01-30 13:54:29 +0000 | [diff] [blame] | 2177 | tryRemoveUserFromSpCacheLater(userId); |
Robin Lee | 49d810c | 2014-09-23 13:50:22 +0100 | [diff] [blame] | 2178 | |
| 2179 | final KeyStore ks = KeyStore.getInstance(); |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 2180 | ks.onUserRemoved(userId); |
Andres Morales | 070fe63 | 2015-06-24 10:37:10 -0700 | [diff] [blame] | 2181 | |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2182 | gateKeeperClearSecureUserId(userId); |
Irina Dumitrescu | d74fb7c | 2019-03-06 16:42:29 +0000 | [diff] [blame] | 2183 | if (unknownUser || mUserManager.getUserInfo(userId).isManagedProfile()) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 2184 | removeKeystoreProfileKey(userId); |
| 2185 | } |
| 2186 | } |
| 2187 | |
| 2188 | private void removeKeystoreProfileKey(int targetUserId) { |
Rubin Xu | 7b76f33 | 2019-11-08 12:10:12 +0000 | [diff] [blame] | 2189 | Slog.i(TAG, "Remove keystore profile key for user: " + targetUserId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 2190 | try { |
| 2191 | java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore"); |
| 2192 | keyStore.load(null); |
Ricky Wai | d398244 | 2016-05-24 19:27:08 +0100 | [diff] [blame] | 2193 | keyStore.deleteEntry(LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT + targetUserId); |
| 2194 | keyStore.deleteEntry(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT + targetUserId); |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 2195 | } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException |
| 2196 | | IOException e) { |
| 2197 | // We have tried our best to remove all keys |
| 2198 | Slog.e(TAG, "Unable to remove keystore profile key for user:" + targetUserId, e); |
| 2199 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 2200 | } |
| 2201 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 2202 | @Override |
| 2203 | public void registerStrongAuthTracker(IStrongAuthTracker tracker) { |
| 2204 | checkPasswordReadPermission(UserHandle.USER_ALL); |
| 2205 | mStrongAuth.registerStrongAuthTracker(tracker); |
| 2206 | } |
| 2207 | |
| 2208 | @Override |
| 2209 | public void unregisterStrongAuthTracker(IStrongAuthTracker tracker) { |
| 2210 | checkPasswordReadPermission(UserHandle.USER_ALL); |
| 2211 | mStrongAuth.unregisterStrongAuthTracker(tracker); |
| 2212 | } |
| 2213 | |
| 2214 | @Override |
| 2215 | public void requireStrongAuth(int strongAuthReason, int userId) { |
| 2216 | checkWritePermission(userId); |
| 2217 | mStrongAuth.requireStrongAuth(strongAuthReason, userId); |
| 2218 | } |
| 2219 | |
Adrian Roos | 4ab7e59 | 2016-04-13 15:38:13 -0700 | [diff] [blame] | 2220 | @Override |
| 2221 | public void userPresent(int userId) { |
| 2222 | checkWritePermission(userId); |
| 2223 | mStrongAuth.reportUnlock(userId); |
| 2224 | } |
| 2225 | |
Victor Chang | a0940d3 | 2016-05-16 19:36:08 +0100 | [diff] [blame] | 2226 | @Override |
| 2227 | public int getStrongAuthForUser(int userId) { |
| 2228 | checkPasswordReadPermission(userId); |
| 2229 | return mStrongAuthTracker.getStrongAuthForUser(userId); |
| 2230 | } |
| 2231 | |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 2232 | private boolean isCallerShell() { |
| 2233 | final int callingUid = Binder.getCallingUid(); |
| 2234 | return callingUid == Process.SHELL_UID || callingUid == Process.ROOT_UID; |
| 2235 | } |
| 2236 | |
| 2237 | private void enforceShell() { |
| 2238 | if (!isCallerShell()) { |
| 2239 | throw new SecurityException("Caller must be shell"); |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | @Override |
| 2244 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2245 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 2246 | enforceShell(); |
| 2247 | final long origId = Binder.clearCallingIdentity(); |
| 2248 | try { |
Felipe Leme | 1fc9c81 | 2018-07-11 10:02:23 -0700 | [diff] [blame] | 2249 | (new LockSettingsShellCommand(new LockPatternUtils(mContext))).exec( |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 2250 | this, in, out, err, args, callback, resultReceiver); |
| 2251 | } finally { |
| 2252 | Binder.restoreCallingIdentity(origId); |
| 2253 | } |
| 2254 | } |
| 2255 | |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 2256 | @Override |
Bo Zhu | 7f414d9 | 2018-02-28 09:28:19 -0800 | [diff] [blame] | 2257 | public void initRecoveryServiceWithSigFile(@NonNull String rootCertificateAlias, |
| 2258 | @NonNull byte[] recoveryServiceCertFile, @NonNull byte[] recoveryServiceSigFile) |
| 2259 | throws RemoteException { |
| 2260 | mRecoverableKeyStoreManager.initRecoveryServiceWithSigFile(rootCertificateAlias, |
| 2261 | recoveryServiceCertFile, recoveryServiceSigFile); |
| 2262 | } |
| 2263 | |
| 2264 | @Override |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 2265 | public @NonNull KeyChainSnapshot getKeyChainSnapshot() throws RemoteException { |
Dmitry Dementyev | b4fb987 | 2018-01-26 11:49:34 -0800 | [diff] [blame] | 2266 | return mRecoverableKeyStoreManager.getKeyChainSnapshot(); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 2267 | } |
| 2268 | |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 2269 | @Override |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 2270 | public void setSnapshotCreatedPendingIntent(@Nullable PendingIntent intent) |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 2271 | throws RemoteException { |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 2272 | mRecoverableKeyStoreManager.setSnapshotCreatedPendingIntent(intent); |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 2273 | } |
| 2274 | |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 2275 | @Override |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 2276 | public void setServerParams(byte[] serverParams) throws RemoteException { |
| 2277 | mRecoverableKeyStoreManager.setServerParams(serverParams); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | @Override |
Robert Berry | bbe02ae | 2018-02-20 19:47:43 +0000 | [diff] [blame] | 2281 | public void setRecoveryStatus(String alias, int status) throws RemoteException { |
| 2282 | mRecoverableKeyStoreManager.setRecoveryStatus(alias, status); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 2283 | } |
| 2284 | |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 2285 | @Override |
| 2286 | public @NonNull Map getRecoveryStatus() throws RemoteException { |
Robert Berry | 56f06b4 | 2018-02-23 13:31:32 +0000 | [diff] [blame] | 2287 | return mRecoverableKeyStoreManager.getRecoveryStatus(); |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 2288 | } |
| 2289 | |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 2290 | @Override |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 2291 | public void setRecoverySecretTypes(@NonNull @KeyChainProtectionParams.UserSecretType |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 2292 | int[] secretTypes) throws RemoteException { |
| 2293 | mRecoverableKeyStoreManager.setRecoverySecretTypes(secretTypes); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | @Override |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 2297 | public @NonNull int[] getRecoverySecretTypes() throws RemoteException { |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 2298 | return mRecoverableKeyStoreManager.getRecoverySecretTypes(); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 2299 | |
| 2300 | } |
| 2301 | |
| 2302 | @Override |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 2303 | public @NonNull byte[] startRecoverySessionWithCertPath(@NonNull String sessionId, |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 2304 | @NonNull String rootCertificateAlias, @NonNull RecoveryCertPath verifierCertPath, |
| 2305 | @NonNull byte[] vaultParams, @NonNull byte[] vaultChallenge, |
| 2306 | @NonNull List<KeyChainProtectionParams> secrets) |
Bo Zhu | 7c1972f | 2018-02-22 21:43:52 -0800 | [diff] [blame] | 2307 | throws RemoteException { |
| 2308 | return mRecoverableKeyStoreManager.startRecoverySessionWithCertPath( |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 2309 | sessionId, rootCertificateAlias, verifierCertPath, vaultParams, vaultChallenge, |
| 2310 | secrets); |
Bo Zhu | 7c1972f | 2018-02-22 21:43:52 -0800 | [diff] [blame] | 2311 | } |
| 2312 | |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 2313 | @Override |
Robert Berry | 4a5c87d | 2018-03-19 18:00:46 +0000 | [diff] [blame] | 2314 | public Map<String, String> recoverKeyChainSnapshot( |
| 2315 | @NonNull String sessionId, |
| 2316 | @NonNull byte[] recoveryKeyBlob, |
| 2317 | @NonNull List<WrappedApplicationKey> applicationKeys) throws RemoteException { |
| 2318 | return mRecoverableKeyStoreManager.recoverKeyChainSnapshot( |
| 2319 | sessionId, recoveryKeyBlob, applicationKeys); |
| 2320 | } |
| 2321 | |
| 2322 | @Override |
Dmitry Dementyev | 745d2c9 | 2018-04-13 14:10:05 -0700 | [diff] [blame] | 2323 | public void closeSession(@NonNull String sessionId) throws RemoteException { |
| 2324 | mRecoverableKeyStoreManager.closeSession(sessionId); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 2325 | } |
| 2326 | |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 2327 | @Override |
Robert Berry | 5daccec | 2018-01-06 19:16:25 +0000 | [diff] [blame] | 2328 | public void removeKey(@NonNull String alias) throws RemoteException { |
| 2329 | mRecoverableKeyStoreManager.removeKey(alias); |
| 2330 | } |
| 2331 | |
| 2332 | @Override |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 2333 | public @Nullable String generateKey(@NonNull String alias) throws RemoteException { |
Robert Berry | a3b9947 | 2018-02-23 15:59:02 +0000 | [diff] [blame] | 2334 | return mRecoverableKeyStoreManager.generateKey(alias); |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | @Override |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 2338 | public @Nullable String generateKeyWithMetadata( |
| 2339 | @NonNull String alias, @Nullable byte[] metadata) throws RemoteException { |
| 2340 | return mRecoverableKeyStoreManager.generateKeyWithMetadata(alias, metadata); |
| 2341 | } |
| 2342 | |
| 2343 | @Override |
| 2344 | public @Nullable String importKey(@NonNull String alias, @NonNull byte[] keyBytes) |
| 2345 | throws RemoteException { |
Bo Zhu | 2c8e538 | 2018-02-26 15:54:25 -0800 | [diff] [blame] | 2346 | return mRecoverableKeyStoreManager.importKey(alias, keyBytes); |
| 2347 | } |
| 2348 | |
| 2349 | @Override |
Bo Zhu | c704834 | 2019-01-03 14:04:58 -0800 | [diff] [blame] | 2350 | public @Nullable String importKeyWithMetadata(@NonNull String alias, @NonNull byte[] keyBytes, |
| 2351 | @Nullable byte[] metadata) throws RemoteException { |
| 2352 | return mRecoverableKeyStoreManager.importKeyWithMetadata(alias, keyBytes, metadata); |
| 2353 | } |
| 2354 | |
| 2355 | @Override |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 2356 | public @Nullable String getKey(@NonNull String alias) throws RemoteException { |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 2357 | return mRecoverableKeyStoreManager.getKey(alias); |
| 2358 | } |
| 2359 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 2360 | private static final String[] VALID_SETTINGS = new String[] { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 2361 | LockPatternUtils.LOCKOUT_PERMANENT_KEY, |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 2362 | LockPatternUtils.PATTERN_EVER_CHOSEN_KEY, |
| 2363 | LockPatternUtils.PASSWORD_TYPE_KEY, |
| 2364 | LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 2365 | LockPatternUtils.LOCK_PASSWORD_SALT_KEY, |
| 2366 | LockPatternUtils.DISABLE_LOCKSCREEN_KEY, |
| 2367 | LockPatternUtils.LOCKSCREEN_OPTIONS, |
| 2368 | LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, |
| 2369 | LockPatternUtils.BIOMETRIC_WEAK_EVER_CHOSEN_KEY, |
| 2370 | LockPatternUtils.LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, |
| 2371 | LockPatternUtils.PASSWORD_HISTORY_KEY, |
| 2372 | Secure.LOCK_PATTERN_ENABLED, |
| 2373 | Secure.LOCK_BIOMETRIC_WEAK_FLAGS, |
| 2374 | Secure.LOCK_PATTERN_VISIBLE, |
| 2375 | Secure.LOCK_PATTERN_TACTILE_FEEDBACK_ENABLED |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 2376 | }; |
| 2377 | |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 2378 | // Reading these settings needs the contacts permission |
| 2379 | private static final String[] READ_CONTACTS_PROTECTED_SETTINGS = new String[] { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 2380 | Secure.LOCK_SCREEN_OWNER_INFO_ENABLED, |
| 2381 | Secure.LOCK_SCREEN_OWNER_INFO |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 2382 | }; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 2383 | |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 2384 | // Reading these settings needs the same permission as checking the password |
| 2385 | private static final String[] READ_PASSWORD_PROTECTED_SETTINGS = new String[] { |
| 2386 | LockPatternUtils.LOCK_PASSWORD_SALT_KEY, |
| 2387 | LockPatternUtils.PASSWORD_HISTORY_KEY, |
Adrian Roos | 855fa30 | 2015-04-02 16:01:12 +0200 | [diff] [blame] | 2388 | LockPatternUtils.PASSWORD_TYPE_KEY, |
Ricky Wai | 7f405f1 | 2016-05-31 12:05:05 +0100 | [diff] [blame] | 2389 | SEPARATE_PROFILE_CHALLENGE_KEY |
Adrian Roos | 001b00d | 2015-02-24 17:08:48 +0100 | [diff] [blame] | 2390 | }; |
| 2391 | |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 2392 | private static final String[] SETTINGS_TO_BACKUP = new String[] { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 2393 | Secure.LOCK_SCREEN_OWNER_INFO_ENABLED, |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 2394 | Secure.LOCK_SCREEN_OWNER_INFO, |
| 2395 | Secure.LOCK_PATTERN_VISIBLE, |
| 2396 | LockPatternUtils.LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS |
Amith Yamasani | 072543f | 2015-02-13 11:09:45 -0800 | [diff] [blame] | 2397 | }; |
| 2398 | |
Andres Morales | 301ea44 | 2015-04-17 09:15:47 -0700 | [diff] [blame] | 2399 | private class GateKeeperDiedRecipient implements IBinder.DeathRecipient { |
| 2400 | @Override |
| 2401 | public void binderDied() { |
| 2402 | mGateKeeperService.asBinder().unlinkToDeath(this, 0); |
| 2403 | mGateKeeperService = null; |
| 2404 | } |
| 2405 | } |
| 2406 | |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2407 | protected synchronized IGateKeeperService getGateKeeperService() { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 2408 | if (mGateKeeperService != null) { |
| 2409 | return mGateKeeperService; |
| 2410 | } |
| 2411 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 2412 | final IBinder service = ServiceManager.getService(Context.GATEKEEPER_SERVICE); |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 2413 | if (service != null) { |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2414 | try { |
| 2415 | service.linkToDeath(new GateKeeperDiedRecipient(), 0); |
| 2416 | } catch (RemoteException e) { |
| 2417 | Slog.w(TAG, " Unable to register death recipient", e); |
| 2418 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 2419 | mGateKeeperService = IGateKeeperService.Stub.asInterface(service); |
| 2420 | return mGateKeeperService; |
| 2421 | } |
| 2422 | |
| 2423 | Slog.e(TAG, "Unable to acquire GateKeeperService"); |
| 2424 | return null; |
| 2425 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2426 | |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2427 | private void gateKeeperClearSecureUserId(int userId) { |
| 2428 | try { |
| 2429 | getGateKeeperService().clearSecureUserId(userId); |
| 2430 | } catch (RemoteException e) { |
| 2431 | Slog.w(TAG, "Failed to clear SID", e); |
| 2432 | } |
| 2433 | } |
| 2434 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2435 | /** |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2436 | * A user's synthetic password does not change so it must be cached in certain circumstances to |
| 2437 | * enable untrusted credential reset. |
| 2438 | * |
| 2439 | * Untrusted credential reset will be removed in a future version (b/68036371) at which point |
| 2440 | * this cache is no longer needed as the SP will always be known when changing the user's |
| 2441 | * credential. |
| 2442 | */ |
| 2443 | @GuardedBy("mSpManager") |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2444 | private SparseArray<AuthenticationToken> mSpCache = new SparseArray<>(); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2445 | |
| 2446 | private void onAuthTokenKnownForUser(@UserIdInt int userId, AuthenticationToken auth) { |
Andrew Scull | ede482d | 2018-01-30 13:54:29 +0000 | [diff] [blame] | 2447 | // Preemptively cache the SP and then try to remove it in a handler. |
| 2448 | Slog.i(TAG, "Caching SP for user " + userId); |
| 2449 | synchronized (mSpManager) { |
| 2450 | mSpCache.put(userId, auth); |
| 2451 | } |
| 2452 | tryRemoveUserFromSpCacheLater(userId); |
| 2453 | |
David Anderson | 6ebc25b | 2019-02-12 16:25:56 -0800 | [diff] [blame] | 2454 | if (mInjector.isGsiRunning()) { |
| 2455 | Slog.w(TAG, "AuthSecret disabled in GSI"); |
| 2456 | return; |
| 2457 | } |
| 2458 | |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2459 | // Pass the primary user's auth secret to the HAL |
| 2460 | if (mAuthSecretService != null && mUserManager.getUserInfo(userId).isPrimary()) { |
| 2461 | try { |
| 2462 | final byte[] rawSecret = auth.deriveVendorAuthSecret(); |
| 2463 | final ArrayList<Byte> secret = new ArrayList<>(rawSecret.length); |
| 2464 | for (int i = 0; i < rawSecret.length; ++i) { |
| 2465 | secret.add(rawSecret[i]); |
| 2466 | } |
| 2467 | mAuthSecretService.primaryUserCredential(secret); |
| 2468 | } catch (RemoteException e) { |
| 2469 | Slog.w(TAG, "Failed to pass primary user secret to AuthSecret HAL", e); |
| 2470 | } |
| 2471 | } |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
Andrew Scull | ede482d | 2018-01-30 13:54:29 +0000 | [diff] [blame] | 2474 | private void tryRemoveUserFromSpCacheLater(@UserIdInt int userId) { |
| 2475 | mHandler.post(() -> { |
| 2476 | if (!shouldCacheSpForUser(userId)) { |
| 2477 | // The transition from 'should not cache' to 'should cache' can only happen if |
| 2478 | // certain admin apps are installed after provisioning e.g. via adb. This is not |
| 2479 | // a common case and we do not seamlessly support; it may result in the SP not |
| 2480 | // being cached when it is needed. The cache can be re-populated by verifying |
| 2481 | // the credential again. |
| 2482 | Slog.i(TAG, "Removing SP from cache for user " + userId); |
| 2483 | synchronized (mSpManager) { |
| 2484 | mSpCache.remove(userId); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2485 | } |
| 2486 | } |
Andrew Scull | ede482d | 2018-01-30 13:54:29 +0000 | [diff] [blame] | 2487 | }); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
Andrew Scull | ede482d | 2018-01-30 13:54:29 +0000 | [diff] [blame] | 2490 | /** Do not hold any of the locks from this service when calling. */ |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2491 | private boolean shouldCacheSpForUser(@UserIdInt int userId) { |
| 2492 | // Before the user setup has completed, an admin could be installed that requires the SP to |
| 2493 | // be cached (see below). |
| 2494 | if (Settings.Secure.getIntForUser(mContext.getContentResolver(), |
| 2495 | Settings.Secure.USER_SETUP_COMPLETE, 0, userId) == 0) { |
| 2496 | return true; |
| 2497 | } |
| 2498 | |
| 2499 | // If the user has an admin which can perform an untrusted credential reset, the SP needs to |
| 2500 | // be cached. If there isn't a DevicePolicyManager then there can't be an admin in the first |
| 2501 | // place so caching is not necessary. |
| 2502 | final DevicePolicyManagerInternal dpmi = LocalServices.getService( |
| 2503 | DevicePolicyManagerInternal.class); |
| 2504 | if (dpmi == null) { |
| 2505 | return false; |
| 2506 | } |
| 2507 | return dpmi.canUserHaveUntrustedCredentialReset(userId); |
| 2508 | } |
| 2509 | |
| 2510 | /** |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2511 | * Precondition: vold and keystore unlocked. |
| 2512 | * |
| 2513 | * Create new synthetic password, set up synthetic password blob protected by the supplied |
| 2514 | * user credential, and make the newly-created SP blob active. |
| 2515 | * |
| 2516 | * The invariant under a synthetic password is: |
| 2517 | * 1. If user credential exists, then both vold and keystore and protected with keys derived |
| 2518 | * from the synthetic password. |
| 2519 | * 2. If user credential does not exist, vold and keystore protection are cleared. This is to |
| 2520 | * make it consistent with current behaviour. It also allows ActivityManager to call |
| 2521 | * unlockUser() with empty secret. |
| 2522 | * 3. Once a user is migrated to have synthetic password, its value will never change, no matter |
| 2523 | * whether the user changes his lockscreen PIN or clear/reset it. When the user clears its |
| 2524 | * lockscreen PIN, we still maintain the existing synthetic password in a password blob |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2525 | * protected by a default PIN. |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2526 | * 4. The user SID is linked with synthetic password, but its cleared/re-created when the user |
| 2527 | * clears/re-creates his lockscreen PIN. |
| 2528 | * |
| 2529 | * |
| 2530 | * Different cases of calling this method: |
| 2531 | * 1. credentialHash != null |
| 2532 | * This implies credential != null, a new SP blob will be provisioned, and existing SID |
| 2533 | * migrated to associate with the new SP. |
| 2534 | * This happens during a normal migration case when the user currently has password. |
| 2535 | * |
| 2536 | * 2. credentialhash == null and credential == null |
Rubin Xu | 9a6d39a5 | 2018-02-07 08:52:34 +0000 | [diff] [blame] | 2537 | * A new SP blob and will be created, while the user has no credentials. |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2538 | * This can happens when we are activating an escrow token on a unsecured device, during |
| 2539 | * which we want to create the SP structure with an empty user credential. |
Rubin Xu | 9a6d39a5 | 2018-02-07 08:52:34 +0000 | [diff] [blame] | 2540 | * This could also happen during an untrusted reset to clear password. |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2541 | * |
| 2542 | * 3. credentialhash == null and credential != null |
| 2543 | * This is the untrusted credential reset, OR the user sets a new lockscreen password |
| 2544 | * FOR THE FIRST TIME on a SP-enabled device. New credential and new SID will be created |
| 2545 | */ |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2546 | @GuardedBy("mSpManager") |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 2547 | @VisibleForTesting |
| 2548 | protected AuthenticationToken initializeSyntheticPasswordLocked(byte[] credentialHash, |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2549 | LockscreenCredential credential, int userId) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2550 | Slog.i(TAG, "Initialize SyntheticPassword for user: " + userId); |
Rubin Xu | 9a6d39a5 | 2018-02-07 08:52:34 +0000 | [diff] [blame] | 2551 | final AuthenticationToken auth = mSpManager.newSyntheticPasswordAndSid( |
| 2552 | getGateKeeperService(), credentialHash, credential, userId); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2553 | onAuthTokenKnownForUser(userId, auth); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2554 | if (auth == null) { |
| 2555 | Slog.wtf(TAG, "initializeSyntheticPasswordLocked returns null auth token"); |
| 2556 | return null; |
| 2557 | } |
| 2558 | long handle = mSpManager.createPasswordBasedSyntheticPassword(getGateKeeperService(), |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2559 | credential, auth, userId); |
| 2560 | if (!credential.isNone()) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2561 | if (credentialHash == null) { |
| 2562 | // Since when initializing SP, we didn't provide an existing password handle |
| 2563 | // for it to migrate SID, we need to create a new SID for the user. |
| 2564 | mSpManager.newSidForUser(getGateKeeperService(), auth, userId); |
| 2565 | } |
| 2566 | mSpManager.verifyChallenge(getGateKeeperService(), auth, 0L, userId); |
| 2567 | setAuthlessUserKeyProtection(userId, auth.deriveDiskEncryptionKey()); |
| 2568 | setKeystorePassword(auth.deriveKeyStorePassword(), userId); |
| 2569 | } else { |
| 2570 | clearUserKeyProtection(userId); |
| 2571 | setKeystorePassword(null, userId); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2572 | gateKeeperClearSecureUserId(userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2573 | } |
| 2574 | fixateNewestUserKeyAuth(userId); |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 2575 | setSyntheticPasswordHandleLocked(handle, userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2576 | return auth; |
| 2577 | } |
| 2578 | |
| 2579 | private long getSyntheticPasswordHandleLocked(int userId) { |
Adrian Roos | 60dcbbf | 2017-08-08 16:19:33 +0200 | [diff] [blame] | 2580 | return getLong(SYNTHETIC_PASSWORD_HANDLE_KEY, |
| 2581 | SyntheticPasswordManager.DEFAULT_HANDLE, userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 2584 | private void setSyntheticPasswordHandleLocked(long handle, int userId) { |
| 2585 | final long oldHandle = getSyntheticPasswordHandleLocked(userId); |
| 2586 | setLong(SYNTHETIC_PASSWORD_HANDLE_KEY, handle, userId); |
| 2587 | setLong(PREV_SYNTHETIC_PASSWORD_HANDLE_KEY, oldHandle, userId); |
| 2588 | setLong(SYNTHETIC_PASSWORD_UPDATE_TIME_KEY, System.currentTimeMillis(), userId); |
| 2589 | |
| 2590 | } |
| 2591 | |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 2592 | @VisibleForTesting |
| 2593 | boolean isSyntheticPasswordBasedCredential(int userId) { |
| 2594 | synchronized (mSpManager) { |
| 2595 | return isSyntheticPasswordBasedCredentialLocked(userId); |
| 2596 | } |
| 2597 | } |
| 2598 | |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 2599 | private boolean isSyntheticPasswordBasedCredentialLocked(int userId) { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 2600 | if (userId == USER_FRP) { |
| 2601 | final int type = mStorage.readPersistentDataBlock().type; |
| 2602 | return type == PersistentData.TYPE_SP || type == PersistentData.TYPE_SP_WEAVER; |
| 2603 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2604 | long handle = getSyntheticPasswordHandleLocked(userId); |
| 2605 | // This is a global setting |
Paul Crowley | 7a0cc0a | 2017-05-31 22:12:57 +0000 | [diff] [blame] | 2606 | long enabled = getLong(SYNTHETIC_PASSWORD_ENABLED_KEY, |
| 2607 | SYNTHETIC_PASSWORD_ENABLED_BY_DEFAULT, UserHandle.USER_SYSTEM); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2608 | return enabled != 0 && handle != SyntheticPasswordManager.DEFAULT_HANDLE; |
| 2609 | } |
| 2610 | |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 2611 | @VisibleForTesting |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 2612 | protected boolean shouldMigrateToSyntheticPasswordLocked(int userId) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2613 | long handle = getSyntheticPasswordHandleLocked(userId); |
| 2614 | // This is a global setting |
Paul Crowley | 7a0cc0a | 2017-05-31 22:12:57 +0000 | [diff] [blame] | 2615 | long enabled = getLong(SYNTHETIC_PASSWORD_ENABLED_KEY, |
| 2616 | SYNTHETIC_PASSWORD_ENABLED_BY_DEFAULT, UserHandle.USER_SYSTEM); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2617 | return enabled != 0 && handle == SyntheticPasswordManager.DEFAULT_HANDLE; |
| 2618 | } |
| 2619 | |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 2620 | private void enableSyntheticPasswordLocked() { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2621 | setLong(SYNTHETIC_PASSWORD_ENABLED_KEY, 1, UserHandle.USER_SYSTEM); |
| 2622 | } |
| 2623 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2624 | private VerifyCredentialResponse spBasedDoVerifyCredential(LockscreenCredential userCredential, |
| 2625 | @ChallengeType int challengeType, long challenge, |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 2626 | int userId, ICheckCredentialProgressCallback progressCallback, |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2627 | @Nullable ArrayList<PendingResetLockout> resetLockouts) { |
Kevin Chyn | be67ce0 | 2019-06-10 16:14:22 -0700 | [diff] [blame] | 2628 | |
Kevin Chyn | 28623b6 | 2019-06-11 11:43:33 -0700 | [diff] [blame] | 2629 | final boolean hasEnrolledBiometrics = mInjector.hasEnrolledBiometrics(userId); |
Kevin Chyn | be67ce0 | 2019-06-10 16:14:22 -0700 | [diff] [blame] | 2630 | |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 2631 | Slog.d(TAG, "spBasedDoVerifyCredential: user=" + userId + " challengeType=" + challengeType |
Kevin Chyn | be67ce0 | 2019-06-10 16:14:22 -0700 | [diff] [blame] | 2632 | + " hasEnrolledBiometrics=" + hasEnrolledBiometrics); |
Rubin Xu | e94a770 | 2017-06-20 17:29:57 +0100 | [diff] [blame] | 2633 | |
Kevin Chyn | a38653c | 2019-02-11 17:46:21 -0800 | [diff] [blame] | 2634 | final PackageManager pm = mContext.getPackageManager(); |
| 2635 | // TODO: When lockout is handled under the HAL for all biometrics (fingerprint), |
| 2636 | // we need to generate challenge for each one, have it signed by GK and reset lockout |
| 2637 | // for each modality. |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 2638 | if (challengeType == CHALLENGE_NONE && pm.hasSystemFeature(PackageManager.FEATURE_FACE) |
Kevin Chyn | be67ce0 | 2019-06-10 16:14:22 -0700 | [diff] [blame] | 2639 | && hasEnrolledBiometrics) { |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 2640 | // If there are multiple profiles in the same account, ensure we only generate the |
| 2641 | // challenge once. |
| 2642 | challengeType = CHALLENGE_INTERNAL; |
Kevin Chyn | a38653c | 2019-02-11 17:46:21 -0800 | [diff] [blame] | 2643 | challenge = mContext.getSystemService(FaceManager.class).generateChallenge(); |
| 2644 | } |
| 2645 | |
Rubin Xu | e94a770 | 2017-06-20 17:29:57 +0100 | [diff] [blame] | 2646 | final AuthenticationResult authResult; |
| 2647 | VerifyCredentialResponse response; |
| 2648 | synchronized (mSpManager) { |
| 2649 | if (!isSyntheticPasswordBasedCredentialLocked(userId)) { |
| 2650 | return null; |
| 2651 | } |
| 2652 | if (userId == USER_FRP) { |
| 2653 | return mSpManager.verifyFrpCredential(getGateKeeperService(), |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2654 | userCredential, progressCallback); |
Rubin Xu | e94a770 | 2017-06-20 17:29:57 +0100 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | long handle = getSyntheticPasswordHandleLocked(userId); |
| 2658 | authResult = mSpManager.unwrapPasswordBasedSyntheticPassword( |
Rubin Xu | cf326f1 | 2017-11-15 11:55:35 +0000 | [diff] [blame] | 2659 | getGateKeeperService(), handle, userCredential, userId, progressCallback); |
Rubin Xu | e94a770 | 2017-06-20 17:29:57 +0100 | [diff] [blame] | 2660 | |
| 2661 | response = authResult.gkResponse; |
| 2662 | // credential has matched |
| 2663 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 2664 | // perform verifyChallenge with synthetic password which generates the real GK auth |
| 2665 | // token and response for the current user |
| 2666 | response = mSpManager.verifyChallenge(getGateKeeperService(), authResult.authToken, |
| 2667 | challenge, userId); |
| 2668 | if (response.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK) { |
| 2669 | // This shouldn't really happen: the unwrapping of SP succeeds, but SP doesn't |
| 2670 | // match the recorded GK password handle. |
| 2671 | Slog.wtf(TAG, "verifyChallenge with SP failed."); |
| 2672 | return VerifyCredentialResponse.ERROR; |
| 2673 | } |
| 2674 | } |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 2675 | } |
| 2676 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2677 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2678 | setUserPasswordMetrics(userCredential, userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2679 | unlockKeystore(authResult.authToken.deriveKeyStorePassword(), userId); |
Kevin Chyn | a38653c | 2019-02-11 17:46:21 -0800 | [diff] [blame] | 2680 | |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 2681 | // Do resetLockout / revokeChallenge when all profiles are unlocked |
| 2682 | if (hasEnrolledBiometrics) { |
| 2683 | if (resetLockouts == null) { |
| 2684 | resetLockouts = new ArrayList<>(); |
Kevin Chyn | a38653c | 2019-02-11 17:46:21 -0800 | [diff] [blame] | 2685 | } |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 2686 | resetLockouts.add(new PendingResetLockout(userId, response.getPayload())); |
Kevin Chyn | a38653c | 2019-02-11 17:46:21 -0800 | [diff] [blame] | 2687 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2688 | |
| 2689 | final byte[] secret = authResult.authToken.deriveDiskEncryptionKey(); |
| 2690 | Slog.i(TAG, "Unlocking user " + userId + " with secret only, length " + secret.length); |
Kevin Chyn | bc29dd7 | 2019-06-13 11:47:54 -0700 | [diff] [blame] | 2691 | unlockUser(userId, null, secret, challengeType, challenge, resetLockouts); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2692 | |
Rubin Xu | e94a770 | 2017-06-20 17:29:57 +0100 | [diff] [blame] | 2693 | activateEscrowTokens(authResult.authToken, userId); |
| 2694 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2695 | if (isManagedProfileWithSeparatedLock(userId)) { |
Pavel Grafov | a8da697 | 2019-04-03 13:58:58 +0100 | [diff] [blame] | 2696 | setDeviceUnlockedForUser(userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2697 | } |
Rubin Xu | e94a770 | 2017-06-20 17:29:57 +0100 | [diff] [blame] | 2698 | mStrongAuth.reportSuccessfulStrongAuthUnlock(userId); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2699 | |
| 2700 | onAuthTokenKnownForUser(userId, authResult.authToken); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2701 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 2702 | if (response.getTimeout() > 0) { |
| 2703 | requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_LOCKOUT, userId); |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | return response; |
| 2708 | } |
| 2709 | |
Pavel Grafov | a8da697 | 2019-04-03 13:58:58 +0100 | [diff] [blame] | 2710 | private void setDeviceUnlockedForUser(int userId) { |
| 2711 | final TrustManager trustManager = mContext.getSystemService(TrustManager.class); |
| 2712 | trustManager.setDeviceLockedForUser(userId, false); |
| 2713 | } |
| 2714 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2715 | /** |
| 2716 | * Change the user's lockscreen password by creating a new SP blob and update the handle, based |
| 2717 | * on an existing authentication token. Even though a new SP blob is created, the underlying |
| 2718 | * synthetic password is never changed. |
| 2719 | * |
| 2720 | * When clearing credential, we keep the SP unchanged, but clear its password handle so its |
| 2721 | * SID is gone. We also clear password from (software-based) keystore and vold, which will be |
| 2722 | * added back when new password is set in future. |
| 2723 | */ |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2724 | @GuardedBy("mSpManager") |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2725 | private long setLockCredentialWithAuthTokenLocked(LockscreenCredential credential, |
| 2726 | AuthenticationToken auth, int userId) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2727 | if (DEBUG) Slog.d(TAG, "setLockCredentialWithAuthTokenLocked: user=" + userId); |
| 2728 | long newHandle = mSpManager.createPasswordBasedSyntheticPassword(getGateKeeperService(), |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2729 | credential, auth, userId); |
| 2730 | final Map<Integer, LockscreenCredential> profilePasswords; |
| 2731 | if (!credential.isNone()) { |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 2732 | // not needed by synchronizeUnifiedWorkChallengeForProfiles() |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2733 | profilePasswords = null; |
| 2734 | |
| 2735 | if (mSpManager.hasSidForUser(userId)) { |
| 2736 | // We are changing password of a secured device, nothing more needed as |
| 2737 | // createPasswordBasedSyntheticPassword has already taken care of maintaining |
| 2738 | // the password handle and SID unchanged. |
| 2739 | |
| 2740 | //refresh auth token |
| 2741 | mSpManager.verifyChallenge(getGateKeeperService(), auth, 0L, userId); |
| 2742 | } else { |
| 2743 | // A new password is set on a previously-unsecured device, we need to generate |
| 2744 | // a new SID, and re-add keys to vold and keystore. |
| 2745 | mSpManager.newSidForUser(getGateKeeperService(), auth, userId); |
| 2746 | mSpManager.verifyChallenge(getGateKeeperService(), auth, 0L, userId); |
| 2747 | setAuthlessUserKeyProtection(userId, auth.deriveDiskEncryptionKey()); |
| 2748 | fixateNewestUserKeyAuth(userId); |
| 2749 | setKeystorePassword(auth.deriveKeyStorePassword(), userId); |
| 2750 | } |
| 2751 | } else { |
| 2752 | // Cache all profile password if they use unified work challenge. This will later be |
| 2753 | // used to clear the profile's password in synchronizeUnifiedWorkChallengeForProfiles() |
| 2754 | profilePasswords = getDecryptedPasswordsForAllTiedProfiles(userId); |
| 2755 | |
| 2756 | // we are clearing password of a secured device, so need to nuke SID as well. |
| 2757 | mSpManager.clearSidForUser(userId); |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2758 | gateKeeperClearSecureUserId(userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2759 | // Clear key from vold so ActivityManager can just unlock the user with empty secret |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 2760 | // during boot. Vold storage needs to be unlocked before manipulation of the keys can |
| 2761 | // succeed. |
| 2762 | unlockUserKey(userId, null, auth.deriveDiskEncryptionKey()); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2763 | clearUserKeyProtection(userId); |
| 2764 | fixateNewestUserKeyAuth(userId); |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 2765 | unlockKeystore(auth.deriveKeyStorePassword(), userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2766 | setKeystorePassword(null, userId); |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 2767 | removeBiometricsForUser(userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2768 | } |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 2769 | setSyntheticPasswordHandleLocked(newHandle, userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2770 | synchronizeUnifiedWorkChallengeForProfiles(userId, profilePasswords); |
Rubin Xu | 7cf4509 | 2017-08-28 11:47:35 +0100 | [diff] [blame] | 2771 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2772 | setUserPasswordMetrics(credential, userId); |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 2773 | |
| 2774 | if (profilePasswords != null) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2775 | for (Map.Entry<Integer, LockscreenCredential> entry : profilePasswords.entrySet()) { |
| 2776 | entry.getValue().zeroize(); |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 2777 | } |
| 2778 | } |
| 2779 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2780 | return newHandle; |
| 2781 | } |
| 2782 | |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 2783 | private void removeBiometricsForUser(int userId) { |
| 2784 | removeAllFingerprintForUser(userId); |
| 2785 | removeAllFaceForUser(userId); |
| 2786 | } |
| 2787 | |
| 2788 | private void removeAllFingerprintForUser(final int userId) { |
| 2789 | FingerprintManager mFingerprintManager = mInjector.getFingerprintManager(); |
| 2790 | if (mFingerprintManager != null && mFingerprintManager.isHardwareDetected()) { |
| 2791 | if (mFingerprintManager.hasEnrolledFingerprints(userId)) { |
| 2792 | mFingerprintManager.setActiveUser(userId); |
| 2793 | CountDownLatch latch = new CountDownLatch(1); |
| 2794 | // For the purposes of M and N, groupId is the same as userId. |
| 2795 | Fingerprint finger = new Fingerprint(null, userId, 0, 0); |
| 2796 | mFingerprintManager.remove(finger, userId, |
| 2797 | fingerprintManagerRemovalCallback(latch)); |
| 2798 | try { |
| 2799 | latch.await(10000, TimeUnit.MILLISECONDS); |
| 2800 | } catch (InterruptedException e) { |
| 2801 | Slog.e(TAG, "Latch interrupted when removing fingerprint", e); |
| 2802 | } |
| 2803 | } |
| 2804 | } |
| 2805 | } |
| 2806 | |
| 2807 | private void removeAllFaceForUser(final int userId) { |
| 2808 | FaceManager mFaceManager = mInjector.getFaceManager(); |
| 2809 | if (mFaceManager != null && mFaceManager.isHardwareDetected()) { |
| 2810 | if (mFaceManager.hasEnrolledTemplates(userId)) { |
| 2811 | mFaceManager.setActiveUser(userId); |
| 2812 | CountDownLatch latch = new CountDownLatch(1); |
| 2813 | Face face = new Face(null, 0, 0); |
| 2814 | mFaceManager.remove(face, userId, faceManagerRemovalCallback(latch)); |
| 2815 | try { |
| 2816 | latch.await(10000, TimeUnit.MILLISECONDS); |
| 2817 | } catch (InterruptedException e) { |
| 2818 | Slog.e(TAG, "Latch interrupted when removing face", e); |
| 2819 | } |
| 2820 | } |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | private FingerprintManager.RemovalCallback fingerprintManagerRemovalCallback( |
| 2825 | CountDownLatch latch) { |
| 2826 | return new FingerprintManager.RemovalCallback() { |
| 2827 | @Override |
| 2828 | public void onRemovalError(Fingerprint fp, int errMsgId, CharSequence err) { |
| 2829 | Slog.e(TAG, String.format( |
| 2830 | "Can't remove fingerprint %d in group %d. Reason: %s", |
| 2831 | fp.getBiometricId(), fp.getGroupId(), err)); |
| 2832 | latch.countDown(); |
| 2833 | } |
| 2834 | |
| 2835 | @Override |
| 2836 | public void onRemovalSucceeded(Fingerprint fp, int remaining) { |
| 2837 | if (remaining == 0) { |
| 2838 | latch.countDown(); |
| 2839 | } |
| 2840 | } |
| 2841 | }; |
| 2842 | } |
| 2843 | |
| 2844 | private FaceManager.RemovalCallback faceManagerRemovalCallback(CountDownLatch latch) { |
| 2845 | return new FaceManager.RemovalCallback() { |
| 2846 | @Override |
| 2847 | public void onRemovalError(Face face, int errMsgId, CharSequence err) { |
| 2848 | Slog.e(TAG, String.format("Can't remove face %d. Reason: %s", |
| 2849 | face.getBiometricId(), err)); |
| 2850 | latch.countDown(); |
| 2851 | } |
| 2852 | |
| 2853 | @Override |
| 2854 | public void onRemovalSucceeded(Face face, int remaining) { |
| 2855 | if (remaining == 0) { |
| 2856 | latch.countDown(); |
| 2857 | } |
| 2858 | } |
| 2859 | }; |
| 2860 | } |
| 2861 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2862 | /** |
| 2863 | * @param savedCredential if the user is a managed profile with unified challenge and |
| 2864 | * savedCredential is empty, LSS will try to re-derive the profile password internally. |
| 2865 | * TODO (b/80170828): Fix this so profile password is always passed in. |
| 2866 | */ |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2867 | @GuardedBy("mSpManager") |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2868 | private boolean spBasedSetLockCredentialInternalLocked(LockscreenCredential credential, |
| 2869 | LockscreenCredential savedCredential, int userId, |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2870 | boolean allowUntrustedChange, boolean isLockTiedToParent) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2871 | if (DEBUG) Slog.d(TAG, "spBasedSetLockCredentialInternalLocked: user=" + userId); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2872 | if (savedCredential.isNone() && isManagedProfileWithUnifiedLock(userId)) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2873 | // get credential from keystore when managed profile has unified lock |
| 2874 | try { |
| 2875 | savedCredential = getDecryptedPasswordForTiedProfile(userId); |
| 2876 | } catch (FileNotFoundException e) { |
| 2877 | Slog.i(TAG, "Child profile key not found"); |
| 2878 | } catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException |
| 2879 | | NoSuchAlgorithmException | NoSuchPaddingException |
| 2880 | | InvalidAlgorithmParameterException | IllegalBlockSizeException |
| 2881 | | BadPaddingException | CertificateException | IOException e) { |
| 2882 | Slog.e(TAG, "Failed to decrypt child profile key", e); |
| 2883 | } |
| 2884 | } |
| 2885 | long handle = getSyntheticPasswordHandleLocked(userId); |
Rubin Xu | 8b30ec3 | 2017-03-05 00:47:09 +0000 | [diff] [blame] | 2886 | AuthenticationResult authResult = mSpManager.unwrapPasswordBasedSyntheticPassword( |
Rubin Xu | cf326f1 | 2017-11-15 11:55:35 +0000 | [diff] [blame] | 2887 | getGateKeeperService(), handle, savedCredential, userId, null); |
Rubin Xu | 8b30ec3 | 2017-03-05 00:47:09 +0000 | [diff] [blame] | 2888 | VerifyCredentialResponse response = authResult.gkResponse; |
| 2889 | AuthenticationToken auth = authResult.authToken; |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 2890 | |
Irina Dumitrescu | c90674d | 2019-02-28 17:34:19 +0000 | [diff] [blame] | 2891 | // If existing credential is provided, the existing credential must match. |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2892 | if (!savedCredential.isNone() && auth == null) { |
| 2893 | Slog.w(TAG, "Failed to enroll: incorrect credential"); |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 2894 | return false; |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 2895 | } |
Rubin Xu | 9a6d39a5 | 2018-02-07 08:52:34 +0000 | [diff] [blame] | 2896 | boolean untrustedReset = false; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2897 | if (auth != null) { |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 2898 | onAuthTokenKnownForUser(userId, auth); |
Irina Dumitrescu | c90674d | 2019-02-28 17:34:19 +0000 | [diff] [blame] | 2899 | } else if (response == null) { |
| 2900 | throw new IllegalStateException("Password change failed."); |
| 2901 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_ERROR) { |
Rubin Xu | 9a6d39a5 | 2018-02-07 08:52:34 +0000 | [diff] [blame] | 2902 | // We are performing an untrusted credential change, by DevicePolicyManager or other |
| 2903 | // internal callers that don't provide the existing credential |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2904 | Slog.w(TAG, "Untrusted credential change invoked"); |
Rubin Xu | 9a6d39a5 | 2018-02-07 08:52:34 +0000 | [diff] [blame] | 2905 | // Try to get a cached auth token, so we can keep SP unchanged. |
| 2906 | auth = mSpCache.get(userId); |
Irina Dumitrescu | c90674d | 2019-02-28 17:34:19 +0000 | [diff] [blame] | 2907 | if (!allowUntrustedChange) { |
| 2908 | throw new IllegalStateException("Untrusted credential change was invoked but it was" |
| 2909 | + " not allowed. This is likely a bug. Auth token is null: " |
| 2910 | + Boolean.toString(auth == null)); |
| 2911 | } |
Rubin Xu | 9a6d39a5 | 2018-02-07 08:52:34 +0000 | [diff] [blame] | 2912 | untrustedReset = true; |
Irina Dumitrescu | c90674d | 2019-02-28 17:34:19 +0000 | [diff] [blame] | 2913 | } else /* responseCode == VerifyCredentialResponse.RESPONSE_RETRY */ { |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 2914 | Slog.w(TAG, "Rate limit exceeded, so password was not changed."); |
| 2915 | return false; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 2916 | } |
Rubin Xu | 9a6d39a5 | 2018-02-07 08:52:34 +0000 | [diff] [blame] | 2917 | |
| 2918 | if (auth != null) { |
| 2919 | if (untrustedReset) { |
| 2920 | // Force change the current SID to mantain existing behaviour that an untrusted |
| 2921 | // reset leads to a change of SID. If the untrusted reset is for clearing the |
| 2922 | // current password, the nuking of the SID will be done in |
| 2923 | // setLockCredentialWithAuthTokenLocked next |
| 2924 | mSpManager.newSidForUser(getGateKeeperService(), auth, userId); |
| 2925 | } |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2926 | setLockCredentialWithAuthTokenLocked(credential, auth, userId); |
Rubin Xu | 9a6d39a5 | 2018-02-07 08:52:34 +0000 | [diff] [blame] | 2927 | mSpManager.destroyPasswordBasedSyntheticPassword(handle, userId); |
| 2928 | } else { |
| 2929 | throw new IllegalStateException( |
| 2930 | "Untrusted credential reset not possible without cached SP"); |
| 2931 | // Could call initializeSyntheticPasswordLocked(null, credential, credentialType, |
| 2932 | // requestedQuality, userId) instead if we still allow untrusted reset that changes |
| 2933 | // synthetic password. That would invalidate existing escrow tokens though. |
| 2934 | } |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2935 | sendCredentialsOnChangeIfRequired(credential, userId, isLockTiedToParent); |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 2936 | return true; |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 2939 | /** |
| 2940 | * Returns a fixed pseudorandom byte string derived from the user's synthetic password. |
| 2941 | * This is used to salt the password history hash to protect the hash against offline |
| 2942 | * bruteforcing, since rederiving this value requires a successful authentication. |
Rubin Xu | 4ed9898 | 2018-05-23 14:27:53 +0100 | [diff] [blame] | 2943 | * If user is a managed profile with unified challenge, currentCredential is ignored. |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 2944 | */ |
| 2945 | @Override |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2946 | public byte[] getHashFactor(LockscreenCredential currentCredential, int userId) { |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 2947 | checkPasswordReadPermission(userId); |
Rubin Xu | 4ed9898 | 2018-05-23 14:27:53 +0100 | [diff] [blame] | 2948 | if (isManagedProfileWithUnifiedLock(userId)) { |
| 2949 | try { |
| 2950 | currentCredential = getDecryptedPasswordForTiedProfile(userId); |
| 2951 | } catch (Exception e) { |
| 2952 | Slog.e(TAG, "Failed to get work profile credential", e); |
| 2953 | return null; |
| 2954 | } |
| 2955 | } |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 2956 | synchronized (mSpManager) { |
| 2957 | if (!isSyntheticPasswordBasedCredentialLocked(userId)) { |
| 2958 | Slog.w(TAG, "Synthetic password not enabled"); |
| 2959 | return null; |
| 2960 | } |
| 2961 | long handle = getSyntheticPasswordHandleLocked(userId); |
| 2962 | AuthenticationResult auth = mSpManager.unwrapPasswordBasedSyntheticPassword( |
| 2963 | getGateKeeperService(), handle, currentCredential, userId, null); |
| 2964 | if (auth.authToken == null) { |
| 2965 | Slog.w(TAG, "Current credential is incorrect"); |
| 2966 | return null; |
| 2967 | } |
| 2968 | return auth.authToken.derivePasswordHashFactor(); |
| 2969 | } |
| 2970 | } |
| 2971 | |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 2972 | private long addEscrowToken(byte[] token, int userId, EscrowTokenStateChangeCallback callback) { |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 2973 | if (DEBUG) Slog.d(TAG, "addEscrowToken: user=" + userId); |
| 2974 | synchronized (mSpManager) { |
| 2975 | enableSyntheticPasswordLocked(); |
Rubin Xu | 8b30ec3 | 2017-03-05 00:47:09 +0000 | [diff] [blame] | 2976 | // Migrate to synthetic password based credentials if the user has no password, |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 2977 | // the token can then be activated immediately. |
| 2978 | AuthenticationToken auth = null; |
| 2979 | if (!isUserSecure(userId)) { |
| 2980 | if (shouldMigrateToSyntheticPasswordLocked(userId)) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2981 | auth = initializeSyntheticPasswordLocked( |
| 2982 | /* credentialHash */ null, LockscreenCredential.createNone(), userId); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 2983 | } else /* isSyntheticPasswordBasedCredentialLocked(userId) */ { |
| 2984 | long pwdHandle = getSyntheticPasswordHandleLocked(userId); |
| 2985 | auth = mSpManager.unwrapPasswordBasedSyntheticPassword(getGateKeeperService(), |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 2986 | pwdHandle, LockscreenCredential.createNone(), userId, null).authToken; |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 2987 | } |
| 2988 | } |
Rubin Xu | 128180b | 2017-04-12 18:02:44 +0100 | [diff] [blame] | 2989 | if (isSyntheticPasswordBasedCredentialLocked(userId)) { |
| 2990 | disableEscrowTokenOnNonManagedDevicesIfNeeded(userId); |
| 2991 | if (!mSpManager.hasEscrowData(userId)) { |
| 2992 | throw new SecurityException("Escrow token is disabled on the current user"); |
| 2993 | } |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 2994 | } |
Ram Periathiruvadi | 32d5355 | 2019-02-19 13:25:46 -0800 | [diff] [blame] | 2995 | long handle = mSpManager.createTokenBasedSyntheticPassword(token, userId, callback); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 2996 | if (auth != null) { |
| 2997 | mSpManager.activateTokenBasedSyntheticPassword(handle, auth, userId); |
| 2998 | } |
| 2999 | return handle; |
| 3000 | } |
| 3001 | } |
| 3002 | |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 3003 | private void activateEscrowTokens(AuthenticationToken auth, int userId) { |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3004 | if (DEBUG) Slog.d(TAG, "activateEscrowTokens: user=" + userId); |
| 3005 | synchronized (mSpManager) { |
Rubin Xu | e94a770 | 2017-06-20 17:29:57 +0100 | [diff] [blame] | 3006 | disableEscrowTokenOnNonManagedDevicesIfNeeded(userId); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3007 | for (long handle : mSpManager.getPendingTokensForUser(userId)) { |
| 3008 | Slog.i(TAG, String.format("activateEscrowTokens: %x %d ", handle, userId)); |
| 3009 | mSpManager.activateTokenBasedSyntheticPassword(handle, auth, userId); |
| 3010 | } |
| 3011 | } |
| 3012 | } |
| 3013 | |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 3014 | private boolean isEscrowTokenActive(long handle, int userId) { |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3015 | synchronized (mSpManager) { |
| 3016 | return mSpManager.existsHandle(handle, userId); |
| 3017 | } |
| 3018 | } |
| 3019 | |
Rubin Xu | fc06773 | 2019-03-18 11:01:18 +0000 | [diff] [blame] | 3020 | @Override |
| 3021 | public boolean hasPendingEscrowToken(int userId) { |
| 3022 | checkPasswordReadPermission(userId); |
| 3023 | synchronized (mSpManager) { |
| 3024 | return !mSpManager.getPendingTokensForUser(userId).isEmpty(); |
| 3025 | } |
| 3026 | } |
| 3027 | |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 3028 | private boolean removeEscrowToken(long handle, int userId) { |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3029 | synchronized (mSpManager) { |
| 3030 | if (handle == getSyntheticPasswordHandleLocked(userId)) { |
| 3031 | Slog.w(TAG, "Cannot remove password handle"); |
| 3032 | return false; |
| 3033 | } |
| 3034 | if (mSpManager.removePendingToken(handle, userId)) { |
| 3035 | return true; |
| 3036 | } |
| 3037 | if (mSpManager.existsHandle(handle, userId)) { |
| 3038 | mSpManager.destroyTokenBasedSyntheticPassword(handle, userId); |
| 3039 | return true; |
| 3040 | } else { |
| 3041 | return false; |
| 3042 | } |
| 3043 | } |
| 3044 | } |
| 3045 | |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 3046 | private boolean setLockCredentialWithToken(LockscreenCredential credential, long tokenHandle, |
| 3047 | byte[] token, int userId) { |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3048 | boolean result; |
| 3049 | synchronized (mSpManager) { |
| 3050 | if (!mSpManager.hasEscrowData(userId)) { |
| 3051 | throw new SecurityException("Escrow token is disabled on the current user"); |
| 3052 | } |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 3053 | result = setLockCredentialWithTokenInternalLocked( |
| 3054 | credential, tokenHandle, token, userId); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3055 | } |
| 3056 | if (result) { |
| 3057 | synchronized (mSeparateChallengeLock) { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 3058 | setSeparateProfileChallengeEnabledLocked(userId, true, /* unused */ null); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3059 | } |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 3060 | if (credential.isNone()) { |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 3061 | // If clearing credential, unlock the user manually in order to progress user start |
| 3062 | // Call unlockUser() on a handler thread so no lock is held (either by LSS or by |
| 3063 | // the caller like DPMS), otherwise it can lead to deadlock. |
| 3064 | mHandler.post(() -> unlockUser(userId, null, null)); |
| 3065 | } |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3066 | notifyPasswordChanged(userId); |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 3067 | notifySeparateProfileChallengeChanged(userId); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3068 | } |
| 3069 | return result; |
| 3070 | } |
| 3071 | |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 3072 | @GuardedBy("mSpManager") |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 3073 | private boolean setLockCredentialWithTokenInternalLocked(LockscreenCredential credential, |
| 3074 | long tokenHandle, byte[] token, int userId) { |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 3075 | final AuthenticationResult result; |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 3076 | result = mSpManager.unwrapTokenBasedSyntheticPassword( |
| 3077 | getGateKeeperService(), tokenHandle, token, userId); |
| 3078 | if (result.authToken == null) { |
| 3079 | Slog.w(TAG, "Invalid escrow token supplied"); |
| 3080 | return false; |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3081 | } |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 3082 | if (result.gkResponse.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK) { |
| 3083 | // Most likely, an untrusted credential reset happened in the past which |
| 3084 | // changed the synthetic password |
| 3085 | Slog.e(TAG, "Obsolete token: synthetic password derived but it fails GK " |
| 3086 | + "verification."); |
| 3087 | return false; |
| 3088 | } |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 3089 | long oldHandle = getSyntheticPasswordHandleLocked(userId); |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 3090 | setLockCredentialWithAuthTokenLocked(credential, result.authToken, userId); |
Rubin Xu | ca7007b | 2019-03-25 11:44:41 +0000 | [diff] [blame] | 3091 | mSpManager.destroyPasswordBasedSyntheticPassword(oldHandle, userId); |
| 3092 | |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 3093 | onAuthTokenKnownForUser(userId, result.authToken); |
| 3094 | return true; |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 3097 | private boolean unlockUserWithToken(long tokenHandle, byte[] token, int userId) { |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3098 | AuthenticationResult authResult; |
| 3099 | synchronized (mSpManager) { |
| 3100 | if (!mSpManager.hasEscrowData(userId)) { |
| 3101 | throw new SecurityException("Escrow token is disabled on the current user"); |
| 3102 | } |
| 3103 | authResult = mSpManager.unwrapTokenBasedSyntheticPassword(getGateKeeperService(), |
| 3104 | tokenHandle, token, userId); |
| 3105 | if (authResult.authToken == null) { |
| 3106 | Slog.w(TAG, "Invalid escrow token supplied"); |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 3107 | return false; |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3108 | } |
| 3109 | } |
| 3110 | unlockUser(userId, null, authResult.authToken.deriveDiskEncryptionKey()); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 3111 | onAuthTokenKnownForUser(userId, authResult.authToken); |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 3112 | return true; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 3113 | } |
| 3114 | |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 3115 | static String timestampToString(long timestamp) { |
| 3116 | return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timestamp)); |
| 3117 | } |
| 3118 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 3119 | @Override |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 3120 | protected void dump(FileDescriptor fd, PrintWriter printWriter, String[] args) { |
| 3121 | if (!DumpUtils.checkDumpPermission(mContext, TAG, printWriter)) return; |
| 3122 | IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, " "); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 3123 | |
Rubin Xu | a0a0d35 | 2017-05-15 16:18:01 +0000 | [diff] [blame] | 3124 | pw.println("Current lock settings service state:"); |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 3125 | |
Rubin Xu | a0a0d35 | 2017-05-15 16:18:01 +0000 | [diff] [blame] | 3126 | pw.println(String.format("SP Enabled = %b", |
| 3127 | mLockPatternUtils.isSyntheticPasswordEnabled())); |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 3128 | pw.println(); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 3129 | |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 3130 | pw.println("User State:"); |
| 3131 | pw.increaseIndent(); |
Rubin Xu | a0a0d35 | 2017-05-15 16:18:01 +0000 | [diff] [blame] | 3132 | List<UserInfo> users = mUserManager.getUsers(); |
| 3133 | for (int user = 0; user < users.size(); user++) { |
| 3134 | final int userId = users.get(user).id; |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 3135 | pw.println("User " + userId); |
| 3136 | pw.increaseIndent(); |
Rubin Xu | a0a0d35 | 2017-05-15 16:18:01 +0000 | [diff] [blame] | 3137 | synchronized (mSpManager) { |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 3138 | pw.println(String.format("SP Handle: %x", |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 3139 | getSyntheticPasswordHandleLocked(userId))); |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 3140 | pw.println(String.format("Last changed: %s (%x)", |
| 3141 | timestampToString(getLong(SYNTHETIC_PASSWORD_UPDATE_TIME_KEY, 0, userId)), |
| 3142 | getLong(PREV_SYNTHETIC_PASSWORD_HANDLE_KEY, 0, userId))); |
Rubin Xu | a0a0d35 | 2017-05-15 16:18:01 +0000 | [diff] [blame] | 3143 | } |
| 3144 | try { |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 3145 | pw.println(String.format("SID: %x", |
Rubin Xu | a0a0d35 | 2017-05-15 16:18:01 +0000 | [diff] [blame] | 3146 | getGateKeeperService().getSecureUserId(userId))); |
| 3147 | } catch (RemoteException e) { |
| 3148 | // ignore. |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 3149 | } |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 3150 | // It's OK to dump the password type since anyone with physical access can just |
| 3151 | // observe it from the keyguard directly. |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 3152 | pw.println("Quality: " + getKeyguardStoredQuality(userId)); |
| 3153 | pw.println("CredentialType: " + getCredentialTypeInternal(userId)); |
Rubin Xu | ba6fef5 | 2019-10-25 11:10:43 +0100 | [diff] [blame] | 3154 | pw.println("SeparateChallenge: " + getSeparateProfileChallengeEnabledInternal(userId)); |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 3155 | pw.println(String.format("Metrics: %s", |
Rubin Xu | 1985486 | 2019-08-15 16:37:23 +0100 | [diff] [blame] | 3156 | getUserPasswordMetrics(userId) != null ? "known" : "unknown")); |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 3157 | pw.decreaseIndent(); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 3158 | } |
Rubin Xu | e2f925f | 2019-08-14 16:53:55 +0100 | [diff] [blame] | 3159 | pw.println(); |
| 3160 | pw.decreaseIndent(); |
| 3161 | |
| 3162 | pw.println("Storage:"); |
| 3163 | pw.increaseIndent(); |
| 3164 | mStorage.dump(pw); |
| 3165 | pw.println(); |
| 3166 | pw.decreaseIndent(); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 3169 | /** |
| 3170 | * Cryptographically disable escrow token support for the current user, if the user is not |
| 3171 | * managed (either user has a profile owner, or if device is managed). Do not disable |
| 3172 | * if we are running an automotive build. |
| 3173 | */ |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3174 | private void disableEscrowTokenOnNonManagedDevicesIfNeeded(int userId) { |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 3175 | final UserManagerInternal userManagerInternal = mInjector.getUserManagerInternal(); |
Rakesh Iyer | cb18d56 | 2017-03-06 18:05:53 -0800 | [diff] [blame] | 3176 | |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 3177 | // Managed profile should have escrow enabled |
| 3178 | if (userManagerInternal.isUserManaged(userId)) { |
| 3179 | Slog.i(TAG, "Managed profile can have escrow token"); |
| 3180 | return; |
| 3181 | } |
Rakesh Iyer | cb18d56 | 2017-03-06 18:05:53 -0800 | [diff] [blame] | 3182 | |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 3183 | // Devices with Device Owner should have escrow enabled on all users. |
| 3184 | if (userManagerInternal.isDeviceManaged()) { |
| 3185 | Slog.i(TAG, "Corp-owned device can have escrow token"); |
| 3186 | return; |
| 3187 | } |
| 3188 | |
| 3189 | // If the device is yet to be provisioned (still in SUW), there is still |
| 3190 | // a chance that Device Owner will be set on the device later, so postpone |
| 3191 | // disabling escrow token for now. |
| 3192 | if (!mInjector.getDeviceStateCache().isDeviceProvisioned()) { |
| 3193 | Slog.i(TAG, "Postpone disabling escrow tokens until device is provisioned"); |
| 3194 | return; |
| 3195 | } |
| 3196 | |
| 3197 | // Escrow tokens are enabled on automotive builds. |
| 3198 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) { |
| 3199 | return; |
| 3200 | } |
| 3201 | |
| 3202 | // Disable escrow token permanently on all other device/user types. |
| 3203 | Slog.i(TAG, "Disabling escrow token on user " + userId); |
| 3204 | if (isSyntheticPasswordBasedCredentialLocked(userId)) { |
| 3205 | mSpManager.destroyEscrowData(userId); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 3206 | } |
| 3207 | } |
| 3208 | |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 3209 | private class DeviceProvisionedObserver extends ContentObserver { |
| 3210 | private final Uri mDeviceProvisionedUri = Settings.Global.getUriFor( |
| 3211 | Settings.Global.DEVICE_PROVISIONED); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 3212 | private final Uri mUserSetupCompleteUri = Settings.Secure.getUriFor( |
| 3213 | Settings.Secure.USER_SETUP_COMPLETE); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 3214 | |
| 3215 | private boolean mRegistered; |
| 3216 | |
| 3217 | public DeviceProvisionedObserver() { |
| 3218 | super(null); |
| 3219 | } |
| 3220 | |
| 3221 | @Override |
Andrew Scull | ede482d | 2018-01-30 13:54:29 +0000 | [diff] [blame] | 3222 | public void onChange(boolean selfChange, Uri uri, @UserIdInt int userId) { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 3223 | if (mDeviceProvisionedUri.equals(uri)) { |
| 3224 | updateRegistration(); |
| 3225 | |
| 3226 | if (isProvisioned()) { |
| 3227 | Slog.i(TAG, "Reporting device setup complete to IGateKeeperService"); |
| 3228 | reportDeviceSetupComplete(); |
Adrian Roos | 454f53f | 2017-08-08 14:56:42 +0200 | [diff] [blame] | 3229 | clearFrpCredentialIfOwnerNotSecure(); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 3230 | } |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 3231 | } else if (mUserSetupCompleteUri.equals(uri)) { |
Andrew Scull | ede482d | 2018-01-30 13:54:29 +0000 | [diff] [blame] | 3232 | tryRemoveUserFromSpCacheLater(userId); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | public void onSystemReady() { |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 3237 | if (frpCredentialEnabled(mContext)) { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 3238 | updateRegistration(); |
| 3239 | } else { |
| 3240 | // If we don't intend to use frpCredentials and we're not provisioned yet, send |
| 3241 | // deviceSetupComplete immediately, so gatekeeper can discard any lingering |
| 3242 | // credentials immediately. |
| 3243 | if (!isProvisioned()) { |
| 3244 | Slog.i(TAG, "FRP credential disabled, reporting device setup complete " |
| 3245 | + "to Gatekeeper immediately"); |
| 3246 | reportDeviceSetupComplete(); |
| 3247 | } |
| 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | private void reportDeviceSetupComplete() { |
| 3252 | try { |
| 3253 | getGateKeeperService().reportDeviceSetupComplete(); |
| 3254 | } catch (RemoteException e) { |
| 3255 | Slog.e(TAG, "Failure reporting to IGateKeeperService", e); |
| 3256 | } |
| 3257 | } |
| 3258 | |
Adrian Roos | 454f53f | 2017-08-08 14:56:42 +0200 | [diff] [blame] | 3259 | /** |
| 3260 | * Clears the FRP credential if the user that controls it does not have a secure |
| 3261 | * lockscreen. |
| 3262 | */ |
| 3263 | private void clearFrpCredentialIfOwnerNotSecure() { |
| 3264 | List<UserInfo> users = mUserManager.getUsers(); |
| 3265 | for (UserInfo user : users) { |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 3266 | if (userOwnsFrpCredential(mContext, user)) { |
Adrian Roos | 454f53f | 2017-08-08 14:56:42 +0200 | [diff] [blame] | 3267 | if (!isUserSecure(user.id)) { |
| 3268 | mStorage.writePersistentDataBlock(PersistentData.TYPE_NONE, user.id, |
| 3269 | 0, null); |
| 3270 | } |
| 3271 | return; |
| 3272 | } |
| 3273 | } |
| 3274 | } |
| 3275 | |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 3276 | private void updateRegistration() { |
| 3277 | boolean register = !isProvisioned(); |
| 3278 | if (register == mRegistered) { |
| 3279 | return; |
| 3280 | } |
| 3281 | if (register) { |
| 3282 | mContext.getContentResolver().registerContentObserver(mDeviceProvisionedUri, |
| 3283 | false, this); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 3284 | mContext.getContentResolver().registerContentObserver(mUserSetupCompleteUri, |
| 3285 | false, this, UserHandle.USER_ALL); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 3286 | } else { |
| 3287 | mContext.getContentResolver().unregisterContentObserver(this); |
| 3288 | } |
| 3289 | mRegistered = register; |
| 3290 | } |
| 3291 | |
| 3292 | private boolean isProvisioned() { |
| 3293 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 3294 | Settings.Global.DEVICE_PROVISIONED, 0) != 0; |
| 3295 | } |
| 3296 | } |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 3297 | |
| 3298 | private final class LocalService extends LockSettingsInternal { |
| 3299 | |
| 3300 | @Override |
Ram Periathiruvadi | 32d5355 | 2019-02-19 13:25:46 -0800 | [diff] [blame] | 3301 | public long addEscrowToken(byte[] token, int userId, |
| 3302 | EscrowTokenStateChangeCallback callback) { |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 3303 | return LockSettingsService.this.addEscrowToken(token, userId, callback); |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | @Override |
| 3307 | public boolean removeEscrowToken(long handle, int userId) { |
| 3308 | return LockSettingsService.this.removeEscrowToken(handle, userId); |
| 3309 | } |
| 3310 | |
| 3311 | @Override |
| 3312 | public boolean isEscrowTokenActive(long handle, int userId) { |
| 3313 | return LockSettingsService.this.isEscrowTokenActive(handle, userId); |
| 3314 | } |
| 3315 | |
| 3316 | @Override |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 3317 | public boolean setLockCredentialWithToken(LockscreenCredential credential, long tokenHandle, |
| 3318 | byte[] token, int userId) { |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 3319 | if (!mLockPatternUtils.hasSecureLockScreen()) { |
| 3320 | throw new UnsupportedOperationException( |
| 3321 | "This operation requires secure lock screen feature."); |
| 3322 | } |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 3323 | return LockSettingsService.this.setLockCredentialWithToken( |
| 3324 | credential, tokenHandle, token, userId); |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 3325 | } |
| 3326 | |
| 3327 | @Override |
| 3328 | public boolean unlockUserWithToken(long tokenHandle, byte[] token, int userId) { |
Rubin Xu | ca6ece5 | 2019-07-31 15:02:13 +0100 | [diff] [blame] | 3329 | return LockSettingsService.this.unlockUserWithToken(tokenHandle, token, userId); |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 3330 | } |
Rubin Xu | 1985486 | 2019-08-15 16:37:23 +0100 | [diff] [blame] | 3331 | |
| 3332 | @Override |
| 3333 | public PasswordMetrics getUserPasswordMetrics(int userHandle) { |
| 3334 | long identity = Binder.clearCallingIdentity(); |
| 3335 | try { |
| 3336 | if (isManagedProfileWithUnifiedLock(userHandle)) { |
| 3337 | // A managed profile with unified challenge is supposed to be protected by the |
| 3338 | // parent lockscreen, so asking for its password metrics is not really useful, |
| 3339 | // as this method would just return the metrics of the random profile password |
| 3340 | Slog.w(TAG, "Querying password metrics for unified challenge profile: " |
| 3341 | + userHandle); |
| 3342 | } |
| 3343 | } finally { |
| 3344 | Binder.restoreCallingIdentity(identity); |
| 3345 | } |
| 3346 | return LockSettingsService.this.getUserPasswordMetrics(userHandle); |
| 3347 | } |
| 3348 | |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 3349 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 3350 | } |