Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Keymaster4.h" |
| 18 | |
| 19 | //#include <android-base/logging.h> |
| 20 | #include <keymasterV4_0/authorization_set.h> |
| 21 | #include <keymasterV4_0/keymaster_utils.h> |
| 22 | |
| 23 | #include <iostream> |
| 24 | #define LOG(x) std::cout |
| 25 | #define PLOG(x) std::cout |
| 26 | |
| 27 | namespace android { |
| 28 | namespace vold { |
| 29 | |
| 30 | using ::android::hardware::hidl_string; |
| 31 | using ::android::hardware::hidl_vec; |
| 32 | using ::android::hardware::keymaster::V4_0::SecurityLevel; |
| 33 | |
| 34 | KeymasterOperation::~KeymasterOperation() { |
| 35 | if (mDevice) mDevice->abort(mOpHandle); |
| 36 | } |
| 37 | |
| 38 | bool KeymasterOperation::updateCompletely(const char* input, size_t inputLen, |
| 39 | const std::function<void(const char*, size_t)> consumer) { |
| 40 | uint32_t inputConsumed = 0; |
| 41 | |
| 42 | km::ErrorCode km_error; |
| 43 | auto hidlCB = [&](km::ErrorCode ret, uint32_t inputConsumedDelta, |
| 44 | const hidl_vec<km::KeyParameter>& /*ignored*/, |
| 45 | const hidl_vec<uint8_t>& _output) { |
| 46 | km_error = ret; |
| 47 | if (km_error != km::ErrorCode::OK) return; |
| 48 | inputConsumed += inputConsumedDelta; |
| 49 | consumer(reinterpret_cast<const char*>(&_output[0]), _output.size()); |
| 50 | }; |
| 51 | |
| 52 | while (inputConsumed != inputLen) { |
| 53 | size_t toRead = static_cast<size_t>(inputLen - inputConsumed); |
| 54 | auto inputBlob = km::support::blob2hidlVec( |
| 55 | reinterpret_cast<const uint8_t*>(&input[inputConsumed]), toRead); |
| 56 | auto error = mDevice->update(mOpHandle, hidl_vec<km::KeyParameter>(), inputBlob, |
| 57 | km::HardwareAuthToken(), km::VerificationToken(), hidlCB); |
| 58 | if (!error.isOk()) { |
| 59 | LOG(ERROR) << "update failed: " << error.description() << std::endl; |
| 60 | mDevice = nullptr; |
| 61 | return false; |
| 62 | } |
| 63 | if (km_error != km::ErrorCode::OK) { |
| 64 | LOG(ERROR) << "update failed, code " << int32_t(km_error) << std::endl; |
| 65 | mDevice = nullptr; |
| 66 | return false; |
| 67 | } |
| 68 | if (inputConsumed > inputLen) { |
| 69 | LOG(ERROR) << "update reported too much input consumed" << std::endl; |
| 70 | mDevice = nullptr; |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool KeymasterOperation::finish(std::string* output) { |
| 78 | km::ErrorCode km_error; |
| 79 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<km::KeyParameter>& /*ignored*/, |
| 80 | const hidl_vec<uint8_t>& _output) { |
| 81 | km_error = ret; |
| 82 | if (km_error != km::ErrorCode::OK) return; |
| 83 | if (output) output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size()); |
| 84 | }; |
| 85 | auto error = mDevice->finish(mOpHandle, hidl_vec<km::KeyParameter>(), hidl_vec<uint8_t>(), |
| 86 | hidl_vec<uint8_t>(), km::HardwareAuthToken(), |
| 87 | km::VerificationToken(), hidlCb); |
| 88 | mDevice = nullptr; |
| 89 | if (!error.isOk()) { |
| 90 | LOG(ERROR) << "finish failed: " << error.description() << std::endl; |
| 91 | return false; |
| 92 | } |
| 93 | if (km_error != km::ErrorCode::OK) { |
| 94 | LOG(ERROR) << "finish failed, code " << int32_t(km_error) << std::endl; |
| 95 | return false; |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | /* static */ bool Keymaster::hmacKeyGenerated = false; |
| 101 | |
| 102 | Keymaster::Keymaster() { |
| 103 | auto devices = KmDevice::enumerateAvailableDevices(); |
| 104 | if (!hmacKeyGenerated) { |
| 105 | KmDevice::performHmacKeyAgreement(devices); |
| 106 | hmacKeyGenerated = true; |
| 107 | } |
| 108 | for (auto& dev : devices) { |
| 109 | // Do not use StrongBox for device encryption / credential encryption. If a security chip |
| 110 | // is present it will have Weaver, which already strengthens CE. We get no additional |
| 111 | // benefit from using StrongBox here, so skip it. |
| 112 | if (dev->halVersion().securityLevel != SecurityLevel::STRONGBOX) { |
| 113 | mDevice = std::move(dev); |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | if (!mDevice) return; |
| 118 | auto& version = mDevice->halVersion(); |
| 119 | LOG(INFO) << "Using " << version.keymasterName << " from " << version.authorName |
| 120 | << " for encryption. Security level: " << toString(version.securityLevel) |
| 121 | << ", HAL: " << mDevice->descriptor() << "/" << mDevice->instanceName() << std::endl; |
| 122 | } |
| 123 | |
| 124 | bool Keymaster::generateKey(const km::AuthorizationSet& inParams, std::string* key) { |
| 125 | km::ErrorCode km_error; |
| 126 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& keyBlob, |
| 127 | const km::KeyCharacteristics& /*ignored*/) { |
| 128 | km_error = ret; |
| 129 | if (km_error != km::ErrorCode::OK) return; |
| 130 | if (key) key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size()); |
| 131 | }; |
| 132 | |
| 133 | auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb); |
| 134 | if (!error.isOk()) { |
| 135 | LOG(ERROR) << "generate_key failed: " << error.description() << std::endl; |
| 136 | return false; |
| 137 | } |
| 138 | if (km_error != km::ErrorCode::OK) { |
| 139 | LOG(ERROR) << "generate_key failed, code " << int32_t(km_error) << std::endl; |
| 140 | return false; |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | bool Keymaster::deleteKey(const std::string& key) { |
| 146 | LOG(ERROR) << "not actually deleting key\n"; |
| 147 | return true; |
| 148 | auto keyBlob = km::support::blob2hidlVec(key); |
| 149 | auto error = mDevice->deleteKey(keyBlob); |
| 150 | if (!error.isOk()) { |
| 151 | LOG(ERROR) << "delete_key failed: " << error.description(); |
| 152 | return false; |
| 153 | } |
| 154 | if (error != km::ErrorCode::OK) { |
| 155 | LOG(ERROR) << "delete_key failed, code " << int32_t(km::ErrorCode(error)); |
| 156 | return false; |
| 157 | } |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | bool Keymaster::upgradeKey(const std::string& oldKey, const km::AuthorizationSet& inParams, |
| 162 | std::string* newKey) { |
| 163 | auto oldKeyBlob = km::support::blob2hidlVec(oldKey); |
| 164 | km::ErrorCode km_error; |
| 165 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) { |
| 166 | km_error = ret; |
| 167 | if (km_error != km::ErrorCode::OK) return; |
| 168 | if (newKey) |
| 169 | newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]), |
| 170 | upgradedKeyBlob.size()); |
| 171 | }; |
| 172 | auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb); |
| 173 | if (!error.isOk()) { |
| 174 | LOG(ERROR) << "upgrade_key failed: " << error.description() << std::endl; |
| 175 | return false; |
| 176 | } |
| 177 | if (km_error != km::ErrorCode::OK) { |
| 178 | LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error) << std::endl; |
| 179 | return false; |
| 180 | } |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | KeymasterOperation Keymaster::begin(km::KeyPurpose purpose, const std::string& key, |
| 185 | const km::AuthorizationSet& inParams, |
| 186 | const km::HardwareAuthToken& authToken, |
| 187 | km::AuthorizationSet* outParams) { |
| 188 | auto keyBlob = km::support::blob2hidlVec(key); |
| 189 | uint64_t mOpHandle; |
| 190 | km::ErrorCode km_error; |
| 191 | |
| 192 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<km::KeyParameter>& _outParams, |
| 193 | uint64_t operationHandle) { |
| 194 | km_error = ret; |
| 195 | if (km_error != km::ErrorCode::OK) return; |
| 196 | if (outParams) *outParams = _outParams; |
| 197 | mOpHandle = operationHandle; |
| 198 | }; |
| 199 | |
| 200 | auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), authToken, hidlCb); |
| 201 | if (!error.isOk()) { |
| 202 | LOG(ERROR) << "begin failed: " << error.description() << std::endl; |
| 203 | return KeymasterOperation(km::ErrorCode::UNKNOWN_ERROR); |
| 204 | } |
| 205 | if (km_error != km::ErrorCode::OK) { |
| 206 | LOG(ERROR) << "begin failed, code " << int32_t(km_error) << std::endl; |
| 207 | return KeymasterOperation(km_error); |
| 208 | } |
| 209 | return KeymasterOperation(mDevice.get(), mOpHandle); |
| 210 | } |
| 211 | |
| 212 | bool Keymaster::isSecure() { |
| 213 | return mDevice->halVersion().securityLevel != km::SecurityLevel::SOFTWARE; |
| 214 | } |
| 215 | |
| 216 | } // namespace vold |
| 217 | } // namespace android |
| 218 | |
| 219 | using namespace ::android::vold; |
| 220 | |
| 221 | int keymaster_compatibility_cryptfs_scrypt() { |
| 222 | Keymaster dev; |
| 223 | if (!dev) { |
| 224 | LOG(ERROR) << "Failed to initiate keymaster session" << std::endl; |
| 225 | return -1; |
| 226 | } |
| 227 | return dev.isSecure(); |
| 228 | } |
| 229 | |
| 230 | static bool write_string_to_buf(const std::string& towrite, uint8_t* buffer, uint32_t buffer_size, |
| 231 | uint32_t* out_size) { |
| 232 | if (!buffer || !out_size) { |
| 233 | LOG(ERROR) << "Missing target pointers" << std::endl; |
| 234 | return false; |
| 235 | } |
| 236 | *out_size = towrite.size(); |
| 237 | if (buffer_size < towrite.size()) { |
| 238 | LOG(ERROR) << "Buffer too small " << buffer_size << " < " << towrite.size() << std::endl; |
| 239 | return false; |
| 240 | } |
| 241 | memset(buffer, '\0', buffer_size); |
| 242 | std::copy(towrite.begin(), towrite.end(), buffer); |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | static km::AuthorizationSet keyParams(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 247 | uint32_t ratelimit) { |
| 248 | return km::AuthorizationSetBuilder() |
| 249 | .RsaSigningKey(rsa_key_size, rsa_exponent) |
| 250 | .NoDigestOrPadding() |
| 251 | .Authorization(km::TAG_BLOB_USAGE_REQUIREMENTS, km::KeyBlobUsageRequirements::STANDALONE) |
| 252 | .Authorization(km::TAG_NO_AUTH_REQUIRED) |
| 253 | .Authorization(km::TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit); |
| 254 | } |
| 255 | |
| 256 | int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 257 | uint32_t ratelimit, uint8_t* key_buffer, |
| 258 | uint32_t key_buffer_size, uint32_t* key_out_size) { |
| 259 | if (key_out_size) { |
| 260 | *key_out_size = 0; |
| 261 | } |
| 262 | Keymaster dev; |
| 263 | if (!dev) { |
| 264 | LOG(ERROR) << "Failed to initiate keymaster session" << std::endl; |
| 265 | return -1; |
| 266 | } |
| 267 | std::string key; |
| 268 | if (!dev.generateKey(keyParams(rsa_key_size, rsa_exponent, ratelimit), &key)) return -1; |
| 269 | if (!write_string_to_buf(key, key_buffer, key_buffer_size, key_out_size)) return -1; |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 274 | uint32_t ratelimit, const uint8_t* key_blob, |
| 275 | size_t key_blob_size, uint8_t* key_buffer, |
| 276 | uint32_t key_buffer_size, uint32_t* key_out_size) { |
| 277 | if (key_out_size) { |
| 278 | *key_out_size = 0; |
| 279 | } |
| 280 | Keymaster dev; |
| 281 | if (!dev) { |
| 282 | LOG(ERROR) << "Failed to initiate keymaster session" << std::endl; |
| 283 | return -1; |
| 284 | } |
| 285 | std::string old_key(reinterpret_cast<const char*>(key_blob), key_blob_size); |
| 286 | std::string new_key; |
| 287 | if (!dev.upgradeKey(old_key, keyParams(rsa_key_size, rsa_exponent, ratelimit), &new_key)) |
| 288 | return -1; |
| 289 | if (!write_string_to_buf(new_key, key_buffer, key_buffer_size, key_out_size)) return -1; |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt( |
| 294 | const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object, |
| 295 | const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size) { |
| 296 | Keymaster dev; |
| 297 | if (!dev) { |
| 298 | LOG(ERROR) << "Failed to initiate keymaster session" << std::endl; |
| 299 | return KeymasterSignResult::error; |
| 300 | } |
| 301 | if (!key_blob || !object || !signature_buffer || !signature_buffer_size) { |
| 302 | LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument" << std::endl; |
| 303 | return KeymasterSignResult::error; |
| 304 | } |
| 305 | |
| 306 | km::AuthorizationSet outParams; |
| 307 | std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size); |
| 308 | std::string input(reinterpret_cast<const char*>(object), object_size); |
| 309 | std::string output; |
| 310 | KeymasterOperation op; |
| 311 | |
| 312 | auto paramBuilder = km::AuthorizationSetBuilder().NoDigestOrPadding(); |
| 313 | while (true) { |
| 314 | op = dev.begin(km::KeyPurpose::SIGN, key, paramBuilder, km::HardwareAuthToken(), &outParams); |
| 315 | if (op.errorCode() == km::ErrorCode::KEY_RATE_LIMIT_EXCEEDED) { |
| 316 | sleep(ratelimit); |
| 317 | continue; |
| 318 | } else |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | if (op.errorCode() == km::ErrorCode::KEY_REQUIRES_UPGRADE) { |
| 323 | LOG(ERROR) << "Keymaster key requires upgrade" << std::endl; |
| 324 | return KeymasterSignResult::upgrade; |
| 325 | } |
| 326 | |
| 327 | if (op.errorCode() != km::ErrorCode::OK) { |
| 328 | LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode()) << std::endl; |
| 329 | return KeymasterSignResult::error; |
| 330 | } |
| 331 | |
| 332 | if (!op.updateCompletely(input, &output)) { |
| 333 | LOG(ERROR) << "Error sending data to keymaster signature transaction: " |
| 334 | << uint32_t(op.errorCode()) << std::endl; |
| 335 | return KeymasterSignResult::error; |
| 336 | } |
| 337 | |
| 338 | if (!op.finish(&output)) { |
| 339 | LOG(ERROR) << "Error finalizing keymaster signature transaction: " |
| 340 | << int32_t(op.errorCode()) << std::endl; |
| 341 | return KeymasterSignResult::error; |
| 342 | } |
| 343 | |
| 344 | *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size())); |
| 345 | if (*signature_buffer == nullptr) { |
| 346 | LOG(ERROR) << "Error allocation buffer for keymaster signature" << std::endl; |
| 347 | return KeymasterSignResult::error; |
| 348 | } |
| 349 | *signature_buffer_size = output.size(); |
| 350 | std::copy(output.data(), output.data() + output.size(), *signature_buffer); |
| 351 | return KeymasterSignResult::ok; |
| 352 | } |