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