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