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