Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "keystore" |
| 18 | |
| 19 | #include "user_state.h" |
| 20 | |
| 21 | #include <dirent.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <sys/stat.h> |
| 26 | |
| 27 | #include <openssl/evp.h> |
Branden Archer | 44d1afa | 2018-12-28 09:10:49 -0800 | [diff] [blame^] | 28 | #include <openssl/rand.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 29 | |
Logan Chien | cdc813f | 2018-04-23 13:52:28 +0800 | [diff] [blame] | 30 | #include <log/log.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 31 | |
| 32 | #include "blob.h" |
| 33 | #include "keystore_utils.h" |
| 34 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 35 | namespace keystore { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 36 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 37 | UserState::UserState(uid_t userId) |
| 38 | : mMasterKeyEntry(".masterkey", "user_" + std::to_string(userId), userId, /* masterkey */ true), |
| 39 | mUserId(userId), mState(STATE_UNINITIALIZED), mRetry(MAX_RETRY) {} |
| 40 | |
| 41 | bool UserState::operator<(const UserState& rhs) const { |
| 42 | return getUserId() < rhs.getUserId(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 45 | bool UserState::operator<(uid_t userId) const { |
| 46 | return getUserId() < userId; |
| 47 | } |
| 48 | |
| 49 | bool operator<(uid_t userId, const UserState& rhs) { |
| 50 | return userId < rhs.getUserId(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool UserState::initialize() { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 54 | if ((mkdir(mMasterKeyEntry.user_dir().c_str(), S_IRUSR | S_IWUSR | S_IXUSR) < 0) && |
| 55 | (errno != EEXIST)) { |
| 56 | ALOGE("Could not create directory '%s'", mMasterKeyEntry.user_dir().c_str()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 60 | if (mMasterKeyEntry.hasKeyBlob()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 61 | setState(STATE_LOCKED); |
| 62 | } else { |
| 63 | setState(STATE_UNINITIALIZED); |
| 64 | } |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | void UserState::setState(State state) { |
| 70 | mState = state; |
| 71 | if (mState == STATE_NO_ERROR || mState == STATE_UNINITIALIZED) { |
| 72 | mRetry = MAX_RETRY; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void UserState::zeroizeMasterKeysInMemory() { |
| 77 | memset(mMasterKey, 0, sizeof(mMasterKey)); |
| 78 | memset(mSalt, 0, sizeof(mSalt)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | bool UserState::deleteMasterKey() { |
| 82 | setState(STATE_UNINITIALIZED); |
| 83 | zeroizeMasterKeysInMemory(); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 84 | return unlink(mMasterKeyEntry.getKeyBlobPath().c_str()) == 0 || errno == ENOENT; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Branden Archer | 44d1afa | 2018-12-28 09:10:49 -0800 | [diff] [blame^] | 87 | ResponseCode UserState::initialize(const android::String8& pw) { |
| 88 | if (!generateMasterKey()) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 89 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 90 | } |
Branden Archer | 44d1afa | 2018-12-28 09:10:49 -0800 | [diff] [blame^] | 91 | ResponseCode response = writeMasterKey(pw); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 92 | if (response != ResponseCode::NO_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 93 | return response; |
| 94 | } |
| 95 | setupMasterKeys(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 96 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 99 | ResponseCode UserState::copyMasterKey(LockedUserState<UserState>* src) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 100 | if (mState != STATE_UNINITIALIZED) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 101 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 102 | } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 103 | if ((*src)->getState() != STATE_NO_ERROR) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 104 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 105 | } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 106 | memcpy(mMasterKey, (*src)->mMasterKey, MASTER_KEY_SIZE_BYTES); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 107 | setupMasterKeys(); |
| 108 | return copyMasterKeyFile(src); |
| 109 | } |
| 110 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 111 | ResponseCode UserState::copyMasterKeyFile(LockedUserState<UserState>* src) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 112 | /* Copy the master key file to the new user. Unfortunately we don't have the src user's |
| 113 | * password so we cannot generate a new file with a new salt. |
| 114 | */ |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 115 | int in = TEMP_FAILURE_RETRY(open((*src)->getMasterKeyFileName().c_str(), O_RDONLY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 116 | if (in < 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 117 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 118 | } |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 119 | blobv3 rawBlob; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 120 | size_t length = readFully(in, (uint8_t*)&rawBlob, sizeof(rawBlob)); |
| 121 | if (close(in) != 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 122 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 123 | } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 124 | int out = TEMP_FAILURE_RETRY(open(mMasterKeyEntry.getKeyBlobPath().c_str(), |
| 125 | O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 126 | if (out < 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 127 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 128 | } |
| 129 | size_t outLength = writeFully(out, (uint8_t*)&rawBlob, length); |
| 130 | if (close(out) != 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 131 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 132 | } |
| 133 | if (outLength != length) { |
| 134 | ALOGW("blob not fully written %zu != %zu", outLength, length); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 135 | unlink(mMasterKeyEntry.getKeyBlobPath().c_str()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 136 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 137 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 138 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Branden Archer | 44d1afa | 2018-12-28 09:10:49 -0800 | [diff] [blame^] | 141 | ResponseCode UserState::writeMasterKey(const android::String8& pw) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 142 | uint8_t passwordKey[MASTER_KEY_SIZE_BYTES]; |
| 143 | generateKeyFromPassword(passwordKey, MASTER_KEY_SIZE_BYTES, pw, mSalt); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 144 | Blob masterKeyBlob(mMasterKey, sizeof(mMasterKey), mSalt, sizeof(mSalt), TYPE_MASTER_KEY); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 145 | auto lockedEntry = LockedKeyBlobEntry::get(mMasterKeyEntry); |
Branden Archer | 44d1afa | 2018-12-28 09:10:49 -0800 | [diff] [blame^] | 146 | return lockedEntry.writeBlobs(masterKeyBlob, {}, passwordKey, STATE_NO_ERROR); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Branden Archer | 44d1afa | 2018-12-28 09:10:49 -0800 | [diff] [blame^] | 149 | ResponseCode UserState::readMasterKey(const android::String8& pw) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 150 | |
| 151 | auto lockedEntry = LockedKeyBlobEntry::get(mMasterKeyEntry); |
| 152 | |
| 153 | int in = TEMP_FAILURE_RETRY(open(mMasterKeyEntry.getKeyBlobPath().c_str(), O_RDONLY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 154 | if (in < 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 155 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // We read the raw blob to just to get the salt to generate the AES key, then we create the Blob |
| 159 | // to use with decryptBlob |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 160 | blobv3 rawBlob; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 161 | size_t length = readFully(in, (uint8_t*)&rawBlob, sizeof(rawBlob)); |
| 162 | if (close(in) != 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 163 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 164 | } |
| 165 | // find salt at EOF if present, otherwise we have an old file |
| 166 | uint8_t* salt; |
| 167 | if (length > SALT_SIZE && rawBlob.info == SALT_SIZE) { |
| 168 | salt = (uint8_t*)&rawBlob + length - SALT_SIZE; |
| 169 | } else { |
Yi Kong | d291675 | 2018-07-26 17:44:27 -0700 | [diff] [blame] | 170 | salt = nullptr; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 171 | } |
| 172 | uint8_t passwordKey[MASTER_KEY_SIZE_BYTES]; |
| 173 | generateKeyFromPassword(passwordKey, MASTER_KEY_SIZE_BYTES, pw, salt); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 174 | Blob masterKeyBlob, dummyBlob; |
| 175 | ResponseCode response; |
| 176 | std::tie(response, masterKeyBlob, dummyBlob) = |
| 177 | lockedEntry.readBlobs(passwordKey, STATE_NO_ERROR); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 178 | if (response == ResponseCode::SYSTEM_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 179 | return response; |
| 180 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 181 | if (response == ResponseCode::NO_ERROR && masterKeyBlob.getLength() == MASTER_KEY_SIZE_BYTES) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 182 | // If salt was missing, generate one and write a new master key file with the salt. |
Yi Kong | d291675 | 2018-07-26 17:44:27 -0700 | [diff] [blame] | 183 | if (salt == nullptr) { |
Branden Archer | 44d1afa | 2018-12-28 09:10:49 -0800 | [diff] [blame^] | 184 | if (!generateSalt()) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 185 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 186 | } |
Branden Archer | 44d1afa | 2018-12-28 09:10:49 -0800 | [diff] [blame^] | 187 | response = writeMasterKey(pw); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 188 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 189 | if (response == ResponseCode::NO_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 190 | memcpy(mMasterKey, masterKeyBlob.getValue(), MASTER_KEY_SIZE_BYTES); |
| 191 | setupMasterKeys(); |
| 192 | } |
| 193 | return response; |
| 194 | } |
| 195 | if (mRetry <= 0) { |
| 196 | reset(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 197 | return ResponseCode::UNINITIALIZED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 198 | } |
| 199 | --mRetry; |
| 200 | switch (mRetry) { |
| 201 | case 0: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 202 | return ResponseCode::WRONG_PASSWORD_0; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 203 | case 1: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 204 | return ResponseCode::WRONG_PASSWORD_1; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 205 | case 2: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 206 | return ResponseCode::WRONG_PASSWORD_2; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 207 | case 3: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 208 | return ResponseCode::WRONG_PASSWORD_3; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 209 | default: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 210 | return ResponseCode::WRONG_PASSWORD_3; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | bool UserState::reset() { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 215 | DIR* dir = opendir(mMasterKeyEntry.user_dir().c_str()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 216 | if (!dir) { |
| 217 | // If the directory doesn't exist then nothing to do. |
| 218 | if (errno == ENOENT) { |
| 219 | return true; |
| 220 | } |
| 221 | ALOGW("couldn't open user directory: %s", strerror(errno)); |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | struct dirent* file; |
Yi Kong | d291675 | 2018-07-26 17:44:27 -0700 | [diff] [blame] | 226 | while ((file = readdir(dir)) != nullptr) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 227 | // skip . and .. |
| 228 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | unlinkat(dirfd(dir), file->d_name, 0); |
| 233 | } |
| 234 | closedir(dir); |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | void UserState::generateKeyFromPassword(uint8_t* key, ssize_t keySize, const android::String8& pw, |
| 239 | uint8_t* salt) { |
| 240 | size_t saltSize; |
Yi Kong | d291675 | 2018-07-26 17:44:27 -0700 | [diff] [blame] | 241 | if (salt != nullptr) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 242 | saltSize = SALT_SIZE; |
| 243 | } else { |
| 244 | // Pre-gingerbread used this hardwired salt, readMasterKey will rewrite these when found |
| 245 | salt = (uint8_t*)"keystore"; |
| 246 | // sizeof = 9, not strlen = 8 |
| 247 | saltSize = sizeof("keystore"); |
| 248 | } |
| 249 | |
| 250 | PKCS5_PBKDF2_HMAC_SHA1(reinterpret_cast<const char*>(pw.string()), pw.length(), salt, saltSize, |
| 251 | 8192, keySize, key); |
| 252 | } |
| 253 | |
Branden Archer | 44d1afa | 2018-12-28 09:10:49 -0800 | [diff] [blame^] | 254 | bool UserState::generateSalt() { |
| 255 | return RAND_bytes(mSalt, sizeof(mSalt)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Branden Archer | 44d1afa | 2018-12-28 09:10:49 -0800 | [diff] [blame^] | 258 | bool UserState::generateMasterKey() { |
| 259 | if (!RAND_bytes(mMasterKey, sizeof(mMasterKey))) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 260 | return false; |
| 261 | } |
Branden Archer | 44d1afa | 2018-12-28 09:10:49 -0800 | [diff] [blame^] | 262 | if (!generateSalt()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 263 | return false; |
| 264 | } |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | void UserState::setupMasterKeys() { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 269 | setState(STATE_NO_ERROR); |
| 270 | } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 271 | |
| 272 | LockedUserState<UserState> UserStateDB::getUserState(uid_t userId) { |
| 273 | std::unique_lock<std::mutex> lock(locked_state_mutex_); |
| 274 | decltype(mMasterKeys.begin()) it; |
| 275 | bool inserted; |
| 276 | std::tie(it, inserted) = mMasterKeys.emplace(userId, userId); |
| 277 | if (inserted) { |
| 278 | if (!it->second.initialize()) { |
| 279 | /* There's not much we can do if initialization fails. Trying to |
| 280 | * unlock the keystore for that user will fail as well, so any |
| 281 | * subsequent request for this user will just return SYSTEM_ERROR. |
| 282 | */ |
| 283 | ALOGE("User initialization failed for %u; subsequent operations will fail", userId); |
| 284 | } |
| 285 | } |
| 286 | return get(std::move(lock), &it->second); |
| 287 | } |
| 288 | |
| 289 | LockedUserState<UserState> UserStateDB::getUserStateByUid(uid_t uid) { |
| 290 | return getUserState(get_user_id(uid)); |
| 291 | } |
| 292 | |
| 293 | LockedUserState<const UserState> UserStateDB::getUserState(uid_t userId) const { |
| 294 | std::unique_lock<std::mutex> lock(locked_state_mutex_); |
| 295 | auto it = mMasterKeys.find(userId); |
| 296 | if (it == mMasterKeys.end()) return {}; |
| 297 | return get(std::move(lock), &it->second); |
| 298 | } |
| 299 | |
| 300 | LockedUserState<const UserState> UserStateDB::getUserStateByUid(uid_t uid) const { |
| 301 | return getUserState(get_user_id(uid)); |
| 302 | } |
| 303 | |
| 304 | } // namespace keystore |