Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1 | /* |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 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 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 17 | #define LOG_TAG "keystore" |
| 18 | |
Shawn Willden | fa5702f | 2017-12-03 15:14:58 -0700 | [diff] [blame] | 19 | #include "KeyStore.h" |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 20 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | #include <fcntl.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 23 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 24 | #include <openssl/bio.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 25 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 26 | #include <utils/String16.h> |
Janis Danisevskis | 6905c33 | 2017-09-01 13:24:23 -0700 | [diff] [blame] | 27 | #include <utils/String8.h> |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 28 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 29 | #include <android/hardware/keymaster/3.0/IKeymasterDevice.h> |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 30 | #include <android/security/IKeystoreService.h> |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 31 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 32 | #include "keystore_utils.h" |
| 33 | #include "permissions.h" |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 34 | #include <keystore/keystore_hidl_support.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 35 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 36 | const char* KeyStore::sOldMasterKey = ".masterkey"; |
| 37 | const char* KeyStore::sMetaDataFile = ".metadata"; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 38 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 39 | const android::String16 KeyStore::sRSAKeyType("RSA"); |
Riley Spahn | eaabae9 | 2014-06-30 12:39:52 -0700 | [diff] [blame] | 40 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 41 | using namespace keystore; |
Janis Danisevskis | 6905c33 | 2017-09-01 13:24:23 -0700 | [diff] [blame] | 42 | using android::String8; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 43 | |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 44 | KeyStore::KeyStore(Entropy* entropy, const km_device_t& device, const km_device_t& fallback, |
| 45 | bool allowNewFallback) |
| 46 | : mEntropy(entropy), mDevice(device), mFallbackDevice(fallback), |
| 47 | mAllowNewFallback(allowNewFallback) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 48 | memset(&mMetaData, '\0', sizeof(mMetaData)); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 51 | KeyStore::~KeyStore() { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 52 | for (android::Vector<UserState*>::iterator it(mMasterKeys.begin()); it != mMasterKeys.end(); |
| 53 | it++) { |
| 54 | delete *it; |
Shawn Willden | 55268b5 | 2015-07-28 11:06:00 -0600 | [diff] [blame] | 55 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 56 | mMasterKeys.clear(); |
Shawn Willden | 55268b5 | 2015-07-28 11:06:00 -0600 | [diff] [blame] | 57 | } |
| 58 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 59 | ResponseCode KeyStore::initialize() { |
| 60 | readMetaData(); |
| 61 | if (upgradeKeystore()) { |
| 62 | writeMetaData(); |
Shawn Willden | 55268b5 | 2015-07-28 11:06:00 -0600 | [diff] [blame] | 63 | } |
| 64 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 65 | return ResponseCode::NO_ERROR; |
Shawn Willden | 55268b5 | 2015-07-28 11:06:00 -0600 | [diff] [blame] | 66 | } |
| 67 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 68 | ResponseCode KeyStore::initializeUser(const android::String8& pw, uid_t userId) { |
| 69 | UserState* userState = getUserState(userId); |
| 70 | return userState->initialize(pw, mEntropy); |
Chad Brubaker | fc18edc | 2015-01-12 15:17:18 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 73 | ResponseCode KeyStore::copyMasterKey(uid_t srcUser, uid_t dstUser) { |
| 74 | UserState* userState = getUserState(dstUser); |
| 75 | UserState* initState = getUserState(srcUser); |
| 76 | return userState->copyMasterKey(initState); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 79 | ResponseCode KeyStore::writeMasterKey(const android::String8& pw, uid_t userId) { |
| 80 | UserState* userState = getUserState(userId); |
| 81 | return userState->writeMasterKey(pw, mEntropy); |
Shawn Willden | 55268b5 | 2015-07-28 11:06:00 -0600 | [diff] [blame] | 82 | } |
| 83 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 84 | ResponseCode KeyStore::readMasterKey(const android::String8& pw, uid_t userId) { |
| 85 | UserState* userState = getUserState(userId); |
| 86 | return userState->readMasterKey(pw, mEntropy); |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 89 | /* Here is the encoding of keys. This is necessary in order to allow arbitrary |
| 90 | * characters in keys. Characters in [0-~] are not encoded. Others are encoded |
| 91 | * into two bytes. The first byte is one of [+-.] which represents the first |
| 92 | * two bits of the character. The second byte encodes the rest of the bits into |
| 93 | * [0-o]. Therefore in the worst case the length of a key gets doubled. Note |
| 94 | * that Base64 cannot be used here due to the need of prefix match on keys. */ |
| 95 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 96 | static size_t encode_key_length(const android::String8& keyName) { |
| 97 | const uint8_t* in = reinterpret_cast<const uint8_t*>(keyName.string()); |
| 98 | size_t length = keyName.length(); |
| 99 | for (int i = length; i > 0; --i, ++in) { |
| 100 | if (*in < '0' || *in > '~') { |
| 101 | ++length; |
| 102 | } |
| 103 | } |
| 104 | return length; |
| 105 | } |
| 106 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 107 | static int encode_key(char* out, const android::String8& keyName) { |
| 108 | const uint8_t* in = reinterpret_cast<const uint8_t*>(keyName.string()); |
| 109 | size_t length = keyName.length(); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 110 | for (int i = length; i > 0; --i, ++in, ++out) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 111 | if (*in < '0' || *in > '~') { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 112 | *out = '+' + (*in >> 6); |
| 113 | *++out = '0' + (*in & 0x3F); |
| 114 | ++length; |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 115 | } else { |
| 116 | *out = *in; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | *out = '\0'; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 120 | return length; |
| 121 | } |
| 122 | |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 123 | android::String8 KeyStore::getKeyName(const android::String8& keyName, const BlobType type) { |
Bin Chen | cfd95ae | 2016-08-22 12:16:09 +1000 | [diff] [blame] | 124 | std::vector<char> encoded(encode_key_length(keyName) + 1); // add 1 for null char |
| 125 | encode_key(encoded.data(), keyName); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 126 | if (type == TYPE_KEY_CHARACTERISTICS) { |
Tucker Sylvestro | 9c28dd5 | 2016-10-06 15:09:48 -0400 | [diff] [blame] | 127 | return android::String8::format(".chr_%s", encoded.data()); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 128 | } else { |
Tucker Sylvestro | 9c28dd5 | 2016-10-06 15:09:48 -0400 | [diff] [blame] | 129 | return android::String8(encoded.data()); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 130 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 133 | android::String8 KeyStore::getKeyNameForUid(const android::String8& keyName, uid_t uid, |
| 134 | const BlobType type) { |
Bin Chen | cfd95ae | 2016-08-22 12:16:09 +1000 | [diff] [blame] | 135 | std::vector<char> encoded(encode_key_length(keyName) + 1); // add 1 for null char |
| 136 | encode_key(encoded.data(), keyName); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 137 | if (type == TYPE_KEY_CHARACTERISTICS) { |
Tucker Sylvestro | 9c28dd5 | 2016-10-06 15:09:48 -0400 | [diff] [blame] | 138 | return android::String8::format(".%u_chr_%s", uid, encoded.data()); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 139 | } else { |
Tucker Sylvestro | 9c28dd5 | 2016-10-06 15:09:48 -0400 | [diff] [blame] | 140 | return android::String8::format("%u_%s", uid, encoded.data()); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 141 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 144 | android::String8 KeyStore::getKeyNameForUidWithDir(const android::String8& keyName, uid_t uid, |
| 145 | const BlobType type) { |
Bin Chen | cfd95ae | 2016-08-22 12:16:09 +1000 | [diff] [blame] | 146 | std::vector<char> encoded(encode_key_length(keyName) + 1); // add 1 for null char |
| 147 | encode_key(encoded.data(), keyName); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 148 | |
| 149 | if (type == TYPE_KEY_CHARACTERISTICS) { |
| 150 | return android::String8::format("%s/.%u_chr_%s", getUserStateByUid(uid)->getUserDirName(), |
Tucker Sylvestro | 9c28dd5 | 2016-10-06 15:09:48 -0400 | [diff] [blame] | 151 | uid, encoded.data()); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 152 | } else { |
| 153 | return android::String8::format("%s/%u_%s", getUserStateByUid(uid)->getUserDirName(), uid, |
Tucker Sylvestro | 9c28dd5 | 2016-10-06 15:09:48 -0400 | [diff] [blame] | 154 | encoded.data()); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 155 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Janis Danisevskis | 31b44f2 | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 158 | NullOr<android::String8> KeyStore::getBlobFileNameIfExists(const android::String8& alias, uid_t uid, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 159 | const BlobType type) { |
Janis Danisevskis | 31b44f2 | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 160 | android::String8 filepath8(getKeyNameForUidWithDir(alias, uid, type)); |
| 161 | |
| 162 | if (!access(filepath8.string(), R_OK | W_OK)) return filepath8; |
| 163 | |
| 164 | // If this is one of the legacy UID->UID mappings, use it. |
| 165 | uid_t euid = get_keystore_euid(uid); |
| 166 | if (euid != uid) { |
| 167 | filepath8 = getKeyNameForUidWithDir(alias, euid, type); |
| 168 | if (!access(filepath8.string(), R_OK | W_OK)) return filepath8; |
| 169 | } |
| 170 | |
| 171 | // They might be using a granted key. |
| 172 | auto grant = mGrants.get(uid, alias.string()); |
| 173 | if (grant) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 174 | filepath8 = String8::format( |
| 175 | "%s/%s", grant->owner_dir_name_.c_str(), |
| 176 | getKeyNameForUid(String8(grant->alias_.c_str()), grant->owner_uid_, type).c_str()); |
Janis Danisevskis | 31b44f2 | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 177 | if (!access(filepath8.string(), R_OK | W_OK)) return filepath8; |
| 178 | } |
| 179 | return {}; |
| 180 | } |
| 181 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 182 | void KeyStore::resetUser(uid_t userId, bool keepUnenryptedEntries) { |
| 183 | android::String8 prefix(""); |
| 184 | android::Vector<android::String16> aliases; |
| 185 | UserState* userState = getUserState(userId); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 186 | if (list(prefix, &aliases, userId) != ResponseCode::NO_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | for (uint32_t i = 0; i < aliases.size(); i++) { |
| 190 | android::String8 filename(aliases[i]); |
| 191 | filename = android::String8::format("%s/%s", userState->getUserDirName(), |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 192 | getKeyName(filename, TYPE_ANY).string()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 193 | bool shouldDelete = true; |
| 194 | if (keepUnenryptedEntries) { |
| 195 | Blob blob; |
| 196 | ResponseCode rc = get(filename, &blob, ::TYPE_ANY, userId); |
| 197 | |
Shawn Willden | 07aebe7 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 198 | switch (rc) { |
| 199 | case ResponseCode::SYSTEM_ERROR: |
| 200 | case ResponseCode::VALUE_CORRUPTED: |
| 201 | // If we can't read blobs, delete them. |
| 202 | shouldDelete = true; |
| 203 | break; |
| 204 | |
| 205 | case ResponseCode::NO_ERROR: |
| 206 | case ResponseCode::LOCKED: |
| 207 | // Delete encrypted blobs but keep unencrypted blobs and super-encrypted blobs. We |
| 208 | // need to keep super-encrypted blobs so we can report that the user is |
| 209 | // unauthenticated if a caller tries to use them, rather than reporting that they |
| 210 | // don't exist. |
| 211 | shouldDelete = blob.isEncrypted(); |
| 212 | break; |
| 213 | |
| 214 | default: |
| 215 | ALOGE("Got unexpected return code %d from KeyStore::get()", rc); |
| 216 | // This shouldn't happen. To be on the safe side, delete it. |
| 217 | shouldDelete = true; |
| 218 | break; |
| 219 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 220 | } |
| 221 | if (shouldDelete) { |
| 222 | del(filename, ::TYPE_ANY, userId); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 223 | |
| 224 | // del() will fail silently if no cached characteristics are present for this alias. |
| 225 | android::String8 chr_filename(aliases[i]); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 226 | chr_filename = android::String8::format( |
| 227 | "%s/%s", userState->getUserDirName(), |
| 228 | getKeyName(chr_filename, TYPE_KEY_CHARACTERISTICS).string()); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 229 | del(chr_filename, ::TYPE_KEY_CHARACTERISTICS, userId); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | if (!userState->deleteMasterKey()) { |
| 233 | ALOGE("Failed to delete user %d's master key", userId); |
| 234 | } |
| 235 | if (!keepUnenryptedEntries) { |
| 236 | if (!userState->reset()) { |
| 237 | ALOGE("Failed to remove user %d's directory", userId); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | bool KeyStore::isEmpty(uid_t userId) const { |
| 243 | const UserState* userState = getUserState(userId); |
| 244 | if (userState == NULL) { |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | DIR* dir = opendir(userState->getUserDirName()); |
| 249 | if (!dir) { |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | bool result = true; |
| 254 | struct dirent* file; |
| 255 | while ((file = readdir(dir)) != NULL) { |
| 256 | // We only care about files. |
| 257 | if (file->d_type != DT_REG) { |
| 258 | continue; |
| 259 | } |
| 260 | |
| 261 | // Skip anything that starts with a "." |
| 262 | if (file->d_name[0] == '.') { |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | result = false; |
| 267 | break; |
| 268 | } |
| 269 | closedir(dir); |
| 270 | return result; |
| 271 | } |
| 272 | |
| 273 | void KeyStore::lock(uid_t userId) { |
| 274 | UserState* userState = getUserState(userId); |
| 275 | userState->zeroizeMasterKeysInMemory(); |
| 276 | userState->setState(STATE_LOCKED); |
| 277 | } |
| 278 | |
| 279 | ResponseCode KeyStore::get(const char* filename, Blob* keyBlob, const BlobType type, uid_t userId) { |
| 280 | UserState* userState = getUserState(userId); |
| 281 | ResponseCode rc = |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 282 | keyBlob->readBlob(filename, userState->getEncryptionKey(), userState->getState()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 283 | if (rc != ResponseCode::NO_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 284 | return rc; |
| 285 | } |
| 286 | |
| 287 | const uint8_t version = keyBlob->getVersion(); |
| 288 | if (version < CURRENT_BLOB_VERSION) { |
| 289 | /* If we upgrade the key, we need to write it to disk again. Then |
| 290 | * it must be read it again since the blob is encrypted each time |
| 291 | * it's written. |
| 292 | */ |
| 293 | if (upgradeBlob(filename, keyBlob, version, type, userId)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 294 | if ((rc = this->put(filename, keyBlob, userId)) != ResponseCode::NO_ERROR || |
Shawn Willden | e983058 | 2017-04-18 10:47:57 -0600 | [diff] [blame] | 295 | (rc = keyBlob->readBlob(filename, userState->getEncryptionKey(), |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 296 | userState->getState())) != ResponseCode::NO_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 297 | return rc; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 303 | * This will upgrade software-backed keys to hardware-backed keys. |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 304 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 305 | if (rc == ResponseCode::NO_ERROR && type == TYPE_KEY_PAIR && keyBlob->isFallback()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 306 | ResponseCode imported = |
| 307 | importKey(keyBlob->getValue(), keyBlob->getLength(), filename, userId, |
| 308 | keyBlob->isEncrypted() ? KEYSTORE_FLAG_ENCRYPTED : KEYSTORE_FLAG_NONE); |
| 309 | |
Shawn Willden | 07aebe7 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 310 | // The HAL allowed the import, reget the key to have the "fresh" version. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 311 | if (imported == ResponseCode::NO_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 312 | rc = get(filename, keyBlob, TYPE_KEY_PAIR, userId); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // Keymaster 0.3 keys are valid keymaster 1.0 keys, so silently upgrade. |
| 317 | if (keyBlob->getType() == TYPE_KEY_PAIR) { |
| 318 | keyBlob->setType(TYPE_KEYMASTER_10); |
| 319 | rc = this->put(filename, keyBlob, userId); |
| 320 | } |
| 321 | |
| 322 | if (type != TYPE_ANY && keyBlob->getType() != type) { |
| 323 | ALOGW("key found but type doesn't match: %d vs %d", keyBlob->getType(), type); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 324 | return ResponseCode::KEY_NOT_FOUND; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | return rc; |
| 328 | } |
| 329 | |
| 330 | ResponseCode KeyStore::put(const char* filename, Blob* keyBlob, uid_t userId) { |
| 331 | UserState* userState = getUserState(userId); |
| 332 | return keyBlob->writeBlob(filename, userState->getEncryptionKey(), userState->getState(), |
| 333 | mEntropy); |
| 334 | } |
| 335 | |
Janis Danisevskis | 31b44f2 | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 336 | static NullOr<std::tuple<uid_t, std::string>> filename2UidAlias(const std::string& filename); |
| 337 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 338 | ResponseCode KeyStore::del(const char* filename, const BlobType type, uid_t userId) { |
| 339 | Blob keyBlob; |
Janis Danisevskis | 31b44f2 | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 340 | auto uidAlias = filename2UidAlias(filename); |
| 341 | uid_t uid; |
| 342 | std::string alias; |
| 343 | if (uidAlias.isOk()) { |
| 344 | std::tie(uid, alias) = std::move(uidAlias).value(); |
| 345 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 346 | ResponseCode rc = get(filename, &keyBlob, type, userId); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 347 | if (rc == ResponseCode::VALUE_CORRUPTED) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 348 | // The file is corrupt, the best we can do is rm it. |
Janis Danisevskis | 31b44f2 | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 349 | if (uidAlias.isOk()) { |
| 350 | // remove possible grants |
| 351 | mGrants.removeAllGrantsToKey(uid, alias); |
| 352 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 353 | return (unlink(filename) && errno != ENOENT) ? ResponseCode::SYSTEM_ERROR |
| 354 | : ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 355 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 356 | if (rc != ResponseCode::NO_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 357 | return rc; |
| 358 | } |
| 359 | |
Janis Danisevskis | 69c434a | 2017-01-30 10:27:10 +0000 | [diff] [blame] | 360 | auto& dev = getDevice(keyBlob); |
| 361 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 362 | if (keyBlob.getType() == ::TYPE_KEY_PAIR || keyBlob.getType() == ::TYPE_KEYMASTER_10) { |
Janis Danisevskis | 69c434a | 2017-01-30 10:27:10 +0000 | [diff] [blame] | 363 | auto ret = KS_HANDLE_HIDL_ERROR(dev->deleteKey(blob2hidlVec(keyBlob))); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 364 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 365 | // A device doesn't have to implement delete_key. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 366 | if (ret != ErrorCode::OK && ret != ErrorCode::UNIMPLEMENTED) |
| 367 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 370 | rc = |
| 371 | (unlink(filename) && errno != ENOENT) ? ResponseCode::SYSTEM_ERROR : ResponseCode::NO_ERROR; |
Janis Danisevskis | 31b44f2 | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 372 | |
| 373 | if (rc == ResponseCode::NO_ERROR && keyBlob.getType() != ::TYPE_KEY_CHARACTERISTICS) { |
| 374 | // now that we have successfully deleted a key, let's make sure there are no stale grants |
| 375 | if (uidAlias.isOk()) { |
| 376 | mGrants.removeAllGrantsToKey(uid, alias); |
| 377 | } |
| 378 | } |
| 379 | return rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 382 | /* |
| 383 | * Converts from the "escaped" format on disk to actual name. |
| 384 | * This will be smaller than the input string. |
| 385 | * |
| 386 | * Characters that should combine with the next at the end will be truncated. |
| 387 | */ |
| 388 | static size_t decode_key_length(const char* in, size_t length) { |
| 389 | size_t outLength = 0; |
| 390 | |
| 391 | for (const char* end = in + length; in < end; in++) { |
| 392 | /* This combines with the next character. */ |
| 393 | if (*in < '0' || *in > '~') { |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | outLength++; |
| 398 | } |
| 399 | return outLength; |
| 400 | } |
| 401 | |
| 402 | static void decode_key(char* out, const char* in, size_t length) { |
| 403 | for (const char* end = in + length; in < end; in++) { |
| 404 | if (*in < '0' || *in > '~') { |
| 405 | /* Truncate combining characters at the end. */ |
| 406 | if (in + 1 >= end) { |
| 407 | break; |
| 408 | } |
| 409 | |
| 410 | *out = (*in++ - '+') << 6; |
| 411 | *out++ |= (*in - '0') & 0x3F; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 412 | } else { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 413 | *out++ = *in; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | *out = '\0'; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Janis Danisevskis | 31b44f2 | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 419 | static NullOr<std::tuple<uid_t, std::string>> filename2UidAlias(const std::string& filepath) { |
| 420 | auto filenamebase = filepath.find_last_of('/'); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 421 | std::string filename = |
| 422 | filenamebase == std::string::npos ? filepath : filepath.substr(filenamebase + 1); |
Janis Danisevskis | 31b44f2 | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 423 | |
| 424 | if (filename[0] == '.') return {}; |
| 425 | |
| 426 | auto sep = filename.find('_'); |
| 427 | if (sep == std::string::npos) return {}; |
| 428 | |
| 429 | std::stringstream s(filename.substr(0, sep)); |
| 430 | uid_t uid; |
| 431 | s >> uid; |
| 432 | if (!s) return {}; |
| 433 | |
| 434 | auto alias = filename.substr(sep + 1); |
| 435 | |
| 436 | std::vector<char> alias_buffer(decode_key_length(alias.c_str(), alias.size()) + 1); |
| 437 | |
| 438 | decode_key(alias_buffer.data(), alias.c_str(), alias.size()); |
| 439 | return std::tuple<uid_t, std::string>(uid, alias_buffer.data()); |
| 440 | } |
| 441 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 442 | ResponseCode KeyStore::list(const android::String8& prefix, |
| 443 | android::Vector<android::String16>* matches, uid_t userId) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 444 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 445 | UserState* userState = getUserState(userId); |
| 446 | size_t n = prefix.length(); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 447 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 448 | DIR* dir = opendir(userState->getUserDirName()); |
| 449 | if (!dir) { |
| 450 | ALOGW("can't open directory for user: %s", strerror(errno)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 451 | return ResponseCode::SYSTEM_ERROR; |
Chad Brubaker | 3a7d9e6 | 2015-06-04 15:01:46 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 454 | struct dirent* file; |
| 455 | while ((file = readdir(dir)) != NULL) { |
| 456 | // We only care about files. |
| 457 | if (file->d_type != DT_REG) { |
| 458 | continue; |
Chad Brubaker | 3a7d9e6 | 2015-06-04 15:01:46 -0700 | [diff] [blame] | 459 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 460 | |
| 461 | // Skip anything that starts with a "." |
| 462 | if (file->d_name[0] == '.') { |
| 463 | continue; |
Chad Brubaker | 3a7d9e6 | 2015-06-04 15:01:46 -0700 | [diff] [blame] | 464 | } |
Chad Brubaker | 3a7d9e6 | 2015-06-04 15:01:46 -0700 | [diff] [blame] | 465 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 466 | if (!strncmp(prefix.string(), file->d_name, n)) { |
| 467 | const char* p = &file->d_name[n]; |
| 468 | size_t plen = strlen(p); |
Chad Brubaker | 3a7d9e6 | 2015-06-04 15:01:46 -0700 | [diff] [blame] | 469 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 470 | size_t extra = decode_key_length(p, plen); |
| 471 | char* match = (char*)malloc(extra + 1); |
| 472 | if (match != NULL) { |
| 473 | decode_key(match, p, plen); |
| 474 | matches->push(android::String16(match, extra)); |
| 475 | free(match); |
Chad Brubaker | df70517 | 2015-06-17 20:17:51 -0700 | [diff] [blame] | 476 | } else { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 477 | ALOGW("could not allocate match of size %zd", extra); |
Chad Brubaker | df70517 | 2015-06-17 20:17:51 -0700 | [diff] [blame] | 478 | } |
Chad Brubaker | 3a7d9e6 | 2015-06-04 15:01:46 -0700 | [diff] [blame] | 479 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 480 | } |
| 481 | closedir(dir); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 482 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 483 | } |
Chad Brubaker | 3a7d9e6 | 2015-06-04 15:01:46 -0700 | [diff] [blame] | 484 | |
Janis Danisevskis | 6905c33 | 2017-09-01 13:24:23 -0700 | [diff] [blame] | 485 | std::string KeyStore::addGrant(const char* alias, uid_t granterUid, uid_t granteeUid) { |
| 486 | return mGrants.put(granteeUid, alias, getUserStateByUid(granterUid)->getUserDirName(), |
| 487 | granterUid); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 488 | } |
Chad Brubaker | 3a7d9e6 | 2015-06-04 15:01:46 -0700 | [diff] [blame] | 489 | |
Janis Danisevskis | 31b44f2 | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 490 | bool KeyStore::removeGrant(const char* alias, const uid_t granterUid, const uid_t granteeUid) { |
| 491 | return mGrants.removeByFileAlias(granteeUid, granterUid, alias); |
| 492 | } |
| 493 | void KeyStore::removeAllGrantsToUid(const uid_t granteeUid) { |
| 494 | mGrants.removeAllGrantsToUid(granteeUid); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 495 | } |
Chad Brubaker | 3a7d9e6 | 2015-06-04 15:01:46 -0700 | [diff] [blame] | 496 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 497 | ResponseCode KeyStore::importKey(const uint8_t* key, size_t keyLen, const char* filename, |
| 498 | uid_t userId, int32_t flags) { |
| 499 | Unique_PKCS8_PRIV_KEY_INFO pkcs8(d2i_PKCS8_PRIV_KEY_INFO(NULL, &key, keyLen)); |
| 500 | if (!pkcs8.get()) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 501 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 502 | } |
| 503 | Unique_EVP_PKEY pkey(EVP_PKCS82PKEY(pkcs8.get())); |
| 504 | if (!pkey.get()) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 505 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 506 | } |
| 507 | int type = EVP_PKEY_type(pkey->type); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 508 | AuthorizationSet params; |
| 509 | add_legacy_key_authorizations(type, ¶ms); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 510 | switch (type) { |
| 511 | case EVP_PKEY_RSA: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 512 | params.push_back(TAG_ALGORITHM, Algorithm::RSA); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 513 | break; |
| 514 | case EVP_PKEY_EC: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 515 | params.push_back(TAG_ALGORITHM, Algorithm::EC); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 516 | break; |
| 517 | default: |
| 518 | ALOGW("Unsupported key type %d", type); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 519 | return ResponseCode::SYSTEM_ERROR; |
Chad Brubaker | 3a7d9e6 | 2015-06-04 15:01:46 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 522 | AuthorizationSet opParams(params); |
| 523 | hidl_vec<uint8_t> blob; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 524 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 525 | ErrorCode error; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 526 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<uint8_t>& keyBlob, |
| 527 | const KeyCharacteristics& /* ignored */) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 528 | error = ret; |
| 529 | if (error != ErrorCode::OK) return; |
| 530 | blob = keyBlob; |
| 531 | }; |
| 532 | auto input = blob2hidlVec(key, keyLen); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 533 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 534 | ErrorCode rc = KS_HANDLE_HIDL_ERROR( |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 535 | mDevice->importKey(params.hidl_data(), KeyFormat::PKCS8, input, hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 536 | if (rc != ErrorCode::OK) return ResponseCode::SYSTEM_ERROR; |
| 537 | if (error != ErrorCode::OK) { |
| 538 | ALOGE("Keymaster error %d importing key pair", error); |
| 539 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 542 | Blob keyBlob(&blob[0], blob.size(), NULL, 0, TYPE_KEYMASTER_10); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 543 | |
| 544 | keyBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 545 | keyBlob.setFallback(false); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 546 | |
| 547 | return put(filename, &keyBlob, userId); |
| 548 | } |
| 549 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 550 | bool KeyStore::isHardwareBacked(const android::String16& /*keyType*/) const { |
Shawn Willden | b8550a0 | 2017-02-23 11:06:05 -0700 | [diff] [blame] | 551 | using ::android::hardware::hidl_string; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 552 | if (mDevice == NULL) { |
| 553 | ALOGW("can't get keymaster device"); |
| 554 | return false; |
| 555 | } |
Janis Danisevskis | e2b6caf | 2017-03-02 16:37:10 -0800 | [diff] [blame] | 556 | |
| 557 | bool isSecure = false; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 558 | auto hidlcb = [&](bool _isSecure, bool, bool, bool, bool, const hidl_string&, |
| 559 | const hidl_string&) { isSecure = _isSecure; }; |
Janis Danisevskis | e2b6caf | 2017-03-02 16:37:10 -0800 | [diff] [blame] | 560 | auto rc = mDevice->getHardwareFeatures(hidlcb); |
| 561 | if (!rc.isOk()) { |
| 562 | ALOGE("Communication with keymaster HAL failed while retrieving hardware features (%s)", |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 563 | rc.description().c_str()); |
Janis Danisevskis | e2b6caf | 2017-03-02 16:37:10 -0800 | [diff] [blame] | 564 | return false; |
| 565 | } |
| 566 | return isSecure; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | ResponseCode KeyStore::getKeyForName(Blob* keyBlob, const android::String8& keyName, |
| 570 | const uid_t uid, const BlobType type) { |
Janis Danisevskis | 31b44f2 | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 571 | auto filepath8 = getBlobFileNameIfExists(keyName, uid, type); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 572 | uid_t userId = get_user_id(uid); |
| 573 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 574 | if (filepath8.isOk()) return get(filepath8.value().string(), keyBlob, type, userId); |
Riley Spahn | eaabae9 | 2014-06-30 12:39:52 -0700 | [diff] [blame] | 575 | |
Janis Danisevskis | 31b44f2 | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 576 | return ResponseCode::KEY_NOT_FOUND; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | UserState* KeyStore::getUserState(uid_t userId) { |
| 580 | for (android::Vector<UserState*>::iterator it(mMasterKeys.begin()); it != mMasterKeys.end(); |
| 581 | it++) { |
| 582 | UserState* state = *it; |
| 583 | if (state->getUserId() == userId) { |
| 584 | return state; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | UserState* userState = new UserState(userId); |
| 589 | if (!userState->initialize()) { |
| 590 | /* There's not much we can do if initialization fails. Trying to |
| 591 | * unlock the keystore for that user will fail as well, so any |
| 592 | * subsequent request for this user will just return SYSTEM_ERROR. |
| 593 | */ |
| 594 | ALOGE("User initialization failed for %u; subsuquent operations will fail", userId); |
| 595 | } |
| 596 | mMasterKeys.add(userState); |
| 597 | return userState; |
| 598 | } |
| 599 | |
| 600 | UserState* KeyStore::getUserStateByUid(uid_t uid) { |
| 601 | uid_t userId = get_user_id(uid); |
| 602 | return getUserState(userId); |
| 603 | } |
| 604 | |
| 605 | const UserState* KeyStore::getUserState(uid_t userId) const { |
| 606 | for (android::Vector<UserState*>::const_iterator it(mMasterKeys.begin()); |
| 607 | it != mMasterKeys.end(); it++) { |
| 608 | UserState* state = *it; |
| 609 | if (state->getUserId() == userId) { |
| 610 | return state; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return NULL; |
| 615 | } |
| 616 | |
| 617 | const UserState* KeyStore::getUserStateByUid(uid_t uid) const { |
| 618 | uid_t userId = get_user_id(uid); |
| 619 | return getUserState(userId); |
| 620 | } |
| 621 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 622 | bool KeyStore::upgradeBlob(const char* filename, Blob* blob, const uint8_t oldVersion, |
Janis Danisevskis | 26b0c37 | 2017-09-20 15:08:49 -0700 | [diff] [blame] | 623 | const BlobType type, uid_t userId) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 624 | bool updated = false; |
| 625 | uint8_t version = oldVersion; |
| 626 | |
| 627 | /* From V0 -> V1: All old types were unknown */ |
| 628 | if (version == 0) { |
| 629 | ALOGV("upgrading to version 1 and setting type %d", type); |
| 630 | |
| 631 | blob->setType(type); |
| 632 | if (type == TYPE_KEY_PAIR) { |
Janis Danisevskis | 26b0c37 | 2017-09-20 15:08:49 -0700 | [diff] [blame] | 633 | importBlobAsKey(blob, filename, userId); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 634 | } |
| 635 | version = 1; |
| 636 | updated = true; |
| 637 | } |
| 638 | |
| 639 | /* From V1 -> V2: All old keys were encrypted */ |
| 640 | if (version == 1) { |
| 641 | ALOGV("upgrading to version 2"); |
| 642 | |
| 643 | blob->setEncrypted(true); |
| 644 | version = 2; |
| 645 | updated = true; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 646 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 647 | |
| 648 | /* |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 649 | * If we've updated, set the key blob to the right version |
| 650 | * and write it. |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 651 | */ |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 652 | if (updated) { |
| 653 | ALOGV("updated and writing file %s", filename); |
| 654 | blob->setVersion(version); |
| 655 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 656 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 657 | return updated; |
| 658 | } |
| 659 | |
| 660 | struct BIO_Delete { |
| 661 | void operator()(BIO* p) const { BIO_free(p); } |
| 662 | }; |
Janis Danisevskis | ccfff10 | 2017-05-01 11:02:51 -0700 | [diff] [blame] | 663 | typedef std::unique_ptr<BIO, BIO_Delete> Unique_BIO; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 664 | |
Janis Danisevskis | 26b0c37 | 2017-09-20 15:08:49 -0700 | [diff] [blame] | 665 | ResponseCode KeyStore::importBlobAsKey(Blob* blob, const char* filename, uid_t userId) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 666 | // We won't even write to the blob directly with this BIO, so const_cast is okay. |
| 667 | Unique_BIO b(BIO_new_mem_buf(const_cast<uint8_t*>(blob->getValue()), blob->getLength())); |
| 668 | if (b.get() == NULL) { |
| 669 | ALOGE("Problem instantiating BIO"); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 670 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | Unique_EVP_PKEY pkey(PEM_read_bio_PrivateKey(b.get(), NULL, NULL, NULL)); |
| 674 | if (pkey.get() == NULL) { |
| 675 | ALOGE("Couldn't read old PEM file"); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 676 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | Unique_PKCS8_PRIV_KEY_INFO pkcs8(EVP_PKEY2PKCS8(pkey.get())); |
| 680 | int len = i2d_PKCS8_PRIV_KEY_INFO(pkcs8.get(), NULL); |
| 681 | if (len < 0) { |
| 682 | ALOGE("Couldn't measure PKCS#8 length"); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 683 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Janis Danisevskis | ccfff10 | 2017-05-01 11:02:51 -0700 | [diff] [blame] | 686 | std::unique_ptr<unsigned char[]> pkcs8key(new unsigned char[len]); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 687 | uint8_t* tmp = pkcs8key.get(); |
| 688 | if (i2d_PKCS8_PRIV_KEY_INFO(pkcs8.get(), &tmp) != len) { |
| 689 | ALOGE("Couldn't convert to PKCS#8"); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 690 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Janis Danisevskis | 26b0c37 | 2017-09-20 15:08:49 -0700 | [diff] [blame] | 693 | ResponseCode rc = importKey(pkcs8key.get(), len, filename, userId, |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 694 | blob->isEncrypted() ? KEYSTORE_FLAG_ENCRYPTED : KEYSTORE_FLAG_NONE); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 695 | if (rc != ResponseCode::NO_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 696 | return rc; |
| 697 | } |
| 698 | |
Janis Danisevskis | 26b0c37 | 2017-09-20 15:08:49 -0700 | [diff] [blame] | 699 | return get(filename, blob, TYPE_KEY_PAIR, userId); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | void KeyStore::readMetaData() { |
| 703 | int in = TEMP_FAILURE_RETRY(open(sMetaDataFile, O_RDONLY)); |
| 704 | if (in < 0) { |
| 705 | return; |
| 706 | } |
| 707 | size_t fileLength = readFully(in, (uint8_t*)&mMetaData, sizeof(mMetaData)); |
| 708 | if (fileLength != sizeof(mMetaData)) { |
| 709 | ALOGI("Metadata file is %zd bytes (%zd experted); upgrade?", fileLength, sizeof(mMetaData)); |
| 710 | } |
| 711 | close(in); |
| 712 | } |
| 713 | |
| 714 | void KeyStore::writeMetaData() { |
| 715 | const char* tmpFileName = ".metadata.tmp"; |
| 716 | int out = |
| 717 | TEMP_FAILURE_RETRY(open(tmpFileName, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR)); |
| 718 | if (out < 0) { |
| 719 | ALOGE("couldn't write metadata file: %s", strerror(errno)); |
| 720 | return; |
| 721 | } |
| 722 | size_t fileLength = writeFully(out, (uint8_t*)&mMetaData, sizeof(mMetaData)); |
| 723 | if (fileLength != sizeof(mMetaData)) { |
| 724 | ALOGI("Could only write %zd bytes to metadata file (%zd expected)", fileLength, |
| 725 | sizeof(mMetaData)); |
| 726 | } |
| 727 | close(out); |
| 728 | rename(tmpFileName, sMetaDataFile); |
| 729 | } |
| 730 | |
| 731 | bool KeyStore::upgradeKeystore() { |
| 732 | bool upgraded = false; |
| 733 | |
| 734 | if (mMetaData.version == 0) { |
| 735 | UserState* userState = getUserStateByUid(0); |
| 736 | |
| 737 | // Initialize first so the directory is made. |
| 738 | userState->initialize(); |
| 739 | |
| 740 | // Migrate the old .masterkey file to user 0. |
| 741 | if (access(sOldMasterKey, R_OK) == 0) { |
| 742 | if (rename(sOldMasterKey, userState->getMasterKeyFileName()) < 0) { |
| 743 | ALOGE("couldn't migrate old masterkey: %s", strerror(errno)); |
| 744 | return false; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | // Initialize again in case we had a key. |
| 749 | userState->initialize(); |
| 750 | |
| 751 | // Try to migrate existing keys. |
| 752 | DIR* dir = opendir("."); |
| 753 | if (!dir) { |
| 754 | // Give up now; maybe we can upgrade later. |
| 755 | ALOGE("couldn't open keystore's directory; something is wrong"); |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | struct dirent* file; |
| 760 | while ((file = readdir(dir)) != NULL) { |
| 761 | // We only care about files. |
| 762 | if (file->d_type != DT_REG) { |
| 763 | continue; |
| 764 | } |
| 765 | |
| 766 | // Skip anything that starts with a "." |
| 767 | if (file->d_name[0] == '.') { |
| 768 | continue; |
| 769 | } |
| 770 | |
| 771 | // Find the current file's user. |
| 772 | char* end; |
| 773 | unsigned long thisUid = strtoul(file->d_name, &end, 10); |
| 774 | if (end[0] != '_' || end[1] == 0) { |
| 775 | continue; |
| 776 | } |
| 777 | UserState* otherUser = getUserStateByUid(thisUid); |
| 778 | if (otherUser->getUserId() != 0) { |
| 779 | unlinkat(dirfd(dir), file->d_name, 0); |
| 780 | } |
| 781 | |
| 782 | // Rename the file into user directory. |
| 783 | DIR* otherdir = opendir(otherUser->getUserDirName()); |
| 784 | if (otherdir == NULL) { |
| 785 | ALOGW("couldn't open user directory for rename"); |
| 786 | continue; |
| 787 | } |
| 788 | if (renameat(dirfd(dir), file->d_name, dirfd(otherdir), file->d_name) < 0) { |
| 789 | ALOGW("couldn't rename blob: %s: %s", file->d_name, strerror(errno)); |
| 790 | } |
| 791 | closedir(otherdir); |
| 792 | } |
| 793 | closedir(dir); |
| 794 | |
| 795 | mMetaData.version = 1; |
| 796 | upgraded = true; |
| 797 | } |
| 798 | |
| 799 | return upgraded; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 800 | } |