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; |
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 | |
Tri Vo | 0d6204e | 2022-09-29 16:15:34 -0700 | [diff] [blame] | 184 | void checkMacedPubkeyVersioned(const MacedPublicKey& macedPubKey, bool testMode, |
| 185 | vector<uint8_t>* payload_value) { |
| 186 | if (rpcHardwareInfo.versionNumber >= VERSION_WITHOUT_TEST_MODE) { |
| 187 | check_maced_pubkey(macedPubKey, false, payload_value); |
| 188 | } else { |
| 189 | check_maced_pubkey(macedPubKey, testMode, payload_value); |
| 190 | } |
| 191 | } |
| 192 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 193 | protected: |
| 194 | std::shared_ptr<IRemotelyProvisionedComponent> provisionable_; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 195 | RpcHardwareInfo rpcHardwareInfo; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 196 | }; |
| 197 | |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 198 | /** |
| 199 | * Verify that every implementation reports a different unique id. |
| 200 | */ |
| 201 | TEST(NonParameterizedTests, eachRpcHasAUniqueId) { |
| 202 | std::set<std::string> uniqueIds; |
| 203 | for (auto hal : ::android::getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor)) { |
| 204 | ASSERT_TRUE(AServiceManager_isDeclared(hal.c_str())); |
| 205 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(hal.c_str())); |
| 206 | std::shared_ptr<IRemotelyProvisionedComponent> rpc = |
| 207 | IRemotelyProvisionedComponent::fromBinder(binder); |
| 208 | ASSERT_NE(rpc, nullptr); |
| 209 | |
| 210 | RpcHardwareInfo hwInfo; |
| 211 | ASSERT_TRUE(rpc->getHardwareInfo(&hwInfo).isOk()); |
| 212 | |
Tri Vo | dd12c48 | 2022-10-12 22:41:28 +0000 | [diff] [blame] | 213 | if (hwInfo.versionNumber >= VERSION_WITH_UNIQUE_ID_SUPPORT) { |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 214 | ASSERT_TRUE(hwInfo.uniqueId); |
| 215 | auto [_, wasInserted] = uniqueIds.insert(*hwInfo.uniqueId); |
| 216 | EXPECT_TRUE(wasInserted); |
| 217 | } else { |
| 218 | ASSERT_FALSE(hwInfo.uniqueId); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | using GetHardwareInfoTests = VtsRemotelyProvisionedComponentTests; |
| 224 | |
| 225 | INSTANTIATE_REM_PROV_AIDL_TEST(GetHardwareInfoTests); |
| 226 | |
| 227 | /** |
| 228 | * Verify that a valid curve is reported by the implementation. |
| 229 | */ |
| 230 | TEST_P(GetHardwareInfoTests, supportsValidCurve) { |
| 231 | RpcHardwareInfo hwInfo; |
| 232 | ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk()); |
| 233 | |
| 234 | const std::set<int> validCurves = {RpcHardwareInfo::CURVE_P256, RpcHardwareInfo::CURVE_25519}; |
| 235 | ASSERT_EQ(validCurves.count(hwInfo.supportedEekCurve), 1) |
| 236 | << "Invalid curve: " << hwInfo.supportedEekCurve; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Verify that the unique id is within the length limits as described in RpcHardwareInfo.aidl. |
| 241 | */ |
| 242 | TEST_P(GetHardwareInfoTests, uniqueId) { |
Tri Vo | dd12c48 | 2022-10-12 22:41:28 +0000 | [diff] [blame] | 243 | if (rpcHardwareInfo.versionNumber < VERSION_WITH_UNIQUE_ID_SUPPORT) { |
Seth Moore | fc86bf4 | 2021-12-09 14:07:04 -0800 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | |
| 247 | RpcHardwareInfo hwInfo; |
| 248 | ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk()); |
| 249 | ASSERT_TRUE(hwInfo.uniqueId); |
| 250 | EXPECT_GE(hwInfo.uniqueId->size(), 1); |
| 251 | EXPECT_LE(hwInfo.uniqueId->size(), 32); |
| 252 | } |
| 253 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 254 | using GenerateKeyTests = VtsRemotelyProvisionedComponentTests; |
| 255 | |
| 256 | INSTANTIATE_REM_PROV_AIDL_TEST(GenerateKeyTests); |
| 257 | |
| 258 | /** |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 259 | * Generate and validate a production-mode key. MAC tag can't be verified, but |
| 260 | * the private key blob should be usable in KeyMint operations. |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 261 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 262 | TEST_P(GenerateKeyTests, generateEcdsaP256Key_prodMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 263 | MacedPublicKey macedPubKey; |
| 264 | bytevec privateKeyBlob; |
| 265 | bool testMode = false; |
| 266 | auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob); |
| 267 | ASSERT_TRUE(status.isOk()); |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 268 | vector<uint8_t> coseKeyData; |
Tri Vo | 0d6204e | 2022-09-29 16:15:34 -0700 | [diff] [blame] | 269 | checkMacedPubkeyVersioned(macedPubKey, testMode, &coseKeyData); |
David Drysdale | 4d3c298 | 2021-03-31 18:21:40 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Generate and validate a production-mode key, then use it as a KeyMint attestation key. |
| 274 | */ |
| 275 | TEST_P(GenerateKeyTests, generateAndUseEcdsaP256Key_prodMode) { |
| 276 | // See if there is a matching IKeyMintDevice for this IRemotelyProvisionedComponent. |
| 277 | std::shared_ptr<IKeyMintDevice> keyMint; |
| 278 | if (!matching_keymint_device(GetParam(), &keyMint)) { |
| 279 | // No matching IKeyMintDevice. |
| 280 | GTEST_SKIP() << "Skipping key use test as no matching KeyMint device found"; |
| 281 | return; |
| 282 | } |
| 283 | KeyMintHardwareInfo info; |
| 284 | ASSERT_TRUE(keyMint->getHardwareInfo(&info).isOk()); |
| 285 | |
| 286 | MacedPublicKey macedPubKey; |
| 287 | bytevec privateKeyBlob; |
| 288 | bool testMode = false; |
| 289 | auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob); |
| 290 | ASSERT_TRUE(status.isOk()); |
| 291 | vector<uint8_t> coseKeyData; |
Tri Vo | 0d6204e | 2022-09-29 16:15:34 -0700 | [diff] [blame] | 292 | checkMacedPubkeyVersioned(macedPubKey, testMode, &coseKeyData); |
David Drysdale | 4d3c298 | 2021-03-31 18:21:40 +0100 | [diff] [blame] | 293 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 294 | AttestationKey attestKey; |
| 295 | attestKey.keyBlob = std::move(privateKeyBlob); |
| 296 | attestKey.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 297 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 298 | // Generate an ECDSA key that is attested by the generated P256 keypair. |
| 299 | AuthorizationSet keyDesc = AuthorizationSetBuilder() |
| 300 | .Authorization(TAG_NO_AUTH_REQUIRED) |
David Drysdale | 915ce25 | 2021-10-14 15:17:36 +0100 | [diff] [blame] | 301 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 302 | .AttestationChallenge("foo") |
| 303 | .AttestationApplicationId("bar") |
| 304 | .Digest(Digest::NONE) |
| 305 | .SetDefaultValidity(); |
| 306 | KeyCreationResult creationResult; |
| 307 | auto result = keyMint->generateKey(keyDesc.vector_data(), attestKey, &creationResult); |
| 308 | ASSERT_TRUE(result.isOk()); |
| 309 | vector<uint8_t> attested_key_blob = std::move(creationResult.keyBlob); |
| 310 | vector<KeyCharacteristics> attested_key_characteristics = |
| 311 | std::move(creationResult.keyCharacteristics); |
| 312 | vector<Certificate> attested_key_cert_chain = std::move(creationResult.certificateChain); |
| 313 | EXPECT_EQ(attested_key_cert_chain.size(), 1); |
| 314 | |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 315 | int32_t aidl_version = 0; |
| 316 | ASSERT_TRUE(keyMint->getInterfaceVersion(&aidl_version).isOk()); |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 317 | AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics); |
| 318 | AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 319 | 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] | 320 | info.securityLevel, |
| 321 | attested_key_cert_chain[0].encodedCertificate)); |
| 322 | |
| 323 | // Attestation by itself is not valid (last entry is not self-signed). |
| 324 | EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain)); |
| 325 | |
| 326 | // The signature over the attested key should correspond to the P256 public key. |
| 327 | X509_Ptr key_cert(parse_cert_blob(attested_key_cert_chain[0].encodedCertificate)); |
| 328 | ASSERT_TRUE(key_cert.get()); |
| 329 | EVP_PKEY_Ptr signing_pubkey; |
| 330 | p256_pub_key(coseKeyData, &signing_pubkey); |
| 331 | ASSERT_TRUE(signing_pubkey.get()); |
| 332 | |
| 333 | ASSERT_TRUE(X509_verify(key_cert.get(), signing_pubkey.get())) |
| 334 | << "Verification of attested certificate failed " |
| 335 | << "OpenSSL error string: " << ERR_error_string(ERR_get_error(), NULL); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Generate and validate a test-mode key. |
| 340 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 341 | TEST_P(GenerateKeyTests, generateEcdsaP256Key_testMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 342 | MacedPublicKey macedPubKey; |
| 343 | bytevec privateKeyBlob; |
| 344 | bool testMode = true; |
| 345 | auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob); |
| 346 | ASSERT_TRUE(status.isOk()); |
Tri Vo | 0d6204e | 2022-09-29 16:15:34 -0700 | [diff] [blame] | 347 | checkMacedPubkeyVersioned(macedPubKey, testMode, nullptr); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 348 | } |
| 349 | |
Tri Vo | 0d6204e | 2022-09-29 16:15:34 -0700 | [diff] [blame] | 350 | class CertificateRequestTestBase : public VtsRemotelyProvisionedComponentTests { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 351 | protected: |
Tri Vo | 0d6204e | 2022-09-29 16:15:34 -0700 | [diff] [blame] | 352 | CertificateRequestTestBase() |
| 353 | : eekId_(string_to_bytevec("eekid")), challenge_(randomBytes(64)) {} |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 354 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 355 | void generateTestEekChain(size_t eekLength) { |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 356 | auto chain = generateEekChain(rpcHardwareInfo.supportedEekCurve, eekLength, eekId_); |
David Drysdale | 08696a7 | 2022-03-10 10:43:25 +0000 | [diff] [blame] | 357 | ASSERT_TRUE(chain) << chain.message(); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 358 | if (chain) testEekChain_ = chain.moveValue(); |
| 359 | testEekLength_ = eekLength; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void generateKeys(bool testMode, size_t numKeys) { |
| 363 | keysToSign_ = std::vector<MacedPublicKey>(numKeys); |
| 364 | cborKeysToSign_ = cppbor::Array(); |
| 365 | |
| 366 | for (auto& key : keysToSign_) { |
| 367 | bytevec privateKeyBlob; |
| 368 | auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &key, &privateKeyBlob); |
| 369 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 370 | |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 371 | vector<uint8_t> payload_value; |
Tri Vo | 0d6204e | 2022-09-29 16:15:34 -0700 | [diff] [blame] | 372 | checkMacedPubkeyVersioned(key, testMode, &payload_value); |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 373 | cborKeysToSign_.add(cppbor::EncodedItem(payload_value)); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | bytevec eekId_; |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 378 | size_t testEekLength_; |
| 379 | EekChain testEekChain_; |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 380 | bytevec challenge_; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 381 | std::vector<MacedPublicKey> keysToSign_; |
| 382 | cppbor::Array cborKeysToSign_; |
| 383 | }; |
| 384 | |
Tri Vo | 0d6204e | 2022-09-29 16:15:34 -0700 | [diff] [blame] | 385 | class CertificateRequestTest : public CertificateRequestTestBase { |
| 386 | protected: |
| 387 | void SetUp() override { |
| 388 | CertificateRequestTestBase::SetUp(); |
| 389 | |
| 390 | if (rpcHardwareInfo.versionNumber >= VERSION_WITHOUT_TEST_MODE) { |
| 391 | GTEST_SKIP() << "This test case only applies to RKP v1 and v2. " |
| 392 | << "RKP version discovered: " << rpcHardwareInfo.versionNumber; |
| 393 | } |
| 394 | } |
| 395 | }; |
| 396 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 397 | /** |
| 398 | * Generate an empty certificate request in test mode, and decrypt and verify the structure and |
| 399 | * content. |
| 400 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 401 | TEST_P(CertificateRequestTest, EmptyRequest_testMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 402 | bool testMode = true; |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 403 | for (size_t eekLength : {2, 3, 7}) { |
| 404 | SCOPED_TRACE(testing::Message() << "EEK of length " << eekLength); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 405 | generateTestEekChain(eekLength); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 406 | |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 407 | bytevec keysToSignMac; |
| 408 | DeviceInfo deviceInfo; |
| 409 | ProtectedData protectedData; |
| 410 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 411 | testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo, |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 412 | &protectedData, &keysToSignMac); |
| 413 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 414 | |
Seth Moore | 2fc6f83 | 2022-09-13 16:10:11 -0700 | [diff] [blame] | 415 | auto result = verifyProductionProtectedData( |
| 416 | deviceInfo, cppbor::Array(), keysToSignMac, protectedData, testEekChain_, eekId_, |
| 417 | rpcHardwareInfo.supportedEekCurve, provisionable_.get(), challenge_); |
| 418 | ASSERT_TRUE(result) << result.message(); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 419 | } |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /** |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 423 | * Ensure that test mode outputs a unique BCC root key every time we request a |
| 424 | * certificate request. Else, it's possible that the test mode API could be used |
| 425 | * to fingerprint devices. Only the GEEK should be allowed to decrypt the same |
| 426 | * device public key multiple times. |
| 427 | */ |
| 428 | TEST_P(CertificateRequestTest, NewKeyPerCallInTestMode) { |
| 429 | constexpr bool testMode = true; |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 430 | |
| 431 | bytevec keysToSignMac; |
| 432 | DeviceInfo deviceInfo; |
| 433 | ProtectedData protectedData; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 434 | generateTestEekChain(3); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 435 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 436 | testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo, |
| 437 | &protectedData, &keysToSignMac); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 438 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 439 | |
Seth Moore | 2fc6f83 | 2022-09-13 16:10:11 -0700 | [diff] [blame] | 440 | auto firstBcc = verifyProductionProtectedData( |
| 441 | deviceInfo, /*keysToSign=*/cppbor::Array(), keysToSignMac, protectedData, testEekChain_, |
| 442 | eekId_, rpcHardwareInfo.supportedEekCurve, provisionable_.get(), challenge_); |
| 443 | ASSERT_TRUE(firstBcc) << firstBcc.message(); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 444 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 445 | status = provisionable_->generateCertificateRequest( |
| 446 | testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo, |
| 447 | &protectedData, &keysToSignMac); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 448 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 449 | |
Seth Moore | 2fc6f83 | 2022-09-13 16:10:11 -0700 | [diff] [blame] | 450 | auto secondBcc = verifyProductionProtectedData( |
| 451 | deviceInfo, /*keysToSign=*/cppbor::Array(), keysToSignMac, protectedData, testEekChain_, |
| 452 | eekId_, rpcHardwareInfo.supportedEekCurve, provisionable_.get(), challenge_); |
| 453 | ASSERT_TRUE(secondBcc) << secondBcc.message(); |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 454 | |
| 455 | // 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] | 456 | for (const auto& i : *firstBcc) { |
| 457 | for (auto& j : *secondBcc) { |
Seth Moore | 42c1133 | 2021-07-02 15:38:17 -0700 | [diff] [blame] | 458 | ASSERT_THAT(i.pubKey, testing::Not(testing::ElementsAreArray(j.pubKey))) |
| 459 | << "Found a repeated pubkey in two generateCertificateRequest test mode calls"; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /** |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 465 | * Generate an empty certificate request in prod mode. This test must be run explicitly, and |
| 466 | * is not run by default. Not all devices are GMS devices, and therefore they do not all |
| 467 | * trust the Google EEK root. |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 468 | */ |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 469 | TEST_P(CertificateRequestTest, DISABLED_EmptyRequest_prodMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 470 | bool testMode = false; |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 471 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 472 | bytevec keysToSignMac; |
| 473 | DeviceInfo deviceInfo; |
| 474 | ProtectedData protectedData; |
| 475 | auto status = provisionable_->generateCertificateRequest( |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 476 | testMode, {} /* keysToSign */, getProdEekChain(rpcHardwareInfo.supportedEekCurve), |
| 477 | challenge_, &deviceInfo, &protectedData, &keysToSignMac); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 478 | EXPECT_TRUE(status.isOk()); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Generate a non-empty certificate request in test mode. Decrypt, parse and validate the contents. |
| 483 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 484 | TEST_P(CertificateRequestTest, NonEmptyRequest_testMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 485 | bool testMode = true; |
| 486 | generateKeys(testMode, 4 /* numKeys */); |
| 487 | |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 488 | for (size_t eekLength : {2, 3, 7}) { |
| 489 | SCOPED_TRACE(testing::Message() << "EEK of length " << eekLength); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 490 | generateTestEekChain(eekLength); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 491 | |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 492 | bytevec keysToSignMac; |
| 493 | DeviceInfo deviceInfo; |
| 494 | ProtectedData protectedData; |
| 495 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 496 | testMode, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo, &protectedData, |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 497 | &keysToSignMac); |
| 498 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 499 | |
Seth Moore | 2fc6f83 | 2022-09-13 16:10:11 -0700 | [diff] [blame] | 500 | auto result = verifyProductionProtectedData( |
| 501 | deviceInfo, cborKeysToSign_, keysToSignMac, protectedData, testEekChain_, eekId_, |
| 502 | rpcHardwareInfo.supportedEekCurve, provisionable_.get(), challenge_); |
| 503 | ASSERT_TRUE(result) << result.message(); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 504 | } |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /** |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 508 | * Generate a non-empty certificate request in prod mode. This test must be run explicitly, and |
| 509 | * is not run by default. Not all devices are GMS devices, and therefore they do not all |
| 510 | * trust the Google EEK root. |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 511 | */ |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 512 | TEST_P(CertificateRequestTest, DISABLED_NonEmptyRequest_prodMode) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 513 | bool testMode = false; |
| 514 | generateKeys(testMode, 4 /* numKeys */); |
| 515 | |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 516 | bytevec keysToSignMac; |
| 517 | DeviceInfo deviceInfo; |
| 518 | ProtectedData protectedData; |
| 519 | auto status = provisionable_->generateCertificateRequest( |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 520 | testMode, keysToSign_, getProdEekChain(rpcHardwareInfo.supportedEekCurve), challenge_, |
| 521 | &deviceInfo, &protectedData, &keysToSignMac); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 522 | EXPECT_TRUE(status.isOk()); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /** |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 526 | * Generate a non-empty certificate request in test mode, but with the MAC corrupted on the keypair. |
| 527 | */ |
| 528 | TEST_P(CertificateRequestTest, NonEmptyRequestCorruptMac_testMode) { |
| 529 | bool testMode = true; |
| 530 | generateKeys(testMode, 1 /* numKeys */); |
David Drysdale | 08696a7 | 2022-03-10 10:43:25 +0000 | [diff] [blame] | 531 | auto result = corrupt_maced_key(keysToSign_[0]); |
| 532 | ASSERT_TRUE(result) << result.moveMessage(); |
| 533 | MacedPublicKey keyWithCorruptMac = result.moveValue(); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 534 | |
| 535 | bytevec keysToSignMac; |
| 536 | DeviceInfo deviceInfo; |
| 537 | ProtectedData protectedData; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 538 | generateTestEekChain(3); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 539 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 540 | testMode, {keyWithCorruptMac}, testEekChain_.chain, challenge_, &deviceInfo, |
| 541 | &protectedData, &keysToSignMac); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 542 | ASSERT_FALSE(status.isOk()) << status.getMessage(); |
| 543 | EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC); |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Generate a non-empty certificate request in prod mode, but with the MAC corrupted on the keypair. |
| 548 | */ |
| 549 | TEST_P(CertificateRequestTest, NonEmptyRequestCorruptMac_prodMode) { |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 550 | bool testMode = false; |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 551 | generateKeys(testMode, 1 /* numKeys */); |
David Drysdale | 08696a7 | 2022-03-10 10:43:25 +0000 | [diff] [blame] | 552 | auto result = corrupt_maced_key(keysToSign_[0]); |
| 553 | ASSERT_TRUE(result) << result.moveMessage(); |
| 554 | MacedPublicKey keyWithCorruptMac = result.moveValue(); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 555 | |
| 556 | bytevec keysToSignMac; |
| 557 | DeviceInfo deviceInfo; |
| 558 | ProtectedData protectedData; |
| 559 | auto status = provisionable_->generateCertificateRequest( |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 560 | testMode, {keyWithCorruptMac}, getProdEekChain(rpcHardwareInfo.supportedEekCurve), |
| 561 | challenge_, &deviceInfo, &protectedData, &keysToSignMac); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 562 | ASSERT_FALSE(status.isOk()) << status.getMessage(); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 563 | EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC); |
David Drysdale | e99ed86 | 2021-03-15 16:43:06 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /** |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 567 | * Generate a non-empty certificate request in prod mode that has a corrupt EEK chain. |
| 568 | * Confirm that the request is rejected. |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 569 | */ |
| 570 | TEST_P(CertificateRequestTest, NonEmptyCorruptEekRequest_prodMode) { |
| 571 | bool testMode = false; |
| 572 | generateKeys(testMode, 4 /* numKeys */); |
| 573 | |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 574 | auto prodEekChain = getProdEekChain(rpcHardwareInfo.supportedEekCurve); |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 575 | auto [parsedChain, _, parseErr] = cppbor::parse(prodEekChain); |
| 576 | ASSERT_NE(parsedChain, nullptr) << parseErr; |
| 577 | ASSERT_NE(parsedChain->asArray(), nullptr); |
| 578 | |
| 579 | for (int ii = 0; ii < parsedChain->asArray()->size(); ++ii) { |
| 580 | auto chain = corrupt_sig_chain(prodEekChain, ii); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 581 | ASSERT_TRUE(chain) << chain.message(); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 582 | |
| 583 | bytevec keysToSignMac; |
| 584 | DeviceInfo deviceInfo; |
| 585 | ProtectedData protectedData; |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 586 | auto status = provisionable_->generateCertificateRequest(testMode, keysToSign_, *chain, |
| 587 | challenge_, &deviceInfo, |
| 588 | &protectedData, &keysToSignMac); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 589 | ASSERT_FALSE(status.isOk()); |
| 590 | ASSERT_EQ(status.getServiceSpecificError(), |
| 591 | BnRemotelyProvisionedComponent::STATUS_INVALID_EEK); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Generate a non-empty certificate request in prod mode that has an incomplete EEK chain. |
| 597 | * Confirm that the request is rejected. |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 598 | */ |
| 599 | TEST_P(CertificateRequestTest, NonEmptyIncompleteEekRequest_prodMode) { |
| 600 | bool testMode = false; |
| 601 | generateKeys(testMode, 4 /* numKeys */); |
| 602 | |
| 603 | // Build an EEK chain that omits the first self-signed cert. |
| 604 | auto truncatedChain = cppbor::Array(); |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 605 | auto [chain, _, parseErr] = cppbor::parse(getProdEekChain(rpcHardwareInfo.supportedEekCurve)); |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 606 | ASSERT_TRUE(chain); |
| 607 | auto eekChain = chain->asArray(); |
| 608 | ASSERT_NE(eekChain, nullptr); |
| 609 | for (size_t ii = 1; ii < eekChain->size(); ii++) { |
| 610 | truncatedChain.add(eekChain->get(ii)->clone()); |
| 611 | } |
| 612 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 613 | bytevec keysToSignMac; |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 614 | DeviceInfo deviceInfo; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 615 | ProtectedData protectedData; |
David Drysdale | cceca9f | 2021-03-12 15:49:47 +0000 | [diff] [blame] | 616 | auto status = provisionable_->generateCertificateRequest( |
| 617 | testMode, keysToSign_, truncatedChain.encode(), challenge_, &deviceInfo, &protectedData, |
| 618 | &keysToSignMac); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 619 | ASSERT_FALSE(status.isOk()); |
| 620 | ASSERT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_EEK); |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Generate a non-empty certificate request in test mode, with prod keys. Must fail with |
| 625 | * STATUS_PRODUCTION_KEY_IN_TEST_REQUEST. |
| 626 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 627 | TEST_P(CertificateRequestTest, NonEmptyRequest_prodKeyInTestCert) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 628 | generateKeys(false /* testMode */, 2 /* numKeys */); |
| 629 | |
| 630 | bytevec keysToSignMac; |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 631 | DeviceInfo deviceInfo; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 632 | ProtectedData protectedData; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 633 | generateTestEekChain(3); |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 634 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 635 | true /* testMode */, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo, |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 636 | &protectedData, &keysToSignMac); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 637 | ASSERT_FALSE(status.isOk()); |
| 638 | ASSERT_EQ(status.getServiceSpecificError(), |
| 639 | BnRemotelyProvisionedComponent::STATUS_PRODUCTION_KEY_IN_TEST_REQUEST); |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Generate a non-empty certificate request in prod mode, with test keys. Must fail with |
| 644 | * STATUS_TEST_KEY_IN_PRODUCTION_REQUEST. |
| 645 | */ |
Max Bires | 126869a | 2021-02-21 18:32:59 -0800 | [diff] [blame] | 646 | TEST_P(CertificateRequestTest, NonEmptyRequest_testKeyInProdCert) { |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 647 | generateKeys(true /* testMode */, 2 /* numKeys */); |
| 648 | |
| 649 | bytevec keysToSignMac; |
Max Bires | fdbb904 | 2021-03-23 12:43:38 -0700 | [diff] [blame] | 650 | DeviceInfo deviceInfo; |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 651 | ProtectedData protectedData; |
subrahmanyaman | fb213d6 | 2022-02-02 23:10:55 +0000 | [diff] [blame] | 652 | generateTestEekChain(3); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 653 | auto status = provisionable_->generateCertificateRequest( |
Seth Moore | 19acbe9 | 2021-06-23 15:15:52 -0700 | [diff] [blame] | 654 | false /* testMode */, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo, |
David Drysdale | c840077 | 2021-03-11 12:35:11 +0000 | [diff] [blame] | 655 | &protectedData, &keysToSignMac); |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 656 | ASSERT_FALSE(status.isOk()); |
| 657 | ASSERT_EQ(status.getServiceSpecificError(), |
| 658 | BnRemotelyProvisionedComponent::STATUS_TEST_KEY_IN_PRODUCTION_REQUEST); |
| 659 | } |
| 660 | |
| 661 | INSTANTIATE_REM_PROV_AIDL_TEST(CertificateRequestTest); |
| 662 | |
Tri Vo | 0d6204e | 2022-09-29 16:15:34 -0700 | [diff] [blame] | 663 | class CertificateRequestV2Test : public CertificateRequestTestBase { |
| 664 | void SetUp() override { |
| 665 | CertificateRequestTestBase::SetUp(); |
| 666 | |
| 667 | if (rpcHardwareInfo.versionNumber < VERSION_WITHOUT_TEST_MODE) { |
| 668 | GTEST_SKIP() << "This test case only applies to RKP v3 and above. " |
| 669 | << "RKP version discovered: " << rpcHardwareInfo.versionNumber; |
| 670 | } |
| 671 | } |
| 672 | }; |
| 673 | |
| 674 | /** |
| 675 | * Generate an empty certificate request, and decrypt and verify the structure and content. |
| 676 | */ |
| 677 | TEST_P(CertificateRequestV2Test, EmptyRequest) { |
| 678 | bytevec csr; |
| 679 | |
| 680 | auto status = |
| 681 | provisionable_->generateCertificateRequestV2({} /* keysToSign */, challenge_, &csr); |
| 682 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 683 | |
| 684 | auto result = verifyProductionCsr(cppbor::Array(), csr, provisionable_.get(), challenge_); |
| 685 | ASSERT_TRUE(result) << result.message(); |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Generate a non-empty certificate request. Decrypt, parse and validate the contents. |
| 690 | */ |
| 691 | TEST_P(CertificateRequestV2Test, NonEmptyRequest) { |
| 692 | generateKeys(false /* testMode */, 1 /* numKeys */); |
| 693 | |
| 694 | bytevec csr; |
| 695 | |
| 696 | auto status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr); |
| 697 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 698 | |
| 699 | auto result = verifyProductionCsr(cborKeysToSign_, csr, provisionable_.get(), challenge_); |
| 700 | ASSERT_TRUE(result) << result.message(); |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Generate a non-empty certificate request. Make sure contents are reproducible. |
| 705 | */ |
| 706 | TEST_P(CertificateRequestV2Test, NonEmptyRequestReproducible) { |
| 707 | generateKeys(false /* testMode */, 1 /* numKeys */); |
| 708 | |
| 709 | bytevec csr; |
| 710 | |
| 711 | auto status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr); |
| 712 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 713 | |
| 714 | auto firstBcc = verifyProductionCsr(cborKeysToSign_, csr, provisionable_.get(), challenge_); |
| 715 | ASSERT_TRUE(firstBcc) << firstBcc.message(); |
| 716 | |
| 717 | status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr); |
| 718 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 719 | |
| 720 | auto secondBcc = verifyProductionCsr(cborKeysToSign_, csr, provisionable_.get(), challenge_); |
| 721 | ASSERT_TRUE(secondBcc) << secondBcc.message(); |
| 722 | |
| 723 | ASSERT_EQ(firstBcc->size(), secondBcc->size()); |
| 724 | for (auto i = 0; i < firstBcc->size(); i++) { |
| 725 | ASSERT_EQ(firstBcc->at(i).pubKey, secondBcc->at(i).pubKey); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * Generate a non-empty certificate request with multiple keys. |
| 731 | */ |
| 732 | TEST_P(CertificateRequestV2Test, NonEmptyRequestMultipleKeys) { |
| 733 | // TODO(b/254137722): define a minimum number of keys that must be supported. |
| 734 | generateKeys(false /* testMode */, 5 /* numKeys */); |
| 735 | |
| 736 | bytevec csr; |
| 737 | |
| 738 | auto status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr); |
| 739 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 740 | |
| 741 | auto result = verifyProductionCsr(cborKeysToSign_, csr, provisionable_.get(), challenge_); |
| 742 | ASSERT_TRUE(result) << result.message(); |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Generate a non-empty certificate request, but with the MAC corrupted on the keypair. |
| 747 | */ |
| 748 | TEST_P(CertificateRequestV2Test, NonEmptyRequestCorruptMac) { |
| 749 | generateKeys(false /* testMode */, 1 /* numKeys */); |
| 750 | auto result = corrupt_maced_key(keysToSign_[0]); |
| 751 | ASSERT_TRUE(result) << result.moveMessage(); |
| 752 | MacedPublicKey keyWithCorruptMac = result.moveValue(); |
| 753 | |
| 754 | bytevec csr; |
| 755 | auto status = |
| 756 | provisionable_->generateCertificateRequestV2({keyWithCorruptMac}, challenge_, &csr); |
| 757 | ASSERT_FALSE(status.isOk()) << status.getMessage(); |
| 758 | EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC); |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Generate a non-empty certificate request in prod mode, with test keys. Test mode must be |
| 763 | * ignored, i.e. test must pass. |
| 764 | */ |
| 765 | TEST_P(CertificateRequestV2Test, NonEmptyRequest_testKeyInProdCert) { |
| 766 | generateKeys(true /* testMode */, 1 /* numKeys */); |
| 767 | |
| 768 | bytevec csr; |
| 769 | auto status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr); |
| 770 | ASSERT_TRUE(status.isOk()) << status.getMessage(); |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * Call generateCertificateRequest(). Make sure it's removed. |
| 775 | */ |
| 776 | TEST_P(CertificateRequestV2Test, CertificateRequestV1Removed) { |
| 777 | generateTestEekChain(2); |
| 778 | bytevec keysToSignMac; |
| 779 | DeviceInfo deviceInfo; |
| 780 | ProtectedData protectedData; |
| 781 | auto status = provisionable_->generateCertificateRequest( |
| 782 | true /* testMode */, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo, |
| 783 | &protectedData, &keysToSignMac); |
| 784 | ASSERT_FALSE(status.isOk()) << status.getMessage(); |
| 785 | EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_REMOVED); |
| 786 | } |
| 787 | |
| 788 | INSTANTIATE_REM_PROV_AIDL_TEST(CertificateRequestV2Test); |
| 789 | |
Shawn Willden | 274bb55 | 2020-09-30 22:39:22 -0600 | [diff] [blame] | 790 | } // namespace aidl::android::hardware::security::keymint::test |