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