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