keymaster1_passthrough_context.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/keymaster1_passthrough_context.h>
  18. #include <keymaster/legacy_support/keymaster_passthrough_key.h>
  19. #include <keymaster/legacy_support/keymaster_passthrough_engine.h>
  20. #include <keymaster/legacy_support/keymaster1_legacy_support.h>
  21. #include <keymaster/legacy_support/keymaster1_engine.h>
  22. #include <keymaster/legacy_support/rsa_keymaster1_key.h>
  23. #include <keymaster/legacy_support/ec_keymaster1_key.h>
  24. #include <keymaster/key_blob_utils/software_keyblobs.h>
  25. #include <keymaster/key_blob_utils/integrity_assured_key_blob.h>
  26. #include <keymaster/key_blob_utils/ocb_utils.h>
  27. #include <keymaster/km_openssl/aes_key.h>
  28. #include <keymaster/km_openssl/hmac_key.h>
  29. #include <keymaster/km_openssl/attestation_utils.h>
  30. #include "soft_attestation_cert.h"
  31. namespace keymaster {
  32. Keymaster1PassthroughContext::Keymaster1PassthroughContext(keymaster1_device_t* dev)
  33. : device_(dev), pt_engine_(KeymasterPassthroughEngine::createInstance(dev)),
  34. km1_engine_(new Keymaster1Engine(dev)) {
  35. }
  36. keymaster_error_t Keymaster1PassthroughContext::SetSystemVersion(uint32_t os_version,
  37. uint32_t os_patchlevel) {
  38. os_version_ = os_version;
  39. os_patchlevel_ = os_patchlevel;
  40. return KM_ERROR_OK;
  41. }
  42. void Keymaster1PassthroughContext::GetSystemVersion(uint32_t* os_version,
  43. uint32_t* os_patchlevel) const {
  44. if (os_version) *os_version = os_version_;
  45. if (os_patchlevel) *os_patchlevel = os_patchlevel_;
  46. }
  47. KeyFactory* Keymaster1PassthroughContext::GetKeyFactory(keymaster_algorithm_t algorithm) const {
  48. auto& result = factories_[algorithm];
  49. if (!result) {
  50. switch(algorithm) {
  51. case KM_ALGORITHM_RSA:
  52. result.reset(new Keymaster1ArbitrationFactory<RsaKeymaster1KeyFactory>(pt_engine_.get(),
  53. KM_ALGORITHM_RSA, device_, this, km1_engine_.get()));
  54. break;
  55. case KM_ALGORITHM_EC:
  56. result.reset(new Keymaster1ArbitrationFactory<EcdsaKeymaster1KeyFactory>(pt_engine_.get(),
  57. KM_ALGORITHM_EC, device_, this, km1_engine_.get()));
  58. break;
  59. case KM_ALGORITHM_AES:
  60. result.reset(new Keymaster1ArbitrationFactory<AesKeyFactory>(pt_engine_.get(),
  61. KM_ALGORITHM_AES, device_, this, this));
  62. break;
  63. case KM_ALGORITHM_HMAC:
  64. result.reset(new Keymaster1ArbitrationFactory<HmacKeyFactory>(pt_engine_.get(),
  65. KM_ALGORITHM_HMAC, device_, this, this));
  66. break;
  67. case KM_ALGORITHM_TRIPLE_DES:
  68. // Not supported by KM1.
  69. return nullptr;
  70. }
  71. }
  72. return result.get();
  73. }
  74. OperationFactory* Keymaster1PassthroughContext::GetOperationFactory(keymaster_algorithm_t algorithm,
  75. keymaster_purpose_t purpose) const {
  76. auto keyfactory = GetKeyFactory(algorithm);
  77. return keyfactory->GetOperationFactory(purpose);
  78. }
  79. keymaster_algorithm_t* Keymaster1PassthroughContext::GetSupportedAlgorithms(
  80. size_t* algorithms_count) const {
  81. if (algorithms_count) *algorithms_count = 0;
  82. return nullptr;
  83. }
  84. keymaster_error_t Keymaster1PassthroughContext::UpgradeKeyBlob(
  85. const KeymasterKeyBlob& key_to_upgrade, const AuthorizationSet& upgrade_params,
  86. KeymasterKeyBlob* upgraded_key) const {
  87. UniquePtr<Key> key;
  88. keymaster_error_t error = ParseKeyBlob(key_to_upgrade, upgrade_params, &key);
  89. if (error != KM_ERROR_OK)
  90. return error;
  91. if (key->hw_enforced().Contains(TAG_PURPOSE) &&
  92. !key->hw_enforced().Contains(TAG_OS_PATCHLEVEL)) {
  93. return KM_ERROR_INVALID_ARGUMENT;
  94. }
  95. return UpgradeSoftKeyBlob(key, os_version_, os_patchlevel_, upgrade_params, upgraded_key);
  96. }
  97. static keymaster_error_t parseKeymaster1HwBlob(const keymaster1_device_t* device,
  98. const KeymasterKeyBlob& blob,
  99. const AuthorizationSet& additional_params,
  100. KeymasterKeyBlob* key_material,
  101. AuthorizationSet* hw_enforced,
  102. AuthorizationSet* sw_enforced) {
  103. keymaster_blob_t client_id = {nullptr, 0};
  104. keymaster_blob_t app_data = {nullptr, 0};
  105. keymaster_blob_t* client_id_ptr = nullptr;
  106. keymaster_blob_t* app_data_ptr = nullptr;
  107. if (additional_params.GetTagValue(TAG_APPLICATION_ID, &client_id))
  108. client_id_ptr = &client_id;
  109. if (additional_params.GetTagValue(TAG_APPLICATION_DATA, &app_data))
  110. app_data_ptr = &app_data;
  111. // Get key characteristics, which incidentally verifies that the HW recognizes the key.
  112. keymaster_key_characteristics_t* characteristics;
  113. keymaster_error_t error = device->get_key_characteristics(device, &blob, client_id_ptr,
  114. app_data_ptr, &characteristics);
  115. if (error != KM_ERROR_OK)
  116. return error;
  117. UniquePtr<keymaster_key_characteristics_t, Characteristics_Delete> characteristics_deleter(
  118. characteristics);
  119. hw_enforced->Reinitialize(characteristics->hw_enforced);
  120. sw_enforced->Reinitialize(characteristics->sw_enforced);
  121. *key_material = blob;
  122. return KM_ERROR_OK;
  123. }
  124. keymaster_error_t Keymaster1PassthroughContext::ParseKeyBlob(const KeymasterKeyBlob& blob,
  125. const AuthorizationSet& additional_params, UniquePtr<Key>* key) const {
  126. AuthorizationSet hw_enforced;
  127. AuthorizationSet sw_enforced;
  128. KeymasterKeyBlob key_material;
  129. AuthorizationSet hidden;
  130. keymaster_error_t error = BuildHiddenAuthorizations(additional_params, &hidden,
  131. softwareRootOfTrust);
  132. if (error != KM_ERROR_OK)
  133. return error;
  134. // Assume it's an integrity-assured blob (new software-only blob
  135. error = DeserializeIntegrityAssuredBlob(blob, hidden, &key_material, &hw_enforced, &sw_enforced);
  136. if (error != KM_ERROR_INVALID_KEY_BLOB && error != KM_ERROR_OK)
  137. return error;
  138. if (error == KM_ERROR_INVALID_KEY_BLOB) {
  139. error = parseKeymaster1HwBlob(km1_engine_->device(), blob, additional_params,
  140. &key_material, &hw_enforced, &sw_enforced);
  141. if (error != KM_ERROR_OK) return error;
  142. }
  143. // GetKeyFactory
  144. keymaster_algorithm_t algorithm;
  145. if (!hw_enforced.GetTagValue(TAG_ALGORITHM, &algorithm) &&
  146. !sw_enforced.GetTagValue(TAG_ALGORITHM, &algorithm)) {
  147. return KM_ERROR_INVALID_ARGUMENT;
  148. }
  149. auto factory = GetKeyFactory(algorithm);
  150. return factory->LoadKey(move(key_material), additional_params, move(hw_enforced),
  151. move(sw_enforced), key);
  152. }
  153. keymaster_error_t Keymaster1PassthroughContext::DeleteKey(const KeymasterKeyBlob& blob) const {
  154. // HACK. Due to a bug with Qualcomm's Keymaster implementation, which causes the device to
  155. // reboot if we pass it a key blob it doesn't understand, we need to check for software
  156. // keys. If it looks like a software key there's nothing to do so we just return.
  157. // Can be removed once b/33385206 is fixed
  158. KeymasterKeyBlob key_material;
  159. AuthorizationSet hw_enforced, sw_enforced;
  160. keymaster_error_t error = DeserializeIntegrityAssuredBlob_NoHmacCheck(
  161. blob, &key_material, &hw_enforced, &sw_enforced);
  162. if (error == KM_ERROR_OK) {
  163. return KM_ERROR_OK;
  164. }
  165. error = km1_engine_->DeleteKey(blob);
  166. if (error == KM_ERROR_INVALID_KEY_BLOB) {
  167. // Some implementations diagnose invalid keys.
  168. // However, all care we about is that the key blob, as supplied, is not usable after the
  169. // call.
  170. return KM_ERROR_OK;
  171. }
  172. return error;
  173. }
  174. keymaster_error_t Keymaster1PassthroughContext::DeleteAllKeys() const {
  175. return km1_engine_->DeleteAllKeys();
  176. }
  177. keymaster_error_t Keymaster1PassthroughContext::AddRngEntropy(const uint8_t* buf,
  178. size_t length) const {
  179. return device_->add_rng_entropy(device_, buf, length);
  180. }
  181. KeymasterEnforcement* Keymaster1PassthroughContext::enforcement_policy() {
  182. return nullptr;
  183. }
  184. keymaster_error_t Keymaster1PassthroughContext::CreateKeyBlob(const AuthorizationSet& key_description,
  185. const keymaster_key_origin_t origin,
  186. const KeymasterKeyBlob& key_material,
  187. KeymasterKeyBlob* blob,
  188. AuthorizationSet* hw_enforced,
  189. AuthorizationSet* sw_enforced) const {
  190. keymaster_error_t error = SetKeyBlobAuthorizations(key_description, origin, os_version_,
  191. os_patchlevel_, hw_enforced, sw_enforced);
  192. if (error != KM_ERROR_OK)
  193. return error;
  194. AuthorizationSet hidden;
  195. error = BuildHiddenAuthorizations(key_description, &hidden, softwareRootOfTrust);
  196. if (error != KM_ERROR_OK)
  197. return error;
  198. return SerializeIntegrityAssuredBlob(key_material, hidden, *hw_enforced, *sw_enforced, blob);
  199. }
  200. keymaster_error_t Keymaster1PassthroughContext::GenerateAttestation(const Key& key,
  201. const AuthorizationSet& attest_params, CertChainPtr* cert_chain) const {
  202. keymaster_error_t error = KM_ERROR_OK;
  203. keymaster_algorithm_t key_algorithm;
  204. if (!key.authorizations().GetTagValue(TAG_ALGORITHM, &key_algorithm)) {
  205. return KM_ERROR_UNKNOWN_ERROR;
  206. }
  207. if ((key_algorithm != KM_ALGORITHM_RSA && key_algorithm != KM_ALGORITHM_EC))
  208. return KM_ERROR_INCOMPATIBLE_ALGORITHM;
  209. // We have established that the given key has the correct algorithm, and because this is the
  210. // SoftKeymasterContext we can assume that the Key is an AsymmetricKey. So we can downcast.
  211. const AsymmetricKey& asymmetric_key = static_cast<const AsymmetricKey&>(key);
  212. auto attestation_chain = getAttestationChain(key_algorithm, &error);
  213. if (error != KM_ERROR_OK) return error;
  214. auto attestation_key = getAttestationKey(key_algorithm, &error);
  215. if (error != KM_ERROR_OK) return error;
  216. return generate_attestation(asymmetric_key, attest_params,
  217. *attestation_chain, *attestation_key, *this, cert_chain);
  218. }
  219. keymaster_error_t Keymaster1PassthroughContext::UnwrapKey(
  220. const KeymasterKeyBlob&, const KeymasterKeyBlob&, const AuthorizationSet&,
  221. const KeymasterKeyBlob&, AuthorizationSet*, keymaster_key_format_t*, KeymasterKeyBlob*) const {
  222. return KM_ERROR_UNIMPLEMENTED;
  223. }
  224. } // namespace keymaster