Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2016, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "android.hardware.keymaster@3.0-impl" |
| 19 | |
| 20 | #include "legacy_keymaster_device_wrapper.h" |
| 21 | |
| 22 | #include <cutils/log.h> |
| 23 | |
| 24 | #include <hardware/keymaster2.h> |
| 25 | #include <hardware/keymaster_defs.h> |
| 26 | #include <keymaster/keymaster_configuration.h> |
| 27 | #include <keymaster/soft_keymaster_device.h> |
| 28 | |
| 29 | namespace android { |
| 30 | namespace keystore { |
| 31 | |
| 32 | using ::keymaster::SoftKeymasterDevice; |
| 33 | |
| 34 | LegacyKeymasterDeviceWrapper::LegacyKeymasterDeviceWrapper(keymaster2_device_t* dev) |
| 35 | : keymaster_device_(dev) {} |
| 36 | |
| 37 | LegacyKeymasterDeviceWrapper::~LegacyKeymasterDeviceWrapper() { |
| 38 | if (keymaster_device_) keymaster_device_->common.close(&keymaster_device_->common); |
| 39 | } |
| 40 | |
| 41 | static inline keymaster_tag_type_t typeFromTag(const keymaster_tag_t tag) { |
| 42 | return keymaster_tag_get_type(tag); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * legacy_enum_conversion converts enums from hidl to keymaster and back. Currently, this is just a |
| 47 | * cast to make the compiler happy. One of two thigs should happen though: |
| 48 | * TODO The keymaster enums should become aliases for the hidl generated enums so that we have a |
| 49 | * single point of truth. Then this cast function can go away. |
| 50 | */ |
| 51 | inline static keymaster_tag_t legacy_enum_conversion(const Tag value) { |
| 52 | return keymaster_tag_t(value); |
| 53 | } |
| 54 | inline static Tag legacy_enum_conversion(const keymaster_tag_t value) { |
| 55 | return Tag(value); |
| 56 | } |
| 57 | inline static keymaster_purpose_t legacy_enum_conversion(const KeyPurpose value) { |
| 58 | return keymaster_purpose_t(value); |
| 59 | } |
| 60 | inline static keymaster_key_format_t legacy_enum_conversion(const KeyFormat value) { |
| 61 | return keymaster_key_format_t(value); |
| 62 | } |
| 63 | inline static ErrorCode legacy_enum_conversion(const keymaster_error_t value) { |
| 64 | return ErrorCode(value); |
| 65 | } |
| 66 | |
| 67 | class KmParamSet : public keymaster_key_param_set_t { |
| 68 | public: |
| 69 | KmParamSet(const hidl_vec<KeyParameter>& keyParams) { |
| 70 | params = new keymaster_key_param_t[keyParams.size()]; |
| 71 | length = keyParams.size(); |
| 72 | for (size_t i = 0; i < keyParams.size(); ++i) { |
| 73 | auto tag = legacy_enum_conversion(keyParams[i].tag); |
| 74 | switch (typeFromTag(tag)) { |
| 75 | case KM_ENUM: |
| 76 | case KM_ENUM_REP: |
| 77 | params[i] = keymaster_param_enum(tag, keyParams[i].f.integer); |
| 78 | break; |
| 79 | case KM_UINT: |
| 80 | case KM_UINT_REP: |
| 81 | params[i] = keymaster_param_int(tag, keyParams[i].f.integer); |
| 82 | break; |
| 83 | case KM_ULONG: |
| 84 | case KM_ULONG_REP: |
| 85 | params[i] = keymaster_param_long(tag, keyParams[i].f.longInteger); |
| 86 | break; |
| 87 | case KM_DATE: |
| 88 | params[i] = keymaster_param_date(tag, keyParams[i].f.dateTime); |
| 89 | break; |
| 90 | case KM_BOOL: |
| 91 | if (keyParams[i].f.boolValue) |
| 92 | params[i] = keymaster_param_bool(tag); |
| 93 | else |
| 94 | params[i].tag = KM_TAG_INVALID; |
| 95 | break; |
| 96 | case KM_BIGNUM: |
| 97 | case KM_BYTES: |
| 98 | params[i] = |
| 99 | keymaster_param_blob(tag, &keyParams[i].blob[0], keyParams[i].blob.size()); |
| 100 | break; |
| 101 | case KM_INVALID: |
| 102 | default: |
| 103 | params[i].tag = KM_TAG_INVALID; |
| 104 | /* just skip */ |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | } |
Chih-Hung Hsieh | f73b198 | 2018-09-25 12:03:21 -0700 | [diff] [blame^] | 109 | KmParamSet(KmParamSet&& other) noexcept |
| 110 | : keymaster_key_param_set_t{other.params, other.length} { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 111 | other.length = 0; |
| 112 | other.params = nullptr; |
| 113 | } |
| 114 | KmParamSet(const KmParamSet&) = delete; |
| 115 | ~KmParamSet() { delete[] params; } |
| 116 | }; |
| 117 | |
| 118 | inline static KmParamSet hidlParams2KmParamSet(const hidl_vec<KeyParameter>& params) { |
| 119 | return KmParamSet(params); |
| 120 | } |
| 121 | |
| 122 | inline static keymaster_blob_t hidlVec2KmBlob(const hidl_vec<uint8_t>& blob) { |
| 123 | /* hidl unmarshals funny pointers if the the blob is empty */ |
| 124 | if (blob.size()) return {&blob[0], blob.size()}; |
| 125 | return {}; |
| 126 | } |
| 127 | |
| 128 | inline static keymaster_key_blob_t hidlVec2KmKeyBlob(const hidl_vec<uint8_t>& blob) { |
| 129 | /* hidl unmarshals funny pointers if the the blob is empty */ |
| 130 | if (blob.size()) return {&blob[0], blob.size()}; |
| 131 | return {}; |
| 132 | } |
| 133 | |
| 134 | inline static hidl_vec<uint8_t> kmBlob2hidlVec(const keymaster_key_blob_t& blob) { |
| 135 | hidl_vec<uint8_t> result; |
| 136 | result.setToExternal(const_cast<unsigned char*>(blob.key_material), blob.key_material_size); |
| 137 | return result; |
| 138 | } |
| 139 | inline static hidl_vec<uint8_t> kmBlob2hidlVec(const keymaster_blob_t& blob) { |
| 140 | hidl_vec<uint8_t> result; |
| 141 | result.setToExternal(const_cast<unsigned char*>(blob.data), blob.data_length); |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | inline static hidl_vec<hidl_vec<uint8_t>> |
| 146 | kmCertChain2Hidl(const keymaster_cert_chain_t* cert_chain) { |
| 147 | hidl_vec<hidl_vec<uint8_t>> result; |
| 148 | if (!cert_chain || cert_chain->entry_count == 0 || !cert_chain->entries) return result; |
| 149 | |
| 150 | result.resize(cert_chain->entry_count); |
| 151 | for (size_t i = 0; i < cert_chain->entry_count; ++i) { |
| 152 | auto& entry = cert_chain->entries[i]; |
| 153 | result[i] = kmBlob2hidlVec(entry); |
| 154 | } |
| 155 | |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | static inline hidl_vec<KeyParameter> kmParamSet2Hidl(const keymaster_key_param_set_t& set) { |
| 160 | hidl_vec<KeyParameter> result; |
| 161 | if (set.length == 0 || set.params == nullptr) return result; |
| 162 | |
| 163 | result.resize(set.length); |
| 164 | keymaster_key_param_t* params = set.params; |
| 165 | for (size_t i = 0; i < set.length; ++i) { |
| 166 | auto tag = params[i].tag; |
| 167 | result[i].tag = legacy_enum_conversion(tag); |
| 168 | switch (typeFromTag(tag)) { |
| 169 | case KM_ENUM: |
| 170 | case KM_ENUM_REP: |
| 171 | result[i].f.integer = params[i].enumerated; |
| 172 | break; |
| 173 | case KM_UINT: |
| 174 | case KM_UINT_REP: |
| 175 | result[i].f.integer = params[i].integer; |
| 176 | break; |
| 177 | case KM_ULONG: |
| 178 | case KM_ULONG_REP: |
| 179 | result[i].f.longInteger = params[i].long_integer; |
| 180 | break; |
| 181 | case KM_DATE: |
| 182 | result[i].f.dateTime = params[i].date_time; |
| 183 | break; |
| 184 | case KM_BOOL: |
| 185 | result[i].f.boolValue = params[i].boolean; |
| 186 | break; |
| 187 | case KM_BIGNUM: |
| 188 | case KM_BYTES: |
| 189 | result[i].blob.setToExternal(const_cast<unsigned char*>(params[i].blob.data), |
| 190 | params[i].blob.data_length); |
| 191 | break; |
| 192 | case KM_INVALID: |
| 193 | default: |
| 194 | params[i].tag = KM_TAG_INVALID; |
| 195 | /* just skip */ |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | return result; |
| 200 | } |
| 201 | |
| 202 | // Methods from ::android::hardware::keymaster::V3_0::IKeymasterDevice follow. |
| 203 | Return<void> LegacyKeymasterDeviceWrapper::getHardwareFeatures(getHardwareFeatures_cb _hidl_cb) { |
Shawn Willden | b8550a0 | 2017-02-23 11:06:05 -0700 | [diff] [blame] | 204 | _hidl_cb(false, false, false, false, false, "Fallback Device", "Google Android Security"); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 205 | return Void(); |
| 206 | } |
| 207 | |
| 208 | Return<ErrorCode> LegacyKeymasterDeviceWrapper::addRngEntropy(const hidl_vec<uint8_t>& data) { |
| 209 | return legacy_enum_conversion( |
| 210 | keymaster_device_->add_rng_entropy(keymaster_device_, &data[0], data.size())); |
| 211 | } |
| 212 | |
| 213 | Return<void> LegacyKeymasterDeviceWrapper::generateKey(const hidl_vec<KeyParameter>& keyParams, |
| 214 | generateKey_cb _hidl_cb) { |
| 215 | // result variables for the wire |
| 216 | KeyCharacteristics resultCharacteristics; |
| 217 | hidl_vec<uint8_t> resultKeyBlob; |
| 218 | |
| 219 | // result variables the backend understands |
| 220 | keymaster_key_blob_t key_blob{nullptr, 0}; |
| 221 | keymaster_key_characteristics_t key_characteristics{{nullptr, 0}, {nullptr, 0}}; |
| 222 | |
| 223 | // convert the parameter set to something our backend understands |
| 224 | auto kmParams = hidlParams2KmParamSet(keyParams); |
| 225 | |
| 226 | auto rc = keymaster_device_->generate_key(keymaster_device_, &kmParams, &key_blob, |
| 227 | &key_characteristics); |
| 228 | |
| 229 | if (rc == KM_ERROR_OK) { |
| 230 | // on success convert the result to wire format |
| 231 | resultKeyBlob = kmBlob2hidlVec(key_blob); |
| 232 | resultCharacteristics.softwareEnforced = kmParamSet2Hidl(key_characteristics.sw_enforced); |
| 233 | resultCharacteristics.teeEnforced = kmParamSet2Hidl(key_characteristics.hw_enforced); |
| 234 | } |
| 235 | |
| 236 | // send results off to the client |
| 237 | _hidl_cb(legacy_enum_conversion(rc), resultKeyBlob, resultCharacteristics); |
| 238 | |
| 239 | // free buffers that we are responsible for |
| 240 | if (key_blob.key_material) free(const_cast<uint8_t*>(key_blob.key_material)); |
| 241 | keymaster_free_characteristics(&key_characteristics); |
| 242 | |
| 243 | return Void(); |
| 244 | } |
| 245 | |
| 246 | Return<void> LegacyKeymasterDeviceWrapper::getKeyCharacteristics( |
| 247 | const hidl_vec<uint8_t>& keyBlob, const hidl_vec<uint8_t>& clientId, |
| 248 | const hidl_vec<uint8_t>& appData, getKeyCharacteristics_cb _hidl_cb) { |
| 249 | // result variables for the wire |
| 250 | KeyCharacteristics resultCharacteristics; |
| 251 | |
| 252 | // result variables the backend understands |
| 253 | keymaster_key_characteristics_t key_characteristics{{nullptr, 0}, {nullptr, 0}}; |
| 254 | |
| 255 | auto kmKeyBlob = hidlVec2KmKeyBlob(keyBlob); |
| 256 | auto kmClientId = hidlVec2KmBlob(clientId); |
| 257 | auto kmAppData = hidlVec2KmBlob(appData); |
| 258 | |
| 259 | auto rc = keymaster_device_->get_key_characteristics( |
| 260 | keymaster_device_, keyBlob.size() ? &kmKeyBlob : nullptr, |
| 261 | clientId.size() ? &kmClientId : nullptr, appData.size() ? &kmAppData : nullptr, |
| 262 | &key_characteristics); |
| 263 | |
| 264 | if (rc == KM_ERROR_OK) { |
| 265 | resultCharacteristics.softwareEnforced = kmParamSet2Hidl(key_characteristics.sw_enforced); |
| 266 | resultCharacteristics.teeEnforced = kmParamSet2Hidl(key_characteristics.hw_enforced); |
| 267 | } |
| 268 | |
| 269 | _hidl_cb(legacy_enum_conversion(rc), resultCharacteristics); |
| 270 | |
| 271 | keymaster_free_characteristics(&key_characteristics); |
| 272 | |
| 273 | return Void(); |
| 274 | } |
| 275 | |
| 276 | Return<void> LegacyKeymasterDeviceWrapper::importKey(const hidl_vec<KeyParameter>& params, |
| 277 | KeyFormat keyFormat, |
| 278 | const hidl_vec<uint8_t>& keyData, |
| 279 | importKey_cb _hidl_cb) { |
| 280 | // result variables for the wire |
| 281 | KeyCharacteristics resultCharacteristics; |
| 282 | hidl_vec<uint8_t> resultKeyBlob; |
| 283 | |
| 284 | // result variables the backend understands |
| 285 | keymaster_key_blob_t key_blob{nullptr, 0}; |
| 286 | keymaster_key_characteristics_t key_characteristics{{nullptr, 0}, {nullptr, 0}}; |
| 287 | |
| 288 | auto kmParams = hidlParams2KmParamSet(params); |
| 289 | auto kmKeyData = hidlVec2KmBlob(keyData); |
| 290 | |
| 291 | auto rc = keymaster_device_->import_key(keymaster_device_, &kmParams, |
| 292 | legacy_enum_conversion(keyFormat), &kmKeyData, |
| 293 | &key_blob, &key_characteristics); |
| 294 | |
| 295 | if (rc == KM_ERROR_OK) { |
| 296 | // on success convert the result to wire format |
| 297 | resultKeyBlob = kmBlob2hidlVec(key_blob); |
| 298 | resultCharacteristics.softwareEnforced = kmParamSet2Hidl(key_characteristics.sw_enforced); |
| 299 | resultCharacteristics.teeEnforced = kmParamSet2Hidl(key_characteristics.hw_enforced); |
| 300 | } |
| 301 | |
| 302 | _hidl_cb(legacy_enum_conversion(rc), resultKeyBlob, resultCharacteristics); |
| 303 | |
| 304 | // free buffers that we are responsible for |
| 305 | if (key_blob.key_material) free(const_cast<uint8_t*>(key_blob.key_material)); |
| 306 | keymaster_free_characteristics(&key_characteristics); |
| 307 | |
| 308 | return Void(); |
| 309 | } |
| 310 | |
| 311 | Return<void> LegacyKeymasterDeviceWrapper::exportKey(KeyFormat exportFormat, |
| 312 | const hidl_vec<uint8_t>& keyBlob, |
| 313 | const hidl_vec<uint8_t>& clientId, |
| 314 | const hidl_vec<uint8_t>& appData, |
| 315 | exportKey_cb _hidl_cb) { |
| 316 | |
| 317 | // result variables for the wire |
| 318 | hidl_vec<uint8_t> resultKeyBlob; |
| 319 | |
| 320 | // result variables the backend understands |
| 321 | keymaster_blob_t out_blob = {}; |
| 322 | |
| 323 | auto kmKeyBlob = hidlVec2KmKeyBlob(keyBlob); |
| 324 | auto kmClientId = hidlVec2KmBlob(clientId); |
| 325 | auto kmAppData = hidlVec2KmBlob(appData); |
| 326 | |
| 327 | auto rc = keymaster_device_->export_key(keymaster_device_, legacy_enum_conversion(exportFormat), |
| 328 | keyBlob.size() ? &kmKeyBlob : nullptr, |
| 329 | clientId.size() ? &kmClientId : nullptr, |
| 330 | appData.size() ? &kmAppData : nullptr, &out_blob); |
| 331 | |
| 332 | if (rc == KM_ERROR_OK) { |
| 333 | // on success convert the result to wire format |
| 334 | // (Can we assume that key_blob is {nullptr, 0} or a valid buffer description?) |
| 335 | resultKeyBlob = kmBlob2hidlVec(out_blob); |
| 336 | } |
| 337 | |
| 338 | _hidl_cb(legacy_enum_conversion(rc), resultKeyBlob); |
| 339 | |
| 340 | // free buffers that we are responsible for |
| 341 | if (out_blob.data) free(const_cast<uint8_t*>(out_blob.data)); |
| 342 | |
| 343 | return Void(); |
| 344 | } |
| 345 | |
| 346 | Return<void> LegacyKeymasterDeviceWrapper::attestKey(const hidl_vec<uint8_t>& keyToAttest, |
| 347 | const hidl_vec<KeyParameter>& attestParams, |
| 348 | attestKey_cb _hidl_cb) { |
| 349 | |
| 350 | hidl_vec<hidl_vec<uint8_t>> resultCertChain; |
| 351 | |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 352 | for (size_t i = 0; i < attestParams.size(); ++i) { |
| 353 | switch (attestParams[i].tag) { |
| 354 | case Tag::ATTESTATION_ID_BRAND: |
| 355 | case Tag::ATTESTATION_ID_DEVICE: |
| 356 | case Tag::ATTESTATION_ID_PRODUCT: |
| 357 | case Tag::ATTESTATION_ID_SERIAL: |
| 358 | case Tag::ATTESTATION_ID_IMEI: |
| 359 | case Tag::ATTESTATION_ID_MEID: |
Bartosz Fabianowski | 634a1aa | 2017-03-20 14:02:32 +0100 | [diff] [blame] | 360 | case Tag::ATTESTATION_ID_MANUFACTURER: |
| 361 | case Tag::ATTESTATION_ID_MODEL: |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 362 | // Device id attestation may only be supported if the device is able to permanently |
| 363 | // destroy its knowledge of the ids. This device is unable to do this, so it must |
| 364 | // never perform any device id attestation. |
| 365 | _hidl_cb(ErrorCode::CANNOT_ATTEST_IDS, resultCertChain); |
| 366 | return Void(); |
| 367 | default: |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 372 | keymaster_cert_chain_t cert_chain = {}; |
| 373 | |
| 374 | auto kmKeyToAttest = hidlVec2KmKeyBlob(keyToAttest); |
| 375 | auto kmAttestParams = hidlParams2KmParamSet(attestParams); |
| 376 | |
| 377 | auto rc = keymaster_device_->attest_key(keymaster_device_, &kmKeyToAttest, &kmAttestParams, |
| 378 | &cert_chain); |
| 379 | |
| 380 | if (rc == KM_ERROR_OK) { |
| 381 | resultCertChain = kmCertChain2Hidl(&cert_chain); |
| 382 | } |
| 383 | |
| 384 | _hidl_cb(legacy_enum_conversion(rc), resultCertChain); |
| 385 | |
| 386 | keymaster_free_cert_chain(&cert_chain); |
| 387 | |
| 388 | return Void(); |
| 389 | } |
| 390 | |
| 391 | Return<void> LegacyKeymasterDeviceWrapper::upgradeKey(const hidl_vec<uint8_t>& keyBlobToUpgrade, |
| 392 | const hidl_vec<KeyParameter>& upgradeParams, |
| 393 | upgradeKey_cb _hidl_cb) { |
| 394 | |
| 395 | // result variables for the wire |
| 396 | hidl_vec<uint8_t> resultKeyBlob; |
| 397 | |
| 398 | // result variables the backend understands |
| 399 | keymaster_key_blob_t key_blob = {}; |
| 400 | |
| 401 | auto kmKeyBlobToUpgrade = hidlVec2KmKeyBlob(keyBlobToUpgrade); |
| 402 | auto kmUpgradeParams = hidlParams2KmParamSet(upgradeParams); |
| 403 | |
| 404 | auto rc = keymaster_device_->upgrade_key(keymaster_device_, &kmKeyBlobToUpgrade, |
| 405 | &kmUpgradeParams, &key_blob); |
| 406 | |
| 407 | if (rc == KM_ERROR_OK) { |
| 408 | // on success convert the result to wire format |
| 409 | resultKeyBlob = kmBlob2hidlVec(key_blob); |
| 410 | } |
| 411 | |
| 412 | _hidl_cb(legacy_enum_conversion(rc), resultKeyBlob); |
| 413 | |
| 414 | if (key_blob.key_material) free(const_cast<uint8_t*>(key_blob.key_material)); |
| 415 | |
| 416 | return Void(); |
| 417 | } |
| 418 | |
| 419 | Return<ErrorCode> LegacyKeymasterDeviceWrapper::deleteKey(const hidl_vec<uint8_t>& keyBlob) { |
| 420 | auto kmKeyBlob = hidlVec2KmKeyBlob(keyBlob); |
| 421 | return legacy_enum_conversion(keymaster_device_->delete_key(keymaster_device_, &kmKeyBlob)); |
| 422 | } |
| 423 | |
| 424 | Return<ErrorCode> LegacyKeymasterDeviceWrapper::deleteAllKeys() { |
| 425 | return legacy_enum_conversion(keymaster_device_->delete_all_keys(keymaster_device_)); |
| 426 | } |
| 427 | |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 428 | Return<ErrorCode> LegacyKeymasterDeviceWrapper::destroyAttestationIds() { |
| 429 | return ErrorCode::UNIMPLEMENTED; |
| 430 | } |
| 431 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 432 | Return<void> LegacyKeymasterDeviceWrapper::begin(KeyPurpose purpose, const hidl_vec<uint8_t>& key, |
| 433 | const hidl_vec<KeyParameter>& inParams, |
| 434 | begin_cb _hidl_cb) { |
| 435 | |
| 436 | // result variables for the wire |
| 437 | hidl_vec<KeyParameter> resultParams; |
| 438 | uint64_t resultOpHandle = 0; |
| 439 | |
| 440 | // result variables the backend understands |
| 441 | keymaster_key_param_set_t out_params{nullptr, 0}; |
| 442 | keymaster_operation_handle_t& operation_handle = resultOpHandle; |
| 443 | |
| 444 | auto kmKey = hidlVec2KmKeyBlob(key); |
| 445 | auto kmInParams = hidlParams2KmParamSet(inParams); |
| 446 | |
| 447 | auto rc = keymaster_device_->begin(keymaster_device_, legacy_enum_conversion(purpose), &kmKey, |
| 448 | &kmInParams, &out_params, &operation_handle); |
| 449 | |
| 450 | if (rc == KM_ERROR_OK) resultParams = kmParamSet2Hidl(out_params); |
| 451 | |
| 452 | _hidl_cb(legacy_enum_conversion(rc), resultParams, resultOpHandle); |
| 453 | |
| 454 | keymaster_free_param_set(&out_params); |
| 455 | |
| 456 | return Void(); |
| 457 | } |
| 458 | |
| 459 | Return<void> LegacyKeymasterDeviceWrapper::update(uint64_t operationHandle, |
| 460 | const hidl_vec<KeyParameter>& inParams, |
| 461 | const hidl_vec<uint8_t>& input, |
| 462 | update_cb _hidl_cb) { |
| 463 | // result variables for the wire |
| 464 | uint32_t resultConsumed = 0; |
| 465 | hidl_vec<KeyParameter> resultParams; |
| 466 | hidl_vec<uint8_t> resultBlob; |
| 467 | |
| 468 | // result variables the backend understands |
| 469 | size_t consumed = 0; |
| 470 | keymaster_key_param_set_t out_params = {}; |
| 471 | keymaster_blob_t out_blob = {}; |
| 472 | |
| 473 | auto kmInParams = hidlParams2KmParamSet(inParams); |
| 474 | auto kmInput = hidlVec2KmBlob(input); |
| 475 | |
| 476 | auto rc = keymaster_device_->update(keymaster_device_, operationHandle, &kmInParams, &kmInput, |
| 477 | &consumed, &out_params, &out_blob); |
| 478 | |
| 479 | if (rc == KM_ERROR_OK) { |
| 480 | resultConsumed = consumed; |
| 481 | resultParams = kmParamSet2Hidl(out_params); |
| 482 | resultBlob = kmBlob2hidlVec(out_blob); |
| 483 | } |
| 484 | |
| 485 | _hidl_cb(legacy_enum_conversion(rc), resultConsumed, resultParams, resultBlob); |
| 486 | |
| 487 | keymaster_free_param_set(&out_params); |
| 488 | if (out_blob.data) free(const_cast<uint8_t*>(out_blob.data)); |
| 489 | |
| 490 | return Void(); |
| 491 | } |
| 492 | |
| 493 | Return<void> LegacyKeymasterDeviceWrapper::finish(uint64_t operationHandle, |
| 494 | const hidl_vec<KeyParameter>& inParams, |
| 495 | const hidl_vec<uint8_t>& input, |
| 496 | const hidl_vec<uint8_t>& signature, |
| 497 | finish_cb _hidl_cb) { |
| 498 | // result variables for the wire |
| 499 | hidl_vec<KeyParameter> resultParams; |
| 500 | hidl_vec<uint8_t> resultBlob; |
| 501 | |
| 502 | // result variables the backend understands |
| 503 | keymaster_key_param_set_t out_params = {}; |
| 504 | keymaster_blob_t out_blob = {}; |
| 505 | |
| 506 | auto kmInParams = hidlParams2KmParamSet(inParams); |
| 507 | auto kmInput = hidlVec2KmBlob(input); |
| 508 | auto kmSignature = hidlVec2KmBlob(signature); |
| 509 | |
| 510 | auto rc = keymaster_device_->finish(keymaster_device_, operationHandle, &kmInParams, &kmInput, |
| 511 | &kmSignature, &out_params, &out_blob); |
| 512 | |
| 513 | if (rc == KM_ERROR_OK) { |
| 514 | resultParams = kmParamSet2Hidl(out_params); |
| 515 | resultBlob = kmBlob2hidlVec(out_blob); |
| 516 | } |
| 517 | |
| 518 | _hidl_cb(legacy_enum_conversion(rc), resultParams, resultBlob); |
| 519 | |
| 520 | keymaster_free_param_set(&out_params); |
| 521 | if (out_blob.data) free(const_cast<uint8_t*>(out_blob.data)); |
| 522 | |
| 523 | return Void(); |
| 524 | } |
| 525 | |
| 526 | Return<ErrorCode> LegacyKeymasterDeviceWrapper::abort(uint64_t operationHandle) { |
| 527 | return legacy_enum_conversion(keymaster_device_->abort(keymaster_device_, operationHandle)); |
| 528 | } |
| 529 | |
| 530 | sp<IKeymasterDevice> makeSoftwareKeymasterDevice() { |
| 531 | keymaster2_device_t* dev = nullptr; |
| 532 | dev = (new SoftKeymasterDevice)->keymaster2_device(); |
| 533 | |
| 534 | auto kmrc = ::keymaster::ConfigureDevice(dev); |
| 535 | if (kmrc != KM_ERROR_OK) { |
| 536 | dev->common.close(&dev->common); |
| 537 | return nullptr; |
| 538 | } |
| 539 | |
| 540 | return new LegacyKeymasterDeviceWrapper(dev); |
| 541 | } |
| 542 | |
| 543 | } // namespace keystore |
| 544 | } // namespace android |