Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1 | /* |
| 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" |
| 18 | #include <cutils/log.h> |
David Drysdale | cdfb961 | 2022-06-13 10:12:12 +0100 | [diff] [blame] | 19 | #include <cutils/properties.h> |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 20 | |
| 21 | #include <keymint_support/key_param_output.h> |
| 22 | #include <keymint_support/openssl_utils.h> |
| 23 | |
| 24 | #include "KeyMintAidlTestBase.h" |
| 25 | |
| 26 | namespace aidl::android::hardware::security::keymint::test { |
| 27 | |
| 28 | namespace { |
| 29 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 30 | bool IsSelfSigned(const vector<Certificate>& chain) { |
| 31 | if (chain.size() != 1) return false; |
| 32 | return ChainSignaturesAreValid(chain); |
| 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
David Drysdale | cdfb961 | 2022-06-13 10:12:12 +0100 | [diff] [blame] | 37 | class AttestKeyTest : public KeyMintAidlTestBase { |
| 38 | protected: |
| 39 | ErrorCode GenerateAttestKey(const AuthorizationSet& key_desc, |
| 40 | const optional<AttestationKey>& attest_key, |
| 41 | vector<uint8_t>* key_blob, |
| 42 | vector<KeyCharacteristics>* key_characteristics, |
| 43 | vector<Certificate>* cert_chain) { |
| 44 | // The original specification for KeyMint v1 required ATTEST_KEY not be combined |
| 45 | // with any other key purpose, but the original VTS tests incorrectly did exactly that. |
| 46 | // This means that a device that launched prior to Android T (API level 33) may |
| 47 | // accept or even require KeyPurpose::SIGN too. |
| 48 | if (property_get_int32("ro.board.first_api_level", 0) < 33) { |
| 49 | AuthorizationSet key_desc_plus_sign = key_desc; |
| 50 | key_desc_plus_sign.push_back(TAG_PURPOSE, KeyPurpose::SIGN); |
| 51 | |
| 52 | auto result = GenerateKey(key_desc_plus_sign, attest_key, key_blob, key_characteristics, |
| 53 | cert_chain); |
| 54 | if (result == ErrorCode::OK) { |
| 55 | return result; |
| 56 | } |
| 57 | // If the key generation failed, it may be because the device is (correctly) |
| 58 | // rejecting the combination of ATTEST_KEY+SIGN. Fall through to try again with |
| 59 | // just ATTEST_KEY. |
| 60 | } |
| 61 | return GenerateKey(key_desc, attest_key, key_blob, key_characteristics, cert_chain); |
| 62 | } |
| 63 | }; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 64 | |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 65 | /* |
| 66 | * AttestKeyTest.AllRsaSizes |
| 67 | * |
| 68 | * This test creates self signed RSA attestation keys of various sizes, and verify they can be |
| 69 | * used to sign other RSA and EC keys. |
| 70 | */ |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 71 | TEST_P(AttestKeyTest, AllRsaSizes) { |
| 72 | for (auto size : ValidKeySizes(Algorithm::RSA)) { |
| 73 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 74 | * Create attestation key. |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 75 | */ |
| 76 | AttestationKey attest_key; |
| 77 | vector<KeyCharacteristics> attest_key_characteristics; |
| 78 | vector<Certificate> attest_key_cert_chain; |
David Drysdale | cdfb961 | 2022-06-13 10:12:12 +0100 | [diff] [blame] | 79 | ASSERT_EQ(ErrorCode::OK, |
| 80 | GenerateAttestKey(AuthorizationSetBuilder() |
| 81 | .RsaKey(size, 65537) |
| 82 | .AttestKey() |
| 83 | .SetDefaultValidity(), |
| 84 | {} /* attestation signing key */, &attest_key.keyBlob, |
| 85 | &attest_key_characteristics, &attest_key_cert_chain)); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 86 | |
Shawn Willden | c410f6f | 2021-04-22 13:28:45 -0600 | [diff] [blame] | 87 | ASSERT_GT(attest_key_cert_chain.size(), 0); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 88 | EXPECT_EQ(attest_key_cert_chain.size(), 1); |
| 89 | EXPECT_TRUE(IsSelfSigned(attest_key_cert_chain)) << "Failed on size " << size; |
| 90 | |
| 91 | /* |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 92 | * Use attestation key to sign RSA signing key |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 93 | */ |
| 94 | attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
| 95 | vector<uint8_t> attested_key_blob; |
| 96 | vector<KeyCharacteristics> attested_key_characteristics; |
| 97 | vector<Certificate> attested_key_cert_chain; |
| 98 | EXPECT_EQ(ErrorCode::OK, |
| 99 | GenerateKey(AuthorizationSetBuilder() |
| 100 | .RsaSigningKey(2048, 65537) |
| 101 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 102 | .AttestationChallenge("foo") |
| 103 | .AttestationApplicationId("bar") |
| 104 | .SetDefaultValidity(), |
| 105 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 106 | &attested_key_cert_chain)); |
| 107 | |
| 108 | CheckedDeleteKey(&attested_key_blob); |
| 109 | |
| 110 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 111 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 112 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced, |
| 113 | SecLevel(), |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 114 | attested_key_cert_chain[0].encodedCertificate)); |
| 115 | |
| 116 | // Attestation by itself is not valid (last entry is not self-signed). |
| 117 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 118 | |
| 119 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 120 | attested_key_cert_chain.push_back(attest_key_cert_chain[0]); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 121 | EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain)); |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 122 | EXPECT_EQ(attested_key_cert_chain.size(), 2); |
| 123 | |
| 124 | /* |
| 125 | * Use attestation key to sign RSA decryption key |
| 126 | */ |
| 127 | attested_key_characteristics.resize(0); |
| 128 | attested_key_cert_chain.resize(0); |
| 129 | EXPECT_EQ(ErrorCode::OK, |
| 130 | GenerateKey(AuthorizationSetBuilder() |
| 131 | .RsaEncryptionKey(2048, 65537) |
| 132 | .Digest(Digest::NONE) |
| 133 | .Padding(PaddingMode::NONE) |
| 134 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 135 | .AttestationChallenge("foo2") |
| 136 | .AttestationApplicationId("bar2") |
| 137 | .SetDefaultValidity(), |
| 138 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 139 | &attested_key_cert_chain)); |
| 140 | |
| 141 | CheckedDeleteKey(&attested_key_blob); |
| 142 | |
| 143 | hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 144 | sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 145 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo2", "bar2", sw_enforced, |
| 146 | hw_enforced, SecLevel(), |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 147 | attested_key_cert_chain[0].encodedCertificate)); |
| 148 | |
| 149 | // Attestation by itself is not valid (last entry is not self-signed). |
| 150 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 151 | |
| 152 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 153 | attested_key_cert_chain.push_back(attest_key_cert_chain[0]); |
| 154 | EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 155 | EXPECT_EQ(attested_key_cert_chain.size(), 2); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 156 | |
| 157 | /* |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 158 | * Use attestation key to sign EC key. Specify a CREATION_DATETIME for this one. |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 159 | */ |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 160 | attested_key_characteristics.resize(0); |
| 161 | attested_key_cert_chain.resize(0); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 162 | uint64_t timestamp = 1619621648000; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 163 | EXPECT_EQ(ErrorCode::OK, |
| 164 | GenerateKey(AuthorizationSetBuilder() |
| 165 | .EcdsaSigningKey(EcCurve::P_256) |
| 166 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 167 | .AttestationChallenge("foo") |
| 168 | .AttestationApplicationId("bar") |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 169 | .Authorization(TAG_CREATION_DATETIME, timestamp) |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 170 | .SetDefaultValidity(), |
| 171 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 172 | &attested_key_cert_chain)); |
| 173 | |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 174 | // The returned key characteristics will include CREATION_DATETIME (checked below) |
| 175 | // in SecurityLevel::KEYSTORE; this will be stripped out in the CheckCharacteristics() |
| 176 | // call below, to match what getKeyCharacteristics() returns (which doesn't include |
| 177 | // any SecurityLevel::KEYSTORE characteristics). |
| 178 | CheckCharacteristics(attested_key_blob, attested_key_characteristics); |
| 179 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 180 | CheckedDeleteKey(&attested_key_blob); |
| 181 | CheckedDeleteKey(&attest_key.keyBlob); |
| 182 | |
| 183 | hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 184 | sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 185 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 186 | // The client-specified CREATION_DATETIME should be in sw_enforced. |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 187 | // Its presence will also trigger verify_attestation_record() to check that |
| 188 | // it is in the attestation extension with a matching value. |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 189 | EXPECT_TRUE(sw_enforced.Contains(TAG_CREATION_DATETIME, timestamp)) |
| 190 | << "expected CREATION_TIMESTAMP in sw_enforced:" << sw_enforced |
| 191 | << " not in hw_enforced:" << hw_enforced; |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 192 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced, |
| 193 | SecLevel(), |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 194 | attested_key_cert_chain[0].encodedCertificate)); |
| 195 | |
| 196 | // Attestation by itself is not valid (last entry is not self-signed). |
| 197 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 198 | |
| 199 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 200 | attested_key_cert_chain.push_back(attest_key_cert_chain[0]); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 201 | EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 202 | |
| 203 | // Bail early if anything failed. |
| 204 | if (HasFailure()) return; |
| 205 | } |
| 206 | } |
| 207 | |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 208 | /* |
David Drysdale | e60248c | 2021-10-04 12:54:13 +0100 | [diff] [blame] | 209 | * AttestKeyTest.RsaAttestKeyMultiPurposeFail |
| 210 | * |
| 211 | * This test attempts to create an RSA attestation key that also allows signing. |
| 212 | */ |
| 213 | TEST_P(AttestKeyTest, RsaAttestKeyMultiPurposeFail) { |
David Drysdale | 50a66b8 | 2022-03-21 15:21:32 +0000 | [diff] [blame] | 214 | if (AidlVersion() < 2) { |
| 215 | // The KeyMint v1 spec required that KeyPurpose::ATTEST_KEY not be combined |
| 216 | // with other key purposes. However, this was not checked at the time |
| 217 | // so we can only be strict about checking this for implementations of KeyMint |
| 218 | // version 2 and above. |
| 219 | GTEST_SKIP() << "Single-purpose for KeyPurpose::ATTEST_KEY only strict since KeyMint v2"; |
| 220 | } |
| 221 | |
David Drysdale | e60248c | 2021-10-04 12:54:13 +0100 | [diff] [blame] | 222 | vector<uint8_t> attest_key_blob; |
| 223 | vector<KeyCharacteristics> attest_key_characteristics; |
| 224 | vector<Certificate> attest_key_cert_chain; |
| 225 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 226 | GenerateKey(AuthorizationSetBuilder() |
| 227 | .RsaSigningKey(2048, 65537) |
| 228 | .AttestKey() |
| 229 | .SetDefaultValidity(), |
| 230 | {} /* attestation signing key */, &attest_key_blob, |
| 231 | &attest_key_characteristics, &attest_key_cert_chain)); |
| 232 | } |
| 233 | |
| 234 | /* |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 235 | * AttestKeyTest.RsaAttestedAttestKeys |
| 236 | * |
| 237 | * This test creates an RSA attestation key signed by factory keys, and varifies it can be |
| 238 | * used to sign other RSA and EC keys. |
| 239 | */ |
| 240 | TEST_P(AttestKeyTest, RsaAttestedAttestKeys) { |
| 241 | auto challenge = "hello"; |
| 242 | auto app_id = "foo"; |
| 243 | |
| 244 | auto subject = "cert subj 2"; |
| 245 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 246 | |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 247 | // An X.509 certificate serial number SHOULD be >0, but this is not policed. Check |
| 248 | // that a zero value doesn't cause problems. |
| 249 | uint64_t serial_int = 0; |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 250 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 251 | |
| 252 | /* |
| 253 | * Create attestation key. |
| 254 | */ |
| 255 | AttestationKey attest_key; |
| 256 | vector<KeyCharacteristics> attest_key_characteristics; |
| 257 | vector<Certificate> attest_key_cert_chain; |
David Drysdale | cdfb961 | 2022-06-13 10:12:12 +0100 | [diff] [blame] | 258 | auto result = GenerateAttestKey(AuthorizationSetBuilder() |
| 259 | .RsaKey(2048, 65537) |
| 260 | .AttestKey() |
| 261 | .AttestationChallenge(challenge) |
| 262 | .AttestationApplicationId(app_id) |
| 263 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 264 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 265 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 266 | .SetDefaultValidity(), |
| 267 | {} /* attestation signing key */, &attest_key.keyBlob, |
| 268 | &attest_key_characteristics, &attest_key_cert_chain); |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 269 | // Strongbox may not support factory provisioned attestation key. |
| 270 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 271 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 272 | } |
| 273 | ASSERT_EQ(ErrorCode::OK, result); |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 274 | |
| 275 | EXPECT_GT(attest_key_cert_chain.size(), 1); |
| 276 | verify_subject_and_serial(attest_key_cert_chain[0], serial_int, subject, false); |
| 277 | EXPECT_TRUE(ChainSignaturesAreValid(attest_key_cert_chain)); |
| 278 | |
| 279 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attest_key_characteristics); |
| 280 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attest_key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 281 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), challenge, app_id, // |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 282 | sw_enforced, hw_enforced, SecLevel(), |
| 283 | attest_key_cert_chain[0].encodedCertificate)); |
| 284 | |
| 285 | /* |
| 286 | * Use attestation key to sign RSA key |
| 287 | */ |
| 288 | attest_key.issuerSubjectName = subject_der; |
| 289 | vector<uint8_t> attested_key_blob; |
| 290 | vector<KeyCharacteristics> attested_key_characteristics; |
| 291 | vector<Certificate> attested_key_cert_chain; |
| 292 | |
| 293 | auto subject2 = "cert subject"; |
| 294 | vector<uint8_t> subject_der2(make_name_from_str(subject2)); |
| 295 | |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 296 | uint64_t serial_int2 = 255; |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 297 | vector<uint8_t> serial_blob2(build_serial_blob(serial_int2)); |
| 298 | |
| 299 | EXPECT_EQ(ErrorCode::OK, |
| 300 | GenerateKey(AuthorizationSetBuilder() |
| 301 | .RsaSigningKey(2048, 65537) |
| 302 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 303 | .AttestationChallenge("foo") |
| 304 | .AttestationApplicationId("bar") |
| 305 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob2) |
| 306 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der2) |
| 307 | .SetDefaultValidity(), |
| 308 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 309 | &attested_key_cert_chain)); |
| 310 | |
| 311 | CheckedDeleteKey(&attested_key_blob); |
| 312 | CheckedDeleteKey(&attest_key.keyBlob); |
| 313 | |
| 314 | AuthorizationSet hw_enforced2 = HwEnforcedAuthorizations(attested_key_characteristics); |
| 315 | AuthorizationSet sw_enforced2 = SwEnforcedAuthorizations(attested_key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 316 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced2, hw_enforced2, |
| 317 | SecLevel(), |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 318 | attested_key_cert_chain[0].encodedCertificate)); |
| 319 | |
| 320 | // Attestation by itself is not valid (last entry is not self-signed). |
| 321 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 322 | |
| 323 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 324 | attested_key_cert_chain.insert(attested_key_cert_chain.end(), attest_key_cert_chain.begin(), |
| 325 | attest_key_cert_chain.end()); |
| 326 | |
| 327 | EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 328 | EXPECT_GT(attested_key_cert_chain.size(), 2); |
| 329 | verify_subject_and_serial(attested_key_cert_chain[0], serial_int2, subject2, false); |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * AttestKeyTest.RsaAttestKeyChaining |
| 334 | * |
| 335 | * This test creates a chain of multiple RSA attest keys, each used to sign the next attest key, |
| 336 | * with the last attest key signed by the factory chain. |
| 337 | */ |
| 338 | TEST_P(AttestKeyTest, RsaAttestKeyChaining) { |
| 339 | const int chain_size = 6; |
| 340 | vector<vector<uint8_t>> key_blob_list(chain_size); |
| 341 | vector<vector<Certificate>> cert_chain_list(chain_size); |
| 342 | |
| 343 | for (int i = 0; i < chain_size; i++) { |
| 344 | string sub = "attest key chaining "; |
| 345 | char index = '1' + i; |
| 346 | string subject = sub + index; |
| 347 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 348 | |
| 349 | uint64_t serial_int = 7000 + i; |
| 350 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 351 | |
| 352 | vector<KeyCharacteristics> attested_key_characteristics; |
| 353 | AttestationKey attest_key; |
| 354 | optional<AttestationKey> attest_key_opt; |
| 355 | |
| 356 | if (i > 0) { |
| 357 | attest_key.issuerSubjectName = make_name_from_str(sub + (char)(index - 1)); |
| 358 | attest_key.keyBlob = key_blob_list[i - 1]; |
| 359 | attest_key_opt = attest_key; |
| 360 | } |
| 361 | |
David Drysdale | cdfb961 | 2022-06-13 10:12:12 +0100 | [diff] [blame] | 362 | auto result = GenerateAttestKey(AuthorizationSetBuilder() |
| 363 | .RsaKey(2048, 65537) |
| 364 | .AttestKey() |
| 365 | .AttestationChallenge("foo") |
| 366 | .AttestationApplicationId("bar") |
| 367 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 368 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 369 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 370 | .SetDefaultValidity(), |
| 371 | attest_key_opt, &key_blob_list[i], |
| 372 | &attested_key_characteristics, &cert_chain_list[i]); |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 373 | // Strongbox may not support factory provisioned attestation key. |
| 374 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 375 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 376 | } |
| 377 | ASSERT_EQ(ErrorCode::OK, result); |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 378 | |
| 379 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 380 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
David Drysdale | a038695 | 2021-08-05 07:50:23 +0100 | [diff] [blame] | 381 | ASSERT_GT(cert_chain_list[i].size(), 0); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 382 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced, |
| 383 | SecLevel(), |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 384 | cert_chain_list[i][0].encodedCertificate)); |
| 385 | |
| 386 | if (i > 0) { |
| 387 | /* |
| 388 | * The first key is attestated with factory chain, but all the rest of the keys are |
| 389 | * not supposed to be returned in attestation certificate chains. |
| 390 | */ |
| 391 | EXPECT_FALSE(ChainSignaturesAreValid(cert_chain_list[i])); |
| 392 | |
| 393 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 394 | cert_chain_list[i].insert(cert_chain_list[i].end(), // |
| 395 | cert_chain_list[i - 1].begin(), // |
| 396 | cert_chain_list[i - 1].end()); |
| 397 | } |
| 398 | |
| 399 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_list[i])); |
| 400 | EXPECT_GT(cert_chain_list[i].size(), i + 1); |
| 401 | verify_subject_and_serial(cert_chain_list[i][0], serial_int, subject, false); |
| 402 | } |
| 403 | |
| 404 | for (int i = 0; i < chain_size; i++) { |
| 405 | CheckedDeleteKey(&key_blob_list[i]); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * AttestKeyTest.EcAttestKeyChaining |
| 411 | * |
| 412 | * This test creates a chain of multiple Ec attest keys, each used to sign the next attest key, |
| 413 | * with the last attest key signed by the factory chain. |
| 414 | */ |
| 415 | TEST_P(AttestKeyTest, EcAttestKeyChaining) { |
| 416 | const int chain_size = 6; |
| 417 | vector<vector<uint8_t>> key_blob_list(chain_size); |
| 418 | vector<vector<Certificate>> cert_chain_list(chain_size); |
| 419 | |
| 420 | for (int i = 0; i < chain_size; i++) { |
| 421 | string sub = "Ec attest key chaining "; |
| 422 | char index = '1' + i; |
| 423 | string subject = sub + index; |
| 424 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 425 | |
| 426 | uint64_t serial_int = 800000 + i; |
| 427 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 428 | |
| 429 | vector<KeyCharacteristics> attested_key_characteristics; |
| 430 | AttestationKey attest_key; |
| 431 | optional<AttestationKey> attest_key_opt; |
| 432 | |
| 433 | if (i > 0) { |
| 434 | attest_key.issuerSubjectName = make_name_from_str(sub + (char)(index - 1)); |
| 435 | attest_key.keyBlob = key_blob_list[i - 1]; |
| 436 | attest_key_opt = attest_key; |
| 437 | } |
| 438 | |
David Drysdale | cdfb961 | 2022-06-13 10:12:12 +0100 | [diff] [blame] | 439 | auto result = GenerateAttestKey(AuthorizationSetBuilder() |
| 440 | .EcdsaKey(EcCurve::P_256) |
| 441 | .AttestKey() |
| 442 | .AttestationChallenge("foo") |
| 443 | .AttestationApplicationId("bar") |
| 444 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 445 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 446 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 447 | .SetDefaultValidity(), |
| 448 | attest_key_opt, &key_blob_list[i], |
| 449 | &attested_key_characteristics, &cert_chain_list[i]); |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 450 | // Strongbox may not support factory provisioned attestation key. |
| 451 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 452 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 453 | } |
| 454 | ASSERT_EQ(ErrorCode::OK, result); |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 455 | |
| 456 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 457 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
David Drysdale | a038695 | 2021-08-05 07:50:23 +0100 | [diff] [blame] | 458 | ASSERT_GT(cert_chain_list[i].size(), 0); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 459 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced, |
| 460 | SecLevel(), |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 461 | cert_chain_list[i][0].encodedCertificate)); |
| 462 | |
| 463 | if (i > 0) { |
| 464 | /* |
| 465 | * The first key is attestated with factory chain, but all the rest of the keys are |
| 466 | * not supposed to be returned in attestation certificate chains. |
| 467 | */ |
| 468 | EXPECT_FALSE(ChainSignaturesAreValid(cert_chain_list[i])); |
| 469 | |
| 470 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 471 | cert_chain_list[i].insert(cert_chain_list[i].end(), // |
| 472 | cert_chain_list[i - 1].begin(), // |
| 473 | cert_chain_list[i - 1].end()); |
| 474 | } |
| 475 | |
| 476 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_list[i])); |
| 477 | EXPECT_GT(cert_chain_list[i].size(), i + 1); |
| 478 | verify_subject_and_serial(cert_chain_list[i][0], serial_int, subject, false); |
| 479 | } |
| 480 | |
| 481 | for (int i = 0; i < chain_size; i++) { |
| 482 | CheckedDeleteKey(&key_blob_list[i]); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /* |
David Drysdale | e60248c | 2021-10-04 12:54:13 +0100 | [diff] [blame] | 487 | * AttestKeyTest.EcAttestKeyMultiPurposeFail |
| 488 | * |
| 489 | * This test attempts to create an EC attestation key that also allows signing. |
| 490 | */ |
| 491 | TEST_P(AttestKeyTest, EcAttestKeyMultiPurposeFail) { |
David Drysdale | 50a66b8 | 2022-03-21 15:21:32 +0000 | [diff] [blame] | 492 | if (AidlVersion() < 2) { |
| 493 | // The KeyMint v1 spec required that KeyPurpose::ATTEST_KEY not be combined |
| 494 | // with other key purposes. However, this was not checked at the time |
| 495 | // so we can only be strict about checking this for implementations of KeyMint |
| 496 | // version 2 and above. |
| 497 | GTEST_SKIP() << "Single-purpose for KeyPurpose::ATTEST_KEY only strict since KeyMint v2"; |
| 498 | } |
David Drysdale | e60248c | 2021-10-04 12:54:13 +0100 | [diff] [blame] | 499 | vector<uint8_t> attest_key_blob; |
| 500 | vector<KeyCharacteristics> attest_key_characteristics; |
| 501 | vector<Certificate> attest_key_cert_chain; |
| 502 | ASSERT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 503 | GenerateKey(AuthorizationSetBuilder() |
| 504 | .EcdsaSigningKey(EcCurve::P_256) |
| 505 | .AttestKey() |
| 506 | .SetDefaultValidity(), |
| 507 | {} /* attestation signing key */, &attest_key_blob, |
| 508 | &attest_key_characteristics, &attest_key_cert_chain)); |
| 509 | } |
| 510 | |
| 511 | /* |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 512 | * AttestKeyTest.AlternateAttestKeyChaining |
| 513 | * |
| 514 | * This test creates a chain of multiple attest keys, in the order Ec - RSA - Ec - RSA .... |
| 515 | * Each attest key is used to sign the next attest key, with the last attest key signed by |
| 516 | * the factory chain. This is to verify different algorithms of attest keys can |
| 517 | * cross sign each other and be chained together. |
| 518 | */ |
| 519 | TEST_P(AttestKeyTest, AlternateAttestKeyChaining) { |
| 520 | const int chain_size = 6; |
| 521 | vector<vector<uint8_t>> key_blob_list(chain_size); |
| 522 | vector<vector<Certificate>> cert_chain_list(chain_size); |
| 523 | |
| 524 | for (int i = 0; i < chain_size; i++) { |
| 525 | string sub = "Alt attest key chaining "; |
| 526 | char index = '1' + i; |
| 527 | string subject = sub + index; |
| 528 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 529 | |
| 530 | uint64_t serial_int = 90000000 + i; |
| 531 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 532 | |
| 533 | vector<KeyCharacteristics> attested_key_characteristics; |
| 534 | AttestationKey attest_key; |
| 535 | optional<AttestationKey> attest_key_opt; |
| 536 | |
| 537 | if (i > 0) { |
| 538 | attest_key.issuerSubjectName = make_name_from_str(sub + (char)(index - 1)); |
| 539 | attest_key.keyBlob = key_blob_list[i - 1]; |
| 540 | attest_key_opt = attest_key; |
| 541 | } |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 542 | ErrorCode result; |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 543 | if ((i & 0x1) == 1) { |
David Drysdale | cdfb961 | 2022-06-13 10:12:12 +0100 | [diff] [blame] | 544 | result = GenerateAttestKey(AuthorizationSetBuilder() |
| 545 | .EcdsaKey(EcCurve::P_256) |
| 546 | .AttestKey() |
| 547 | .AttestationChallenge("foo") |
| 548 | .AttestationApplicationId("bar") |
| 549 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 550 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 551 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 552 | .SetDefaultValidity(), |
| 553 | attest_key_opt, &key_blob_list[i], |
| 554 | &attested_key_characteristics, &cert_chain_list[i]); |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 555 | } else { |
David Drysdale | cdfb961 | 2022-06-13 10:12:12 +0100 | [diff] [blame] | 556 | result = GenerateAttestKey(AuthorizationSetBuilder() |
| 557 | .RsaKey(2048, 65537) |
| 558 | .AttestKey() |
| 559 | .AttestationChallenge("foo") |
| 560 | .AttestationApplicationId("bar") |
| 561 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 562 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 563 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 564 | .SetDefaultValidity(), |
| 565 | attest_key_opt, &key_blob_list[i], |
| 566 | &attested_key_characteristics, &cert_chain_list[i]); |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 567 | } |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 568 | // Strongbox may not support factory provisioned attestation key. |
| 569 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 570 | if (result == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED) return; |
| 571 | } |
| 572 | ASSERT_EQ(ErrorCode::OK, result); |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 573 | |
| 574 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 575 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
David Drysdale | a038695 | 2021-08-05 07:50:23 +0100 | [diff] [blame] | 576 | ASSERT_GT(cert_chain_list[i].size(), 0); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 577 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced, |
| 578 | SecLevel(), |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 579 | cert_chain_list[i][0].encodedCertificate)); |
| 580 | |
| 581 | if (i > 0) { |
| 582 | /* |
| 583 | * The first key is attestated with factory chain, but all the rest of the keys are |
| 584 | * not supposed to be returned in attestation certificate chains. |
| 585 | */ |
| 586 | EXPECT_FALSE(ChainSignaturesAreValid(cert_chain_list[i])); |
| 587 | |
| 588 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 589 | cert_chain_list[i].insert(cert_chain_list[i].end(), // |
| 590 | cert_chain_list[i - 1].begin(), // |
| 591 | cert_chain_list[i - 1].end()); |
| 592 | } |
| 593 | |
| 594 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_list[i])); |
| 595 | EXPECT_GT(cert_chain_list[i].size(), i + 1); |
| 596 | verify_subject_and_serial(cert_chain_list[i][0], serial_int, subject, false); |
| 597 | } |
| 598 | |
| 599 | for (int i = 0; i < chain_size; i++) { |
| 600 | CheckedDeleteKey(&key_blob_list[i]); |
| 601 | } |
| 602 | } |
| 603 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 604 | TEST_P(AttestKeyTest, MissingChallenge) { |
| 605 | for (auto size : ValidKeySizes(Algorithm::RSA)) { |
| 606 | /* |
| 607 | * Create attestation key. |
| 608 | */ |
| 609 | AttestationKey attest_key; |
| 610 | vector<KeyCharacteristics> attest_key_characteristics; |
| 611 | vector<Certificate> attest_key_cert_chain; |
David Drysdale | cdfb961 | 2022-06-13 10:12:12 +0100 | [diff] [blame] | 612 | ASSERT_EQ(ErrorCode::OK, |
| 613 | GenerateAttestKey(AuthorizationSetBuilder() |
| 614 | .RsaKey(size, 65537) |
| 615 | .AttestKey() |
| 616 | .SetDefaultValidity(), |
| 617 | {} /* attestation signing key */, &attest_key.keyBlob, |
| 618 | &attest_key_characteristics, &attest_key_cert_chain)); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 619 | |
| 620 | EXPECT_EQ(attest_key_cert_chain.size(), 1); |
| 621 | EXPECT_TRUE(IsSelfSigned(attest_key_cert_chain)) << "Failed on size " << size; |
| 622 | |
| 623 | /* |
| 624 | * Use attestation key to sign RSA / ECDSA key but forget to provide a challenge |
| 625 | */ |
| 626 | attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
| 627 | vector<uint8_t> attested_key_blob; |
| 628 | vector<KeyCharacteristics> attested_key_characteristics; |
| 629 | vector<Certificate> attested_key_cert_chain; |
Tommy Chiu | c93c439 | 2021-05-11 18:36:50 +0800 | [diff] [blame] | 630 | EXPECT_EQ(ErrorCode::ATTESTATION_CHALLENGE_MISSING, |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 631 | GenerateKey(AuthorizationSetBuilder() |
| 632 | .RsaSigningKey(2048, 65537) |
| 633 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 634 | .AttestationApplicationId("bar") |
| 635 | .SetDefaultValidity(), |
| 636 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 637 | &attested_key_cert_chain)); |
| 638 | |
Tommy Chiu | c93c439 | 2021-05-11 18:36:50 +0800 | [diff] [blame] | 639 | EXPECT_EQ(ErrorCode::ATTESTATION_CHALLENGE_MISSING, |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 640 | GenerateKey(AuthorizationSetBuilder() |
| 641 | .EcdsaSigningKey(EcCurve::P_256) |
| 642 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 643 | .AttestationApplicationId("bar") |
| 644 | .SetDefaultValidity(), |
| 645 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 646 | &attested_key_cert_chain)); |
| 647 | |
| 648 | CheckedDeleteKey(&attest_key.keyBlob); |
| 649 | } |
| 650 | } |
| 651 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 652 | TEST_P(AttestKeyTest, AllEcCurves) { |
| 653 | for (auto curve : ValidCurves()) { |
| 654 | /* |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 655 | * Create attestation key. |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 656 | */ |
| 657 | AttestationKey attest_key; |
| 658 | vector<KeyCharacteristics> attest_key_characteristics; |
| 659 | vector<Certificate> attest_key_cert_chain; |
David Drysdale | b3b1214 | 2021-11-01 17:13:27 +0000 | [diff] [blame] | 660 | ASSERT_EQ( |
| 661 | ErrorCode::OK, |
David Drysdale | cdfb961 | 2022-06-13 10:12:12 +0100 | [diff] [blame] | 662 | GenerateAttestKey( |
David Drysdale | b3b1214 | 2021-11-01 17:13:27 +0000 | [diff] [blame] | 663 | AuthorizationSetBuilder().EcdsaKey(curve).AttestKey().SetDefaultValidity(), |
| 664 | {} /* attestation signing key */, &attest_key.keyBlob, |
| 665 | &attest_key_characteristics, &attest_key_cert_chain)); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 666 | |
Shawn Willden | c410f6f | 2021-04-22 13:28:45 -0600 | [diff] [blame] | 667 | ASSERT_GT(attest_key_cert_chain.size(), 0); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 668 | EXPECT_EQ(attest_key_cert_chain.size(), 1); |
| 669 | EXPECT_TRUE(IsSelfSigned(attest_key_cert_chain)) << "Failed on curve " << curve; |
| 670 | |
| 671 | /* |
| 672 | * Use attestation key to sign RSA key |
| 673 | */ |
| 674 | attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
| 675 | vector<uint8_t> attested_key_blob; |
| 676 | vector<KeyCharacteristics> attested_key_characteristics; |
| 677 | vector<Certificate> attested_key_cert_chain; |
| 678 | EXPECT_EQ(ErrorCode::OK, |
| 679 | GenerateKey(AuthorizationSetBuilder() |
| 680 | .RsaSigningKey(2048, 65537) |
| 681 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 682 | .AttestationChallenge("foo") |
| 683 | .AttestationApplicationId("bar") |
| 684 | .SetDefaultValidity(), |
| 685 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 686 | &attested_key_cert_chain)); |
| 687 | |
Brian J Murray | aa8a758 | 2021-12-06 22:13:50 -0800 | [diff] [blame] | 688 | ASSERT_GT(attested_key_cert_chain.size(), 0); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 689 | CheckedDeleteKey(&attested_key_blob); |
| 690 | |
| 691 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 692 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 693 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced, |
| 694 | SecLevel(), |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 695 | attested_key_cert_chain[0].encodedCertificate)); |
| 696 | |
| 697 | // Attestation by itself is not valid (last entry is not self-signed). |
| 698 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 699 | |
| 700 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 701 | if (attest_key_cert_chain.size() > 0) { |
| 702 | attested_key_cert_chain.push_back(attest_key_cert_chain[0]); |
| 703 | } |
| 704 | EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 705 | |
| 706 | /* |
| 707 | * Use attestation key to sign EC key |
| 708 | */ |
| 709 | EXPECT_EQ(ErrorCode::OK, |
| 710 | GenerateKey(AuthorizationSetBuilder() |
| 711 | .EcdsaSigningKey(EcCurve::P_256) |
| 712 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 713 | .AttestationChallenge("foo") |
| 714 | .AttestationApplicationId("bar") |
| 715 | .SetDefaultValidity(), |
| 716 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 717 | &attested_key_cert_chain)); |
| 718 | |
Brian J Murray | aa8a758 | 2021-12-06 22:13:50 -0800 | [diff] [blame] | 719 | ASSERT_GT(attested_key_cert_chain.size(), 0); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 720 | CheckedDeleteKey(&attested_key_blob); |
| 721 | CheckedDeleteKey(&attest_key.keyBlob); |
| 722 | |
| 723 | hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 724 | sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 725 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), "foo", "bar", sw_enforced, hw_enforced, |
| 726 | SecLevel(), |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 727 | attested_key_cert_chain[0].encodedCertificate)); |
| 728 | |
| 729 | // Attestation by itself is not valid (last entry is not self-signed). |
| 730 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 731 | |
| 732 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 733 | if (attest_key_cert_chain.size() > 0) { |
| 734 | attested_key_cert_chain.push_back(attest_key_cert_chain[0]); |
| 735 | } |
| 736 | EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 737 | |
| 738 | // Bail early if anything failed. |
| 739 | if (HasFailure()) return; |
| 740 | } |
| 741 | } |
| 742 | |
Shawn Willden | 7bbf629 | 2021-04-01 12:57:21 -0600 | [diff] [blame] | 743 | TEST_P(AttestKeyTest, AttestWithNonAttestKey) { |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 744 | // Create non-attestation key. |
Shawn Willden | 7bbf629 | 2021-04-01 12:57:21 -0600 | [diff] [blame] | 745 | AttestationKey non_attest_key; |
| 746 | vector<KeyCharacteristics> non_attest_key_characteristics; |
| 747 | vector<Certificate> non_attest_key_cert_chain; |
| 748 | ASSERT_EQ( |
| 749 | ErrorCode::OK, |
| 750 | GenerateKey( |
| 751 | AuthorizationSetBuilder().EcdsaSigningKey(EcCurve::P_256).SetDefaultValidity(), |
David Drysdale | a676c3b | 2021-06-14 14:46:02 +0100 | [diff] [blame] | 752 | {} /* attestation signing key */, &non_attest_key.keyBlob, |
Shawn Willden | 7bbf629 | 2021-04-01 12:57:21 -0600 | [diff] [blame] | 753 | &non_attest_key_characteristics, &non_attest_key_cert_chain)); |
| 754 | |
Shawn Willden | c410f6f | 2021-04-22 13:28:45 -0600 | [diff] [blame] | 755 | ASSERT_GT(non_attest_key_cert_chain.size(), 0); |
Shawn Willden | 7bbf629 | 2021-04-01 12:57:21 -0600 | [diff] [blame] | 756 | EXPECT_EQ(non_attest_key_cert_chain.size(), 1); |
| 757 | EXPECT_TRUE(IsSelfSigned(non_attest_key_cert_chain)); |
| 758 | |
| 759 | // Attempt to sign attestation with non-attest key. |
| 760 | vector<uint8_t> attested_key_blob; |
| 761 | vector<KeyCharacteristics> attested_key_characteristics; |
| 762 | vector<Certificate> attested_key_cert_chain; |
| 763 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 764 | GenerateKey(AuthorizationSetBuilder() |
| 765 | .EcdsaSigningKey(EcCurve::P_256) |
| 766 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 767 | .AttestationChallenge("foo") |
| 768 | .AttestationApplicationId("bar") |
| 769 | .SetDefaultValidity(), |
| 770 | non_attest_key, &attested_key_blob, &attested_key_characteristics, |
| 771 | &attested_key_cert_chain)); |
| 772 | } |
| 773 | |
David Drysdale | a676c3b | 2021-06-14 14:46:02 +0100 | [diff] [blame] | 774 | TEST_P(AttestKeyTest, EcdsaAttestationID) { |
David Drysdale | 32a7bec | 2022-05-03 18:48:57 +0100 | [diff] [blame] | 775 | if (is_gsi_image()) { |
| 776 | // GSI sets up a standard set of device identifiers that may not match |
| 777 | // the device identifiers held by the device. |
| 778 | GTEST_SKIP() << "Test not applicable under GSI"; |
| 779 | } |
David Drysdale | a676c3b | 2021-06-14 14:46:02 +0100 | [diff] [blame] | 780 | // Create attestation key. |
| 781 | AttestationKey attest_key; |
| 782 | vector<KeyCharacteristics> attest_key_characteristics; |
| 783 | vector<Certificate> attest_key_cert_chain; |
David Drysdale | cdfb961 | 2022-06-13 10:12:12 +0100 | [diff] [blame] | 784 | ASSERT_EQ(ErrorCode::OK, |
| 785 | GenerateAttestKey(AuthorizationSetBuilder() |
| 786 | .EcdsaKey(EcCurve::P_256) |
| 787 | .AttestKey() |
| 788 | .SetDefaultValidity(), |
| 789 | {} /* attestation signing key */, &attest_key.keyBlob, |
| 790 | &attest_key_characteristics, &attest_key_cert_chain)); |
David Drysdale | a676c3b | 2021-06-14 14:46:02 +0100 | [diff] [blame] | 791 | attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
| 792 | ASSERT_GT(attest_key_cert_chain.size(), 0); |
| 793 | EXPECT_EQ(attest_key_cert_chain.size(), 1); |
| 794 | EXPECT_TRUE(IsSelfSigned(attest_key_cert_chain)); |
| 795 | |
| 796 | // Collection of valid attestation ID tags. |
| 797 | auto attestation_id_tags = AuthorizationSetBuilder(); |
| 798 | add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_BRAND, "ro.product.brand"); |
| 799 | add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_DEVICE, "ro.product.device"); |
| 800 | add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_PRODUCT, "ro.product.name"); |
| 801 | add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_SERIAL, "ro.serial"); |
| 802 | add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_MANUFACTURER, |
| 803 | "ro.product.manufacturer"); |
| 804 | add_tag_from_prop(&attestation_id_tags, TAG_ATTESTATION_ID_MODEL, "ro.product.model"); |
| 805 | |
| 806 | for (const KeyParameter& tag : attestation_id_tags) { |
| 807 | SCOPED_TRACE(testing::Message() << "+tag-" << tag); |
| 808 | // Use attestation key to sign an ECDSA key, but include an attestation ID field. |
| 809 | AuthorizationSetBuilder builder = AuthorizationSetBuilder() |
| 810 | .EcdsaSigningKey(EcCurve::P_256) |
| 811 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 812 | .AttestationChallenge("challenge") |
| 813 | .AttestationApplicationId("foo") |
| 814 | .SetDefaultValidity(); |
| 815 | builder.push_back(tag); |
| 816 | vector<uint8_t> attested_key_blob; |
| 817 | vector<KeyCharacteristics> attested_key_characteristics; |
| 818 | vector<Certificate> attested_key_cert_chain; |
| 819 | auto result = GenerateKey(builder, attest_key, &attested_key_blob, |
| 820 | &attested_key_characteristics, &attested_key_cert_chain); |
Prashant Patil | 88ad189 | 2022-03-15 16:31:02 +0000 | [diff] [blame] | 821 | if (result == ErrorCode::CANNOT_ATTEST_IDS && !isDeviceIdAttestationRequired()) { |
David Drysdale | a676c3b | 2021-06-14 14:46:02 +0100 | [diff] [blame] | 822 | continue; |
| 823 | } |
| 824 | |
| 825 | ASSERT_EQ(result, ErrorCode::OK); |
| 826 | |
| 827 | CheckedDeleteKey(&attested_key_blob); |
| 828 | |
| 829 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 830 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
| 831 | |
| 832 | // The attested key characteristics will not contain APPLICATION_ID_* fields (their |
| 833 | // spec definitions all have "Must never appear in KeyCharacteristics"), but the |
| 834 | // attestation extension should contain them, so make sure the extra tag is added. |
| 835 | hw_enforced.push_back(tag); |
| 836 | |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 837 | EXPECT_TRUE(verify_attestation_record(AidlVersion(), "challenge", "foo", sw_enforced, |
| 838 | hw_enforced, SecLevel(), |
David Drysdale | a676c3b | 2021-06-14 14:46:02 +0100 | [diff] [blame] | 839 | attested_key_cert_chain[0].encodedCertificate)); |
| 840 | } |
| 841 | CheckedDeleteKey(&attest_key.keyBlob); |
| 842 | } |
| 843 | |
| 844 | TEST_P(AttestKeyTest, EcdsaAttestationMismatchID) { |
| 845 | // Create attestation key. |
| 846 | AttestationKey attest_key; |
| 847 | vector<KeyCharacteristics> attest_key_characteristics; |
| 848 | vector<Certificate> attest_key_cert_chain; |
David Drysdale | cdfb961 | 2022-06-13 10:12:12 +0100 | [diff] [blame] | 849 | ASSERT_EQ(ErrorCode::OK, |
| 850 | GenerateAttestKey(AuthorizationSetBuilder() |
| 851 | .EcdsaKey(EcCurve::P_256) |
| 852 | .AttestKey() |
| 853 | .SetDefaultValidity(), |
| 854 | {} /* attestation signing key */, &attest_key.keyBlob, |
| 855 | &attest_key_characteristics, &attest_key_cert_chain)); |
David Drysdale | a676c3b | 2021-06-14 14:46:02 +0100 | [diff] [blame] | 856 | attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
| 857 | ASSERT_GT(attest_key_cert_chain.size(), 0); |
| 858 | EXPECT_EQ(attest_key_cert_chain.size(), 1); |
| 859 | EXPECT_TRUE(IsSelfSigned(attest_key_cert_chain)); |
| 860 | |
| 861 | // Collection of invalid attestation ID tags. |
| 862 | auto attestation_id_tags = |
| 863 | AuthorizationSetBuilder() |
| 864 | .Authorization(TAG_ATTESTATION_ID_BRAND, "bogus-brand") |
| 865 | .Authorization(TAG_ATTESTATION_ID_DEVICE, "devious-device") |
| 866 | .Authorization(TAG_ATTESTATION_ID_PRODUCT, "punctured-product") |
| 867 | .Authorization(TAG_ATTESTATION_ID_SERIAL, "suspicious-serial") |
| 868 | .Authorization(TAG_ATTESTATION_ID_IMEI, "invalid-imei") |
| 869 | .Authorization(TAG_ATTESTATION_ID_MEID, "mismatching-meid") |
| 870 | .Authorization(TAG_ATTESTATION_ID_MANUFACTURER, "malformed-manufacturer") |
| 871 | .Authorization(TAG_ATTESTATION_ID_MODEL, "malicious-model"); |
| 872 | vector<uint8_t> key_blob; |
| 873 | vector<KeyCharacteristics> key_characteristics; |
| 874 | |
| 875 | for (const KeyParameter& invalid_tag : attestation_id_tags) { |
| 876 | SCOPED_TRACE(testing::Message() << "+tag-" << invalid_tag); |
| 877 | |
| 878 | // Use attestation key to sign an ECDSA key, but include an invalid |
| 879 | // attestation ID field. |
| 880 | AuthorizationSetBuilder builder = AuthorizationSetBuilder() |
| 881 | .EcdsaSigningKey(EcCurve::P_256) |
| 882 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 883 | .AttestationChallenge("challenge") |
| 884 | .AttestationApplicationId("foo") |
| 885 | .SetDefaultValidity(); |
| 886 | builder.push_back(invalid_tag); |
| 887 | vector<uint8_t> attested_key_blob; |
| 888 | vector<KeyCharacteristics> attested_key_characteristics; |
| 889 | vector<Certificate> attested_key_cert_chain; |
| 890 | auto result = GenerateKey(builder, attest_key, &attested_key_blob, |
| 891 | &attested_key_characteristics, &attested_key_cert_chain); |
| 892 | |
| 893 | ASSERT_TRUE(result == ErrorCode::CANNOT_ATTEST_IDS || result == ErrorCode::INVALID_TAG) |
| 894 | << "result = " << result; |
| 895 | } |
| 896 | CheckedDeleteKey(&attest_key.keyBlob); |
| 897 | } |
| 898 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 899 | INSTANTIATE_KEYMINT_AIDL_TEST(AttestKeyTest); |
| 900 | |
| 901 | } // namespace aidl::android::hardware::security::keymint::test |