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