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