wrapped_key.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2017 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. #include <keymaster/attestation_record.h>
  17. #include <keymaster/logger.h>
  18. #include <keymaster/wrapped_key.h>
  19. #include <openssl/asn1t.h>
  20. #include <keymaster/android_keymaster_utils.h>
  21. #include <keymaster/km_openssl/openssl_err.h>
  22. #include <keymaster/km_openssl/openssl_utils.h>
  23. namespace keymaster {
  24. IMPLEMENT_ASN1_FUNCTIONS(KM_WRAPPED_KEY_DESCRIPTION);
  25. IMPLEMENT_ASN1_FUNCTIONS(KM_WRAPPED_KEY);
  26. struct KM_WRAPPED_KEY_Delete {
  27. void operator()(KM_WRAPPED_KEY* p) { KM_WRAPPED_KEY_free(p); }
  28. };
  29. struct KM_WRAPPED_KEY_DESCRIPTION_Delete {
  30. void operator()(KM_WRAPPED_KEY_DESCRIPTION* p) { KM_WRAPPED_KEY_DESCRIPTION_free(p); }
  31. };
  32. // DER encode a wrapped key for secure import
  33. keymaster_error_t build_wrapped_key(const KeymasterKeyBlob& transit_key, const KeymasterBlob& iv,
  34. keymaster_key_format_t key_format,
  35. const KeymasterKeyBlob& secure_key, const KeymasterBlob& tag,
  36. const AuthorizationSet& auth_set,
  37. KeymasterKeyBlob* der_wrapped_key) {
  38. UniquePtr<KM_WRAPPED_KEY, KM_WRAPPED_KEY_Delete> wrapped_key(KM_WRAPPED_KEY_new());
  39. if (!wrapped_key.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
  40. if (!ASN1_OCTET_STRING_set(wrapped_key->transit_key, transit_key.key_material,
  41. transit_key.key_material_size) ||
  42. !ASN1_OCTET_STRING_set(wrapped_key->iv, iv.data, iv.data_length) ||
  43. !ASN1_OCTET_STRING_set(wrapped_key->secure_key, secure_key.key_material,
  44. secure_key.key_material_size) ||
  45. !ASN1_OCTET_STRING_set(wrapped_key->tag, tag.data, tag.data_length) ||
  46. !ASN1_INTEGER_set(wrapped_key->wrapped_key_description->key_format, key_format)) {
  47. return TranslateLastOpenSslError();
  48. }
  49. auto err = build_auth_list(auth_set, wrapped_key->wrapped_key_description->auth_list);
  50. if (err != KM_ERROR_OK) {
  51. return err;
  52. }
  53. int len = i2d_KM_WRAPPED_KEY(wrapped_key.get(), nullptr);
  54. if (len < 0) {
  55. return TranslateLastOpenSslError();
  56. }
  57. if (!der_wrapped_key->Reset(len)) {
  58. return KM_ERROR_MEMORY_ALLOCATION_FAILED;
  59. }
  60. uint8_t* p = der_wrapped_key->writable_data();
  61. len = i2d_KM_WRAPPED_KEY(wrapped_key.get(), &p);
  62. if (len < 0) {
  63. return TranslateLastOpenSslError();
  64. }
  65. return KM_ERROR_OK;
  66. }
  67. // Parse the DER-encoded wrapped key format
  68. keymaster_error_t parse_wrapped_key(const KeymasterKeyBlob& wrapped_key, KeymasterBlob* iv,
  69. KeymasterKeyBlob* transit_key, KeymasterKeyBlob* secure_key,
  70. KeymasterBlob* tag, AuthorizationSet* auth_list,
  71. keymaster_key_format_t* key_format,
  72. KeymasterBlob* wrapped_key_description) {
  73. if (!iv || !transit_key || !secure_key || !tag || !auth_list || !key_format ||
  74. !wrapped_key_description) {
  75. return KM_ERROR_UNEXPECTED_NULL_POINTER;
  76. }
  77. const uint8_t* tmp = wrapped_key.key_material;
  78. UniquePtr<KM_WRAPPED_KEY, KM_WRAPPED_KEY_Delete> record(
  79. d2i_KM_WRAPPED_KEY(nullptr, &tmp, wrapped_key.key_material_size));
  80. if (!record.get()) return TranslateLastOpenSslError();
  81. *iv = KeymasterBlob(record->iv->data, record->iv->length);
  82. if (record->iv->data && !iv->data) {
  83. return KM_ERROR_MEMORY_ALLOCATION_FAILED;
  84. }
  85. *transit_key = KeymasterKeyBlob(record->transit_key->data, record->transit_key->length);
  86. if (record->transit_key->data && !transit_key->key_material) {
  87. return KM_ERROR_MEMORY_ALLOCATION_FAILED;
  88. }
  89. *secure_key = KeymasterKeyBlob(record->secure_key->data, record->secure_key->length);
  90. if (record->secure_key->data && !secure_key->key_material) {
  91. return KM_ERROR_MEMORY_ALLOCATION_FAILED;
  92. }
  93. *tag = KeymasterBlob(record->tag->data, record->tag->length);
  94. if (record->tag->data && !tag->data) {
  95. return KM_ERROR_MEMORY_ALLOCATION_FAILED;
  96. }
  97. // Re-serialize the wrapped key description
  98. int len = i2d_KM_WRAPPED_KEY_DESCRIPTION(record->wrapped_key_description, nullptr);
  99. if (len < 0) {
  100. return TranslateLastOpenSslError();
  101. }
  102. if (!wrapped_key_description->Reset(len)) {
  103. return KM_ERROR_MEMORY_ALLOCATION_FAILED;
  104. }
  105. uint8_t* p = wrapped_key_description->writable_data();
  106. if (i2d_KM_WRAPPED_KEY_DESCRIPTION(record->wrapped_key_description, &p) < 0) {
  107. return TranslateLastOpenSslError();
  108. }
  109. *key_format = static_cast<keymaster_key_format_t>(
  110. ASN1_INTEGER_get(record->wrapped_key_description->key_format));
  111. return extract_auth_list(record->wrapped_key_description->auth_list, auth_list);
  112. }
  113. } // namespace keymaster