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