blob: e46aeee12c63f52130651620c8496e2793e53b7a [file] [log] [blame]
Shawn Willden7c130392020-12-21 09:58:22 -07001/*
2 * Copyright (C) 2021 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#define LOG_TAG "keymint_1_attest_key_test"
Rajesh Nyamagoudeb644cf2022-12-15 03:50:52 +000018#include <android-base/logging.h>
19#include <android-base/strings.h>
Shawn Willden7c130392020-12-21 09:58:22 -070020#include <cutils/log.h>
David Drysdale43489272022-06-13 10:12:12 +010021#include <cutils/properties.h>
Shawn Willden7c130392020-12-21 09:58:22 -070022
23#include <keymint_support/key_param_output.h>
24#include <keymint_support/openssl_utils.h>
25
26#include "KeyMintAidlTestBase.h"
27
28namespace aidl::android::hardware::security::keymint::test {
29
30namespace {
Rajesh Nyamagoudeb644cf2022-12-15 03:50:52 +000031string TELEPHONY_CMD_GET_IMEI = "cmd phone get-imei ";
Shawn Willden7c130392020-12-21 09:58:22 -070032
Shawn Willden7c130392020-12-21 09:58:22 -070033bool IsSelfSigned(const vector<Certificate>& chain) {
34 if (chain.size() != 1) return false;
35 return ChainSignaturesAreValid(chain);
36}
37
Rajesh Nyamagoudeb644cf2022-12-15 03:50:52 +000038/*
39 * Run a shell command and collect the output of it. If any error, set an empty string as the
40 * output.
41 */
42string exec_command(string command) {
43 char buffer[128];
44 string result = "";
45
46 FILE* pipe = popen(command.c_str(), "r");
47 if (!pipe) {
48 LOG(ERROR) << "popen failed.";
49 return result;
50 }
51
52 // read till end of process:
53 while (!feof(pipe)) {
54 if (fgets(buffer, 128, pipe) != NULL) {
55 result += buffer;
56 }
57 }
58
59 pclose(pipe);
60 return result;
61}
62
63/*
64 * Get IMEI using Telephony service shell command. If any error while executing the command
65 * then empty string will be returned as output.
66 */
67string get_imei(int slot) {
68 string cmd = TELEPHONY_CMD_GET_IMEI + std::to_string(slot);
69 string output = exec_command(cmd);
70
71 if (output.empty()) {
72 LOG(ERROR) << "Command failed. Cmd: " << cmd;
73 return "";
74 }
75
76 vector<string> out = ::android::base::Tokenize(::android::base::Trim(output), "Device IMEI:");
77
78 if (out.size() != 1) {
79 LOG(ERROR) << "Error in parsing the command output. Cmd: " << cmd;
80 return "";
81 }
82
83 return ::android::base::Trim(out[0]);
84}
85
Shawn Willden7c130392020-12-21 09:58:22 -070086} // namespace
87
David Drysdale43489272022-06-13 10:12:12 +010088class AttestKeyTest : public KeyMintAidlTestBase {
Benjamin Grimberg981c9c22023-01-05 14:48:36 +020089 public:
90 void SetUp() override {
91 check_skip_test();
92 KeyMintAidlTestBase::SetUp();
93 }
94
David Drysdale43489272022-06-13 10:12:12 +010095 protected:
Benjamin Grimberg981c9c22023-01-05 14:48:36 +020096 const string FEATURE_KEYSTORE_APP_ATTEST_KEY = "android.hardware.keystore.app_attest_key";
97
98 const string FEATURE_STRONGBOX_KEYSTORE = "android.hardware.strongbox_keystore";
99
David Drysdale43489272022-06-13 10:12:12 +0100100 ErrorCode GenerateAttestKey(const AuthorizationSet& key_desc,
101 const optional<AttestationKey>& attest_key,
102 vector<uint8_t>* key_blob,
103 vector<KeyCharacteristics>* key_characteristics,
104 vector<Certificate>* cert_chain) {
105 // The original specification for KeyMint v1 required ATTEST_KEY not be combined
106 // with any other key purpose, but the original VTS tests incorrectly did exactly that.
107 // This means that a device that launched prior to Android T (API level 33) may
108 // accept or even require KeyPurpose::SIGN too.
109 if (property_get_int32("ro.board.first_api_level", 0) < 33) {
110 AuthorizationSet key_desc_plus_sign = key_desc;
111 key_desc_plus_sign.push_back(TAG_PURPOSE, KeyPurpose::SIGN);
112
113 auto result = GenerateKey(key_desc_plus_sign, attest_key, key_blob, key_characteristics,
114 cert_chain);
115 if (result == ErrorCode::OK) {
116 return result;
117 }
118 // If the key generation failed, it may be because the device is (correctly)
119 // rejecting the combination of ATTEST_KEY+SIGN. Fall through to try again with
120 // just ATTEST_KEY.
121 }
122 return GenerateKey(key_desc, attest_key, key_blob, key_characteristics, cert_chain);
123 }
Benjamin Grimberg981c9c22023-01-05 14:48:36 +0200124
125 // Check if ATTEST_KEY feature is disabled
126 bool is_attest_key_feature_disabled(void) const {
127 if (!check_feature(FEATURE_KEYSTORE_APP_ATTEST_KEY)) {
128 GTEST_LOG_(INFO) << "Feature " + FEATURE_KEYSTORE_APP_ATTEST_KEY + " is disabled";
129 return true;
130 }
131
132 return false;
133 }
134
135 // Check if StrongBox KeyStore is enabled
136 bool is_strongbox_enabled(void) const {
137 if (check_feature(FEATURE_STRONGBOX_KEYSTORE)) {
138 GTEST_LOG_(INFO) << "Feature " + FEATURE_STRONGBOX_KEYSTORE + " is enabled";
139 return true;
140 }
141
142 return false;
143 }
144
145 // Check if chipset has received a waiver allowing it to be launched with
146 // Android S (or later) with Keymaster 4.0 in StrongBox
147 bool is_chipset_allowed_km4_strongbox(void) const {
148 std::array<char, PROPERTY_VALUE_MAX> buffer;
149
150 auto res = property_get("ro.vendor.qti.soc_model", buffer.data(), nullptr);
151 if (res <= 0) return false;
152
153 const string allowed_soc_models[] = {"SM8450", "SM8475", "SM8550", "SXR2230P"};
154
155 for (const string model : allowed_soc_models) {
156 if (model.compare(buffer.data()) == 0) {
157 GTEST_LOG_(INFO) << "QTI SOC Model " + model + " is allowed SB KM 4.0";
158 return true;
159 }
160 }
161
162 return false;
163 }
164
165 // Skip the test if all the following conditions hold:
166 // 1. ATTEST_KEY feature is disabled
167 // 2. STRONGBOX is enabled
168 // 3. The device is running one of the chipsets that have received a waiver
169 // allowing it to be launched with Android S (or later) with Keymaster 4.0
170 // in StrongBox
171 void check_skip_test(void) const {
172 if (is_attest_key_feature_disabled() && is_strongbox_enabled() &&
173 is_chipset_allowed_km4_strongbox()) {
174 GTEST_SKIP() << "Test is not applicable";
175 }
176 }
David Drysdale43489272022-06-13 10:12:12 +0100177};
Shawn Willden7c130392020-12-21 09:58:22 -0700178
Selene Huang8f9494c2021-04-21 15:10:36 -0700179/*
180 * AttestKeyTest.AllRsaSizes
181 *
182 * This test creates self signed RSA attestation keys of various sizes, and verify they can be
183 * used to sign other RSA and EC keys.
184 */
Shawn Willden7c130392020-12-21 09:58:22 -0700185TEST_P(AttestKeyTest, AllRsaSizes) {
186 for (auto size : ValidKeySizes(Algorithm::RSA)) {
187 /*
David Drysdale7de9feb2021-03-05 14:56:19 +0000188 * Create attestation key.
Shawn Willden7c130392020-12-21 09:58:22 -0700189 */
190 AttestationKey attest_key;
191 vector<KeyCharacteristics> attest_key_characteristics;
192 vector<Certificate> attest_key_cert_chain;
David Drysdale43489272022-06-13 10:12:12 +0100193 ASSERT_EQ(ErrorCode::OK,
194 GenerateAttestKey(AuthorizationSetBuilder()
195 .RsaKey(size, 65537)
196 .AttestKey()
197 .SetDefaultValidity(),
198 {} /* attestation signing key */, &attest_key.keyBlob,
199 &attest_key_characteristics, &attest_key_cert_chain));
Shawn Willden7c130392020-12-21 09:58:22 -0700200
Shawn Willdenc410f6f2021-04-22 13:28:45 -0600201 ASSERT_GT(attest_key_cert_chain.size(), 0);
Shawn Willden7c130392020-12-21 09:58:22 -0700202 EXPECT_EQ(attest_key_cert_chain.size(), 1);
203 EXPECT_TRUE(IsSelfSigned(attest_key_cert_chain)) << "Failed on size " << size;
204
205 /*
Selene Huang8f9494c2021-04-21 15:10:36 -0700206 * Use attestation key to sign RSA signing key
Shawn Willden7c130392020-12-21 09:58:22 -0700207 */
208 attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key");
209 vector<uint8_t> attested_key_blob;
210 vector<KeyCharacteristics> attested_key_characteristics;
211 vector<Certificate> attested_key_cert_chain;
212 EXPECT_EQ(ErrorCode::OK,
213 GenerateKey(AuthorizationSetBuilder()
214 .RsaSigningKey(2048, 65537)
215 .Authorization(TAG_NO_AUTH_REQUIRED)
216 .AttestationChallenge("foo")
217 .AttestationApplicationId("bar")
218 .SetDefaultValidity(),
219 attest_key, &attested_key_blob, &attested_key_characteristics,
220 &attested_key_cert_chain));
221
222 CheckedDeleteKey(&attested_key_blob);
223
224 AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics);
225 AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000226 EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced,
227 SecLevel(),
Shawn Willden7c130392020-12-21 09:58:22 -0700228 attested_key_cert_chain[0].encodedCertificate));
229
230 // Attestation by itself is not valid (last entry is not self-signed).
231 EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain));
232
233 // Appending the attest_key chain to the attested_key_chain should yield a valid chain.
Selene Huang8f9494c2021-04-21 15:10:36 -0700234 attested_key_cert_chain.push_back(attest_key_cert_chain[0]);
Shawn Willden7c130392020-12-21 09:58:22 -0700235 EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain));
Selene Huang8f9494c2021-04-21 15:10:36 -0700236 EXPECT_EQ(attested_key_cert_chain.size(), 2);
237
238 /*
239 * Use attestation key to sign RSA decryption key
240 */
241 attested_key_characteristics.resize(0);
242 attested_key_cert_chain.resize(0);
243 EXPECT_EQ(ErrorCode::OK,
244 GenerateKey(AuthorizationSetBuilder()
245 .RsaEncryptionKey(2048, 65537)
246 .Digest(Digest::NONE)
247 .Padding(PaddingMode::NONE)
248 .Authorization(TAG_NO_AUTH_REQUIRED)
249 .AttestationChallenge("foo2")
250 .AttestationApplicationId("bar2")
251 .SetDefaultValidity(),
252 attest_key, &attested_key_blob, &attested_key_characteristics,
253 &attested_key_cert_chain));
254
255 CheckedDeleteKey(&attested_key_blob);
256
257 hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics);
258 sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000259 EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo2", "bar2", sw_enforced,
260 hw_enforced, SecLevel(),
Selene Huang8f9494c2021-04-21 15:10:36 -0700261 attested_key_cert_chain[0].encodedCertificate));
262
263 // Attestation by itself is not valid (last entry is not self-signed).
264 EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain));
265
266 // Appending the attest_key chain to the attested_key_chain should yield a valid chain.
267 attested_key_cert_chain.push_back(attest_key_cert_chain[0]);
268 EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain));
269 EXPECT_EQ(attested_key_cert_chain.size(), 2);
Shawn Willden7c130392020-12-21 09:58:22 -0700270
271 /*
David Drysdaled2cc8c22021-04-15 13:29:45 +0100272 * Use attestation key to sign EC key. Specify a CREATION_DATETIME for this one.
Shawn Willden7c130392020-12-21 09:58:22 -0700273 */
Selene Huang8f9494c2021-04-21 15:10:36 -0700274 attested_key_characteristics.resize(0);
275 attested_key_cert_chain.resize(0);
David Drysdaled2cc8c22021-04-15 13:29:45 +0100276 uint64_t timestamp = 1619621648000;
Shawn Willden7c130392020-12-21 09:58:22 -0700277 EXPECT_EQ(ErrorCode::OK,
278 GenerateKey(AuthorizationSetBuilder()
279 .EcdsaSigningKey(EcCurve::P_256)
280 .Authorization(TAG_NO_AUTH_REQUIRED)
281 .AttestationChallenge("foo")
282 .AttestationApplicationId("bar")
David Drysdaled2cc8c22021-04-15 13:29:45 +0100283 .Authorization(TAG_CREATION_DATETIME, timestamp)
Shawn Willden7c130392020-12-21 09:58:22 -0700284 .SetDefaultValidity(),
285 attest_key, &attested_key_blob, &attested_key_characteristics,
286 &attested_key_cert_chain));
287
David Drysdale300b5552021-05-20 12:05:26 +0100288 // The returned key characteristics will include CREATION_DATETIME (checked below)
289 // in SecurityLevel::KEYSTORE; this will be stripped out in the CheckCharacteristics()
290 // call below, to match what getKeyCharacteristics() returns (which doesn't include
291 // any SecurityLevel::KEYSTORE characteristics).
292 CheckCharacteristics(attested_key_blob, attested_key_characteristics);
293
Shawn Willden7c130392020-12-21 09:58:22 -0700294 CheckedDeleteKey(&attested_key_blob);
295 CheckedDeleteKey(&attest_key.keyBlob);
296
297 hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics);
298 sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics);
David Drysdale300b5552021-05-20 12:05:26 +0100299
David Drysdaled2cc8c22021-04-15 13:29:45 +0100300 // The client-specified CREATION_DATETIME should be in sw_enforced.
David Drysdale7dff4fc2021-12-10 10:10:52 +0000301 // Its presence will also trigger verify_attestation_record() to check that
302 // it is in the attestation extension with a matching value.
David Drysdaled2cc8c22021-04-15 13:29:45 +0100303 EXPECT_TRUE(sw_enforced.Contains(TAG_CREATION_DATETIME, timestamp))
304 << "expected CREATION_TIMESTAMP in sw_enforced:" << sw_enforced
305 << " not in hw_enforced:" << hw_enforced;
David Drysdale7dff4fc2021-12-10 10:10:52 +0000306 EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced,
307 SecLevel(),
Shawn Willden7c130392020-12-21 09:58:22 -0700308 attested_key_cert_chain[0].encodedCertificate));
309
310 // Attestation by itself is not valid (last entry is not self-signed).
311 EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain));
312
313 // Appending the attest_key chain to the attested_key_chain should yield a valid chain.
Selene Huang8f9494c2021-04-21 15:10:36 -0700314 attested_key_cert_chain.push_back(attest_key_cert_chain[0]);
Shawn Willden7c130392020-12-21 09:58:22 -0700315 EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain));
316
317 // Bail early if anything failed.
318 if (HasFailure()) return;
319 }
320}
321
Selene Huang8f9494c2021-04-21 15:10:36 -0700322/*
David Drysdalee60248c2021-10-04 12:54:13 +0100323 * AttestKeyTest.RsaAttestKeyMultiPurposeFail
324 *
325 * This test attempts to create an RSA attestation key that also allows signing.
326 */
327TEST_P(AttestKeyTest, RsaAttestKeyMultiPurposeFail) {
David Drysdale50a66b82022-03-21 15:21:32 +0000328 if (AidlVersion() < 2) {
329 // The KeyMint v1 spec required that KeyPurpose::ATTEST_KEY not be combined
330 // with other key purposes. However, this was not checked at the time
331 // so we can only be strict about checking this for implementations of KeyMint
332 // version 2 and above.
333 GTEST_SKIP() << "Single-purpose for KeyPurpose::ATTEST_KEY only strict since KeyMint v2";
334 }
335
David Drysdalee60248c2021-10-04 12:54:13 +0100336 vector<uint8_t> attest_key_blob;
337 vector<KeyCharacteristics> attest_key_characteristics;
338 vector<Certificate> attest_key_cert_chain;
339 ASSERT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE,
340 GenerateKey(AuthorizationSetBuilder()
341 .RsaSigningKey(2048, 65537)
342 .AttestKey()
343 .SetDefaultValidity(),
344 {} /* attestation signing key */, &attest_key_blob,
345 &attest_key_characteristics, &attest_key_cert_chain));
346}
347
348/*
Selene Huang8f9494c2021-04-21 15:10:36 -0700349 * AttestKeyTest.RsaAttestedAttestKeys
350 *
351 * This test creates an RSA attestation key signed by factory keys, and varifies it can be
352 * used to sign other RSA and EC keys.
353 */
354TEST_P(AttestKeyTest, RsaAttestedAttestKeys) {
355 auto challenge = "hello";
356 auto app_id = "foo";
357
358 auto subject = "cert subj 2";
359 vector<uint8_t> subject_der(make_name_from_str(subject));
360
David Drysdaledb0dcf52021-05-18 11:43:31 +0100361 // An X.509 certificate serial number SHOULD be >0, but this is not policed. Check
362 // that a zero value doesn't cause problems.
363 uint64_t serial_int = 0;
Selene Huang8f9494c2021-04-21 15:10:36 -0700364 vector<uint8_t> serial_blob(build_serial_blob(serial_int));
365
366 /*
367 * Create attestation key.
368 */
369 AttestationKey attest_key;
370 vector<KeyCharacteristics> attest_key_characteristics;
371 vector<Certificate> attest_key_cert_chain;
David Drysdale43489272022-06-13 10:12:12 +0100372 auto result = GenerateAttestKey(AuthorizationSetBuilder()
373 .RsaKey(2048, 65537)
374 .AttestKey()
375 .AttestationChallenge(challenge)
376 .AttestationApplicationId(app_id)
377 .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob)
378 .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der)
379 .Authorization(TAG_NO_AUTH_REQUIRED)
380 .SetDefaultValidity(),
381 {} /* attestation signing key */, &attest_key.keyBlob,
382 &attest_key_characteristics, &attest_key_cert_chain);
subrahmanyaman05642492022-02-05 07:10:56 +0000383 // Strongbox may not support factory provisioned attestation key.
384 if (SecLevel() == SecurityLevel::STRONGBOX) {
385 if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return;
386 }
387 ASSERT_EQ(ErrorCode::OK, result);
Selene Huang8f9494c2021-04-21 15:10:36 -0700388
389 EXPECT_GT(attest_key_cert_chain.size(), 1);
390 verify_subject_and_serial(attest_key_cert_chain[0], serial_int, subject, false);
391 EXPECT_TRUE(ChainSignaturesAreValid(attest_key_cert_chain));
392
393 AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attest_key_characteristics);
394 AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attest_key_characteristics);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000395 EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, //
Selene Huang8f9494c2021-04-21 15:10:36 -0700396 sw_enforced, hw_enforced, SecLevel(),
397 attest_key_cert_chain[0].encodedCertificate));
398
399 /*
400 * Use attestation key to sign RSA key
401 */
402 attest_key.issuerSubjectName = subject_der;
403 vector<uint8_t> attested_key_blob;
404 vector<KeyCharacteristics> attested_key_characteristics;
405 vector<Certificate> attested_key_cert_chain;
406
407 auto subject2 = "cert subject";
408 vector<uint8_t> subject_der2(make_name_from_str(subject2));
409
David Drysdaledb0dcf52021-05-18 11:43:31 +0100410 uint64_t serial_int2 = 255;
Selene Huang8f9494c2021-04-21 15:10:36 -0700411 vector<uint8_t> serial_blob2(build_serial_blob(serial_int2));
412
413 EXPECT_EQ(ErrorCode::OK,
414 GenerateKey(AuthorizationSetBuilder()
415 .RsaSigningKey(2048, 65537)
416 .Authorization(TAG_NO_AUTH_REQUIRED)
417 .AttestationChallenge("foo")
418 .AttestationApplicationId("bar")
419 .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob2)
420 .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der2)
421 .SetDefaultValidity(),
422 attest_key, &attested_key_blob, &attested_key_characteristics,
423 &attested_key_cert_chain));
424
425 CheckedDeleteKey(&attested_key_blob);
426 CheckedDeleteKey(&attest_key.keyBlob);
427
428 AuthorizationSet hw_enforced2 = HwEnforcedAuthorizations(attested_key_characteristics);
429 AuthorizationSet sw_enforced2 = SwEnforcedAuthorizations(attested_key_characteristics);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000430 EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced2, hw_enforced2,
431 SecLevel(),
Selene Huang8f9494c2021-04-21 15:10:36 -0700432 attested_key_cert_chain[0].encodedCertificate));
433
434 // Attestation by itself is not valid (last entry is not self-signed).
435 EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain));
436
437 // Appending the attest_key chain to the attested_key_chain should yield a valid chain.
438 attested_key_cert_chain.insert(attested_key_cert_chain.end(), attest_key_cert_chain.begin(),
439 attest_key_cert_chain.end());
440
441 EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain));
442 EXPECT_GT(attested_key_cert_chain.size(), 2);
443 verify_subject_and_serial(attested_key_cert_chain[0], serial_int2, subject2, false);
444}
445
446/*
447 * AttestKeyTest.RsaAttestKeyChaining
448 *
449 * This test creates a chain of multiple RSA attest keys, each used to sign the next attest key,
450 * with the last attest key signed by the factory chain.
451 */
452TEST_P(AttestKeyTest, RsaAttestKeyChaining) {
453 const int chain_size = 6;
454 vector<vector<uint8_t>> key_blob_list(chain_size);
455 vector<vector<Certificate>> cert_chain_list(chain_size);
456
457 for (int i = 0; i < chain_size; i++) {
458 string sub = "attest key chaining ";
459 char index = '1' + i;
460 string subject = sub + index;
461 vector<uint8_t> subject_der(make_name_from_str(subject));
462
463 uint64_t serial_int = 7000 + i;
464 vector<uint8_t> serial_blob(build_serial_blob(serial_int));
465
466 vector<KeyCharacteristics> attested_key_characteristics;
467 AttestationKey attest_key;
468 optional<AttestationKey> attest_key_opt;
469
470 if (i > 0) {
471 attest_key.issuerSubjectName = make_name_from_str(sub + (char)(index - 1));
472 attest_key.keyBlob = key_blob_list[i - 1];
473 attest_key_opt = attest_key;
474 }
475
David Drysdale43489272022-06-13 10:12:12 +0100476 auto result = GenerateAttestKey(AuthorizationSetBuilder()
477 .RsaKey(2048, 65537)
478 .AttestKey()
479 .AttestationChallenge("foo")
480 .AttestationApplicationId("bar")
481 .Authorization(TAG_NO_AUTH_REQUIRED)
482 .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob)
483 .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der)
484 .SetDefaultValidity(),
485 attest_key_opt, &key_blob_list[i],
486 &attested_key_characteristics, &cert_chain_list[i]);
subrahmanyaman05642492022-02-05 07:10:56 +0000487 // Strongbox may not support factory provisioned attestation key.
488 if (SecLevel() == SecurityLevel::STRONGBOX) {
489 if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return;
490 }
491 ASSERT_EQ(ErrorCode::OK, result);
Selene Huang8f9494c2021-04-21 15:10:36 -0700492
493 AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics);
494 AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics);
David Drysdalea0386952021-08-05 07:50:23 +0100495 ASSERT_GT(cert_chain_list[i].size(), 0);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000496 EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced,
497 SecLevel(),
Selene Huang8f9494c2021-04-21 15:10:36 -0700498 cert_chain_list[i][0].encodedCertificate));
499
500 if (i > 0) {
501 /*
502 * The first key is attestated with factory chain, but all the rest of the keys are
503 * not supposed to be returned in attestation certificate chains.
504 */
505 EXPECT_FALSE(ChainSignaturesAreValid(cert_chain_list[i]));
506
507 // Appending the attest_key chain to the attested_key_chain should yield a valid chain.
508 cert_chain_list[i].insert(cert_chain_list[i].end(), //
509 cert_chain_list[i - 1].begin(), //
510 cert_chain_list[i - 1].end());
511 }
512
513 EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_list[i]));
514 EXPECT_GT(cert_chain_list[i].size(), i + 1);
515 verify_subject_and_serial(cert_chain_list[i][0], serial_int, subject, false);
516 }
517
518 for (int i = 0; i < chain_size; i++) {
519 CheckedDeleteKey(&key_blob_list[i]);
520 }
521}
522
523/*
524 * AttestKeyTest.EcAttestKeyChaining
525 *
526 * This test creates a chain of multiple Ec attest keys, each used to sign the next attest key,
527 * with the last attest key signed by the factory chain.
528 */
529TEST_P(AttestKeyTest, EcAttestKeyChaining) {
530 const int chain_size = 6;
531 vector<vector<uint8_t>> key_blob_list(chain_size);
532 vector<vector<Certificate>> cert_chain_list(chain_size);
533
534 for (int i = 0; i < chain_size; i++) {
535 string sub = "Ec attest key chaining ";
536 char index = '1' + i;
537 string subject = sub + index;
538 vector<uint8_t> subject_der(make_name_from_str(subject));
539
540 uint64_t serial_int = 800000 + i;
541 vector<uint8_t> serial_blob(build_serial_blob(serial_int));
542
543 vector<KeyCharacteristics> attested_key_characteristics;
544 AttestationKey attest_key;
545 optional<AttestationKey> attest_key_opt;
546
547 if (i > 0) {
548 attest_key.issuerSubjectName = make_name_from_str(sub + (char)(index - 1));
549 attest_key.keyBlob = key_blob_list[i - 1];
550 attest_key_opt = attest_key;
551 }
552
David Drysdale43489272022-06-13 10:12:12 +0100553 auto result = GenerateAttestKey(AuthorizationSetBuilder()
554 .EcdsaKey(EcCurve::P_256)
555 .AttestKey()
556 .AttestationChallenge("foo")
557 .AttestationApplicationId("bar")
558 .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob)
559 .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der)
560 .Authorization(TAG_NO_AUTH_REQUIRED)
561 .SetDefaultValidity(),
562 attest_key_opt, &key_blob_list[i],
563 &attested_key_characteristics, &cert_chain_list[i]);
subrahmanyaman05642492022-02-05 07:10:56 +0000564 // Strongbox may not support factory provisioned attestation key.
565 if (SecLevel() == SecurityLevel::STRONGBOX) {
566 if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return;
567 }
568 ASSERT_EQ(ErrorCode::OK, result);
Selene Huang8f9494c2021-04-21 15:10:36 -0700569
570 AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics);
571 AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics);
David Drysdalea0386952021-08-05 07:50:23 +0100572 ASSERT_GT(cert_chain_list[i].size(), 0);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000573 EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced,
574 SecLevel(),
Selene Huang8f9494c2021-04-21 15:10:36 -0700575 cert_chain_list[i][0].encodedCertificate));
576
577 if (i > 0) {
578 /*
579 * The first key is attestated with factory chain, but all the rest of the keys are
580 * not supposed to be returned in attestation certificate chains.
581 */
582 EXPECT_FALSE(ChainSignaturesAreValid(cert_chain_list[i]));
583
584 // Appending the attest_key chain to the attested_key_chain should yield a valid chain.
585 cert_chain_list[i].insert(cert_chain_list[i].end(), //
586 cert_chain_list[i - 1].begin(), //
587 cert_chain_list[i - 1].end());
588 }
589
590 EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_list[i]));
591 EXPECT_GT(cert_chain_list[i].size(), i + 1);
592 verify_subject_and_serial(cert_chain_list[i][0], serial_int, subject, false);
593 }
594
595 for (int i = 0; i < chain_size; i++) {
596 CheckedDeleteKey(&key_blob_list[i]);
597 }
598}
599
600/*
David Drysdalee60248c2021-10-04 12:54:13 +0100601 * AttestKeyTest.EcAttestKeyMultiPurposeFail
602 *
603 * This test attempts to create an EC attestation key that also allows signing.
604 */
605TEST_P(AttestKeyTest, EcAttestKeyMultiPurposeFail) {
David Drysdale50a66b82022-03-21 15:21:32 +0000606 if (AidlVersion() < 2) {
607 // The KeyMint v1 spec required that KeyPurpose::ATTEST_KEY not be combined
608 // with other key purposes. However, this was not checked at the time
609 // so we can only be strict about checking this for implementations of KeyMint
610 // version 2 and above.
611 GTEST_SKIP() << "Single-purpose for KeyPurpose::ATTEST_KEY only strict since KeyMint v2";
612 }
David Drysdalee60248c2021-10-04 12:54:13 +0100613 vector<uint8_t> attest_key_blob;
614 vector<KeyCharacteristics> attest_key_characteristics;
615 vector<Certificate> attest_key_cert_chain;
616 ASSERT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE,
617 GenerateKey(AuthorizationSetBuilder()
618 .EcdsaSigningKey(EcCurve::P_256)
619 .AttestKey()
620 .SetDefaultValidity(),
621 {} /* attestation signing key */, &attest_key_blob,
622 &attest_key_characteristics, &attest_key_cert_chain));
623}
624
625/*
Selene Huang8f9494c2021-04-21 15:10:36 -0700626 * AttestKeyTest.AlternateAttestKeyChaining
627 *
628 * This test creates a chain of multiple attest keys, in the order Ec - RSA - Ec - RSA ....
629 * Each attest key is used to sign the next attest key, with the last attest key signed by
630 * the factory chain. This is to verify different algorithms of attest keys can
631 * cross sign each other and be chained together.
632 */
633TEST_P(AttestKeyTest, AlternateAttestKeyChaining) {
634 const int chain_size = 6;
635 vector<vector<uint8_t>> key_blob_list(chain_size);
636 vector<vector<Certificate>> cert_chain_list(chain_size);
637
638 for (int i = 0; i < chain_size; i++) {
639 string sub = "Alt attest key chaining ";
640 char index = '1' + i;
641 string subject = sub + index;
642 vector<uint8_t> subject_der(make_name_from_str(subject));
643
644 uint64_t serial_int = 90000000 + i;
645 vector<uint8_t> serial_blob(build_serial_blob(serial_int));
646
647 vector<KeyCharacteristics> attested_key_characteristics;
648 AttestationKey attest_key;
649 optional<AttestationKey> attest_key_opt;
650
651 if (i > 0) {
652 attest_key.issuerSubjectName = make_name_from_str(sub + (char)(index - 1));
653 attest_key.keyBlob = key_blob_list[i - 1];
654 attest_key_opt = attest_key;
655 }
subrahmanyaman05642492022-02-05 07:10:56 +0000656 ErrorCode result;
Selene Huang8f9494c2021-04-21 15:10:36 -0700657 if ((i & 0x1) == 1) {
David Drysdale43489272022-06-13 10:12:12 +0100658 result = GenerateAttestKey(AuthorizationSetBuilder()
659 .EcdsaKey(EcCurve::P_256)
660 .AttestKey()
661 .AttestationChallenge("foo")
662 .AttestationApplicationId("bar")
663 .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob)
664 .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der)
665 .Authorization(TAG_NO_AUTH_REQUIRED)
666 .SetDefaultValidity(),
667 attest_key_opt, &key_blob_list[i],
668 &attested_key_characteristics, &cert_chain_list[i]);
Selene Huang8f9494c2021-04-21 15:10:36 -0700669 } else {
David Drysdale43489272022-06-13 10:12:12 +0100670 result = GenerateAttestKey(AuthorizationSetBuilder()
671 .RsaKey(2048, 65537)
672 .AttestKey()
673 .AttestationChallenge("foo")
674 .AttestationApplicationId("bar")
675 .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob)
676 .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der)
677 .Authorization(TAG_NO_AUTH_REQUIRED)
678 .SetDefaultValidity(),
679 attest_key_opt, &key_blob_list[i],
680 &attested_key_characteristics, &cert_chain_list[i]);
Selene Huang8f9494c2021-04-21 15:10:36 -0700681 }
subrahmanyaman05642492022-02-05 07:10:56 +0000682 // Strongbox may not support factory provisioned attestation key.
683 if (SecLevel() == SecurityLevel::STRONGBOX) {
684 if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return;
685 }
686 ASSERT_EQ(ErrorCode::OK, result);
Selene Huang8f9494c2021-04-21 15:10:36 -0700687
688 AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics);
689 AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics);
David Drysdalea0386952021-08-05 07:50:23 +0100690 ASSERT_GT(cert_chain_list[i].size(), 0);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000691 EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced,
692 SecLevel(),
Selene Huang8f9494c2021-04-21 15:10:36 -0700693 cert_chain_list[i][0].encodedCertificate));
694
695 if (i > 0) {
696 /*
697 * The first key is attestated with factory chain, but all the rest of the keys are
698 * not supposed to be returned in attestation certificate chains.
699 */
700 EXPECT_FALSE(ChainSignaturesAreValid(cert_chain_list[i]));
701
702 // Appending the attest_key chain to the attested_key_chain should yield a valid chain.
703 cert_chain_list[i].insert(cert_chain_list[i].end(), //
704 cert_chain_list[i - 1].begin(), //
705 cert_chain_list[i - 1].end());
706 }
707
708 EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_list[i]));
709 EXPECT_GT(cert_chain_list[i].size(), i + 1);
710 verify_subject_and_serial(cert_chain_list[i][0], serial_int, subject, false);
711 }
712
713 for (int i = 0; i < chain_size; i++) {
714 CheckedDeleteKey(&key_blob_list[i]);
715 }
716}
717
David Drysdaled2cc8c22021-04-15 13:29:45 +0100718TEST_P(AttestKeyTest, MissingChallenge) {
719 for (auto size : ValidKeySizes(Algorithm::RSA)) {
720 /*
721 * Create attestation key.
722 */
723 AttestationKey attest_key;
724 vector<KeyCharacteristics> attest_key_characteristics;
725 vector<Certificate> attest_key_cert_chain;
David Drysdale43489272022-06-13 10:12:12 +0100726 ASSERT_EQ(ErrorCode::OK,
727 GenerateAttestKey(AuthorizationSetBuilder()
728 .RsaKey(size, 65537)
729 .AttestKey()
730 .SetDefaultValidity(),
731 {} /* attestation signing key */, &attest_key.keyBlob,
732 &attest_key_characteristics, &attest_key_cert_chain));
David Drysdaled2cc8c22021-04-15 13:29:45 +0100733
734 EXPECT_EQ(attest_key_cert_chain.size(), 1);
735 EXPECT_TRUE(IsSelfSigned(attest_key_cert_chain)) << "Failed on size " << size;
736
737 /*
738 * Use attestation key to sign RSA / ECDSA key but forget to provide a challenge
739 */
740 attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key");
741 vector<uint8_t> attested_key_blob;
742 vector<KeyCharacteristics> attested_key_characteristics;
743 vector<Certificate> attested_key_cert_chain;
Tommy Chiuc93c4392021-05-11 18:36:50 +0800744 EXPECT_EQ(ErrorCode::ATTESTATION_CHALLENGE_MISSING,
David Drysdaled2cc8c22021-04-15 13:29:45 +0100745 GenerateKey(AuthorizationSetBuilder()
746 .RsaSigningKey(2048, 65537)
747 .Authorization(TAG_NO_AUTH_REQUIRED)
748 .AttestationApplicationId("bar")
749 .SetDefaultValidity(),
750 attest_key, &attested_key_blob, &attested_key_characteristics,
751 &attested_key_cert_chain));
752
Tommy Chiuc93c4392021-05-11 18:36:50 +0800753 EXPECT_EQ(ErrorCode::ATTESTATION_CHALLENGE_MISSING,
David Drysdaled2cc8c22021-04-15 13:29:45 +0100754 GenerateKey(AuthorizationSetBuilder()
755 .EcdsaSigningKey(EcCurve::P_256)
756 .Authorization(TAG_NO_AUTH_REQUIRED)
757 .AttestationApplicationId("bar")
758 .SetDefaultValidity(),
759 attest_key, &attested_key_blob, &attested_key_characteristics,
760 &attested_key_cert_chain));
761
762 CheckedDeleteKey(&attest_key.keyBlob);
763 }
764}
765
Shawn Willden7c130392020-12-21 09:58:22 -0700766TEST_P(AttestKeyTest, AllEcCurves) {
767 for (auto curve : ValidCurves()) {
768 /*
David Drysdale7de9feb2021-03-05 14:56:19 +0000769 * Create attestation key.
Shawn Willden7c130392020-12-21 09:58:22 -0700770 */
771 AttestationKey attest_key;
772 vector<KeyCharacteristics> attest_key_characteristics;
773 vector<Certificate> attest_key_cert_chain;
David Drysdaleb3b12142021-11-01 17:13:27 +0000774 ASSERT_EQ(
775 ErrorCode::OK,
David Drysdale43489272022-06-13 10:12:12 +0100776 GenerateAttestKey(
David Drysdaleb3b12142021-11-01 17:13:27 +0000777 AuthorizationSetBuilder().EcdsaKey(curve).AttestKey().SetDefaultValidity(),
778 {} /* attestation signing key */, &attest_key.keyBlob,
779 &attest_key_characteristics, &attest_key_cert_chain));
Shawn Willden7c130392020-12-21 09:58:22 -0700780
Shawn Willdenc410f6f2021-04-22 13:28:45 -0600781 ASSERT_GT(attest_key_cert_chain.size(), 0);
Shawn Willden7c130392020-12-21 09:58:22 -0700782 EXPECT_EQ(attest_key_cert_chain.size(), 1);
783 EXPECT_TRUE(IsSelfSigned(attest_key_cert_chain)) << "Failed on curve " << curve;
784
785 /*
786 * Use attestation key to sign RSA key
787 */
788 attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key");
789 vector<uint8_t> attested_key_blob;
790 vector<KeyCharacteristics> attested_key_characteristics;
791 vector<Certificate> attested_key_cert_chain;
792 EXPECT_EQ(ErrorCode::OK,
793 GenerateKey(AuthorizationSetBuilder()
794 .RsaSigningKey(2048, 65537)
795 .Authorization(TAG_NO_AUTH_REQUIRED)
796 .AttestationChallenge("foo")
797 .AttestationApplicationId("bar")
798 .SetDefaultValidity(),
799 attest_key, &attested_key_blob, &attested_key_characteristics,
800 &attested_key_cert_chain));
801
Brian J Murrayaa8a7582021-12-06 22:13:50 -0800802 ASSERT_GT(attested_key_cert_chain.size(), 0);
Shawn Willden7c130392020-12-21 09:58:22 -0700803 CheckedDeleteKey(&attested_key_blob);
804
805 AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics);
806 AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000807 EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced,
808 SecLevel(),
Shawn Willden7c130392020-12-21 09:58:22 -0700809 attested_key_cert_chain[0].encodedCertificate));
810
811 // Attestation by itself is not valid (last entry is not self-signed).
812 EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain));
813
814 // Appending the attest_key chain to the attested_key_chain should yield a valid chain.
815 if (attest_key_cert_chain.size() > 0) {
816 attested_key_cert_chain.push_back(attest_key_cert_chain[0]);
817 }
818 EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain));
819
820 /*
821 * Use attestation key to sign EC key
822 */
823 EXPECT_EQ(ErrorCode::OK,
824 GenerateKey(AuthorizationSetBuilder()
825 .EcdsaSigningKey(EcCurve::P_256)
826 .Authorization(TAG_NO_AUTH_REQUIRED)
827 .AttestationChallenge("foo")
828 .AttestationApplicationId("bar")
829 .SetDefaultValidity(),
830 attest_key, &attested_key_blob, &attested_key_characteristics,
831 &attested_key_cert_chain));
832
Brian J Murrayaa8a7582021-12-06 22:13:50 -0800833 ASSERT_GT(attested_key_cert_chain.size(), 0);
Shawn Willden7c130392020-12-21 09:58:22 -0700834 CheckedDeleteKey(&attested_key_blob);
835 CheckedDeleteKey(&attest_key.keyBlob);
836
837 hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics);
838 sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000839 EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced,
840 SecLevel(),
Shawn Willden7c130392020-12-21 09:58:22 -0700841 attested_key_cert_chain[0].encodedCertificate));
842
843 // Attestation by itself is not valid (last entry is not self-signed).
844 EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain));
845
846 // Appending the attest_key chain to the attested_key_chain should yield a valid chain.
847 if (attest_key_cert_chain.size() > 0) {
848 attested_key_cert_chain.push_back(attest_key_cert_chain[0]);
849 }
850 EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain));
851
852 // Bail early if anything failed.
853 if (HasFailure()) return;
854 }
855}
856
Shawn Willden7bbf6292021-04-01 12:57:21 -0600857TEST_P(AttestKeyTest, AttestWithNonAttestKey) {
David Drysdale7de9feb2021-03-05 14:56:19 +0000858 // Create non-attestation key.
Shawn Willden7bbf6292021-04-01 12:57:21 -0600859 AttestationKey non_attest_key;
860 vector<KeyCharacteristics> non_attest_key_characteristics;
861 vector<Certificate> non_attest_key_cert_chain;
862 ASSERT_EQ(
863 ErrorCode::OK,
864 GenerateKey(
865 AuthorizationSetBuilder().EcdsaSigningKey(EcCurve::P_256).SetDefaultValidity(),
David Drysdalea676c3b2021-06-14 14:46:02 +0100866 {} /* attestation signing key */, &non_attest_key.keyBlob,
Shawn Willden7bbf6292021-04-01 12:57:21 -0600867 &non_attest_key_characteristics, &non_attest_key_cert_chain));
868
Shawn Willdenc410f6f2021-04-22 13:28:45 -0600869 ASSERT_GT(non_attest_key_cert_chain.size(), 0);
Shawn Willden7bbf6292021-04-01 12:57:21 -0600870 EXPECT_EQ(non_attest_key_cert_chain.size(), 1);
871 EXPECT_TRUE(IsSelfSigned(non_attest_key_cert_chain));
872
873 // Attempt to sign attestation with non-attest key.
874 vector<uint8_t> attested_key_blob;
875 vector<KeyCharacteristics> attested_key_characteristics;
876 vector<Certificate> attested_key_cert_chain;
877 EXPECT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE,
878 GenerateKey(AuthorizationSetBuilder()
879 .EcdsaSigningKey(EcCurve::P_256)
880 .Authorization(TAG_NO_AUTH_REQUIRED)
881 .AttestationChallenge("foo")
882 .AttestationApplicationId("bar")
883 .SetDefaultValidity(),
884 non_attest_key, &attested_key_blob, &attested_key_characteristics,
885 &attested_key_cert_chain));
886}
887
David Drysdalea676c3b2021-06-14 14:46:02 +0100888TEST_P(AttestKeyTest, EcdsaAttestationID) {
David Drysdale555ba002022-05-03 18:48:57 +0100889 if (is_gsi_image()) {
890 // GSI sets up a standard set of device identifiers that may not match
891 // the device identifiers held by the device.
892 GTEST_SKIP() << "Test not applicable under GSI";
893 }
David Drysdalea676c3b2021-06-14 14:46:02 +0100894 // Create attestation key.
895 AttestationKey attest_key;
896 vector<KeyCharacteristics> attest_key_characteristics;
897 vector<Certificate> attest_key_cert_chain;
David Drysdale43489272022-06-13 10:12:12 +0100898 ASSERT_EQ(ErrorCode::OK,
899 GenerateAttestKey(AuthorizationSetBuilder()
900 .EcdsaKey(EcCurve::P_256)
901 .AttestKey()
902 .SetDefaultValidity(),
903 {} /* attestation signing key */, &attest_key.keyBlob,
904 &attest_key_characteristics, &attest_key_cert_chain));
David Drysdalea676c3b2021-06-14 14:46:02 +0100905 attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key");
906 ASSERT_GT(attest_key_cert_chain.size(), 0);
907 EXPECT_EQ(attest_key_cert_chain.size(), 1);
908 EXPECT_TRUE(IsSelfSigned(attest_key_cert_chain));
909
910 // Collection of valid attestation ID tags.
911 auto attestation_id_tags = AuthorizationSetBuilder();
Prashant Patil8d779bf2022-09-28 16:09:29 +0100912 // Use ro.product.brand_for_attestation property for attestation if it is present else fallback
913 // to ro.product.brand
914 std::string prop_value =
915 ::android::base::GetProperty("ro.product.brand_for_attestation", /* default= */ "");
916 if (!prop_value.empty()) {
917 add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_BRAND,
918 "ro.product.brand_for_attestation");
919 } else {
920 add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_BRAND, "ro.product.brand");
921 }
David Drysdalea676c3b2021-06-14 14:46:02 +0100922 add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_DEVICE, "ro.product.device");
Prashant Patil8d779bf2022-09-28 16:09:29 +0100923 // Use ro.product.name_for_attestation property for attestation if it is present else fallback
924 // to ro.product.name
925 prop_value = ::android::base::GetProperty("ro.product.name_for_attestation", /* default= */ "");
926 if (!prop_value.empty()) {
927 add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_PRODUCT,
928 "ro.product.name_for_attestation");
929 } else {
930 add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_PRODUCT, "ro.product.name");
931 }
Tri Vo799e4352022-11-07 17:23:50 -0800932 add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_SERIAL, "ro.serialno");
David Drysdalea676c3b2021-06-14 14:46:02 +0100933 add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_MANUFACTURER,
934 "ro.product.manufacturer");
Prashant Patil8d779bf2022-09-28 16:09:29 +0100935 // Use ro.product.model_for_attestation property for attestation if it is present else fallback
936 // to ro.product.model
937 prop_value =
938 ::android::base::GetProperty("ro.product.model_for_attestation", /* default= */ "");
939 if (!prop_value.empty()) {
940 add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_MODEL,
941 "ro.product.model_for_attestation");
942 } else {
943 add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_MODEL, "ro.product.model");
944 }
David Drysdalea676c3b2021-06-14 14:46:02 +0100945
Rajesh Nyamagoudeb644cf2022-12-15 03:50:52 +0000946 string imei = get_imei(0);
947 if (!imei.empty()) {
948 attestation_id_tags.Authorization(TAG_ATTESTATION_ID_IMEI, imei.data(), imei.size());
949 }
950
David Drysdalea676c3b2021-06-14 14:46:02 +0100951 for (const KeyParameter& tag : attestation_id_tags) {
952 SCOPED_TRACE(testing::Message() << "+tag-" << tag);
953 // Use attestation key to sign an ECDSA key, but include an attestation ID field.
954 AuthorizationSetBuilder builder = AuthorizationSetBuilder()
955 .EcdsaSigningKey(EcCurve::P_256)
956 .Authorization(TAG_NO_AUTH_REQUIRED)
957 .AttestationChallenge("challenge")
958 .AttestationApplicationId("foo")
959 .SetDefaultValidity();
960 builder.push_back(tag);
961 vector<uint8_t> attested_key_blob;
962 vector<KeyCharacteristics> attested_key_characteristics;
963 vector<Certificate> attested_key_cert_chain;
964 auto result = GenerateKey(builder, attest_key, &attested_key_blob,
965 &attested_key_characteristics, &attested_key_cert_chain);
Prashant Patil88ad1892022-03-15 16:31:02 +0000966 if (result == ErrorCode::CANNOT_ATTEST_IDS && !isDeviceIdAttestationRequired()) {
David Drysdalea676c3b2021-06-14 14:46:02 +0100967 continue;
968 }
969
970 ASSERT_EQ(result, ErrorCode::OK);
971
972 CheckedDeleteKey(&attested_key_blob);
973
974 AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics);
975 AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics);
976
977 // The attested key characteristics will not contain APPLICATION_ID_* fields (their
978 // spec definitions all have "Must never appear in KeyCharacteristics"), but the
979 // attestation extension should contain them, so make sure the extra tag is added.
980 hw_enforced.push_back(tag);
981
David Drysdale7dff4fc2021-12-10 10:10:52 +0000982 EXPECT_TRUE(verify_attestation_record(AidlVersion(), "challenge", "foo", sw_enforced,
983 hw_enforced, SecLevel(),
David Drysdalea676c3b2021-06-14 14:46:02 +0100984 attested_key_cert_chain[0].encodedCertificate));
985 }
986 CheckedDeleteKey(&attest_key.keyBlob);
987}
988
989TEST_P(AttestKeyTest, EcdsaAttestationMismatchID) {
990 // Create attestation key.
991 AttestationKey attest_key;
992 vector<KeyCharacteristics> attest_key_characteristics;
993 vector<Certificate> attest_key_cert_chain;
David Drysdale43489272022-06-13 10:12:12 +0100994 ASSERT_EQ(ErrorCode::OK,
995 GenerateAttestKey(AuthorizationSetBuilder()
996 .EcdsaKey(EcCurve::P_256)
997 .AttestKey()
998 .SetDefaultValidity(),
999 {} /* attestation signing key */, &attest_key.keyBlob,
1000 &attest_key_characteristics, &attest_key_cert_chain));
David Drysdalea676c3b2021-06-14 14:46:02 +01001001 attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key");
1002 ASSERT_GT(attest_key_cert_chain.size(), 0);
1003 EXPECT_EQ(attest_key_cert_chain.size(), 1);
1004 EXPECT_TRUE(IsSelfSigned(attest_key_cert_chain));
1005
1006 // Collection of invalid attestation ID tags.
1007 auto attestation_id_tags =
1008 AuthorizationSetBuilder()
1009 .Authorization(TAG_ATTESTATION_ID_BRAND, "bogus-brand")
1010 .Authorization(TAG_ATTESTATION_ID_DEVICE, "devious-device")
1011 .Authorization(TAG_ATTESTATION_ID_PRODUCT, "punctured-product")
1012 .Authorization(TAG_ATTESTATION_ID_SERIAL, "suspicious-serial")
1013 .Authorization(TAG_ATTESTATION_ID_IMEI, "invalid-imei")
1014 .Authorization(TAG_ATTESTATION_ID_MEID, "mismatching-meid")
1015 .Authorization(TAG_ATTESTATION_ID_MANUFACTURER, "malformed-manufacturer")
1016 .Authorization(TAG_ATTESTATION_ID_MODEL, "malicious-model");
1017 vector<uint8_t> key_blob;
1018 vector<KeyCharacteristics> key_characteristics;
1019
1020 for (const KeyParameter& invalid_tag : attestation_id_tags) {
1021 SCOPED_TRACE(testing::Message() << "+tag-" << invalid_tag);
1022
1023 // Use attestation key to sign an ECDSA key, but include an invalid
1024 // attestation ID field.
1025 AuthorizationSetBuilder builder = AuthorizationSetBuilder()
1026 .EcdsaSigningKey(EcCurve::P_256)
1027 .Authorization(TAG_NO_AUTH_REQUIRED)
1028 .AttestationChallenge("challenge")
1029 .AttestationApplicationId("foo")
1030 .SetDefaultValidity();
1031 builder.push_back(invalid_tag);
1032 vector<uint8_t> attested_key_blob;
1033 vector<KeyCharacteristics> attested_key_characteristics;
1034 vector<Certificate> attested_key_cert_chain;
1035 auto result = GenerateKey(builder, attest_key, &attested_key_blob,
1036 &attested_key_characteristics, &attested_key_cert_chain);
1037
1038 ASSERT_TRUE(result == ErrorCode::CANNOT_ATTEST_IDS || result == ErrorCode::INVALID_TAG)
1039 << "result = " << result;
Max Biresa97ec692022-11-21 23:37:54 -08001040 device_id_attestation_vsr_check(result);
David Drysdalea676c3b2021-06-14 14:46:02 +01001041 }
1042 CheckedDeleteKey(&attest_key.keyBlob);
1043}
1044
Shawn Willden7c130392020-12-21 09:58:22 -07001045INSTANTIATE_KEYMINT_AIDL_TEST(AttestKeyTest);
1046
1047} // namespace aidl::android::hardware::security::keymint::test