keymaster_passthrough_key.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/legacy_support/keymaster_passthrough_key.h>
  18. namespace keymaster {
  19. keymaster_error_t
  20. KeymasterPassthroughKeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
  21. const AuthorizationSet& additional_params,
  22. AuthorizationSet&& hw_enforced,
  23. AuthorizationSet&& sw_enforced,
  24. UniquePtr<Key>* key) const {
  25. keymaster_error_t error = KM_ERROR_OK;
  26. if (!key)
  27. return KM_ERROR_OUTPUT_PARAMETER_NULL;
  28. key->reset(new (std::nothrow) KeymasterPassthroughKey(move(key_material), move(hw_enforced),
  29. move(sw_enforced), this, &error,
  30. additional_params, engine_));
  31. if (!key->get())
  32. error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
  33. return error;
  34. }
  35. const keymaster_key_format_t*
  36. KeymasterPassthroughKeyFactory::SupportedImportFormats(size_t* format_count) const {
  37. if (format_count) *format_count = 0;
  38. return nullptr;
  39. }
  40. const keymaster_key_format_t*
  41. KeymasterPassthroughKeyFactory::SupportedExportFormats(size_t* format_count) const {
  42. if (format_count) *format_count = 0;
  43. return nullptr;
  44. }
  45. keymaster_error_t
  46. KeymasterPassthroughKey::formatted_key_material(keymaster_key_format_t format,
  47. UniquePtr<uint8_t[]>* material,
  48. size_t* size) const {
  49. if (!material || !size) {
  50. return KM_ERROR_OUTPUT_PARAMETER_NULL;
  51. }
  52. keymaster_blob_t km_app_data = {};
  53. KeymasterBlob app_data;
  54. if (additional_parameters_.GetTagValue(TAG_APPLICATION_DATA, &km_app_data)) {
  55. app_data = KeymasterBlob(km_app_data);
  56. }
  57. keymaster_blob_t km_client_id = {};
  58. KeymasterBlob client_id;
  59. if (additional_parameters_.GetTagValue(TAG_APPLICATION_ID, &km_client_id)) {
  60. client_id = KeymasterBlob(km_client_id);
  61. }
  62. KeymasterBlob export_data;
  63. keymaster_error_t error = engine_->ExportKey(format, key_material(), client_id, app_data,
  64. &export_data);
  65. if (error == KM_ERROR_OK) {
  66. keymaster_blob_t export_blob = export_data.release();
  67. material->reset(const_cast<uint8_t*>(export_blob.data));
  68. *size = export_blob.data_length;
  69. }
  70. return error;
  71. }
  72. } // namespace keymaster