Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | */ |
Andrew Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 16 | |
| 17 | package com.android.server.locksettings; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 21 | import android.app.admin.DevicePolicyManager; |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 22 | import android.content.Context; |
Adrian Roos | 60dcbbf | 2017-08-08 16:19:33 +0200 | [diff] [blame] | 23 | import android.content.pm.UserInfo; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 24 | import android.hardware.weaver.V1_0.IWeaver; |
| 25 | import android.hardware.weaver.V1_0.WeaverConfig; |
| 26 | import android.hardware.weaver.V1_0.WeaverReadResponse; |
| 27 | import android.hardware.weaver.V1_0.WeaverReadStatus; |
| 28 | import android.hardware.weaver.V1_0.WeaverStatus; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 29 | import android.os.RemoteException; |
| 30 | import android.os.UserManager; |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 31 | import android.security.GateKeeper; |
Bo Zhu | f2c9934 | 2018-12-06 14:52:33 -0800 | [diff] [blame] | 32 | import android.security.Scrypt; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 33 | import android.service.gatekeeper.GateKeeperResponse; |
| 34 | import android.service.gatekeeper.IGateKeeperService; |
| 35 | import android.util.ArrayMap; |
| 36 | import android.util.Log; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 37 | import android.util.Slog; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 38 | |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 39 | import com.android.internal.annotations.VisibleForTesting; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 40 | import com.android.internal.util.ArrayUtils; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 41 | import com.android.internal.widget.ICheckCredentialProgressCallback; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 42 | import com.android.internal.widget.LockPatternUtils; |
| 43 | import com.android.internal.widget.VerifyCredentialResponse; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 44 | import com.android.server.locksettings.LockSettingsStorage.PersistentData; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 45 | |
| 46 | import libcore.util.HexEncoding; |
| 47 | |
| 48 | import java.nio.ByteBuffer; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 49 | import java.security.NoSuchAlgorithmException; |
| 50 | import java.security.SecureRandom; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 51 | import java.util.ArrayList; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 52 | import java.util.Arrays; |
| 53 | import java.util.Collections; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 54 | import java.util.HashSet; |
| 55 | import java.util.List; |
| 56 | import java.util.Map; |
| 57 | import java.util.NoSuchElementException; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 58 | import java.util.Set; |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * A class that maintains the wrapping of synthetic password by user credentials or escrow tokens. |
| 63 | * It's (mostly) a pure storage for synthetic passwords, providing APIs to creating and destroying |
| 64 | * synthetic password blobs which are wrapped by user credentials or escrow tokens. |
| 65 | * |
| 66 | * Here is the assumptions it makes: |
| 67 | * Each user has one single synthetic password at any time. |
| 68 | * The SP has an associated password handle, which binds to the SID for that user. The password |
| 69 | * handle is persisted by SyntheticPasswordManager internally. |
| 70 | * If the user credential is null, it's treated as if the credential is DEFAULT_PASSWORD |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 71 | * |
| 72 | * Information persisted on disk: |
| 73 | * for each user (stored under DEFAULT_HANDLE): |
| 74 | * SP_HANDLE_NAME: GateKeeper password handle of synthetic password. Only available if user |
| 75 | * credential exists, cleared when user clears their credential. |
Rubin Xu | 128180b | 2017-04-12 18:02:44 +0100 | [diff] [blame] | 76 | * SP_E0_NAME, SP_P1_NAME: Secret to derive synthetic password when combined with escrow |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 77 | * tokens. Destroyed when escrow support is turned off for the given user. |
| 78 | * |
| 79 | * for each SP blob under the user (stored under the corresponding handle): |
| 80 | * SP_BLOB_NAME: The encrypted synthetic password. Always exists. |
| 81 | * PASSWORD_DATA_NAME: Metadata about user credential. Only exists for password based SP. |
Rubin Xu | 128180b | 2017-04-12 18:02:44 +0100 | [diff] [blame] | 82 | * SECDISCARDABLE_NAME: Part of the necessary ingredient to decrypt SP_BLOB_NAME for the |
| 83 | * purpose of secure deletion. Exists if this is a non-weaver SP |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 84 | * (both password and token based), or it's a token-based SP under weaver. |
| 85 | * WEAVER_SLOT: Metadata about the weaver slot used. Only exists if this is a SP under weaver. |
| 86 | * |
| 87 | * |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 88 | */ |
| 89 | public class SyntheticPasswordManager { |
| 90 | private static final String SP_BLOB_NAME = "spblob"; |
| 91 | private static final String SP_E0_NAME = "e0"; |
| 92 | private static final String SP_P1_NAME = "p1"; |
| 93 | private static final String SP_HANDLE_NAME = "handle"; |
| 94 | private static final String SECDISCARDABLE_NAME = "secdis"; |
| 95 | private static final int SECDISCARDABLE_LENGTH = 16 * 1024; |
| 96 | private static final String PASSWORD_DATA_NAME = "pwd"; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 97 | private static final String WEAVER_SLOT_NAME = "weaver"; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 98 | |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 99 | public static final long DEFAULT_HANDLE = 0L; |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 100 | private static final byte[] DEFAULT_PASSWORD = "default-password".getBytes(); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 101 | |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 102 | private static final byte WEAVER_VERSION = 1; |
| 103 | private static final int INVALID_WEAVER_SLOT = -1; |
| 104 | |
Rubin Xu | 8c52865 | 2017-10-31 15:40:32 +0000 | [diff] [blame] | 105 | private static final byte SYNTHETIC_PASSWORD_VERSION_V1 = 1; |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 106 | private static final byte SYNTHETIC_PASSWORD_VERSION_V2 = 2; |
| 107 | private static final byte SYNTHETIC_PASSWORD_VERSION_V3 = 3; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 108 | private static final byte SYNTHETIC_PASSWORD_PASSWORD_BASED = 0; |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 109 | private static final byte SYNTHETIC_PASSWORD_TOKEN_BASED = 1; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 110 | |
| 111 | // 256-bit synthetic password |
| 112 | private static final byte SYNTHETIC_PASSWORD_LENGTH = 256 / 8; |
| 113 | |
Rubin Xu | 157bddd | 2017-04-24 15:51:59 +0100 | [diff] [blame] | 114 | private static final int PASSWORD_SCRYPT_N = 11; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 115 | private static final int PASSWORD_SCRYPT_R = 3; |
| 116 | private static final int PASSWORD_SCRYPT_P = 1; |
| 117 | private static final int PASSWORD_SALT_LENGTH = 16; |
| 118 | private static final int PASSWORD_TOKEN_LENGTH = 32; |
| 119 | private static final String TAG = "SyntheticPasswordManager"; |
| 120 | |
| 121 | private static final byte[] PERSONALISATION_SECDISCARDABLE = "secdiscardable-transform".getBytes(); |
| 122 | private static final byte[] PERSONALIZATION_KEY_STORE_PASSWORD = "keystore-password".getBytes(); |
| 123 | private static final byte[] PERSONALIZATION_USER_GK_AUTH = "user-gk-authentication".getBytes(); |
| 124 | private static final byte[] PERSONALIZATION_SP_GK_AUTH = "sp-gk-authentication".getBytes(); |
| 125 | private static final byte[] PERSONALIZATION_FBE_KEY = "fbe-key".getBytes(); |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 126 | private static final byte[] PERSONALIZATION_AUTHSECRET_KEY = "authsecret-hal".getBytes(); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 127 | private static final byte[] PERSONALIZATION_SP_SPLIT = "sp-split".getBytes(); |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 128 | private static final byte[] PERSONALIZATION_PASSWORD_HASH = "pw-hash".getBytes(); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 129 | private static final byte[] PERSONALIZATION_E0 = "e0-encryption".getBytes(); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 130 | private static final byte[] PERSONALISATION_WEAVER_PASSWORD = "weaver-pwd".getBytes(); |
| 131 | private static final byte[] PERSONALISATION_WEAVER_KEY = "weaver-key".getBytes(); |
| 132 | private static final byte[] PERSONALISATION_WEAVER_TOKEN = "weaver-token".getBytes(); |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 133 | private static final byte[] PERSONALISATION_CONTEXT = |
| 134 | "android-synthetic-password-personalization-context".getBytes(); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 135 | |
| 136 | static class AuthenticationResult { |
| 137 | public AuthenticationToken authToken; |
| 138 | public VerifyCredentialResponse gkResponse; |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 139 | public int credentialType; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static class AuthenticationToken { |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 143 | private final byte mVersion; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 144 | /* |
| 145 | * Here is the relationship between all three fields: |
| 146 | * P0 and P1 are two randomly-generated blocks. P1 is stored on disk but P0 is not. |
| 147 | * syntheticPassword = hash(P0 || P1) |
| 148 | * E0 = P0 encrypted under syntheticPassword, stored on disk. |
| 149 | */ |
| 150 | private @Nullable byte[] E0; |
| 151 | private @Nullable byte[] P1; |
| 152 | private @NonNull String syntheticPassword; |
| 153 | |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 154 | AuthenticationToken(byte version) { |
| 155 | mVersion = version; |
| 156 | } |
| 157 | |
| 158 | private byte[] derivePassword(byte[] personalization) { |
| 159 | if (mVersion == SYNTHETIC_PASSWORD_VERSION_V3) { |
| 160 | return (new SP800Derive(syntheticPassword.getBytes())) |
| 161 | .withContext(personalization, PERSONALISATION_CONTEXT); |
| 162 | } else { |
| 163 | return SyntheticPasswordCrypto.personalisedHash(personalization, |
| 164 | syntheticPassword.getBytes()); |
| 165 | } |
| 166 | } |
| 167 | |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 168 | public byte[] deriveKeyStorePassword() { |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 169 | return bytesToHex(derivePassword(PERSONALIZATION_KEY_STORE_PASSWORD)); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | public byte[] deriveGkPassword() { |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 173 | return derivePassword(PERSONALIZATION_SP_GK_AUTH); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | public byte[] deriveDiskEncryptionKey() { |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 177 | return derivePassword(PERSONALIZATION_FBE_KEY); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 180 | public byte[] deriveVendorAuthSecret() { |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 181 | return derivePassword(PERSONALIZATION_AUTHSECRET_KEY); |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 184 | public byte[] derivePasswordHashFactor() { |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 185 | return derivePassword(PERSONALIZATION_PASSWORD_HASH); |
Rubin Xu | f01e907 | 2018-03-30 20:59:28 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 188 | private void initialize(byte[] P0, byte[] P1) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 189 | this.P1 = P1; |
| 190 | this.syntheticPassword = String.valueOf(HexEncoding.encode( |
| 191 | SyntheticPasswordCrypto.personalisedHash( |
| 192 | PERSONALIZATION_SP_SPLIT, P0, P1))); |
| 193 | this.E0 = SyntheticPasswordCrypto.encrypt(this.syntheticPassword.getBytes(), |
| 194 | PERSONALIZATION_E0, P0); |
| 195 | } |
| 196 | |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 197 | public void recreate(byte[] secret) { |
| 198 | initialize(secret, this.P1); |
| 199 | } |
| 200 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 201 | protected static AuthenticationToken create() { |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 202 | AuthenticationToken result = new AuthenticationToken(SYNTHETIC_PASSWORD_VERSION_V3); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 203 | result.initialize(secureRandom(SYNTHETIC_PASSWORD_LENGTH), |
| 204 | secureRandom(SYNTHETIC_PASSWORD_LENGTH)); |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | public byte[] computeP0() { |
| 209 | if (E0 == null) { |
| 210 | return null; |
| 211 | } |
| 212 | return SyntheticPasswordCrypto.decrypt(syntheticPassword.getBytes(), PERSONALIZATION_E0, |
| 213 | E0); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static class PasswordData { |
| 218 | byte scryptN; |
| 219 | byte scryptR; |
| 220 | byte scryptP; |
| 221 | public int passwordType; |
| 222 | byte[] salt; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 223 | // For GateKeeper-based credential, this is the password handle returned by GK, |
| 224 | // for weaver-based credential, this is empty. |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 225 | public byte[] passwordHandle; |
| 226 | |
| 227 | public static PasswordData create(int passwordType) { |
| 228 | PasswordData result = new PasswordData(); |
| 229 | result.scryptN = PASSWORD_SCRYPT_N; |
| 230 | result.scryptR = PASSWORD_SCRYPT_R; |
| 231 | result.scryptP = PASSWORD_SCRYPT_P; |
| 232 | result.passwordType = passwordType; |
| 233 | result.salt = secureRandom(PASSWORD_SALT_LENGTH); |
| 234 | return result; |
| 235 | } |
| 236 | |
| 237 | public static PasswordData fromBytes(byte[] data) { |
| 238 | PasswordData result = new PasswordData(); |
| 239 | ByteBuffer buffer = ByteBuffer.allocate(data.length); |
| 240 | buffer.put(data, 0, data.length); |
| 241 | buffer.flip(); |
| 242 | result.passwordType = buffer.getInt(); |
| 243 | result.scryptN = buffer.get(); |
| 244 | result.scryptR = buffer.get(); |
| 245 | result.scryptP = buffer.get(); |
| 246 | int saltLen = buffer.getInt(); |
| 247 | result.salt = new byte[saltLen]; |
| 248 | buffer.get(result.salt); |
| 249 | int handleLen = buffer.getInt(); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 250 | if (handleLen > 0) { |
| 251 | result.passwordHandle = new byte[handleLen]; |
| 252 | buffer.get(result.passwordHandle); |
| 253 | } else { |
| 254 | result.passwordHandle = null; |
| 255 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 256 | return result; |
| 257 | } |
| 258 | |
| 259 | public byte[] toBytes() { |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 260 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 261 | ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES + 3 * Byte.BYTES |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 262 | + Integer.BYTES + salt.length + Integer.BYTES + |
| 263 | (passwordHandle != null ? passwordHandle.length : 0)); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 264 | buffer.putInt(passwordType); |
| 265 | buffer.put(scryptN); |
| 266 | buffer.put(scryptR); |
| 267 | buffer.put(scryptP); |
| 268 | buffer.putInt(salt.length); |
| 269 | buffer.put(salt); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 270 | if (passwordHandle != null && passwordHandle.length > 0) { |
| 271 | buffer.putInt(passwordHandle.length); |
| 272 | buffer.put(passwordHandle); |
| 273 | } else { |
| 274 | buffer.putInt(0); |
| 275 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 276 | return buffer.array(); |
| 277 | } |
| 278 | } |
| 279 | |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 280 | static class TokenData { |
| 281 | byte[] secdiscardableOnDisk; |
| 282 | byte[] weaverSecret; |
| 283 | byte[] aggregatedSecret; |
| 284 | } |
| 285 | |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 286 | private final Context mContext; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 287 | private LockSettingsStorage mStorage; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 288 | private IWeaver mWeaver; |
| 289 | private WeaverConfig mWeaverConfig; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 290 | |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 291 | private final UserManager mUserManager; |
| 292 | |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 293 | public SyntheticPasswordManager(Context context, LockSettingsStorage storage, |
| 294 | UserManager userManager) { |
| 295 | mContext = context; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 296 | mStorage = storage; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 297 | mUserManager = userManager; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 300 | @VisibleForTesting |
| 301 | protected IWeaver getWeaverService() throws RemoteException { |
| 302 | try { |
| 303 | return IWeaver.getService(); |
| 304 | } catch (NoSuchElementException e) { |
| 305 | Slog.i(TAG, "Device does not support weaver"); |
| 306 | return null; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | public synchronized void initWeaverService() { |
| 311 | if (mWeaver != null) { |
| 312 | return; |
| 313 | } |
| 314 | try { |
| 315 | mWeaverConfig = null; |
| 316 | mWeaver = getWeaverService(); |
| 317 | if (mWeaver != null) { |
| 318 | mWeaver.getConfig((int status, WeaverConfig config) -> { |
| 319 | if (status == WeaverStatus.OK && config.slots > 0) { |
| 320 | mWeaverConfig = config; |
| 321 | } else { |
| 322 | Slog.e(TAG, "Failed to get weaver config, status " + status |
| 323 | + " slots: " + config.slots); |
| 324 | mWeaver = null; |
| 325 | } |
| 326 | }); |
| 327 | } |
| 328 | } catch (RemoteException e) { |
| 329 | Slog.e(TAG, "Failed to get weaver service", e); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | private synchronized boolean isWeaverAvailable() { |
| 334 | if (mWeaver == null) { |
| 335 | //Re-initializing weaver in case there was a transient error preventing access to it. |
| 336 | initWeaverService(); |
| 337 | } |
| 338 | return mWeaver != null && mWeaverConfig.slots > 0; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Enroll the given key value pair into the specified weaver slot. if the given key is null, |
| 343 | * a default all-zero key is used. If the value is not specified, a fresh random secret is |
| 344 | * generated as the value. |
| 345 | * |
| 346 | * @return the value stored in the weaver slot |
| 347 | * @throws RemoteException |
| 348 | */ |
| 349 | private byte[] weaverEnroll(int slot, byte[] key, @Nullable byte[] value) |
| 350 | throws RemoteException { |
| 351 | if (slot == INVALID_WEAVER_SLOT || slot >= mWeaverConfig.slots) { |
| 352 | throw new RuntimeException("Invalid slot for weaver"); |
| 353 | } |
| 354 | if (key == null) { |
| 355 | key = new byte[mWeaverConfig.keySize]; |
| 356 | } else if (key.length != mWeaverConfig.keySize) { |
| 357 | throw new RuntimeException("Invalid key size for weaver"); |
| 358 | } |
| 359 | if (value == null) { |
| 360 | value = secureRandom(mWeaverConfig.valueSize); |
| 361 | } |
| 362 | int writeStatus = mWeaver.write(slot, toByteArrayList(key), toByteArrayList(value)); |
| 363 | if (writeStatus != WeaverStatus.OK) { |
| 364 | Log.e(TAG, "weaver write failed, slot: " + slot + " status: " + writeStatus); |
| 365 | return null; |
| 366 | } |
| 367 | return value; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Verify the supplied key against a weaver slot, returning a response indicating whether |
| 372 | * the verification is successful, throttled or failed. If successful, the bound secret |
| 373 | * is also returned. |
| 374 | * @throws RemoteException |
| 375 | */ |
| 376 | private VerifyCredentialResponse weaverVerify(int slot, byte[] key) throws RemoteException { |
| 377 | if (slot == INVALID_WEAVER_SLOT || slot >= mWeaverConfig.slots) { |
| 378 | throw new RuntimeException("Invalid slot for weaver"); |
| 379 | } |
| 380 | if (key == null) { |
| 381 | key = new byte[mWeaverConfig.keySize]; |
| 382 | } else if (key.length != mWeaverConfig.keySize) { |
| 383 | throw new RuntimeException("Invalid key size for weaver"); |
| 384 | } |
| 385 | final VerifyCredentialResponse[] response = new VerifyCredentialResponse[1]; |
| 386 | mWeaver.read(slot, toByteArrayList(key), (int status, WeaverReadResponse readResponse) -> { |
| 387 | switch (status) { |
| 388 | case WeaverReadStatus.OK: |
| 389 | response[0] = new VerifyCredentialResponse( |
| 390 | fromByteArrayList(readResponse.value)); |
| 391 | break; |
| 392 | case WeaverReadStatus.THROTTLE: |
| 393 | response[0] = new VerifyCredentialResponse(readResponse.timeout); |
| 394 | Log.e(TAG, "weaver read failed (THROTTLE), slot: " + slot); |
| 395 | break; |
| 396 | case WeaverReadStatus.INCORRECT_KEY: |
| 397 | if (readResponse.timeout == 0) { |
| 398 | response[0] = VerifyCredentialResponse.ERROR; |
| 399 | Log.e(TAG, "weaver read failed (INCORRECT_KEY), slot: " + slot); |
| 400 | } else { |
| 401 | response[0] = new VerifyCredentialResponse(readResponse.timeout); |
| 402 | Log.e(TAG, "weaver read failed (INCORRECT_KEY/THROTTLE), slot: " + slot); |
| 403 | } |
| 404 | break; |
| 405 | case WeaverReadStatus.FAILED: |
| 406 | response[0] = VerifyCredentialResponse.ERROR; |
| 407 | Log.e(TAG, "weaver read failed (FAILED), slot: " + slot); |
| 408 | break; |
| 409 | default: |
| 410 | response[0] = VerifyCredentialResponse.ERROR; |
| 411 | Log.e(TAG, "weaver read unknown status " + status + ", slot: " + slot); |
| 412 | break; |
| 413 | } |
| 414 | }); |
| 415 | return response[0]; |
| 416 | } |
| 417 | |
| 418 | public void removeUser(int userId) { |
Rubin Xu | 2f22ee4 | 2017-09-15 11:56:53 +0100 | [diff] [blame] | 419 | for (long handle : mStorage.listSyntheticPasswordHandlesForUser(SP_BLOB_NAME, userId)) { |
| 420 | destroyWeaverSlot(handle, userId); |
| 421 | destroySPBlobKey(getHandleName(handle)); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 422 | } |
| 423 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 424 | |
| 425 | public int getCredentialType(long handle, int userId) { |
| 426 | byte[] passwordData = loadState(PASSWORD_DATA_NAME, handle, userId); |
| 427 | if (passwordData == null) { |
| 428 | Log.w(TAG, "getCredentialType: encountered empty password data for user " + userId); |
| 429 | return LockPatternUtils.CREDENTIAL_TYPE_NONE; |
| 430 | } |
| 431 | return PasswordData.fromBytes(passwordData).passwordType; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Initializing a new Authentication token, possibly from an existing credential and hash. |
| 436 | * |
| 437 | * The authentication token would bear a randomly-generated synthetic password. |
| 438 | * |
| 439 | * This method has the side effect of rebinding the SID of the given user to the |
| 440 | * newly-generated SP. |
| 441 | * |
| 442 | * If the existing credential hash is non-null, the existing SID mill be migrated so |
| 443 | * the synthetic password in the authentication token will produce the same SID |
| 444 | * (the corresponding synthetic password handle is persisted by SyntheticPasswordManager |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 445 | * in a per-user data storage.) |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 446 | * |
| 447 | * If the existing credential hash is null, it means the given user should have no SID so |
| 448 | * SyntheticPasswordManager will nuke any SP handle previously persisted. In this case, |
| 449 | * the supplied credential parameter is also ignored. |
| 450 | * |
| 451 | * Also saves the escrow information necessary to re-generate the synthetic password under |
| 452 | * an escrow scheme. This information can be removed with {@link #destroyEscrowData} if |
| 453 | * password escrow should be disabled completely on the given user. |
| 454 | * |
| 455 | */ |
| 456 | public AuthenticationToken newSyntheticPasswordAndSid(IGateKeeperService gatekeeper, |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 457 | byte[] hash, byte[] credential, int userId) throws RemoteException { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 458 | AuthenticationToken result = AuthenticationToken.create(); |
| 459 | GateKeeperResponse response; |
| 460 | if (hash != null) { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 461 | response = gatekeeper.enroll(userId, hash, credential, |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 462 | result.deriveGkPassword()); |
| 463 | if (response.getResponseCode() != GateKeeperResponse.RESPONSE_OK) { |
| 464 | Log.w(TAG, "Fail to migrate SID, assuming no SID, user " + userId); |
| 465 | clearSidForUser(userId); |
| 466 | } else { |
| 467 | saveSyntheticPasswordHandle(response.getPayload(), userId); |
| 468 | } |
| 469 | } else { |
| 470 | clearSidForUser(userId); |
| 471 | } |
| 472 | saveEscrowData(result, userId); |
| 473 | return result; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Enroll a new password handle and SID for the given synthetic password and persist it on disk. |
| 478 | * Used when adding password to previously-unsecured devices. |
| 479 | */ |
| 480 | public void newSidForUser(IGateKeeperService gatekeeper, AuthenticationToken authToken, |
| 481 | int userId) throws RemoteException { |
| 482 | GateKeeperResponse response = gatekeeper.enroll(userId, null, null, |
| 483 | authToken.deriveGkPassword()); |
| 484 | if (response.getResponseCode() != GateKeeperResponse.RESPONSE_OK) { |
| 485 | Log.e(TAG, "Fail to create new SID for user " + userId); |
| 486 | return; |
| 487 | } |
| 488 | saveSyntheticPasswordHandle(response.getPayload(), userId); |
| 489 | } |
| 490 | |
| 491 | // Nuke the SP handle (and as a result, its SID) for the given user. |
| 492 | public void clearSidForUser(int userId) { |
Rubin Xu | aa32d15 | 2017-04-27 17:01:05 +0100 | [diff] [blame] | 493 | destroyState(SP_HANDLE_NAME, DEFAULT_HANDLE, userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | public boolean hasSidForUser(int userId) { |
| 497 | return hasState(SP_HANDLE_NAME, DEFAULT_HANDLE, userId); |
| 498 | } |
| 499 | |
| 500 | // if null, it means there is no SID associated with the user |
| 501 | // This can happen if the user is migrated to SP but currently |
| 502 | // do not have a lockscreen password. |
| 503 | private byte[] loadSyntheticPasswordHandle(int userId) { |
| 504 | return loadState(SP_HANDLE_NAME, DEFAULT_HANDLE, userId); |
| 505 | } |
| 506 | |
| 507 | private void saveSyntheticPasswordHandle(byte[] spHandle, int userId) { |
| 508 | saveState(SP_HANDLE_NAME, spHandle, DEFAULT_HANDLE, userId); |
| 509 | } |
| 510 | |
| 511 | private boolean loadEscrowData(AuthenticationToken authToken, int userId) { |
| 512 | authToken.E0 = loadState(SP_E0_NAME, DEFAULT_HANDLE, userId); |
| 513 | authToken.P1 = loadState(SP_P1_NAME, DEFAULT_HANDLE, userId); |
| 514 | return authToken.E0 != null && authToken.P1 != null; |
| 515 | } |
| 516 | |
| 517 | private void saveEscrowData(AuthenticationToken authToken, int userId) { |
| 518 | saveState(SP_E0_NAME, authToken.E0, DEFAULT_HANDLE, userId); |
| 519 | saveState(SP_P1_NAME, authToken.P1, DEFAULT_HANDLE, userId); |
| 520 | } |
| 521 | |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 522 | public boolean hasEscrowData(int userId) { |
| 523 | return hasState(SP_E0_NAME, DEFAULT_HANDLE, userId) |
| 524 | && hasState(SP_P1_NAME, DEFAULT_HANDLE, userId); |
| 525 | } |
| 526 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 527 | public void destroyEscrowData(int userId) { |
Rubin Xu | aa32d15 | 2017-04-27 17:01:05 +0100 | [diff] [blame] | 528 | destroyState(SP_E0_NAME, DEFAULT_HANDLE, userId); |
| 529 | destroyState(SP_P1_NAME, DEFAULT_HANDLE, userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 532 | private int loadWeaverSlot(long handle, int userId) { |
| 533 | final int LENGTH = Byte.BYTES + Integer.BYTES; |
| 534 | byte[] data = loadState(WEAVER_SLOT_NAME, handle, userId); |
| 535 | if (data == null || data.length != LENGTH) { |
| 536 | return INVALID_WEAVER_SLOT; |
| 537 | } |
| 538 | ByteBuffer buffer = ByteBuffer.allocate(LENGTH); |
| 539 | buffer.put(data, 0, data.length); |
| 540 | buffer.flip(); |
| 541 | if (buffer.get() != WEAVER_VERSION) { |
| 542 | Log.e(TAG, "Invalid weaver slot version of handle " + handle); |
| 543 | return INVALID_WEAVER_SLOT; |
| 544 | } |
| 545 | return buffer.getInt(); |
| 546 | } |
| 547 | |
| 548 | private void saveWeaverSlot(int slot, long handle, int userId) { |
| 549 | ByteBuffer buffer = ByteBuffer.allocate(Byte.BYTES + Integer.BYTES); |
| 550 | buffer.put(WEAVER_VERSION); |
| 551 | buffer.putInt(slot); |
| 552 | saveState(WEAVER_SLOT_NAME, buffer.array(), handle, userId); |
| 553 | } |
| 554 | |
| 555 | private void destroyWeaverSlot(long handle, int userId) { |
| 556 | int slot = loadWeaverSlot(handle, userId); |
Rubin Xu | a339df0 | 2018-04-10 09:44:18 +0100 | [diff] [blame] | 557 | destroyState(WEAVER_SLOT_NAME, handle, userId); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 558 | if (slot != INVALID_WEAVER_SLOT) { |
Rubin Xu | a339df0 | 2018-04-10 09:44:18 +0100 | [diff] [blame] | 559 | Set<Integer> usedSlots = getUsedWeaverSlots(); |
| 560 | if (!usedSlots.contains(slot)) { |
| 561 | Log.i(TAG, "Destroy weaver slot " + slot + " for user " + userId); |
| 562 | try { |
| 563 | weaverEnroll(slot, null, null); |
| 564 | } catch (RemoteException e) { |
| 565 | Log.w(TAG, "Failed to destroy slot", e); |
| 566 | } |
| 567 | } else { |
| 568 | Log.w(TAG, "Skip destroying reused weaver slot " + slot + " for user " + userId); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 569 | } |
| 570 | } |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 571 | } |
| 572 | |
Rubin Xu | a339df0 | 2018-04-10 09:44:18 +0100 | [diff] [blame] | 573 | /** |
| 574 | * Return the set of weaver slots that are currently in use by all users on the device. |
| 575 | * <p> |
| 576 | * <em>Note:</em> Users who are in the process of being deleted are not tracked here |
| 577 | * (due to them being marked as partial in UserManager so not visible from |
| 578 | * {@link UserManager#getUsers}). As a result their weaver slots will not be considered |
| 579 | * taken and can be reused by new users. Care should be taken when cleaning up the |
| 580 | * deleted user in {@link #removeUser}, to prevent a reused slot from being erased |
| 581 | * unintentionally. |
| 582 | */ |
| 583 | private Set<Integer> getUsedWeaverSlots() { |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 584 | Map<Integer, List<Long>> slotHandles = mStorage.listSyntheticPasswordHandlesForAllUsers( |
| 585 | WEAVER_SLOT_NAME); |
| 586 | HashSet<Integer> slots = new HashSet<>(); |
| 587 | for (Map.Entry<Integer, List<Long>> entry : slotHandles.entrySet()) { |
| 588 | for (Long handle : entry.getValue()) { |
| 589 | int slot = loadWeaverSlot(handle, entry.getKey()); |
| 590 | slots.add(slot); |
| 591 | } |
| 592 | } |
Rubin Xu | a339df0 | 2018-04-10 09:44:18 +0100 | [diff] [blame] | 593 | return slots; |
| 594 | } |
| 595 | |
| 596 | private int getNextAvailableWeaverSlot() { |
| 597 | Set<Integer> usedSlots = getUsedWeaverSlots(); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 598 | for (int i = 0; i < mWeaverConfig.slots; i++) { |
Rubin Xu | a339df0 | 2018-04-10 09:44:18 +0100 | [diff] [blame] | 599 | if (!usedSlots.contains(i)) { |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 600 | return i; |
| 601 | } |
| 602 | } |
| 603 | throw new RuntimeException("Run out of weaver slots."); |
| 604 | } |
| 605 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 606 | /** |
| 607 | * Create a new password based SP blob based on the supplied authentication token, such that |
| 608 | * a future successful authentication with unwrapPasswordBasedSyntheticPassword() would result |
| 609 | * in the same authentication token. |
| 610 | * |
| 611 | * This method only creates SP blob wrapping around the given synthetic password and does not |
| 612 | * handle logic around SID or SP handle. The caller should separately ensure that the user's SID |
| 613 | * is consistent with the device state by calling other APIs in this class. |
| 614 | * |
| 615 | * @see #newSidForUser |
| 616 | * @see #clearSidForUser |
| 617 | */ |
| 618 | public long createPasswordBasedSyntheticPassword(IGateKeeperService gatekeeper, |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 619 | byte[] credential, int credentialType, AuthenticationToken authToken, |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 620 | int requestedQuality, int userId) |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 621 | throws RemoteException { |
| 622 | if (credential == null || credentialType == LockPatternUtils.CREDENTIAL_TYPE_NONE) { |
| 623 | credentialType = LockPatternUtils.CREDENTIAL_TYPE_NONE; |
| 624 | credential = DEFAULT_PASSWORD; |
| 625 | } |
| 626 | |
| 627 | long handle = generateHandle(); |
| 628 | PasswordData pwd = PasswordData.create(credentialType); |
| 629 | byte[] pwdToken = computePasswordToken(credential, pwd); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 630 | final long sid; |
| 631 | final byte[] applicationId; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 632 | |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 633 | if (isWeaverAvailable()) { |
| 634 | // Weaver based user password |
| 635 | int weaverSlot = getNextAvailableWeaverSlot(); |
Rubin Xu | a339df0 | 2018-04-10 09:44:18 +0100 | [diff] [blame] | 636 | Log.i(TAG, "Weaver enroll password to slot " + weaverSlot + " for user " + userId); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 637 | byte[] weaverSecret = weaverEnroll(weaverSlot, passwordTokenToWeaverKey(pwdToken), null); |
| 638 | if (weaverSecret == null) { |
| 639 | Log.e(TAG, "Fail to enroll user password under weaver " + userId); |
| 640 | return DEFAULT_HANDLE; |
| 641 | } |
| 642 | saveWeaverSlot(weaverSlot, handle, userId); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 643 | synchronizeWeaverFrpPassword(pwd, requestedQuality, userId, weaverSlot); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 644 | |
| 645 | pwd.passwordHandle = null; |
| 646 | sid = GateKeeper.INVALID_SECURE_USER_ID; |
| 647 | applicationId = transformUnderWeaverSecret(pwdToken, weaverSecret); |
| 648 | } else { |
Rubin Xu | 54c19b6 | 2017-04-26 19:40:15 +0100 | [diff] [blame] | 649 | // In case GK enrollment leaves persistent state around (in RPMB), this will nuke them |
| 650 | // to prevent them from accumulating and causing problems. |
| 651 | gatekeeper.clearSecureUserId(fakeUid(userId)); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 652 | // GateKeeper based user password |
| 653 | GateKeeperResponse response = gatekeeper.enroll(fakeUid(userId), null, null, |
| 654 | passwordTokenToGkInput(pwdToken)); |
| 655 | if (response.getResponseCode() != GateKeeperResponse.RESPONSE_OK) { |
| 656 | Log.e(TAG, "Fail to enroll user password when creating SP for user " + userId); |
| 657 | return DEFAULT_HANDLE; |
| 658 | } |
| 659 | pwd.passwordHandle = response.getPayload(); |
| 660 | sid = sidFromPasswordHandle(pwd.passwordHandle); |
| 661 | applicationId = transformUnderSecdiscardable(pwdToken, |
| 662 | createSecdiscardable(handle, userId)); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 663 | synchronizeFrpPassword(pwd, requestedQuality, userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 664 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 665 | saveState(PASSWORD_DATA_NAME, pwd.toBytes(), handle, userId); |
| 666 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 667 | createSyntheticPasswordBlob(handle, SYNTHETIC_PASSWORD_PASSWORD_BASED, authToken, |
| 668 | applicationId, sid, userId); |
| 669 | return handle; |
| 670 | } |
| 671 | |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 672 | public VerifyCredentialResponse verifyFrpCredential(IGateKeeperService gatekeeper, |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 673 | byte[] userCredential, int credentialType, |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 674 | ICheckCredentialProgressCallback progressCallback) throws RemoteException { |
| 675 | PersistentData persistentData = mStorage.readPersistentDataBlock(); |
| 676 | if (persistentData.type == PersistentData.TYPE_SP) { |
| 677 | PasswordData pwd = PasswordData.fromBytes(persistentData.payload); |
| 678 | byte[] pwdToken = computePasswordToken(userCredential, pwd); |
| 679 | |
Adrian Roos | b7d6932 | 2017-08-08 18:32:03 +0200 | [diff] [blame] | 680 | GateKeeperResponse response = gatekeeper.verifyChallenge(fakeUid(persistentData.userId), |
| 681 | 0 /* challenge */, pwd.passwordHandle, passwordTokenToGkInput(pwdToken)); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 682 | return VerifyCredentialResponse.fromGateKeeperResponse(response); |
| 683 | } else if (persistentData.type == PersistentData.TYPE_SP_WEAVER) { |
| 684 | PasswordData pwd = PasswordData.fromBytes(persistentData.payload); |
| 685 | byte[] pwdToken = computePasswordToken(userCredential, pwd); |
| 686 | int weaverSlot = persistentData.userId; |
| 687 | |
| 688 | return weaverVerify(weaverSlot, passwordTokenToWeaverKey(pwdToken)).stripPayload(); |
| 689 | } else { |
| 690 | Log.e(TAG, "persistentData.type must be TYPE_SP or TYPE_SP_WEAVER, but is " |
| 691 | + persistentData.type); |
| 692 | return VerifyCredentialResponse.ERROR; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | |
Adrian Roos | 60dcbbf | 2017-08-08 16:19:33 +0200 | [diff] [blame] | 697 | public void migrateFrpPasswordLocked(long handle, UserInfo userInfo, int requestedQuality) { |
| 698 | if (mStorage.getPersistentDataBlock() != null |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 699 | && LockPatternUtils.userOwnsFrpCredential(mContext, userInfo)) { |
Adrian Roos | 60dcbbf | 2017-08-08 16:19:33 +0200 | [diff] [blame] | 700 | PasswordData pwd = PasswordData.fromBytes(loadState(PASSWORD_DATA_NAME, handle, |
| 701 | userInfo.id)); |
| 702 | if (pwd.passwordType != LockPatternUtils.CREDENTIAL_TYPE_NONE) { |
| 703 | int weaverSlot = loadWeaverSlot(handle, userInfo.id); |
| 704 | if (weaverSlot != INVALID_WEAVER_SLOT) { |
| 705 | synchronizeWeaverFrpPassword(pwd, requestedQuality, userInfo.id, weaverSlot); |
| 706 | } else { |
| 707 | synchronizeFrpPassword(pwd, requestedQuality, userInfo.id); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 713 | private void synchronizeFrpPassword(PasswordData pwd, |
| 714 | int requestedQuality, int userId) { |
| 715 | if (mStorage.getPersistentDataBlock() != null |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 716 | && LockPatternUtils.userOwnsFrpCredential(mContext, |
| 717 | mUserManager.getUserInfo(userId))) { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 718 | if (pwd.passwordType != LockPatternUtils.CREDENTIAL_TYPE_NONE) { |
| 719 | mStorage.writePersistentDataBlock(PersistentData.TYPE_SP, userId, requestedQuality, |
| 720 | pwd.toBytes()); |
| 721 | } else { |
| 722 | mStorage.writePersistentDataBlock(PersistentData.TYPE_NONE, userId, 0, null); |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | private void synchronizeWeaverFrpPassword(PasswordData pwd, int requestedQuality, int userId, |
| 728 | int weaverSlot) { |
| 729 | if (mStorage.getPersistentDataBlock() != null |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 730 | && LockPatternUtils.userOwnsFrpCredential(mContext, |
| 731 | mUserManager.getUserInfo(userId))) { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 732 | if (pwd.passwordType != LockPatternUtils.CREDENTIAL_TYPE_NONE) { |
| 733 | mStorage.writePersistentDataBlock(PersistentData.TYPE_SP_WEAVER, weaverSlot, |
| 734 | requestedQuality, pwd.toBytes()); |
| 735 | } else { |
| 736 | mStorage.writePersistentDataBlock(PersistentData.TYPE_NONE, 0, 0, null); |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 741 | private ArrayMap<Integer, ArrayMap<Long, TokenData>> tokenMap = new ArrayMap<>(); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 742 | |
| 743 | public long createTokenBasedSyntheticPassword(byte[] token, int userId) { |
| 744 | long handle = generateHandle(); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 745 | if (!tokenMap.containsKey(userId)) { |
| 746 | tokenMap.put(userId, new ArrayMap<>()); |
| 747 | } |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 748 | TokenData tokenData = new TokenData(); |
| 749 | final byte[] secdiscardable = secureRandom(SECDISCARDABLE_LENGTH); |
| 750 | if (isWeaverAvailable()) { |
| 751 | tokenData.weaverSecret = secureRandom(mWeaverConfig.valueSize); |
| 752 | tokenData.secdiscardableOnDisk = SyntheticPasswordCrypto.encrypt(tokenData.weaverSecret, |
| 753 | PERSONALISATION_WEAVER_TOKEN, secdiscardable); |
| 754 | } else { |
| 755 | tokenData.secdiscardableOnDisk = secdiscardable; |
| 756 | tokenData.weaverSecret = null; |
| 757 | } |
| 758 | tokenData.aggregatedSecret = transformUnderSecdiscardable(token, secdiscardable); |
| 759 | |
| 760 | tokenMap.get(userId).put(handle, tokenData); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 761 | return handle; |
| 762 | } |
| 763 | |
| 764 | public Set<Long> getPendingTokensForUser(int userId) { |
| 765 | if (!tokenMap.containsKey(userId)) { |
| 766 | return Collections.emptySet(); |
| 767 | } |
| 768 | return tokenMap.get(userId).keySet(); |
| 769 | } |
| 770 | |
| 771 | public boolean removePendingToken(long handle, int userId) { |
| 772 | if (!tokenMap.containsKey(userId)) { |
| 773 | return false; |
| 774 | } |
| 775 | return tokenMap.get(userId).remove(handle) != null; |
| 776 | } |
| 777 | |
| 778 | public boolean activateTokenBasedSyntheticPassword(long handle, AuthenticationToken authToken, |
| 779 | int userId) { |
| 780 | if (!tokenMap.containsKey(userId)) { |
| 781 | return false; |
| 782 | } |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 783 | TokenData tokenData = tokenMap.get(userId).get(handle); |
| 784 | if (tokenData == null) { |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 785 | return false; |
| 786 | } |
| 787 | if (!loadEscrowData(authToken, userId)) { |
| 788 | Log.w(TAG, "User is not escrowable"); |
| 789 | return false; |
| 790 | } |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 791 | if (isWeaverAvailable()) { |
| 792 | int slot = getNextAvailableWeaverSlot(); |
| 793 | try { |
Rubin Xu | a339df0 | 2018-04-10 09:44:18 +0100 | [diff] [blame] | 794 | Log.i(TAG, "Weaver enroll token to slot " + slot + " for user " + userId); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 795 | weaverEnroll(slot, null, tokenData.weaverSecret); |
| 796 | } catch (RemoteException e) { |
| 797 | Log.e(TAG, "Failed to enroll weaver secret when activating token", e); |
| 798 | return false; |
| 799 | } |
| 800 | saveWeaverSlot(slot, handle, userId); |
| 801 | } |
| 802 | saveSecdiscardable(handle, tokenData.secdiscardableOnDisk, userId); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 803 | createSyntheticPasswordBlob(handle, SYNTHETIC_PASSWORD_TOKEN_BASED, authToken, |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 804 | tokenData.aggregatedSecret, 0L, userId); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 805 | tokenMap.get(userId).remove(handle); |
| 806 | return true; |
| 807 | } |
| 808 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 809 | private void createSyntheticPasswordBlob(long handle, byte type, AuthenticationToken authToken, |
| 810 | byte[] applicationId, long sid, int userId) { |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 811 | final byte[] secret; |
| 812 | if (type == SYNTHETIC_PASSWORD_TOKEN_BASED) { |
| 813 | secret = authToken.computeP0(); |
| 814 | } else { |
| 815 | secret = authToken.syntheticPassword.getBytes(); |
| 816 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 817 | byte[] content = createSPBlob(getHandleName(handle), secret, applicationId, sid); |
| 818 | byte[] blob = new byte[content.length + 1 + 1]; |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 819 | /* |
| 820 | * We can upgrade from v1 to v2 because that's just a change in the way that |
| 821 | * the SP is stored. However, we can't upgrade to v3 because that is a change |
| 822 | * in the way that passwords are derived from the SP. |
| 823 | */ |
| 824 | if (authToken.mVersion == SYNTHETIC_PASSWORD_VERSION_V3) { |
| 825 | blob[0] = SYNTHETIC_PASSWORD_VERSION_V3; |
| 826 | } else { |
| 827 | blob[0] = SYNTHETIC_PASSWORD_VERSION_V2; |
| 828 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 829 | blob[1] = type; |
| 830 | System.arraycopy(content, 0, blob, 2, content.length); |
| 831 | saveState(SP_BLOB_NAME, blob, handle, userId); |
| 832 | } |
| 833 | |
| 834 | /** |
| 835 | * Decrypt a synthetic password by supplying the user credential and corresponding password |
| 836 | * blob handle generated previously. If the decryption is successful, initiate a GateKeeper |
| 837 | * verification to referesh the SID & Auth token maintained by the system. |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 838 | * Note: the credential type is not validated here since there are call sites where the type is |
| 839 | * unknown. Caller might choose to validate it by examining AuthenticationResult.credentialType |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 840 | */ |
| 841 | public AuthenticationResult unwrapPasswordBasedSyntheticPassword(IGateKeeperService gatekeeper, |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 842 | long handle, byte[] credential, int userId, |
Rubin Xu | cf326f1 | 2017-11-15 11:55:35 +0000 | [diff] [blame] | 843 | ICheckCredentialProgressCallback progressCallback) throws RemoteException { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 844 | if (credential == null) { |
| 845 | credential = DEFAULT_PASSWORD; |
| 846 | } |
| 847 | AuthenticationResult result = new AuthenticationResult(); |
| 848 | PasswordData pwd = PasswordData.fromBytes(loadState(PASSWORD_DATA_NAME, handle, userId)); |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 849 | result.credentialType = pwd.passwordType; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 850 | byte[] pwdToken = computePasswordToken(credential, pwd); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 851 | |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 852 | final byte[] applicationId; |
Rubin Xu | 8c52865 | 2017-10-31 15:40:32 +0000 | [diff] [blame] | 853 | final long sid; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 854 | int weaverSlot = loadWeaverSlot(handle, userId); |
| 855 | if (weaverSlot != INVALID_WEAVER_SLOT) { |
| 856 | // Weaver based user password |
| 857 | if (!isWeaverAvailable()) { |
| 858 | Log.e(TAG, "No weaver service to unwrap password based SP"); |
| 859 | result.gkResponse = VerifyCredentialResponse.ERROR; |
| 860 | return result; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 861 | } |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 862 | result.gkResponse = weaverVerify(weaverSlot, passwordTokenToWeaverKey(pwdToken)); |
| 863 | if (result.gkResponse.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK) { |
| 864 | return result; |
| 865 | } |
Rubin Xu | 8c52865 | 2017-10-31 15:40:32 +0000 | [diff] [blame] | 866 | sid = GateKeeper.INVALID_SECURE_USER_ID; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 867 | applicationId = transformUnderWeaverSecret(pwdToken, result.gkResponse.getPayload()); |
| 868 | } else { |
| 869 | byte[] gkPwdToken = passwordTokenToGkInput(pwdToken); |
| 870 | GateKeeperResponse response = gatekeeper.verifyChallenge(fakeUid(userId), 0L, |
| 871 | pwd.passwordHandle, gkPwdToken); |
| 872 | int responseCode = response.getResponseCode(); |
| 873 | if (responseCode == GateKeeperResponse.RESPONSE_OK) { |
| 874 | result.gkResponse = VerifyCredentialResponse.OK; |
| 875 | if (response.getShouldReEnroll()) { |
| 876 | GateKeeperResponse reenrollResponse = gatekeeper.enroll(fakeUid(userId), |
| 877 | pwd.passwordHandle, gkPwdToken, gkPwdToken); |
| 878 | if (reenrollResponse.getResponseCode() == GateKeeperResponse.RESPONSE_OK) { |
| 879 | pwd.passwordHandle = reenrollResponse.getPayload(); |
| 880 | saveState(PASSWORD_DATA_NAME, pwd.toBytes(), handle, userId); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 881 | synchronizeFrpPassword(pwd, |
| 882 | pwd.passwordType == LockPatternUtils.CREDENTIAL_TYPE_PATTERN |
| 883 | ? DevicePolicyManager.PASSWORD_QUALITY_SOMETHING |
| 884 | : DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC |
| 885 | /* TODO(roosa): keep the same password quality */, |
| 886 | userId); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 887 | } else { |
| 888 | Log.w(TAG, "Fail to re-enroll user password for user " + userId); |
| 889 | // continue the flow anyway |
| 890 | } |
| 891 | } |
| 892 | } else if (responseCode == GateKeeperResponse.RESPONSE_RETRY) { |
| 893 | result.gkResponse = new VerifyCredentialResponse(response.getTimeout()); |
| 894 | return result; |
| 895 | } else { |
| 896 | result.gkResponse = VerifyCredentialResponse.ERROR; |
| 897 | return result; |
| 898 | } |
Rubin Xu | 8c52865 | 2017-10-31 15:40:32 +0000 | [diff] [blame] | 899 | sid = sidFromPasswordHandle(pwd.passwordHandle); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 900 | applicationId = transformUnderSecdiscardable(pwdToken, |
| 901 | loadSecdiscardable(handle, userId)); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 902 | } |
Rubin Xu | cf326f1 | 2017-11-15 11:55:35 +0000 | [diff] [blame] | 903 | // Supplied credential passes first stage weaver/gatekeeper check so it should be correct. |
| 904 | // Notify the callback so the keyguard UI can proceed immediately. |
| 905 | if (progressCallback != null) { |
| 906 | progressCallback.onCredentialVerified(); |
| 907 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 908 | result.authToken = unwrapSyntheticPasswordBlob(handle, SYNTHETIC_PASSWORD_PASSWORD_BASED, |
Rubin Xu | 8c52865 | 2017-10-31 15:40:32 +0000 | [diff] [blame] | 909 | applicationId, sid, userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 910 | |
| 911 | // Perform verifyChallenge to refresh auth tokens for GK if user password exists. |
| 912 | result.gkResponse = verifyChallenge(gatekeeper, result.authToken, 0L, userId); |
| 913 | return result; |
| 914 | } |
| 915 | |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 916 | /** |
| 917 | * Decrypt a synthetic password by supplying an escrow token and corresponding token |
| 918 | * blob handle generated previously. If the decryption is successful, initiate a GateKeeper |
| 919 | * verification to referesh the SID & Auth token maintained by the system. |
| 920 | */ |
| 921 | public @NonNull AuthenticationResult unwrapTokenBasedSyntheticPassword( |
| 922 | IGateKeeperService gatekeeper, long handle, byte[] token, int userId) |
| 923 | throws RemoteException { |
| 924 | AuthenticationResult result = new AuthenticationResult(); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 925 | byte[] secdiscardable = loadSecdiscardable(handle, userId); |
| 926 | int slotId = loadWeaverSlot(handle, userId); |
| 927 | if (slotId != INVALID_WEAVER_SLOT) { |
| 928 | if (!isWeaverAvailable()) { |
| 929 | Log.e(TAG, "No weaver service to unwrap token based SP"); |
| 930 | result.gkResponse = VerifyCredentialResponse.ERROR; |
| 931 | return result; |
| 932 | } |
| 933 | VerifyCredentialResponse response = weaverVerify(slotId, null); |
| 934 | if (response.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK || |
| 935 | response.getPayload() == null) { |
| 936 | Log.e(TAG, "Failed to retrieve weaver secret when unwrapping token"); |
| 937 | result.gkResponse = VerifyCredentialResponse.ERROR; |
| 938 | return result; |
| 939 | } |
| 940 | secdiscardable = SyntheticPasswordCrypto.decrypt(response.getPayload(), |
| 941 | PERSONALISATION_WEAVER_TOKEN, secdiscardable); |
| 942 | } |
| 943 | byte[] applicationId = transformUnderSecdiscardable(token, secdiscardable); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 944 | result.authToken = unwrapSyntheticPasswordBlob(handle, SYNTHETIC_PASSWORD_TOKEN_BASED, |
Rubin Xu | 8c52865 | 2017-10-31 15:40:32 +0000 | [diff] [blame] | 945 | applicationId, 0L, userId); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 946 | if (result.authToken != null) { |
| 947 | result.gkResponse = verifyChallenge(gatekeeper, result.authToken, 0L, userId); |
| 948 | if (result.gkResponse == null) { |
| 949 | // The user currently has no password. return OK with null payload so null |
| 950 | // is propagated to unlockUser() |
| 951 | result.gkResponse = VerifyCredentialResponse.OK; |
| 952 | } |
| 953 | } else { |
| 954 | result.gkResponse = VerifyCredentialResponse.ERROR; |
| 955 | } |
| 956 | return result; |
| 957 | } |
| 958 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 959 | private AuthenticationToken unwrapSyntheticPasswordBlob(long handle, byte type, |
Rubin Xu | 8c52865 | 2017-10-31 15:40:32 +0000 | [diff] [blame] | 960 | byte[] applicationId, long sid, int userId) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 961 | byte[] blob = loadState(SP_BLOB_NAME, handle, userId); |
| 962 | if (blob == null) { |
| 963 | return null; |
| 964 | } |
Rubin Xu | 8c52865 | 2017-10-31 15:40:32 +0000 | [diff] [blame] | 965 | final byte version = blob[0]; |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 966 | if (version != SYNTHETIC_PASSWORD_VERSION_V3 |
| 967 | && version != SYNTHETIC_PASSWORD_VERSION_V2 |
| 968 | && version != SYNTHETIC_PASSWORD_VERSION_V1) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 969 | throw new RuntimeException("Unknown blob version"); |
| 970 | } |
| 971 | if (blob[1] != type) { |
| 972 | throw new RuntimeException("Invalid blob type"); |
| 973 | } |
Rubin Xu | 8c52865 | 2017-10-31 15:40:32 +0000 | [diff] [blame] | 974 | final byte[] secret; |
| 975 | if (version == SYNTHETIC_PASSWORD_VERSION_V1) { |
| 976 | secret = SyntheticPasswordCrypto.decryptBlobV1(getHandleName(handle), |
| 977 | Arrays.copyOfRange(blob, 2, blob.length), applicationId); |
| 978 | } else { |
| 979 | secret = decryptSPBlob(getHandleName(handle), |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 980 | Arrays.copyOfRange(blob, 2, blob.length), applicationId); |
Rubin Xu | 8c52865 | 2017-10-31 15:40:32 +0000 | [diff] [blame] | 981 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 982 | if (secret == null) { |
| 983 | Log.e(TAG, "Fail to decrypt SP for user " + userId); |
| 984 | return null; |
| 985 | } |
Paul Crowley | 0d40d6e7 | 2018-11-28 12:48:16 -0800 | [diff] [blame] | 986 | AuthenticationToken result = new AuthenticationToken(version); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 987 | if (type == SYNTHETIC_PASSWORD_TOKEN_BASED) { |
| 988 | if (!loadEscrowData(result, userId)) { |
| 989 | Log.e(TAG, "User is not escrowable: " + userId); |
| 990 | return null; |
| 991 | } |
| 992 | result.recreate(secret); |
| 993 | } else { |
| 994 | result.syntheticPassword = new String(secret); |
| 995 | } |
Rubin Xu | 8c52865 | 2017-10-31 15:40:32 +0000 | [diff] [blame] | 996 | if (version == SYNTHETIC_PASSWORD_VERSION_V1) { |
| 997 | Log.i(TAG, "Upgrade v1 SP blob for user " + userId + ", type = " + type); |
| 998 | createSyntheticPasswordBlob(handle, type, result, applicationId, sid, userId); |
| 999 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1000 | return result; |
| 1001 | } |
| 1002 | |
| 1003 | /** |
| 1004 | * performs GK verifyChallenge and returns auth token, re-enrolling SP password handle |
| 1005 | * if required. |
| 1006 | * |
| 1007 | * Normally performing verifyChallenge with an AuthenticationToken should always return |
| 1008 | * RESPONSE_OK, since user authentication failures are detected earlier when trying to |
| 1009 | * decrypt SP. |
| 1010 | */ |
Rubin Xu | 8b30ec3 | 2017-03-05 00:47:09 +0000 | [diff] [blame] | 1011 | public @Nullable VerifyCredentialResponse verifyChallenge(IGateKeeperService gatekeeper, |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1012 | @NonNull AuthenticationToken auth, long challenge, int userId) throws RemoteException { |
| 1013 | byte[] spHandle = loadSyntheticPasswordHandle(userId); |
| 1014 | if (spHandle == null) { |
| 1015 | // There is no password handle associated with the given user, i.e. the user is not |
| 1016 | // secured by lockscreen and has no SID, so just return here; |
| 1017 | return null; |
| 1018 | } |
| 1019 | VerifyCredentialResponse result; |
| 1020 | GateKeeperResponse response = gatekeeper.verifyChallenge(userId, challenge, |
| 1021 | spHandle, auth.deriveGkPassword()); |
| 1022 | int responseCode = response.getResponseCode(); |
| 1023 | if (responseCode == GateKeeperResponse.RESPONSE_OK) { |
| 1024 | result = new VerifyCredentialResponse(response.getPayload()); |
| 1025 | if (response.getShouldReEnroll()) { |
| 1026 | response = gatekeeper.enroll(userId, spHandle, |
| 1027 | spHandle, auth.deriveGkPassword()); |
| 1028 | if (response.getResponseCode() == GateKeeperResponse.RESPONSE_OK) { |
| 1029 | spHandle = response.getPayload(); |
| 1030 | saveSyntheticPasswordHandle(spHandle, userId); |
| 1031 | // Call self again to re-verify with updated handle |
| 1032 | return verifyChallenge(gatekeeper, auth, challenge, userId); |
| 1033 | } else { |
| 1034 | Log.w(TAG, "Fail to re-enroll SP handle for user " + userId); |
| 1035 | // Fall through, return existing handle |
| 1036 | } |
| 1037 | } |
| 1038 | } else if (responseCode == GateKeeperResponse.RESPONSE_RETRY) { |
| 1039 | result = new VerifyCredentialResponse(response.getTimeout()); |
| 1040 | } else { |
| 1041 | result = VerifyCredentialResponse.ERROR; |
| 1042 | } |
| 1043 | return result; |
| 1044 | } |
| 1045 | |
| 1046 | public boolean existsHandle(long handle, int userId) { |
| 1047 | return hasState(SP_BLOB_NAME, handle, userId); |
| 1048 | } |
| 1049 | |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 1050 | public void destroyTokenBasedSyntheticPassword(long handle, int userId) { |
| 1051 | destroySyntheticPassword(handle, userId); |
Rubin Xu | aa32d15 | 2017-04-27 17:01:05 +0100 | [diff] [blame] | 1052 | destroyState(SECDISCARDABLE_NAME, handle, userId); |
Rubin Xu | f095f83 | 2017-01-31 15:23:34 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1055 | public void destroyPasswordBasedSyntheticPassword(long handle, int userId) { |
| 1056 | destroySyntheticPassword(handle, userId); |
Rubin Xu | aa32d15 | 2017-04-27 17:01:05 +0100 | [diff] [blame] | 1057 | destroyState(SECDISCARDABLE_NAME, handle, userId); |
| 1058 | destroyState(PASSWORD_DATA_NAME, handle, userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | private void destroySyntheticPassword(long handle, int userId) { |
Rubin Xu | aa32d15 | 2017-04-27 17:01:05 +0100 | [diff] [blame] | 1062 | destroyState(SP_BLOB_NAME, handle, userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1063 | destroySPBlobKey(getHandleName(handle)); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 1064 | if (hasState(WEAVER_SLOT_NAME, handle, userId)) { |
| 1065 | destroyWeaverSlot(handle, userId); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | private byte[] transformUnderWeaverSecret(byte[] data, byte[] secret) { |
| 1070 | byte[] weaverSecret = SyntheticPasswordCrypto.personalisedHash( |
| 1071 | PERSONALISATION_WEAVER_PASSWORD, secret); |
| 1072 | byte[] result = new byte[data.length + weaverSecret.length]; |
| 1073 | System.arraycopy(data, 0, result, 0, data.length); |
| 1074 | System.arraycopy(weaverSecret, 0, result, data.length, weaverSecret.length); |
| 1075 | return result; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | private byte[] transformUnderSecdiscardable(byte[] data, byte[] rawSecdiscardable) { |
| 1079 | byte[] secdiscardable = SyntheticPasswordCrypto.personalisedHash( |
| 1080 | PERSONALISATION_SECDISCARDABLE, rawSecdiscardable); |
| 1081 | byte[] result = new byte[data.length + secdiscardable.length]; |
| 1082 | System.arraycopy(data, 0, result, 0, data.length); |
| 1083 | System.arraycopy(secdiscardable, 0, result, data.length, secdiscardable.length); |
| 1084 | return result; |
| 1085 | } |
| 1086 | |
| 1087 | private byte[] createSecdiscardable(long handle, int userId) { |
| 1088 | byte[] data = secureRandom(SECDISCARDABLE_LENGTH); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 1089 | saveSecdiscardable(handle, data, userId); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1090 | return data; |
| 1091 | } |
| 1092 | |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 1093 | private void saveSecdiscardable(long handle, byte[] secdiscardable, int userId) { |
| 1094 | saveState(SECDISCARDABLE_NAME, secdiscardable, handle, userId); |
| 1095 | } |
| 1096 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1097 | private byte[] loadSecdiscardable(long handle, int userId) { |
| 1098 | return loadState(SECDISCARDABLE_NAME, handle, userId); |
| 1099 | } |
| 1100 | |
| 1101 | private boolean hasState(String stateName, long handle, int userId) { |
| 1102 | return !ArrayUtils.isEmpty(loadState(stateName, handle, userId)); |
| 1103 | } |
| 1104 | |
| 1105 | private byte[] loadState(String stateName, long handle, int userId) { |
| 1106 | return mStorage.readSyntheticPasswordState(userId, handle, stateName); |
| 1107 | } |
| 1108 | |
| 1109 | private void saveState(String stateName, byte[] data, long handle, int userId) { |
| 1110 | mStorage.writeSyntheticPasswordState(userId, handle, stateName, data); |
| 1111 | } |
| 1112 | |
Rubin Xu | aa32d15 | 2017-04-27 17:01:05 +0100 | [diff] [blame] | 1113 | private void destroyState(String stateName, long handle, int userId) { |
| 1114 | mStorage.deleteSyntheticPasswordState(userId, handle, stateName); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | protected byte[] decryptSPBlob(String blobKeyName, byte[] blob, byte[] applicationId) { |
| 1118 | return SyntheticPasswordCrypto.decryptBlob(blobKeyName, blob, applicationId); |
| 1119 | } |
| 1120 | |
| 1121 | protected byte[] createSPBlob(String blobKeyName, byte[] data, byte[] applicationId, long sid) { |
| 1122 | return SyntheticPasswordCrypto.createBlob(blobKeyName, data, applicationId, sid); |
| 1123 | } |
| 1124 | |
| 1125 | protected void destroySPBlobKey(String keyAlias) { |
| 1126 | SyntheticPasswordCrypto.destroyBlobKey(keyAlias); |
| 1127 | } |
| 1128 | |
| 1129 | public static long generateHandle() { |
| 1130 | SecureRandom rng = new SecureRandom(); |
| 1131 | long result; |
| 1132 | do { |
| 1133 | result = rng.nextLong(); |
| 1134 | } while (result == DEFAULT_HANDLE); |
| 1135 | return result; |
| 1136 | } |
| 1137 | |
| 1138 | private int fakeUid(int uid) { |
| 1139 | return 100000 + uid; |
| 1140 | } |
| 1141 | |
| 1142 | protected static byte[] secureRandom(int length) { |
| 1143 | try { |
| 1144 | return SecureRandom.getInstance("SHA1PRNG").generateSeed(length); |
| 1145 | } catch (NoSuchAlgorithmException e) { |
| 1146 | e.printStackTrace(); |
| 1147 | return null; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | private String getHandleName(long handle) { |
| 1152 | return String.format("%s%x", LockPatternUtils.SYNTHETIC_PASSWORD_KEY_PREFIX, handle); |
| 1153 | } |
| 1154 | |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1155 | private byte[] computePasswordToken(byte[] password, PasswordData data) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1156 | return scrypt(password, data.salt, 1 << data.scryptN, 1 << data.scryptR, 1 << data.scryptP, |
| 1157 | PASSWORD_TOKEN_LENGTH); |
| 1158 | } |
| 1159 | |
| 1160 | private byte[] passwordTokenToGkInput(byte[] token) { |
| 1161 | return SyntheticPasswordCrypto.personalisedHash(PERSONALIZATION_USER_GK_AUTH, token); |
| 1162 | } |
| 1163 | |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 1164 | private byte[] passwordTokenToWeaverKey(byte[] token) { |
| 1165 | byte[] key = SyntheticPasswordCrypto.personalisedHash(PERSONALISATION_WEAVER_KEY, token); |
| 1166 | if (key.length < mWeaverConfig.keySize) { |
| 1167 | throw new RuntimeException("weaver key length too small"); |
| 1168 | } |
| 1169 | return Arrays.copyOf(key, mWeaverConfig.keySize); |
| 1170 | } |
| 1171 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1172 | protected long sidFromPasswordHandle(byte[] handle) { |
| 1173 | return nativeSidFromPasswordHandle(handle); |
| 1174 | } |
| 1175 | |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1176 | protected byte[] scrypt(byte[] password, byte[] salt, int n, int r, int p, int outLen) { |
| 1177 | return new Scrypt().scrypt(password, salt, n, r, p, outLen); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | native long nativeSidFromPasswordHandle(byte[] handle); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1181 | |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 1182 | protected static ArrayList<Byte> toByteArrayList(byte[] data) { |
| 1183 | ArrayList<Byte> result = new ArrayList<Byte>(data.length); |
| 1184 | for (int i = 0; i < data.length; i++) { |
| 1185 | result.add(data[i]); |
| 1186 | } |
| 1187 | return result; |
| 1188 | } |
| 1189 | |
| 1190 | protected static byte[] fromByteArrayList(ArrayList<Byte> data) { |
| 1191 | byte[] result = new byte[data.size()]; |
| 1192 | for (int i = 0; i < data.size(); i++) { |
| 1193 | result[i] = data.get(i); |
| 1194 | } |
| 1195 | return result; |
| 1196 | } |
| 1197 | |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1198 | protected static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(); |
| 1199 | private static byte[] bytesToHex(byte[] bytes) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1200 | if (bytes == null) { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1201 | return "null".getBytes(); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1202 | } |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1203 | byte[] hexBytes = new byte[bytes.length * 2]; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1204 | for ( int j = 0; j < bytes.length; j++ ) { |
| 1205 | int v = bytes[j] & 0xFF; |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1206 | hexBytes[j * 2] = HEX_ARRAY[v >>> 4]; |
| 1207 | hexBytes[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1208 | } |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 1209 | return hexBytes; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 1210 | } |
| 1211 | } |