Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [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 "Keymaster.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 20 | #include <keystore/keymaster_tags.h> |
| 21 | #include <keystore/authorization_set.h> |
| 22 | #include <keystore/keystore_hidl_support.h> |
| 23 | |
| 24 | using namespace ::keystore; |
Shawn Willden | 12e72ad | 2017-03-01 06:44:15 -0700 | [diff] [blame] | 25 | using android::hardware::hidl_string; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace vold { |
| 29 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 30 | KeymasterOperation::~KeymasterOperation() { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 31 | if (mDevice.get()) mDevice->abort(mOpHandle); |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 34 | bool KeymasterOperation::updateCompletely(const char* input, size_t inputLen, |
| 35 | const std::function<void(const char*, size_t)> consumer) { |
| 36 | uint32_t inputConsumed = 0; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 37 | |
| 38 | ErrorCode km_error; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 39 | auto hidlCB = [&] (ErrorCode ret, uint32_t inputConsumedDelta, |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 40 | const hidl_vec<KeyParameter>& /*ignored*/, const hidl_vec<uint8_t>& _output) { |
| 41 | km_error = ret; |
| 42 | if (km_error != ErrorCode::OK) return; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 43 | inputConsumed += inputConsumedDelta; |
| 44 | consumer(reinterpret_cast<const char*>(&_output[0]), _output.size()); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 47 | while (inputConsumed != inputLen) { |
| 48 | size_t toRead = static_cast<size_t>(inputLen - inputConsumed); |
| 49 | auto inputBlob = |
| 50 | blob2hidlVec(reinterpret_cast<const uint8_t*>(&input[inputConsumed]), toRead); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 51 | auto error = mDevice->update(mOpHandle, hidl_vec<KeyParameter>(), inputBlob, hidlCB); |
| 52 | if (!error.isOk()) { |
| 53 | LOG(ERROR) << "update failed: " << error.description(); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 54 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 55 | return false; |
| 56 | } |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 57 | if (km_error != ErrorCode::OK) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 58 | LOG(ERROR) << "update failed, code " << int32_t(km_error); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 59 | mDevice = nullptr; |
| 60 | return false; |
| 61 | } |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 62 | if (inputConsumed > inputLen) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 63 | LOG(ERROR) << "update reported too much input consumed"; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 64 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 65 | return false; |
| 66 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 71 | bool KeymasterOperation::finish(std::string* output) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 72 | ErrorCode km_error; |
| 73 | auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& /*ignored*/, |
| 74 | const hidl_vec<uint8_t>& _output) { |
| 75 | km_error = ret; |
| 76 | if (km_error != ErrorCode::OK) return; |
| 77 | if (output) |
| 78 | output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size()); |
| 79 | }; |
| 80 | auto error = mDevice->finish(mOpHandle, hidl_vec<KeyParameter>(), hidl_vec<uint8_t>(), |
| 81 | hidl_vec<uint8_t>(), hidlCb); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 82 | mDevice = nullptr; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 83 | if (!error.isOk()) { |
| 84 | LOG(ERROR) << "finish failed: " << error.description(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 85 | return false; |
| 86 | } |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 87 | if (km_error != ErrorCode::OK) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 88 | LOG(ERROR) << "finish failed, code " << int32_t(km_error); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 89 | return false; |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 90 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
| 94 | Keymaster::Keymaster() { |
Chris Phoenix | 9e8a577 | 2017-01-25 15:15:46 -0800 | [diff] [blame] | 95 | mDevice = ::android::hardware::keymaster::V3_0::IKeymasterDevice::getService(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 98 | bool Keymaster::generateKey(const AuthorizationSet& inParams, std::string* key) { |
| 99 | ErrorCode km_error; |
| 100 | auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& keyBlob, |
| 101 | const KeyCharacteristics& /*ignored*/) { |
| 102 | km_error = ret; |
| 103 | if (km_error != ErrorCode::OK) return; |
| 104 | if (key) |
| 105 | key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size()); |
| 106 | }; |
| 107 | |
| 108 | auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb); |
| 109 | if (!error.isOk()) { |
| 110 | LOG(ERROR) << "generate_key failed: " << error.description(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 111 | return false; |
| 112 | } |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 113 | if (km_error != ErrorCode::OK) { |
| 114 | LOG(ERROR) << "generate_key failed, code " << int32_t(km_error); |
| 115 | return false; |
| 116 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 117 | return true; |
| 118 | } |
| 119 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 120 | bool Keymaster::deleteKey(const std::string& key) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 121 | auto keyBlob = blob2hidlVec(key); |
| 122 | auto error = mDevice->deleteKey(keyBlob); |
| 123 | if (!error.isOk()) { |
| 124 | LOG(ERROR) << "delete_key failed: " << error.description(); |
| 125 | return false; |
| 126 | } |
| 127 | if (ErrorCode(error) != ErrorCode::OK) { |
| 128 | LOG(ERROR) << "delete_key failed, code " << uint32_t(ErrorCode(error)); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 129 | return false; |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 134 | bool Keymaster::upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams, |
| 135 | std::string* newKey) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 136 | auto oldKeyBlob = blob2hidlVec(oldKey); |
| 137 | ErrorCode km_error; |
| 138 | auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) { |
| 139 | km_error = ret; |
| 140 | if (km_error != ErrorCode::OK) return; |
| 141 | if (newKey) |
| 142 | newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]), |
| 143 | upgradedKeyBlob.size()); |
| 144 | }; |
| 145 | auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb); |
| 146 | if (!error.isOk()) { |
| 147 | LOG(ERROR) << "upgrade_key failed: " << error.description(); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 148 | return false; |
| 149 | } |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 150 | if (km_error != ErrorCode::OK) { |
| 151 | LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error); |
| 152 | return false; |
| 153 | } |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 157 | KeymasterOperation Keymaster::begin(KeyPurpose purpose, const std::string& key, |
| 158 | const AuthorizationSet& inParams, |
| 159 | AuthorizationSet* outParams) { |
| 160 | auto keyBlob = blob2hidlVec(key); |
| 161 | uint64_t mOpHandle; |
| 162 | ErrorCode km_error; |
| 163 | |
| 164 | auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& _outParams, |
| 165 | uint64_t operationHandle) { |
| 166 | km_error = ret; |
| 167 | if (km_error != ErrorCode::OK) return; |
| 168 | if (outParams) |
| 169 | *outParams = _outParams; |
| 170 | mOpHandle = operationHandle; |
| 171 | }; |
| 172 | |
| 173 | auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), hidlCb); |
| 174 | if (!error.isOk()) { |
| 175 | LOG(ERROR) << "begin failed: " << error.description(); |
| 176 | return KeymasterOperation(ErrorCode::UNKNOWN_ERROR); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 177 | } |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 178 | if (km_error != ErrorCode::OK) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 179 | LOG(ERROR) << "begin failed, code " << int32_t(km_error); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 180 | return KeymasterOperation(km_error); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 181 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 182 | return KeymasterOperation(mDevice, mOpHandle); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 183 | } |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 184 | bool Keymaster::isSecure() { |
| 185 | bool _isSecure = false; |
| 186 | auto rc = mDevice->getHardwareFeatures( |
Shawn Willden | 12e72ad | 2017-03-01 06:44:15 -0700 | [diff] [blame] | 187 | [&] (bool isSecure, bool, bool, bool, bool, const hidl_string&, const hidl_string&) { |
| 188 | _isSecure = isSecure; }); |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 189 | return rc.isOk() && _isSecure; |
| 190 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 191 | |
| 192 | } // namespace vold |
| 193 | } // namespace android |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 194 | |
| 195 | using namespace ::android::vold; |
| 196 | |
| 197 | int keymaster_compatibility_cryptfs_scrypt() { |
Janis Danisevskis | e7152c3 | 2017-03-08 11:02:30 -0800 | [diff] [blame] | 198 | Keymaster dev; |
| 199 | if (!dev) { |
| 200 | LOG(ERROR) << "Failed to initiate keymaster session"; |
| 201 | return -1; |
| 202 | } |
| 203 | return dev.isSecure(); |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, |
| 207 | uint64_t rsa_exponent, |
| 208 | uint32_t ratelimit, |
| 209 | uint8_t* key_buffer, |
| 210 | uint32_t key_buffer_size, |
| 211 | uint32_t* key_out_size) |
| 212 | { |
| 213 | Keymaster dev; |
| 214 | std::string key; |
| 215 | if (!dev) { |
| 216 | LOG(ERROR) << "Failed to initiate keymaster session"; |
| 217 | return -1; |
| 218 | } |
| 219 | if (!key_buffer || !key_out_size) { |
| 220 | LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument"; |
| 221 | return -1; |
| 222 | } |
| 223 | if (key_out_size) { |
| 224 | *key_out_size = 0; |
| 225 | } |
| 226 | |
| 227 | auto paramBuilder = AuthorizationSetBuilder() |
| 228 | .Authorization(TAG_ALGORITHM, Algorithm::RSA) |
| 229 | .Authorization(TAG_KEY_SIZE, rsa_key_size) |
| 230 | .Authorization(TAG_RSA_PUBLIC_EXPONENT, rsa_exponent) |
| 231 | .Authorization(TAG_PURPOSE, KeyPurpose::SIGN) |
| 232 | .Authorization(TAG_PADDING, PaddingMode::NONE) |
| 233 | .Authorization(TAG_DIGEST, Digest::NONE) |
| 234 | .Authorization(TAG_BLOB_USAGE_REQUIREMENTS, |
| 235 | KeyBlobUsageRequirements::STANDALONE) |
| 236 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 237 | .Authorization(TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit); |
| 238 | |
| 239 | if (!dev.generateKey(paramBuilder, &key)) { |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | if (key_out_size) { |
| 244 | *key_out_size = key.size(); |
| 245 | } |
| 246 | |
| 247 | if (key_buffer_size < key.size()) { |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | std::copy(key.data(), key.data() + key.size(), key_buffer); |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | int keymaster_sign_object_for_cryptfs_scrypt(const uint8_t* key_blob, |
| 256 | size_t key_blob_size, |
| 257 | uint32_t ratelimit, |
| 258 | const uint8_t* object, |
| 259 | const size_t object_size, |
| 260 | uint8_t** signature_buffer, |
| 261 | size_t* signature_buffer_size) |
| 262 | { |
| 263 | Keymaster dev; |
| 264 | if (!dev) { |
| 265 | LOG(ERROR) << "Failed to initiate keymaster session"; |
| 266 | return -1; |
| 267 | } |
| 268 | if (!key_blob || !object || !signature_buffer || !signature_buffer_size) { |
| 269 | LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument"; |
| 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | AuthorizationSet outParams; |
| 274 | std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size); |
| 275 | std::string input(reinterpret_cast<const char*>(object), object_size); |
| 276 | std::string output; |
| 277 | KeymasterOperation op; |
| 278 | |
| 279 | auto paramBuilder = AuthorizationSetBuilder() |
| 280 | .Authorization(TAG_PADDING, PaddingMode::NONE) |
| 281 | .Authorization(TAG_DIGEST, Digest::NONE); |
| 282 | |
| 283 | while (true) { |
| 284 | op = dev.begin(KeyPurpose::SIGN, key, paramBuilder, &outParams); |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 285 | if (op.errorCode() == ErrorCode::KEY_RATE_LIMIT_EXCEEDED) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 286 | sleep(ratelimit); |
| 287 | continue; |
| 288 | } else break; |
| 289 | } |
| 290 | |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 291 | if (op.errorCode() != ErrorCode::OK) { |
| 292 | LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode()); |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 293 | return -1; |
| 294 | } |
| 295 | |
| 296 | if (!op.updateCompletely(input, &output)) { |
| 297 | LOG(ERROR) << "Error sending data to keymaster signature transaction: " |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 298 | << uint32_t(op.errorCode()); |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 299 | return -1; |
| 300 | } |
| 301 | |
| 302 | if (!op.finish(&output)) { |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 303 | LOG(ERROR) << "Error finalizing keymaster signature transaction: " << int32_t(op.errorCode()); |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size())); |
| 308 | if (*signature_buffer == nullptr) { |
| 309 | LOG(ERROR) << "Error allocation buffer for keymaster signature"; |
| 310 | return -1; |
| 311 | } |
| 312 | *signature_buffer_size = output.size(); |
| 313 | std::copy(output.data(), output.data() + output.size(), *signature_buffer); |
| 314 | return 0; |
| 315 | } |