attestation_record_test.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2016 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 <fstream>
  17. #include <gtest/gtest.h>
  18. #include <keymaster/keymaster_context.h>
  19. #include "android_keymaster_test_utils.h"
  20. #include <keymaster/attestation_record.h>
  21. namespace keymaster {
  22. namespace test {
  23. class TestContext : public AttestationRecordContext {
  24. public:
  25. keymaster_security_level_t GetSecurityLevel() const override {
  26. return KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT;
  27. }
  28. keymaster_error_t GenerateUniqueId(uint64_t /* creation_date_time */,
  29. const keymaster_blob_t& application_id,
  30. bool /* reset_since_rotation */,
  31. Buffer* unique_id) const override {
  32. // Use the application ID directly as the unique ID.
  33. unique_id->Reinitialize(application_id.data, application_id.data_length);
  34. return KM_ERROR_OK;
  35. }
  36. keymaster_error_t GetVerifiedBootParams(keymaster_blob_t* verified_boot_key,
  37. keymaster_verified_boot_t* verified_boot_state,
  38. bool* device_locked) const override {
  39. verified_boot_key->data = vboot_key_;
  40. verified_boot_key->data_length = sizeof(vboot_key_);
  41. *verified_boot_state = KM_VERIFIED_BOOT_VERIFIED;
  42. *device_locked = true;
  43. return KM_ERROR_OK;
  44. }
  45. void VerifyRootOfTrust(const keymaster_blob_t& verified_boot_key,
  46. keymaster_verified_boot_t verified_boot_state, bool device_locked) {
  47. EXPECT_EQ(sizeof(vboot_key_), verified_boot_key.data_length);
  48. if (sizeof(vboot_key_) == verified_boot_key.data_length) {
  49. EXPECT_EQ(0, memcmp(verified_boot_key.data, vboot_key_, sizeof(vboot_key_)));
  50. }
  51. EXPECT_TRUE(device_locked);
  52. EXPECT_EQ(KM_VERIFIED_BOOT_VERIFIED, verified_boot_state);
  53. }
  54. private:
  55. uint8_t vboot_key_[32]{"test_vboot_key"};
  56. };
  57. TEST(AttestTest, Simple) {
  58. TestContext context;
  59. AuthorizationSet hw_set(AuthorizationSetBuilder()
  60. .RsaSigningKey(512, 3)
  61. .Digest(KM_DIGEST_SHA_2_256)
  62. .Digest(KM_DIGEST_SHA_2_384)
  63. .Authorization(TAG_OS_VERSION, 60000)
  64. .Authorization(TAG_OS_PATCHLEVEL, 201512)
  65. .Authorization(TAG_INCLUDE_UNIQUE_ID));
  66. AuthorizationSet sw_set(AuthorizationSetBuilder()
  67. .Authorization(TAG_ACTIVE_DATETIME, 10)
  68. .Authorization(TAG_CREATION_DATETIME, 10)
  69. .Authorization(TAG_APPLICATION_ID, "fake_app_id", 11));
  70. UniquePtr<uint8_t[]> asn1;
  71. size_t asn1_len = 0;
  72. AuthorizationSet attest_params(
  73. AuthorizationSetBuilder()
  74. .Authorization(TAG_ATTESTATION_CHALLENGE, "fake_challenge", 14)
  75. .Authorization(TAG_ATTESTATION_APPLICATION_ID, "fake_attest_app_id", 18));
  76. ASSERT_EQ(KM_ERROR_OK,
  77. build_attestation_record(attest_params, sw_set, hw_set, context, &asn1, &asn1_len));
  78. EXPECT_GT(asn1_len, 0U);
  79. std::ofstream output("attest.der",
  80. std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
  81. if (output)
  82. output.write(reinterpret_cast<const char*>(asn1.get()), asn1_len);
  83. output.close();
  84. AuthorizationSet parsed_hw_set;
  85. AuthorizationSet parsed_sw_set;
  86. uint32_t attestation_version;
  87. uint32_t keymaster_version;
  88. keymaster_security_level_t attestation_security_level;
  89. keymaster_security_level_t keymaster_security_level;
  90. keymaster_blob_t attestation_challenge = {};
  91. keymaster_blob_t unique_id = {};
  92. EXPECT_EQ(KM_ERROR_OK,
  93. parse_attestation_record(asn1.get(), asn1_len, &attestation_version,
  94. &attestation_security_level, &keymaster_version,
  95. &keymaster_security_level, &attestation_challenge,
  96. &parsed_sw_set, &parsed_hw_set, &unique_id));
  97. // Check that the challenge is consistent across build and parse.
  98. EXPECT_EQ("fake_challenge",
  99. std::string(reinterpret_cast<const char*>(attestation_challenge.data), 14));
  100. delete[] attestation_challenge.data;
  101. // Check that the unique id was populated as expected.
  102. EXPECT_EQ("fake_app_id", std::string(reinterpret_cast<const char*>(unique_id.data), 11));
  103. delete[] unique_id.data;
  104. // The attestation ID is expected to appear in parsed_sw_set.
  105. sw_set.push_back(TAG_ATTESTATION_APPLICATION_ID, "fake_attest_app_id", 18);
  106. // The TAG_INCLUDE_UNIQUE_ID tag is not expected to appear in parsed_hw_set.
  107. hw_set.erase(hw_set.find(TAG_INCLUDE_UNIQUE_ID));
  108. // Check that the list of tags is consistent across build and parse.
  109. hw_set.Sort();
  110. sw_set.Sort();
  111. parsed_hw_set.Sort();
  112. parsed_sw_set.Sort();
  113. EXPECT_EQ(hw_set, parsed_hw_set);
  114. EXPECT_EQ(sw_set, parsed_sw_set);
  115. // Check the root of trust values.
  116. keymaster_blob_t verified_boot_key;
  117. keymaster_verified_boot_t verified_boot_state;
  118. bool device_locked;
  119. EXPECT_EQ(KM_ERROR_OK, parse_root_of_trust(asn1.get(), asn1_len, &verified_boot_key,
  120. &verified_boot_state, &device_locked));
  121. context.VerifyRootOfTrust(verified_boot_key, verified_boot_state, device_locked);
  122. delete[] verified_boot_key.data;
  123. }
  124. } // namespace test
  125. } // namespace keymaster