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