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 | |
Seth Moore | f1f6215 | 2022-09-13 12:00:30 -0700 | [diff] [blame] | 17 | #include <memory> |
Seth Moore | 2fc6f83 | 2022-09-13 16:10:11 -0700 | [diff] [blame] | 18 | #include <string> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 19 | #define LOG_TAG "VtsRemotelyProvisionableComponentTests" |
| 20 | |
Max Bires | 261a049 | 2021-04-19 18:55:56 -0700 | [diff] [blame] | 21 | #include <AndroidRemotelyProvisionedComponentDevice.h> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 22 | #include <aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.h> |
| 23 | #include <aidl/android/hardware/security/keymint/SecurityLevel.h> |
| 24 | #include <android/binder_manager.h> |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 25 | #include <binder/IServiceManager.h> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 26 | #include <cppbor_parse.h> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 27 | #include <gmock/gmock.h> |
Max Bires | 9704ff6 | 2021-04-07 11:12:01 -0700 | [diff] [blame] | 28 | #include <keymaster/cppcose/cppcose.h> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 29 | #include <keymaster/keymaster_configuration.h> |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 30 | #include <keymint_support/authorization_set.h> |
| 31 | #include <openssl/ec.h> |
| 32 | #include <openssl/ec_key.h> |
| 33 | #include <openssl/x509.h> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 34 | #include <remote_prov/remote_prov_utils.h> |
Max Bires | 757ed42 | 2022-09-07 16:20:31 -0700 | [diff] [blame] | 35 | #include <optional> |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 36 | #include <set> |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 37 | #include <vector> |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 38 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 39 | #include "KeyMintAidlTestBase.h" |
| 40 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 41 | namespace aidl::android::hardware::security::keymint::test { |
| 42 | |
| 43 | using ::std::string; |
| 44 | using ::std::vector; |
| 45 | |
| 46 | namespace { |
| 47 | |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 48 | constexpr int32_t VERSION_WITH_UNIQUE_ID_SUPPORT = 2; |
| 49 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 50 | #define INSTANTIATE_REM_PROV_AIDL_TEST(name) \ |
Seth Moore | 6305e23 | 2021-07-27 14:20:17 -0700 | [diff] [blame] | 51 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name); \ |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 52 | INSTANTIATE_TEST_SUITE_P( \ |
| 53 | PerInstance, name, \ |
| 54 | testing::ValuesIn(VtsRemotelyProvisionedComponentTests::build_params()), \ |
| 55 | ::android::PrintInstanceNameToString) |
| 56 | |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 57 | using ::android::sp; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 58 | using bytevec = std::vector<uint8_t>; |
| 59 | using testing::MatchesRegex; |
| 60 | using namespace remote_prov; |
| 61 | using namespace keymaster; |
| 62 | |
| 63 | bytevec string_to_bytevec(const char* s) { |
| 64 | const uint8_t* p = reinterpret_cast<const uint8_t*>(s); |
| 65 | return bytevec(p, p + strlen(s)); |
| 66 | } |
| 67 | |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 68 | ErrMsgOr<MacedPublicKey> corrupt_maced_key(const MacedPublicKey& macedPubKey) { |
| 69 | auto [coseMac0, _, mac0ParseErr] = cppbor::parse(macedPubKey.macedKey); |
| 70 | if (!coseMac0 || coseMac0->asArray()->size() != kCoseMac0EntryCount) { |
| 71 | return "COSE Mac0 parse failed"; |
| 72 | } |
| 73 | auto protParams = coseMac0->asArray()->get(kCoseMac0ProtectedParams)->asBstr(); |
| 74 | auto unprotParams = coseMac0->asArray()->get(kCoseMac0UnprotectedParams)->asMap(); |
| 75 | auto payload = coseMac0->asArray()->get(kCoseMac0Payload)->asBstr(); |
| 76 | auto tag = coseMac0->asArray()->get(kCoseMac0Tag)->asBstr(); |
| 77 | if (!protParams || !unprotParams || !payload || !tag) { |
| 78 | return "Invalid COSE_Sign1: missing content"; |
| 79 | } |
| 80 | auto corruptMac0 = cppbor::Array(); |
| 81 | corruptMac0.add(protParams->clone()); |
| 82 | corruptMac0.add(unprotParams->clone()); |
| 83 | corruptMac0.add(payload->clone()); |
| 84 | vector<uint8_t> tagData = tag->value(); |
| 85 | tagData[0] ^= 0x08; |
| 86 | tagData[tagData.size() - 1] ^= 0x80; |
| 87 | corruptMac0.add(cppbor::Bstr(tagData)); |
| 88 | |
| 89 | return MacedPublicKey{corruptMac0.encode()}; |
| 90 | } |
| 91 | |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 92 | ErrMsgOr<cppbor::Array> corrupt_sig(const cppbor::Array* coseSign1) { |
| 93 | if (coseSign1->size() != kCoseSign1EntryCount) { |
| 94 | return "Invalid COSE_Sign1, wrong entry count"; |
| 95 | } |
| 96 | const cppbor::Bstr* protectedParams = coseSign1->get(kCoseSign1ProtectedParams)->asBstr(); |
| 97 | const cppbor::Map* unprotectedParams = coseSign1->get(kCoseSign1UnprotectedParams)->asMap(); |
| 98 | const cppbor::Bstr* payload = coseSign1->get(kCoseSign1Payload)->asBstr(); |
| 99 | const cppbor::Bstr* signature = coseSign1->get(kCoseSign1Signature)->asBstr(); |
| 100 | if (!protectedParams || !unprotectedParams || !payload || !signature) { |
| 101 | return "Invalid COSE_Sign1: missing content"; |
| 102 | } |
| 103 | |
| 104 | auto corruptSig = cppbor::Array(); |
| 105 | corruptSig.add(protectedParams->clone()); |
| 106 | corruptSig.add(unprotectedParams->clone()); |
| 107 | corruptSig.add(payload->clone()); |
| 108 | vector<uint8_t> sigData = signature->value(); |
| 109 | sigData[0] ^= 0x08; |
| 110 | corruptSig.add(cppbor::Bstr(sigData)); |
| 111 | |
| 112 | return std::move(corruptSig); |
| 113 | } |
| 114 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 115 | ErrMsgOr<bytevec> corrupt_sig_chain(const bytevec& encodedEekChain, int which) { |
| 116 | auto [chain, _, parseErr] = cppbor::parse(encodedEekChain); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 117 | if (!chain || !chain->asArray()) { |
| 118 | return "EekChain parse failed"; |
| 119 | } |
| 120 | |
| 121 | cppbor::Array* eekChain = chain->asArray(); |
| 122 | if (which >= eekChain->size()) { |
| 123 | return "selected sig out of range"; |
| 124 | } |
| 125 | auto corruptChain = cppbor::Array(); |
| 126 | |
| 127 | for (int ii = 0; ii < eekChain->size(); ++ii) { |
| 128 | if (ii == which) { |
| 129 | auto sig = corrupt_sig(eekChain->get(which)->asArray()); |
| 130 | if (!sig) { |
| 131 | return "Failed to build corrupted signature" + sig.moveMessage(); |
| 132 | } |
| 133 | corruptChain.add(sig.moveValue()); |
| 134 | } else { |
| 135 | corruptChain.add(eekChain->get(ii)->clone()); |
| 136 | } |
| 137 | } |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 138 | return corruptChain.encode(); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 139 | } |
| 140 | |
David Drysdale | 4d3c298 | 2021-03-31 18:21:40 +0100 | [diff] [blame] | 141 | string device_suffix(const string& name) { |
| 142 | size_t pos = name.find('/'); |
| 143 | if (pos == string::npos) { |
| 144 | return name; |
| 145 | } |
| 146 | return name.substr(pos + 1); |
| 147 | } |
| 148 | |
| 149 | bool matching_keymint_device(const string& rp_name, std::shared_ptr<IKeyMintDevice>* keyMint) { |
| 150 | string rp_suffix = device_suffix(rp_name); |
| 151 | |
| 152 | vector<string> km_names = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor); |
| 153 | for (const string& km_name : km_names) { |
| 154 | // If the suffix of the KeyMint instance equals the suffix of the |
| 155 | // RemotelyProvisionedComponent instance, assume they match. |
| 156 | if (device_suffix(km_name) == rp_suffix && AServiceManager_isDeclared(km_name.c_str())) { |
| 157 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(km_name.c_str())); |
| 158 | *keyMint = IKeyMintDevice::fromBinder(binder); |
| 159 | return true; |
| 160 | } |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 165 | } // namespace |
| 166 | |
| 167 | class VtsRemotelyProvisionedComponentTests : public testing::TestWithParam<std::string> { |
| 168 | public: |
| 169 | virtual void SetUp() override { |
| 170 | if (AServiceManager_isDeclared(GetParam().c_str())) { |
| 171 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(GetParam().c_str())); |
| 172 | provisionable_ = IRemotelyProvisionedComponent::fromBinder(binder); |
| 173 | } |
| 174 | ASSERT_NE(provisionable_, nullptr); |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 175 | ASSERT_TRUE(provisionable_->getHardwareInfo(&rpcHardwareInfo).isOk()); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static vector<string> build_params() { |
| 179 | auto params = ::android::getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor); |
| 180 | return params; |
| 181 | } |
| 182 | |
| 183 | protected: |
| 184 | std::shared_ptr<IRemotelyProvisionedComponent> provisionable_; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 185 | RpcHardwareInfo rpcHardwareInfo; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 186 | }; |
| 187 | |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 188 | /** |
| 189 | * Verify that every implementation reports a different unique id. |
| 190 | */ |
| 191 | TEST(NonParameterizedTests, eachRpcHasAUniqueId) { |
| 192 | std::set<std::string> uniqueIds; |
| 193 | for (auto hal : ::android::getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor)) { |
| 194 | ASSERT_TRUE(AServiceManager_isDeclared(hal.c_str())); |
| 195 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(hal.c_str())); |
| 196 | std::shared_ptr<IRemotelyProvisionedComponent> rpc = |
| 197 | IRemotelyProvisionedComponent::fromBinder(binder); |
| 198 | ASSERT_NE(rpc, nullptr); |
| 199 | |
| 200 | RpcHardwareInfo hwInfo; |
| 201 | ASSERT_TRUE(rpc->getHardwareInfo(&hwInfo).isOk()); |
| 202 | |
Tri Vo | dd12c48 | 2022-10-12 22:41:28 +0000 | [diff] [blame] | 203 | if (hwInfo.versionNumber >= VERSION_WITH_UNIQUE_ID_SUPPORT) { |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 204 | ASSERT_TRUE(hwInfo.uniqueId); |
| 205 | auto [_, wasInserted] = uniqueIds.insert(*hwInfo.uniqueId); |
| 206 | EXPECT_TRUE(wasInserted); |
| 207 | } else { |
| 208 | ASSERT_FALSE(hwInfo.uniqueId); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | using GetHardwareInfoTests = VtsRemotelyProvisionedComponentTests; |
| 214 | |
| 215 | INSTANTIATE_REM_PROV_AIDL_TEST(GetHardwareInfoTests); |
| 216 | |
| 217 | /** |
| 218 | * Verify that a valid curve is reported by the implementation. |
| 219 | */ |
| 220 | TEST_P(GetHardwareInfoTests, supportsValidCurve) { |
| 221 | RpcHardwareInfo hwInfo; |
| 222 | ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk()); |
| 223 | |
| 224 | const std::set<int> validCurves = {RpcHardwareInfo::CURVE_P256, RpcHardwareInfo::CURVE_25519}; |
| 225 | ASSERT_EQ(validCurves.count(hwInfo.supportedEekCurve), 1) |
| 226 | << "Invalid curve: " << hwInfo.supportedEekCurve; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Verify that the unique id is within the length limits as described in RpcHardwareInfo.aidl. |
| 231 | */ |
| 232 | TEST_P(GetHardwareInfoTests, uniqueId) { |
Tri Vo | dd12c48 | 2022-10-12 22:41:28 +0000 | [diff] [blame] | 233 | if (rpcHardwareInfo.versionNumber < VERSION_WITH_UNIQUE_ID_SUPPORT) { |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | |
| 237 | RpcHardwareInfo hwInfo; |
| 238 | ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk()); |
| 239 | ASSERT_TRUE(hwInfo.uniqueId); |
| 240 | EXPECT_GE(hwInfo.uniqueId->size(), 1); |
| 241 | EXPECT_LE(hwInfo.uniqueId->size(), 32); |
| 242 | } |
| 243 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 244 | using GenerateKeyTests = VtsRemotelyProvisionedComponentTests; |
| 245 | |
| 246 | INSTANTIATE_REM_PROV_AIDL_TEST(GenerateKeyTests); |
| 247 | |
| 248 | /** |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 249 | * Generate and validate a production-mode key. MAC tag can't be verified, but |
| 250 | * the private key blob should be usable in KeyMint operations. |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 251 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 252 | TEST_P(GenerateKeyTests, generateEcdsaP256Key_prodMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 253 | MacedPublicKey macedPubKey; |
| 254 | bytevec privateKeyBlob; |
| 255 | bool testMode = false; |
| 256 | auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob); |
| 257 | ASSERT_TRUE(status.isOk()); |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 258 | vector<uint8_t> coseKeyData; |
| 259 | check_maced_pubkey(macedPubKey, testMode, &coseKeyData); |
David Drysdale | 4d3c298 | 2021-03-31 18:21:40 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Generate and validate a production-mode key, then use it as a KeyMint attestation key. |
| 264 | */ |
| 265 | TEST_P(GenerateKeyTests, generateAndUseEcdsaP256Key_prodMode) { |
| 266 | // See if there is a matching IKeyMintDevice for this IRemotelyProvisionedComponent. |
| 267 | std::shared_ptr<IKeyMintDevice> keyMint; |
| 268 | if (!matching_keymint_device(GetParam(), &keyMint)) { |
| 269 | // No matching IKeyMintDevice. |
| 270 | GTEST_SKIP() << "Skipping key use test as no matching KeyMint device found"; |
| 271 | return; |
| 272 | } |
| 273 | KeyMintHardwareInfo info; |
| 274 | ASSERT_TRUE(keyMint->getHardwareInfo(&info).isOk()); |
| 275 | |
| 276 | MacedPublicKey macedPubKey; |
| 277 | bytevec privateKeyBlob; |
| 278 | bool testMode = false; |
| 279 | auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob); |
| 280 | ASSERT_TRUE(status.isOk()); |
| 281 | vector<uint8_t> coseKeyData; |
| 282 | check_maced_pubkey(macedPubKey, testMode, &coseKeyData); |
| 283 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 284 | AttestationKey attestKey; |
| 285 | attestKey.keyBlob = std::move(privateKeyBlob); |
| 286 | attestKey.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 287 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 288 | // Generate an ECDSA key that is attested by the generated P256 keypair. |
| 289 | AuthorizationSet keyDesc = AuthorizationSetBuilder() |
| 290 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | 915ce25 | 2021-10-14 15:17:36 +0100 | [diff] [blame] | 291 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 292 | .AttestationChallenge("foo") |
| 293 | .AttestationApplicationId("bar") |
| 294 | .Digest(Digest::NONE) |
| 295 | .SetDefaultValidity(); |
| 296 | KeyCreationResult creationResult; |
| 297 | auto result = keyMint->generateKey(keyDesc.vector_data(), attestKey, &creationResult); |
| 298 | ASSERT_TRUE(result.isOk()); |
| 299 | vector<uint8_t> attested_key_blob = std::move(creationResult.keyBlob); |
| 300 | vector<KeyCharacteristics> attested_key_characteristics = |
| 301 | std::move(creationResult.keyCharacteristics); |
| 302 | vector<Certificate> attested_key_cert_chain = std::move(creationResult.certificateChain); |
| 303 | EXPECT_EQ(attested_key_cert_chain.size(), 1); |
| 304 | |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 305 | int32_t aidl_version = 0; |
| 306 | ASSERT_TRUE(keyMint->getInterfaceVersion(&aidl_version).isOk()); |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 307 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 308 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 309 | 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] | 310 | info.securityLevel, |
| 311 | attested_key_cert_chain[0].encodedCertificate)); |
| 312 | |
| 313 | // Attestation by itself is not valid (last entry is not self-signed). |
| 314 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 315 | |
| 316 | // The signature over the attested key should correspond to the P256 public key. |
| 317 | X509_Ptr key_cert(parse_cert_blob(attested_key_cert_chain[0].encodedCertificate)); |
| 318 | ASSERT_TRUE(key_cert.get()); |
| 319 | EVP_PKEY_Ptr signing_pubkey; |
| 320 | p256_pub_key(coseKeyData, &signing_pubkey); |
| 321 | ASSERT_TRUE(signing_pubkey.get()); |
| 322 | |
| 323 | ASSERT_TRUE(X509_verify(key_cert.get(), signing_pubkey.get())) |
| 324 | << "Verification of attested certificate failed " |
| 325 | << "OpenSSL error string: " << ERR_error_string(ERR_get_error(), NULL); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Generate and validate a test-mode key. |
| 330 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 331 | TEST_P(GenerateKeyTests, generateEcdsaP256Key_testMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 332 | MacedPublicKey macedPubKey; |
| 333 | bytevec privateKeyBlob; |
| 334 | bool testMode = true; |
| 335 | auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob); |
| 336 | ASSERT_TRUE(status.isOk()); |
| 337 | |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 338 | check_maced_pubkey(macedPubKey, testMode, nullptr); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | class CertificateRequestTest : public VtsRemotelyProvisionedComponentTests { |
| 342 | protected: |
Max Bires | 89c7488 | 2022-03-27 21:06:11 -0700 | [diff] [blame] | 343 | CertificateRequestTest() : eekId_(string_to_bytevec("eekid")), challenge_(randomBytes(64)) {} |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 344 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 345 | void generateTestEekChain(size_t eekLength) { |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 346 | auto chain = generateEekChain(rpcHardwareInfo.supportedEekCurve, eekLength, eekId_); |
David Drysdale | 08696a7 | 2022-03-10 10:43:25 +0000 | [diff] [blame] | 347 | ASSERT_TRUE(chain) << chain.message(); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 348 | if (chain) testEekChain_ = chain.moveValue(); |
| 349 | testEekLength_ = eekLength; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void generateKeys(bool testMode, size_t numKeys) { |
| 353 | keysToSign_ = std::vector<MacedPublicKey>(numKeys); |
| 354 | cborKeysToSign_ = cppbor::Array(); |
| 355 | |
| 356 | for (auto& key : keysToSign_) { |
| 357 | bytevec privateKeyBlob; |
| 358 | auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &key, &privateKeyBlob); |
| 359 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 360 | |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 361 | vector<uint8_t> payload_value; |
| 362 | check_maced_pubkey(key, testMode, &payload_value); |
| 363 | cborKeysToSign_.add(cppbor::EncodedItem(payload_value)); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | bytevec eekId_; |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 368 | size_t testEekLength_; |
| 369 | EekChain testEekChain_; |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 370 | bytevec challenge_; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 371 | std::vector<MacedPublicKey> keysToSign_; |
| 372 | cppbor::Array cborKeysToSign_; |
| 373 | }; |
| 374 | |
| 375 | /** |
| 376 | * Generate an empty certificate request in test mode, and decrypt and verify the structure and |
| 377 | * content. |
| 378 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 379 | TEST_P(CertificateRequestTest, EmptyRequest_testMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 380 | bool testMode = true; |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 381 | for (size_t eekLength : {2, 3, 7}) { |
| 382 | SCOPED_TRACE(testing::Message() << "EEK of length " << eekLength); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 383 | generateTestEekChain(eekLength); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 384 | |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 385 | bytevec keysToSignMac; |
| 386 | DeviceInfo deviceInfo; |
| 387 | ProtectedData protectedData; |
| 388 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 389 | testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo, |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 390 | &protectedData, &keysToSignMac); |
| 391 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 392 | |
Seth Moore | 2fc6f83 | 2022-09-13 16:10:11 -0700 | [diff] [blame] | 393 | auto result = verifyProductionProtectedData( |
| 394 | deviceInfo, cppbor::Array(), keysToSignMac, protectedData, testEekChain_, eekId_, |
| 395 | rpcHardwareInfo.supportedEekCurve, provisionable_.get(), challenge_); |
| 396 | ASSERT_TRUE(result) << result.message(); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 397 | } |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 401 | * Ensure that test mode outputs a unique BCC root key every time we request a |
| 402 | * certificate request. Else, it's possible that the test mode API could be used |
| 403 | * to fingerprint devices. Only the GEEK should be allowed to decrypt the same |
| 404 | * device public key multiple times. |
| 405 | */ |
| 406 | TEST_P(CertificateRequestTest, NewKeyPerCallInTestMode) { |
| 407 | constexpr bool testMode = true; |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 408 | |
| 409 | bytevec keysToSignMac; |
| 410 | DeviceInfo deviceInfo; |
| 411 | ProtectedData protectedData; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 412 | generateTestEekChain(3); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 413 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 414 | testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo, |
| 415 | &protectedData, &keysToSignMac); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 416 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 417 | |
Seth Moore | 2fc6f83 | 2022-09-13 16:10:11 -0700 | [diff] [blame] | 418 | auto firstBcc = verifyProductionProtectedData( |
| 419 | deviceInfo, /*keysToSign=*/cppbor::Array(), keysToSignMac, protectedData, testEekChain_, |
| 420 | eekId_, rpcHardwareInfo.supportedEekCurve, provisionable_.get(), challenge_); |
| 421 | ASSERT_TRUE(firstBcc) << firstBcc.message(); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 422 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 423 | status = provisionable_->generateCertificateRequest( |
| 424 | testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo, |
| 425 | &protectedData, &keysToSignMac); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 426 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 427 | |
Seth Moore | 2fc6f83 | 2022-09-13 16:10:11 -0700 | [diff] [blame] | 428 | auto secondBcc = verifyProductionProtectedData( |
| 429 | deviceInfo, /*keysToSign=*/cppbor::Array(), keysToSignMac, protectedData, testEekChain_, |
| 430 | eekId_, rpcHardwareInfo.supportedEekCurve, provisionable_.get(), challenge_); |
| 431 | ASSERT_TRUE(secondBcc) << secondBcc.message(); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 432 | |
| 433 | // Verify that none of the keys in the first BCC are repeated in the second one. |
Seth Moore | 2fc6f83 | 2022-09-13 16:10:11 -0700 | [diff] [blame] | 434 | for (const auto& i : *firstBcc) { |
| 435 | for (auto& j : *secondBcc) { |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 436 | ASSERT_THAT(i.pubKey, testing::Not(testing::ElementsAreArray(j.pubKey))) |
| 437 | << "Found a repeated pubkey in two generateCertificateRequest test mode calls"; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | /** |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 443 | * Generate an empty certificate request in prod mode. This test must be run explicitly, and |
| 444 | * is not run by default. Not all devices are GMS devices, and therefore they do not all |
| 445 | * trust the Google EEK root. |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 446 | */ |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 447 | TEST_P(CertificateRequestTest, DISABLED_EmptyRequest_prodMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 448 | bool testMode = false; |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 449 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 450 | bytevec keysToSignMac; |
| 451 | DeviceInfo deviceInfo; |
| 452 | ProtectedData protectedData; |
| 453 | auto status = provisionable_->generateCertificateRequest( |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 454 | testMode, {} /* keysToSign */, getProdEekChain(rpcHardwareInfo.supportedEekCurve), |
| 455 | challenge_, &deviceInfo, &protectedData, &keysToSignMac); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 456 | EXPECT_TRUE(status.isOk()); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Generate a non-empty certificate request in test mode. Decrypt, parse and validate the contents. |
| 461 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 462 | TEST_P(CertificateRequestTest, NonEmptyRequest_testMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 463 | bool testMode = true; |
| 464 | generateKeys(testMode, 4 /* numKeys */); |
| 465 | |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 466 | for (size_t eekLength : {2, 3, 7}) { |
| 467 | SCOPED_TRACE(testing::Message() << "EEK of length " << eekLength); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 468 | generateTestEekChain(eekLength); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 469 | |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 470 | bytevec keysToSignMac; |
| 471 | DeviceInfo deviceInfo; |
| 472 | ProtectedData protectedData; |
| 473 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 474 | testMode, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo, &protectedData, |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 475 | &keysToSignMac); |
| 476 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 477 | |
Seth Moore | 2fc6f83 | 2022-09-13 16:10:11 -0700 | [diff] [blame] | 478 | auto result = verifyProductionProtectedData( |
| 479 | deviceInfo, cborKeysToSign_, keysToSignMac, protectedData, testEekChain_, eekId_, |
| 480 | rpcHardwareInfo.supportedEekCurve, provisionable_.get(), challenge_); |
| 481 | ASSERT_TRUE(result) << result.message(); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 482 | } |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /** |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 486 | * Generate a non-empty certificate request in prod mode. This test must be run explicitly, and |
| 487 | * is not run by default. Not all devices are GMS devices, and therefore they do not all |
| 488 | * trust the Google EEK root. |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 489 | */ |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 490 | TEST_P(CertificateRequestTest, DISABLED_NonEmptyRequest_prodMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 491 | bool testMode = false; |
| 492 | generateKeys(testMode, 4 /* numKeys */); |
| 493 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 494 | bytevec keysToSignMac; |
| 495 | DeviceInfo deviceInfo; |
| 496 | ProtectedData protectedData; |
| 497 | auto status = provisionable_->generateCertificateRequest( |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 498 | testMode, keysToSign_, getProdEekChain(rpcHardwareInfo.supportedEekCurve), challenge_, |
| 499 | &deviceInfo, &protectedData, &keysToSignMac); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 500 | EXPECT_TRUE(status.isOk()); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /** |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 504 | * Generate a non-empty certificate request in test mode, but with the MAC corrupted on the keypair. |
| 505 | */ |
| 506 | TEST_P(CertificateRequestTest, NonEmptyRequestCorruptMac_testMode) { |
| 507 | bool testMode = true; |
| 508 | generateKeys(testMode, 1 /* numKeys */); |
David Drysdale | 08696a7 | 2022-03-10 10:43:25 +0000 | [diff] [blame] | 509 | auto result = corrupt_maced_key(keysToSign_[0]); |
| 510 | ASSERT_TRUE(result) << result.moveMessage(); |
| 511 | MacedPublicKey keyWithCorruptMac = result.moveValue(); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 512 | |
| 513 | bytevec keysToSignMac; |
| 514 | DeviceInfo deviceInfo; |
| 515 | ProtectedData protectedData; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 516 | generateTestEekChain(3); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 517 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 518 | testMode, {keyWithCorruptMac}, testEekChain_.chain, challenge_, &deviceInfo, |
| 519 | &protectedData, &keysToSignMac); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 520 | ASSERT_FALSE(status.isOk()) << status.getMessage(); |
| 521 | EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Generate a non-empty certificate request in prod mode, but with the MAC corrupted on the keypair. |
| 526 | */ |
| 527 | TEST_P(CertificateRequestTest, NonEmptyRequestCorruptMac_prodMode) { |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 528 | bool testMode = false; |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 529 | generateKeys(testMode, 1 /* numKeys */); |
David Drysdale | 08696a7 | 2022-03-10 10:43:25 +0000 | [diff] [blame] | 530 | auto result = corrupt_maced_key(keysToSign_[0]); |
| 531 | ASSERT_TRUE(result) << result.moveMessage(); |
| 532 | MacedPublicKey keyWithCorruptMac = result.moveValue(); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 533 | |
| 534 | bytevec keysToSignMac; |
| 535 | DeviceInfo deviceInfo; |
| 536 | ProtectedData protectedData; |
| 537 | auto status = provisionable_->generateCertificateRequest( |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 538 | testMode, {keyWithCorruptMac}, getProdEekChain(rpcHardwareInfo.supportedEekCurve), |
| 539 | challenge_, &deviceInfo, &protectedData, &keysToSignMac); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 540 | ASSERT_FALSE(status.isOk()) << status.getMessage(); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 541 | EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 545 | * Generate a non-empty certificate request in prod mode that has a corrupt EEK chain. |
| 546 | * Confirm that the request is rejected. |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 547 | */ |
| 548 | TEST_P(CertificateRequestTest, NonEmptyCorruptEekRequest_prodMode) { |
| 549 | bool testMode = false; |
| 550 | generateKeys(testMode, 4 /* numKeys */); |
| 551 | |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 552 | auto prodEekChain = getProdEekChain(rpcHardwareInfo.supportedEekCurve); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 553 | auto [parsedChain, _, parseErr] = cppbor::parse(prodEekChain); |
| 554 | ASSERT_NE(parsedChain, nullptr) << parseErr; |
| 555 | ASSERT_NE(parsedChain->asArray(), nullptr); |
| 556 | |
| 557 | for (int ii = 0; ii < parsedChain->asArray()->size(); ++ii) { |
| 558 | auto chain = corrupt_sig_chain(prodEekChain, ii); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 559 | ASSERT_TRUE(chain) << chain.message(); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 560 | |
| 561 | bytevec keysToSignMac; |
| 562 | DeviceInfo deviceInfo; |
| 563 | ProtectedData protectedData; |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 564 | auto status = provisionable_->generateCertificateRequest(testMode, keysToSign_, *chain, |
| 565 | challenge_, &deviceInfo, |
| 566 | &protectedData, &keysToSignMac); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 567 | ASSERT_FALSE(status.isOk()); |
| 568 | ASSERT_EQ(status.getServiceSpecificError(), |
| 569 | BnRemotelyProvisionedComponent::STATUS_INVALID_EEK); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Generate a non-empty certificate request in prod mode that has an incomplete EEK chain. |
| 575 | * Confirm that the request is rejected. |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 576 | */ |
| 577 | TEST_P(CertificateRequestTest, NonEmptyIncompleteEekRequest_prodMode) { |
| 578 | bool testMode = false; |
| 579 | generateKeys(testMode, 4 /* numKeys */); |
| 580 | |
| 581 | // Build an EEK chain that omits the first self-signed cert. |
| 582 | auto truncatedChain = cppbor::Array(); |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 583 | auto [chain, _, parseErr] = cppbor::parse(getProdEekChain(rpcHardwareInfo.supportedEekCurve)); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 584 | ASSERT_TRUE(chain); |
| 585 | auto eekChain = chain->asArray(); |
| 586 | ASSERT_NE(eekChain, nullptr); |
| 587 | for (size_t ii = 1; ii < eekChain->size(); ii++) { |
| 588 | truncatedChain.add(eekChain->get(ii)->clone()); |
| 589 | } |
| 590 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 591 | bytevec keysToSignMac; |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 592 | DeviceInfo deviceInfo; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 593 | ProtectedData protectedData; |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 594 | auto status = provisionable_->generateCertificateRequest( |
| 595 | testMode, keysToSign_, truncatedChain.encode(), challenge_, &deviceInfo, &protectedData, |
| 596 | &keysToSignMac); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 597 | ASSERT_FALSE(status.isOk()); |
| 598 | ASSERT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_EEK); |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Generate a non-empty certificate request in test mode, with prod keys. Must fail with |
| 603 | * STATUS_PRODUCTION_KEY_IN_TEST_REQUEST. |
| 604 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 605 | TEST_P(CertificateRequestTest, NonEmptyRequest_prodKeyInTestCert) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 606 | generateKeys(false /* testMode */, 2 /* numKeys */); |
| 607 | |
| 608 | bytevec keysToSignMac; |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 609 | DeviceInfo deviceInfo; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 610 | ProtectedData protectedData; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 611 | generateTestEekChain(3); |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 612 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 613 | true /* testMode */, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo, |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 614 | &protectedData, &keysToSignMac); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 615 | ASSERT_FALSE(status.isOk()); |
| 616 | ASSERT_EQ(status.getServiceSpecificError(), |
| 617 | BnRemotelyProvisionedComponent::STATUS_PRODUCTION_KEY_IN_TEST_REQUEST); |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Generate a non-empty certificate request in prod mode, with test keys. Must fail with |
| 622 | * STATUS_TEST_KEY_IN_PRODUCTION_REQUEST. |
| 623 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 624 | TEST_P(CertificateRequestTest, NonEmptyRequest_testKeyInProdCert) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 625 | generateKeys(true /* testMode */, 2 /* numKeys */); |
| 626 | |
| 627 | bytevec keysToSignMac; |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 628 | DeviceInfo deviceInfo; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 629 | ProtectedData protectedData; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 630 | generateTestEekChain(3); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 631 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 632 | false /* testMode */, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo, |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 633 | &protectedData, &keysToSignMac); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 634 | ASSERT_FALSE(status.isOk()); |
| 635 | ASSERT_EQ(status.getServiceSpecificError(), |
| 636 | BnRemotelyProvisionedComponent::STATUS_TEST_KEY_IN_PRODUCTION_REQUEST); |
| 637 | } |
| 638 | |
| 639 | INSTANTIATE_REM_PROV_AIDL_TEST(CertificateRequestTest); |
| 640 | |
| 641 | } // namespace aidl::android::hardware::security::keymint::test |