The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
| 17 | package com.android.internal.widget; |
| 18 | |
Rubin Xu | 682d167 | 2018-03-21 09:13:44 +0000 | [diff] [blame] | 19 | import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC; |
Rubin Xu | 682d167 | 2018-03-21 09:13:44 +0000 | [diff] [blame] | 20 | import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC; |
| 21 | import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX; |
| 22 | import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_SOMETHING; |
| 23 | import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
| 24 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 25 | import android.annotation.IntDef; |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 26 | import android.annotation.NonNull; |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 27 | import android.annotation.Nullable; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 28 | import android.app.admin.DevicePolicyManager; |
Pavel Grafov | 6f33484 | 2019-08-06 14:37:06 +0100 | [diff] [blame] | 29 | import android.app.admin.PasswordMetrics; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 30 | import android.app.trust.IStrongAuthTracker; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 31 | import android.app.trust.TrustManager; |
Artur Satayev | 00f8b53 | 2019-12-10 17:47:55 +0000 | [diff] [blame] | 32 | import android.compat.annotation.UnsupportedAppUsage; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 33 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.content.ContentResolver; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 35 | import android.content.Context; |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 36 | import android.content.pm.UserInfo; |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 37 | import android.os.AsyncTask; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 38 | import android.os.Handler; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 39 | import android.os.IBinder; |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 40 | import android.os.Looper; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 41 | import android.os.Message; |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 42 | import android.os.RemoteException; |
| 43 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 45 | import android.os.UserHandle; |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 46 | import android.os.UserManager; |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 47 | import android.os.storage.IStorageManager; |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 48 | import android.os.storage.StorageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.text.TextUtils; |
| 51 | import android.util.Log; |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 52 | import android.util.SparseBooleanArray; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 53 | import android.util.SparseIntArray; |
Kevin Chyn | a3e5582 | 2017-10-04 15:47:24 -0700 | [diff] [blame] | 54 | import android.util.SparseLongArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 56 | import com.android.internal.annotations.VisibleForTesting; |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 57 | import com.android.server.LocalServices; |
Kevin Chyn | b17d409 | 2018-10-01 19:07:03 -0700 | [diff] [blame] | 58 | |
Artur Satayev | 00f8b53 | 2019-12-10 17:47:55 +0000 | [diff] [blame] | 59 | import com.google.android.collect.Lists; |
| 60 | |
Rubin Xu | a3c71a1 | 2019-12-04 15:25:02 +0000 | [diff] [blame] | 61 | import libcore.util.HexEncoding; |
| 62 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 63 | import java.lang.annotation.Retention; |
| 64 | import java.lang.annotation.RetentionPolicy; |
Brian Carlstrom | 929a1c2 | 2011-02-01 21:54:09 -0800 | [diff] [blame] | 65 | import java.security.MessageDigest; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import java.security.NoSuchAlgorithmException; |
Jim Miller | 11b019d | 2010-01-20 16:34:45 -0800 | [diff] [blame] | 67 | import java.security.SecureRandom; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 68 | import java.util.ArrayList; |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 69 | import java.util.Arrays; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 70 | import java.util.Collection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | import java.util.List; |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 72 | import java.util.StringJoiner; |
Kevin Chyn | b17d409 | 2018-10-01 19:07:03 -0700 | [diff] [blame] | 73 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | /** |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 75 | * Utilities for the lock pattern and its settings. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | */ |
| 77 | public class LockPatternUtils { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | private static final String TAG = "LockPatternUtils"; |
Adrian Roos | 8370e47 | 2017-07-23 14:35:59 +0200 | [diff] [blame] | 79 | private static final boolean FRP_CREDENTIAL_ENABLED = true; |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 80 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | /** |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 82 | * The key to identify when the lock pattern enabled flag is being accessed for legacy reasons. |
Bryce Lee | 4614596 | 2015-12-14 14:39:10 -0800 | [diff] [blame] | 83 | */ |
| 84 | public static final String LEGACY_LOCK_PATTERN_ENABLED = "legacy_lock_pattern_enabled"; |
| 85 | |
| 86 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | * The interval of the countdown for showing progress of the lockout. |
| 88 | */ |
| 89 | public static final long FAILED_ATTEMPT_COUNTDOWN_INTERVAL_MS = 1000L; |
| 90 | |
Jim Miller | 4f36995 | 2011-08-19 18:29:22 -0700 | [diff] [blame] | 91 | /** |
| 92 | * This dictates when we start telling the user that continued failed attempts will wipe |
| 93 | * their device. |
| 94 | */ |
| 95 | public static final int FAILED_ATTEMPTS_BEFORE_WIPE_GRACE = 5; |
| 96 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | /** |
| 98 | * The minimum number of dots in a valid pattern. |
| 99 | */ |
| 100 | public static final int MIN_LOCK_PATTERN_SIZE = 4; |
| 101 | |
| 102 | /** |
Adrian Roos | f80e66ce9 | 2015-01-15 23:27:24 +0100 | [diff] [blame] | 103 | * The minimum size of a valid password. |
| 104 | */ |
| 105 | public static final int MIN_LOCK_PASSWORD_SIZE = 4; |
| 106 | |
| 107 | /** |
Pavel Grafov | 19a4fb3 | 2019-03-15 12:55:12 +0000 | [diff] [blame] | 108 | * The minimum number of dots the user must include in a wrong pattern attempt for it to be |
| 109 | * counted. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | */ |
Jim Miller | 4f36995 | 2011-08-19 18:29:22 -0700 | [diff] [blame] | 111 | public static final int MIN_PATTERN_REGISTER_FAIL = MIN_LOCK_PATTERN_SIZE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | |
Pavel Grafov | 6f33484 | 2019-08-06 14:37:06 +0100 | [diff] [blame] | 113 | // NOTE: When modifying this, make sure credential sufficiency validation logic is intact. |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 114 | public static final int CREDENTIAL_TYPE_NONE = -1; |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 115 | public static final int CREDENTIAL_TYPE_PATTERN = 1; |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 116 | // This is the legacy value persisted on disk. Never return it to clients, but internally |
| 117 | // we still need it to handle upgrade cases. |
| 118 | public static final int CREDENTIAL_TYPE_PASSWORD_OR_PIN = 2; |
| 119 | public static final int CREDENTIAL_TYPE_PIN = 3; |
| 120 | public static final int CREDENTIAL_TYPE_PASSWORD = 4; |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 121 | |
Pavel Grafov | 19a4fb3 | 2019-03-15 12:55:12 +0000 | [diff] [blame] | 122 | @Retention(RetentionPolicy.SOURCE) |
| 123 | @IntDef(prefix = {"CREDENTIAL_TYPE_"}, value = { |
| 124 | CREDENTIAL_TYPE_NONE, |
| 125 | CREDENTIAL_TYPE_PATTERN, |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 126 | CREDENTIAL_TYPE_PASSWORD, |
| 127 | CREDENTIAL_TYPE_PIN, |
| 128 | // CREDENTIAL_TYPE_PASSWORD_OR_PIN is missing on purpose. |
Pavel Grafov | 19a4fb3 | 2019-03-15 12:55:12 +0000 | [diff] [blame] | 129 | }) |
| 130 | public @interface CredentialType {} |
| 131 | |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Special user id for triggering the FRP verification flow. |
| 134 | */ |
| 135 | public static final int USER_FRP = UserHandle.USER_NULL + 1; |
| 136 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 137 | @Deprecated |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 138 | public final static String LOCKOUT_PERMANENT_KEY = "lockscreen.lockedoutpermanently"; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 139 | public final static String PATTERN_EVER_CHOSEN_KEY = "lockscreen.patterneverchosen"; |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 140 | public final static String PASSWORD_TYPE_KEY = "lockscreen.password_type"; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 141 | @Deprecated |
| 142 | public final static String PASSWORD_TYPE_ALTERNATE_KEY = "lockscreen.password_type_alternate"; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 143 | public final static String LOCK_PASSWORD_SALT_KEY = "lockscreen.password_salt"; |
| 144 | public final static String DISABLE_LOCKSCREEN_KEY = "lockscreen.disabled"; |
| 145 | public final static String LOCKSCREEN_OPTIONS = "lockscreen.options"; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 146 | @Deprecated |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 147 | public final static String LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK |
| 148 | = "lockscreen.biometric_weak_fallback"; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 149 | @Deprecated |
Danielle Millett | 7a07219 | 2011-10-03 17:36:01 -0400 | [diff] [blame] | 150 | public final static String BIOMETRIC_WEAK_EVER_CHOSEN_KEY |
| 151 | = "lockscreen.biometricweakeverchosen"; |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 152 | public final static String LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS |
| 153 | = "lockscreen.power_button_instantly_locks"; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 154 | @Deprecated |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 155 | public final static String LOCKSCREEN_WIDGETS_ENABLED = "lockscreen.widgets_enabled"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 157 | public final static String PASSWORD_HISTORY_KEY = "lockscreen.passwordhistory"; |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 158 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 159 | private static final String LOCK_SCREEN_OWNER_INFO = Settings.Secure.LOCK_SCREEN_OWNER_INFO; |
| 160 | private static final String LOCK_SCREEN_OWNER_INFO_ENABLED = |
| 161 | Settings.Secure.LOCK_SCREEN_OWNER_INFO_ENABLED; |
| 162 | |
Andrei Stingaceanu | 6644cd9 | 2015-11-10 13:03:31 +0000 | [diff] [blame] | 163 | private static final String LOCK_SCREEN_DEVICE_OWNER_INFO = "lockscreen.device_owner_info"; |
| 164 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 165 | private static final String ENABLED_TRUST_AGENTS = "lockscreen.enabledtrustagents"; |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 166 | private static final String IS_TRUST_USUALLY_MANAGED = "lockscreen.istrustusuallymanaged"; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 167 | |
Ricky Wai | d398244 | 2016-05-24 19:27:08 +0100 | [diff] [blame] | 168 | public static final String PROFILE_KEY_NAME_ENCRYPT = "profile_key_name_encrypt_"; |
| 169 | public static final String PROFILE_KEY_NAME_DECRYPT = "profile_key_name_decrypt_"; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 170 | public static final String SYNTHETIC_PASSWORD_KEY_PREFIX = "synthetic_password_"; |
| 171 | |
| 172 | public static final String SYNTHETIC_PASSWORD_HANDLE_KEY = "sp-handle"; |
| 173 | public static final String SYNTHETIC_PASSWORD_ENABLED_KEY = "enable-sp"; |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 174 | public static final int SYNTHETIC_PASSWORD_ENABLED_BY_DEFAULT = 1; |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 175 | private static final String HISTORY_DELIMITER = ","; |
Ricky Wai | d398244 | 2016-05-24 19:27:08 +0100 | [diff] [blame] | 176 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 177 | @UnsupportedAppUsage |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 178 | private final Context mContext; |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 179 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | private final ContentResolver mContentResolver; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 181 | private DevicePolicyManager mDevicePolicyManager; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 182 | private ILockSettings mLockSettingsService; |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 183 | private UserManager mUserManager; |
Adrian Roos | 7a3bf7c | 2016-07-12 15:31:55 -0700 | [diff] [blame] | 184 | private final Handler mHandler; |
Kevin Chyn | a3e5582 | 2017-10-04 15:47:24 -0700 | [diff] [blame] | 185 | private final SparseLongArray mLockoutDeadlines = new SparseLongArray(); |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 186 | private Boolean mHasSecureLockScreen; |
Jim Miller | ee82f8f | 2012-10-01 16:26:18 -0700 | [diff] [blame] | 187 | |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 188 | /** |
| 189 | * Use {@link TrustManager#isTrustUsuallyManaged(int)}. |
| 190 | * |
| 191 | * This returns the lazily-peristed value and should only be used by TrustManagerService. |
| 192 | */ |
| 193 | public boolean isTrustUsuallyManaged(int userId) { |
| 194 | if (!(mLockSettingsService instanceof ILockSettings.Stub)) { |
| 195 | throw new IllegalStateException("May only be called by TrustManagerService. " |
| 196 | + "Use TrustManager.isTrustUsuallyManaged()"); |
| 197 | } |
| 198 | try { |
| 199 | return getLockSettings().getBoolean(IS_TRUST_USUALLY_MANAGED, false, userId); |
| 200 | } catch (RemoteException e) { |
| 201 | return false; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | public void setTrustUsuallyManaged(boolean managed, int userId) { |
| 206 | try { |
| 207 | getLockSettings().setBoolean(IS_TRUST_USUALLY_MANAGED, managed, userId); |
| 208 | } catch (RemoteException e) { |
| 209 | // System dead. |
| 210 | } |
| 211 | } |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 212 | |
Adrian Roos | 4ab7e59 | 2016-04-13 15:38:13 -0700 | [diff] [blame] | 213 | public void userPresent(int userId) { |
| 214 | try { |
| 215 | getLockSettings().userPresent(userId); |
| 216 | } catch (RemoteException e) { |
| 217 | throw e.rethrowFromSystemServer(); |
| 218 | } |
| 219 | } |
| 220 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 221 | public static final class RequestThrottledException extends Exception { |
| 222 | private int mTimeoutMs; |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 223 | @UnsupportedAppUsage |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 224 | public RequestThrottledException(int timeoutMs) { |
| 225 | mTimeoutMs = timeoutMs; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * @return The amount of time in ms before another request may |
| 230 | * be executed |
| 231 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 232 | @UnsupportedAppUsage |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 233 | public int getTimeoutMs() { |
| 234 | return mTimeoutMs; |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 239 | @UnsupportedAppUsage |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 240 | public DevicePolicyManager getDevicePolicyManager() { |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 241 | if (mDevicePolicyManager == null) { |
| 242 | mDevicePolicyManager = |
| 243 | (DevicePolicyManager)mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 244 | if (mDevicePolicyManager == null) { |
| 245 | Log.e(TAG, "Can't get DevicePolicyManagerService: is it running?", |
| 246 | new IllegalStateException("Stack trace:")); |
| 247 | } |
| 248 | } |
| 249 | return mDevicePolicyManager; |
| 250 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 251 | |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 252 | private UserManager getUserManager() { |
| 253 | if (mUserManager == null) { |
| 254 | mUserManager = UserManager.get(mContext); |
| 255 | } |
| 256 | return mUserManager; |
| 257 | } |
| 258 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 259 | private TrustManager getTrustManager() { |
| 260 | TrustManager trust = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE); |
| 261 | if (trust == null) { |
| 262 | Log.e(TAG, "Can't get TrustManagerService: is it running?", |
| 263 | new IllegalStateException("Stack trace:")); |
| 264 | } |
| 265 | return trust; |
| 266 | } |
| 267 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 268 | @UnsupportedAppUsage |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 269 | public LockPatternUtils(Context context) { |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 270 | mContext = context; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 271 | mContentResolver = context.getContentResolver(); |
Adrian Roos | 7a3bf7c | 2016-07-12 15:31:55 -0700 | [diff] [blame] | 272 | |
| 273 | Looper looper = Looper.myLooper(); |
| 274 | mHandler = looper != null ? new Handler(looper) : null; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 275 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 276 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 277 | @UnsupportedAppUsage |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 278 | @VisibleForTesting |
| 279 | public ILockSettings getLockSettings() { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 280 | if (mLockSettingsService == null) { |
Adrian Roos | 450ce9f | 2014-10-29 14:30:37 +0100 | [diff] [blame] | 281 | ILockSettings service = ILockSettings.Stub.asInterface( |
| 282 | ServiceManager.getService("lock_settings")); |
Adrian Roos | 138b833 | 2014-11-11 13:51:07 +0100 | [diff] [blame] | 283 | mLockSettingsService = service; |
Brad Fitzpatrick | 9088100 | 2010-08-23 18:30:08 -0700 | [diff] [blame] | 284 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 285 | return mLockSettingsService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 288 | public int getRequestedMinimumPasswordLength(int userId) { |
| 289 | return getDevicePolicyManager().getPasswordMinimumLength(null, userId); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Pavel Grafov | 2e7929e | 2018-05-04 17:29:36 +0100 | [diff] [blame] | 292 | public int getMaximumPasswordLength(int quality) { |
| 293 | return getDevicePolicyManager().getPasswordMaximumLength(quality); |
| 294 | } |
| 295 | |
Pavel Grafov | 6f33484 | 2019-08-06 14:37:06 +0100 | [diff] [blame] | 296 | public PasswordMetrics getRequestedPasswordMetrics(int userId) { |
| 297 | return getDevicePolicyManager().getPasswordMinimumMetrics(userId); |
| 298 | } |
| 299 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 300 | public int getRequestedPasswordQuality(int userId) { |
| 301 | return getDevicePolicyManager().getPasswordQuality(null, userId); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | private int getRequestedPasswordHistoryLength(int userId) { |
| 305 | return getDevicePolicyManager().getPasswordHistoryLength(null, userId); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 308 | public int getRequestedPasswordMinimumLetters(int userId) { |
| 309 | return getDevicePolicyManager().getPasswordMinimumLetters(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 312 | public int getRequestedPasswordMinimumUpperCase(int userId) { |
| 313 | return getDevicePolicyManager().getPasswordMinimumUpperCase(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 316 | public int getRequestedPasswordMinimumLowerCase(int userId) { |
| 317 | return getDevicePolicyManager().getPasswordMinimumLowerCase(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 320 | public int getRequestedPasswordMinimumNumeric(int userId) { |
| 321 | return getDevicePolicyManager().getPasswordMinimumNumeric(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 324 | public int getRequestedPasswordMinimumSymbols(int userId) { |
| 325 | return getDevicePolicyManager().getPasswordMinimumSymbols(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 328 | public int getRequestedPasswordMinimumNonLetter(int userId) { |
| 329 | return getDevicePolicyManager().getPasswordMinimumNonLetter(null, userId); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 330 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 331 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 332 | @UnsupportedAppUsage |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 333 | public void reportFailedPasswordAttempt(int userId) { |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 334 | if (userId == USER_FRP && frpCredentialEnabled(mContext)) { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 335 | return; |
| 336 | } |
Adrian Roos | 4f994eb | 2014-07-23 15:45:05 +0200 | [diff] [blame] | 337 | getDevicePolicyManager().reportFailedPasswordAttempt(userId); |
| 338 | getTrustManager().reportUnlockAttempt(false /* authenticated */, userId); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 341 | @UnsupportedAppUsage |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 342 | public void reportSuccessfulPasswordAttempt(int userId) { |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 343 | if (userId == USER_FRP && frpCredentialEnabled(mContext)) { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 344 | return; |
| 345 | } |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 346 | getDevicePolicyManager().reportSuccessfulPasswordAttempt(userId); |
| 347 | getTrustManager().reportUnlockAttempt(true /* authenticated */, userId); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 350 | public void reportPasswordLockout(int timeoutMs, int userId) { |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 351 | if (userId == USER_FRP && frpCredentialEnabled(mContext)) { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 352 | return; |
| 353 | } |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 354 | getTrustManager().reportUnlockLockout(timeoutMs, userId); |
| 355 | } |
| 356 | |
Clara Bayarri | 51e41ad | 2016-02-11 17:48:53 +0000 | [diff] [blame] | 357 | public int getCurrentFailedPasswordAttempts(int userId) { |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 358 | if (userId == USER_FRP && frpCredentialEnabled(mContext)) { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 359 | return 0; |
| 360 | } |
Clara Bayarri | 51e41ad | 2016-02-11 17:48:53 +0000 | [diff] [blame] | 361 | return getDevicePolicyManager().getCurrentFailedPasswordAttempts(userId); |
| 362 | } |
| 363 | |
| 364 | public int getMaximumFailedPasswordsForWipe(int userId) { |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 365 | if (userId == USER_FRP && frpCredentialEnabled(mContext)) { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 366 | return 0; |
| 367 | } |
Clara Bayarri | 51e41ad | 2016-02-11 17:48:53 +0000 | [diff] [blame] | 368 | return getDevicePolicyManager().getMaximumFailedPasswordsForWipe( |
| 369 | null /* componentName */, userId); |
| 370 | } |
| 371 | |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 372 | /** |
| 373 | * Check to see if a credential matches the saved one. |
| 374 | * If credential matches, return an opaque attestation that the challenge was verified. |
| 375 | * |
| 376 | * @param credential The credential to check. |
| 377 | * @param challenge The challenge to verify against the credential |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 378 | * @param userId The user whose credential is being verified |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 379 | * @return the attestation that the challenge was verified, or null |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 380 | * @throws RequestThrottledException if credential verification is being throttled due to |
| 381 | * to many incorrect attempts. |
| 382 | * @throws IllegalStateException if called on the main thread. |
| 383 | */ |
| 384 | public byte[] verifyCredential(@NonNull LockscreenCredential credential, long challenge, |
| 385 | int userId) throws RequestThrottledException { |
| 386 | throwIfCalledOnMainThread(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 387 | try { |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 388 | VerifyCredentialResponse response = getLockSettings().verifyCredential( |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 389 | credential, challenge, userId); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 390 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 391 | return response.getPayload(); |
| 392 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 393 | throw new RequestThrottledException(response.getTimeout()); |
| 394 | } else { |
| 395 | return null; |
| 396 | } |
| 397 | } catch (RemoteException re) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 398 | Log.e(TAG, "failed to verify credential", re); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 399 | return null; |
| 400 | } |
| 401 | } |
| 402 | |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 403 | /** |
| 404 | * Check to see if a credential matches the saved one. |
| 405 | * |
| 406 | * @param credential The credential to check. |
| 407 | * @param userId The user whose credential is being checked |
| 408 | * @param progressCallback callback to deliver early signal that the credential matches |
| 409 | * @return {@code true} if credential matches, {@code false} otherwise |
| 410 | * @throws RequestThrottledException if credential verification is being throttled due to |
| 411 | * to many incorrect attempts. |
| 412 | * @throws IllegalStateException if called on the main thread. |
| 413 | */ |
| 414 | public boolean checkCredential(@NonNull LockscreenCredential credential, int userId, |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 415 | @Nullable CheckCredentialProgressCallback progressCallback) |
| 416 | throws RequestThrottledException { |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 417 | throwIfCalledOnMainThread(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 418 | try { |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 419 | VerifyCredentialResponse response = getLockSettings().checkCredential( |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 420 | credential, userId, wrapCallback(progressCallback)); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 421 | |
| 422 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 423 | return true; |
| 424 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 425 | throw new RequestThrottledException(response.getTimeout()); |
| 426 | } else { |
| 427 | return false; |
| 428 | } |
| 429 | } catch (RemoteException re) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 430 | Log.e(TAG, "failed to check credential", re); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 431 | return false; |
| 432 | } |
| 433 | } |
| 434 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | /** |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 436 | * Check if the credential of a managed profile with unified challenge matches. In this context, |
| 437 | * The credential should be the parent user's lockscreen password. If credential matches, |
| 438 | * return an opaque attestation associated with the managed profile that the challenge was |
| 439 | * verified. |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 440 | * |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 441 | * @param credential The parent user's credential to check. |
| 442 | * @param challenge The challenge to verify against the credential |
| 443 | * @return the attestation that the challenge was verified, or null |
| 444 | * @param userId The managed profile user id |
| 445 | * @throws RequestThrottledException if credential verification is being throttled due to |
| 446 | * to many incorrect attempts. |
| 447 | * @throws IllegalStateException if called on the main thread. |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 448 | */ |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 449 | public byte[] verifyTiedProfileChallenge(@NonNull LockscreenCredential credential, |
| 450 | long challenge, int userId) throws RequestThrottledException { |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 451 | throwIfCalledOnMainThread(); |
| 452 | try { |
| 453 | VerifyCredentialResponse response = |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 454 | getLockSettings().verifyTiedProfileChallenge(credential, challenge, userId); |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 455 | |
| 456 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 457 | return response.getPayload(); |
| 458 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 459 | throw new RequestThrottledException(response.getTimeout()); |
| 460 | } else { |
| 461 | return null; |
| 462 | } |
| 463 | } catch (RemoteException re) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 464 | Log.e(TAG, "failed to verify tied profile credential", re); |
Ricky Wai | 53940d4 | 2016-04-05 15:29:24 +0100 | [diff] [blame] | 465 | return null; |
| 466 | } |
| 467 | } |
| 468 | |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 469 | /** |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 470 | * Check to see if vold already has the password. |
| 471 | * Note that this also clears vold's copy of the password. |
| 472 | * @return Whether the vold password matches or not. |
| 473 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 474 | public boolean checkVoldPassword(int userId) { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 475 | try { |
| 476 | return getLockSettings().checkVoldPassword(userId); |
| 477 | } catch (RemoteException re) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 478 | Log.e(TAG, "failed to check vold password", re); |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 479 | return false; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /** |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 484 | * Returns the password history hash factor, needed to check new password against password |
Rubin Xu | 7959e2a | 2019-07-31 11:26:53 +0100 | [diff] [blame] | 485 | * history with {@link #checkPasswordHistory(byte[], byte[], int)} |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 486 | */ |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 487 | public byte[] getPasswordHistoryHashFactor(@NonNull LockscreenCredential currentPassword, |
| 488 | int userId) { |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 489 | try { |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 490 | return getLockSettings().getHashFactor(currentPassword, userId); |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 491 | } catch (RemoteException e) { |
| 492 | Log.e(TAG, "failed to get hash factor", e); |
| 493 | return null; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | /** |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 498 | * Check to see if a password matches any of the passwords stored in the |
| 499 | * password history. |
| 500 | * |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 501 | * @param passwordToCheck The password to check. |
| 502 | * @param hashFactor Hash factor of the current user returned from |
| 503 | * {@link ILockSettings#getHashFactor} |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 504 | * @return Whether the password matches any in the history. |
| 505 | */ |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 506 | public boolean checkPasswordHistory(byte[] passwordToCheck, byte[] hashFactor, int userId) { |
| 507 | if (passwordToCheck == null || passwordToCheck.length == 0) { |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 508 | Log.e(TAG, "checkPasswordHistory: empty password"); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 509 | return false; |
| 510 | } |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 511 | String passwordHistory = getString(PASSWORD_HISTORY_KEY, userId); |
| 512 | if (TextUtils.isEmpty(passwordHistory)) { |
| 513 | return false; |
| 514 | } |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 515 | int passwordHistoryLength = getRequestedPasswordHistoryLength(userId); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 516 | if(passwordHistoryLength == 0) { |
| 517 | return false; |
| 518 | } |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 519 | String legacyHash = legacyPasswordToHash(passwordToCheck, userId); |
| 520 | String passwordHash = passwordToHistoryHash(passwordToCheck, hashFactor, userId); |
| 521 | String[] history = passwordHistory.split(HISTORY_DELIMITER); |
| 522 | // Password History may be too long... |
| 523 | for (int i = 0; i < Math.min(passwordHistoryLength, history.length); i++) { |
| 524 | if (history[i].equals(legacyHash) || history[i].equals(passwordHash)) { |
| 525 | return true; |
| 526 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 527 | } |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 528 | return false; |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /** |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 532 | * Return true if the user has ever chosen a pattern. This is true even if the pattern is |
| 533 | * currently cleared. |
| 534 | * |
| 535 | * @return True if the user has ever chosen a pattern. |
| 536 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 537 | public boolean isPatternEverChosen(int userId) { |
| 538 | return getBoolean(PATTERN_EVER_CHOSEN_KEY, false, userId); |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /** |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 542 | * Records that the user has chosen a pattern at some time, even if the pattern is |
| 543 | * currently cleared. |
| 544 | */ |
| 545 | public void reportPatternWasChosen(int userId) { |
| 546 | setBoolean(PATTERN_EVER_CHOSEN_KEY, true, userId); |
| 547 | } |
| 548 | |
| 549 | /** |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 550 | * Used by device policy manager to validate the current password |
| 551 | * information it has. |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 552 | * @Deprecated use {@link #getKeyguardStoredPasswordQuality} |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 553 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 554 | @UnsupportedAppUsage |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 555 | public int getActivePasswordQuality(int userId) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 556 | return getKeyguardStoredPasswordQuality(userId); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 557 | } |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 558 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 559 | /** |
Ricky Wai | 4613fe4 | 2016-05-24 11:11:42 +0100 | [diff] [blame] | 560 | * Use it to reset keystore without wiping work profile |
| 561 | */ |
| 562 | public void resetKeyStore(int userId) { |
| 563 | try { |
| 564 | getLockSettings().resetKeyStore(userId); |
| 565 | } catch (RemoteException e) { |
| 566 | // It should not happen |
| 567 | Log.e(TAG, "Couldn't reset keystore " + e); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /** |
Benjamin Franz | 51ed794 | 2015-04-07 16:34:56 +0100 | [diff] [blame] | 572 | * Disable showing lock screen at all for a given user. |
| 573 | * This is only meaningful if pattern, pin or password are not set. |
Jim Miller | 2a98a4c | 2010-11-19 18:49:26 -0800 | [diff] [blame] | 574 | * |
Benjamin Franz | 51ed794 | 2015-04-07 16:34:56 +0100 | [diff] [blame] | 575 | * @param disable Disables lock screen when true |
| 576 | * @param userId User ID of the user this has effect on |
| 577 | */ |
| 578 | public void setLockScreenDisabled(boolean disable, int userId) { |
| 579 | setBoolean(DISABLE_LOCKSCREEN_KEY, disable, userId); |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Determine if LockScreen is disabled for the current user. This is used to decide whether |
| 584 | * LockScreen is shown after reboot or after screen timeout / short press on power. |
| 585 | * |
| 586 | * @return true if lock screen is disabled |
Jim Miller | 2a98a4c | 2010-11-19 18:49:26 -0800 | [diff] [blame] | 587 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 588 | @UnsupportedAppUsage |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 589 | public boolean isLockScreenDisabled(int userId) { |
Evan Rosky | 2eff745 | 2016-06-09 12:32:33 -0700 | [diff] [blame] | 590 | if (isSecure(userId)) { |
| 591 | return false; |
| 592 | } |
| 593 | boolean disabledByDefault = mContext.getResources().getBoolean( |
| 594 | com.android.internal.R.bool.config_disableLockscreenByDefault); |
| 595 | boolean isSystemUser = UserManager.isSplitSystemUser() && userId == UserHandle.USER_SYSTEM; |
Christine Franks | 5856be8 | 2017-06-23 18:12:46 -0700 | [diff] [blame] | 596 | UserInfo userInfo = getUserManager().getUserInfo(userId); |
| 597 | boolean isDemoUser = UserManager.isDeviceInDemoMode(mContext) && userInfo != null |
| 598 | && userInfo.isDemo(); |
Evan Rosky | 2eff745 | 2016-06-09 12:32:33 -0700 | [diff] [blame] | 599 | return getBoolean(DISABLE_LOCKSCREEN_KEY, false, userId) |
Christine Franks | 5856be8 | 2017-06-23 18:12:46 -0700 | [diff] [blame] | 600 | || (disabledByDefault && !isSystemUser) |
| 601 | || isDemoUser; |
Jim Miller | 2a98a4c | 2010-11-19 18:49:26 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 604 | /** Returns if the given quality maps to an alphabetic password */ |
| 605 | public static boolean isQualityAlphabeticPassword(int quality) { |
| 606 | return quality >= PASSWORD_QUALITY_ALPHABETIC; |
| 607 | } |
| 608 | |
| 609 | /** Returns if the given quality maps to an numeric pin */ |
| 610 | public static boolean isQualityNumericPin(int quality) { |
| 611 | return quality == PASSWORD_QUALITY_NUMERIC || quality == PASSWORD_QUALITY_NUMERIC_COMPLEX; |
| 612 | } |
| 613 | |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 614 | /** Returns the canonical password quality corresponding to the given credential type. */ |
| 615 | public static int credentialTypeToPasswordQuality(int credentialType) { |
| 616 | switch (credentialType) { |
| 617 | case CREDENTIAL_TYPE_NONE: |
| 618 | return PASSWORD_QUALITY_UNSPECIFIED; |
| 619 | case CREDENTIAL_TYPE_PATTERN: |
| 620 | return PASSWORD_QUALITY_SOMETHING; |
| 621 | case CREDENTIAL_TYPE_PIN: |
| 622 | return PASSWORD_QUALITY_NUMERIC; |
| 623 | case CREDENTIAL_TYPE_PASSWORD: |
| 624 | return PASSWORD_QUALITY_ALPHABETIC; |
| 625 | default: |
| 626 | throw new IllegalStateException("Unknown type: " + credentialType); |
| 627 | } |
| 628 | } |
| 629 | |
Jim Miller | 2a98a4c | 2010-11-19 18:49:26 -0800 | [diff] [blame] | 630 | /** |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 631 | * Save a new lockscreen credential. |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 632 | * |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 633 | * <p> This method will fail (returning {@code false}) if the previously saved credential |
| 634 | * provided is incorrect, or if the lockscreen verification is still being throttled. |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 635 | * |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 636 | * @param newCredential The new credential to save |
| 637 | * @param savedCredential The current credential |
Rubin Xu | d952240 | 2019-11-06 13:54:27 +0000 | [diff] [blame] | 638 | * @param userHandle the user whose lockscreen credential is to be changed |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 639 | * |
| 640 | * @return whether this method saved the new password successfully or not. This flow will fail |
| 641 | * and return false if the given credential is wrong. |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 642 | * @throws RuntimeException if password change encountered an unrecoverable error. |
Rubin Xu | d952240 | 2019-11-06 13:54:27 +0000 | [diff] [blame] | 643 | * @throws UnsupportedOperationException secure lockscreen is not supported on this device. |
| 644 | * @throws IllegalArgumentException if new credential is too short. |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 645 | */ |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 646 | public boolean setLockCredential(@NonNull LockscreenCredential newCredential, |
Rubin Xu | d952240 | 2019-11-06 13:54:27 +0000 | [diff] [blame] | 647 | @NonNull LockscreenCredential savedCredential, int userHandle) { |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 648 | if (!hasSecureLockScreen()) { |
| 649 | throw new UnsupportedOperationException( |
| 650 | "This operation requires the lock screen feature."); |
| 651 | } |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 652 | newCredential.checkLength(); |
Rubin Xu | 682d167 | 2018-03-21 09:13:44 +0000 | [diff] [blame] | 653 | |
Rubin Xu | 682d167 | 2018-03-21 09:13:44 +0000 | [diff] [blame] | 654 | try { |
Rubin Xu | d952240 | 2019-11-06 13:54:27 +0000 | [diff] [blame] | 655 | if (!getLockSettings().setLockCredential(newCredential, savedCredential, userHandle)) { |
Rubin Xu | 6a7303d | 2019-08-16 13:19:56 +0100 | [diff] [blame] | 656 | return false; |
| 657 | } |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 658 | } catch (RemoteException e) { |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 659 | throw new RuntimeException("Unable to save lock password", e); |
Rubin Xu | 682d167 | 2018-03-21 09:13:44 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 662 | onPostPasswordChanged(newCredential, userHandle); |
Irina Dumitrescu | c90674d | 2019-02-28 17:34:19 +0000 | [diff] [blame] | 663 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 666 | private void onPostPasswordChanged(LockscreenCredential newCredential, int userHandle) { |
| 667 | updateEncryptionPasswordIfNeeded(newCredential, userHandle); |
| 668 | if (newCredential.isPattern()) { |
| 669 | reportPatternWasChosen(userHandle); |
| 670 | } |
| 671 | updatePasswordHistory(newCredential, userHandle); |
| 672 | reportEnabledTrustAgentsChanged(userHandle); |
| 673 | } |
| 674 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 675 | private void updateCryptoUserInfo(int userId) { |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 676 | if (userId != UserHandle.USER_SYSTEM) { |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 677 | return; |
| 678 | } |
| 679 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 680 | final String ownerInfo = isOwnerInfoEnabled(userId) ? getOwnerInfo(userId) : ""; |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 681 | |
| 682 | IBinder service = ServiceManager.getService("mount"); |
| 683 | if (service == null) { |
| 684 | Log.e(TAG, "Could not find the mount service to update the user info"); |
| 685 | return; |
| 686 | } |
| 687 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 688 | IStorageManager storageManager = IStorageManager.Stub.asInterface(service); |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 689 | try { |
| 690 | Log.d(TAG, "Setting owner info"); |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 691 | storageManager.setField(StorageManager.OWNER_INFO_KEY, ownerInfo); |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 692 | } catch (RemoteException e) { |
| 693 | Log.e(TAG, "Error changing user info", e); |
| 694 | } |
| 695 | } |
| 696 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 697 | @UnsupportedAppUsage |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 698 | public void setOwnerInfo(String info, int userId) { |
| 699 | setString(LOCK_SCREEN_OWNER_INFO, info, userId); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 700 | updateCryptoUserInfo(userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 703 | @UnsupportedAppUsage |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 704 | public void setOwnerInfoEnabled(boolean enabled, int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 705 | setBoolean(LOCK_SCREEN_OWNER_INFO_ENABLED, enabled, userId); |
| 706 | updateCryptoUserInfo(userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 709 | @UnsupportedAppUsage |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 710 | public String getOwnerInfo(int userId) { |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 711 | return getString(LOCK_SCREEN_OWNER_INFO, userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 714 | public boolean isOwnerInfoEnabled(int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 715 | return getBoolean(LOCK_SCREEN_OWNER_INFO_ENABLED, false, userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 716 | } |
| 717 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | /** |
Andrei Stingaceanu | 6644cd9 | 2015-11-10 13:03:31 +0000 | [diff] [blame] | 719 | * Sets the device owner information. If the information is {@code null} or empty then the |
| 720 | * device owner info is cleared. |
| 721 | * |
| 722 | * @param info Device owner information which will be displayed instead of the user |
| 723 | * owner info. |
| 724 | */ |
| 725 | public void setDeviceOwnerInfo(String info) { |
| 726 | if (info != null && info.isEmpty()) { |
| 727 | info = null; |
| 728 | } |
| 729 | |
| 730 | setString(LOCK_SCREEN_DEVICE_OWNER_INFO, info, UserHandle.USER_SYSTEM); |
| 731 | } |
| 732 | |
| 733 | public String getDeviceOwnerInfo() { |
| 734 | return getString(LOCK_SCREEN_DEVICE_OWNER_INFO, UserHandle.USER_SYSTEM); |
| 735 | } |
| 736 | |
| 737 | public boolean isDeviceOwnerInfoEnabled() { |
| 738 | return getDeviceOwnerInfo() != null; |
| 739 | } |
| 740 | |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 741 | /** Update the encryption password if it is enabled **/ |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 742 | private void updateEncryptionPassword(final int type, final byte[] password) { |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 743 | if (!hasSecureLockScreen()) { |
| 744 | throw new UnsupportedOperationException( |
| 745 | "This operation requires the lock screen feature."); |
| 746 | } |
Amith Yamasani | cd410ba | 2014-10-17 11:16:58 -0700 | [diff] [blame] | 747 | if (!isDeviceEncryptionEnabled()) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 748 | return; |
| 749 | } |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 750 | final IBinder service = ServiceManager.getService("mount"); |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 751 | if (service == null) { |
| 752 | Log.e(TAG, "Could not find the mount service to update the encryption password"); |
| 753 | return; |
| 754 | } |
| 755 | |
Paul Crowley | a9f35462 | 2019-06-03 11:22:38 -0700 | [diff] [blame] | 756 | // TODO(b/120484642): This is a location where we still use a String for vold |
| 757 | String passwordString = password != null ? new String(password) : null; |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 758 | new AsyncTask<Void, Void, Void>() { |
| 759 | @Override |
| 760 | protected Void doInBackground(Void... dummy) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 761 | IStorageManager storageManager = IStorageManager.Stub.asInterface(service); |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 762 | try { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 763 | storageManager.changeEncryptionPassword(type, passwordString); |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 764 | } catch (RemoteException e) { |
| 765 | Log.e(TAG, "Error changing encryption password", e); |
| 766 | } |
| 767 | return null; |
| 768 | } |
| 769 | }.execute(); |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 770 | } |
| 771 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 772 | /** |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 773 | * Update device encryption password if calling user is USER_SYSTEM and device supports |
| 774 | * encryption. |
| 775 | */ |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 776 | private void updateEncryptionPasswordIfNeeded(LockscreenCredential credential, int userHandle) { |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 777 | // Update the device encryption password. |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 778 | if (userHandle != UserHandle.USER_SYSTEM || !isDeviceEncryptionEnabled()) { |
| 779 | return; |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 780 | } |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 781 | if (!shouldEncryptWithCredentials(true)) { |
| 782 | updateEncryptionPassword(StorageManager.CRYPT_TYPE_DEFAULT, null); |
| 783 | return; |
| 784 | } |
| 785 | if (credential.isNone()) { |
| 786 | // Set the encryption password to default. |
| 787 | setCredentialRequiredToDecrypt(false); |
| 788 | } |
| 789 | updateEncryptionPassword(credential.getStorageCryptType(), credential.getCredential()); |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 792 | /** |
| 793 | * Store the hash of the *current* password in the password history list, if device policy |
| 794 | * enforces password history requirement. |
| 795 | */ |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 796 | private void updatePasswordHistory(LockscreenCredential password, int userHandle) { |
| 797 | if (password.isNone()) { |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 798 | return; |
| 799 | } |
| 800 | if (password.isPattern()) { |
| 801 | // Do not keep track of historical patterns |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 802 | return; |
| 803 | } |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 804 | // Add the password to the password history. We assume all |
| 805 | // password hashes have the same length for simplicity of implementation. |
| 806 | String passwordHistory = getString(PASSWORD_HISTORY_KEY, userHandle); |
| 807 | if (passwordHistory == null) { |
| 808 | passwordHistory = ""; |
| 809 | } |
| 810 | int passwordHistoryLength = getRequestedPasswordHistoryLength(userHandle); |
| 811 | if (passwordHistoryLength == 0) { |
| 812 | passwordHistory = ""; |
| 813 | } else { |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 814 | final byte[] hashFactor = getPasswordHistoryHashFactor(password, userHandle); |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 815 | String hash = passwordToHistoryHash(password.getCredential(), hashFactor, userHandle); |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 816 | if (hash == null) { |
| 817 | Log.e(TAG, "Compute new style password hash failed, fallback to legacy style"); |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 818 | hash = legacyPasswordToHash(password.getCredential(), userHandle); |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 819 | } |
| 820 | if (TextUtils.isEmpty(passwordHistory)) { |
| 821 | passwordHistory = hash; |
| 822 | } else { |
| 823 | String[] history = passwordHistory.split(HISTORY_DELIMITER); |
| 824 | StringJoiner joiner = new StringJoiner(HISTORY_DELIMITER); |
| 825 | joiner.add(hash); |
| 826 | for (int i = 0; i < passwordHistoryLength - 1 && i < history.length; i++) { |
| 827 | joiner.add(history[i]); |
| 828 | } |
| 829 | passwordHistory = joiner.toString(); |
| 830 | } |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 831 | } |
| 832 | setString(PASSWORD_HISTORY_KEY, passwordHistory, userHandle); |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 835 | /** |
Jim Miller | 6848dc8 | 2014-10-13 18:51:53 -0700 | [diff] [blame] | 836 | * Determine if the device supports encryption, even if it's set to default. This |
| 837 | * differs from isDeviceEncrypted() in that it returns true even if the device is |
| 838 | * encrypted with the default password. |
| 839 | * @return true if device encryption is enabled |
| 840 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 841 | @UnsupportedAppUsage |
Jim Miller | 6848dc8 | 2014-10-13 18:51:53 -0700 | [diff] [blame] | 842 | public static boolean isDeviceEncryptionEnabled() { |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 843 | return StorageManager.isEncrypted(); |
Jim Miller | 6848dc8 | 2014-10-13 18:51:53 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /** |
Paul Lawrence | 5c21c70 | 2016-01-29 13:26:19 -0800 | [diff] [blame] | 847 | * Determine if the device is file encrypted |
| 848 | * @return true if device is file encrypted |
| 849 | */ |
| 850 | public static boolean isFileEncryptionEnabled() { |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 851 | return StorageManager.isFileEncryptedNativeOrEmulated(); |
Paul Lawrence | 5c21c70 | 2016-01-29 13:26:19 -0800 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /** |
Svetoslav | 16e4a1a | 2014-09-29 18:16:20 -0700 | [diff] [blame] | 855 | * Clears the encryption password. |
| 856 | */ |
| 857 | public void clearEncryptionPassword() { |
| 858 | updateEncryptionPassword(StorageManager.CRYPT_TYPE_DEFAULT, null); |
| 859 | } |
| 860 | |
| 861 | /** |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 862 | * Retrieves the quality mode for {@code userHandle}. |
| 863 | * @see DevicePolicyManager#getPasswordQuality(android.content.ComponentName) |
Adrian Roos | 1572ee3 | 2014-09-01 16:24:32 +0200 | [diff] [blame] | 864 | * |
| 865 | * @return stored password quality |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 866 | * @deprecated use {@link #getCredentialTypeForUser(int)} instead |
Adrian Roos | 1572ee3 | 2014-09-01 16:24:32 +0200 | [diff] [blame] | 867 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 868 | @UnsupportedAppUsage |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 869 | @Deprecated |
Adrian Roos | 1572ee3 | 2014-09-01 16:24:32 +0200 | [diff] [blame] | 870 | public int getKeyguardStoredPasswordQuality(int userHandle) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 871 | return credentialTypeToPasswordQuality(getCredentialTypeForUser(userHandle)); |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Danielle Millett | 5839698 | 2011-09-30 13:55:07 -0400 | [diff] [blame] | 874 | /** |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 875 | * Enables/disables the Separate Profile Challenge for this {@code userHandle}. This is a no-op |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 876 | * for user handles that do not belong to a managed profile. |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 877 | * |
| 878 | * @param userHandle Managed profile user id |
| 879 | * @param enabled True if separate challenge is enabled |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 880 | * @param profilePassword Managed profile previous password. Null when {@code enabled} is |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 881 | * true |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 882 | */ |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 883 | public void setSeparateProfileChallengeEnabled(int userHandle, boolean enabled, |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 884 | LockscreenCredential profilePassword) { |
Pavel Grafov | b319125 | 2017-07-14 12:30:31 +0100 | [diff] [blame] | 885 | if (!isManagedProfile(userHandle)) { |
| 886 | return; |
| 887 | } |
| 888 | try { |
| 889 | getLockSettings().setSeparateProfileChallengeEnabled(userHandle, enabled, |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 890 | profilePassword); |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 891 | reportEnabledTrustAgentsChanged(userHandle); |
Pavel Grafov | b319125 | 2017-07-14 12:30:31 +0100 | [diff] [blame] | 892 | } catch (RemoteException e) { |
| 893 | Log.e(TAG, "Couldn't update work profile challenge enabled"); |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
| 897 | /** |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 898 | * Returns true if {@code userHandle} is a managed profile with separate challenge. |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 899 | */ |
| 900 | public boolean isSeparateProfileChallengeEnabled(int userHandle) { |
Pavel Grafov | b319125 | 2017-07-14 12:30:31 +0100 | [diff] [blame] | 901 | return isManagedProfile(userHandle) && hasSeparateChallenge(userHandle); |
| 902 | } |
| 903 | |
| 904 | /** |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 905 | * Returns true if {@code userHandle} is a managed profile with unified challenge. |
Pavel Grafov | b319125 | 2017-07-14 12:30:31 +0100 | [diff] [blame] | 906 | */ |
| 907 | public boolean isManagedProfileWithUnifiedChallenge(int userHandle) { |
| 908 | return isManagedProfile(userHandle) && !hasSeparateChallenge(userHandle); |
| 909 | } |
| 910 | |
| 911 | /** |
| 912 | * Retrieves whether the current DPM allows use of the Profile Challenge. |
| 913 | */ |
| 914 | public boolean isSeparateProfileChallengeAllowed(int userHandle) { |
| 915 | return isManagedProfile(userHandle) |
| 916 | && getDevicePolicyManager().isSeparateProfileChallengeAllowed(userHandle); |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * Retrieves whether the current profile and device locks can be unified. |
Pavel Grafov | c4f87e9 | 2017-10-26 16:34:25 +0100 | [diff] [blame] | 921 | * @param userHandle profile user handle. |
Pavel Grafov | b319125 | 2017-07-14 12:30:31 +0100 | [diff] [blame] | 922 | */ |
| 923 | public boolean isSeparateProfileChallengeAllowedToUnify(int userHandle) { |
Pavel Grafov | c4f87e9 | 2017-10-26 16:34:25 +0100 | [diff] [blame] | 924 | return getDevicePolicyManager().isProfileActivePasswordSufficientForParent(userHandle) |
| 925 | && !getUserManager().hasUserRestriction( |
| 926 | UserManager.DISALLOW_UNIFIED_PASSWORD, UserHandle.of(userHandle)); |
Pavel Grafov | b319125 | 2017-07-14 12:30:31 +0100 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | private boolean hasSeparateChallenge(int userHandle) { |
Ricky Wai | dc283a8 | 2016-03-24 19:55:08 +0000 | [diff] [blame] | 930 | try { |
| 931 | return getLockSettings().getSeparateProfileChallengeEnabled(userHandle); |
| 932 | } catch (RemoteException e) { |
| 933 | Log.e(TAG, "Couldn't get separate profile challenge enabled"); |
| 934 | // Default value is false |
| 935 | return false; |
| 936 | } |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Pavel Grafov | b319125 | 2017-07-14 12:30:31 +0100 | [diff] [blame] | 939 | private boolean isManagedProfile(int userHandle) { |
| 940 | final UserInfo info = getUserManager().getUserInfo(userHandle); |
| 941 | return info != null && info.isManagedProfile(); |
Clara Bayarri | d769391 | 2016-01-22 17:26:31 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | * Deserialize a pattern. |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 946 | * @param bytes The pattern serialized with {@link #patternToByteArray} |
| 947 | * @return The pattern. |
| 948 | */ |
| 949 | public static List<LockPatternView.Cell> byteArrayToPattern(byte[] bytes) { |
| 950 | if (bytes == null) { |
| 951 | return null; |
| 952 | } |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 953 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | List<LockPatternView.Cell> result = Lists.newArrayList(); |
| 955 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | for (int i = 0; i < bytes.length; i++) { |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 957 | byte b = (byte) (bytes[i] - '1'); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | result.add(LockPatternView.Cell.of(b / 3, b % 3)); |
| 959 | } |
| 960 | return result; |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * Serialize a pattern. |
| 965 | * @param pattern The pattern. |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 966 | * @return The pattern in byte array form. |
| 967 | */ |
| 968 | public static byte[] patternToByteArray(List<LockPatternView.Cell> pattern) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | if (pattern == null) { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 970 | return new byte[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | } |
| 972 | final int patternSize = pattern.size(); |
| 973 | |
| 974 | byte[] res = new byte[patternSize]; |
| 975 | for (int i = 0; i < patternSize; i++) { |
| 976 | LockPatternView.Cell cell = pattern.get(i); |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 977 | res[i] = (byte) (cell.getRow() * 3 + cell.getColumn() + '1'); |
| 978 | } |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 979 | return res; |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 982 | private String getSalt(int userId) { |
| 983 | long salt = getLong(LOCK_PASSWORD_SALT_KEY, 0, userId); |
Jim Miller | 11b019d | 2010-01-20 16:34:45 -0800 | [diff] [blame] | 984 | if (salt == 0) { |
| 985 | try { |
| 986 | salt = SecureRandom.getInstance("SHA1PRNG").nextLong(); |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 987 | setLong(LOCK_PASSWORD_SALT_KEY, salt, userId); |
| 988 | Log.v(TAG, "Initialized lock password salt for user: " + userId); |
Jim Miller | 11b019d | 2010-01-20 16:34:45 -0800 | [diff] [blame] | 989 | } catch (NoSuchAlgorithmException e) { |
| 990 | // Throw an exception rather than storing a password we'll never be able to recover |
| 991 | throw new IllegalStateException("Couldn't get SecureRandom number", e); |
| 992 | } |
| 993 | } |
| 994 | return Long.toHexString(salt); |
| 995 | } |
| 996 | |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 997 | /** |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 998 | * Generate a hash for the given password. To avoid brute force attacks, we use a salted hash. |
| 999 | * Not the most secure, but it is at least a second level of protection. First level is that |
| 1000 | * the file is in a location only readable by the system process. |
Narayan Kamath | 78108a3e | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1001 | * |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1002 | * @param password the gesture pattern. |
Narayan Kamath | 78108a3e | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1003 | * |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1004 | * @return the hash of the pattern in a byte array. |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 1005 | * TODO: move to LockscreenCredential class |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1006 | */ |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1007 | public String legacyPasswordToHash(byte[] password, int userId) { |
| 1008 | if (password == null || password.length == 0) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1009 | return null; |
| 1010 | } |
Narayan Kamath | 78108a3e | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1011 | |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1012 | try { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1013 | // Previously the password was passed as a String with the following code: |
| 1014 | // byte[] saltedPassword = (password + getSalt(userId)).getBytes(); |
| 1015 | // The code below creates the identical digest preimage using byte arrays: |
| 1016 | byte[] salt = getSalt(userId).getBytes(); |
| 1017 | byte[] saltedPassword = Arrays.copyOf(password, password.length + salt.length); |
| 1018 | System.arraycopy(salt, 0, saltedPassword, password.length, salt.length); |
Narayan Kamath | 78108a3e | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1019 | byte[] sha1 = MessageDigest.getInstance("SHA-1").digest(saltedPassword); |
| 1020 | byte[] md5 = MessageDigest.getInstance("MD5").digest(saltedPassword); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1021 | |
Narayan Kamath | 78108a3e | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1022 | byte[] combined = new byte[sha1.length + md5.length]; |
| 1023 | System.arraycopy(sha1, 0, combined, 0, sha1.length); |
| 1024 | System.arraycopy(md5, 0, combined, sha1.length, md5.length); |
| 1025 | |
| 1026 | final char[] hexEncoded = HexEncoding.encode(combined); |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1027 | Arrays.fill(saltedPassword, (byte) 0); |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 1028 | return new String(hexEncoded); |
| 1029 | } catch (NoSuchAlgorithmException e) { |
| 1030 | throw new AssertionError("Missing digest algorithm: ", e); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /** |
| 1035 | * Hash the password for password history check purpose. |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 1036 | * TODO: move to LockscreenCredential class |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 1037 | */ |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1038 | private String passwordToHistoryHash(byte[] passwordToHash, byte[] hashFactor, int userId) { |
| 1039 | if (passwordToHash == null || passwordToHash.length == 0 || hashFactor == null) { |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 1040 | return null; |
| 1041 | } |
| 1042 | try { |
| 1043 | MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); |
| 1044 | sha256.update(hashFactor); |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1045 | byte[] salt = getSalt(userId).getBytes(); |
| 1046 | byte[] saltedPassword = Arrays.copyOf(passwordToHash, passwordToHash.length |
| 1047 | + salt.length); |
| 1048 | System.arraycopy(salt, 0, saltedPassword, passwordToHash.length, salt.length); |
| 1049 | sha256.update(saltedPassword); |
| 1050 | Arrays.fill(saltedPassword, (byte) 0); |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 1051 | return new String(HexEncoding.encode(sha256.digest())); |
Narayan Kamath | 78108a3e | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 1052 | } catch (NoSuchAlgorithmException e) { |
| 1053 | throw new AssertionError("Missing digest algorithm: ", e); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1054 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | /** |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1058 | * Returns the credential type of the user, can be one of {@link #CREDENTIAL_TYPE_NONE}, |
| 1059 | * {@link #CREDENTIAL_TYPE_PATTERN}, {@link #CREDENTIAL_TYPE_PIN} and |
| 1060 | * {@link #CREDENTIAL_TYPE_PASSWORD} |
| 1061 | */ |
| 1062 | public @CredentialType int getCredentialTypeForUser(int userHandle) { |
| 1063 | try { |
| 1064 | return getLockSettings().getCredentialType(userHandle); |
| 1065 | } catch (RemoteException re) { |
| 1066 | Log.e(TAG, "failed to get credential type", re); |
| 1067 | return CREDENTIAL_TYPE_NONE; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /** |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1072 | * @param userId the user for which to report the value |
| 1073 | * @return Whether the lock screen is secured. |
| 1074 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 1075 | @UnsupportedAppUsage |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1076 | public boolean isSecure(int userId) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1077 | int type = getCredentialTypeForUser(userId); |
| 1078 | return type != CREDENTIAL_TYPE_NONE; |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1079 | } |
| 1080 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 1081 | @UnsupportedAppUsage |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 1082 | public boolean isLockPasswordEnabled(int userId) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1083 | int type = getCredentialTypeForUser(userId); |
| 1084 | return type == CREDENTIAL_TYPE_PASSWORD || type == CREDENTIAL_TYPE_PIN; |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1087 | /** |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 1088 | * @return Whether the lock pattern is enabled |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 1090 | @UnsupportedAppUsage |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 1091 | public boolean isLockPatternEnabled(int userId) { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1092 | int type = getCredentialTypeForUser(userId); |
| 1093 | return type == CREDENTIAL_TYPE_PATTERN; |
Danielle Millett | 925a7d8 | 2012-03-19 18:02:20 -0400 | [diff] [blame] | 1094 | } |
| 1095 | |
Bryce Lee | 4614596 | 2015-12-14 14:39:10 -0800 | [diff] [blame] | 1096 | @Deprecated |
| 1097 | public boolean isLegacyLockPatternEnabled(int userId) { |
| 1098 | // Note: this value should default to {@code true} to avoid any reset that might result. |
| 1099 | // We must use a special key to read this value, since it will by default return the value |
| 1100 | // based on the new logic. |
| 1101 | return getBoolean(LEGACY_LOCK_PATTERN_ENABLED, true, userId); |
| 1102 | } |
| 1103 | |
| 1104 | @Deprecated |
| 1105 | public void setLegacyLockPatternEnabled(int userId) { |
| 1106 | setBoolean(Settings.Secure.LOCK_PATTERN_ENABLED, true, userId); |
| 1107 | } |
| 1108 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1109 | /** |
| 1110 | * @return Whether the visible pattern is enabled. |
| 1111 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 1112 | @UnsupportedAppUsage |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1113 | public boolean isVisiblePatternEnabled(int userId) { |
| 1114 | return getBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * Set whether the visible pattern is enabled. |
| 1119 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1120 | public void setVisiblePatternEnabled(boolean enabled, int userId) { |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 1121 | setBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, enabled, userId); |
Paul Lawrence | 878ba0a | 2014-08-20 13:08:01 -0700 | [diff] [blame] | 1122 | |
| 1123 | // Update for crypto if owner |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 1124 | if (userId != UserHandle.USER_SYSTEM) { |
Paul Lawrence | 878ba0a | 2014-08-20 13:08:01 -0700 | [diff] [blame] | 1125 | return; |
| 1126 | } |
| 1127 | |
| 1128 | IBinder service = ServiceManager.getService("mount"); |
| 1129 | if (service == null) { |
| 1130 | Log.e(TAG, "Could not find the mount service to update the user info"); |
| 1131 | return; |
| 1132 | } |
| 1133 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1134 | IStorageManager storageManager = IStorageManager.Stub.asInterface(service); |
Paul Lawrence | 878ba0a | 2014-08-20 13:08:01 -0700 | [diff] [blame] | 1135 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1136 | storageManager.setField(StorageManager.PATTERN_VISIBLE_KEY, enabled ? "1" : "0"); |
Paul Lawrence | 878ba0a | 2014-08-20 13:08:01 -0700 | [diff] [blame] | 1137 | } catch (RemoteException e) { |
| 1138 | Log.e(TAG, "Error changing pattern visible state", e); |
| 1139 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 1142 | public boolean isVisiblePatternEverChosen(int userId) { |
| 1143 | return getString(Settings.Secure.LOCK_PATTERN_VISIBLE, userId) != null; |
| 1144 | } |
| 1145 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1146 | /** |
Paul Lawrence | d8fdb33 | 2015-05-18 13:26:11 -0700 | [diff] [blame] | 1147 | * Set whether the visible password is enabled for cryptkeeper screen. |
| 1148 | */ |
| 1149 | public void setVisiblePasswordEnabled(boolean enabled, int userId) { |
| 1150 | // Update for crypto if owner |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 1151 | if (userId != UserHandle.USER_SYSTEM) { |
Paul Lawrence | d8fdb33 | 2015-05-18 13:26:11 -0700 | [diff] [blame] | 1152 | return; |
| 1153 | } |
| 1154 | |
| 1155 | IBinder service = ServiceManager.getService("mount"); |
| 1156 | if (service == null) { |
| 1157 | Log.e(TAG, "Could not find the mount service to update the user info"); |
| 1158 | return; |
| 1159 | } |
| 1160 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1161 | IStorageManager storageManager = IStorageManager.Stub.asInterface(service); |
Paul Lawrence | d8fdb33 | 2015-05-18 13:26:11 -0700 | [diff] [blame] | 1162 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1163 | storageManager.setField(StorageManager.PASSWORD_VISIBLE_KEY, enabled ? "1" : "0"); |
Paul Lawrence | d8fdb33 | 2015-05-18 13:26:11 -0700 | [diff] [blame] | 1164 | } catch (RemoteException e) { |
| 1165 | Log.e(TAG, "Error changing password visible state", e); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | * @return Whether tactile feedback for the pattern is enabled. |
| 1171 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 1172 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | public boolean isTactileFeedbackEnabled() { |
Jeff Sharkey | 5ed9d68 | 2012-10-10 14:28:27 -0700 | [diff] [blame] | 1174 | return Settings.System.getIntForUser(mContentResolver, |
| 1175 | Settings.System.HAPTIC_FEEDBACK_ENABLED, 1, UserHandle.USER_CURRENT) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | /** |
| 1179 | * Set and store the lockout deadline, meaning the user can't attempt his/her unlock |
| 1180 | * pattern until the deadline has passed. |
| 1181 | * @return the chosen deadline. |
| 1182 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 1183 | @UnsupportedAppUsage |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1184 | public long setLockoutAttemptDeadline(int userId, int timeoutMs) { |
| 1185 | final long deadline = SystemClock.elapsedRealtime() + timeoutMs; |
Adrian Roos | b7d81d9 | 2017-08-08 13:08:01 +0200 | [diff] [blame] | 1186 | if (userId == USER_FRP) { |
| 1187 | // For secure password storage (that is required for FRP), the underlying storage also |
| 1188 | // enforces the deadline. Since we cannot store settings for the FRP user, don't. |
| 1189 | return deadline; |
| 1190 | } |
Kevin Chyn | a3e5582 | 2017-10-04 15:47:24 -0700 | [diff] [blame] | 1191 | mLockoutDeadlines.put(userId, deadline); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1192 | return deadline; |
| 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * @return The elapsed time in millis in the future when the user is allowed to |
| 1197 | * attempt to enter his/her lock pattern, or 0 if the user is welcome to |
| 1198 | * enter a pattern. |
| 1199 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1200 | public long getLockoutAttemptDeadline(int userId) { |
Kevin Chyn | a3e5582 | 2017-10-04 15:47:24 -0700 | [diff] [blame] | 1201 | final long deadline = mLockoutDeadlines.get(userId, 0L); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1202 | final long now = SystemClock.elapsedRealtime(); |
Jorim Jaggi | e3e6d56 | 2015-09-28 13:57:37 -0700 | [diff] [blame] | 1203 | if (deadline < now && deadline != 0) { |
Andres Morales | a4e2337 | 2015-09-08 13:17:37 -0700 | [diff] [blame] | 1204 | // timeout expired |
Kevin Chyn | a3e5582 | 2017-10-04 15:47:24 -0700 | [diff] [blame] | 1205 | mLockoutDeadlines.put(userId, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1206 | return 0L; |
| 1207 | } |
| 1208 | return deadline; |
| 1209 | } |
| 1210 | |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1211 | private boolean getBoolean(String secureSettingKey, boolean defaultValue, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1212 | try { |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1213 | return getLockSettings().getBoolean(secureSettingKey, defaultValue, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1214 | } catch (RemoteException re) { |
| 1215 | return defaultValue; |
| 1216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1219 | private void setBoolean(String secureSettingKey, boolean enabled, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1220 | try { |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1221 | getLockSettings().setBoolean(secureSettingKey, enabled, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1222 | } catch (RemoteException re) { |
| 1223 | // What can we do? |
| 1224 | Log.e(TAG, "Couldn't write boolean " + secureSettingKey + re); |
| 1225 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1226 | } |
| 1227 | |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 1228 | private long getLong(String secureSettingKey, long defaultValue, int userHandle) { |
| 1229 | try { |
| 1230 | return getLockSettings().getLong(secureSettingKey, defaultValue, userHandle); |
| 1231 | } catch (RemoteException re) { |
| 1232 | return defaultValue; |
| 1233 | } |
| 1234 | } |
| 1235 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 1236 | @UnsupportedAppUsage |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1237 | private void setLong(String secureSettingKey, long value, int userHandle) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1238 | try { |
Kenny Guy | 0cf1370 | 2013-11-29 16:33:05 +0000 | [diff] [blame] | 1239 | getLockSettings().setLong(secureSettingKey, value, userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1240 | } catch (RemoteException re) { |
| 1241 | // What can we do? |
| 1242 | Log.e(TAG, "Couldn't write long " + secureSettingKey + re); |
| 1243 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 1246 | @UnsupportedAppUsage |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1247 | private String getString(String secureSettingKey, int userHandle) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1248 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1249 | return getLockSettings().getString(secureSettingKey, null, userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1250 | } catch (RemoteException re) { |
| 1251 | return null; |
| 1252 | } |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 1255 | @UnsupportedAppUsage |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1256 | private void setString(String secureSettingKey, String value, int userHandle) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1257 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1258 | getLockSettings().setString(secureSettingKey, value, userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1259 | } catch (RemoteException re) { |
| 1260 | // What can we do? |
| 1261 | Log.e(TAG, "Couldn't write string " + secureSettingKey + re); |
| 1262 | } |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1265 | public void setPowerButtonInstantlyLocks(boolean enabled, int userId) { |
| 1266 | setBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, enabled, userId); |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 1267 | } |
| 1268 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 1269 | @UnsupportedAppUsage |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1270 | public boolean getPowerButtonInstantlyLocks(int userId) { |
| 1271 | return getBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, true, userId); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1272 | } |
| 1273 | |
Bryan Mawhinney | e483b56 | 2017-05-15 14:46:05 +0100 | [diff] [blame] | 1274 | public boolean isPowerButtonInstantlyLocksEverChosen(int userId) { |
| 1275 | return getString(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, userId) != null; |
| 1276 | } |
| 1277 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1278 | public void setEnabledTrustAgents(Collection<ComponentName> activeTrustAgents, int userId) { |
| 1279 | StringBuilder sb = new StringBuilder(); |
| 1280 | for (ComponentName cn : activeTrustAgents) { |
| 1281 | if (sb.length() > 0) { |
| 1282 | sb.append(','); |
| 1283 | } |
| 1284 | sb.append(cn.flattenToShortString()); |
| 1285 | } |
| 1286 | setString(ENABLED_TRUST_AGENTS, sb.toString(), userId); |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1287 | getTrustManager().reportEnabledTrustAgentsChanged(userId); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | public List<ComponentName> getEnabledTrustAgents(int userId) { |
| 1291 | String serialized = getString(ENABLED_TRUST_AGENTS, userId); |
| 1292 | if (TextUtils.isEmpty(serialized)) { |
| 1293 | return null; |
| 1294 | } |
| 1295 | String[] split = serialized.split(","); |
| 1296 | ArrayList<ComponentName> activeTrustAgents = new ArrayList<ComponentName>(split.length); |
| 1297 | for (String s : split) { |
| 1298 | if (!TextUtils.isEmpty(s)) { |
| 1299 | activeTrustAgents.add(ComponentName.unflattenFromString(s)); |
| 1300 | } |
| 1301 | } |
| 1302 | return activeTrustAgents; |
| 1303 | } |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 1304 | |
| 1305 | /** |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 1306 | * Disable trust until credentials have been entered for user {@code userId}. |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1307 | * |
| 1308 | * Requires the {@link android.Manifest.permission#ACCESS_KEYGUARD_SECURE_STORAGE} permission. |
| 1309 | * |
| 1310 | * @param userId either an explicit user id or {@link android.os.UserHandle#USER_ALL} |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 1311 | */ |
| 1312 | public void requireCredentialEntry(int userId) { |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1313 | requireStrongAuth(StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST, userId); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | /** |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 1317 | * Requests strong authentication for user {@code userId}. |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1318 | * |
| 1319 | * Requires the {@link android.Manifest.permission#ACCESS_KEYGUARD_SECURE_STORAGE} permission. |
| 1320 | * |
| 1321 | * @param strongAuthReason a combination of {@link StrongAuthTracker.StrongAuthFlags} indicating |
| 1322 | * the reason for and the strength of the requested authentication. |
| 1323 | * @param userId either an explicit user id or {@link android.os.UserHandle#USER_ALL} |
| 1324 | */ |
| 1325 | public void requireStrongAuth(@StrongAuthTracker.StrongAuthFlags int strongAuthReason, |
| 1326 | int userId) { |
| 1327 | try { |
| 1328 | getLockSettings().requireStrongAuth(strongAuthReason, userId); |
| 1329 | } catch (RemoteException e) { |
| 1330 | Log.e(TAG, "Error while requesting strong auth: " + e); |
| 1331 | } |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 1332 | } |
Adrian Roos | 4b9e324 | 2014-08-20 23:36:25 +0200 | [diff] [blame] | 1333 | |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 1334 | private void reportEnabledTrustAgentsChanged(int userHandle) { |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 1335 | getTrustManager().reportEnabledTrustAgentsChanged(userHandle); |
Adrian Roos | 4b9e324 | 2014-08-20 23:36:25 +0200 | [diff] [blame] | 1336 | } |
Jim Miller | dd5de71 | 2014-10-16 19:50:18 -0700 | [diff] [blame] | 1337 | |
| 1338 | public boolean isCredentialRequiredToDecrypt(boolean defaultValue) { |
| 1339 | final int value = Settings.Global.getInt(mContentResolver, |
| 1340 | Settings.Global.REQUIRE_PASSWORD_TO_DECRYPT, -1); |
| 1341 | return value == -1 ? defaultValue : (value != 0); |
| 1342 | } |
| 1343 | |
| 1344 | public void setCredentialRequiredToDecrypt(boolean required) { |
Robin Lee | d39113e | 2016-01-22 15:44:49 +0000 | [diff] [blame] | 1345 | if (!(getUserManager().isSystemUser() || getUserManager().isPrimaryUser())) { |
| 1346 | throw new IllegalStateException( |
| 1347 | "Only the system or primary user may call setCredentialRequiredForDecrypt()"); |
Jim Miller | dd5de71 | 2014-10-16 19:50:18 -0700 | [diff] [blame] | 1348 | } |
Paul Lawrence | 688af6c | 2015-05-15 11:26:17 -0700 | [diff] [blame] | 1349 | |
| 1350 | if (isDeviceEncryptionEnabled()){ |
| 1351 | Settings.Global.putInt(mContext.getContentResolver(), |
| 1352 | Settings.Global.REQUIRE_PASSWORD_TO_DECRYPT, required ? 1 : 0); |
| 1353 | } |
Jim Miller | dd5de71 | 2014-10-16 19:50:18 -0700 | [diff] [blame] | 1354 | } |
Andrei Kapishnikov | 4eb6a36 | 2015-04-02 15:21:20 -0400 | [diff] [blame] | 1355 | |
| 1356 | private boolean isDoNotAskCredentialsOnBootSet() { |
Rubin Xu | 4929a5d | 2017-01-23 23:55:28 +0000 | [diff] [blame] | 1357 | return getDevicePolicyManager().getDoNotAskCredentialsOnBoot(); |
Andrei Kapishnikov | 4eb6a36 | 2015-04-02 15:21:20 -0400 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | private boolean shouldEncryptWithCredentials(boolean defaultValue) { |
| 1361 | return isCredentialRequiredToDecrypt(defaultValue) && !isDoNotAskCredentialsOnBootSet(); |
| 1362 | } |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 1363 | |
| 1364 | private void throwIfCalledOnMainThread() { |
| 1365 | if (Looper.getMainLooper().isCurrentThread()) { |
| 1366 | throw new IllegalStateException("should not be called from the main thread."); |
| 1367 | } |
| 1368 | } |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1369 | |
| 1370 | public void registerStrongAuthTracker(final StrongAuthTracker strongAuthTracker) { |
| 1371 | try { |
| 1372 | getLockSettings().registerStrongAuthTracker(strongAuthTracker.mStub); |
| 1373 | } catch (RemoteException e) { |
| 1374 | throw new RuntimeException("Could not register StrongAuthTracker"); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | public void unregisterStrongAuthTracker(final StrongAuthTracker strongAuthTracker) { |
| 1379 | try { |
| 1380 | getLockSettings().unregisterStrongAuthTracker(strongAuthTracker.mStub); |
| 1381 | } catch (RemoteException e) { |
| 1382 | Log.e(TAG, "Could not unregister StrongAuthTracker", e); |
| 1383 | } |
| 1384 | } |
| 1385 | |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 1386 | public void reportSuccessfulBiometricUnlock(boolean isStrongBiometric, int userId) { |
| 1387 | try { |
| 1388 | getLockSettings().reportSuccessfulBiometricUnlock(isStrongBiometric, userId); |
| 1389 | } catch (RemoteException e) { |
| 1390 | Log.e(TAG, "Could not report successful biometric unlock", e); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | public void scheduleNonStrongBiometricIdleTimeout(int userId) { |
| 1395 | try { |
| 1396 | getLockSettings().scheduleNonStrongBiometricIdleTimeout(userId); |
| 1397 | } catch (RemoteException e) { |
| 1398 | Log.e(TAG, "Could not schedule non-strong biometric idle timeout", e); |
| 1399 | } |
| 1400 | } |
| 1401 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1402 | /** |
Victor Chang | a0940d3 | 2016-05-16 19:36:08 +0100 | [diff] [blame] | 1403 | * @see StrongAuthTracker#getStrongAuthForUser |
| 1404 | */ |
| 1405 | public int getStrongAuthForUser(int userId) { |
| 1406 | try { |
| 1407 | return getLockSettings().getStrongAuthForUser(userId); |
| 1408 | } catch (RemoteException e) { |
| 1409 | Log.e(TAG, "Could not get StrongAuth", e); |
| 1410 | return StrongAuthTracker.getDefaultFlags(mContext); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | /** |
| 1415 | * @see StrongAuthTracker#isTrustAllowedForUser |
| 1416 | */ |
| 1417 | public boolean isTrustAllowedForUser(int userId) { |
| 1418 | return getStrongAuthForUser(userId) == StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED; |
| 1419 | } |
| 1420 | |
| 1421 | /** |
Rubin Xu | fc06773 | 2019-03-18 11:01:18 +0000 | [diff] [blame] | 1422 | * @see StrongAuthTracker#isBiometricAllowedForUser(int) |
Victor Chang | a0940d3 | 2016-05-16 19:36:08 +0100 | [diff] [blame] | 1423 | */ |
Kevin Chyn | b17d409 | 2018-10-01 19:07:03 -0700 | [diff] [blame] | 1424 | public boolean isBiometricAllowedForUser(int userId) { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1425 | return (getStrongAuthForUser(userId) & ~StrongAuthTracker.ALLOWING_BIOMETRIC) == 0; |
Victor Chang | a0940d3 | 2016-05-16 19:36:08 +0100 | [diff] [blame] | 1426 | } |
| 1427 | |
Chad Brubaker | ea8c5ef | 2018-03-15 16:37:43 -0700 | [diff] [blame] | 1428 | public boolean isUserInLockdown(int userId) { |
| 1429 | return getStrongAuthForUser(userId) |
| 1430 | == StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN; |
| 1431 | } |
| 1432 | |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 1433 | private ICheckCredentialProgressCallback wrapCallback( |
| 1434 | final CheckCredentialProgressCallback callback) { |
| 1435 | if (callback == null) { |
| 1436 | return null; |
| 1437 | } else { |
Adrian Roos | 7a3bf7c | 2016-07-12 15:31:55 -0700 | [diff] [blame] | 1438 | if (mHandler == null) { |
| 1439 | throw new IllegalStateException("Must construct LockPatternUtils on a looper thread" |
| 1440 | + " to use progress callbacks."); |
| 1441 | } |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 1442 | return new ICheckCredentialProgressCallback.Stub() { |
| 1443 | |
| 1444 | @Override |
| 1445 | public void onCredentialVerified() throws RemoteException { |
| 1446 | mHandler.post(callback::onEarlyMatched); |
| 1447 | } |
| 1448 | }; |
| 1449 | } |
| 1450 | } |
| 1451 | |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 1452 | private LockSettingsInternal getLockSettingsInternal() { |
| 1453 | LockSettingsInternal service = LocalServices.getService(LockSettingsInternal.class); |
| 1454 | if (service == null) { |
| 1455 | throw new SecurityException("Only available to system server itself"); |
| 1456 | } |
| 1457 | return service; |
| 1458 | } |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 1459 | /** |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 1460 | * Create an escrow token for the current user, which can later be used to unlock FBE |
| 1461 | * or change user password. |
| 1462 | * |
| 1463 | * After adding, if the user currently has lockscreen password, he will need to perform a |
| 1464 | * confirm credential operation in order to activate the token for future use. If the user |
| 1465 | * has no secure lockscreen, then the token is activated immediately. |
| 1466 | * |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 1467 | * <p>This method is only available to code running in the system server process itself. |
| 1468 | * |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 1469 | * @return a unique 64-bit token handle which is needed to refer to this token later. |
| 1470 | */ |
Ram Periathiruvadi | 32d5355 | 2019-02-19 13:25:46 -0800 | [diff] [blame] | 1471 | public long addEscrowToken(byte[] token, int userId, |
| 1472 | @Nullable EscrowTokenStateChangeCallback callback) { |
| 1473 | return getLockSettingsInternal().addEscrowToken(token, userId, callback); |
| 1474 | } |
| 1475 | |
| 1476 | /** |
| 1477 | * Callback interface to notify when an added escrow token has been activated. |
| 1478 | */ |
| 1479 | public interface EscrowTokenStateChangeCallback { |
| 1480 | /** |
| 1481 | * The method to be called when the token is activated. |
| 1482 | * @param handle 64 bit handle corresponding to the escrow token |
| 1483 | * @param userid user for whom the escrow token has been added |
| 1484 | */ |
| 1485 | void onEscrowTokenActivated(long handle, int userid); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | /** |
| 1489 | * Remove an escrow token. |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 1490 | * |
| 1491 | * <p>This method is only available to code running in the system server process itself. |
| 1492 | * |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 1493 | * @return true if the given handle refers to a valid token previously returned from |
| 1494 | * {@link #addEscrowToken}, whether it's active or not. return false otherwise. |
| 1495 | */ |
| 1496 | public boolean removeEscrowToken(long handle, int userId) { |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 1497 | return getLockSettingsInternal().removeEscrowToken(handle, userId); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | /** |
| 1501 | * Check if the given escrow token is active or not. Only active token can be used to call |
| 1502 | * {@link #setLockCredentialWithToken} and {@link #unlockUserWithToken} |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 1503 | * |
| 1504 | * <p>This method is only available to code running in the system server process itself. |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 1505 | */ |
| 1506 | public boolean isEscrowTokenActive(long handle, int userId) { |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 1507 | return getLockSettingsInternal().isEscrowTokenActive(handle, userId); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Rubin Xu | 7cf4509 | 2017-08-28 11:47:35 +0100 | [diff] [blame] | 1510 | /** |
| 1511 | * Change a user's lock credential with a pre-configured escrow token. |
| 1512 | * |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 1513 | * <p>This method is only available to code running in the system server process itself. |
| 1514 | * |
Rubin Xu | 7cf4509 | 2017-08-28 11:47:35 +0100 | [diff] [blame] | 1515 | * @param credential The new credential to be set |
Rubin Xu | 7cf4509 | 2017-08-28 11:47:35 +0100 | [diff] [blame] | 1516 | * @param tokenHandle Handle of the escrow token |
| 1517 | * @param token Escrow token |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 1518 | * @param userHandle The user who's lock credential to be changed |
Rubin Xu | 7cf4509 | 2017-08-28 11:47:35 +0100 | [diff] [blame] | 1519 | * @return {@code true} if the operation is successful. |
| 1520 | */ |
Rubin Xu | bb88320 | 2019-10-09 11:22:53 +0100 | [diff] [blame] | 1521 | public boolean setLockCredentialWithToken(@NonNull LockscreenCredential credential, |
| 1522 | long tokenHandle, byte[] token, int userHandle) { |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 1523 | if (!hasSecureLockScreen()) { |
| 1524 | throw new UnsupportedOperationException( |
| 1525 | "This operation requires the lock screen feature."); |
| 1526 | } |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 1527 | credential.checkLength(); |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 1528 | LockSettingsInternal localService = getLockSettingsInternal(); |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 1529 | |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1530 | if (!localService.setLockCredentialWithToken(credential, tokenHandle, token, userHandle)) { |
| 1531 | return false; |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 1532 | } |
Rubin Xu | 6232a4c | 2019-10-08 16:58:54 +0100 | [diff] [blame] | 1533 | |
| 1534 | onPostPasswordChanged(credential, userHandle); |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 1535 | return true; |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 1538 | /** |
| 1539 | * Unlock the specified user by an pre-activated escrow token. This should have the same effect |
| 1540 | * on device encryption as the user entering his lockscreen credentials for the first time after |
| 1541 | * boot, this includes unlocking the user's credential-encrypted storage as well as the keystore |
| 1542 | * |
| 1543 | * <p>This method is only available to code running in the system server process itself. |
| 1544 | * |
| 1545 | * @return {@code true} if the supplied token is valid and unlock succeeds, |
| 1546 | * {@code false} otherwise. |
| 1547 | */ |
| 1548 | public boolean unlockUserWithToken(long tokenHandle, byte[] token, int userId) { |
| 1549 | return getLockSettingsInternal().unlockUserWithToken(tokenHandle, token, userId); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | /** |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 1554 | * Callback to be notified about progress when checking credentials. |
| 1555 | */ |
| 1556 | public interface CheckCredentialProgressCallback { |
| 1557 | |
| 1558 | /** |
| 1559 | * Called as soon as possible when we know that the credentials match but the user hasn't |
| 1560 | * been fully unlocked. |
| 1561 | */ |
| 1562 | void onEarlyMatched(); |
| 1563 | } |
| 1564 | |
Victor Chang | a0940d3 | 2016-05-16 19:36:08 +0100 | [diff] [blame] | 1565 | /** |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1566 | * Tracks the global strong authentication state. |
| 1567 | */ |
| 1568 | public static class StrongAuthTracker { |
| 1569 | |
| 1570 | @IntDef(flag = true, |
| 1571 | value = { STRONG_AUTH_NOT_REQUIRED, |
| 1572 | STRONG_AUTH_REQUIRED_AFTER_BOOT, |
| 1573 | STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW, |
Adrian Roos | 9d6fc92 | 2016-08-10 17:09:55 -0700 | [diff] [blame] | 1574 | SOME_AUTH_REQUIRED_AFTER_USER_REQUEST, |
Michal Karpinski | c52f867 | 2016-11-18 11:32:45 +0000 | [diff] [blame] | 1575 | STRONG_AUTH_REQUIRED_AFTER_LOCKOUT, |
Chad Brubaker | 4f28f0d | 2017-09-07 14:28:13 -0700 | [diff] [blame] | 1576 | STRONG_AUTH_REQUIRED_AFTER_TIMEOUT, |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 1577 | STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN, |
| 1578 | STRONG_AUTH_REQUIRED_AFTER_NON_STRONG_BIOMETRICS_TIMEOUT}) |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1579 | @Retention(RetentionPolicy.SOURCE) |
| 1580 | public @interface StrongAuthFlags {} |
| 1581 | |
| 1582 | /** |
| 1583 | * Strong authentication is not required. |
| 1584 | */ |
| 1585 | public static final int STRONG_AUTH_NOT_REQUIRED = 0x0; |
| 1586 | |
| 1587 | /** |
| 1588 | * Strong authentication is required because the user has not authenticated since boot. |
| 1589 | */ |
| 1590 | public static final int STRONG_AUTH_REQUIRED_AFTER_BOOT = 0x1; |
| 1591 | |
| 1592 | /** |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1593 | * Strong authentication is required because a device admin has requested it. |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1594 | */ |
| 1595 | public static final int STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW = 0x2; |
| 1596 | |
| 1597 | /** |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1598 | * Some authentication is required because the user has temporarily disabled trust. |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1599 | */ |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1600 | public static final int SOME_AUTH_REQUIRED_AFTER_USER_REQUEST = 0x4; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1601 | |
Adrian Roos | 873010d | 2015-08-25 15:59:00 -0700 | [diff] [blame] | 1602 | /** |
| 1603 | * Strong authentication is required because the user has been locked out after too many |
| 1604 | * attempts. |
| 1605 | */ |
| 1606 | public static final int STRONG_AUTH_REQUIRED_AFTER_LOCKOUT = 0x8; |
| 1607 | |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1608 | /** |
Michal Karpinski | c52f867 | 2016-11-18 11:32:45 +0000 | [diff] [blame] | 1609 | * Strong authentication is required because it hasn't been used for a time required by |
| 1610 | * a device admin. |
| 1611 | */ |
| 1612 | public static final int STRONG_AUTH_REQUIRED_AFTER_TIMEOUT = 0x10; |
| 1613 | |
| 1614 | /** |
Chad Brubaker | 4f28f0d | 2017-09-07 14:28:13 -0700 | [diff] [blame] | 1615 | * Strong authentication is required because the user has triggered lockdown. |
| 1616 | */ |
| 1617 | public static final int STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN = 0x20; |
| 1618 | |
| 1619 | /** |
Kenny Root | f76cfc3 | 2019-11-08 14:36:03 -0800 | [diff] [blame] | 1620 | * Strong authentication is required to prepare for unattended upgrade. |
| 1621 | */ |
| 1622 | public static final int STRONG_AUTH_REQUIRED_FOR_UNATTENDED_UPDATE = 0x40; |
| 1623 | |
| 1624 | /** |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 1625 | * Strong authentication is required because it hasn't been used for a time after a |
| 1626 | * non-strong biometric (i.e. weak or convenience biometric) is used to unlock device. |
| 1627 | */ |
| 1628 | public static final int STRONG_AUTH_REQUIRED_AFTER_NON_STRONG_BIOMETRICS_TIMEOUT = 0x80; |
| 1629 | |
| 1630 | /** |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1631 | * Strong auth flags that do not prevent biometric methods from being accepted as auth. |
| 1632 | * If any other flags are set, biometric authentication is disabled. |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1633 | */ |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1634 | private static final int ALLOWING_BIOMETRIC = STRONG_AUTH_NOT_REQUIRED |
Adrian Roos | 9d6fc92 | 2016-08-10 17:09:55 -0700 | [diff] [blame] | 1635 | | SOME_AUTH_REQUIRED_AFTER_USER_REQUEST; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1636 | |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1637 | private final SparseIntArray mStrongAuthRequiredForUser = new SparseIntArray(); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1638 | private final H mHandler; |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1639 | private final int mDefaultStrongAuthFlags; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1640 | |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 1641 | private final SparseBooleanArray mIsNonStrongBiometricAllowedForUser = |
| 1642 | new SparseBooleanArray(); |
| 1643 | private final boolean mDefaultIsNonStrongBiometricAllowed = true; |
| 1644 | |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1645 | public StrongAuthTracker(Context context) { |
| 1646 | this(context, Looper.myLooper()); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /** |
| 1650 | * @param looper the looper on whose thread calls to {@link #onStrongAuthRequiredChanged} |
| 1651 | * will be scheduled. |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1652 | * @param context the current {@link Context} |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1653 | */ |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1654 | public StrongAuthTracker(Context context, Looper looper) { |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1655 | mHandler = new H(looper); |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1656 | mDefaultStrongAuthFlags = getDefaultFlags(context); |
| 1657 | } |
| 1658 | |
| 1659 | public static @StrongAuthFlags int getDefaultFlags(Context context) { |
| 1660 | boolean strongAuthRequired = context.getResources().getBoolean( |
| 1661 | com.android.internal.R.bool.config_strongAuthRequiredOnBoot); |
| 1662 | return strongAuthRequired ? STRONG_AUTH_REQUIRED_AFTER_BOOT : STRONG_AUTH_NOT_REQUIRED; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | /** |
| 1666 | * Returns {@link #STRONG_AUTH_NOT_REQUIRED} if strong authentication is not required, |
| 1667 | * otherwise returns a combination of {@link StrongAuthFlags} indicating why strong |
| 1668 | * authentication is required. |
| 1669 | * |
| 1670 | * @param userId the user for whom the state is queried. |
| 1671 | */ |
| 1672 | public @StrongAuthFlags int getStrongAuthForUser(int userId) { |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1673 | return mStrongAuthRequiredForUser.get(userId, mDefaultStrongAuthFlags); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | /** |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 1677 | * @return true if unlocking with trust alone is allowed for {@code userId} by the current |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1678 | * strong authentication requirements. |
| 1679 | */ |
| 1680 | public boolean isTrustAllowedForUser(int userId) { |
| 1681 | return getStrongAuthForUser(userId) == STRONG_AUTH_NOT_REQUIRED; |
| 1682 | } |
| 1683 | |
| 1684 | /** |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 1685 | * @return true if unlocking with a biometric method alone is allowed for {@code userId} |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1686 | * by the current strong authentication requirements. |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1687 | */ |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 1688 | public boolean isBiometricAllowedForUser(boolean isStrongBiometric, int userId) { |
| 1689 | boolean allowed = ((getStrongAuthForUser(userId) & ~ALLOWING_BIOMETRIC) == 0); |
| 1690 | if (!isStrongBiometric) { |
| 1691 | allowed &= isNonStrongBiometricAllowedAfterIdleTimeout(userId); |
| 1692 | } |
| 1693 | return allowed; |
| 1694 | } |
| 1695 | |
| 1696 | /** |
| 1697 | * @return true if unlocking with a non-strong (i.e. weak or convenience) biometric method |
| 1698 | * alone is allowed for {@code userId}, otherwise returns false. |
| 1699 | */ |
| 1700 | public boolean isNonStrongBiometricAllowedAfterIdleTimeout(int userId) { |
| 1701 | return mIsNonStrongBiometricAllowedForUser.get(userId, |
| 1702 | mDefaultIsNonStrongBiometricAllowed); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | /** |
Rubin Xu | a58125d | 2019-09-06 20:11:48 +0100 | [diff] [blame] | 1706 | * Called when the strong authentication requirements for {@code userId} changed. |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1707 | */ |
| 1708 | public void onStrongAuthRequiredChanged(int userId) { |
| 1709 | } |
| 1710 | |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 1711 | /** |
| 1712 | * Called when whether non-strong biometric is allowed for {@code userId} changed. |
| 1713 | */ |
| 1714 | public void onIsNonStrongBiometricAllowedChanged(int userId) { |
| 1715 | } |
| 1716 | |
Victor Chang | a0940d3 | 2016-05-16 19:36:08 +0100 | [diff] [blame] | 1717 | protected void handleStrongAuthRequiredChanged(@StrongAuthFlags int strongAuthFlags, |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1718 | int userId) { |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1719 | int oldValue = getStrongAuthForUser(userId); |
| 1720 | if (strongAuthFlags != oldValue) { |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 1721 | if (strongAuthFlags == mDefaultStrongAuthFlags) { |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1722 | mStrongAuthRequiredForUser.delete(userId); |
| 1723 | } else { |
| 1724 | mStrongAuthRequiredForUser.put(userId, strongAuthFlags); |
| 1725 | } |
| 1726 | onStrongAuthRequiredChanged(userId); |
| 1727 | } |
| 1728 | } |
| 1729 | |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 1730 | protected void handleIsNonStrongBiometricAllowedChanged(boolean allowed, |
| 1731 | int userId) { |
| 1732 | boolean oldValue = isNonStrongBiometricAllowedAfterIdleTimeout(userId); |
| 1733 | if (allowed != oldValue) { |
| 1734 | if (allowed == mDefaultIsNonStrongBiometricAllowed) { |
| 1735 | mIsNonStrongBiometricAllowedForUser.delete(userId); |
| 1736 | } else { |
| 1737 | mIsNonStrongBiometricAllowedForUser.put(userId, allowed); |
| 1738 | } |
| 1739 | onIsNonStrongBiometricAllowedChanged(userId); |
| 1740 | } |
| 1741 | } |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1742 | |
Victor Chang | a0940d3 | 2016-05-16 19:36:08 +0100 | [diff] [blame] | 1743 | protected final IStrongAuthTracker.Stub mStub = new IStrongAuthTracker.Stub() { |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1744 | @Override |
| 1745 | public void onStrongAuthRequiredChanged(@StrongAuthFlags int strongAuthFlags, |
| 1746 | int userId) { |
| 1747 | mHandler.obtainMessage(H.MSG_ON_STRONG_AUTH_REQUIRED_CHANGED, |
| 1748 | strongAuthFlags, userId).sendToTarget(); |
| 1749 | } |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 1750 | |
| 1751 | @Override |
| 1752 | public void onIsNonStrongBiometricAllowedChanged(boolean allowed, int userId) { |
| 1753 | mHandler.obtainMessage(H.MSG_ON_IS_NON_STRONG_BIOMETRIC_ALLOWED_CHANGED, |
| 1754 | allowed ? 1 : 0, userId).sendToTarget(); |
| 1755 | } |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1756 | }; |
| 1757 | |
| 1758 | private class H extends Handler { |
| 1759 | static final int MSG_ON_STRONG_AUTH_REQUIRED_CHANGED = 1; |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 1760 | static final int MSG_ON_IS_NON_STRONG_BIOMETRIC_ALLOWED_CHANGED = 2; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1761 | |
| 1762 | public H(Looper looper) { |
| 1763 | super(looper); |
| 1764 | } |
| 1765 | |
| 1766 | @Override |
| 1767 | public void handleMessage(Message msg) { |
| 1768 | switch (msg.what) { |
| 1769 | case MSG_ON_STRONG_AUTH_REQUIRED_CHANGED: |
| 1770 | handleStrongAuthRequiredChanged(msg.arg1, msg.arg2); |
| 1771 | break; |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 1772 | case MSG_ON_IS_NON_STRONG_BIOMETRIC_ALLOWED_CHANGED: |
| 1773 | handleIsNonStrongBiometricAllowedChanged(msg.arg1 == 1 /* allowed */, |
| 1774 | msg.arg2); |
| 1775 | break; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1776 | } |
| 1777 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | public void enableSyntheticPassword() { |
| 1782 | setLong(SYNTHETIC_PASSWORD_ENABLED_KEY, 1L, UserHandle.USER_SYSTEM); |
| 1783 | } |
| 1784 | |
Paul Crowley | 7a0cc0a | 2017-05-31 22:12:57 +0000 | [diff] [blame] | 1785 | public void disableSyntheticPassword() { |
| 1786 | setLong(SYNTHETIC_PASSWORD_ENABLED_KEY, 0L, UserHandle.USER_SYSTEM); |
| 1787 | } |
| 1788 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1789 | public boolean isSyntheticPasswordEnabled() { |
Rubin Xu | 5e891bc | 2019-10-14 10:22:23 +0100 | [diff] [blame] | 1790 | return getLong(SYNTHETIC_PASSWORD_ENABLED_KEY, SYNTHETIC_PASSWORD_ENABLED_BY_DEFAULT, |
| 1791 | UserHandle.USER_SYSTEM) != 0; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1792 | } |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1793 | |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 1794 | /** |
Rubin Xu | fc06773 | 2019-03-18 11:01:18 +0000 | [diff] [blame] | 1795 | * Returns whether the given user has pending escrow tokens |
| 1796 | */ |
| 1797 | public boolean hasPendingEscrowToken(int userId) { |
| 1798 | try { |
| 1799 | return getLockSettings().hasPendingEscrowToken(userId); |
| 1800 | } catch (RemoteException e) { |
| 1801 | e.rethrowFromSystemServer(); |
| 1802 | } |
| 1803 | return false; |
| 1804 | } |
| 1805 | |
| 1806 | /** |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 1807 | * Return true if the device supports the lock screen feature, false otherwise. |
| 1808 | */ |
| 1809 | public boolean hasSecureLockScreen() { |
| 1810 | if (mHasSecureLockScreen == null) { |
Rubin Xu | 3744acf | 2020-01-02 16:39:50 +0000 | [diff] [blame] | 1811 | try { |
| 1812 | mHasSecureLockScreen = Boolean.valueOf(getLockSettings().hasSecureLockScreen()); |
| 1813 | } catch (RemoteException e) { |
| 1814 | e.rethrowFromSystemServer(); |
| 1815 | } |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 1816 | } |
| 1817 | return mHasSecureLockScreen.booleanValue(); |
| 1818 | } |
| 1819 | |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 1820 | public static boolean userOwnsFrpCredential(Context context, UserInfo info) { |
| 1821 | return info != null && info.isPrimary() && info.isAdmin() && frpCredentialEnabled(context); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 1824 | public static boolean frpCredentialEnabled(Context context) { |
| 1825 | return FRP_CREDENTIAL_ENABLED && context.getResources().getBoolean( |
| 1826 | com.android.internal.R.bool.config_enableCredentialFactoryResetProtection); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 1827 | } |
Rubin Xu | a3c71a1 | 2019-12-04 15:25:02 +0000 | [diff] [blame] | 1828 | |
| 1829 | /** |
| 1830 | * Attempt to rederive the unified work challenge for the specified profile user and unlock the |
| 1831 | * user. If successful, this would allow the user to leave quiet mode automatically without |
| 1832 | * additional user authentication. |
| 1833 | * |
| 1834 | * This is made possible by the framework storing an encrypted copy of the unified challenge |
| 1835 | * auth-bound to the primary user's lockscreen. As long as the primery user has unlocked |
| 1836 | * recently (7 days), the framework will be able to decrypt it and plug the secret into the |
| 1837 | * unlock flow. |
| 1838 | * |
| 1839 | * @return {@code true} if automatic unlocking is successful, {@code false} otherwise. |
| 1840 | */ |
| 1841 | public boolean tryUnlockWithCachedUnifiedChallenge(int userId) { |
| 1842 | try { |
| 1843 | return getLockSettings().tryUnlockWithCachedUnifiedChallenge(userId); |
| 1844 | } catch (RemoteException re) { |
| 1845 | return false; |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | /** Remove cached unified profile challenge, for testing and CTS usage. */ |
| 1850 | public void removeCachedUnifiedChallenge(int userId) { |
| 1851 | try { |
| 1852 | getLockSettings().removeCachedUnifiedChallenge(userId); |
| 1853 | } catch (RemoteException re) { |
| 1854 | re.rethrowFromSystemServer(); |
| 1855 | } |
| 1856 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | } |