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 | 020d535 | 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 | e489a03 | 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 | |
| 31 | #include <cutils/log.h> |
| 32 | |
| 33 | #include "blob.h" |
| 34 | #include "keystore_utils.h" |
| 35 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 36 | |
| 37 | UserState::UserState(uid_t userId) : |
| 38 | mUserId(userId), mState(STATE_UNINITIALIZED), mRetry(MAX_RETRY) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 39 | asprintf(&mUserDir, "user_%u", mUserId); |
| 40 | asprintf(&mMasterKeyFile, "%s/.masterkey", mUserDir); |
| 41 | } |
| 42 | |
| 43 | UserState::~UserState() { |
| 44 | free(mUserDir); |
| 45 | free(mMasterKeyFile); |
| 46 | } |
| 47 | |
| 48 | bool UserState::initialize() { |
| 49 | if ((mkdir(mUserDir, S_IRUSR | S_IWUSR | S_IXUSR) < 0) && (errno != EEXIST)) { |
| 50 | ALOGE("Could not create directory '%s'", mUserDir); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | if (access(mMasterKeyFile, R_OK) == 0) { |
| 55 | setState(STATE_LOCKED); |
| 56 | } else { |
| 57 | setState(STATE_UNINITIALIZED); |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | void UserState::setState(State state) { |
| 64 | mState = state; |
| 65 | if (mState == STATE_NO_ERROR || mState == STATE_UNINITIALIZED) { |
| 66 | mRetry = MAX_RETRY; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void UserState::zeroizeMasterKeysInMemory() { |
| 71 | memset(mMasterKey, 0, sizeof(mMasterKey)); |
| 72 | memset(mSalt, 0, sizeof(mSalt)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool UserState::deleteMasterKey() { |
| 76 | setState(STATE_UNINITIALIZED); |
| 77 | zeroizeMasterKeysInMemory(); |
| 78 | return unlink(mMasterKeyFile) == 0 || errno == ENOENT; |
| 79 | } |
| 80 | |
Branden Archer | e489a03 | 2018-12-28 09:10:49 -0800 | [diff] [blame] | 81 | ResponseCode UserState::initialize(const android::String8& pw) { |
| 82 | if (!generateMasterKey()) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 83 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 84 | } |
Branden Archer | e489a03 | 2018-12-28 09:10:49 -0800 | [diff] [blame] | 85 | ResponseCode response = writeMasterKey(pw); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 86 | if (response != ResponseCode::NO_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 87 | return response; |
| 88 | } |
| 89 | setupMasterKeys(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 90 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | ResponseCode UserState::copyMasterKey(UserState* src) { |
| 94 | if (mState != STATE_UNINITIALIZED) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 95 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 96 | } |
| 97 | if (src->getState() != STATE_NO_ERROR) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 98 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 99 | } |
| 100 | memcpy(mMasterKey, src->mMasterKey, MASTER_KEY_SIZE_BYTES); |
| 101 | setupMasterKeys(); |
| 102 | return copyMasterKeyFile(src); |
| 103 | } |
| 104 | |
| 105 | ResponseCode UserState::copyMasterKeyFile(UserState* src) { |
| 106 | /* Copy the master key file to the new user. Unfortunately we don't have the src user's |
| 107 | * password so we cannot generate a new file with a new salt. |
| 108 | */ |
| 109 | int in = TEMP_FAILURE_RETRY(open(src->getMasterKeyFileName(), O_RDONLY)); |
| 110 | if (in < 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 111 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 112 | } |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 113 | blobv3 rawBlob; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 114 | size_t length = readFully(in, (uint8_t*)&rawBlob, sizeof(rawBlob)); |
| 115 | if (close(in) != 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 116 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 117 | } |
| 118 | int out = |
| 119 | TEMP_FAILURE_RETRY(open(mMasterKeyFile, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR)); |
| 120 | if (out < 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 121 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 122 | } |
| 123 | size_t outLength = writeFully(out, (uint8_t*)&rawBlob, length); |
| 124 | if (close(out) != 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 125 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 126 | } |
| 127 | if (outLength != length) { |
| 128 | ALOGW("blob not fully written %zu != %zu", outLength, length); |
| 129 | unlink(mMasterKeyFile); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 130 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 131 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 132 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Branden Archer | e489a03 | 2018-12-28 09:10:49 -0800 | [diff] [blame] | 135 | ResponseCode UserState::writeMasterKey(const android::String8& pw) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 136 | uint8_t passwordKey[MASTER_KEY_SIZE_BYTES]; |
| 137 | generateKeyFromPassword(passwordKey, MASTER_KEY_SIZE_BYTES, pw, mSalt); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 138 | Blob masterKeyBlob(mMasterKey, sizeof(mMasterKey), mSalt, sizeof(mSalt), TYPE_MASTER_KEY); |
Branden Archer | e489a03 | 2018-12-28 09:10:49 -0800 | [diff] [blame] | 139 | return masterKeyBlob.writeBlob(mMasterKeyFile, passwordKey, STATE_NO_ERROR); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Branden Archer | e489a03 | 2018-12-28 09:10:49 -0800 | [diff] [blame] | 142 | ResponseCode UserState::readMasterKey(const android::String8& pw) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 143 | int in = TEMP_FAILURE_RETRY(open(mMasterKeyFile, O_RDONLY)); |
| 144 | if (in < 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 145 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // We read the raw blob to just to get the salt to generate the AES key, then we create the Blob |
| 149 | // to use with decryptBlob |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 150 | blobv3 rawBlob; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 151 | size_t length = readFully(in, (uint8_t*)&rawBlob, sizeof(rawBlob)); |
| 152 | if (close(in) != 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 153 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 154 | } |
| 155 | // find salt at EOF if present, otherwise we have an old file |
| 156 | uint8_t* salt; |
| 157 | if (length > SALT_SIZE && rawBlob.info == SALT_SIZE) { |
| 158 | salt = (uint8_t*)&rawBlob + length - SALT_SIZE; |
| 159 | } else { |
| 160 | salt = NULL; |
| 161 | } |
| 162 | uint8_t passwordKey[MASTER_KEY_SIZE_BYTES]; |
| 163 | generateKeyFromPassword(passwordKey, MASTER_KEY_SIZE_BYTES, pw, salt); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 164 | Blob masterKeyBlob(rawBlob); |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 165 | ResponseCode response = masterKeyBlob.readBlob(mMasterKeyFile, passwordKey, STATE_NO_ERROR); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 166 | if (response == ResponseCode::SYSTEM_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 167 | return response; |
| 168 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 169 | if (response == ResponseCode::NO_ERROR && masterKeyBlob.getLength() == MASTER_KEY_SIZE_BYTES) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 170 | // If salt was missing, generate one and write a new master key file with the salt. |
| 171 | if (salt == NULL) { |
Branden Archer | e489a03 | 2018-12-28 09:10:49 -0800 | [diff] [blame] | 172 | if (!generateSalt()) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 173 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 174 | } |
Branden Archer | e489a03 | 2018-12-28 09:10:49 -0800 | [diff] [blame] | 175 | response = writeMasterKey(pw); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 176 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 177 | if (response == ResponseCode::NO_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 178 | memcpy(mMasterKey, masterKeyBlob.getValue(), MASTER_KEY_SIZE_BYTES); |
| 179 | setupMasterKeys(); |
| 180 | } |
| 181 | return response; |
| 182 | } |
| 183 | if (mRetry <= 0) { |
| 184 | reset(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 185 | return ResponseCode::UNINITIALIZED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 186 | } |
| 187 | --mRetry; |
| 188 | switch (mRetry) { |
| 189 | case 0: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 190 | return ResponseCode::WRONG_PASSWORD_0; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 191 | case 1: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 192 | return ResponseCode::WRONG_PASSWORD_1; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 193 | case 2: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 194 | return ResponseCode::WRONG_PASSWORD_2; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 195 | case 3: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 196 | return ResponseCode::WRONG_PASSWORD_3; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 197 | default: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 198 | return ResponseCode::WRONG_PASSWORD_3; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | bool UserState::reset() { |
| 203 | DIR* dir = opendir(getUserDirName()); |
| 204 | if (!dir) { |
| 205 | // If the directory doesn't exist then nothing to do. |
| 206 | if (errno == ENOENT) { |
| 207 | return true; |
| 208 | } |
| 209 | ALOGW("couldn't open user directory: %s", strerror(errno)); |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | struct dirent* file; |
| 214 | while ((file = readdir(dir)) != NULL) { |
| 215 | // skip . and .. |
| 216 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | unlinkat(dirfd(dir), file->d_name, 0); |
| 221 | } |
| 222 | closedir(dir); |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | void UserState::generateKeyFromPassword(uint8_t* key, ssize_t keySize, const android::String8& pw, |
| 227 | uint8_t* salt) { |
| 228 | size_t saltSize; |
| 229 | if (salt != NULL) { |
| 230 | saltSize = SALT_SIZE; |
| 231 | } else { |
| 232 | // Pre-gingerbread used this hardwired salt, readMasterKey will rewrite these when found |
| 233 | salt = (uint8_t*)"keystore"; |
| 234 | // sizeof = 9, not strlen = 8 |
| 235 | saltSize = sizeof("keystore"); |
| 236 | } |
| 237 | |
Branden Archer | 020d535 | 2018-12-28 12:24:53 -0800 | [diff] [blame^] | 238 | const EVP_MD* digest = EVP_sha256(); |
| 239 | |
| 240 | // SHA1 was used prior to increasing the key size |
| 241 | if (keySize == SHA1_DIGEST_SIZE_BYTES) { |
| 242 | digest = EVP_sha1(); |
| 243 | } |
| 244 | |
| 245 | PKCS5_PBKDF2_HMAC(reinterpret_cast<const char*>(pw.string()), pw.length(), salt, saltSize, 8192, |
| 246 | digest, keySize, key); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Branden Archer | e489a03 | 2018-12-28 09:10:49 -0800 | [diff] [blame] | 249 | bool UserState::generateSalt() { |
| 250 | return RAND_bytes(mSalt, sizeof(mSalt)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Branden Archer | e489a03 | 2018-12-28 09:10:49 -0800 | [diff] [blame] | 253 | bool UserState::generateMasterKey() { |
| 254 | if (!RAND_bytes(mMasterKey, sizeof(mMasterKey))) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 255 | return false; |
| 256 | } |
Branden Archer | e489a03 | 2018-12-28 09:10:49 -0800 | [diff] [blame] | 257 | if (!generateSalt()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 258 | return false; |
| 259 | } |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | void UserState::setupMasterKeys() { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 264 | setState(STATE_NO_ERROR); |
| 265 | } |