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