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 | |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 221 | /* |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 222 | int keymaster_compatibility_cryptfs_scrypt() { |
| 223 | Keymaster dev; |
| 224 | if (!dev) { |
| 225 | LOG(ERROR) << "Failed to initiate keymaster session" << std::endl; |
| 226 | return -1; |
| 227 | } |
| 228 | return dev.isSecure(); |
| 229 | } |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 230 | */ |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 231 | |
| 232 | static bool write_string_to_buf(const std::string& towrite, uint8_t* buffer, uint32_t buffer_size, |
| 233 | uint32_t* out_size) { |
| 234 | if (!buffer || !out_size) { |
| 235 | LOG(ERROR) << "Missing target pointers" << std::endl; |
| 236 | return false; |
| 237 | } |
| 238 | *out_size = towrite.size(); |
| 239 | if (buffer_size < towrite.size()) { |
| 240 | LOG(ERROR) << "Buffer too small " << buffer_size << " < " << towrite.size() << std::endl; |
| 241 | return false; |
| 242 | } |
| 243 | memset(buffer, '\0', buffer_size); |
| 244 | std::copy(towrite.begin(), towrite.end(), buffer); |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | static km::AuthorizationSet keyParams(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 249 | uint32_t ratelimit) { |
| 250 | return km::AuthorizationSetBuilder() |
| 251 | .RsaSigningKey(rsa_key_size, rsa_exponent) |
| 252 | .NoDigestOrPadding() |
| 253 | .Authorization(km::TAG_BLOB_USAGE_REQUIREMENTS, km::KeyBlobUsageRequirements::STANDALONE) |
| 254 | .Authorization(km::TAG_NO_AUTH_REQUIRED) |
| 255 | .Authorization(km::TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit); |
| 256 | } |
| 257 | |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 258 | /* |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 259 | int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 260 | uint32_t ratelimit, uint8_t* key_buffer, |
| 261 | uint32_t key_buffer_size, uint32_t* key_out_size) { |
| 262 | if (key_out_size) { |
| 263 | *key_out_size = 0; |
| 264 | } |
| 265 | Keymaster dev; |
| 266 | if (!dev) { |
| 267 | LOG(ERROR) << "Failed to initiate keymaster session" << std::endl; |
| 268 | return -1; |
| 269 | } |
| 270 | std::string key; |
| 271 | if (!dev.generateKey(keyParams(rsa_key_size, rsa_exponent, ratelimit), &key)) return -1; |
| 272 | if (!write_string_to_buf(key, key_buffer, key_buffer_size, key_out_size)) return -1; |
| 273 | return 0; |
| 274 | } |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 275 | */ |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 276 | |
| 277 | int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 278 | uint32_t ratelimit, const uint8_t* key_blob, |
| 279 | size_t key_blob_size, uint8_t* key_buffer, |
| 280 | uint32_t key_buffer_size, uint32_t* key_out_size) { |
| 281 | if (key_out_size) { |
| 282 | *key_out_size = 0; |
| 283 | } |
| 284 | Keymaster dev; |
| 285 | if (!dev) { |
| 286 | LOG(ERROR) << "Failed to initiate keymaster session" << std::endl; |
| 287 | return -1; |
| 288 | } |
| 289 | std::string old_key(reinterpret_cast<const char*>(key_blob), key_blob_size); |
| 290 | std::string new_key; |
| 291 | if (!dev.upgradeKey(old_key, keyParams(rsa_key_size, rsa_exponent, ratelimit), &new_key)) |
| 292 | return -1; |
| 293 | if (!write_string_to_buf(new_key, key_buffer, key_buffer_size, key_out_size)) return -1; |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt( |
| 298 | const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object, |
| 299 | const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size) { |
| 300 | Keymaster dev; |
| 301 | if (!dev) { |
| 302 | LOG(ERROR) << "Failed to initiate keymaster session" << std::endl; |
| 303 | return KeymasterSignResult::error; |
| 304 | } |
| 305 | if (!key_blob || !object || !signature_buffer || !signature_buffer_size) { |
| 306 | LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument" << std::endl; |
| 307 | return KeymasterSignResult::error; |
| 308 | } |
| 309 | |
| 310 | km::AuthorizationSet outParams; |
| 311 | std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size); |
| 312 | std::string input(reinterpret_cast<const char*>(object), object_size); |
| 313 | std::string output; |
| 314 | KeymasterOperation op; |
| 315 | |
| 316 | auto paramBuilder = km::AuthorizationSetBuilder().NoDigestOrPadding(); |
| 317 | while (true) { |
| 318 | op = dev.begin(km::KeyPurpose::SIGN, key, paramBuilder, km::HardwareAuthToken(), &outParams); |
| 319 | if (op.errorCode() == km::ErrorCode::KEY_RATE_LIMIT_EXCEEDED) { |
| 320 | sleep(ratelimit); |
| 321 | continue; |
| 322 | } else |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | if (op.errorCode() == km::ErrorCode::KEY_REQUIRES_UPGRADE) { |
| 327 | LOG(ERROR) << "Keymaster key requires upgrade" << std::endl; |
| 328 | return KeymasterSignResult::upgrade; |
| 329 | } |
| 330 | |
| 331 | if (op.errorCode() != km::ErrorCode::OK) { |
| 332 | LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode()) << std::endl; |
| 333 | return KeymasterSignResult::error; |
| 334 | } |
| 335 | |
| 336 | if (!op.updateCompletely(input, &output)) { |
| 337 | LOG(ERROR) << "Error sending data to keymaster signature transaction: " |
| 338 | << uint32_t(op.errorCode()) << std::endl; |
| 339 | return KeymasterSignResult::error; |
| 340 | } |
| 341 | |
| 342 | if (!op.finish(&output)) { |
| 343 | LOG(ERROR) << "Error finalizing keymaster signature transaction: " |
| 344 | << int32_t(op.errorCode()) << std::endl; |
| 345 | return KeymasterSignResult::error; |
| 346 | } |
| 347 | |
| 348 | *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size())); |
| 349 | if (*signature_buffer == nullptr) { |
| 350 | LOG(ERROR) << "Error allocation buffer for keymaster signature" << std::endl; |
| 351 | return KeymasterSignResult::error; |
| 352 | } |
| 353 | *signature_buffer_size = output.size(); |
| 354 | std::copy(output.data(), output.data() + output.size(), *signature_buffer); |
| 355 | return KeymasterSignResult::ok; |
| 356 | } |