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 | /* |
| 47 | * Create attestaton key. |
| 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() |
| 53 | .RsaSigningKey(size, 65537) |
| 54 | .AttestKey() |
| 55 | .SetDefaultValidity(), |
| 56 | {} /* attestation signing key */, &attest_key.keyBlob, |
| 57 | &attest_key_characteristics, &attest_key_cert_chain)); |
| 58 | |
| 59 | EXPECT_EQ(attest_key_cert_chain.size(), 1); |
| 60 | EXPECT_TRUE(IsSelfSigned(attest_key_cert_chain)) << "Failed on size " << size; |
| 61 | |
| 62 | /* |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 63 | * Use attestation key to sign RSA signing key |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 64 | */ |
| 65 | attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
| 66 | vector<uint8_t> attested_key_blob; |
| 67 | vector<KeyCharacteristics> attested_key_characteristics; |
| 68 | vector<Certificate> attested_key_cert_chain; |
| 69 | EXPECT_EQ(ErrorCode::OK, |
| 70 | GenerateKey(AuthorizationSetBuilder() |
| 71 | .RsaSigningKey(2048, 65537) |
| 72 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 73 | .AttestationChallenge("foo") |
| 74 | .AttestationApplicationId("bar") |
| 75 | .SetDefaultValidity(), |
| 76 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 77 | &attested_key_cert_chain)); |
| 78 | |
| 79 | CheckedDeleteKey(&attested_key_blob); |
| 80 | |
| 81 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 82 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
| 83 | EXPECT_TRUE(verify_attestation_record("foo", "bar", sw_enforced, hw_enforced, SecLevel(), |
| 84 | attested_key_cert_chain[0].encodedCertificate)); |
| 85 | |
| 86 | // Attestation by itself is not valid (last entry is not self-signed). |
| 87 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 88 | |
| 89 | // 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] | 90 | attested_key_cert_chain.push_back(attest_key_cert_chain[0]); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 91 | EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain)); |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 92 | EXPECT_EQ(attested_key_cert_chain.size(), 2); |
| 93 | |
| 94 | /* |
| 95 | * Use attestation key to sign RSA decryption key |
| 96 | */ |
| 97 | attested_key_characteristics.resize(0); |
| 98 | attested_key_cert_chain.resize(0); |
| 99 | EXPECT_EQ(ErrorCode::OK, |
| 100 | GenerateKey(AuthorizationSetBuilder() |
| 101 | .RsaEncryptionKey(2048, 65537) |
| 102 | .Digest(Digest::NONE) |
| 103 | .Padding(PaddingMode::NONE) |
| 104 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 105 | .AttestationChallenge("foo2") |
| 106 | .AttestationApplicationId("bar2") |
| 107 | .SetDefaultValidity(), |
| 108 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 109 | &attested_key_cert_chain)); |
| 110 | |
| 111 | CheckedDeleteKey(&attested_key_blob); |
| 112 | |
| 113 | hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 114 | sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
| 115 | EXPECT_TRUE(verify_attestation_record("foo2", "bar2", sw_enforced, hw_enforced, SecLevel(), |
| 116 | attested_key_cert_chain[0].encodedCertificate)); |
| 117 | |
| 118 | // Attestation by itself is not valid (last entry is not self-signed). |
| 119 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 120 | |
| 121 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 122 | attested_key_cert_chain.push_back(attest_key_cert_chain[0]); |
| 123 | EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 124 | EXPECT_EQ(attested_key_cert_chain.size(), 2); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * Use attestation key to sign EC key |
| 128 | */ |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 129 | attested_key_characteristics.resize(0); |
| 130 | attested_key_cert_chain.resize(0); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 131 | EXPECT_EQ(ErrorCode::OK, |
| 132 | GenerateKey(AuthorizationSetBuilder() |
| 133 | .EcdsaSigningKey(EcCurve::P_256) |
| 134 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 135 | .AttestationChallenge("foo") |
| 136 | .AttestationApplicationId("bar") |
| 137 | .SetDefaultValidity(), |
| 138 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 139 | &attested_key_cert_chain)); |
| 140 | |
| 141 | CheckedDeleteKey(&attested_key_blob); |
| 142 | CheckedDeleteKey(&attest_key.keyBlob); |
| 143 | |
| 144 | hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 145 | sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
| 146 | EXPECT_TRUE(verify_attestation_record("foo", "bar", sw_enforced, hw_enforced, SecLevel(), |
| 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. |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 153 | attested_key_cert_chain.push_back(attest_key_cert_chain[0]); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 154 | EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 155 | |
| 156 | // Bail early if anything failed. |
| 157 | if (HasFailure()) return; |
| 158 | } |
| 159 | } |
| 160 | |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 161 | /* |
| 162 | * AttestKeyTest.RsaAttestedAttestKeys |
| 163 | * |
| 164 | * This test creates an RSA attestation key signed by factory keys, and varifies it can be |
| 165 | * used to sign other RSA and EC keys. |
| 166 | */ |
| 167 | TEST_P(AttestKeyTest, RsaAttestedAttestKeys) { |
| 168 | auto challenge = "hello"; |
| 169 | auto app_id = "foo"; |
| 170 | |
| 171 | auto subject = "cert subj 2"; |
| 172 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 173 | |
| 174 | uint64_t serial_int = 66; |
| 175 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 176 | |
| 177 | /* |
| 178 | * Create attestation key. |
| 179 | */ |
| 180 | AttestationKey attest_key; |
| 181 | vector<KeyCharacteristics> attest_key_characteristics; |
| 182 | vector<Certificate> attest_key_cert_chain; |
| 183 | ASSERT_EQ(ErrorCode::OK, |
| 184 | GenerateKey(AuthorizationSetBuilder() |
| 185 | .RsaSigningKey(2048, 65537) |
| 186 | .AttestKey() |
| 187 | .AttestationChallenge(challenge) |
| 188 | .AttestationApplicationId(app_id) |
| 189 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 190 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 191 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 192 | .SetDefaultValidity(), |
| 193 | {} /* attestation signing key */, &attest_key.keyBlob, |
| 194 | &attest_key_characteristics, &attest_key_cert_chain)); |
| 195 | |
| 196 | EXPECT_GT(attest_key_cert_chain.size(), 1); |
| 197 | verify_subject_and_serial(attest_key_cert_chain[0], serial_int, subject, false); |
| 198 | EXPECT_TRUE(ChainSignaturesAreValid(attest_key_cert_chain)); |
| 199 | |
| 200 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attest_key_characteristics); |
| 201 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attest_key_characteristics); |
| 202 | EXPECT_TRUE(verify_attestation_record(challenge, app_id, // |
| 203 | sw_enforced, hw_enforced, SecLevel(), |
| 204 | attest_key_cert_chain[0].encodedCertificate)); |
| 205 | |
| 206 | /* |
| 207 | * Use attestation key to sign RSA key |
| 208 | */ |
| 209 | attest_key.issuerSubjectName = subject_der; |
| 210 | vector<uint8_t> attested_key_blob; |
| 211 | vector<KeyCharacteristics> attested_key_characteristics; |
| 212 | vector<Certificate> attested_key_cert_chain; |
| 213 | |
| 214 | auto subject2 = "cert subject"; |
| 215 | vector<uint8_t> subject_der2(make_name_from_str(subject2)); |
| 216 | |
| 217 | uint64_t serial_int2 = 987; |
| 218 | vector<uint8_t> serial_blob2(build_serial_blob(serial_int2)); |
| 219 | |
| 220 | EXPECT_EQ(ErrorCode::OK, |
| 221 | GenerateKey(AuthorizationSetBuilder() |
| 222 | .RsaSigningKey(2048, 65537) |
| 223 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 224 | .AttestationChallenge("foo") |
| 225 | .AttestationApplicationId("bar") |
| 226 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob2) |
| 227 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der2) |
| 228 | .SetDefaultValidity(), |
| 229 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 230 | &attested_key_cert_chain)); |
| 231 | |
| 232 | CheckedDeleteKey(&attested_key_blob); |
| 233 | CheckedDeleteKey(&attest_key.keyBlob); |
| 234 | |
| 235 | AuthorizationSet hw_enforced2 = HwEnforcedAuthorizations(attested_key_characteristics); |
| 236 | AuthorizationSet sw_enforced2 = SwEnforcedAuthorizations(attested_key_characteristics); |
| 237 | EXPECT_TRUE(verify_attestation_record("foo", "bar", sw_enforced2, hw_enforced2, SecLevel(), |
| 238 | attested_key_cert_chain[0].encodedCertificate)); |
| 239 | |
| 240 | // Attestation by itself is not valid (last entry is not self-signed). |
| 241 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 242 | |
| 243 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 244 | attested_key_cert_chain.insert(attested_key_cert_chain.end(), attest_key_cert_chain.begin(), |
| 245 | attest_key_cert_chain.end()); |
| 246 | |
| 247 | EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 248 | EXPECT_GT(attested_key_cert_chain.size(), 2); |
| 249 | verify_subject_and_serial(attested_key_cert_chain[0], serial_int2, subject2, false); |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * AttestKeyTest.RsaAttestKeyChaining |
| 254 | * |
| 255 | * This test creates a chain of multiple RSA attest keys, each used to sign the next attest key, |
| 256 | * with the last attest key signed by the factory chain. |
| 257 | */ |
| 258 | TEST_P(AttestKeyTest, RsaAttestKeyChaining) { |
| 259 | const int chain_size = 6; |
| 260 | vector<vector<uint8_t>> key_blob_list(chain_size); |
| 261 | vector<vector<Certificate>> cert_chain_list(chain_size); |
| 262 | |
| 263 | for (int i = 0; i < chain_size; i++) { |
| 264 | string sub = "attest key chaining "; |
| 265 | char index = '1' + i; |
| 266 | string subject = sub + index; |
| 267 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 268 | |
| 269 | uint64_t serial_int = 7000 + i; |
| 270 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 271 | |
| 272 | vector<KeyCharacteristics> attested_key_characteristics; |
| 273 | AttestationKey attest_key; |
| 274 | optional<AttestationKey> attest_key_opt; |
| 275 | |
| 276 | if (i > 0) { |
| 277 | attest_key.issuerSubjectName = make_name_from_str(sub + (char)(index - 1)); |
| 278 | attest_key.keyBlob = key_blob_list[i - 1]; |
| 279 | attest_key_opt = attest_key; |
| 280 | } |
| 281 | |
| 282 | EXPECT_EQ(ErrorCode::OK, |
| 283 | GenerateKey(AuthorizationSetBuilder() |
| 284 | .RsaSigningKey(2048, 65537) |
| 285 | .AttestKey() |
| 286 | .AttestationChallenge("foo") |
| 287 | .AttestationApplicationId("bar") |
| 288 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 289 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 290 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 291 | .SetDefaultValidity(), |
| 292 | attest_key_opt, &key_blob_list[i], &attested_key_characteristics, |
| 293 | &cert_chain_list[i])); |
| 294 | |
| 295 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 296 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
| 297 | EXPECT_TRUE(verify_attestation_record("foo", "bar", sw_enforced, hw_enforced, SecLevel(), |
| 298 | cert_chain_list[i][0].encodedCertificate)); |
| 299 | |
| 300 | if (i > 0) { |
| 301 | /* |
| 302 | * The first key is attestated with factory chain, but all the rest of the keys are |
| 303 | * not supposed to be returned in attestation certificate chains. |
| 304 | */ |
| 305 | EXPECT_FALSE(ChainSignaturesAreValid(cert_chain_list[i])); |
| 306 | |
| 307 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 308 | cert_chain_list[i].insert(cert_chain_list[i].end(), // |
| 309 | cert_chain_list[i - 1].begin(), // |
| 310 | cert_chain_list[i - 1].end()); |
| 311 | } |
| 312 | |
| 313 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_list[i])); |
| 314 | EXPECT_GT(cert_chain_list[i].size(), i + 1); |
| 315 | verify_subject_and_serial(cert_chain_list[i][0], serial_int, subject, false); |
| 316 | } |
| 317 | |
| 318 | for (int i = 0; i < chain_size; i++) { |
| 319 | CheckedDeleteKey(&key_blob_list[i]); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * AttestKeyTest.EcAttestKeyChaining |
| 325 | * |
| 326 | * This test creates a chain of multiple Ec attest keys, each used to sign the next attest key, |
| 327 | * with the last attest key signed by the factory chain. |
| 328 | */ |
| 329 | TEST_P(AttestKeyTest, EcAttestKeyChaining) { |
| 330 | const int chain_size = 6; |
| 331 | vector<vector<uint8_t>> key_blob_list(chain_size); |
| 332 | vector<vector<Certificate>> cert_chain_list(chain_size); |
| 333 | |
| 334 | for (int i = 0; i < chain_size; i++) { |
| 335 | string sub = "Ec attest key chaining "; |
| 336 | char index = '1' + i; |
| 337 | string subject = sub + index; |
| 338 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 339 | |
| 340 | uint64_t serial_int = 800000 + i; |
| 341 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 342 | |
| 343 | vector<KeyCharacteristics> attested_key_characteristics; |
| 344 | AttestationKey attest_key; |
| 345 | optional<AttestationKey> attest_key_opt; |
| 346 | |
| 347 | if (i > 0) { |
| 348 | attest_key.issuerSubjectName = make_name_from_str(sub + (char)(index - 1)); |
| 349 | attest_key.keyBlob = key_blob_list[i - 1]; |
| 350 | attest_key_opt = attest_key; |
| 351 | } |
| 352 | |
| 353 | EXPECT_EQ(ErrorCode::OK, |
| 354 | GenerateKey(AuthorizationSetBuilder() |
| 355 | .EcdsaSigningKey(224) |
| 356 | .AttestKey() |
| 357 | .AttestationChallenge("foo") |
| 358 | .AttestationApplicationId("bar") |
| 359 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 360 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 361 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 362 | .SetDefaultValidity(), |
| 363 | attest_key_opt, &key_blob_list[i], &attested_key_characteristics, |
| 364 | &cert_chain_list[i])); |
| 365 | |
| 366 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 367 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
| 368 | EXPECT_TRUE(verify_attestation_record("foo", "bar", sw_enforced, hw_enforced, SecLevel(), |
| 369 | cert_chain_list[i][0].encodedCertificate)); |
| 370 | |
| 371 | if (i > 0) { |
| 372 | /* |
| 373 | * The first key is attestated with factory chain, but all the rest of the keys are |
| 374 | * not supposed to be returned in attestation certificate chains. |
| 375 | */ |
| 376 | EXPECT_FALSE(ChainSignaturesAreValid(cert_chain_list[i])); |
| 377 | |
| 378 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 379 | cert_chain_list[i].insert(cert_chain_list[i].end(), // |
| 380 | cert_chain_list[i - 1].begin(), // |
| 381 | cert_chain_list[i - 1].end()); |
| 382 | } |
| 383 | |
| 384 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_list[i])); |
| 385 | EXPECT_GT(cert_chain_list[i].size(), i + 1); |
| 386 | verify_subject_and_serial(cert_chain_list[i][0], serial_int, subject, false); |
| 387 | } |
| 388 | |
| 389 | for (int i = 0; i < chain_size; i++) { |
| 390 | CheckedDeleteKey(&key_blob_list[i]); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * AttestKeyTest.AlternateAttestKeyChaining |
| 396 | * |
| 397 | * This test creates a chain of multiple attest keys, in the order Ec - RSA - Ec - RSA .... |
| 398 | * Each attest key is used to sign the next attest key, with the last attest key signed by |
| 399 | * the factory chain. This is to verify different algorithms of attest keys can |
| 400 | * cross sign each other and be chained together. |
| 401 | */ |
| 402 | TEST_P(AttestKeyTest, AlternateAttestKeyChaining) { |
| 403 | const int chain_size = 6; |
| 404 | vector<vector<uint8_t>> key_blob_list(chain_size); |
| 405 | vector<vector<Certificate>> cert_chain_list(chain_size); |
| 406 | |
| 407 | for (int i = 0; i < chain_size; i++) { |
| 408 | string sub = "Alt attest key chaining "; |
| 409 | char index = '1' + i; |
| 410 | string subject = sub + index; |
| 411 | vector<uint8_t> subject_der(make_name_from_str(subject)); |
| 412 | |
| 413 | uint64_t serial_int = 90000000 + i; |
| 414 | vector<uint8_t> serial_blob(build_serial_blob(serial_int)); |
| 415 | |
| 416 | vector<KeyCharacteristics> attested_key_characteristics; |
| 417 | AttestationKey attest_key; |
| 418 | optional<AttestationKey> attest_key_opt; |
| 419 | |
| 420 | if (i > 0) { |
| 421 | attest_key.issuerSubjectName = make_name_from_str(sub + (char)(index - 1)); |
| 422 | attest_key.keyBlob = key_blob_list[i - 1]; |
| 423 | attest_key_opt = attest_key; |
| 424 | } |
| 425 | |
| 426 | if ((i & 0x1) == 1) { |
| 427 | EXPECT_EQ(ErrorCode::OK, |
| 428 | GenerateKey(AuthorizationSetBuilder() |
| 429 | .EcdsaSigningKey(224) |
| 430 | .AttestKey() |
| 431 | .AttestationChallenge("foo") |
| 432 | .AttestationApplicationId("bar") |
| 433 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 434 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 435 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 436 | .SetDefaultValidity(), |
| 437 | attest_key_opt, &key_blob_list[i], &attested_key_characteristics, |
| 438 | &cert_chain_list[i])); |
| 439 | } else { |
| 440 | EXPECT_EQ(ErrorCode::OK, |
| 441 | GenerateKey(AuthorizationSetBuilder() |
| 442 | .RsaSigningKey(2048, 65537) |
| 443 | .AttestKey() |
| 444 | .AttestationChallenge("foo") |
| 445 | .AttestationApplicationId("bar") |
| 446 | .Authorization(TAG_CERTIFICATE_SERIAL, serial_blob) |
| 447 | .Authorization(TAG_CERTIFICATE_SUBJECT, subject_der) |
| 448 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 449 | .SetDefaultValidity(), |
| 450 | attest_key_opt, &key_blob_list[i], &attested_key_characteristics, |
| 451 | &cert_chain_list[i])); |
| 452 | } |
| 453 | |
| 454 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 455 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
| 456 | EXPECT_TRUE(verify_attestation_record("foo", "bar", sw_enforced, hw_enforced, SecLevel(), |
| 457 | cert_chain_list[i][0].encodedCertificate)); |
| 458 | |
| 459 | if (i > 0) { |
| 460 | /* |
| 461 | * The first key is attestated with factory chain, but all the rest of the keys are |
| 462 | * not supposed to be returned in attestation certificate chains. |
| 463 | */ |
| 464 | EXPECT_FALSE(ChainSignaturesAreValid(cert_chain_list[i])); |
| 465 | |
| 466 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 467 | cert_chain_list[i].insert(cert_chain_list[i].end(), // |
| 468 | cert_chain_list[i - 1].begin(), // |
| 469 | cert_chain_list[i - 1].end()); |
| 470 | } |
| 471 | |
| 472 | EXPECT_TRUE(ChainSignaturesAreValid(cert_chain_list[i])); |
| 473 | EXPECT_GT(cert_chain_list[i].size(), i + 1); |
| 474 | verify_subject_and_serial(cert_chain_list[i][0], serial_int, subject, false); |
| 475 | } |
| 476 | |
| 477 | for (int i = 0; i < chain_size; i++) { |
| 478 | CheckedDeleteKey(&key_blob_list[i]); |
| 479 | } |
| 480 | } |
| 481 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 482 | TEST_P(AttestKeyTest, AllEcCurves) { |
| 483 | for (auto curve : ValidCurves()) { |
| 484 | /* |
| 485 | * Create attestaton key. |
| 486 | */ |
| 487 | AttestationKey attest_key; |
| 488 | vector<KeyCharacteristics> attest_key_characteristics; |
| 489 | vector<Certificate> attest_key_cert_chain; |
| 490 | ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder() |
| 491 | .EcdsaSigningKey(curve) |
| 492 | .AttestKey() |
| 493 | .SetDefaultValidity(), |
| 494 | {} /* attestation siging key */, &attest_key.keyBlob, |
| 495 | &attest_key_characteristics, &attest_key_cert_chain)); |
| 496 | |
| 497 | EXPECT_EQ(attest_key_cert_chain.size(), 1); |
| 498 | EXPECT_TRUE(IsSelfSigned(attest_key_cert_chain)) << "Failed on curve " << curve; |
| 499 | |
| 500 | /* |
| 501 | * Use attestation key to sign RSA key |
| 502 | */ |
| 503 | attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
| 504 | vector<uint8_t> attested_key_blob; |
| 505 | vector<KeyCharacteristics> attested_key_characteristics; |
| 506 | vector<Certificate> attested_key_cert_chain; |
| 507 | EXPECT_EQ(ErrorCode::OK, |
| 508 | GenerateKey(AuthorizationSetBuilder() |
| 509 | .RsaSigningKey(2048, 65537) |
| 510 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 511 | .AttestationChallenge("foo") |
| 512 | .AttestationApplicationId("bar") |
| 513 | .SetDefaultValidity(), |
| 514 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 515 | &attested_key_cert_chain)); |
| 516 | |
| 517 | CheckedDeleteKey(&attested_key_blob); |
| 518 | |
| 519 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 520 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
| 521 | EXPECT_TRUE(verify_attestation_record("foo", "bar", sw_enforced, hw_enforced, SecLevel(), |
| 522 | attested_key_cert_chain[0].encodedCertificate)); |
| 523 | |
| 524 | // Attestation by itself is not valid (last entry is not self-signed). |
| 525 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 526 | |
| 527 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 528 | if (attest_key_cert_chain.size() > 0) { |
| 529 | attested_key_cert_chain.push_back(attest_key_cert_chain[0]); |
| 530 | } |
| 531 | EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 532 | |
| 533 | /* |
| 534 | * Use attestation key to sign EC key |
| 535 | */ |
| 536 | EXPECT_EQ(ErrorCode::OK, |
| 537 | GenerateKey(AuthorizationSetBuilder() |
| 538 | .EcdsaSigningKey(EcCurve::P_256) |
| 539 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 540 | .AttestationChallenge("foo") |
| 541 | .AttestationApplicationId("bar") |
| 542 | .SetDefaultValidity(), |
| 543 | attest_key, &attested_key_blob, &attested_key_characteristics, |
| 544 | &attested_key_cert_chain)); |
| 545 | |
| 546 | CheckedDeleteKey(&attested_key_blob); |
| 547 | CheckedDeleteKey(&attest_key.keyBlob); |
| 548 | |
| 549 | hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 550 | sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
| 551 | EXPECT_TRUE(verify_attestation_record("foo", "bar", sw_enforced, hw_enforced, SecLevel(), |
| 552 | attested_key_cert_chain[0].encodedCertificate)); |
| 553 | |
| 554 | // Attestation by itself is not valid (last entry is not self-signed). |
| 555 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 556 | |
| 557 | // Appending the attest_key chain to the attested_key_chain should yield a valid chain. |
| 558 | if (attest_key_cert_chain.size() > 0) { |
| 559 | attested_key_cert_chain.push_back(attest_key_cert_chain[0]); |
| 560 | } |
| 561 | EXPECT_TRUE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 562 | |
| 563 | // Bail early if anything failed. |
| 564 | if (HasFailure()) return; |
| 565 | } |
| 566 | } |
| 567 | |
Shawn Willden | 7bbf629 | 2021-04-01 12:57:21 -0600 | [diff] [blame] | 568 | TEST_P(AttestKeyTest, AttestWithNonAttestKey) { |
| 569 | // Create non-attestaton key. |
| 570 | AttestationKey non_attest_key; |
| 571 | vector<KeyCharacteristics> non_attest_key_characteristics; |
| 572 | vector<Certificate> non_attest_key_cert_chain; |
| 573 | ASSERT_EQ( |
| 574 | ErrorCode::OK, |
| 575 | GenerateKey( |
| 576 | AuthorizationSetBuilder().EcdsaSigningKey(EcCurve::P_256).SetDefaultValidity(), |
| 577 | {} /* attestation siging key */, &non_attest_key.keyBlob, |
| 578 | &non_attest_key_characteristics, &non_attest_key_cert_chain)); |
| 579 | |
| 580 | EXPECT_EQ(non_attest_key_cert_chain.size(), 1); |
| 581 | EXPECT_TRUE(IsSelfSigned(non_attest_key_cert_chain)); |
| 582 | |
| 583 | // Attempt to sign attestation with non-attest key. |
| 584 | vector<uint8_t> attested_key_blob; |
| 585 | vector<KeyCharacteristics> attested_key_characteristics; |
| 586 | vector<Certificate> attested_key_cert_chain; |
| 587 | EXPECT_EQ(ErrorCode::INCOMPATIBLE_PURPOSE, |
| 588 | GenerateKey(AuthorizationSetBuilder() |
| 589 | .EcdsaSigningKey(EcCurve::P_256) |
| 590 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 591 | .AttestationChallenge("foo") |
| 592 | .AttestationApplicationId("bar") |
| 593 | .SetDefaultValidity(), |
| 594 | non_attest_key, &attested_key_blob, &attested_key_characteristics, |
| 595 | &attested_key_cert_chain)); |
| 596 | } |
| 597 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 598 | INSTANTIATE_KEYMINT_AIDL_TEST(AttestKeyTest); |
| 599 | |
| 600 | } // namespace aidl::android::hardware::security::keymint::test |