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