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