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 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Adrian Roos | d6aa6cb | 2015-04-16 19:31:29 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 21 | import android.app.admin.DevicePolicyManager; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 22 | import android.app.trust.IStrongAuthTracker; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 23 | import android.app.trust.TrustManager; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 24 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.ContentResolver; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 26 | import android.content.Context; |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 27 | import android.os.AsyncTask; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 28 | import android.os.Handler; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 29 | import android.os.IBinder; |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 30 | import android.os.Looper; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 31 | import android.os.Message; |
Jim Miller | 69ac988 | 2010-02-24 15:35:05 -0800 | [diff] [blame] | 32 | import android.os.RemoteException; |
| 33 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.SystemClock; |
Jim Miller | 6848dc8 | 2014-10-13 18:51:53 -0700 | [diff] [blame] | 35 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 36 | import android.os.UserHandle; |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 37 | import android.os.storage.IMountService; |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 38 | import android.os.storage.StorageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.text.TextUtils; |
| 41 | import android.util.Log; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 42 | import android.util.SparseIntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 44 | import com.google.android.collect.Lists; |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 45 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 46 | import java.lang.annotation.Retention; |
| 47 | import java.lang.annotation.RetentionPolicy; |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 48 | import java.nio.charset.StandardCharsets; |
Brian Carlstrom | 929a1c2 | 2011-02-01 21:54:09 -0800 | [diff] [blame] | 49 | import java.security.MessageDigest; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import java.security.NoSuchAlgorithmException; |
Jim Miller | 11b019d | 2010-01-20 16:34:45 -0800 | [diff] [blame] | 51 | import java.security.SecureRandom; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 52 | import java.util.ArrayList; |
| 53 | import java.util.Collection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import java.util.List; |
| 55 | |
Adrian Roos | c2e0168 | 2015-01-15 23:20:20 +0100 | [diff] [blame] | 56 | import libcore.util.HexEncoding; |
| 57 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | /** |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 59 | * Utilities for the lock pattern and its settings. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | */ |
| 61 | public class LockPatternUtils { |
| 62 | |
| 63 | private static final String TAG = "LockPatternUtils"; |
Brian Colonna | a0ee004 | 2014-07-16 13:50:47 -0400 | [diff] [blame] | 64 | private static final boolean DEBUG = false; |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 65 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | /** |
Bryce Lee | 4614596 | 2015-12-14 14:39:10 -0800 | [diff] [blame] | 67 | * The key to identify when the lock pattern enabled flag is being acccessed for legacy reasons. |
| 68 | */ |
| 69 | public static final String LEGACY_LOCK_PATTERN_ENABLED = "legacy_lock_pattern_enabled"; |
| 70 | |
| 71 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | * The number of incorrect attempts before which we fall back on an alternative |
| 73 | * method of verifying the user, and resetting their lock pattern. |
| 74 | */ |
| 75 | public static final int FAILED_ATTEMPTS_BEFORE_RESET = 20; |
| 76 | |
| 77 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | * The interval of the countdown for showing progress of the lockout. |
| 79 | */ |
| 80 | public static final long FAILED_ATTEMPT_COUNTDOWN_INTERVAL_MS = 1000L; |
| 81 | |
Jim Miller | 4f36995 | 2011-08-19 18:29:22 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * This dictates when we start telling the user that continued failed attempts will wipe |
| 85 | * their device. |
| 86 | */ |
| 87 | public static final int FAILED_ATTEMPTS_BEFORE_WIPE_GRACE = 5; |
| 88 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | /** |
| 90 | * The minimum number of dots in a valid pattern. |
| 91 | */ |
| 92 | public static final int MIN_LOCK_PATTERN_SIZE = 4; |
| 93 | |
| 94 | /** |
Adrian Roos | f80e66ce9 | 2015-01-15 23:27:24 +0100 | [diff] [blame] | 95 | * The minimum size of a valid password. |
| 96 | */ |
| 97 | public static final int MIN_LOCK_PASSWORD_SIZE = 4; |
| 98 | |
| 99 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | * The minimum number of dots the user must include in a wrong pattern |
| 101 | * attempt for it to be counted against the counts that affect |
| 102 | * {@link #FAILED_ATTEMPTS_BEFORE_TIMEOUT} and {@link #FAILED_ATTEMPTS_BEFORE_RESET} |
| 103 | */ |
Jim Miller | 4f36995 | 2011-08-19 18:29:22 -0700 | [diff] [blame] | 104 | 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] | 105 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 106 | @Deprecated |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 107 | public final static String LOCKOUT_PERMANENT_KEY = "lockscreen.lockedoutpermanently"; |
| 108 | public final static String LOCKOUT_ATTEMPT_DEADLINE = "lockscreen.lockoutattemptdeadline"; |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 109 | public final static String LOCKOUT_ATTEMPT_TIMEOUT_MS = "lockscreen.lockoutattempttimeoutmss"; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 110 | public final static String PATTERN_EVER_CHOSEN_KEY = "lockscreen.patterneverchosen"; |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 111 | public final static String PASSWORD_TYPE_KEY = "lockscreen.password_type"; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 112 | @Deprecated |
| 113 | public final static String PASSWORD_TYPE_ALTERNATE_KEY = "lockscreen.password_type_alternate"; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 114 | public final static String LOCK_PASSWORD_SALT_KEY = "lockscreen.password_salt"; |
| 115 | public final static String DISABLE_LOCKSCREEN_KEY = "lockscreen.disabled"; |
| 116 | public final static String LOCKSCREEN_OPTIONS = "lockscreen.options"; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 117 | @Deprecated |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 118 | public final static String LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK |
| 119 | = "lockscreen.biometric_weak_fallback"; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 120 | @Deprecated |
Danielle Millett | 7a07219 | 2011-10-03 17:36:01 -0400 | [diff] [blame] | 121 | public final static String BIOMETRIC_WEAK_EVER_CHOSEN_KEY |
| 122 | = "lockscreen.biometricweakeverchosen"; |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 123 | public final static String LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS |
| 124 | = "lockscreen.power_button_instantly_locks"; |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 125 | @Deprecated |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 126 | 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] | 127 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 128 | public final static String PASSWORD_HISTORY_KEY = "lockscreen.passwordhistory"; |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 129 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 130 | private static final String LOCK_SCREEN_OWNER_INFO = Settings.Secure.LOCK_SCREEN_OWNER_INFO; |
| 131 | private static final String LOCK_SCREEN_OWNER_INFO_ENABLED = |
| 132 | Settings.Secure.LOCK_SCREEN_OWNER_INFO_ENABLED; |
| 133 | |
Andrei Stingaceanu | 6644cd9 | 2015-11-10 13:03:31 +0000 | [diff] [blame] | 134 | private static final String LOCK_SCREEN_DEVICE_OWNER_INFO = "lockscreen.device_owner_info"; |
| 135 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 136 | private static final String ENABLED_TRUST_AGENTS = "lockscreen.enabledtrustagents"; |
| 137 | |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 138 | // Maximum allowed number of repeated or ordered characters in a sequence before we'll |
| 139 | // consider it a complex PIN/password. |
| 140 | public static final int MAX_ALLOWED_SEQUENCE = 3; |
| 141 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 142 | private final Context mContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | private final ContentResolver mContentResolver; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 144 | private DevicePolicyManager mDevicePolicyManager; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 145 | private ILockSettings mLockSettingsService; |
Jim Miller | ee82f8f | 2012-10-01 16:26:18 -0700 | [diff] [blame] | 146 | |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 147 | |
| 148 | public static final class RequestThrottledException extends Exception { |
| 149 | private int mTimeoutMs; |
| 150 | public RequestThrottledException(int timeoutMs) { |
| 151 | mTimeoutMs = timeoutMs; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @return The amount of time in ms before another request may |
| 156 | * be executed |
| 157 | */ |
| 158 | public int getTimeoutMs() { |
| 159 | return mTimeoutMs; |
| 160 | } |
| 161 | |
| 162 | } |
| 163 | |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 164 | public DevicePolicyManager getDevicePolicyManager() { |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 165 | if (mDevicePolicyManager == null) { |
| 166 | mDevicePolicyManager = |
| 167 | (DevicePolicyManager)mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 168 | if (mDevicePolicyManager == null) { |
| 169 | Log.e(TAG, "Can't get DevicePolicyManagerService: is it running?", |
| 170 | new IllegalStateException("Stack trace:")); |
| 171 | } |
| 172 | } |
| 173 | return mDevicePolicyManager; |
| 174 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 175 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 176 | private TrustManager getTrustManager() { |
| 177 | TrustManager trust = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE); |
| 178 | if (trust == null) { |
| 179 | Log.e(TAG, "Can't get TrustManagerService: is it running?", |
| 180 | new IllegalStateException("Stack trace:")); |
| 181 | } |
| 182 | return trust; |
| 183 | } |
| 184 | |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 185 | public LockPatternUtils(Context context) { |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 186 | mContext = context; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 187 | mContentResolver = context.getContentResolver(); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 188 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 189 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 190 | private ILockSettings getLockSettings() { |
| 191 | if (mLockSettingsService == null) { |
Adrian Roos | 450ce9f | 2014-10-29 14:30:37 +0100 | [diff] [blame] | 192 | ILockSettings service = ILockSettings.Stub.asInterface( |
| 193 | ServiceManager.getService("lock_settings")); |
Adrian Roos | 138b833 | 2014-11-11 13:51:07 +0100 | [diff] [blame] | 194 | mLockSettingsService = service; |
Brad Fitzpatrick | 9088100 | 2010-08-23 18:30:08 -0700 | [diff] [blame] | 195 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 196 | return mLockSettingsService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 199 | public int getRequestedMinimumPasswordLength(int userId) { |
| 200 | return getDevicePolicyManager().getPasswordMinimumLength(null, userId); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 203 | /** |
| 204 | * Gets the device policy password mode. If the mode is non-specific, returns |
| 205 | * MODE_PATTERN which allows the user to choose anything. |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 206 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 207 | public int getRequestedPasswordQuality(int userId) { |
| 208 | return getDevicePolicyManager().getPasswordQuality(null, userId); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | private int getRequestedPasswordHistoryLength(int userId) { |
| 212 | return getDevicePolicyManager().getPasswordHistoryLength(null, userId); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 215 | public int getRequestedPasswordMinimumLetters(int userId) { |
| 216 | return getDevicePolicyManager().getPasswordMinimumLetters(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 219 | public int getRequestedPasswordMinimumUpperCase(int userId) { |
| 220 | return getDevicePolicyManager().getPasswordMinimumUpperCase(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 223 | public int getRequestedPasswordMinimumLowerCase(int userId) { |
| 224 | return getDevicePolicyManager().getPasswordMinimumLowerCase(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 227 | public int getRequestedPasswordMinimumNumeric(int userId) { |
| 228 | return getDevicePolicyManager().getPasswordMinimumNumeric(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 231 | public int getRequestedPasswordMinimumSymbols(int userId) { |
| 232 | return getDevicePolicyManager().getPasswordMinimumSymbols(null, userId); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 235 | public int getRequestedPasswordMinimumNonLetter(int userId) { |
| 236 | return getDevicePolicyManager().getPasswordMinimumNonLetter(null, userId); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 237 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 238 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 239 | public void reportFailedPasswordAttempt(int userId) { |
Adrian Roos | 4f994eb | 2014-07-23 15:45:05 +0200 | [diff] [blame] | 240 | getDevicePolicyManager().reportFailedPasswordAttempt(userId); |
| 241 | getTrustManager().reportUnlockAttempt(false /* authenticated */, userId); |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 242 | requireStrongAuth(StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL, userId); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 245 | public void reportSuccessfulPasswordAttempt(int userId) { |
| 246 | getDevicePolicyManager().reportSuccessfulPasswordAttempt(userId); |
| 247 | getTrustManager().reportUnlockAttempt(true /* authenticated */, userId); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 248 | } |
| 249 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | /** |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 251 | * Check to see if a pattern matches the saved pattern. |
| 252 | * If pattern matches, return an opaque attestation that the challenge |
| 253 | * was verified. |
| 254 | * |
| 255 | * @param pattern The pattern to check. |
| 256 | * @param challenge The challenge to verify against the pattern |
| 257 | * @return the attestation that the challenge was verified, or null. |
| 258 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 259 | public byte[] verifyPattern(List<LockPatternView.Cell> pattern, long challenge, int userId) |
| 260 | throws RequestThrottledException { |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 261 | throwIfCalledOnMainThread(); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 262 | try { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 263 | VerifyCredentialResponse response = |
| 264 | getLockSettings().verifyPattern(patternToString(pattern), challenge, userId); |
| 265 | if (response == null) { |
| 266 | // Shouldn't happen |
| 267 | return null; |
| 268 | } |
| 269 | |
| 270 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 271 | return response.getPayload(); |
| 272 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 273 | throw new RequestThrottledException(response.getTimeout()); |
| 274 | } else { |
| 275 | return null; |
| 276 | } |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 277 | } catch (RemoteException re) { |
| 278 | return null; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | * Check to see if a pattern matches the saved pattern. If no pattern exists, |
| 284 | * always returns true. |
| 285 | * @param pattern The pattern to check. |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 286 | * @return Whether the pattern matches the stored one. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 288 | public boolean checkPattern(List<LockPatternView.Cell> pattern, int userId) |
| 289 | throws RequestThrottledException { |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 290 | throwIfCalledOnMainThread(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | try { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 292 | VerifyCredentialResponse response = |
| 293 | getLockSettings().checkPattern(patternToString(pattern), userId); |
| 294 | |
| 295 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 296 | return true; |
| 297 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 298 | throw new RequestThrottledException(response.getTimeout()); |
| 299 | } else { |
| 300 | return false; |
| 301 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 302 | } catch (RemoteException re) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | return true; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 308 | * Check to see if a password matches the saved password. |
| 309 | * If password matches, return an opaque attestation that the challenge |
| 310 | * was verified. |
| 311 | * |
| 312 | * @param password The password to check. |
| 313 | * @param challenge The challenge to verify against the password |
| 314 | * @return the attestation that the challenge was verified, or null. |
| 315 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 316 | public byte[] verifyPassword(String password, long challenge, int userId) |
| 317 | throws RequestThrottledException { |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 318 | throwIfCalledOnMainThread(); |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 319 | try { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 320 | VerifyCredentialResponse response = |
| 321 | getLockSettings().verifyPassword(password, challenge, userId); |
| 322 | |
| 323 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 324 | return response.getPayload(); |
| 325 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 326 | throw new RequestThrottledException(response.getTimeout()); |
| 327 | } else { |
| 328 | return null; |
| 329 | } |
Andres Morales | d9fc85a | 2015-04-09 19:14:42 -0700 | [diff] [blame] | 330 | } catch (RemoteException re) { |
| 331 | return null; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 336 | * Check to see if a password matches the saved password. If no password exists, |
| 337 | * always returns true. |
| 338 | * @param password The password to check. |
| 339 | * @return Whether the password matches the stored one. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 341 | public boolean checkPassword(String password, int userId) throws RequestThrottledException { |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 342 | throwIfCalledOnMainThread(); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 343 | try { |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 344 | VerifyCredentialResponse response = |
| 345 | getLockSettings().checkPassword(password, userId); |
| 346 | if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) { |
| 347 | return true; |
| 348 | } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) { |
| 349 | throw new RequestThrottledException(response.getTimeout()); |
| 350 | } else { |
| 351 | return false; |
| 352 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 353 | } catch (RemoteException re) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 354 | return true; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 359 | * Check to see if vold already has the password. |
| 360 | * Note that this also clears vold's copy of the password. |
| 361 | * @return Whether the vold password matches or not. |
| 362 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 363 | public boolean checkVoldPassword(int userId) { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 364 | try { |
| 365 | return getLockSettings().checkVoldPassword(userId); |
| 366 | } catch (RemoteException re) { |
| 367 | return false; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 372 | * Check to see if a password matches any of the passwords stored in the |
| 373 | * password history. |
| 374 | * |
| 375 | * @param password The password to check. |
| 376 | * @return Whether the password matches any in the history. |
| 377 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 378 | public boolean checkPasswordHistory(String password, int userId) { |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 379 | String passwordHashString = new String( |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 380 | passwordToHash(password, userId), StandardCharsets.UTF_8); |
| 381 | String passwordHistory = getString(PASSWORD_HISTORY_KEY, userId); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 382 | if (passwordHistory == null) { |
| 383 | return false; |
| 384 | } |
| 385 | // Password History may be too long... |
| 386 | int passwordHashLength = passwordHashString.length(); |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 387 | int passwordHistoryLength = getRequestedPasswordHistoryLength(userId); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 388 | if(passwordHistoryLength == 0) { |
| 389 | return false; |
| 390 | } |
| 391 | int neededPasswordHistoryLength = passwordHashLength * passwordHistoryLength |
| 392 | + passwordHistoryLength - 1; |
| 393 | if (passwordHistory.length() > neededPasswordHistoryLength) { |
| 394 | passwordHistory = passwordHistory.substring(0, neededPasswordHistoryLength); |
| 395 | } |
| 396 | return passwordHistory.contains(passwordHashString); |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /** |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 400 | * Check to see if the user has stored a lock pattern. |
| 401 | * @return Whether a saved pattern exists. |
| 402 | */ |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 403 | private boolean savedPatternExists(int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 404 | try { |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 405 | return getLockSettings().havePattern(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 406 | } catch (RemoteException re) { |
| 407 | return false; |
| 408 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Check to see if the user has stored a lock pattern. |
| 413 | * @return Whether a saved pattern exists. |
| 414 | */ |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 415 | private boolean savedPasswordExists(int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 416 | try { |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 417 | return getLockSettings().havePassword(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 418 | } catch (RemoteException re) { |
| 419 | return false; |
| 420 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /** |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 424 | * Return true if the user has ever chosen a pattern. This is true even if the pattern is |
| 425 | * currently cleared. |
| 426 | * |
| 427 | * @return True if the user has ever chosen a pattern. |
| 428 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 429 | public boolean isPatternEverChosen(int userId) { |
| 430 | return getBoolean(PATTERN_EVER_CHOSEN_KEY, false, userId); |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Used by device policy manager to validate the current password |
| 435 | * information it has. |
| 436 | */ |
| 437 | public int getActivePasswordQuality(int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 438 | int quality = getKeyguardStoredPasswordQuality(userId); |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 439 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 440 | if (isLockPasswordEnabled(quality, userId)) { |
| 441 | // Quality is a password and a password exists. Return the quality. |
| 442 | return quality; |
| 443 | } |
| 444 | |
| 445 | if (isLockPatternEnabled(quality, userId)) { |
| 446 | // Quality is a pattern and a pattern exists. Return the quality. |
| 447 | return quality; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 448 | } |
Danielle Millett | c8fb532 | 2011-10-04 12:18:51 -0400 | [diff] [blame] | 449 | |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 450 | return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 451 | } |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 452 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 453 | /** |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 454 | * Clear any lock pattern or password. |
| 455 | */ |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 456 | public void clearLock(int userHandle) { |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 457 | setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, userHandle); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 458 | |
| 459 | try { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 460 | getLockSettings().setLockPassword(null, null, userHandle); |
| 461 | getLockSettings().setLockPattern(null, null, userHandle); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 462 | } catch (RemoteException e) { |
| 463 | // well, we tried... |
| 464 | } |
| 465 | |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 466 | if (userHandle == UserHandle.USER_SYSTEM) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 467 | // Set the encryption password to default. |
| 468 | updateEncryptionPassword(StorageManager.CRYPT_TYPE_DEFAULT, null); |
| 469 | } |
| 470 | |
Wen ZHANG | 95bbbdd | 2015-04-15 19:32:06 +0100 | [diff] [blame] | 471 | setCredentialRequiredToDecrypt(false); |
| 472 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 473 | getDevicePolicyManager().setActivePasswordState( |
| 474 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, 0, 0, 0, 0, 0, 0, 0, userHandle); |
| 475 | |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 476 | onAfterChangingPassword(userHandle); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 477 | } |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 478 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 479 | /** |
Benjamin Franz | 51ed794 | 2015-04-07 16:34:56 +0100 | [diff] [blame] | 480 | * Disable showing lock screen at all for a given user. |
| 481 | * This is only meaningful if pattern, pin or password are not set. |
Jim Miller | 2a98a4c | 2010-11-19 18:49:26 -0800 | [diff] [blame] | 482 | * |
Benjamin Franz | 51ed794 | 2015-04-07 16:34:56 +0100 | [diff] [blame] | 483 | * @param disable Disables lock screen when true |
| 484 | * @param userId User ID of the user this has effect on |
| 485 | */ |
| 486 | public void setLockScreenDisabled(boolean disable, int userId) { |
| 487 | setBoolean(DISABLE_LOCKSCREEN_KEY, disable, userId); |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Determine if LockScreen is disabled for the current user. This is used to decide whether |
| 492 | * LockScreen is shown after reboot or after screen timeout / short press on power. |
| 493 | * |
| 494 | * @return true if lock screen is disabled |
Jim Miller | 2a98a4c | 2010-11-19 18:49:26 -0800 | [diff] [blame] | 495 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 496 | public boolean isLockScreenDisabled(int userId) { |
| 497 | return !isSecure(userId) && |
| 498 | getBoolean(DISABLE_LOCKSCREEN_KEY, false, userId); |
Jim Miller | 2a98a4c | 2010-11-19 18:49:26 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /** |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 502 | * Save a lock pattern. |
| 503 | * @param pattern The new pattern to save. |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 504 | * @param userId the user whose pattern is to be saved. |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 505 | */ |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 506 | public void saveLockPattern(List<LockPatternView.Cell> pattern, int userId) { |
| 507 | this.saveLockPattern(pattern, null, userId); |
| 508 | } |
Danielle Millett | 2364a22 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 509 | /** |
| 510 | * Save a lock pattern. |
| 511 | * @param pattern The new pattern to save. |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 512 | * @param savedPattern The previously saved pattern, converted to String format |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 513 | * @param userId the user whose pattern is to be saved. |
| 514 | */ |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 515 | public void saveLockPattern(List<LockPatternView.Cell> pattern, String savedPattern, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | try { |
Adrian Roos | f80e66ce9 | 2015-01-15 23:27:24 +0100 | [diff] [blame] | 517 | if (pattern == null || pattern.size() < MIN_LOCK_PATTERN_SIZE) { |
| 518 | throw new IllegalArgumentException("pattern must not be null and at least " |
| 519 | + MIN_LOCK_PATTERN_SIZE + " dots long."); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 520 | } |
| 521 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 522 | getLockSettings().setLockPattern(patternToString(pattern), savedPattern, userId); |
Dianne Hackborn | 2509d3c | 2010-03-08 12:54:25 -0800 | [diff] [blame] | 523 | DevicePolicyManager dpm = getDevicePolicyManager(); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 524 | |
| 525 | // Update the device encryption password. |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 526 | if (userId == UserHandle.USER_SYSTEM |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 527 | && LockPatternUtils.isDeviceEncryptionEnabled()) { |
Andrei Kapishnikov | 4eb6a36 | 2015-04-02 15:21:20 -0400 | [diff] [blame] | 528 | if (!shouldEncryptWithCredentials(true)) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 529 | clearEncryptionPassword(); |
| 530 | } else { |
| 531 | String stringPattern = patternToString(pattern); |
| 532 | updateEncryptionPassword(StorageManager.CRYPT_TYPE_PATTERN, stringPattern); |
Paul Lawrence | 8e39736 | 2014-01-27 15:22:30 -0800 | [diff] [blame] | 533 | } |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 534 | } |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 535 | |
| 536 | setBoolean(PATTERN_EVER_CHOSEN_KEY, true, userId); |
| 537 | |
| 538 | setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING, userId); |
| 539 | dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING, |
| 540 | pattern.size(), 0, 0, 0, 0, 0, 0, userId); |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 541 | onAfterChangingPassword(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 542 | } catch (RemoteException re) { |
| 543 | Log.e(TAG, "Couldn't save lock pattern " + re); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 547 | private void updateCryptoUserInfo(int userId) { |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 548 | if (userId != UserHandle.USER_SYSTEM) { |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 549 | return; |
| 550 | } |
| 551 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 552 | final String ownerInfo = isOwnerInfoEnabled(userId) ? getOwnerInfo(userId) : ""; |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 553 | |
| 554 | IBinder service = ServiceManager.getService("mount"); |
| 555 | if (service == null) { |
| 556 | Log.e(TAG, "Could not find the mount service to update the user info"); |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | IMountService mountService = IMountService.Stub.asInterface(service); |
| 561 | try { |
| 562 | Log.d(TAG, "Setting owner info"); |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 563 | mountService.setField(StorageManager.OWNER_INFO_KEY, ownerInfo); |
Paul Lawrence | e51dcf9 | 2014-03-18 10:56:00 -0700 | [diff] [blame] | 564 | } catch (RemoteException e) { |
| 565 | Log.e(TAG, "Error changing user info", e); |
| 566 | } |
| 567 | } |
| 568 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 569 | public void setOwnerInfo(String info, int userId) { |
| 570 | setString(LOCK_SCREEN_OWNER_INFO, info, userId); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 571 | updateCryptoUserInfo(userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 574 | public void setOwnerInfoEnabled(boolean enabled, int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 575 | setBoolean(LOCK_SCREEN_OWNER_INFO_ENABLED, enabled, userId); |
| 576 | updateCryptoUserInfo(userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | public String getOwnerInfo(int userId) { |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 580 | return getString(LOCK_SCREEN_OWNER_INFO, userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 583 | public boolean isOwnerInfoEnabled(int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 584 | return getBoolean(LOCK_SCREEN_OWNER_INFO_ENABLED, false, userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 585 | } |
| 586 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | /** |
Andrei Stingaceanu | 6644cd9 | 2015-11-10 13:03:31 +0000 | [diff] [blame] | 588 | * Sets the device owner information. If the information is {@code null} or empty then the |
| 589 | * device owner info is cleared. |
| 590 | * |
| 591 | * @param info Device owner information which will be displayed instead of the user |
| 592 | * owner info. |
| 593 | */ |
| 594 | public void setDeviceOwnerInfo(String info) { |
| 595 | if (info != null && info.isEmpty()) { |
| 596 | info = null; |
| 597 | } |
| 598 | |
| 599 | setString(LOCK_SCREEN_DEVICE_OWNER_INFO, info, UserHandle.USER_SYSTEM); |
| 600 | } |
| 601 | |
| 602 | public String getDeviceOwnerInfo() { |
| 603 | return getString(LOCK_SCREEN_DEVICE_OWNER_INFO, UserHandle.USER_SYSTEM); |
| 604 | } |
| 605 | |
| 606 | public boolean isDeviceOwnerInfoEnabled() { |
| 607 | return getDeviceOwnerInfo() != null; |
| 608 | } |
| 609 | |
| 610 | /** |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 611 | * Compute the password quality from the given password string. |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 612 | */ |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 613 | static public int computePasswordQuality(String password) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 614 | boolean hasDigit = false; |
| 615 | boolean hasNonDigit = false; |
| 616 | final int len = password.length(); |
| 617 | for (int i = 0; i < len; i++) { |
| 618 | if (Character.isDigit(password.charAt(i))) { |
| 619 | hasDigit = true; |
| 620 | } else { |
| 621 | hasNonDigit = true; |
| 622 | } |
| 623 | } |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 624 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 625 | if (hasNonDigit && hasDigit) { |
| 626 | return DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC; |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 627 | } |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 628 | if (hasNonDigit) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 629 | return DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC; |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 630 | } |
| 631 | if (hasDigit) { |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 632 | return maxLengthSequence(password) > MAX_ALLOWED_SEQUENCE |
| 633 | ? DevicePolicyManager.PASSWORD_QUALITY_NUMERIC |
| 634 | : DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX; |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 635 | } |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 636 | return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 637 | } |
Jim Miller | 5b0fb3a | 2010-02-23 13:46:35 -0800 | [diff] [blame] | 638 | |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 639 | private static int categoryChar(char c) { |
| 640 | if ('a' <= c && c <= 'z') return 0; |
| 641 | if ('A' <= c && c <= 'Z') return 1; |
| 642 | if ('0' <= c && c <= '9') return 2; |
| 643 | return 3; |
| 644 | } |
| 645 | |
| 646 | private static int maxDiffCategory(int category) { |
| 647 | if (category == 0 || category == 1) return 1; |
| 648 | else if (category == 2) return 10; |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Returns the maximum length of a sequential characters. A sequence is defined as |
| 654 | * monotonically increasing characters with a constant interval or the same character repeated. |
| 655 | * |
| 656 | * For example: |
| 657 | * maxLengthSequence("1234") == 4 |
| 658 | * maxLengthSequence("1234abc") == 4 |
| 659 | * maxLengthSequence("aabc") == 3 |
| 660 | * maxLengthSequence("qwertyuio") == 1 |
| 661 | * maxLengthSequence("@ABC") == 3 |
| 662 | * maxLengthSequence(";;;;") == 4 (anything that repeats) |
| 663 | * maxLengthSequence(":;<=>") == 1 (ordered, but not composed of alphas or digits) |
| 664 | * |
| 665 | * @param string the pass |
| 666 | * @return the number of sequential letters or digits |
| 667 | */ |
| 668 | public static int maxLengthSequence(String string) { |
| 669 | if (string.length() == 0) return 0; |
| 670 | char previousChar = string.charAt(0); |
| 671 | int category = categoryChar(previousChar); //current category of the sequence |
| 672 | int diff = 0; //difference between two consecutive characters |
| 673 | boolean hasDiff = false; //if we are currently targeting a sequence |
| 674 | int maxLength = 0; //maximum length of a sequence already found |
| 675 | int startSequence = 0; //where the current sequence started |
| 676 | for (int current = 1; current < string.length(); current++) { |
| 677 | char currentChar = string.charAt(current); |
| 678 | int categoryCurrent = categoryChar(currentChar); |
| 679 | int currentDiff = (int) currentChar - (int) previousChar; |
| 680 | if (categoryCurrent != category || Math.abs(currentDiff) > maxDiffCategory(category)) { |
| 681 | maxLength = Math.max(maxLength, current - startSequence); |
| 682 | startSequence = current; |
| 683 | hasDiff = false; |
| 684 | category = categoryCurrent; |
| 685 | } |
| 686 | else { |
| 687 | if(hasDiff && currentDiff != diff) { |
| 688 | maxLength = Math.max(maxLength, current - startSequence); |
| 689 | startSequence = current - 1; |
| 690 | } |
| 691 | diff = currentDiff; |
| 692 | hasDiff = true; |
| 693 | } |
| 694 | previousChar = currentChar; |
| 695 | } |
| 696 | maxLength = Math.max(maxLength, string.length() - startSequence); |
| 697 | return maxLength; |
| 698 | } |
| 699 | |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 700 | /** Update the encryption password if it is enabled **/ |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 701 | private void updateEncryptionPassword(final int type, final String password) { |
Amith Yamasani | cd410ba | 2014-10-17 11:16:58 -0700 | [diff] [blame] | 702 | if (!isDeviceEncryptionEnabled()) { |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 703 | return; |
| 704 | } |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 705 | final IBinder service = ServiceManager.getService("mount"); |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 706 | if (service == null) { |
| 707 | Log.e(TAG, "Could not find the mount service to update the encryption password"); |
| 708 | return; |
| 709 | } |
| 710 | |
Paul Lawrence | 3a5a0be | 2014-09-25 09:26:34 -0700 | [diff] [blame] | 711 | new AsyncTask<Void, Void, Void>() { |
| 712 | @Override |
| 713 | protected Void doInBackground(Void... dummy) { |
| 714 | IMountService mountService = IMountService.Stub.asInterface(service); |
| 715 | try { |
| 716 | mountService.changeEncryptionPassword(type, password); |
| 717 | } catch (RemoteException e) { |
| 718 | Log.e(TAG, "Error changing encryption password", e); |
| 719 | } |
| 720 | return null; |
| 721 | } |
| 722 | }.execute(); |
Jason parks | f7b3cd4 | 2011-01-27 09:28:25 -0600 | [diff] [blame] | 723 | } |
| 724 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 725 | /** |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 726 | * Save a lock password. Does not ensure that the password is as good |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 727 | * as the requested mode, but will adjust the mode to be as good as the |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 728 | * password. |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 729 | * @param password The password to save |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 730 | * @param savedPassword The previously saved lock password, or null if none |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 731 | * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)} |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 732 | * @param userHandle The userId of the user to change the password for |
| 733 | */ |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 734 | public void saveLockPassword(String password, String savedPassword, int quality, |
| 735 | int userHandle) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 736 | try { |
Dianne Hackborn | 2509d3c | 2010-03-08 12:54:25 -0800 | [diff] [blame] | 737 | DevicePolicyManager dpm = getDevicePolicyManager(); |
Adrian Roos | f80e66ce9 | 2015-01-15 23:27:24 +0100 | [diff] [blame] | 738 | if (password == null || password.length() < MIN_LOCK_PASSWORD_SIZE) { |
| 739 | throw new IllegalArgumentException("password must not be null and at least " |
| 740 | + "of length " + MIN_LOCK_PASSWORD_SIZE); |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 741 | } |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 742 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 743 | getLockSettings().setLockPassword(password, savedPassword, userHandle); |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 744 | int computedQuality = computePasswordQuality(password); |
| 745 | |
| 746 | // Update the device encryption password. |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 747 | if (userHandle == UserHandle.USER_SYSTEM |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 748 | && LockPatternUtils.isDeviceEncryptionEnabled()) { |
Andrei Kapishnikov | 4eb6a36 | 2015-04-02 15:21:20 -0400 | [diff] [blame] | 749 | if (!shouldEncryptWithCredentials(true)) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 750 | clearEncryptionPassword(); |
| 751 | } else { |
| 752 | boolean numeric = computedQuality |
| 753 | == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC; |
| 754 | boolean numericComplex = computedQuality |
| 755 | == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX; |
| 756 | int type = numeric || numericComplex ? StorageManager.CRYPT_TYPE_PIN |
| 757 | : StorageManager.CRYPT_TYPE_PASSWORD; |
| 758 | updateEncryptionPassword(type, password); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | setLong(PASSWORD_TYPE_KEY, Math.max(quality, computedQuality), userHandle); |
| 763 | if (computedQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
| 764 | int letters = 0; |
| 765 | int uppercase = 0; |
| 766 | int lowercase = 0; |
| 767 | int numbers = 0; |
| 768 | int symbols = 0; |
| 769 | int nonletter = 0; |
| 770 | for (int i = 0; i < password.length(); i++) { |
| 771 | char c = password.charAt(i); |
| 772 | if (c >= 'A' && c <= 'Z') { |
| 773 | letters++; |
| 774 | uppercase++; |
| 775 | } else if (c >= 'a' && c <= 'z') { |
| 776 | letters++; |
| 777 | lowercase++; |
| 778 | } else if (c >= '0' && c <= '9') { |
| 779 | numbers++; |
| 780 | nonletter++; |
| 781 | } else { |
| 782 | symbols++; |
| 783 | nonletter++; |
| 784 | } |
| 785 | } |
| 786 | dpm.setActivePasswordState(Math.max(quality, computedQuality), |
| 787 | password.length(), letters, uppercase, lowercase, |
| 788 | numbers, symbols, nonletter, userHandle); |
| 789 | } else { |
| 790 | // The password is not anything. |
| 791 | dpm.setActivePasswordState( |
| 792 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 793 | 0, 0, 0, 0, 0, 0, 0, userHandle); |
| 794 | } |
| 795 | |
| 796 | // Add the password to the password history. We assume all |
| 797 | // password hashes have the same length for simplicity of implementation. |
| 798 | String passwordHistory = getString(PASSWORD_HISTORY_KEY, userHandle); |
| 799 | if (passwordHistory == null) { |
| 800 | passwordHistory = ""; |
| 801 | } |
| 802 | int passwordHistoryLength = getRequestedPasswordHistoryLength(userHandle); |
| 803 | if (passwordHistoryLength == 0) { |
| 804 | passwordHistory = ""; |
| 805 | } else { |
| 806 | byte[] hash = passwordToHash(password, userHandle); |
| 807 | passwordHistory = new String(hash, StandardCharsets.UTF_8) + "," + passwordHistory; |
| 808 | // Cut it to contain passwordHistoryLength hashes |
| 809 | // and passwordHistoryLength -1 commas. |
| 810 | passwordHistory = passwordHistory.substring(0, Math.min(hash.length |
| 811 | * passwordHistoryLength + passwordHistoryLength - 1, passwordHistory |
| 812 | .length())); |
| 813 | } |
| 814 | setString(PASSWORD_HISTORY_KEY, passwordHistory, userHandle); |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 815 | onAfterChangingPassword(userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 816 | } catch (RemoteException re) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 817 | // Cant do much |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 818 | Log.e(TAG, "Unable to save lock password " + re); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
Jim Miller | cd70988 | 2010-03-25 18:24:02 -0700 | [diff] [blame] | 822 | /** |
Svetoslav | 16e4a1a | 2014-09-29 18:16:20 -0700 | [diff] [blame] | 823 | * Gets whether the device is encrypted. |
| 824 | * |
| 825 | * @return Whether the device is encrypted. |
| 826 | */ |
| 827 | public static boolean isDeviceEncrypted() { |
| 828 | IMountService mountService = IMountService.Stub.asInterface( |
| 829 | ServiceManager.getService("mount")); |
| 830 | try { |
| 831 | return mountService.getEncryptionState() != IMountService.ENCRYPTION_STATE_NONE |
| 832 | && mountService.getPasswordType() != StorageManager.CRYPT_TYPE_DEFAULT; |
| 833 | } catch (RemoteException re) { |
| 834 | Log.e(TAG, "Error getting encryption state", re); |
| 835 | } |
| 836 | return true; |
| 837 | } |
| 838 | |
| 839 | /** |
Jim Miller | 6848dc8 | 2014-10-13 18:51:53 -0700 | [diff] [blame] | 840 | * Determine if the device supports encryption, even if it's set to default. This |
| 841 | * differs from isDeviceEncrypted() in that it returns true even if the device is |
| 842 | * encrypted with the default password. |
| 843 | * @return true if device encryption is enabled |
| 844 | */ |
| 845 | public static boolean isDeviceEncryptionEnabled() { |
| 846 | final String status = SystemProperties.get("ro.crypto.state", "unsupported"); |
| 847 | return "encrypted".equalsIgnoreCase(status); |
| 848 | } |
| 849 | |
| 850 | /** |
Svetoslav | 16e4a1a | 2014-09-29 18:16:20 -0700 | [diff] [blame] | 851 | * Clears the encryption password. |
| 852 | */ |
| 853 | public void clearEncryptionPassword() { |
| 854 | updateEncryptionPassword(StorageManager.CRYPT_TYPE_DEFAULT, null); |
| 855 | } |
| 856 | |
| 857 | /** |
Adrian Roos | 1572ee3 | 2014-09-01 16:24:32 +0200 | [diff] [blame] | 858 | * Retrieves the quality mode for {@param userHandle}. |
| 859 | * {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)} |
| 860 | * |
| 861 | * @return stored password quality |
| 862 | */ |
| 863 | public int getKeyguardStoredPasswordQuality(int userHandle) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 864 | return (int) getLong(PASSWORD_TYPE_KEY, |
Adrian Roos | 1572ee3 | 2014-09-01 16:24:32 +0200 | [diff] [blame] | 865 | DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, userHandle); |
Jim Miller | 6edf263 | 2011-09-05 16:03:14 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Danielle Millett | 5839698 | 2011-09-30 13:55:07 -0400 | [diff] [blame] | 868 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | * Deserialize a pattern. |
| 870 | * @param string The pattern serialized with {@link #patternToString} |
| 871 | * @return The pattern. |
| 872 | */ |
| 873 | public static List<LockPatternView.Cell> stringToPattern(String string) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 874 | if (string == null) { |
| 875 | return null; |
| 876 | } |
| 877 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | List<LockPatternView.Cell> result = Lists.newArrayList(); |
| 879 | |
| 880 | final byte[] bytes = string.getBytes(); |
| 881 | for (int i = 0; i < bytes.length; i++) { |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 882 | byte b = (byte) (bytes[i] - '1'); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | result.add(LockPatternView.Cell.of(b / 3, b % 3)); |
| 884 | } |
| 885 | return result; |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * Serialize a pattern. |
| 890 | * @param pattern The pattern. |
| 891 | * @return The pattern in string form. |
| 892 | */ |
| 893 | public static String patternToString(List<LockPatternView.Cell> pattern) { |
| 894 | if (pattern == null) { |
| 895 | return ""; |
| 896 | } |
| 897 | final int patternSize = pattern.size(); |
| 898 | |
| 899 | byte[] res = new byte[patternSize]; |
| 900 | for (int i = 0; i < patternSize; i++) { |
| 901 | LockPatternView.Cell cell = pattern.get(i); |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 902 | res[i] = (byte) (cell.getRow() * 3 + cell.getColumn() + '1'); |
| 903 | } |
| 904 | return new String(res); |
| 905 | } |
| 906 | |
| 907 | public static String patternStringToBaseZero(String pattern) { |
| 908 | if (pattern == null) { |
| 909 | return ""; |
| 910 | } |
| 911 | final int patternSize = pattern.length(); |
| 912 | |
| 913 | byte[] res = new byte[patternSize]; |
| 914 | final byte[] bytes = pattern.getBytes(); |
| 915 | for (int i = 0; i < patternSize; i++) { |
| 916 | res[i] = (byte) (bytes[i] - '1'); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | } |
| 918 | return new String(res); |
| 919 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 920 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | /* |
| 922 | * Generate an SHA-1 hash for the pattern. Not the most secure, but it is |
| 923 | * at least a second level of protection. First level is that the file |
| 924 | * is in a location only readable by the system process. |
| 925 | * @param pattern the gesture pattern. |
| 926 | * @return the hash of the pattern in a byte array. |
| 927 | */ |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 928 | public static byte[] patternToHash(List<LockPatternView.Cell> pattern) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | if (pattern == null) { |
| 930 | return null; |
| 931 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 932 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | final int patternSize = pattern.size(); |
| 934 | byte[] res = new byte[patternSize]; |
| 935 | for (int i = 0; i < patternSize; i++) { |
| 936 | LockPatternView.Cell cell = pattern.get(i); |
| 937 | res[i] = (byte) (cell.getRow() * 3 + cell.getColumn()); |
| 938 | } |
| 939 | try { |
| 940 | MessageDigest md = MessageDigest.getInstance("SHA-1"); |
| 941 | byte[] hash = md.digest(res); |
| 942 | return hash; |
| 943 | } catch (NoSuchAlgorithmException nsa) { |
| 944 | return res; |
| 945 | } |
| 946 | } |
| 947 | |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 948 | private String getSalt(int userId) { |
| 949 | long salt = getLong(LOCK_PASSWORD_SALT_KEY, 0, userId); |
Jim Miller | 11b019d | 2010-01-20 16:34:45 -0800 | [diff] [blame] | 950 | if (salt == 0) { |
| 951 | try { |
| 952 | salt = SecureRandom.getInstance("SHA1PRNG").nextLong(); |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 953 | setLong(LOCK_PASSWORD_SALT_KEY, salt, userId); |
| 954 | Log.v(TAG, "Initialized lock password salt for user: " + userId); |
Jim Miller | 11b019d | 2010-01-20 16:34:45 -0800 | [diff] [blame] | 955 | } catch (NoSuchAlgorithmException e) { |
| 956 | // Throw an exception rather than storing a password we'll never be able to recover |
| 957 | throw new IllegalStateException("Couldn't get SecureRandom number", e); |
| 958 | } |
| 959 | } |
| 960 | return Long.toHexString(salt); |
| 961 | } |
| 962 | |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 963 | /* |
| 964 | * Generate a hash for the given password. To avoid brute force attacks, we use a salted hash. |
| 965 | * Not the most secure, but it is at least a second level of protection. First level is that |
| 966 | * the file is in a location only readable by the system process. |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 967 | * |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 968 | * @param password the gesture pattern. |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 969 | * |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 970 | * @return the hash of the pattern in a byte array. |
| 971 | */ |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 972 | public byte[] passwordToHash(String password, int userId) { |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 973 | if (password == null) { |
| 974 | return null; |
| 975 | } |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 976 | |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 977 | try { |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 978 | byte[] saltedPassword = (password + getSalt(userId)).getBytes(); |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 979 | byte[] sha1 = MessageDigest.getInstance("SHA-1").digest(saltedPassword); |
| 980 | byte[] md5 = MessageDigest.getInstance("MD5").digest(saltedPassword); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 981 | |
Narayan Kamath | 78108a3 | 2014-12-16 12:56:23 +0000 | [diff] [blame] | 982 | byte[] combined = new byte[sha1.length + md5.length]; |
| 983 | System.arraycopy(sha1, 0, combined, 0, sha1.length); |
| 984 | System.arraycopy(md5, 0, combined, sha1.length, md5.length); |
| 985 | |
| 986 | final char[] hexEncoded = HexEncoding.encode(combined); |
| 987 | return new String(hexEncoded).getBytes(StandardCharsets.UTF_8); |
| 988 | } catch (NoSuchAlgorithmException e) { |
| 989 | throw new AssertionError("Missing digest algorithm: ", e); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 990 | } |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /** |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 994 | * @param userId the user for which to report the value |
| 995 | * @return Whether the lock screen is secured. |
| 996 | */ |
| 997 | public boolean isSecure(int userId) { |
| 998 | int mode = getKeyguardStoredPasswordQuality(userId); |
| 999 | return isLockPatternEnabled(mode, userId) || isLockPasswordEnabled(mode, userId); |
| 1000 | } |
| 1001 | |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 1002 | public boolean isLockPasswordEnabled(int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1003 | return isLockPasswordEnabled(getKeyguardStoredPasswordQuality(userId), userId); |
| 1004 | } |
| 1005 | |
| 1006 | private boolean isLockPasswordEnabled(int mode, int userId) { |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1007 | final boolean passwordEnabled = mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC |
| 1008 | || mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 1009 | || mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX |
Danielle Millett | 73da5fe | 2011-09-13 16:20:05 -0400 | [diff] [blame] | 1010 | || mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC |
| 1011 | || mode == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX; |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1012 | return passwordEnabled && savedPasswordExists(userId); |
Jim Miller | 69aa4a9 | 2009-12-22 19:03:28 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | /** |
Adrian Roos | 230635e | 2015-01-07 20:50:29 +0100 | [diff] [blame] | 1016 | * @return Whether the lock pattern is enabled |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | */ |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 1018 | public boolean isLockPatternEnabled(int userId) { |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1019 | return isLockPatternEnabled(getKeyguardStoredPasswordQuality(userId), userId); |
Danielle Millett | 925a7d8 | 2012-03-19 18:02:20 -0400 | [diff] [blame] | 1020 | } |
| 1021 | |
Bryce Lee | 4614596 | 2015-12-14 14:39:10 -0800 | [diff] [blame] | 1022 | @Deprecated |
| 1023 | public boolean isLegacyLockPatternEnabled(int userId) { |
| 1024 | // Note: this value should default to {@code true} to avoid any reset that might result. |
| 1025 | // We must use a special key to read this value, since it will by default return the value |
| 1026 | // based on the new logic. |
| 1027 | return getBoolean(LEGACY_LOCK_PATTERN_ENABLED, true, userId); |
| 1028 | } |
| 1029 | |
| 1030 | @Deprecated |
| 1031 | public void setLegacyLockPatternEnabled(int userId) { |
| 1032 | setBoolean(Settings.Secure.LOCK_PATTERN_ENABLED, true, userId); |
| 1033 | } |
| 1034 | |
Adrian Roos | 9dd16eb | 2015-01-08 16:20:49 +0100 | [diff] [blame] | 1035 | private boolean isLockPatternEnabled(int mode, int userId) { |
| 1036 | return mode == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING |
| 1037 | && savedPatternExists(userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * @return Whether the visible pattern is enabled. |
| 1042 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1043 | public boolean isVisiblePatternEnabled(int userId) { |
| 1044 | return getBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | /** |
| 1048 | * Set whether the visible pattern is enabled. |
| 1049 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1050 | public void setVisiblePatternEnabled(boolean enabled, int userId) { |
Adrian Roos | dce0122 | 2015-01-07 22:39:01 +0100 | [diff] [blame] | 1051 | setBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, enabled, userId); |
Paul Lawrence | 878ba0a | 2014-08-20 13:08:01 -0700 | [diff] [blame] | 1052 | |
| 1053 | // Update for crypto if owner |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 1054 | if (userId != UserHandle.USER_SYSTEM) { |
Paul Lawrence | 878ba0a | 2014-08-20 13:08:01 -0700 | [diff] [blame] | 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | IBinder service = ServiceManager.getService("mount"); |
| 1059 | if (service == null) { |
| 1060 | Log.e(TAG, "Could not find the mount service to update the user info"); |
| 1061 | return; |
| 1062 | } |
| 1063 | |
| 1064 | IMountService mountService = IMountService.Stub.asInterface(service); |
| 1065 | try { |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 1066 | mountService.setField(StorageManager.PATTERN_VISIBLE_KEY, enabled ? "1" : "0"); |
Paul Lawrence | 878ba0a | 2014-08-20 13:08:01 -0700 | [diff] [blame] | 1067 | } catch (RemoteException e) { |
| 1068 | Log.e(TAG, "Error changing pattern visible state", e); |
| 1069 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /** |
Paul Lawrence | d8fdb33 | 2015-05-18 13:26:11 -0700 | [diff] [blame] | 1073 | * Set whether the visible password is enabled for cryptkeeper screen. |
| 1074 | */ |
| 1075 | public void setVisiblePasswordEnabled(boolean enabled, int userId) { |
| 1076 | // Update for crypto if owner |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 1077 | if (userId != UserHandle.USER_SYSTEM) { |
Paul Lawrence | d8fdb33 | 2015-05-18 13:26:11 -0700 | [diff] [blame] | 1078 | return; |
| 1079 | } |
| 1080 | |
| 1081 | IBinder service = ServiceManager.getService("mount"); |
| 1082 | if (service == null) { |
| 1083 | Log.e(TAG, "Could not find the mount service to update the user info"); |
| 1084 | return; |
| 1085 | } |
| 1086 | |
| 1087 | IMountService mountService = IMountService.Stub.asInterface(service); |
| 1088 | try { |
| 1089 | mountService.setField(StorageManager.PASSWORD_VISIBLE_KEY, enabled ? "1" : "0"); |
| 1090 | } catch (RemoteException e) { |
| 1091 | Log.e(TAG, "Error changing password visible state", e); |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1096 | * @return Whether tactile feedback for the pattern is enabled. |
| 1097 | */ |
| 1098 | public boolean isTactileFeedbackEnabled() { |
Jeff Sharkey | 5ed9d68 | 2012-10-10 14:28:27 -0700 | [diff] [blame] | 1099 | return Settings.System.getIntForUser(mContentResolver, |
| 1100 | 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] | 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * Set and store the lockout deadline, meaning the user can't attempt his/her unlock |
| 1105 | * pattern until the deadline has passed. |
| 1106 | * @return the chosen deadline. |
| 1107 | */ |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1108 | public long setLockoutAttemptDeadline(int userId, int timeoutMs) { |
| 1109 | final long deadline = SystemClock.elapsedRealtime() + timeoutMs; |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1110 | setLong(LOCKOUT_ATTEMPT_DEADLINE, deadline, userId); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1111 | setLong(LOCKOUT_ATTEMPT_TIMEOUT_MS, timeoutMs, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | return deadline; |
| 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * @return The elapsed time in millis in the future when the user is allowed to |
| 1117 | * attempt to enter his/her lock pattern, or 0 if the user is welcome to |
| 1118 | * enter a pattern. |
| 1119 | */ |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1120 | public long getLockoutAttemptDeadline(int userId) { |
Andres Morales | a4e2337 | 2015-09-08 13:17:37 -0700 | [diff] [blame] | 1121 | long deadline = getLong(LOCKOUT_ATTEMPT_DEADLINE, 0L, userId); |
Andres Morales | 2397427 | 2015-05-14 22:42:26 -0700 | [diff] [blame] | 1122 | final long timeoutMs = getLong(LOCKOUT_ATTEMPT_TIMEOUT_MS, 0L, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1123 | final long now = SystemClock.elapsedRealtime(); |
Jorim Jaggi | e3e6d56 | 2015-09-28 13:57:37 -0700 | [diff] [blame] | 1124 | if (deadline < now && deadline != 0) { |
Andres Morales | a4e2337 | 2015-09-08 13:17:37 -0700 | [diff] [blame] | 1125 | // timeout expired |
| 1126 | setLong(LOCKOUT_ATTEMPT_DEADLINE, 0, userId); |
| 1127 | setLong(LOCKOUT_ATTEMPT_TIMEOUT_MS, 0, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | return 0L; |
| 1129 | } |
Andres Morales | a4e2337 | 2015-09-08 13:17:37 -0700 | [diff] [blame] | 1130 | |
| 1131 | if (deadline > (now + timeoutMs)) { |
| 1132 | // device was rebooted, set new deadline |
| 1133 | deadline = now + timeoutMs; |
| 1134 | setLong(LOCKOUT_ATTEMPT_DEADLINE, deadline, userId); |
| 1135 | } |
| 1136 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | return deadline; |
| 1138 | } |
| 1139 | |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1140 | private boolean getBoolean(String secureSettingKey, boolean defaultValue, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1141 | try { |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1142 | return getLockSettings().getBoolean(secureSettingKey, defaultValue, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1143 | } catch (RemoteException re) { |
| 1144 | return defaultValue; |
| 1145 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1148 | private void setBoolean(String secureSettingKey, boolean enabled, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1149 | try { |
Jim Miller | f45bb40 | 2013-08-20 18:58:32 -0700 | [diff] [blame] | 1150 | getLockSettings().setBoolean(secureSettingKey, enabled, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1151 | } catch (RemoteException re) { |
| 1152 | // What can we do? |
| 1153 | Log.e(TAG, "Couldn't write boolean " + secureSettingKey + re); |
| 1154 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1155 | } |
| 1156 | |
Geoffrey Borggaard | 987672d2 | 2014-07-18 16:47:18 -0400 | [diff] [blame] | 1157 | private long getLong(String secureSettingKey, long defaultValue, int userHandle) { |
| 1158 | try { |
| 1159 | return getLockSettings().getLong(secureSettingKey, defaultValue, userHandle); |
| 1160 | } catch (RemoteException re) { |
| 1161 | return defaultValue; |
| 1162 | } |
| 1163 | } |
| 1164 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1165 | private void setLong(String secureSettingKey, long value, int userHandle) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1166 | try { |
Kenny Guy | 0cf1370 | 2013-11-29 16:33:05 +0000 | [diff] [blame] | 1167 | getLockSettings().setLong(secureSettingKey, value, userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1168 | } catch (RemoteException re) { |
| 1169 | // What can we do? |
| 1170 | Log.e(TAG, "Couldn't write long " + secureSettingKey + re); |
| 1171 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | } |
| 1173 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1174 | private String getString(String secureSettingKey, int userHandle) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1175 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1176 | return getLockSettings().getString(secureSettingKey, null, userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1177 | } catch (RemoteException re) { |
| 1178 | return null; |
| 1179 | } |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1182 | private void setString(String secureSettingKey, String value, int userHandle) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1183 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1184 | getLockSettings().setString(secureSettingKey, value, userHandle); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 1185 | } catch (RemoteException re) { |
| 1186 | // What can we do? |
| 1187 | Log.e(TAG, "Couldn't write string " + secureSettingKey + re); |
| 1188 | } |
Konstantin Lopyrev | 863f22d | 2010-05-12 17:16:58 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1191 | public void setPowerButtonInstantlyLocks(boolean enabled, int userId) { |
| 1192 | setBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, enabled, userId); |
Jim Miller | a4edd15 | 2012-01-06 18:24:04 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1195 | public boolean getPowerButtonInstantlyLocks(int userId) { |
| 1196 | return getBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, true, userId); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | public void setEnabledTrustAgents(Collection<ComponentName> activeTrustAgents, int userId) { |
| 1200 | StringBuilder sb = new StringBuilder(); |
| 1201 | for (ComponentName cn : activeTrustAgents) { |
| 1202 | if (sb.length() > 0) { |
| 1203 | sb.append(','); |
| 1204 | } |
| 1205 | sb.append(cn.flattenToShortString()); |
| 1206 | } |
| 1207 | setString(ENABLED_TRUST_AGENTS, sb.toString(), userId); |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 1208 | getTrustManager().reportEnabledTrustAgentsChanged(userId); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | public List<ComponentName> getEnabledTrustAgents(int userId) { |
| 1212 | String serialized = getString(ENABLED_TRUST_AGENTS, userId); |
| 1213 | if (TextUtils.isEmpty(serialized)) { |
| 1214 | return null; |
| 1215 | } |
| 1216 | String[] split = serialized.split(","); |
| 1217 | ArrayList<ComponentName> activeTrustAgents = new ArrayList<ComponentName>(split.length); |
| 1218 | for (String s : split) { |
| 1219 | if (!TextUtils.isEmpty(s)) { |
| 1220 | activeTrustAgents.add(ComponentName.unflattenFromString(s)); |
| 1221 | } |
| 1222 | } |
| 1223 | return activeTrustAgents; |
| 1224 | } |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 1225 | |
| 1226 | /** |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1227 | * Disable trust until credentials have been entered for user {@param userId}. |
| 1228 | * |
| 1229 | * Requires the {@link android.Manifest.permission#ACCESS_KEYGUARD_SECURE_STORAGE} permission. |
| 1230 | * |
| 1231 | * @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] | 1232 | */ |
| 1233 | public void requireCredentialEntry(int userId) { |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1234 | requireStrongAuth(StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST, userId); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | /** |
| 1238 | * Requests strong authentication for user {@param userId}. |
| 1239 | * |
| 1240 | * Requires the {@link android.Manifest.permission#ACCESS_KEYGUARD_SECURE_STORAGE} permission. |
| 1241 | * |
| 1242 | * @param strongAuthReason a combination of {@link StrongAuthTracker.StrongAuthFlags} indicating |
| 1243 | * the reason for and the strength of the requested authentication. |
| 1244 | * @param userId either an explicit user id or {@link android.os.UserHandle#USER_ALL} |
| 1245 | */ |
| 1246 | public void requireStrongAuth(@StrongAuthTracker.StrongAuthFlags int strongAuthReason, |
| 1247 | int userId) { |
| 1248 | try { |
| 1249 | getLockSettings().requireStrongAuth(strongAuthReason, userId); |
| 1250 | } catch (RemoteException e) { |
| 1251 | Log.e(TAG, "Error while requesting strong auth: " + e); |
| 1252 | } |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 1253 | } |
Adrian Roos | 4b9e324 | 2014-08-20 23:36:25 +0200 | [diff] [blame] | 1254 | |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 1255 | private void onAfterChangingPassword(int userHandle) { |
| 1256 | getTrustManager().reportEnabledTrustAgentsChanged(userHandle); |
Adrian Roos | 4b9e324 | 2014-08-20 23:36:25 +0200 | [diff] [blame] | 1257 | } |
Jim Miller | dd5de71 | 2014-10-16 19:50:18 -0700 | [diff] [blame] | 1258 | |
| 1259 | public boolean isCredentialRequiredToDecrypt(boolean defaultValue) { |
| 1260 | final int value = Settings.Global.getInt(mContentResolver, |
| 1261 | Settings.Global.REQUIRE_PASSWORD_TO_DECRYPT, -1); |
| 1262 | return value == -1 ? defaultValue : (value != 0); |
| 1263 | } |
| 1264 | |
| 1265 | public void setCredentialRequiredToDecrypt(boolean required) { |
Xiaohui Chen | 86c69dd | 2015-08-27 15:44:02 -0700 | [diff] [blame] | 1266 | if (ActivityManager.getCurrentUser() != UserHandle.USER_SYSTEM) { |
Jim Miller | dd5de71 | 2014-10-16 19:50:18 -0700 | [diff] [blame] | 1267 | Log.w(TAG, "Only device owner may call setCredentialRequiredForDecrypt()"); |
| 1268 | return; |
| 1269 | } |
Paul Lawrence | 688af6c | 2015-05-15 11:26:17 -0700 | [diff] [blame] | 1270 | |
| 1271 | if (isDeviceEncryptionEnabled()){ |
| 1272 | Settings.Global.putInt(mContext.getContentResolver(), |
| 1273 | Settings.Global.REQUIRE_PASSWORD_TO_DECRYPT, required ? 1 : 0); |
| 1274 | } |
Jim Miller | dd5de71 | 2014-10-16 19:50:18 -0700 | [diff] [blame] | 1275 | } |
Andrei Kapishnikov | 4eb6a36 | 2015-04-02 15:21:20 -0400 | [diff] [blame] | 1276 | |
| 1277 | private boolean isDoNotAskCredentialsOnBootSet() { |
| 1278 | return mDevicePolicyManager.getDoNotAskCredentialsOnBoot(); |
| 1279 | } |
| 1280 | |
| 1281 | private boolean shouldEncryptWithCredentials(boolean defaultValue) { |
| 1282 | return isCredentialRequiredToDecrypt(defaultValue) && !isDoNotAskCredentialsOnBootSet(); |
| 1283 | } |
Xiyuan Xia | aa26294 | 2015-05-05 15:18:45 -0700 | [diff] [blame] | 1284 | |
| 1285 | private void throwIfCalledOnMainThread() { |
| 1286 | if (Looper.getMainLooper().isCurrentThread()) { |
| 1287 | throw new IllegalStateException("should not be called from the main thread."); |
| 1288 | } |
| 1289 | } |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1290 | |
Clara Bayarri | 10ad84a | 2015-12-01 17:38:05 +0000 | [diff] [blame] | 1291 | public static boolean isSeparateWorkChallengeEnabled() { |
| 1292 | return StorageManager.isFileBasedEncryptionEnabled(); |
| 1293 | } |
| 1294 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1295 | public void registerStrongAuthTracker(final StrongAuthTracker strongAuthTracker) { |
| 1296 | try { |
| 1297 | getLockSettings().registerStrongAuthTracker(strongAuthTracker.mStub); |
| 1298 | } catch (RemoteException e) { |
| 1299 | throw new RuntimeException("Could not register StrongAuthTracker"); |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | public void unregisterStrongAuthTracker(final StrongAuthTracker strongAuthTracker) { |
| 1304 | try { |
| 1305 | getLockSettings().unregisterStrongAuthTracker(strongAuthTracker.mStub); |
| 1306 | } catch (RemoteException e) { |
| 1307 | Log.e(TAG, "Could not unregister StrongAuthTracker", e); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | /** |
| 1312 | * Tracks the global strong authentication state. |
| 1313 | */ |
| 1314 | public static class StrongAuthTracker { |
| 1315 | |
| 1316 | @IntDef(flag = true, |
| 1317 | value = { STRONG_AUTH_NOT_REQUIRED, |
| 1318 | STRONG_AUTH_REQUIRED_AFTER_BOOT, |
| 1319 | STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW, |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1320 | SOME_AUTH_REQUIRED_AFTER_USER_REQUEST}) |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1321 | @Retention(RetentionPolicy.SOURCE) |
| 1322 | public @interface StrongAuthFlags {} |
| 1323 | |
| 1324 | /** |
| 1325 | * Strong authentication is not required. |
| 1326 | */ |
| 1327 | public static final int STRONG_AUTH_NOT_REQUIRED = 0x0; |
| 1328 | |
| 1329 | /** |
| 1330 | * Strong authentication is required because the user has not authenticated since boot. |
| 1331 | */ |
| 1332 | public static final int STRONG_AUTH_REQUIRED_AFTER_BOOT = 0x1; |
| 1333 | |
| 1334 | /** |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1335 | * Strong authentication is required because a device admin has requested it. |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1336 | */ |
| 1337 | public static final int STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW = 0x2; |
| 1338 | |
| 1339 | /** |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1340 | * Some authentication is required because the user has temporarily disabled trust. |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1341 | */ |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1342 | public static final int SOME_AUTH_REQUIRED_AFTER_USER_REQUEST = 0x4; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1343 | |
Adrian Roos | 873010d | 2015-08-25 15:59:00 -0700 | [diff] [blame] | 1344 | /** |
| 1345 | * Strong authentication is required because the user has been locked out after too many |
| 1346 | * attempts. |
| 1347 | */ |
| 1348 | public static final int STRONG_AUTH_REQUIRED_AFTER_LOCKOUT = 0x8; |
| 1349 | |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1350 | /** |
| 1351 | * Some authentication is required because the user has entered a wrong credential. |
| 1352 | */ |
| 1353 | public static final int SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL = 0x10; |
| 1354 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1355 | public static final int DEFAULT = STRONG_AUTH_REQUIRED_AFTER_BOOT; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1356 | |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1357 | private static final int ALLOWING_FINGERPRINT = STRONG_AUTH_NOT_REQUIRED |
Jorim Jaggi | 16093fe3 | 2015-09-09 23:29:17 +0000 | [diff] [blame] | 1358 | | SOME_AUTH_REQUIRED_AFTER_USER_REQUEST |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1359 | | SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1360 | |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1361 | private final SparseIntArray mStrongAuthRequiredForUser = new SparseIntArray(); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1362 | private final H mHandler; |
| 1363 | |
| 1364 | public StrongAuthTracker() { |
| 1365 | this(Looper.myLooper()); |
| 1366 | } |
| 1367 | |
| 1368 | /** |
| 1369 | * @param looper the looper on whose thread calls to {@link #onStrongAuthRequiredChanged} |
| 1370 | * will be scheduled. |
| 1371 | */ |
| 1372 | public StrongAuthTracker(Looper looper) { |
| 1373 | mHandler = new H(looper); |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * Returns {@link #STRONG_AUTH_NOT_REQUIRED} if strong authentication is not required, |
| 1378 | * otherwise returns a combination of {@link StrongAuthFlags} indicating why strong |
| 1379 | * authentication is required. |
| 1380 | * |
| 1381 | * @param userId the user for whom the state is queried. |
| 1382 | */ |
| 1383 | public @StrongAuthFlags int getStrongAuthForUser(int userId) { |
| 1384 | return mStrongAuthRequiredForUser.get(userId, DEFAULT); |
| 1385 | } |
| 1386 | |
| 1387 | /** |
| 1388 | * @return true if unlocking with trust alone is allowed for {@param userId} by the current |
| 1389 | * strong authentication requirements. |
| 1390 | */ |
| 1391 | public boolean isTrustAllowedForUser(int userId) { |
| 1392 | return getStrongAuthForUser(userId) == STRONG_AUTH_NOT_REQUIRED; |
| 1393 | } |
| 1394 | |
| 1395 | /** |
| 1396 | * @return true if unlocking with fingerprint alone is allowed for {@param userId} by the |
| 1397 | * current strong authentication requirements. |
| 1398 | */ |
| 1399 | public boolean isFingerprintAllowedForUser(int userId) { |
Adrian Roos | af4ab3e | 2015-09-02 13:23:30 -0700 | [diff] [blame] | 1400 | return (getStrongAuthForUser(userId) & ~ALLOWING_FINGERPRINT) == 0; |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | /** |
| 1404 | * Called when the strong authentication requirements for {@param userId} changed. |
| 1405 | */ |
| 1406 | public void onStrongAuthRequiredChanged(int userId) { |
| 1407 | } |
| 1408 | |
| 1409 | void handleStrongAuthRequiredChanged(@StrongAuthFlags int strongAuthFlags, |
| 1410 | int userId) { |
| 1411 | |
| 1412 | int oldValue = getStrongAuthForUser(userId); |
| 1413 | if (strongAuthFlags != oldValue) { |
| 1414 | if (strongAuthFlags == DEFAULT) { |
| 1415 | mStrongAuthRequiredForUser.delete(userId); |
| 1416 | } else { |
| 1417 | mStrongAuthRequiredForUser.put(userId, strongAuthFlags); |
| 1418 | } |
| 1419 | onStrongAuthRequiredChanged(userId); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | |
| 1424 | final IStrongAuthTracker.Stub mStub = new IStrongAuthTracker.Stub() { |
| 1425 | @Override |
| 1426 | public void onStrongAuthRequiredChanged(@StrongAuthFlags int strongAuthFlags, |
| 1427 | int userId) { |
| 1428 | mHandler.obtainMessage(H.MSG_ON_STRONG_AUTH_REQUIRED_CHANGED, |
| 1429 | strongAuthFlags, userId).sendToTarget(); |
| 1430 | } |
| 1431 | }; |
| 1432 | |
| 1433 | private class H extends Handler { |
| 1434 | static final int MSG_ON_STRONG_AUTH_REQUIRED_CHANGED = 1; |
| 1435 | |
| 1436 | public H(Looper looper) { |
| 1437 | super(looper); |
| 1438 | } |
| 1439 | |
| 1440 | @Override |
| 1441 | public void handleMessage(Message msg) { |
| 1442 | switch (msg.what) { |
| 1443 | case MSG_ON_STRONG_AUTH_REQUIRED_CHANGED: |
| 1444 | handleStrongAuthRequiredChanged(msg.arg1, msg.arg2); |
| 1445 | break; |
| 1446 | } |
| 1447 | } |
| 1448 | }; |
| 1449 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | } |