• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <fstream>
18 
19 #include <cppbor_parse.h>
20 #include <gtest/gtest.h>
21 
22 #include <keymaster/contexts/soft_attestation_context.h>
23 #include <keymaster/keymaster_context.h>
24 #include <keymaster/km_openssl/attestation_record.h>
25 
26 #include "android_keymaster_test_utils.h"
27 
28 // Use TAG_KDF as an 'unknown tag', as it is not deliberately thrown out
29 // in attestation_record.cpp, but still among the keymaster tag types.
30 #define UNKNOWN_TAG static_cast<keymaster_tag_t>(KM_ULONG_REP | 50)
31 #define UNKNOWN_TAG_VALUE 0
32 
33 namespace keymaster {
34 namespace test {
35 
36 TypedTag<KM_ULONG_REP, UNKNOWN_TAG> UNKNOWN_TAG_T;
37 
38 class TestContext : public SoftAttestationContext {
39   public:
TestContext(KmVersion version)40     TestContext(KmVersion version) : SoftAttestationContext(version) {}
41 
GetSecurityLevel() const42     keymaster_security_level_t GetSecurityLevel() const override {
43         return KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT;
44     }
GenerateUniqueId(uint64_t,const keymaster_blob_t & application_id,bool,keymaster_error_t * error) const45     Buffer GenerateUniqueId(uint64_t /* creation_date_time */,
46                             const keymaster_blob_t& application_id, bool /* reset_since_rotation */,
47                             keymaster_error_t* error) const override {
48         // Use the application ID directly as the unique ID.
49         *error = KM_ERROR_OK;
50         return {application_id.data, application_id.data_length};
51     }
GetVerifiedBootParams(keymaster_error_t * error) const52     const VerifiedBootParams* GetVerifiedBootParams(keymaster_error_t* error) const override {
53         static VerifiedBootParams params{};
54         params.verified_boot_key = {vboot_key_, sizeof(vboot_key_)};
55         params.verified_boot_state = KM_VERIFIED_BOOT_VERIFIED;
56         params.device_locked = true;
57         *error = KM_ERROR_OK;
58         return &params;
59     }
60 
VerifyRootOfTrust(const keymaster_blob_t & verified_boot_key,keymaster_verified_boot_t verified_boot_state,bool device_locked)61     void VerifyRootOfTrust(const keymaster_blob_t& verified_boot_key,
62                            keymaster_verified_boot_t verified_boot_state, bool device_locked) {
63         EXPECT_EQ(sizeof(vboot_key_), verified_boot_key.data_length);
64         if (sizeof(vboot_key_) == verified_boot_key.data_length) {
65             EXPECT_EQ(0, memcmp(verified_boot_key.data, vboot_key_, sizeof(vboot_key_)));
66         }
67         EXPECT_TRUE(device_locked);
68         EXPECT_EQ(KM_VERIFIED_BOOT_VERIFIED, verified_boot_state);
69     }
70 
71   private:
72     uint8_t vboot_key_[32]{"test_vboot_key"};
73 };
74 
75 class KeymintTestContext : public TestContext {
76   public:
KeymintTestContext()77     KeymintTestContext() : TestContext(KmVersion::KEYMINT_1) {}
78 };
79 
80 class KeymasterTestContext : public TestContext {
81   public:
KeymasterTestContext()82     KeymasterTestContext() : TestContext(KmVersion::KEYMASTER_4_1) {}  // Last Keymaster version
83 };
84 
TEST(AttestAsn1Test,Simple)85 TEST(AttestAsn1Test, Simple) {
86     KeymasterTestContext context;
87     AuthorizationSet hw_set(AuthorizationSetBuilder()
88                                 .RsaSigningKey(512, 3)
89                                 .Digest(KM_DIGEST_SHA_2_256)
90                                 .Digest(KM_DIGEST_SHA_2_384)
91                                 .Authorization(TAG_OS_VERSION, 60000)
92                                 .Authorization(TAG_OS_PATCHLEVEL, 201512)
93                                 .Authorization(TAG_INCLUDE_UNIQUE_ID));
94     AuthorizationSet sw_set(AuthorizationSetBuilder()
95                                 .Authorization(TAG_ACTIVE_DATETIME, 10)
96                                 .Authorization(TAG_CREATION_DATETIME, 10)
97                                 .Authorization(TAG_APPLICATION_ID, "fake_app_id", 19)
98                                 .Authorization(TAG_APPLICATION_DATA, "fake_app_data", 12));
99 
100     UniquePtr<uint8_t[]> asn1;
101     size_t asn1_len = 0;
102     AuthorizationSet attest_params(
103         AuthorizationSetBuilder()
104             .Authorization(TAG_ATTESTATION_CHALLENGE, "fake_challenge", 14)
105             .Authorization(TAG_ATTESTATION_APPLICATION_ID, "fake_attest_app_id", 18));
106     ASSERT_EQ(KM_ERROR_OK,
107               build_attestation_record(attest_params, sw_set, hw_set, context, &asn1, &asn1_len));
108     EXPECT_GT(asn1_len, 0U);
109 
110     std::ofstream output("attest.der",
111                          std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
112     if (output) output.write(reinterpret_cast<const char*>(asn1.get()), asn1_len);
113     output.close();
114 
115     AuthorizationSet parsed_hw_set;
116     AuthorizationSet parsed_sw_set;
117     uint32_t attestation_version;
118     uint32_t keymaster_version;
119     keymaster_security_level_t attestation_security_level;
120     keymaster_security_level_t keymaster_security_level;
121     keymaster_blob_t attestation_challenge = {};
122     keymaster_blob_t unique_id = {};
123     EXPECT_EQ(KM_ERROR_OK,
124               parse_attestation_record(asn1.get(), asn1_len, &attestation_version,
125                                        &attestation_security_level, &keymaster_version,
126                                        &keymaster_security_level, &attestation_challenge,
127                                        &parsed_sw_set, &parsed_hw_set, &unique_id));
128 
129     // Check that the challenge is consistent across build and parse.
130     EXPECT_EQ("fake_challenge",
131               std::string(reinterpret_cast<const char*>(attestation_challenge.data), 14));
132     delete[] attestation_challenge.data;
133 
134     // Check that the unique id was populated as expected.
135     EXPECT_EQ("fake_app_id", std::string(reinterpret_cast<const char*>(unique_id.data), 11));
136     delete[] unique_id.data;
137 
138     // The attestation ID is expected to appear in parsed_sw_set.
139     sw_set.push_back(TAG_ATTESTATION_APPLICATION_ID, "fake_attest_app_id", 18);
140 
141     // The TAG_INCLUDE_UNIQUE_ID tag is not expected to appear in parsed_hw_set.
142     hw_set.erase(hw_set.find(TAG_INCLUDE_UNIQUE_ID));
143 
144     // Application data is not expected to appear in parsed_sw_set.
145     sw_set.erase(sw_set.find(TAG_APPLICATION_ID));
146     sw_set.erase(sw_set.find(TAG_APPLICATION_DATA));
147 
148     // Check that the list of tags is consistent across build and parse.
149     hw_set.Sort();
150     sw_set.Sort();
151     parsed_hw_set.Sort();
152     parsed_sw_set.Sort();
153     EXPECT_EQ(hw_set, parsed_hw_set);
154     EXPECT_EQ(sw_set, parsed_sw_set);
155 
156     // Check the root of trust values.
157     keymaster_blob_t verified_boot_key;
158     keymaster_verified_boot_t verified_boot_state;
159     bool device_locked;
160     EXPECT_EQ(KM_ERROR_OK, parse_root_of_trust(asn1.get(), asn1_len, &verified_boot_key,
161                                                &verified_boot_state, &device_locked));
162     context.VerifyRootOfTrust(verified_boot_key, verified_boot_state, device_locked);
163     delete[] verified_boot_key.data;
164 }
165 
TEST(EatTest,Simple)166 TEST(EatTest, Simple) {
167     KeymintTestContext context;
168     AuthorizationSet hw_set(AuthorizationSetBuilder()
169                                 .RsaSigningKey(512, 3)
170                                 .Digest(KM_DIGEST_SHA_2_256)
171                                 .Digest(KM_DIGEST_SHA_2_384)
172                                 .Authorization(TAG_OS_VERSION, 60000)
173                                 .Authorization(TAG_OS_PATCHLEVEL, 201512)
174                                 .Authorization(TAG_INCLUDE_UNIQUE_ID)
175                                 .Authorization(TAG_ATTESTATION_ID_IMEI, "490154203237518", 15));
176     AuthorizationSet sw_set(AuthorizationSetBuilder()
177                                 .Authorization(TAG_ACTIVE_DATETIME, 10)
178                                 .Authorization(TAG_CREATION_DATETIME, 10)
179                                 .Authorization(TAG_APPLICATION_ID, "fake_app_id", 19)
180                                 .Authorization(TAG_APPLICATION_DATA, "fake_app_data", 12));
181 
182     std::vector<uint8_t> eat;
183     AuthorizationSet attest_params(
184         AuthorizationSetBuilder()
185             .Authorization(TAG_ATTESTATION_CHALLENGE, "fake_challenge", 14)
186             .Authorization(TAG_ATTESTATION_APPLICATION_ID, "fake_attest_app_id", 18));
187     ASSERT_EQ(KM_ERROR_OK, build_eat_record(attest_params, sw_set, hw_set, context, &eat));
188     EXPECT_GT(eat.size(), 0U);
189 
190     std::ofstream output("eat.der",
191                          std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
192     if (output) output.write(reinterpret_cast<const char*>(&eat[0]), eat.size() * sizeof(uint8_t));
193     output.close();
194 
195     AuthorizationSet parsed_hw_set;
196     AuthorizationSet parsed_sw_set;
197     uint32_t attestation_version;
198     uint32_t keymaster_version;
199     keymaster_security_level_t attestation_security_level;
200     keymaster_security_level_t keymaster_security_level;
201     keymaster_blob_t attestation_challenge = {};
202     keymaster_blob_t unique_id = {};
203     keymaster_blob_t verified_boot_key = {};
204     keymaster_verified_boot_t verified_boot_state;
205     bool device_locked;
206     std::vector<int64_t> unexpected_claims;
207     EXPECT_EQ(KM_ERROR_OK,
208               parse_eat_record(eat.data(), eat.size(), &attestation_version,
209                                &attestation_security_level, &keymaster_version,
210                                &keymaster_security_level, &attestation_challenge, &parsed_sw_set,
211                                &parsed_hw_set, &unique_id, &verified_boot_key, &verified_boot_state,
212                                &device_locked, &unexpected_claims));
213 
214     // Check that there were no unexpected claims when parsing.
215     EXPECT_EQ(std::vector<int64_t>(), unexpected_claims);
216 
217     // Check that the challenge is consistent across build and parse.
218     EXPECT_EQ("fake_challenge",
219               std::string(reinterpret_cast<const char*>(attestation_challenge.data), 14));
220     delete[] attestation_challenge.data;
221 
222     // Check that the unique id was populated as expected.
223     EXPECT_EQ("fake_app_id", std::string(reinterpret_cast<const char*>(unique_id.data), 11));
224     delete[] unique_id.data;
225 
226     // The attestation ID is expected to appear in parsed_sw_set.
227     sw_set.push_back(TAG_ATTESTATION_APPLICATION_ID, "fake_attest_app_id", 18);
228 
229     // The TAG_INCLUDE_UNIQUE_ID tag is not expected to appear in parsed_hw_set.
230     hw_set.erase(hw_set.find(TAG_INCLUDE_UNIQUE_ID));
231 
232     // Application data is not expected to appear in parsed_sw_set.
233     sw_set.erase(sw_set.find(TAG_APPLICATION_ID));
234     sw_set.erase(sw_set.find(TAG_APPLICATION_DATA));
235 
236     // Check that the list of tags is consistent across build and parse.
237     hw_set.Sort();
238     sw_set.Sort();
239     parsed_hw_set.Sort();
240     parsed_sw_set.Sort();
241     EXPECT_EQ(hw_set, parsed_hw_set);
242     EXPECT_EQ(sw_set, parsed_sw_set);
243 
244     // Check the root of trust values.
245     context.VerifyRootOfTrust(verified_boot_key, verified_boot_state, device_locked);
246     delete[] verified_boot_key.data;
247 }
248 
TEST(BadImeiTest,Simple)249 TEST(BadImeiTest, Simple) {
250     KeymintTestContext context;
251     AuthorizationSet hw_set(
252         AuthorizationSetBuilder().Authorization(TAG_ATTESTATION_ID_IMEI, "1234567890123456", 16));
253     AuthorizationSet attest_params(
254         AuthorizationSetBuilder()
255             .Authorization(TAG_ATTESTATION_CHALLENGE, "fake_challenge", 14)
256             .Authorization(TAG_ATTESTATION_APPLICATION_ID, "fake_attest_app_id", 18));
257     AuthorizationSet sw_set;
258 
259     std::vector<uint8_t> eat;
260     ASSERT_EQ(KM_ERROR_INVALID_TAG, build_eat_record(attest_params, sw_set, hw_set, context, &eat));
261 }
262 
TEST(MissingAuthChallengeTest,Simple)263 TEST(MissingAuthChallengeTest, Simple) {
264     KeymintTestContext context;
265     AuthorizationSet hw_set(AuthorizationSetBuilder().Authorization(TAG_OS_PATCHLEVEL, 201512));
266     AuthorizationSet attest_params(AuthorizationSetBuilder().Authorization(
267         TAG_ATTESTATION_APPLICATION_ID, "fake_attest_app_id", 18));
268     AuthorizationSet sw_set;
269 
270     std::vector<uint8_t> eat;
271     ASSERT_EQ(KM_ERROR_ATTESTATION_CHALLENGE_MISSING,
272               build_eat_record(attest_params, sw_set, hw_set, context, &eat));
273 }
274 
TEST(UnknownTagTest,Simple)275 TEST(UnknownTagTest, Simple) {
276     KeymintTestContext context;
277     AuthorizationSet unknown_tag_set(
278         AuthorizationSetBuilder().Authorization(UNKNOWN_TAG_T, UNKNOWN_TAG_VALUE));
279 
280     // Test adding an unknown tag to both sets. The tag should be retained only in the software
281     // submod.
282     std::vector<uint8_t> eat;
283     AuthorizationSet attest_params(
284         AuthorizationSetBuilder()
285             .Authorization(TAG_ATTESTATION_CHALLENGE, "fake_challenge", 14)
286             .Authorization(TAG_ATTESTATION_APPLICATION_ID, "fake_attest_app_id", 18));
287     ASSERT_EQ(KM_ERROR_OK,
288               build_eat_record(attest_params, unknown_tag_set, unknown_tag_set, context, &eat));
289     EXPECT_GT(eat.size(), 0U);
290 
291     AuthorizationSet parsed_hw_set;
292     AuthorizationSet parsed_sw_set;
293     uint32_t attestation_version;
294     uint32_t keymaster_version;
295     keymaster_security_level_t attestation_security_level;
296     keymaster_security_level_t keymaster_security_level;
297     keymaster_blob_t attestation_challenge = {};
298     keymaster_blob_t unique_id = {};
299     keymaster_blob_t verified_boot_key = {};
300     keymaster_verified_boot_t verified_boot_state;
301     bool device_locked;
302     std::vector<int64_t> unexpected_claims;
303     // Parsing should fail, because the software submod retains the unknown tag.
304     EXPECT_EQ(KM_ERROR_INVALID_TAG,
305               parse_eat_record(eat.data(), eat.size(), &attestation_version,
306                                &attestation_security_level, &keymaster_version,
307                                &keymaster_security_level, &attestation_challenge, &parsed_sw_set,
308                                &parsed_hw_set, &unique_id, &verified_boot_key, &verified_boot_state,
309                                &device_locked, &unexpected_claims));
310 
311     // Perform a manual inspection of the EAT token, checking that the tag is retained in the
312     // software submod, but not in the hardware submod.
313     auto [top_level_item, next_pos, error] = cppbor::parse(eat.data(), eat.size());
314     ASSERT_NE(top_level_item, nullptr);
315     const cppbor::Map* eat_map = top_level_item->asMap();
316     ASSERT_NE(eat_map, nullptr);
317     bool found_in_software_submod = false;
318     bool found_in_hardware_submod = false;
319     for (size_t i = 0; i < eat_map->size(); i++) {
320         auto& [eat_key, eat_value] = (*eat_map)[i];
321         const cppbor::Int* root_key = eat_key->asInt();
322         if ((EatClaim)root_key->value() == EatClaim::SUBMODS) {
323             const cppbor::Map* submods_map = eat_value->asMap();
324             // Check for each submod whether it contains the expected value.
325             for (size_t j = 0; j < submods_map->size(); j++) {
326                 auto& [submod_key, submod_value] = (*submods_map)[j];
327                 const cppbor::Map* submod_map = submod_value->asMap();
328                 bool found_in_submod = false;
329                 EatSecurityLevel submod_security_level;
330                 for (size_t k = 0; k < submod_map->size(); k++) {
331                     auto& [key_item, value_item] = (*submod_map)[k];
332                     const cppbor::Int* key_int = key_item->asInt();
333                     if (key_int->value() == convert_to_eat_claim(UNKNOWN_TAG_T)) {
334                         found_in_submod = true;
335                     } else if ((EatClaim)key_int->value() == EatClaim::SECURITY_LEVEL) {
336                         submod_security_level = (EatSecurityLevel)value_item->asInt()->value();
337                     }
338                 }
339                 if (submod_security_level == EatSecurityLevel::UNRESTRICTED) {
340                     found_in_software_submod = found_in_submod;
341                 } else if (submod_security_level == EatSecurityLevel::SECURE_RESTRICTED) {
342                     found_in_hardware_submod = found_in_submod;
343                 }
344             }
345         }
346     }
347     EXPECT_FALSE(found_in_hardware_submod);
348     EXPECT_TRUE(found_in_software_submod);
349 }
350 
351 }  // namespace test
352 }  // namespace keymaster
353