keymaster2_passthrough_context.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. **
  3. ** Copyright 2017, 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. #include <keymaster/contexts/keymaster2_passthrough_context.h>
  18. #include <keymaster/legacy_support/keymaster_passthrough_key.h>
  19. #include <keymaster/legacy_support/keymaster_passthrough_engine.h>
  20. namespace keymaster {
  21. Keymaster2PassthroughContext::Keymaster2PassthroughContext(keymaster2_device_t* dev)
  22. : device_(dev), engine_(KeymasterPassthroughEngine::createInstance(dev)) {
  23. }
  24. keymaster_error_t Keymaster2PassthroughContext::SetSystemVersion(uint32_t os_version,
  25. uint32_t os_patchlevel) {
  26. os_version_ = os_version;
  27. os_patchlevel_ = os_patchlevel;
  28. return KM_ERROR_OK;
  29. }
  30. void Keymaster2PassthroughContext::GetSystemVersion(uint32_t* os_version,
  31. uint32_t* os_patchlevel) const {
  32. if (os_version) *os_version = os_version_;
  33. if (os_patchlevel) *os_patchlevel = os_patchlevel_;
  34. }
  35. KeyFactory* Keymaster2PassthroughContext::GetKeyFactory(keymaster_algorithm_t algorithm) const {
  36. auto& result = factories_[algorithm];
  37. if (!result) {
  38. result.reset(new KeymasterPassthroughKeyFactory(engine_.get(), algorithm));
  39. }
  40. return result.get();
  41. }
  42. OperationFactory* Keymaster2PassthroughContext::GetOperationFactory(keymaster_algorithm_t algorithm,
  43. keymaster_purpose_t purpose) const {
  44. auto keyfactory = GetKeyFactory(algorithm);
  45. return keyfactory->GetOperationFactory(purpose);
  46. }
  47. keymaster_algorithm_t* Keymaster2PassthroughContext::GetSupportedAlgorithms(
  48. size_t* algorithms_count) const {
  49. if (algorithms_count) *algorithms_count = 0;
  50. return nullptr;
  51. }
  52. keymaster_error_t Keymaster2PassthroughContext::UpgradeKeyBlob(
  53. const KeymasterKeyBlob& key_to_upgrade, const AuthorizationSet& upgrade_params,
  54. KeymasterKeyBlob* upgraded_key) const {
  55. if (!upgraded_key) return KM_ERROR_UNEXPECTED_NULL_POINTER;
  56. *upgraded_key = {};
  57. return device_->upgrade_key(device_, &key_to_upgrade, &upgrade_params, upgraded_key);
  58. }
  59. keymaster_error_t Keymaster2PassthroughContext::ParseKeyBlob(const KeymasterKeyBlob& blob,
  60. const AuthorizationSet& additional_params, UniquePtr<Key>* key) const {
  61. keymaster_key_characteristics_t characteristics = {};
  62. keymaster_blob_t clientId;
  63. keymaster_blob_t applicationData;
  64. keymaster_blob_t* clientIdPtr = &clientId;
  65. keymaster_blob_t* applicationDataPtr = &applicationData;
  66. if (!additional_params.GetTagValue(TAG_APPLICATION_ID, clientIdPtr)) {
  67. clientIdPtr = nullptr;
  68. }
  69. if (!additional_params.GetTagValue(TAG_APPLICATION_DATA, applicationDataPtr)) {
  70. applicationDataPtr = nullptr;
  71. }
  72. auto rc = device_->get_key_characteristics(device_, &blob, clientIdPtr, applicationDataPtr,
  73. &characteristics);
  74. if (rc != KM_ERROR_OK) return rc;
  75. AuthorizationSet hw_enforced;
  76. AuthorizationSet sw_enforced;
  77. hw_enforced.Reinitialize(characteristics.hw_enforced);
  78. sw_enforced.Reinitialize(characteristics.sw_enforced);
  79. keymaster_free_characteristics(&characteristics);
  80. // GetKeyFactory
  81. keymaster_algorithm_t algorithm;
  82. if (!hw_enforced.GetTagValue(TAG_ALGORITHM, &algorithm) &&
  83. !sw_enforced.GetTagValue(TAG_ALGORITHM, &algorithm)) {
  84. return KM_ERROR_INVALID_ARGUMENT;
  85. }
  86. KeymasterKeyBlob key_material = blob;
  87. auto factory = GetKeyFactory(algorithm);
  88. return factory->LoadKey(move(key_material), additional_params, move(hw_enforced),
  89. move(sw_enforced), key);
  90. }
  91. keymaster_error_t Keymaster2PassthroughContext::DeleteKey(const KeymasterKeyBlob& blob) const {
  92. return device_->delete_key(device_, &blob);
  93. }
  94. keymaster_error_t Keymaster2PassthroughContext::DeleteAllKeys() const {
  95. return device_->delete_all_keys(device_);
  96. }
  97. keymaster_error_t Keymaster2PassthroughContext::AddRngEntropy(const uint8_t* buf,
  98. size_t length) const {
  99. return device_->add_rng_entropy(device_, buf, length);
  100. }
  101. KeymasterEnforcement* Keymaster2PassthroughContext::enforcement_policy() {
  102. return nullptr;
  103. }
  104. keymaster_error_t Keymaster2PassthroughContext::GenerateAttestation(const Key& key,
  105. const AuthorizationSet& attest_params, CertChainPtr* cert_chain) const {
  106. if (!cert_chain) return KM_ERROR_UNEXPECTED_NULL_POINTER;
  107. keymaster_cert_chain_t cchain{};
  108. auto rc = device_->attest_key(device_, &key.key_material(), &attest_params, &cchain);
  109. if (rc == KM_ERROR_OK) {
  110. cert_chain->reset(new keymaster_cert_chain_t);
  111. **cert_chain = { new keymaster_blob_t[cchain.entry_count], cchain.entry_count };
  112. for (size_t i = 0; i < cchain.entry_count; ++i) {
  113. (*cert_chain)->entries[i] = { dup_array(cchain.entries[i].data,
  114. cchain.entries[i].data_length),
  115. cchain.entries[i].data_length };
  116. free(const_cast<uint8_t*>(cchain.entries[i].data));
  117. }
  118. free(cchain.entries);
  119. }
  120. return rc;
  121. }
  122. keymaster_error_t Keymaster2PassthroughContext::UnwrapKey(
  123. const KeymasterKeyBlob&, const KeymasterKeyBlob&, const AuthorizationSet&,
  124. const KeymasterKeyBlob&, AuthorizationSet*, keymaster_key_format_t*, KeymasterKeyBlob*) const {
  125. return KM_ERROR_UNIMPLEMENTED;
  126. }
  127. } // namespace keymaster