blob: 3d97daf2745f47a687935e0f70a223da38719860 [file] [log] [blame]
Shawn Willden3f7c80a2020-01-15 19:09:50 -07001/*
2 * Copyright (C) 2020 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
nagendra modadugueb7f3522020-04-01 02:40:59 -070017#define LOG_TAG "keymaster_hidl_hal_test"
18#include <cutils/log.h>
Eran Messericb238ff2021-06-08 11:38:31 +010019#include <vector>
nagendra modadugueb7f3522020-04-01 02:40:59 -070020
Shawn Willden3f7c80a2020-01-15 19:09:50 -070021#include "Keymaster4_1HidlTest.h"
22
23#include <cutils/properties.h>
24
25#include <openssl/x509.h>
26
27#include <keymasterV4_1/attestation_record.h>
28#include <keymasterV4_1/authorization_set.h>
29
nagendra modadugueb7f3522020-04-01 02:40:59 -070030// Not to dump the attestation by default. Can enable by specify the parameter
31// "--dump_attestations" on lunching VTS
32static bool dumpAttestations = false;
33
Shawn Willden3f7c80a2020-01-15 19:09:50 -070034namespace android::hardware::keymaster::V4_0 {
35
36bool operator==(const AuthorizationSet& a, const AuthorizationSet& b) {
37 return std::equal(a.begin(), a.end(), b.begin(), b.end());
38}
39
40} // namespace android::hardware::keymaster::V4_0
41
42namespace android::hardware::keymaster::V4_1 {
43
44inline ::std::ostream& operator<<(::std::ostream& os, Tag tag) {
45 return os << toString(tag);
46}
47
48namespace test {
49
50using std::string;
51using std::tuple;
52
53namespace {
54
55char nibble2hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
56 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
57
58string bin2hex(const hidl_vec<uint8_t>& data) {
59 string retval;
60 retval.reserve(data.size() * 2 + 1);
61 for (uint8_t byte : data) {
62 retval.push_back(nibble2hex[0x0F & (byte >> 4)]);
63 retval.push_back(nibble2hex[0x0F & byte]);
64 }
65 return retval;
66}
67
nagendra modadugueb7f3522020-04-01 02:40:59 -070068inline void dumpContent(string content) {
69 std::cout << content << std::endl;
70}
71
Shawn Willden3f7c80a2020-01-15 19:09:50 -070072struct AuthorizationSetDifferences {
73 string aName;
74 string bName;
75 AuthorizationSet aWhackB;
76 AuthorizationSet bWhackA;
77};
78
79std::ostream& operator<<(std::ostream& o, const AuthorizationSetDifferences& diffs) {
80 if (!diffs.aWhackB.empty()) {
81 o << "Set " << diffs.aName << " contains the following that " << diffs.bName << " does not"
82 << diffs.aWhackB;
83 if (!diffs.bWhackA.empty()) o << std::endl;
84 }
85
86 if (!diffs.bWhackA.empty()) {
87 o << "Set " << diffs.bName << " contains the following that " << diffs.aName << " does not"
88 << diffs.bWhackA;
89 }
90 return o;
91}
92
93// Computes and returns a \ b and b \ a ('\' is the set-difference operator, a \ b means all the
94// elements that are in a but not b, i.e. take a and whack all the elements in b) to the provided
95// stream. The sets must be sorted.
96//
97// This provides a simple and clear view of how the two sets differ, generally much
98// easier than scrutinizing printouts of the two sets.
99AuthorizationSetDifferences difference(string aName, const AuthorizationSet& a, string bName,
100 const AuthorizationSet& b) {
101 AuthorizationSetDifferences diffs = {std::move(aName), std::move(bName), {}, {}};
102 std::set_difference(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(diffs.aWhackB));
103 std::set_difference(b.begin(), b.end(), a.begin(), a.end(), std::back_inserter(diffs.bWhackA));
104 return diffs;
105}
106
107#define DIFFERENCE(a, b) difference(#a, a, #b, b)
108
109void check_root_of_trust(const RootOfTrust& root_of_trust) {
110 char vb_meta_device_state[PROPERTY_VALUE_MAX];
111 if (property_get("ro.boot.vbmeta.device_state", vb_meta_device_state, "") == 0) return;
112
113 char vb_meta_digest[PROPERTY_VALUE_MAX];
114 EXPECT_GT(property_get("ro.boot.vbmeta.digest", vb_meta_digest, ""), 0);
115 EXPECT_EQ(vb_meta_digest, bin2hex(root_of_trust.verified_boot_hash));
116
117 // Verified boot key should be all 0's if the boot state is not verified or self signed
118 HidlBuf empty_boot_key(string(32, '\0'));
119
120 char vb_meta_bootstate[PROPERTY_VALUE_MAX];
121 auto& verified_boot_key = root_of_trust.verified_boot_key;
122 auto& verified_boot_state = root_of_trust.verified_boot_state;
123 EXPECT_GT(property_get("ro.boot.verifiedbootstate", vb_meta_bootstate, ""), 0);
124 if (!strcmp(vb_meta_bootstate, "green")) {
125 EXPECT_EQ(verified_boot_state, V4_0::KM_VERIFIED_BOOT_VERIFIED);
126 EXPECT_NE(verified_boot_key, empty_boot_key);
127 } else if (!strcmp(vb_meta_bootstate, "yellow")) {
128 EXPECT_EQ(verified_boot_state, V4_0::KM_VERIFIED_BOOT_SELF_SIGNED);
129 EXPECT_NE(verified_boot_key, empty_boot_key);
130 } else if (!strcmp(vb_meta_bootstate, "orange")) {
131 EXPECT_EQ(verified_boot_state, V4_0::KM_VERIFIED_BOOT_UNVERIFIED);
132 EXPECT_EQ(verified_boot_key, empty_boot_key);
133 } else if (!strcmp(vb_meta_bootstate, "red")) {
134 EXPECT_EQ(verified_boot_state, V4_0::KM_VERIFIED_BOOT_FAILED);
135 } else {
136 EXPECT_EQ(verified_boot_state, V4_0::KM_VERIFIED_BOOT_UNVERIFIED);
137 EXPECT_EQ(verified_boot_key, empty_boot_key);
138 }
139}
140
nagendra modadugueb7f3522020-04-01 02:40:59 -0700141bool tag_in_list(const KeyParameter& entry) {
142 // Attestations don't contain everything in key authorization lists, so we need to filter
143 // the key lists to produce the lists that we expect to match the attestations.
144 auto tag_list = {
145 Tag::INCLUDE_UNIQUE_ID, Tag::BLOB_USAGE_REQUIREMENTS, Tag::EC_CURVE,
146 Tag::HARDWARE_TYPE, Tag::VENDOR_PATCHLEVEL, Tag::BOOT_PATCHLEVEL,
147 Tag::CREATION_DATETIME,
148 };
149 return std::find(tag_list.begin(), tag_list.end(), (V4_1::Tag)entry.tag) != tag_list.end();
150}
151
152AuthorizationSet filter_tags(const AuthorizationSet& set) {
153 AuthorizationSet filtered;
154 std::remove_copy_if(set.begin(), set.end(), std::back_inserter(filtered), tag_in_list);
155 return filtered;
156}
157
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700158void check_attestation_record(AttestationRecord attestation, const HidlBuf& challenge,
159 AuthorizationSet expected_sw_enforced,
160 AuthorizationSet expected_hw_enforced,
161 SecurityLevel expected_security_level) {
162 EXPECT_EQ(41U, attestation.keymaster_version);
163 EXPECT_EQ(4U, attestation.attestation_version);
164 EXPECT_EQ(expected_security_level, attestation.attestation_security_level);
165 EXPECT_EQ(expected_security_level, attestation.keymaster_security_level);
166 EXPECT_EQ(challenge, attestation.attestation_challenge);
167
168 check_root_of_trust(attestation.root_of_trust);
169
170 // Sort all of the authorization lists, so that equality matching works.
171 expected_sw_enforced.Sort();
172 expected_hw_enforced.Sort();
173 attestation.software_enforced.Sort();
174 attestation.hardware_enforced.Sort();
175
nagendra modadugueb7f3522020-04-01 02:40:59 -0700176 EXPECT_EQ(filter_tags(expected_sw_enforced), filter_tags(attestation.software_enforced))
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700177 << DIFFERENCE(expected_sw_enforced, attestation.software_enforced);
nagendra modadugueb7f3522020-04-01 02:40:59 -0700178 EXPECT_EQ(filter_tags(expected_hw_enforced), filter_tags(attestation.hardware_enforced))
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700179 << DIFFERENCE(expected_hw_enforced, attestation.hardware_enforced);
180}
181
Eran Messericb238ff2021-06-08 11:38:31 +0100182X509_Ptr parse_cert_blob(const std::vector<uint8_t>& blob) {
183 const uint8_t* p = blob.data();
184 return X509_Ptr(d2i_X509(nullptr /* allocate new */, &p, blob.size()));
185}
186
187bool check_certificate_chain_signatures(const hidl_vec<hidl_vec<uint8_t>>& cert_chain) {
188 // TODO: Check that root is self-signed once b/187803288 is resolved.
189 for (size_t i = 0; i < cert_chain.size() - 1; ++i) {
190 X509_Ptr key_cert(parse_cert_blob(cert_chain[i]));
191 X509_Ptr signing_cert(parse_cert_blob(cert_chain[i + 1]));
192
193 if (!key_cert.get() || !signing_cert.get()) {
194 return false;
195 }
196
197 EVP_PKEY_Ptr signing_pubkey(X509_get_pubkey(signing_cert.get()));
198 if (!signing_pubkey.get()) {
199 return false;
200 }
201
202 if (!X509_verify(key_cert.get(), signing_pubkey.get())) {
203 return false;
204 }
205 }
206 return true;
207}
208
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700209} // namespace
210
211using std::string;
212using DeviceUniqueAttestationTest = Keymaster4_1HidlTest;
213
nagendra modadugu5d531a22020-03-31 15:55:01 -0700214TEST_P(DeviceUniqueAttestationTest, NonStrongBoxOnly) {
215 if (SecLevel() == SecurityLevel::STRONGBOX) return;
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700216
217 ASSERT_EQ(ErrorCode::OK, convert(GenerateKey(AuthorizationSetBuilder()
218 .Authorization(TAG_NO_AUTH_REQUIRED)
219 .RsaSigningKey(2048, 65537)
220 .Digest(Digest::SHA_2_256)
221 .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN)
222 .Authorization(TAG_INCLUDE_UNIQUE_ID))));
223
224 hidl_vec<hidl_vec<uint8_t>> cert_chain;
225 EXPECT_EQ(ErrorCode::UNIMPLEMENTED,
226 convert(AttestKey(
227 AuthorizationSetBuilder()
228 .Authorization(TAG_DEVICE_UNIQUE_ATTESTATION)
229 .Authorization(TAG_ATTESTATION_CHALLENGE, HidlBuf("challenge"))
230 .Authorization(TAG_ATTESTATION_APPLICATION_ID, HidlBuf("foo")),
231 &cert_chain)));
232 CheckedDeleteKey();
233
234 ASSERT_EQ(ErrorCode::OK, convert(GenerateKey(AuthorizationSetBuilder()
235 .Authorization(TAG_NO_AUTH_REQUIRED)
236 .EcdsaSigningKey(EcCurve::P_256)
237 .Digest(Digest::SHA_2_256)
238 .Authorization(TAG_INCLUDE_UNIQUE_ID))));
239
240 EXPECT_EQ(ErrorCode::UNIMPLEMENTED,
241 convert(AttestKey(
242 AuthorizationSetBuilder()
allen.zhang569a6122020-07-15 09:53:26 +0800243 .Authorization(TAG_DEVICE_UNIQUE_ATTESTATION)
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700244 .Authorization(TAG_ATTESTATION_CHALLENGE, HidlBuf("challenge"))
245 .Authorization(TAG_ATTESTATION_APPLICATION_ID, HidlBuf("foo")),
246 &cert_chain)));
allen.zhang569a6122020-07-15 09:53:26 +0800247 CheckedDeleteKey();
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700248}
249
250TEST_P(DeviceUniqueAttestationTest, Rsa) {
251 if (SecLevel() != SecurityLevel::STRONGBOX) return;
Chirag Pathak2f408e52021-02-19 23:10:05 +0000252 ASSERT_EQ(ErrorCode::OK, convert(GenerateKey(AuthorizationSetBuilder()
253 .Authorization(TAG_NO_AUTH_REQUIRED)
254 .RsaSigningKey(2048, 65537)
255 .Digest(Digest::SHA_2_256)
256 .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN)
257 .Authorization(TAG_INCLUDE_UNIQUE_ID))));
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700258
259 hidl_vec<hidl_vec<uint8_t>> cert_chain;
260 HidlBuf challenge("challenge");
261 HidlBuf app_id("foo");
Chirag Pathak2f408e52021-02-19 23:10:05 +0000262 ErrorCode result =
263 convert(AttestKey(AuthorizationSetBuilder()
264 .Authorization(TAG_DEVICE_UNIQUE_ATTESTATION)
265 .Authorization(TAG_ATTESTATION_CHALLENGE, challenge)
266 .Authorization(TAG_ATTESTATION_APPLICATION_ID, app_id),
267 &cert_chain));
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700268
Chirag Pathak2f408e52021-02-19 23:10:05 +0000269 // It is optional for Strong box to support DeviceUniqueAttestation.
270 if (result == ErrorCode::CANNOT_ATTEST_IDS) return;
271
272 EXPECT_EQ(ErrorCode::OK, result);
nagendra modadugueb7f3522020-04-01 02:40:59 -0700273 EXPECT_EQ(2U, cert_chain.size());
Eran Messericb238ff2021-06-08 11:38:31 +0100274 EXPECT_TRUE(check_certificate_chain_signatures(cert_chain));
Tommy Chiu566d1cb2021-05-11 18:09:26 +0800275 if (dumpAttestations) {
276 for (auto cert_ : cert_chain) dumpContent(bin2hex(cert_));
277 }
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700278 auto [err, attestation] = parse_attestation_record(cert_chain[0]);
nagendra modadugueb7f3522020-04-01 02:40:59 -0700279 ASSERT_EQ(ErrorCode::OK, err);
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700280
Chirag Pathak2f408e52021-02-19 23:10:05 +0000281 check_attestation_record(
282 attestation, challenge,
283 /* sw_enforced */
284 AuthorizationSetBuilder().Authorization(TAG_ATTESTATION_APPLICATION_ID, app_id),
285 /* hw_enforced */
286 AuthorizationSetBuilder()
287 .Authorization(TAG_DEVICE_UNIQUE_ATTESTATION)
288 .Authorization(TAG_NO_AUTH_REQUIRED)
289 .RsaSigningKey(2048, 65537)
290 .Digest(Digest::SHA_2_256)
291 .Padding(PaddingMode::RSA_PKCS1_1_5_SIGN)
292 .Authorization(TAG_ORIGIN, KeyOrigin::GENERATED)
293 .Authorization(TAG_OS_VERSION, os_version())
294 .Authorization(TAG_OS_PATCHLEVEL, os_patch_level()),
295 SecLevel());
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700296}
297
298TEST_P(DeviceUniqueAttestationTest, Ecdsa) {
299 if (SecLevel() != SecurityLevel::STRONGBOX) return;
Chirag Pathak2f408e52021-02-19 23:10:05 +0000300 ASSERT_EQ(ErrorCode::OK, convert(GenerateKey(AuthorizationSetBuilder()
301 .Authorization(TAG_NO_AUTH_REQUIRED)
302 .EcdsaSigningKey(256)
303 .Digest(Digest::SHA_2_256)
304 .Authorization(TAG_INCLUDE_UNIQUE_ID))));
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700305
306 hidl_vec<hidl_vec<uint8_t>> cert_chain;
307 HidlBuf challenge("challenge");
308 HidlBuf app_id("foo");
Chirag Pathak2f408e52021-02-19 23:10:05 +0000309 ErrorCode result =
310 convert(AttestKey(AuthorizationSetBuilder()
311 .Authorization(TAG_DEVICE_UNIQUE_ATTESTATION)
312 .Authorization(TAG_ATTESTATION_CHALLENGE, challenge)
313 .Authorization(TAG_ATTESTATION_APPLICATION_ID, app_id),
314 &cert_chain));
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700315
Chirag Pathak2f408e52021-02-19 23:10:05 +0000316 // It is optional for Strong box to support DeviceUniqueAttestation.
317 if (result == ErrorCode::CANNOT_ATTEST_IDS) return;
318
319 EXPECT_EQ(ErrorCode::OK, result);
nagendra modadugueb7f3522020-04-01 02:40:59 -0700320 EXPECT_EQ(2U, cert_chain.size());
Eran Messericb238ff2021-06-08 11:38:31 +0100321 EXPECT_TRUE(check_certificate_chain_signatures(cert_chain));
Tommy Chiu566d1cb2021-05-11 18:09:26 +0800322 if (dumpAttestations) {
323 for (auto cert_ : cert_chain) dumpContent(bin2hex(cert_));
324 }
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700325 auto [err, attestation] = parse_attestation_record(cert_chain[0]);
nagendra modadugueb7f3522020-04-01 02:40:59 -0700326 ASSERT_EQ(ErrorCode::OK, err);
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700327
Chirag Pathak2f408e52021-02-19 23:10:05 +0000328 check_attestation_record(
329 attestation, challenge,
nagendra modadugueb7f3522020-04-01 02:40:59 -0700330 /* sw_enforced */
331 AuthorizationSetBuilder().Authorization(TAG_ATTESTATION_APPLICATION_ID, app_id),
332 /* hw_enforced */
333 AuthorizationSetBuilder()
334 .Authorization(TAG_DEVICE_UNIQUE_ATTESTATION)
335 .Authorization(TAG_NO_AUTH_REQUIRED)
336 .EcdsaSigningKey(256)
337 .Digest(Digest::SHA_2_256)
338 .Authorization(TAG_EC_CURVE, EcCurve::P_256)
339 .Authorization(TAG_ORIGIN, KeyOrigin::GENERATED)
340 .Authorization(TAG_OS_VERSION, os_version())
341 .Authorization(TAG_OS_PATCHLEVEL, os_patch_level()),
342 SecLevel());
Shawn Willden3f7c80a2020-01-15 19:09:50 -0700343}
344
345INSTANTIATE_KEYMASTER_4_1_HIDL_TEST(DeviceUniqueAttestationTest);
346
347} // namespace test
348} // namespace android::hardware::keymaster::V4_1
nagendra modadugueb7f3522020-04-01 02:40:59 -0700349
350int main(int argc, char** argv) {
351 ::testing::InitGoogleTest(&argc, argv);
352 for (int i = 1; i < argc; ++i) {
353 if (argv[i][0] == '-') {
354 if (std::string(argv[i]) == "--dump_attestations") {
355 dumpAttestations = true;
356 }
357 }
358 }
359 int status = RUN_ALL_TESTS();
360 ALOGI("Test result = %d", status);
361 return status;
362}