Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "VtsRemotelyProvisionableComponentTests" |
| 18 | |
Max Bires | 261a049 | 2021-04-19 18:55:56 -0700 | [diff] [blame] | 19 | #include <AndroidRemotelyProvisionedComponentDevice.h> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 20 | #include <aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.h> |
| 21 | #include <aidl/android/hardware/security/keymint/SecurityLevel.h> |
| 22 | #include <android/binder_manager.h> |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 24 | #include <cppbor_parse.h> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 25 | #include <gmock/gmock.h> |
Max Bires | 9704ff6 | 2021-04-07 11:12:01 -0700 | [diff] [blame] | 26 | #include <keymaster/cppcose/cppcose.h> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 27 | #include <keymaster/keymaster_configuration.h> |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 28 | #include <keymint_support/authorization_set.h> |
| 29 | #include <openssl/ec.h> |
| 30 | #include <openssl/ec_key.h> |
| 31 | #include <openssl/x509.h> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 32 | #include <remote_prov/remote_prov_utils.h> |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 33 | #include <set> |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 34 | #include <vector> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 35 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 36 | #include "KeyMintAidlTestBase.h" |
| 37 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 38 | namespace aidl::android::hardware::security::keymint::test { |
| 39 | |
| 40 | using ::std::string; |
| 41 | using ::std::vector; |
| 42 | |
| 43 | namespace { |
| 44 | |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 45 | constexpr int32_t VERSION_WITH_UNIQUE_ID_SUPPORT = 2; |
| 46 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 47 | #define INSTANTIATE_REM_PROV_AIDL_TEST(name) \ |
Seth Moore | 6305e23 | 2021-07-27 14:20:17 -0700 | [diff] [blame] | 48 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name); \ |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 49 | INSTANTIATE_TEST_SUITE_P( \ |
| 50 | PerInstance, name, \ |
| 51 | testing::ValuesIn(VtsRemotelyProvisionedComponentTests::build_params()), \ |
| 52 | ::android::PrintInstanceNameToString) |
| 53 | |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 54 | using ::android::sp; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 55 | using bytevec = std::vector<uint8_t>; |
| 56 | using testing::MatchesRegex; |
| 57 | using namespace remote_prov; |
| 58 | using namespace keymaster; |
| 59 | |
Max Bires | 8b09c50 | 2022-01-30 20:03:16 -0800 | [diff] [blame] | 60 | std::set<std::string> getAllowedVbStates() { |
| 61 | return {"green", "yellow", "orange"}; |
| 62 | } |
| 63 | |
| 64 | std::set<std::string> getAllowedBootloaderStates() { |
| 65 | return {"locked", "unlocked"}; |
| 66 | } |
| 67 | |
| 68 | std::set<std::string> getAllowedSecurityLevels() { |
| 69 | return {"tee", "strongbox"}; |
| 70 | } |
| 71 | |
| 72 | std::set<std::string> getAllowedAttIdStates() { |
| 73 | return {"locked", "open"}; |
| 74 | } |
| 75 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 76 | bytevec string_to_bytevec(const char* s) { |
| 77 | const uint8_t* p = reinterpret_cast<const uint8_t*>(s); |
| 78 | return bytevec(p, p + strlen(s)); |
| 79 | } |
| 80 | |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 81 | ErrMsgOr<MacedPublicKey> corrupt_maced_key(const MacedPublicKey& macedPubKey) { |
| 82 | auto [coseMac0, _, mac0ParseErr] = cppbor::parse(macedPubKey.macedKey); |
| 83 | if (!coseMac0 || coseMac0->asArray()->size() != kCoseMac0EntryCount) { |
| 84 | return "COSE Mac0 parse failed"; |
| 85 | } |
| 86 | auto protParams = coseMac0->asArray()->get(kCoseMac0ProtectedParams)->asBstr(); |
| 87 | auto unprotParams = coseMac0->asArray()->get(kCoseMac0UnprotectedParams)->asMap(); |
| 88 | auto payload = coseMac0->asArray()->get(kCoseMac0Payload)->asBstr(); |
| 89 | auto tag = coseMac0->asArray()->get(kCoseMac0Tag)->asBstr(); |
| 90 | if (!protParams || !unprotParams || !payload || !tag) { |
| 91 | return "Invalid COSE_Sign1: missing content"; |
| 92 | } |
| 93 | auto corruptMac0 = cppbor::Array(); |
| 94 | corruptMac0.add(protParams->clone()); |
| 95 | corruptMac0.add(unprotParams->clone()); |
| 96 | corruptMac0.add(payload->clone()); |
| 97 | vector<uint8_t> tagData = tag->value(); |
| 98 | tagData[0] ^= 0x08; |
| 99 | tagData[tagData.size() - 1] ^= 0x80; |
| 100 | corruptMac0.add(cppbor::Bstr(tagData)); |
| 101 | |
| 102 | return MacedPublicKey{corruptMac0.encode()}; |
| 103 | } |
| 104 | |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 105 | ErrMsgOr<cppbor::Array> corrupt_sig(const cppbor::Array* coseSign1) { |
| 106 | if (coseSign1->size() != kCoseSign1EntryCount) { |
| 107 | return "Invalid COSE_Sign1, wrong entry count"; |
| 108 | } |
| 109 | const cppbor::Bstr* protectedParams = coseSign1->get(kCoseSign1ProtectedParams)->asBstr(); |
| 110 | const cppbor::Map* unprotectedParams = coseSign1->get(kCoseSign1UnprotectedParams)->asMap(); |
| 111 | const cppbor::Bstr* payload = coseSign1->get(kCoseSign1Payload)->asBstr(); |
| 112 | const cppbor::Bstr* signature = coseSign1->get(kCoseSign1Signature)->asBstr(); |
| 113 | if (!protectedParams || !unprotectedParams || !payload || !signature) { |
| 114 | return "Invalid COSE_Sign1: missing content"; |
| 115 | } |
| 116 | |
| 117 | auto corruptSig = cppbor::Array(); |
| 118 | corruptSig.add(protectedParams->clone()); |
| 119 | corruptSig.add(unprotectedParams->clone()); |
| 120 | corruptSig.add(payload->clone()); |
| 121 | vector<uint8_t> sigData = signature->value(); |
| 122 | sigData[0] ^= 0x08; |
| 123 | corruptSig.add(cppbor::Bstr(sigData)); |
| 124 | |
| 125 | return std::move(corruptSig); |
| 126 | } |
| 127 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 128 | ErrMsgOr<bytevec> corrupt_sig_chain(const bytevec& encodedEekChain, int which) { |
| 129 | auto [chain, _, parseErr] = cppbor::parse(encodedEekChain); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 130 | if (!chain || !chain->asArray()) { |
| 131 | return "EekChain parse failed"; |
| 132 | } |
| 133 | |
| 134 | cppbor::Array* eekChain = chain->asArray(); |
| 135 | if (which >= eekChain->size()) { |
| 136 | return "selected sig out of range"; |
| 137 | } |
| 138 | auto corruptChain = cppbor::Array(); |
| 139 | |
| 140 | for (int ii = 0; ii < eekChain->size(); ++ii) { |
| 141 | if (ii == which) { |
| 142 | auto sig = corrupt_sig(eekChain->get(which)->asArray()); |
| 143 | if (!sig) { |
| 144 | return "Failed to build corrupted signature" + sig.moveMessage(); |
| 145 | } |
| 146 | corruptChain.add(sig.moveValue()); |
| 147 | } else { |
| 148 | corruptChain.add(eekChain->get(ii)->clone()); |
| 149 | } |
| 150 | } |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 151 | return corruptChain.encode(); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 152 | } |
| 153 | |
David Drysdale | 4d3c298 | 2021-03-31 18:21:40 +0100 | [diff] [blame] | 154 | string device_suffix(const string& name) { |
| 155 | size_t pos = name.find('/'); |
| 156 | if (pos == string::npos) { |
| 157 | return name; |
| 158 | } |
| 159 | return name.substr(pos + 1); |
| 160 | } |
| 161 | |
| 162 | bool matching_keymint_device(const string& rp_name, std::shared_ptr<IKeyMintDevice>* keyMint) { |
| 163 | string rp_suffix = device_suffix(rp_name); |
| 164 | |
| 165 | vector<string> km_names = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor); |
| 166 | for (const string& km_name : km_names) { |
| 167 | // If the suffix of the KeyMint instance equals the suffix of the |
| 168 | // RemotelyProvisionedComponent instance, assume they match. |
| 169 | if (device_suffix(km_name) == rp_suffix && AServiceManager_isDeclared(km_name.c_str())) { |
| 170 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(km_name.c_str())); |
| 171 | *keyMint = IKeyMintDevice::fromBinder(binder); |
| 172 | return true; |
| 173 | } |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 178 | } // namespace |
| 179 | |
| 180 | class VtsRemotelyProvisionedComponentTests : public testing::TestWithParam<std::string> { |
| 181 | public: |
| 182 | virtual void SetUp() override { |
| 183 | if (AServiceManager_isDeclared(GetParam().c_str())) { |
| 184 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(GetParam().c_str())); |
| 185 | provisionable_ = IRemotelyProvisionedComponent::fromBinder(binder); |
| 186 | } |
| 187 | ASSERT_NE(provisionable_, nullptr); |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 188 | ASSERT_TRUE(provisionable_->getHardwareInfo(&rpcHardwareInfo).isOk()); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static vector<string> build_params() { |
| 192 | auto params = ::android::getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor); |
| 193 | return params; |
| 194 | } |
| 195 | |
| 196 | protected: |
| 197 | std::shared_ptr<IRemotelyProvisionedComponent> provisionable_; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 198 | RpcHardwareInfo rpcHardwareInfo; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 199 | }; |
| 200 | |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 201 | /** |
| 202 | * Verify that every implementation reports a different unique id. |
| 203 | */ |
| 204 | TEST(NonParameterizedTests, eachRpcHasAUniqueId) { |
| 205 | std::set<std::string> uniqueIds; |
| 206 | for (auto hal : ::android::getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor)) { |
| 207 | ASSERT_TRUE(AServiceManager_isDeclared(hal.c_str())); |
| 208 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(hal.c_str())); |
| 209 | std::shared_ptr<IRemotelyProvisionedComponent> rpc = |
| 210 | IRemotelyProvisionedComponent::fromBinder(binder); |
| 211 | ASSERT_NE(rpc, nullptr); |
| 212 | |
| 213 | RpcHardwareInfo hwInfo; |
| 214 | ASSERT_TRUE(rpc->getHardwareInfo(&hwInfo).isOk()); |
| 215 | |
| 216 | int32_t version; |
| 217 | ASSERT_TRUE(rpc->getInterfaceVersion(&version).isOk()); |
| 218 | if (version >= VERSION_WITH_UNIQUE_ID_SUPPORT) { |
| 219 | ASSERT_TRUE(hwInfo.uniqueId); |
| 220 | auto [_, wasInserted] = uniqueIds.insert(*hwInfo.uniqueId); |
| 221 | EXPECT_TRUE(wasInserted); |
| 222 | } else { |
| 223 | ASSERT_FALSE(hwInfo.uniqueId); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | using GetHardwareInfoTests = VtsRemotelyProvisionedComponentTests; |
| 229 | |
| 230 | INSTANTIATE_REM_PROV_AIDL_TEST(GetHardwareInfoTests); |
| 231 | |
| 232 | /** |
| 233 | * Verify that a valid curve is reported by the implementation. |
| 234 | */ |
| 235 | TEST_P(GetHardwareInfoTests, supportsValidCurve) { |
| 236 | RpcHardwareInfo hwInfo; |
| 237 | ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk()); |
| 238 | |
| 239 | const std::set<int> validCurves = {RpcHardwareInfo::CURVE_P256, RpcHardwareInfo::CURVE_25519}; |
| 240 | ASSERT_EQ(validCurves.count(hwInfo.supportedEekCurve), 1) |
| 241 | << "Invalid curve: " << hwInfo.supportedEekCurve; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Verify that the unique id is within the length limits as described in RpcHardwareInfo.aidl. |
| 246 | */ |
| 247 | TEST_P(GetHardwareInfoTests, uniqueId) { |
| 248 | int32_t version; |
| 249 | ASSERT_TRUE(provisionable_->getInterfaceVersion(&version).isOk()); |
| 250 | |
| 251 | if (version < VERSION_WITH_UNIQUE_ID_SUPPORT) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | RpcHardwareInfo hwInfo; |
| 256 | ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk()); |
| 257 | ASSERT_TRUE(hwInfo.uniqueId); |
| 258 | EXPECT_GE(hwInfo.uniqueId->size(), 1); |
| 259 | EXPECT_LE(hwInfo.uniqueId->size(), 32); |
| 260 | } |
| 261 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 262 | using GenerateKeyTests = VtsRemotelyProvisionedComponentTests; |
| 263 | |
| 264 | INSTANTIATE_REM_PROV_AIDL_TEST(GenerateKeyTests); |
| 265 | |
| 266 | /** |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 267 | * Generate and validate a production-mode key. MAC tag can't be verified, but |
| 268 | * the private key blob should be usable in KeyMint operations. |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 269 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 270 | TEST_P(GenerateKeyTests, generateEcdsaP256Key_prodMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 271 | MacedPublicKey macedPubKey; |
| 272 | bytevec privateKeyBlob; |
| 273 | bool testMode = false; |
| 274 | auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob); |
| 275 | ASSERT_TRUE(status.isOk()); |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 276 | vector<uint8_t> coseKeyData; |
| 277 | check_maced_pubkey(macedPubKey, testMode, &coseKeyData); |
David Drysdale | 4d3c298 | 2021-03-31 18:21:40 +0100 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Generate and validate a production-mode key, then use it as a KeyMint attestation key. |
| 282 | */ |
| 283 | TEST_P(GenerateKeyTests, generateAndUseEcdsaP256Key_prodMode) { |
| 284 | // See if there is a matching IKeyMintDevice for this IRemotelyProvisionedComponent. |
| 285 | std::shared_ptr<IKeyMintDevice> keyMint; |
| 286 | if (!matching_keymint_device(GetParam(), &keyMint)) { |
| 287 | // No matching IKeyMintDevice. |
| 288 | GTEST_SKIP() << "Skipping key use test as no matching KeyMint device found"; |
| 289 | return; |
| 290 | } |
| 291 | KeyMintHardwareInfo info; |
| 292 | ASSERT_TRUE(keyMint->getHardwareInfo(&info).isOk()); |
| 293 | |
| 294 | MacedPublicKey macedPubKey; |
| 295 | bytevec privateKeyBlob; |
| 296 | bool testMode = false; |
| 297 | auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob); |
| 298 | ASSERT_TRUE(status.isOk()); |
| 299 | vector<uint8_t> coseKeyData; |
| 300 | check_maced_pubkey(macedPubKey, testMode, &coseKeyData); |
| 301 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 302 | AttestationKey attestKey; |
| 303 | attestKey.keyBlob = std::move(privateKeyBlob); |
| 304 | attestKey.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 305 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 306 | // Generate an ECDSA key that is attested by the generated P256 keypair. |
| 307 | AuthorizationSet keyDesc = AuthorizationSetBuilder() |
| 308 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | 915ce25 | 2021-10-14 15:17:36 +0100 | [diff] [blame] | 309 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 310 | .AttestationChallenge("foo") |
| 311 | .AttestationApplicationId("bar") |
| 312 | .Digest(Digest::NONE) |
| 313 | .SetDefaultValidity(); |
| 314 | KeyCreationResult creationResult; |
| 315 | auto result = keyMint->generateKey(keyDesc.vector_data(), attestKey, &creationResult); |
| 316 | ASSERT_TRUE(result.isOk()); |
| 317 | vector<uint8_t> attested_key_blob = std::move(creationResult.keyBlob); |
| 318 | vector<KeyCharacteristics> attested_key_characteristics = |
| 319 | std::move(creationResult.keyCharacteristics); |
| 320 | vector<Certificate> attested_key_cert_chain = std::move(creationResult.certificateChain); |
| 321 | EXPECT_EQ(attested_key_cert_chain.size(), 1); |
| 322 | |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 323 | int32_t aidl_version = 0; |
| 324 | ASSERT_TRUE(keyMint->getInterfaceVersion(&aidl_version).isOk()); |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 325 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 326 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 327 | EXPECT_TRUE(verify_attestation_record(aidl_version, "foo", "bar", sw_enforced, hw_enforced, |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 328 | info.securityLevel, |
| 329 | attested_key_cert_chain[0].encodedCertificate)); |
| 330 | |
| 331 | // Attestation by itself is not valid (last entry is not self-signed). |
| 332 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 333 | |
| 334 | // The signature over the attested key should correspond to the P256 public key. |
| 335 | X509_Ptr key_cert(parse_cert_blob(attested_key_cert_chain[0].encodedCertificate)); |
| 336 | ASSERT_TRUE(key_cert.get()); |
| 337 | EVP_PKEY_Ptr signing_pubkey; |
| 338 | p256_pub_key(coseKeyData, &signing_pubkey); |
| 339 | ASSERT_TRUE(signing_pubkey.get()); |
| 340 | |
| 341 | ASSERT_TRUE(X509_verify(key_cert.get(), signing_pubkey.get())) |
| 342 | << "Verification of attested certificate failed " |
| 343 | << "OpenSSL error string: " << ERR_error_string(ERR_get_error(), NULL); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Generate and validate a test-mode key. |
| 348 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 349 | TEST_P(GenerateKeyTests, generateEcdsaP256Key_testMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 350 | MacedPublicKey macedPubKey; |
| 351 | bytevec privateKeyBlob; |
| 352 | bool testMode = true; |
| 353 | auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob); |
| 354 | ASSERT_TRUE(status.isOk()); |
| 355 | |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 356 | check_maced_pubkey(macedPubKey, testMode, nullptr); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | class CertificateRequestTest : public VtsRemotelyProvisionedComponentTests { |
| 360 | protected: |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 361 | CertificateRequestTest() : eekId_(string_to_bytevec("eekid")), challenge_(randomBytes(32)) { |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 364 | void generateTestEekChain(size_t eekLength) { |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 365 | auto chain = generateEekChain(rpcHardwareInfo.supportedEekCurve, eekLength, eekId_); |
David Drysdale | 08696a7 | 2022-03-10 10:43:25 +0000 | [diff] [blame^] | 366 | ASSERT_TRUE(chain) << chain.message(); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 367 | if (chain) testEekChain_ = chain.moveValue(); |
| 368 | testEekLength_ = eekLength; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void generateKeys(bool testMode, size_t numKeys) { |
| 372 | keysToSign_ = std::vector<MacedPublicKey>(numKeys); |
| 373 | cborKeysToSign_ = cppbor::Array(); |
| 374 | |
| 375 | for (auto& key : keysToSign_) { |
| 376 | bytevec privateKeyBlob; |
| 377 | auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &key, &privateKeyBlob); |
| 378 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 379 | |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 380 | vector<uint8_t> payload_value; |
| 381 | check_maced_pubkey(key, testMode, &payload_value); |
| 382 | cborKeysToSign_.add(cppbor::EncodedItem(payload_value)); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 386 | ErrMsgOr<bytevec> getSessionKey(ErrMsgOr<std::pair<bytevec, bytevec>>& senderPubkey) { |
| 387 | if (rpcHardwareInfo.supportedEekCurve == RpcHardwareInfo::CURVE_25519 || |
| 388 | rpcHardwareInfo.supportedEekCurve == RpcHardwareInfo::CURVE_NONE) { |
| 389 | return x25519_HKDF_DeriveKey(testEekChain_.last_pubkey, testEekChain_.last_privkey, |
| 390 | senderPubkey->first, false /* senderIsA */); |
| 391 | } else { |
| 392 | return ECDH_HKDF_DeriveKey(testEekChain_.last_pubkey, testEekChain_.last_privkey, |
| 393 | senderPubkey->first, false /* senderIsA */); |
| 394 | } |
| 395 | } |
| 396 | |
David Drysdale | f6fc5a6 | 2021-03-31 16:14:31 +0100 | [diff] [blame] | 397 | void checkProtectedData(const DeviceInfo& deviceInfo, const cppbor::Array& keysToSign, |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 398 | const bytevec& keysToSignMac, const ProtectedData& protectedData, |
| 399 | std::vector<BccEntryData>* bccOutput = nullptr) { |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 400 | auto [parsedProtectedData, _, protDataErrMsg] = cppbor::parse(protectedData.protectedData); |
| 401 | ASSERT_TRUE(parsedProtectedData) << protDataErrMsg; |
| 402 | ASSERT_TRUE(parsedProtectedData->asArray()); |
| 403 | ASSERT_EQ(parsedProtectedData->asArray()->size(), kCoseEncryptEntryCount); |
| 404 | |
| 405 | auto senderPubkey = getSenderPubKeyFromCoseEncrypt(parsedProtectedData); |
| 406 | ASSERT_TRUE(senderPubkey) << senderPubkey.message(); |
| 407 | EXPECT_EQ(senderPubkey->second, eekId_); |
| 408 | |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 409 | auto sessionKey = getSessionKey(senderPubkey); |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 410 | ASSERT_TRUE(sessionKey) << sessionKey.message(); |
| 411 | |
| 412 | auto protectedDataPayload = |
| 413 | decryptCoseEncrypt(*sessionKey, parsedProtectedData.get(), bytevec{} /* aad */); |
| 414 | ASSERT_TRUE(protectedDataPayload) << protectedDataPayload.message(); |
| 415 | |
| 416 | auto [parsedPayload, __, payloadErrMsg] = cppbor::parse(*protectedDataPayload); |
| 417 | ASSERT_TRUE(parsedPayload) << "Failed to parse payload: " << payloadErrMsg; |
| 418 | ASSERT_TRUE(parsedPayload->asArray()); |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 419 | // Strongbox may contain additional certificate chain. |
| 420 | EXPECT_LE(parsedPayload->asArray()->size(), 3U); |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 421 | |
| 422 | auto& signedMac = parsedPayload->asArray()->get(0); |
| 423 | auto& bcc = parsedPayload->asArray()->get(1); |
| 424 | ASSERT_TRUE(signedMac && signedMac->asArray()); |
| 425 | ASSERT_TRUE(bcc && bcc->asArray()); |
| 426 | |
| 427 | // BCC is [ pubkey, + BccEntry] |
| 428 | auto bccContents = validateBcc(bcc->asArray()); |
| 429 | ASSERT_TRUE(bccContents) << "\n" << bccContents.message() << "\n" << prettyPrint(bcc.get()); |
| 430 | ASSERT_GT(bccContents->size(), 0U); |
| 431 | |
David Drysdale | f6fc5a6 | 2021-03-31 16:14:31 +0100 | [diff] [blame] | 432 | auto [deviceInfoMap, __2, deviceInfoErrMsg] = cppbor::parse(deviceInfo.deviceInfo); |
| 433 | ASSERT_TRUE(deviceInfoMap) << "Failed to parse deviceInfo: " << deviceInfoErrMsg; |
| 434 | ASSERT_TRUE(deviceInfoMap->asMap()); |
| 435 | |
Max Bires | 8c255e6 | 2022-02-02 12:03:28 -0800 | [diff] [blame] | 436 | checkDeviceInfo(deviceInfoMap->asMap(), deviceInfo.deviceInfo); |
Max Bires | 8b09c50 | 2022-01-30 20:03:16 -0800 | [diff] [blame] | 437 | |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 438 | auto& signingKey = bccContents->back().pubKey; |
Seth Moore | 798188a | 2021-06-17 10:58:27 -0700 | [diff] [blame] | 439 | auto macKey = verifyAndParseCoseSign1(signedMac->asArray(), signingKey, |
David Drysdale | f6fc5a6 | 2021-03-31 16:14:31 +0100 | [diff] [blame] | 440 | cppbor::Array() // SignedMacAad |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 441 | .add(challenge_) |
David Drysdale | f6fc5a6 | 2021-03-31 16:14:31 +0100 | [diff] [blame] | 442 | .add(std::move(deviceInfoMap)) |
Max Bires | 8dff0b3 | 2021-05-26 13:05:09 -0700 | [diff] [blame] | 443 | .add(keysToSignMac) |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 444 | .encode()); |
| 445 | ASSERT_TRUE(macKey) << macKey.message(); |
| 446 | |
| 447 | auto coseMac0 = cppbor::Array() |
| 448 | .add(cppbor::Map() // protected |
| 449 | .add(ALGORITHM, HMAC_256) |
| 450 | .canonicalize() |
| 451 | .encode()) |
| 452 | .add(cppbor::Map()) // unprotected |
| 453 | .add(keysToSign.encode()) // payload (keysToSign) |
| 454 | .add(keysToSignMac); // tag |
| 455 | |
| 456 | auto macPayload = verifyAndParseCoseMac0(&coseMac0, *macKey); |
| 457 | ASSERT_TRUE(macPayload) << macPayload.message(); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 458 | |
| 459 | if (bccOutput) { |
| 460 | *bccOutput = std::move(*bccContents); |
| 461 | } |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Max Bires | 8b09c50 | 2022-01-30 20:03:16 -0800 | [diff] [blame] | 464 | void checkType(const cppbor::Map* devInfo, uint8_t majorType, std::string entryName) { |
| 465 | const auto& val = devInfo->get(entryName); |
| 466 | ASSERT_TRUE(val) << entryName << " does not exist"; |
| 467 | ASSERT_EQ(val->type(), majorType) << entryName << " has the wrong type."; |
| 468 | switch (majorType) { |
| 469 | case cppbor::TSTR: |
| 470 | ASSERT_GT(val->asTstr()->value().size(), 0); |
| 471 | break; |
| 472 | case cppbor::BSTR: |
| 473 | ASSERT_GT(val->asBstr()->value().size(), 0); |
| 474 | break; |
| 475 | default: |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | |
Max Bires | 8c255e6 | 2022-02-02 12:03:28 -0800 | [diff] [blame] | 480 | void checkDeviceInfo(const cppbor::Map* deviceInfo, bytevec deviceInfoBytes) { |
Max Bires | 8b09c50 | 2022-01-30 20:03:16 -0800 | [diff] [blame] | 481 | const auto& version = deviceInfo->get("version"); |
| 482 | ASSERT_TRUE(version); |
| 483 | ASSERT_TRUE(version->asUint()); |
| 484 | RpcHardwareInfo info; |
| 485 | provisionable_->getHardwareInfo(&info); |
| 486 | ASSERT_EQ(version->asUint()->value(), info.versionNumber); |
| 487 | std::set<std::string> allowList; |
| 488 | switch (version->asUint()->value()) { |
| 489 | // These fields became mandated in version 2. |
| 490 | case 2: |
| 491 | checkType(deviceInfo, cppbor::TSTR, "brand"); |
| 492 | checkType(deviceInfo, cppbor::TSTR, "manufacturer"); |
| 493 | checkType(deviceInfo, cppbor::TSTR, "product"); |
| 494 | checkType(deviceInfo, cppbor::TSTR, "model"); |
| 495 | checkType(deviceInfo, cppbor::TSTR, "device"); |
| 496 | // TODO: Refactor the KeyMint code that validates these fields and include it here. |
| 497 | checkType(deviceInfo, cppbor::TSTR, "vb_state"); |
| 498 | allowList = getAllowedVbStates(); |
| 499 | ASSERT_NE(allowList.find(deviceInfo->get("vb_state")->asTstr()->value()), |
| 500 | allowList.end()); |
| 501 | checkType(deviceInfo, cppbor::TSTR, "bootloader_state"); |
| 502 | allowList = getAllowedBootloaderStates(); |
| 503 | ASSERT_NE(allowList.find(deviceInfo->get("bootloader_state")->asTstr()->value()), |
| 504 | allowList.end()); |
| 505 | checkType(deviceInfo, cppbor::BSTR, "vbmeta_digest"); |
Max Bires | 8b09c50 | 2022-01-30 20:03:16 -0800 | [diff] [blame] | 506 | checkType(deviceInfo, cppbor::UINT, "system_patch_level"); |
| 507 | checkType(deviceInfo, cppbor::UINT, "boot_patch_level"); |
| 508 | checkType(deviceInfo, cppbor::UINT, "vendor_patch_level"); |
| 509 | checkType(deviceInfo, cppbor::UINT, "fused"); |
| 510 | ASSERT_LT(deviceInfo->get("fused")->asUint()->value(), 2); // Must be 0 or 1. |
| 511 | checkType(deviceInfo, cppbor::TSTR, "security_level"); |
| 512 | allowList = getAllowedSecurityLevels(); |
| 513 | ASSERT_NE(allowList.find(deviceInfo->get("security_level")->asTstr()->value()), |
| 514 | allowList.end()); |
Max Bires | 9c28a1e | 2022-02-03 19:14:06 -0800 | [diff] [blame] | 515 | if (deviceInfo->get("security_level")->asTstr()->value() == "tee") { |
| 516 | checkType(deviceInfo, cppbor::TSTR, "os_version"); |
| 517 | } |
Max Bires | 8b09c50 | 2022-01-30 20:03:16 -0800 | [diff] [blame] | 518 | break; |
| 519 | case 1: |
| 520 | checkType(deviceInfo, cppbor::TSTR, "security_level"); |
| 521 | allowList = getAllowedSecurityLevels(); |
| 522 | ASSERT_NE(allowList.find(deviceInfo->get("security_level")->asTstr()->value()), |
| 523 | allowList.end()); |
| 524 | if (version->asUint()->value() == 1) { |
| 525 | checkType(deviceInfo, cppbor::TSTR, "att_id_state"); |
| 526 | allowList = getAllowedAttIdStates(); |
| 527 | ASSERT_NE(allowList.find(deviceInfo->get("att_id_state")->asTstr()->value()), |
| 528 | allowList.end()); |
| 529 | } |
| 530 | break; |
| 531 | default: |
| 532 | FAIL() << "Unrecognized version: " << version->asUint()->value(); |
| 533 | } |
Max Bires | 8c255e6 | 2022-02-02 12:03:28 -0800 | [diff] [blame] | 534 | ASSERT_EQ(deviceInfo->clone()->asMap()->canonicalize().encode(), deviceInfoBytes) |
| 535 | << "DeviceInfo ordering is non-canonical."; |
Max Bires | 8b09c50 | 2022-01-30 20:03:16 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 538 | bytevec eekId_; |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 539 | size_t testEekLength_; |
| 540 | EekChain testEekChain_; |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 541 | bytevec challenge_; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 542 | std::vector<MacedPublicKey> keysToSign_; |
| 543 | cppbor::Array cborKeysToSign_; |
| 544 | }; |
| 545 | |
| 546 | /** |
| 547 | * Generate an empty certificate request in test mode, and decrypt and verify the structure and |
| 548 | * content. |
| 549 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 550 | TEST_P(CertificateRequestTest, EmptyRequest_testMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 551 | bool testMode = true; |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 552 | for (size_t eekLength : {2, 3, 7}) { |
| 553 | SCOPED_TRACE(testing::Message() << "EEK of length " << eekLength); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 554 | generateTestEekChain(eekLength); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 555 | |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 556 | bytevec keysToSignMac; |
| 557 | DeviceInfo deviceInfo; |
| 558 | ProtectedData protectedData; |
| 559 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 560 | testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo, |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 561 | &protectedData, &keysToSignMac); |
| 562 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 563 | |
David Drysdale | f6fc5a6 | 2021-03-31 16:14:31 +0100 | [diff] [blame] | 564 | checkProtectedData(deviceInfo, cppbor::Array(), keysToSignMac, protectedData); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 565 | } |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /** |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 569 | * Ensure that test mode outputs a unique BCC root key every time we request a |
| 570 | * certificate request. Else, it's possible that the test mode API could be used |
| 571 | * to fingerprint devices. Only the GEEK should be allowed to decrypt the same |
| 572 | * device public key multiple times. |
| 573 | */ |
| 574 | TEST_P(CertificateRequestTest, NewKeyPerCallInTestMode) { |
| 575 | constexpr bool testMode = true; |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 576 | |
| 577 | bytevec keysToSignMac; |
| 578 | DeviceInfo deviceInfo; |
| 579 | ProtectedData protectedData; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 580 | generateTestEekChain(3); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 581 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 582 | testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo, |
| 583 | &protectedData, &keysToSignMac); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 584 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 585 | |
| 586 | std::vector<BccEntryData> firstBcc; |
| 587 | checkProtectedData(deviceInfo, /*keysToSign=*/cppbor::Array(), keysToSignMac, protectedData, |
| 588 | &firstBcc); |
| 589 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 590 | status = provisionable_->generateCertificateRequest( |
| 591 | testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo, |
| 592 | &protectedData, &keysToSignMac); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 593 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 594 | |
| 595 | std::vector<BccEntryData> secondBcc; |
| 596 | checkProtectedData(deviceInfo, /*keysToSign=*/cppbor::Array(), keysToSignMac, protectedData, |
| 597 | &secondBcc); |
| 598 | |
| 599 | // Verify that none of the keys in the first BCC are repeated in the second one. |
| 600 | for (const auto& i : firstBcc) { |
| 601 | for (auto& j : secondBcc) { |
| 602 | ASSERT_THAT(i.pubKey, testing::Not(testing::ElementsAreArray(j.pubKey))) |
| 603 | << "Found a repeated pubkey in two generateCertificateRequest test mode calls"; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /** |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 609 | * Generate an empty certificate request in prod mode. This test must be run explicitly, and |
| 610 | * is not run by default. Not all devices are GMS devices, and therefore they do not all |
| 611 | * trust the Google EEK root. |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 612 | */ |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 613 | TEST_P(CertificateRequestTest, DISABLED_EmptyRequest_prodMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 614 | bool testMode = false; |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 615 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 616 | bytevec keysToSignMac; |
| 617 | DeviceInfo deviceInfo; |
| 618 | ProtectedData protectedData; |
| 619 | auto status = provisionable_->generateCertificateRequest( |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 620 | testMode, {} /* keysToSign */, getProdEekChain(rpcHardwareInfo.supportedEekCurve), |
| 621 | challenge_, &deviceInfo, &protectedData, &keysToSignMac); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 622 | EXPECT_TRUE(status.isOk()); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Generate a non-empty certificate request in test mode. Decrypt, parse and validate the contents. |
| 627 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 628 | TEST_P(CertificateRequestTest, NonEmptyRequest_testMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 629 | bool testMode = true; |
| 630 | generateKeys(testMode, 4 /* numKeys */); |
| 631 | |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 632 | for (size_t eekLength : {2, 3, 7}) { |
| 633 | SCOPED_TRACE(testing::Message() << "EEK of length " << eekLength); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 634 | generateTestEekChain(eekLength); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 635 | |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 636 | bytevec keysToSignMac; |
| 637 | DeviceInfo deviceInfo; |
| 638 | ProtectedData protectedData; |
| 639 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 640 | testMode, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo, &protectedData, |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 641 | &keysToSignMac); |
| 642 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 643 | |
David Drysdale | f6fc5a6 | 2021-03-31 16:14:31 +0100 | [diff] [blame] | 644 | checkProtectedData(deviceInfo, cborKeysToSign_, keysToSignMac, protectedData); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 645 | } |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /** |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 649 | * Generate a non-empty certificate request in prod mode. This test must be run explicitly, and |
| 650 | * is not run by default. Not all devices are GMS devices, and therefore they do not all |
| 651 | * trust the Google EEK root. |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 652 | */ |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 653 | TEST_P(CertificateRequestTest, DISABLED_NonEmptyRequest_prodMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 654 | bool testMode = false; |
| 655 | generateKeys(testMode, 4 /* numKeys */); |
| 656 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 657 | bytevec keysToSignMac; |
| 658 | DeviceInfo deviceInfo; |
| 659 | ProtectedData protectedData; |
| 660 | auto status = provisionable_->generateCertificateRequest( |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 661 | testMode, keysToSign_, getProdEekChain(rpcHardwareInfo.supportedEekCurve), challenge_, |
| 662 | &deviceInfo, &protectedData, &keysToSignMac); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 663 | EXPECT_TRUE(status.isOk()); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /** |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 667 | * Generate a non-empty certificate request in test mode, but with the MAC corrupted on the keypair. |
| 668 | */ |
| 669 | TEST_P(CertificateRequestTest, NonEmptyRequestCorruptMac_testMode) { |
| 670 | bool testMode = true; |
| 671 | generateKeys(testMode, 1 /* numKeys */); |
David Drysdale | 08696a7 | 2022-03-10 10:43:25 +0000 | [diff] [blame^] | 672 | auto result = corrupt_maced_key(keysToSign_[0]); |
| 673 | ASSERT_TRUE(result) << result.moveMessage(); |
| 674 | MacedPublicKey keyWithCorruptMac = result.moveValue(); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 675 | |
| 676 | bytevec keysToSignMac; |
| 677 | DeviceInfo deviceInfo; |
| 678 | ProtectedData protectedData; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 679 | generateTestEekChain(3); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 680 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 681 | testMode, {keyWithCorruptMac}, testEekChain_.chain, challenge_, &deviceInfo, |
| 682 | &protectedData, &keysToSignMac); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 683 | ASSERT_FALSE(status.isOk()) << status.getMessage(); |
| 684 | EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC); |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * Generate a non-empty certificate request in prod mode, but with the MAC corrupted on the keypair. |
| 689 | */ |
| 690 | TEST_P(CertificateRequestTest, NonEmptyRequestCorruptMac_prodMode) { |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 691 | bool testMode = false; |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 692 | generateKeys(testMode, 1 /* numKeys */); |
David Drysdale | 08696a7 | 2022-03-10 10:43:25 +0000 | [diff] [blame^] | 693 | auto result = corrupt_maced_key(keysToSign_[0]); |
| 694 | ASSERT_TRUE(result) << result.moveMessage(); |
| 695 | MacedPublicKey keyWithCorruptMac = result.moveValue(); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 696 | |
| 697 | bytevec keysToSignMac; |
| 698 | DeviceInfo deviceInfo; |
| 699 | ProtectedData protectedData; |
| 700 | auto status = provisionable_->generateCertificateRequest( |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 701 | testMode, {keyWithCorruptMac}, getProdEekChain(rpcHardwareInfo.supportedEekCurve), |
| 702 | challenge_, &deviceInfo, &protectedData, &keysToSignMac); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 703 | ASSERT_FALSE(status.isOk()) << status.getMessage(); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 704 | EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /** |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 708 | * Generate a non-empty certificate request in prod mode that has a corrupt EEK chain. |
| 709 | * Confirm that the request is rejected. |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 710 | */ |
| 711 | TEST_P(CertificateRequestTest, NonEmptyCorruptEekRequest_prodMode) { |
| 712 | bool testMode = false; |
| 713 | generateKeys(testMode, 4 /* numKeys */); |
| 714 | |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 715 | auto prodEekChain = getProdEekChain(rpcHardwareInfo.supportedEekCurve); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 716 | auto [parsedChain, _, parseErr] = cppbor::parse(prodEekChain); |
| 717 | ASSERT_NE(parsedChain, nullptr) << parseErr; |
| 718 | ASSERT_NE(parsedChain->asArray(), nullptr); |
| 719 | |
| 720 | for (int ii = 0; ii < parsedChain->asArray()->size(); ++ii) { |
| 721 | auto chain = corrupt_sig_chain(prodEekChain, ii); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 722 | ASSERT_TRUE(chain) << chain.message(); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 723 | |
| 724 | bytevec keysToSignMac; |
| 725 | DeviceInfo deviceInfo; |
| 726 | ProtectedData protectedData; |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 727 | auto status = provisionable_->generateCertificateRequest(testMode, keysToSign_, *chain, |
| 728 | challenge_, &deviceInfo, |
| 729 | &protectedData, &keysToSignMac); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 730 | ASSERT_FALSE(status.isOk()); |
| 731 | ASSERT_EQ(status.getServiceSpecificError(), |
| 732 | BnRemotelyProvisionedComponent::STATUS_INVALID_EEK); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * Generate a non-empty certificate request in prod mode that has an incomplete EEK chain. |
| 738 | * Confirm that the request is rejected. |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 739 | */ |
| 740 | TEST_P(CertificateRequestTest, NonEmptyIncompleteEekRequest_prodMode) { |
| 741 | bool testMode = false; |
| 742 | generateKeys(testMode, 4 /* numKeys */); |
| 743 | |
| 744 | // Build an EEK chain that omits the first self-signed cert. |
| 745 | auto truncatedChain = cppbor::Array(); |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 746 | auto [chain, _, parseErr] = cppbor::parse(getProdEekChain(rpcHardwareInfo.supportedEekCurve)); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 747 | ASSERT_TRUE(chain); |
| 748 | auto eekChain = chain->asArray(); |
| 749 | ASSERT_NE(eekChain, nullptr); |
| 750 | for (size_t ii = 1; ii < eekChain->size(); ii++) { |
| 751 | truncatedChain.add(eekChain->get(ii)->clone()); |
| 752 | } |
| 753 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 754 | bytevec keysToSignMac; |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 755 | DeviceInfo deviceInfo; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 756 | ProtectedData protectedData; |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 757 | auto status = provisionable_->generateCertificateRequest( |
| 758 | testMode, keysToSign_, truncatedChain.encode(), challenge_, &deviceInfo, &protectedData, |
| 759 | &keysToSignMac); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 760 | ASSERT_FALSE(status.isOk()); |
| 761 | ASSERT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_EEK); |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Generate a non-empty certificate request in test mode, with prod keys. Must fail with |
| 766 | * STATUS_PRODUCTION_KEY_IN_TEST_REQUEST. |
| 767 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 768 | TEST_P(CertificateRequestTest, NonEmptyRequest_prodKeyInTestCert) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 769 | generateKeys(false /* testMode */, 2 /* numKeys */); |
| 770 | |
| 771 | bytevec keysToSignMac; |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 772 | DeviceInfo deviceInfo; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 773 | ProtectedData protectedData; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 774 | generateTestEekChain(3); |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 775 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 776 | true /* testMode */, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo, |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 777 | &protectedData, &keysToSignMac); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 778 | ASSERT_FALSE(status.isOk()); |
| 779 | ASSERT_EQ(status.getServiceSpecificError(), |
| 780 | BnRemotelyProvisionedComponent::STATUS_PRODUCTION_KEY_IN_TEST_REQUEST); |
| 781 | } |
| 782 | |
| 783 | /** |
| 784 | * Generate a non-empty certificate request in prod mode, with test keys. Must fail with |
| 785 | * STATUS_TEST_KEY_IN_PRODUCTION_REQUEST. |
| 786 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 787 | TEST_P(CertificateRequestTest, NonEmptyRequest_testKeyInProdCert) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 788 | generateKeys(true /* testMode */, 2 /* numKeys */); |
| 789 | |
| 790 | bytevec keysToSignMac; |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 791 | DeviceInfo deviceInfo; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 792 | ProtectedData protectedData; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 793 | generateTestEekChain(3); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 794 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 795 | false /* testMode */, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo, |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 796 | &protectedData, &keysToSignMac); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 797 | ASSERT_FALSE(status.isOk()); |
| 798 | ASSERT_EQ(status.getServiceSpecificError(), |
| 799 | BnRemotelyProvisionedComponent::STATUS_TEST_KEY_IN_PRODUCTION_REQUEST); |
| 800 | } |
| 801 | |
| 802 | INSTANTIATE_REM_PROV_AIDL_TEST(CertificateRequestTest); |
| 803 | |
| 804 | } // namespace aidl::android::hardware::security::keymint::test |