blob: 8f918af6eca1825f1474cf288f585801e92fd23f [file] [log] [blame]
Shawn Willden274bb552020-09-30 22:39:22 -06001/*
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 Mooref1f62152022-09-13 12:00:30 -070017#include <memory>
Seth Moore2fc6f832022-09-13 16:10:11 -070018#include <string>
Shawn Willden274bb552020-09-30 22:39:22 -060019#define LOG_TAG "VtsRemotelyProvisionableComponentTests"
20
Seth Moore8f810b12022-12-12 16:51:01 -080021#include <aidl/android/hardware/security/keymint/BnRemotelyProvisionedComponent.h>
Shawn Willden274bb552020-09-30 22:39:22 -060022#include <aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.h>
23#include <aidl/android/hardware/security/keymint/SecurityLevel.h>
24#include <android/binder_manager.h>
Seth Moorefc86bf42021-12-09 14:07:04 -080025#include <binder/IServiceManager.h>
Tri Voec50ee12023-02-14 16:29:53 -080026#include <cppbor.h>
Shawn Willden274bb552020-09-30 22:39:22 -060027#include <cppbor_parse.h>
Shawn Willden274bb552020-09-30 22:39:22 -060028#include <gmock/gmock.h>
Max Bires9704ff62021-04-07 11:12:01 -070029#include <keymaster/cppcose/cppcose.h>
Shawn Willden274bb552020-09-30 22:39:22 -060030#include <keymaster/keymaster_configuration.h>
David Drysdalef0d516d2021-03-22 07:51:43 +000031#include <keymint_support/authorization_set.h>
32#include <openssl/ec.h>
33#include <openssl/ec_key.h>
34#include <openssl/x509.h>
Shawn Willden274bb552020-09-30 22:39:22 -060035#include <remote_prov/remote_prov_utils.h>
Max Bires757ed422022-09-07 16:20:31 -070036#include <optional>
Seth Moorefc86bf42021-12-09 14:07:04 -080037#include <set>
Seth Moore42c11332021-07-02 15:38:17 -070038#include <vector>
Shawn Willden274bb552020-09-30 22:39:22 -060039
David Drysdalef0d516d2021-03-22 07:51:43 +000040#include "KeyMintAidlTestBase.h"
41
Shawn Willden274bb552020-09-30 22:39:22 -060042namespace aidl::android::hardware::security::keymint::test {
43
44using ::std::string;
45using ::std::vector;
46
47namespace {
48
Seth Moorefc86bf42021-12-09 14:07:04 -080049constexpr int32_t VERSION_WITH_UNIQUE_ID_SUPPORT = 2;
Andrew Scullf2ae1932023-04-24 19:09:09 +000050
51constexpr int32_t VERSION_WITHOUT_EEK = 3;
Tri Vo0d6204e2022-09-29 16:15:34 -070052constexpr int32_t VERSION_WITHOUT_TEST_MODE = 3;
Andrew Scullf2ae1932023-04-24 19:09:09 +000053constexpr int32_t VERSION_WITH_CERTIFICATE_REQUEST_V2 = 3;
54constexpr int32_t VERSION_WITH_SUPPORTED_NUM_KEYS_IN_CSR = 3;
Seth Moorefc86bf42021-12-09 14:07:04 -080055
Tommy Chiufde3ad12023-03-17 05:58:28 +000056constexpr uint8_t MIN_CHALLENGE_SIZE = 0;
57constexpr uint8_t MAX_CHALLENGE_SIZE = 64;
Alice Wangf112ec92024-10-08 07:30:28 +000058
Andrew Scull21461522024-03-25 15:08:08 +000059const string KEYMINT_STRONGBOX_INSTANCE_NAME =
60 "android.hardware.security.keymint.IKeyMintDevice/strongbox";
Tommy Chiufde3ad12023-03-17 05:58:28 +000061
Shawn Willden274bb552020-09-30 22:39:22 -060062#define INSTANTIATE_REM_PROV_AIDL_TEST(name) \
Seth Moore6305e232021-07-27 14:20:17 -070063 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name); \
Shawn Willden274bb552020-09-30 22:39:22 -060064 INSTANTIATE_TEST_SUITE_P( \
65 PerInstance, name, \
66 testing::ValuesIn(VtsRemotelyProvisionedComponentTests::build_params()), \
67 ::android::PrintInstanceNameToString)
68
Seth Moorefc86bf42021-12-09 14:07:04 -080069using ::android::sp;
Shawn Willden274bb552020-09-30 22:39:22 -060070using bytevec = std::vector<uint8_t>;
71using testing::MatchesRegex;
72using namespace remote_prov;
73using namespace keymaster;
74
75bytevec string_to_bytevec(const char* s) {
76 const uint8_t* p = reinterpret_cast<const uint8_t*>(s);
77 return bytevec(p, p + strlen(s));
78}
79
David Drysdalee99ed862021-03-15 16:43:06 +000080ErrMsgOr<MacedPublicKey> corrupt_maced_key(const MacedPublicKey& macedPubKey) {
81 auto [coseMac0, _, mac0ParseErr] = cppbor::parse(macedPubKey.macedKey);
82 if (!coseMac0 || coseMac0->asArray()->size() != kCoseMac0EntryCount) {
83 return "COSE Mac0 parse failed";
84 }
85 auto protParams = coseMac0->asArray()->get(kCoseMac0ProtectedParams)->asBstr();
86 auto unprotParams = coseMac0->asArray()->get(kCoseMac0UnprotectedParams)->asMap();
87 auto payload = coseMac0->asArray()->get(kCoseMac0Payload)->asBstr();
88 auto tag = coseMac0->asArray()->get(kCoseMac0Tag)->asBstr();
89 if (!protParams || !unprotParams || !payload || !tag) {
90 return "Invalid COSE_Sign1: missing content";
91 }
92 auto corruptMac0 = cppbor::Array();
93 corruptMac0.add(protParams->clone());
94 corruptMac0.add(unprotParams->clone());
95 corruptMac0.add(payload->clone());
96 vector<uint8_t> tagData = tag->value();
97 tagData[0] ^= 0x08;
98 tagData[tagData.size() - 1] ^= 0x80;
99 corruptMac0.add(cppbor::Bstr(tagData));
100
101 return MacedPublicKey{corruptMac0.encode()};
102}
103
David Drysdalecceca9f2021-03-12 15:49:47 +0000104ErrMsgOr<cppbor::Array> corrupt_sig(const cppbor::Array* coseSign1) {
105 if (coseSign1->size() != kCoseSign1EntryCount) {
106 return "Invalid COSE_Sign1, wrong entry count";
107 }
108 const cppbor::Bstr* protectedParams = coseSign1->get(kCoseSign1ProtectedParams)->asBstr();
109 const cppbor::Map* unprotectedParams = coseSign1->get(kCoseSign1UnprotectedParams)->asMap();
110 const cppbor::Bstr* payload = coseSign1->get(kCoseSign1Payload)->asBstr();
111 const cppbor::Bstr* signature = coseSign1->get(kCoseSign1Signature)->asBstr();
112 if (!protectedParams || !unprotectedParams || !payload || !signature) {
113 return "Invalid COSE_Sign1: missing content";
114 }
115
116 auto corruptSig = cppbor::Array();
117 corruptSig.add(protectedParams->clone());
118 corruptSig.add(unprotectedParams->clone());
119 corruptSig.add(payload->clone());
120 vector<uint8_t> sigData = signature->value();
121 sigData[0] ^= 0x08;
122 corruptSig.add(cppbor::Bstr(sigData));
123
124 return std::move(corruptSig);
125}
126
Seth Moore19acbe92021-06-23 15:15:52 -0700127ErrMsgOr<bytevec> corrupt_sig_chain(const bytevec& encodedEekChain, int which) {
128 auto [chain, _, parseErr] = cppbor::parse(encodedEekChain);
David Drysdalecceca9f2021-03-12 15:49:47 +0000129 if (!chain || !chain->asArray()) {
130 return "EekChain parse failed";
131 }
132
133 cppbor::Array* eekChain = chain->asArray();
134 if (which >= eekChain->size()) {
135 return "selected sig out of range";
136 }
137 auto corruptChain = cppbor::Array();
138
139 for (int ii = 0; ii < eekChain->size(); ++ii) {
140 if (ii == which) {
141 auto sig = corrupt_sig(eekChain->get(which)->asArray());
142 if (!sig) {
143 return "Failed to build corrupted signature" + sig.moveMessage();
144 }
145 corruptChain.add(sig.moveValue());
146 } else {
147 corruptChain.add(eekChain->get(ii)->clone());
148 }
149 }
Seth Moore19acbe92021-06-23 15:15:52 -0700150 return corruptChain.encode();
David Drysdalecceca9f2021-03-12 15:49:47 +0000151}
152
David Drysdale4d3c2982021-03-31 18:21:40 +0100153string device_suffix(const string& name) {
154 size_t pos = name.find('/');
155 if (pos == string::npos) {
156 return name;
157 }
158 return name.substr(pos + 1);
159}
160
161bool matching_keymint_device(const string& rp_name, std::shared_ptr<IKeyMintDevice>* keyMint) {
162 string rp_suffix = device_suffix(rp_name);
163
164 vector<string> km_names = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor);
165 for (const string& km_name : km_names) {
166 // If the suffix of the KeyMint instance equals the suffix of the
167 // RemotelyProvisionedComponent instance, assume they match.
168 if (device_suffix(km_name) == rp_suffix && AServiceManager_isDeclared(km_name.c_str())) {
169 ::ndk::SpAIBinder binder(AServiceManager_waitForService(km_name.c_str()));
170 *keyMint = IKeyMintDevice::fromBinder(binder);
171 return true;
172 }
173 }
174 return false;
175}
176
Shawn Willden274bb552020-09-30 22:39:22 -0600177} // namespace
178
179class VtsRemotelyProvisionedComponentTests : public testing::TestWithParam<std::string> {
180 public:
181 virtual void SetUp() override {
182 if (AServiceManager_isDeclared(GetParam().c_str())) {
183 ::ndk::SpAIBinder binder(AServiceManager_waitForService(GetParam().c_str()));
184 provisionable_ = IRemotelyProvisionedComponent::fromBinder(binder);
185 }
186 ASSERT_NE(provisionable_, nullptr);
Alice Wang895f4b32023-11-14 07:38:18 +0000187 auto status = provisionable_->getHardwareInfo(&rpcHardwareInfo);
Alice Wangf112ec92024-10-08 07:30:28 +0000188 isRkpVmInstance_ = GetParam() == RKPVM_INSTANCE_NAME;
Karuna Wadheraca6d8cc2024-07-18 14:08:36 +0000189 if (isRkpVmInstance_) {
Alice Wang46ff2862024-05-14 07:07:05 +0000190 if (status.getExceptionCode() == EX_UNSUPPORTED_OPERATION) {
191 GTEST_SKIP() << "The RKP VM is not supported on this system.";
192 }
193 int apiLevel = get_vsr_api_level();
194 if (apiLevel < __ANDROID_API_V__) {
195 GTEST_SKIP() << "The RKP VM is supported only on V+ devices. Vendor API level: "
196 << apiLevel;
197 }
Alice Wang895f4b32023-11-14 07:38:18 +0000198 }
199 ASSERT_TRUE(status.isOk());
Shawn Willden274bb552020-09-30 22:39:22 -0600200 }
201
202 static vector<string> build_params() {
203 auto params = ::android::getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor);
204 return params;
205 }
206
207 protected:
208 std::shared_ptr<IRemotelyProvisionedComponent> provisionable_;
subrahmanyamanfb213d62022-02-02 23:10:55 +0000209 RpcHardwareInfo rpcHardwareInfo;
Karuna Wadheraca6d8cc2024-07-18 14:08:36 +0000210 bool isRkpVmInstance_;
Shawn Willden274bb552020-09-30 22:39:22 -0600211};
212
Seth Moorefc86bf42021-12-09 14:07:04 -0800213/**
214 * Verify that every implementation reports a different unique id.
215 */
216TEST(NonParameterizedTests, eachRpcHasAUniqueId) {
217 std::set<std::string> uniqueIds;
218 for (auto hal : ::android::getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor)) {
219 ASSERT_TRUE(AServiceManager_isDeclared(hal.c_str()));
220 ::ndk::SpAIBinder binder(AServiceManager_waitForService(hal.c_str()));
221 std::shared_ptr<IRemotelyProvisionedComponent> rpc =
222 IRemotelyProvisionedComponent::fromBinder(binder);
223 ASSERT_NE(rpc, nullptr);
224
225 RpcHardwareInfo hwInfo;
Alice Wang895f4b32023-11-14 07:38:18 +0000226 auto status = rpc->getHardwareInfo(&hwInfo);
Alice Wangf112ec92024-10-08 07:30:28 +0000227 if (hal == RKPVM_INSTANCE_NAME && status.getExceptionCode() == EX_UNSUPPORTED_OPERATION) {
Alice Wang895f4b32023-11-14 07:38:18 +0000228 GTEST_SKIP() << "The RKP VM is not supported on this system.";
229 }
230 ASSERT_TRUE(status.isOk());
Seth Moorefc86bf42021-12-09 14:07:04 -0800231
Tri Vodd12c482022-10-12 22:41:28 +0000232 if (hwInfo.versionNumber >= VERSION_WITH_UNIQUE_ID_SUPPORT) {
Seth Moorefc86bf42021-12-09 14:07:04 -0800233 ASSERT_TRUE(hwInfo.uniqueId);
234 auto [_, wasInserted] = uniqueIds.insert(*hwInfo.uniqueId);
235 EXPECT_TRUE(wasInserted);
236 } else {
237 ASSERT_FALSE(hwInfo.uniqueId);
238 }
239 }
240}
241
Andrew Scull21461522024-03-25 15:08:08 +0000242/**
243 * Verify that the default implementation supports DICE if there is a StrongBox KeyMint instance
244 * on the device.
245 */
246// @VsrTest = 3.10-015
247TEST(NonParameterizedTests, requireDiceOnDefaultInstanceIfStrongboxPresent) {
248 int vsr_api_level = get_vsr_api_level();
249 if (vsr_api_level < 35) {
250 GTEST_SKIP() << "Applies only to VSR API level 35 or newer, this device is: "
251 << vsr_api_level;
252 }
253
254 if (!AServiceManager_isDeclared(KEYMINT_STRONGBOX_INSTANCE_NAME.c_str())) {
255 GTEST_SKIP() << "Strongbox is not present on this device.";
256 }
257
258 ::ndk::SpAIBinder binder(AServiceManager_waitForService(DEFAULT_INSTANCE_NAME.c_str()));
259 std::shared_ptr<IRemotelyProvisionedComponent> rpc =
260 IRemotelyProvisionedComponent::fromBinder(binder);
261 ASSERT_NE(rpc, nullptr);
262
263 bytevec challenge = randomBytes(64);
264 bytevec csr;
265 auto status = rpc->generateCertificateRequestV2({} /* keysToSign */, challenge, &csr);
266 EXPECT_TRUE(status.isOk()) << status.getDescription();
267
Alice Wangf112ec92024-10-08 07:30:28 +0000268 auto result = isCsrWithProperDiceChain(csr, DEFAULT_INSTANCE_NAME);
Andrew Scull21461522024-03-25 15:08:08 +0000269 ASSERT_TRUE(result) << result.message();
270 ASSERT_TRUE(*result);
271}
272
Seth Moorefc86bf42021-12-09 14:07:04 -0800273using GetHardwareInfoTests = VtsRemotelyProvisionedComponentTests;
274
275INSTANTIATE_REM_PROV_AIDL_TEST(GetHardwareInfoTests);
276
277/**
278 * Verify that a valid curve is reported by the implementation.
279 */
280TEST_P(GetHardwareInfoTests, supportsValidCurve) {
281 RpcHardwareInfo hwInfo;
282 ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk());
283
Andrew Scullf2ae1932023-04-24 19:09:09 +0000284 if (rpcHardwareInfo.versionNumber >= VERSION_WITHOUT_EEK) {
285 ASSERT_EQ(hwInfo.supportedEekCurve, RpcHardwareInfo::CURVE_NONE)
286 << "Invalid curve: " << hwInfo.supportedEekCurve;
287 return;
Hasini Gunasinghe666b2712023-01-05 21:35:51 +0000288 }
Andrew Scullf2ae1932023-04-24 19:09:09 +0000289
290 const std::set<int> validCurves = {RpcHardwareInfo::CURVE_P256, RpcHardwareInfo::CURVE_25519};
Seth Moorefc86bf42021-12-09 14:07:04 -0800291 ASSERT_EQ(validCurves.count(hwInfo.supportedEekCurve), 1)
292 << "Invalid curve: " << hwInfo.supportedEekCurve;
293}
294
295/**
296 * Verify that the unique id is within the length limits as described in RpcHardwareInfo.aidl.
297 */
298TEST_P(GetHardwareInfoTests, uniqueId) {
Tri Vodd12c482022-10-12 22:41:28 +0000299 if (rpcHardwareInfo.versionNumber < VERSION_WITH_UNIQUE_ID_SUPPORT) {
Seth Moorefc86bf42021-12-09 14:07:04 -0800300 return;
301 }
302
303 RpcHardwareInfo hwInfo;
304 ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk());
305 ASSERT_TRUE(hwInfo.uniqueId);
306 EXPECT_GE(hwInfo.uniqueId->size(), 1);
307 EXPECT_LE(hwInfo.uniqueId->size(), 32);
308}
309
Tri Vo9cab73c2022-10-28 13:40:24 -0700310/**
311 * Verify implementation supports at least MIN_SUPPORTED_NUM_KEYS_IN_CSR keys in a CSR.
312 */
313TEST_P(GetHardwareInfoTests, supportedNumKeysInCsr) {
Andrew Scullf2ae1932023-04-24 19:09:09 +0000314 if (rpcHardwareInfo.versionNumber < VERSION_WITH_SUPPORTED_NUM_KEYS_IN_CSR) {
Tri Vo9cab73c2022-10-28 13:40:24 -0700315 return;
316 }
317
318 RpcHardwareInfo hwInfo;
319 ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk());
320 ASSERT_GE(hwInfo.supportedNumKeysInCsr, RpcHardwareInfo::MIN_SUPPORTED_NUM_KEYS_IN_CSR);
321}
322
Shawn Willden274bb552020-09-30 22:39:22 -0600323using GenerateKeyTests = VtsRemotelyProvisionedComponentTests;
324
325INSTANTIATE_REM_PROV_AIDL_TEST(GenerateKeyTests);
326
327/**
David Drysdalef0d516d2021-03-22 07:51:43 +0000328 * Generate and validate a production-mode key. MAC tag can't be verified, but
329 * the private key blob should be usable in KeyMint operations.
Shawn Willden274bb552020-09-30 22:39:22 -0600330 */
Max Bires126869a2021-02-21 18:32:59 -0800331TEST_P(GenerateKeyTests, generateEcdsaP256Key_prodMode) {
Shawn Willden274bb552020-09-30 22:39:22 -0600332 MacedPublicKey macedPubKey;
333 bytevec privateKeyBlob;
334 bool testMode = false;
335 auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob);
336 ASSERT_TRUE(status.isOk());
David Drysdalef0d516d2021-03-22 07:51:43 +0000337 vector<uint8_t> coseKeyData;
Tri Vob0b8acc2022-11-30 16:22:23 -0800338 check_maced_pubkey(macedPubKey, testMode, &coseKeyData);
David Drysdale4d3c2982021-03-31 18:21:40 +0100339}
340
341/**
342 * Generate and validate a production-mode key, then use it as a KeyMint attestation key.
343 */
344TEST_P(GenerateKeyTests, generateAndUseEcdsaP256Key_prodMode) {
345 // See if there is a matching IKeyMintDevice for this IRemotelyProvisionedComponent.
346 std::shared_ptr<IKeyMintDevice> keyMint;
347 if (!matching_keymint_device(GetParam(), &keyMint)) {
348 // No matching IKeyMintDevice.
349 GTEST_SKIP() << "Skipping key use test as no matching KeyMint device found";
350 return;
351 }
352 KeyMintHardwareInfo info;
353 ASSERT_TRUE(keyMint->getHardwareInfo(&info).isOk());
354
355 MacedPublicKey macedPubKey;
356 bytevec privateKeyBlob;
357 bool testMode = false;
358 auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob);
359 ASSERT_TRUE(status.isOk());
360 vector<uint8_t> coseKeyData;
Tri Vob0b8acc2022-11-30 16:22:23 -0800361 check_maced_pubkey(macedPubKey, testMode, &coseKeyData);
David Drysdale4d3c2982021-03-31 18:21:40 +0100362
David Drysdalef0d516d2021-03-22 07:51:43 +0000363 AttestationKey attestKey;
364 attestKey.keyBlob = std::move(privateKeyBlob);
365 attestKey.issuerSubjectName = make_name_from_str("Android Keystore Key");
Shawn Willden274bb552020-09-30 22:39:22 -0600366
David Drysdalef0d516d2021-03-22 07:51:43 +0000367 // Generate an ECDSA key that is attested by the generated P256 keypair.
368 AuthorizationSet keyDesc = AuthorizationSetBuilder()
369 .Authorization(TAG_NO_AUTH_REQUIRED)
David Drysdale915ce252021-10-14 15:17:36 +0100370 .EcdsaSigningKey(EcCurve::P_256)
David Drysdalef0d516d2021-03-22 07:51:43 +0000371 .AttestationChallenge("foo")
372 .AttestationApplicationId("bar")
373 .Digest(Digest::NONE)
374 .SetDefaultValidity();
375 KeyCreationResult creationResult;
376 auto result = keyMint->generateKey(keyDesc.vector_data(), attestKey, &creationResult);
377 ASSERT_TRUE(result.isOk());
378 vector<uint8_t> attested_key_blob = std::move(creationResult.keyBlob);
379 vector<KeyCharacteristics> attested_key_characteristics =
380 std::move(creationResult.keyCharacteristics);
381 vector<Certificate> attested_key_cert_chain = std::move(creationResult.certificateChain);
382 EXPECT_EQ(attested_key_cert_chain.size(), 1);
383
David Drysdale7dff4fc2021-12-10 10:10:52 +0000384 int32_t aidl_version = 0;
385 ASSERT_TRUE(keyMint->getInterfaceVersion(&aidl_version).isOk());
David Drysdalef0d516d2021-03-22 07:51:43 +0000386 AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics);
387 AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000388 EXPECT_TRUE(verify_attestation_record(aidl_version, "foo", "bar", sw_enforced, hw_enforced,
David Drysdalef0d516d2021-03-22 07:51:43 +0000389 info.securityLevel,
390 attested_key_cert_chain[0].encodedCertificate));
391
392 // Attestation by itself is not valid (last entry is not self-signed).
393 EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain));
394
395 // The signature over the attested key should correspond to the P256 public key.
396 X509_Ptr key_cert(parse_cert_blob(attested_key_cert_chain[0].encodedCertificate));
397 ASSERT_TRUE(key_cert.get());
398 EVP_PKEY_Ptr signing_pubkey;
399 p256_pub_key(coseKeyData, &signing_pubkey);
400 ASSERT_TRUE(signing_pubkey.get());
401
402 ASSERT_TRUE(X509_verify(key_cert.get(), signing_pubkey.get()))
403 << "Verification of attested certificate failed "
404 << "OpenSSL error string: " << ERR_error_string(ERR_get_error(), NULL);
Shawn Willden274bb552020-09-30 22:39:22 -0600405}
406
407/**
408 * Generate and validate a test-mode key.
409 */
Max Bires126869a2021-02-21 18:32:59 -0800410TEST_P(GenerateKeyTests, generateEcdsaP256Key_testMode) {
Shawn Willden274bb552020-09-30 22:39:22 -0600411 MacedPublicKey macedPubKey;
412 bytevec privateKeyBlob;
413 bool testMode = true;
414 auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob);
Andrew Scullf2ae1932023-04-24 19:09:09 +0000415
416 if (rpcHardwareInfo.versionNumber >= VERSION_WITHOUT_TEST_MODE) {
417 ASSERT_FALSE(status.isOk());
418 EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_REMOVED);
419 return;
420 }
421
Shawn Willden274bb552020-09-30 22:39:22 -0600422 ASSERT_TRUE(status.isOk());
Tri Vob0b8acc2022-11-30 16:22:23 -0800423 check_maced_pubkey(macedPubKey, testMode, nullptr);
Shawn Willden274bb552020-09-30 22:39:22 -0600424}
425
Tri Vo0d6204e2022-09-29 16:15:34 -0700426class CertificateRequestTestBase : public VtsRemotelyProvisionedComponentTests {
Shawn Willden274bb552020-09-30 22:39:22 -0600427 protected:
Tri Vo0d6204e2022-09-29 16:15:34 -0700428 CertificateRequestTestBase()
429 : eekId_(string_to_bytevec("eekid")), challenge_(randomBytes(64)) {}
David Drysdalecceca9f2021-03-12 15:49:47 +0000430
Seth Moore19acbe92021-06-23 15:15:52 -0700431 void generateTestEekChain(size_t eekLength) {
subrahmanyamanfb213d62022-02-02 23:10:55 +0000432 auto chain = generateEekChain(rpcHardwareInfo.supportedEekCurve, eekLength, eekId_);
David Drysdale08696a72022-03-10 10:43:25 +0000433 ASSERT_TRUE(chain) << chain.message();
Seth Moore19acbe92021-06-23 15:15:52 -0700434 if (chain) testEekChain_ = chain.moveValue();
435 testEekLength_ = eekLength;
Shawn Willden274bb552020-09-30 22:39:22 -0600436 }
437
438 void generateKeys(bool testMode, size_t numKeys) {
439 keysToSign_ = std::vector<MacedPublicKey>(numKeys);
440 cborKeysToSign_ = cppbor::Array();
441
442 for (auto& key : keysToSign_) {
443 bytevec privateKeyBlob;
444 auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &key, &privateKeyBlob);
Frederick Mayle9891be02023-11-03 14:39:24 -0700445 ASSERT_TRUE(status.isOk()) << status.getDescription();
Shawn Willden274bb552020-09-30 22:39:22 -0600446
David Drysdalec8400772021-03-11 12:35:11 +0000447 vector<uint8_t> payload_value;
Tri Vob0b8acc2022-11-30 16:22:23 -0800448 check_maced_pubkey(key, testMode, &payload_value);
David Drysdalec8400772021-03-11 12:35:11 +0000449 cborKeysToSign_.add(cppbor::EncodedItem(payload_value));
Shawn Willden274bb552020-09-30 22:39:22 -0600450 }
451 }
452
453 bytevec eekId_;
Seth Moore19acbe92021-06-23 15:15:52 -0700454 size_t testEekLength_;
455 EekChain testEekChain_;
David Drysdalec8400772021-03-11 12:35:11 +0000456 bytevec challenge_;
Shawn Willden274bb552020-09-30 22:39:22 -0600457 std::vector<MacedPublicKey> keysToSign_;
458 cppbor::Array cborKeysToSign_;
459};
460
Tri Vo0d6204e2022-09-29 16:15:34 -0700461class CertificateRequestTest : public CertificateRequestTestBase {
462 protected:
463 void SetUp() override {
464 CertificateRequestTestBase::SetUp();
Andrew Scull1bcb6022022-12-27 10:43:27 +0000465 ASSERT_FALSE(HasFatalFailure());
Tri Vo0d6204e2022-09-29 16:15:34 -0700466
Andrew Scullf2ae1932023-04-24 19:09:09 +0000467 if (rpcHardwareInfo.versionNumber >= VERSION_WITH_CERTIFICATE_REQUEST_V2) {
Andrew Scull95b31312023-03-27 19:16:07 +0000468 GTEST_SKIP() << "This test case only applies to RKP v1 and v2. "
469 << "RKP version discovered: " << rpcHardwareInfo.versionNumber;
Tri Vo0d6204e2022-09-29 16:15:34 -0700470 }
471 }
472};
473
Shawn Willden274bb552020-09-30 22:39:22 -0600474/**
475 * Generate an empty certificate request in test mode, and decrypt and verify the structure and
476 * content.
477 */
Max Bires126869a2021-02-21 18:32:59 -0800478TEST_P(CertificateRequestTest, EmptyRequest_testMode) {
Shawn Willden274bb552020-09-30 22:39:22 -0600479 bool testMode = true;
David Drysdalecceca9f2021-03-12 15:49:47 +0000480 for (size_t eekLength : {2, 3, 7}) {
481 SCOPED_TRACE(testing::Message() << "EEK of length " << eekLength);
Seth Moore19acbe92021-06-23 15:15:52 -0700482 generateTestEekChain(eekLength);
Shawn Willden274bb552020-09-30 22:39:22 -0600483
David Drysdalecceca9f2021-03-12 15:49:47 +0000484 bytevec keysToSignMac;
485 DeviceInfo deviceInfo;
486 ProtectedData protectedData;
487 auto status = provisionable_->generateCertificateRequest(
Seth Moore19acbe92021-06-23 15:15:52 -0700488 testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo,
David Drysdalecceca9f2021-03-12 15:49:47 +0000489 &protectedData, &keysToSignMac);
Frederick Mayle9891be02023-11-03 14:39:24 -0700490 ASSERT_TRUE(status.isOk()) << status.getDescription();
David Drysdalecceca9f2021-03-12 15:49:47 +0000491
Seth Moore2fc6f832022-09-13 16:10:11 -0700492 auto result = verifyProductionProtectedData(
493 deviceInfo, cppbor::Array(), keysToSignMac, protectedData, testEekChain_, eekId_,
Alice Wangf112ec92024-10-08 07:30:28 +0000494 rpcHardwareInfo.supportedEekCurve, provisionable_.get(), GetParam(), challenge_);
Seth Moore2fc6f832022-09-13 16:10:11 -0700495 ASSERT_TRUE(result) << result.message();
David Drysdalecceca9f2021-03-12 15:49:47 +0000496 }
Shawn Willden274bb552020-09-30 22:39:22 -0600497}
498
499/**
Seth Moore42c11332021-07-02 15:38:17 -0700500 * Ensure that test mode outputs a unique BCC root key every time we request a
501 * certificate request. Else, it's possible that the test mode API could be used
502 * to fingerprint devices. Only the GEEK should be allowed to decrypt the same
503 * device public key multiple times.
504 */
505TEST_P(CertificateRequestTest, NewKeyPerCallInTestMode) {
506 constexpr bool testMode = true;
Seth Moore42c11332021-07-02 15:38:17 -0700507
508 bytevec keysToSignMac;
509 DeviceInfo deviceInfo;
510 ProtectedData protectedData;
subrahmanyamanfb213d62022-02-02 23:10:55 +0000511 generateTestEekChain(3);
Seth Moore42c11332021-07-02 15:38:17 -0700512 auto status = provisionable_->generateCertificateRequest(
Seth Moore19acbe92021-06-23 15:15:52 -0700513 testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo,
514 &protectedData, &keysToSignMac);
Frederick Mayle9891be02023-11-03 14:39:24 -0700515 ASSERT_TRUE(status.isOk()) << status.getDescription();
Seth Moore42c11332021-07-02 15:38:17 -0700516
Alice Wangf112ec92024-10-08 07:30:28 +0000517 auto firstBcc = verifyProductionProtectedData(deviceInfo, /*keysToSign=*/cppbor::Array(),
518 keysToSignMac, protectedData, testEekChain_,
519 eekId_, rpcHardwareInfo.supportedEekCurve,
520 provisionable_.get(), GetParam(), challenge_);
Seth Moore2fc6f832022-09-13 16:10:11 -0700521 ASSERT_TRUE(firstBcc) << firstBcc.message();
Seth Moore42c11332021-07-02 15:38:17 -0700522
Seth Moore19acbe92021-06-23 15:15:52 -0700523 status = provisionable_->generateCertificateRequest(
524 testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo,
525 &protectedData, &keysToSignMac);
Frederick Mayle9891be02023-11-03 14:39:24 -0700526 ASSERT_TRUE(status.isOk()) << status.getDescription();
Seth Moore42c11332021-07-02 15:38:17 -0700527
Alice Wangf112ec92024-10-08 07:30:28 +0000528 auto secondBcc = verifyProductionProtectedData(deviceInfo, /*keysToSign=*/cppbor::Array(),
529 keysToSignMac, protectedData, testEekChain_,
530 eekId_, rpcHardwareInfo.supportedEekCurve,
531 provisionable_.get(), GetParam(), challenge_);
Seth Moore2fc6f832022-09-13 16:10:11 -0700532 ASSERT_TRUE(secondBcc) << secondBcc.message();
Seth Moore42c11332021-07-02 15:38:17 -0700533
534 // Verify that none of the keys in the first BCC are repeated in the second one.
Seth Moore2fc6f832022-09-13 16:10:11 -0700535 for (const auto& i : *firstBcc) {
536 for (auto& j : *secondBcc) {
Seth Moore42c11332021-07-02 15:38:17 -0700537 ASSERT_THAT(i.pubKey, testing::Not(testing::ElementsAreArray(j.pubKey)))
538 << "Found a repeated pubkey in two generateCertificateRequest test mode calls";
539 }
540 }
541}
542
543/**
Seth Moore19acbe92021-06-23 15:15:52 -0700544 * Generate an empty certificate request in prod mode. This test must be run explicitly, and
545 * is not run by default. Not all devices are GMS devices, and therefore they do not all
546 * trust the Google EEK root.
Shawn Willden274bb552020-09-30 22:39:22 -0600547 */
Seth Moore19acbe92021-06-23 15:15:52 -0700548TEST_P(CertificateRequestTest, DISABLED_EmptyRequest_prodMode) {
Shawn Willden274bb552020-09-30 22:39:22 -0600549 bool testMode = false;
David Drysdalecceca9f2021-03-12 15:49:47 +0000550
Seth Moore19acbe92021-06-23 15:15:52 -0700551 bytevec keysToSignMac;
552 DeviceInfo deviceInfo;
553 ProtectedData protectedData;
554 auto status = provisionable_->generateCertificateRequest(
subrahmanyamanfb213d62022-02-02 23:10:55 +0000555 testMode, {} /* keysToSign */, getProdEekChain(rpcHardwareInfo.supportedEekCurve),
556 challenge_, &deviceInfo, &protectedData, &keysToSignMac);
Seth Moore19acbe92021-06-23 15:15:52 -0700557 EXPECT_TRUE(status.isOk());
Shawn Willden274bb552020-09-30 22:39:22 -0600558}
559
560/**
561 * Generate a non-empty certificate request in test mode. Decrypt, parse and validate the contents.
562 */
Max Bires126869a2021-02-21 18:32:59 -0800563TEST_P(CertificateRequestTest, NonEmptyRequest_testMode) {
Shawn Willden274bb552020-09-30 22:39:22 -0600564 bool testMode = true;
565 generateKeys(testMode, 4 /* numKeys */);
566
David Drysdalecceca9f2021-03-12 15:49:47 +0000567 for (size_t eekLength : {2, 3, 7}) {
568 SCOPED_TRACE(testing::Message() << "EEK of length " << eekLength);
Seth Moore19acbe92021-06-23 15:15:52 -0700569 generateTestEekChain(eekLength);
Shawn Willden274bb552020-09-30 22:39:22 -0600570
David Drysdalecceca9f2021-03-12 15:49:47 +0000571 bytevec keysToSignMac;
572 DeviceInfo deviceInfo;
573 ProtectedData protectedData;
574 auto status = provisionable_->generateCertificateRequest(
Seth Moore19acbe92021-06-23 15:15:52 -0700575 testMode, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo, &protectedData,
David Drysdalecceca9f2021-03-12 15:49:47 +0000576 &keysToSignMac);
Frederick Mayle9891be02023-11-03 14:39:24 -0700577 ASSERT_TRUE(status.isOk()) << status.getDescription();
David Drysdalecceca9f2021-03-12 15:49:47 +0000578
Seth Moore2fc6f832022-09-13 16:10:11 -0700579 auto result = verifyProductionProtectedData(
580 deviceInfo, cborKeysToSign_, keysToSignMac, protectedData, testEekChain_, eekId_,
Alice Wangf112ec92024-10-08 07:30:28 +0000581 rpcHardwareInfo.supportedEekCurve, provisionable_.get(), GetParam(), challenge_);
Seth Moore2fc6f832022-09-13 16:10:11 -0700582 ASSERT_TRUE(result) << result.message();
David Drysdalecceca9f2021-03-12 15:49:47 +0000583 }
Shawn Willden274bb552020-09-30 22:39:22 -0600584}
585
586/**
Seth Moore19acbe92021-06-23 15:15:52 -0700587 * Generate a non-empty certificate request in prod mode. This test must be run explicitly, and
588 * is not run by default. Not all devices are GMS devices, and therefore they do not all
589 * trust the Google EEK root.
Shawn Willden274bb552020-09-30 22:39:22 -0600590 */
Seth Moore19acbe92021-06-23 15:15:52 -0700591TEST_P(CertificateRequestTest, DISABLED_NonEmptyRequest_prodMode) {
Shawn Willden274bb552020-09-30 22:39:22 -0600592 bool testMode = false;
593 generateKeys(testMode, 4 /* numKeys */);
594
Seth Moore19acbe92021-06-23 15:15:52 -0700595 bytevec keysToSignMac;
596 DeviceInfo deviceInfo;
597 ProtectedData protectedData;
598 auto status = provisionable_->generateCertificateRequest(
subrahmanyamanfb213d62022-02-02 23:10:55 +0000599 testMode, keysToSign_, getProdEekChain(rpcHardwareInfo.supportedEekCurve), challenge_,
600 &deviceInfo, &protectedData, &keysToSignMac);
Seth Moore19acbe92021-06-23 15:15:52 -0700601 EXPECT_TRUE(status.isOk());
David Drysdalecceca9f2021-03-12 15:49:47 +0000602}
603
604/**
David Drysdalee99ed862021-03-15 16:43:06 +0000605 * Generate a non-empty certificate request in test mode, but with the MAC corrupted on the keypair.
606 */
607TEST_P(CertificateRequestTest, NonEmptyRequestCorruptMac_testMode) {
608 bool testMode = true;
609 generateKeys(testMode, 1 /* numKeys */);
David Drysdale08696a72022-03-10 10:43:25 +0000610 auto result = corrupt_maced_key(keysToSign_[0]);
611 ASSERT_TRUE(result) << result.moveMessage();
612 MacedPublicKey keyWithCorruptMac = result.moveValue();
David Drysdalee99ed862021-03-15 16:43:06 +0000613
614 bytevec keysToSignMac;
615 DeviceInfo deviceInfo;
616 ProtectedData protectedData;
subrahmanyamanfb213d62022-02-02 23:10:55 +0000617 generateTestEekChain(3);
David Drysdalee99ed862021-03-15 16:43:06 +0000618 auto status = provisionable_->generateCertificateRequest(
Seth Moore19acbe92021-06-23 15:15:52 -0700619 testMode, {keyWithCorruptMac}, testEekChain_.chain, challenge_, &deviceInfo,
620 &protectedData, &keysToSignMac);
Frederick Mayle9891be02023-11-03 14:39:24 -0700621 ASSERT_FALSE(status.isOk()) << status.getDescription();
David Drysdalee99ed862021-03-15 16:43:06 +0000622 EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC);
623}
624
625/**
626 * Generate a non-empty certificate request in prod mode, but with the MAC corrupted on the keypair.
627 */
628TEST_P(CertificateRequestTest, NonEmptyRequestCorruptMac_prodMode) {
Seth Moore19acbe92021-06-23 15:15:52 -0700629 bool testMode = false;
David Drysdalee99ed862021-03-15 16:43:06 +0000630 generateKeys(testMode, 1 /* numKeys */);
David Drysdale08696a72022-03-10 10:43:25 +0000631 auto result = corrupt_maced_key(keysToSign_[0]);
632 ASSERT_TRUE(result) << result.moveMessage();
633 MacedPublicKey keyWithCorruptMac = result.moveValue();
David Drysdalee99ed862021-03-15 16:43:06 +0000634
635 bytevec keysToSignMac;
636 DeviceInfo deviceInfo;
637 ProtectedData protectedData;
638 auto status = provisionable_->generateCertificateRequest(
subrahmanyamanfb213d62022-02-02 23:10:55 +0000639 testMode, {keyWithCorruptMac}, getProdEekChain(rpcHardwareInfo.supportedEekCurve),
640 challenge_, &deviceInfo, &protectedData, &keysToSignMac);
Frederick Mayle9891be02023-11-03 14:39:24 -0700641 ASSERT_FALSE(status.isOk()) << status.getDescription();
Seth Moore19acbe92021-06-23 15:15:52 -0700642 EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC);
David Drysdalee99ed862021-03-15 16:43:06 +0000643}
644
645/**
David Drysdalecceca9f2021-03-12 15:49:47 +0000646 * Generate a non-empty certificate request in prod mode that has a corrupt EEK chain.
647 * Confirm that the request is rejected.
David Drysdalecceca9f2021-03-12 15:49:47 +0000648 */
649TEST_P(CertificateRequestTest, NonEmptyCorruptEekRequest_prodMode) {
650 bool testMode = false;
651 generateKeys(testMode, 4 /* numKeys */);
652
subrahmanyamanfb213d62022-02-02 23:10:55 +0000653 auto prodEekChain = getProdEekChain(rpcHardwareInfo.supportedEekCurve);
Seth Moore19acbe92021-06-23 15:15:52 -0700654 auto [parsedChain, _, parseErr] = cppbor::parse(prodEekChain);
655 ASSERT_NE(parsedChain, nullptr) << parseErr;
656 ASSERT_NE(parsedChain->asArray(), nullptr);
657
658 for (int ii = 0; ii < parsedChain->asArray()->size(); ++ii) {
659 auto chain = corrupt_sig_chain(prodEekChain, ii);
David Drysdalecceca9f2021-03-12 15:49:47 +0000660 ASSERT_TRUE(chain) << chain.message();
David Drysdalecceca9f2021-03-12 15:49:47 +0000661
662 bytevec keysToSignMac;
663 DeviceInfo deviceInfo;
664 ProtectedData protectedData;
Seth Moore19acbe92021-06-23 15:15:52 -0700665 auto status = provisionable_->generateCertificateRequest(testMode, keysToSign_, *chain,
666 challenge_, &deviceInfo,
667 &protectedData, &keysToSignMac);
David Drysdalecceca9f2021-03-12 15:49:47 +0000668 ASSERT_FALSE(status.isOk());
669 ASSERT_EQ(status.getServiceSpecificError(),
670 BnRemotelyProvisionedComponent::STATUS_INVALID_EEK);
671 }
672}
673
674/**
675 * Generate a non-empty certificate request in prod mode that has an incomplete EEK chain.
676 * Confirm that the request is rejected.
David Drysdalecceca9f2021-03-12 15:49:47 +0000677 */
678TEST_P(CertificateRequestTest, NonEmptyIncompleteEekRequest_prodMode) {
679 bool testMode = false;
680 generateKeys(testMode, 4 /* numKeys */);
681
682 // Build an EEK chain that omits the first self-signed cert.
683 auto truncatedChain = cppbor::Array();
subrahmanyamanfb213d62022-02-02 23:10:55 +0000684 auto [chain, _, parseErr] = cppbor::parse(getProdEekChain(rpcHardwareInfo.supportedEekCurve));
David Drysdalecceca9f2021-03-12 15:49:47 +0000685 ASSERT_TRUE(chain);
686 auto eekChain = chain->asArray();
687 ASSERT_NE(eekChain, nullptr);
688 for (size_t ii = 1; ii < eekChain->size(); ii++) {
689 truncatedChain.add(eekChain->get(ii)->clone());
690 }
691
Shawn Willden274bb552020-09-30 22:39:22 -0600692 bytevec keysToSignMac;
Max Biresfdbb9042021-03-23 12:43:38 -0700693 DeviceInfo deviceInfo;
Shawn Willden274bb552020-09-30 22:39:22 -0600694 ProtectedData protectedData;
David Drysdalecceca9f2021-03-12 15:49:47 +0000695 auto status = provisionable_->generateCertificateRequest(
696 testMode, keysToSign_, truncatedChain.encode(), challenge_, &deviceInfo, &protectedData,
697 &keysToSignMac);
Shawn Willden274bb552020-09-30 22:39:22 -0600698 ASSERT_FALSE(status.isOk());
699 ASSERT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_EEK);
700}
701
702/**
703 * Generate a non-empty certificate request in test mode, with prod keys. Must fail with
704 * STATUS_PRODUCTION_KEY_IN_TEST_REQUEST.
705 */
Max Bires126869a2021-02-21 18:32:59 -0800706TEST_P(CertificateRequestTest, NonEmptyRequest_prodKeyInTestCert) {
Shawn Willden274bb552020-09-30 22:39:22 -0600707 generateKeys(false /* testMode */, 2 /* numKeys */);
708
709 bytevec keysToSignMac;
Max Biresfdbb9042021-03-23 12:43:38 -0700710 DeviceInfo deviceInfo;
Shawn Willden274bb552020-09-30 22:39:22 -0600711 ProtectedData protectedData;
subrahmanyamanfb213d62022-02-02 23:10:55 +0000712 generateTestEekChain(3);
Max Biresfdbb9042021-03-23 12:43:38 -0700713 auto status = provisionable_->generateCertificateRequest(
Seth Moore19acbe92021-06-23 15:15:52 -0700714 true /* testMode */, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo,
Max Biresfdbb9042021-03-23 12:43:38 -0700715 &protectedData, &keysToSignMac);
Shawn Willden274bb552020-09-30 22:39:22 -0600716 ASSERT_FALSE(status.isOk());
717 ASSERT_EQ(status.getServiceSpecificError(),
718 BnRemotelyProvisionedComponent::STATUS_PRODUCTION_KEY_IN_TEST_REQUEST);
719}
720
721/**
722 * Generate a non-empty certificate request in prod mode, with test keys. Must fail with
723 * STATUS_TEST_KEY_IN_PRODUCTION_REQUEST.
724 */
Max Bires126869a2021-02-21 18:32:59 -0800725TEST_P(CertificateRequestTest, NonEmptyRequest_testKeyInProdCert) {
Shawn Willden274bb552020-09-30 22:39:22 -0600726 generateKeys(true /* testMode */, 2 /* numKeys */);
727
728 bytevec keysToSignMac;
Max Biresfdbb9042021-03-23 12:43:38 -0700729 DeviceInfo deviceInfo;
Shawn Willden274bb552020-09-30 22:39:22 -0600730 ProtectedData protectedData;
subrahmanyamanfb213d62022-02-02 23:10:55 +0000731 generateTestEekChain(3);
Shawn Willden274bb552020-09-30 22:39:22 -0600732 auto status = provisionable_->generateCertificateRequest(
Seth Moore19acbe92021-06-23 15:15:52 -0700733 false /* testMode */, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo,
David Drysdalec8400772021-03-11 12:35:11 +0000734 &protectedData, &keysToSignMac);
Shawn Willden274bb552020-09-30 22:39:22 -0600735 ASSERT_FALSE(status.isOk());
736 ASSERT_EQ(status.getServiceSpecificError(),
737 BnRemotelyProvisionedComponent::STATUS_TEST_KEY_IN_PRODUCTION_REQUEST);
738}
739
740INSTANTIATE_REM_PROV_AIDL_TEST(CertificateRequestTest);
741
Tri Vo0d6204e2022-09-29 16:15:34 -0700742class CertificateRequestV2Test : public CertificateRequestTestBase {
743 void SetUp() override {
744 CertificateRequestTestBase::SetUp();
Andrew Scull1bcb6022022-12-27 10:43:27 +0000745 ASSERT_FALSE(HasFatalFailure());
Tri Vo0d6204e2022-09-29 16:15:34 -0700746
Andrew Scullf2ae1932023-04-24 19:09:09 +0000747 if (rpcHardwareInfo.versionNumber < VERSION_WITH_CERTIFICATE_REQUEST_V2) {
Tri Vo0d6204e2022-09-29 16:15:34 -0700748 GTEST_SKIP() << "This test case only applies to RKP v3 and above. "
749 << "RKP version discovered: " << rpcHardwareInfo.versionNumber;
750 }
751 }
752};
753
754/**
Tommy Chiufde3ad12023-03-17 05:58:28 +0000755 * Generate an empty certificate request with all possible length of challenge, and decrypt and
756 * verify the structure and content.
Tri Vo0d6204e2022-09-29 16:15:34 -0700757 */
Seth Mooreac1639f2023-04-27 11:14:11 -0700758// @VsrTest = 3.10-015
Tri Vo0d6204e2022-09-29 16:15:34 -0700759TEST_P(CertificateRequestV2Test, EmptyRequest) {
760 bytevec csr;
761
Tommy Chiufde3ad12023-03-17 05:58:28 +0000762 for (auto size = MIN_CHALLENGE_SIZE; size <= MAX_CHALLENGE_SIZE; size++) {
763 SCOPED_TRACE(testing::Message() << "challenge[" << size << "]");
764 auto challenge = randomBytes(size);
765 auto status =
766 provisionable_->generateCertificateRequestV2({} /* keysToSign */, challenge, &csr);
Frederick Mayle9891be02023-11-03 14:39:24 -0700767 ASSERT_TRUE(status.isOk()) << status.getDescription();
Tri Vo0d6204e2022-09-29 16:15:34 -0700768
Alice Wangf112ec92024-10-08 07:30:28 +0000769 auto result = verifyProductionCsr(cppbor::Array(), csr, provisionable_.get(), GetParam(),
770 challenge, isRkpVmInstance_);
Tommy Chiufde3ad12023-03-17 05:58:28 +0000771 ASSERT_TRUE(result) << result.message();
772 }
Tri Vo0d6204e2022-09-29 16:15:34 -0700773}
774
775/**
Tommy Chiufde3ad12023-03-17 05:58:28 +0000776 * Generate a non-empty certificate request with all possible length of challenge. Decrypt, parse
777 * and validate the contents.
Tri Vo0d6204e2022-09-29 16:15:34 -0700778 */
Seth Mooreac1639f2023-04-27 11:14:11 -0700779// @VsrTest = 3.10-015
Tri Vo0d6204e2022-09-29 16:15:34 -0700780TEST_P(CertificateRequestV2Test, NonEmptyRequest) {
781 generateKeys(false /* testMode */, 1 /* numKeys */);
782
783 bytevec csr;
784
Tommy Chiufde3ad12023-03-17 05:58:28 +0000785 for (auto size = MIN_CHALLENGE_SIZE; size <= MAX_CHALLENGE_SIZE; size++) {
786 SCOPED_TRACE(testing::Message() << "challenge[" << size << "]");
787 auto challenge = randomBytes(size);
788 auto status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge, &csr);
Frederick Mayle9891be02023-11-03 14:39:24 -0700789 ASSERT_TRUE(status.isOk()) << status.getDescription();
Tri Vo0d6204e2022-09-29 16:15:34 -0700790
Alice Wangf112ec92024-10-08 07:30:28 +0000791 auto result = verifyProductionCsr(cborKeysToSign_, csr, provisionable_.get(), GetParam(),
792 challenge, isRkpVmInstance_);
Tommy Chiufde3ad12023-03-17 05:58:28 +0000793 ASSERT_TRUE(result) << result.message();
794 }
795}
796
797/**
798 * Generate an empty certificate request with invalid size of challenge
799 */
800TEST_P(CertificateRequestV2Test, EmptyRequestWithInvalidChallengeFail) {
801 bytevec csr;
802
803 auto status = provisionable_->generateCertificateRequestV2(
804 /* keysToSign */ {}, randomBytes(MAX_CHALLENGE_SIZE + 1), &csr);
Frederick Mayle9891be02023-11-03 14:39:24 -0700805 EXPECT_FALSE(status.isOk()) << status.getDescription();
Tommy Chiufde3ad12023-03-17 05:58:28 +0000806 EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_FAILED);
Tri Vo0d6204e2022-09-29 16:15:34 -0700807}
808
809/**
Andrew Scullfb49ad22022-11-09 21:02:48 +0000810 * Generate a non-empty certificate request. Make sure contents are reproducible but allow for the
811 * signature to be different since algorithms including ECDSA P-256 can include a random value.
Tri Vo0d6204e2022-09-29 16:15:34 -0700812 */
Seth Mooreac1639f2023-04-27 11:14:11 -0700813// @VsrTest = 3.10-015
Tri Vo0d6204e2022-09-29 16:15:34 -0700814TEST_P(CertificateRequestV2Test, NonEmptyRequestReproducible) {
815 generateKeys(false /* testMode */, 1 /* numKeys */);
816
817 bytevec csr;
818
819 auto status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr);
Frederick Mayle9891be02023-11-03 14:39:24 -0700820 ASSERT_TRUE(status.isOk()) << status.getDescription();
Tri Vo0d6204e2022-09-29 16:15:34 -0700821
Alice Wangf112ec92024-10-08 07:30:28 +0000822 auto firstCsr = verifyProductionCsr(cborKeysToSign_, csr, provisionable_.get(), GetParam(),
823 challenge_, isRkpVmInstance_);
Andrew Scullfb49ad22022-11-09 21:02:48 +0000824 ASSERT_TRUE(firstCsr) << firstCsr.message();
Tri Vo0d6204e2022-09-29 16:15:34 -0700825
826 status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr);
Frederick Mayle9891be02023-11-03 14:39:24 -0700827 ASSERT_TRUE(status.isOk()) << status.getDescription();
Tri Vo0d6204e2022-09-29 16:15:34 -0700828
Alice Wangf112ec92024-10-08 07:30:28 +0000829 auto secondCsr = verifyProductionCsr(cborKeysToSign_, csr, provisionable_.get(), GetParam(),
830 challenge_, isRkpVmInstance_);
Andrew Scullfb49ad22022-11-09 21:02:48 +0000831 ASSERT_TRUE(secondCsr) << secondCsr.message();
Tri Vo0d6204e2022-09-29 16:15:34 -0700832
Andrew Scullfb49ad22022-11-09 21:02:48 +0000833 ASSERT_EQ(**firstCsr, **secondCsr);
Tri Vo0d6204e2022-09-29 16:15:34 -0700834}
835
836/**
837 * Generate a non-empty certificate request with multiple keys.
838 */
Seth Mooreac1639f2023-04-27 11:14:11 -0700839// @VsrTest = 3.10-015
Tri Vo0d6204e2022-09-29 16:15:34 -0700840TEST_P(CertificateRequestV2Test, NonEmptyRequestMultipleKeys) {
Tri Vo9cab73c2022-10-28 13:40:24 -0700841 generateKeys(false /* testMode */, rpcHardwareInfo.supportedNumKeysInCsr /* numKeys */);
Tri Vo0d6204e2022-09-29 16:15:34 -0700842
843 bytevec csr;
844
845 auto status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr);
Frederick Mayle9891be02023-11-03 14:39:24 -0700846 ASSERT_TRUE(status.isOk()) << status.getDescription();
Tri Vo0d6204e2022-09-29 16:15:34 -0700847
Alice Wangf112ec92024-10-08 07:30:28 +0000848 auto result = verifyProductionCsr(cborKeysToSign_, csr, provisionable_.get(), GetParam(),
849 challenge_, isRkpVmInstance_);
Tri Vo0d6204e2022-09-29 16:15:34 -0700850 ASSERT_TRUE(result) << result.message();
851}
852
853/**
854 * Generate a non-empty certificate request, but with the MAC corrupted on the keypair.
855 */
856TEST_P(CertificateRequestV2Test, NonEmptyRequestCorruptMac) {
857 generateKeys(false /* testMode */, 1 /* numKeys */);
858 auto result = corrupt_maced_key(keysToSign_[0]);
859 ASSERT_TRUE(result) << result.moveMessage();
860 MacedPublicKey keyWithCorruptMac = result.moveValue();
861
862 bytevec csr;
863 auto status =
864 provisionable_->generateCertificateRequestV2({keyWithCorruptMac}, challenge_, &csr);
Frederick Mayle9891be02023-11-03 14:39:24 -0700865 ASSERT_FALSE(status.isOk()) << status.getDescription();
Tri Vo0d6204e2022-09-29 16:15:34 -0700866 EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC);
867}
868
869/**
Andrew Scullf2ae1932023-04-24 19:09:09 +0000870 * Call generateCertificateRequest(). Make sure it's removed.
Tri Vo0d6204e2022-09-29 16:15:34 -0700871 */
Andrew Scullf2ae1932023-04-24 19:09:09 +0000872TEST_P(CertificateRequestV2Test, CertificateRequestV1Removed_prodMode) {
873 bytevec keysToSignMac;
874 DeviceInfo deviceInfo;
875 ProtectedData protectedData;
876 auto status = provisionable_->generateCertificateRequest(
877 false /* testMode */, {} /* keysToSign */, {} /* EEK chain */, challenge_, &deviceInfo,
878 &protectedData, &keysToSignMac);
Frederick Mayle9891be02023-11-03 14:39:24 -0700879 ASSERT_FALSE(status.isOk()) << status.getDescription();
Andrew Scullf2ae1932023-04-24 19:09:09 +0000880 EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_REMOVED);
Tri Vo0d6204e2022-09-29 16:15:34 -0700881}
882
Andrew Scull95b31312023-03-27 19:16:07 +0000883/**
Andrew Scullf2ae1932023-04-24 19:09:09 +0000884 * Call generateCertificateRequest() in test mode. Make sure it's removed.
Andrew Scull95b31312023-03-27 19:16:07 +0000885 */
Andrew Scullf2ae1932023-04-24 19:09:09 +0000886TEST_P(CertificateRequestV2Test, CertificateRequestV1Removed_testMode) {
Andrew Scull95b31312023-03-27 19:16:07 +0000887 bytevec keysToSignMac;
888 DeviceInfo deviceInfo;
889 ProtectedData protectedData;
890 auto status = provisionable_->generateCertificateRequest(
891 true /* testMode */, {} /* keysToSign */, {} /* EEK chain */, challenge_, &deviceInfo,
892 &protectedData, &keysToSignMac);
Frederick Mayle9891be02023-11-03 14:39:24 -0700893 ASSERT_FALSE(status.isOk()) << status.getDescription();
Andrew Scull95b31312023-03-27 19:16:07 +0000894 EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_REMOVED);
895}
896
Tri Voec50ee12023-02-14 16:29:53 -0800897void parse_root_of_trust(const vector<uint8_t>& attestation_cert,
898 vector<uint8_t>* verified_boot_key, VerifiedBoot* verified_boot_state,
899 bool* device_locked, vector<uint8_t>* verified_boot_hash) {
900 X509_Ptr cert(parse_cert_blob(attestation_cert));
901 ASSERT_TRUE(cert.get());
902
903 ASN1_OCTET_STRING* attest_rec = get_attestation_record(cert.get());
904 ASSERT_TRUE(attest_rec);
905
906 auto error = parse_root_of_trust(attest_rec->data, attest_rec->length, verified_boot_key,
907 verified_boot_state, device_locked, verified_boot_hash);
908 ASSERT_EQ(error, ErrorCode::OK);
909}
910
911/**
912 * Generate a CSR and verify DeviceInfo against IDs attested by KeyMint.
913 */
Seth Mooreac1639f2023-04-27 11:14:11 -0700914// @VsrTest = 3.10-015
Tri Voec50ee12023-02-14 16:29:53 -0800915TEST_P(CertificateRequestV2Test, DeviceInfo) {
916 // See if there is a matching IKeyMintDevice for this IRemotelyProvisionedComponent.
917 std::shared_ptr<IKeyMintDevice> keyMint;
918 if (!matching_keymint_device(GetParam(), &keyMint)) {
919 // No matching IKeyMintDevice.
920 GTEST_SKIP() << "Skipping key use test as no matching KeyMint device found";
921 return;
922 }
923 KeyMintHardwareInfo info;
924 ASSERT_TRUE(keyMint->getHardwareInfo(&info).isOk());
925
926 // Get IDs attested by KeyMint.
927 MacedPublicKey macedPubKey;
928 bytevec privateKeyBlob;
929 auto irpcStatus =
930 provisionable_->generateEcdsaP256KeyPair(false, &macedPubKey, &privateKeyBlob);
931 ASSERT_TRUE(irpcStatus.isOk());
932
933 AttestationKey attestKey;
934 attestKey.keyBlob = std::move(privateKeyBlob);
935 attestKey.issuerSubjectName = make_name_from_str("Android Keystore Key");
936
937 // Generate an ECDSA key that is attested by the generated P256 keypair.
938 AuthorizationSet keyDesc = AuthorizationSetBuilder()
939 .Authorization(TAG_NO_AUTH_REQUIRED)
940 .EcdsaSigningKey(EcCurve::P_256)
941 .AttestationChallenge("foo")
942 .AttestationApplicationId("bar")
943 .Digest(Digest::NONE)
944 .SetDefaultValidity();
945 KeyCreationResult creationResult;
946 auto kmStatus = keyMint->generateKey(keyDesc.vector_data(), attestKey, &creationResult);
947 ASSERT_TRUE(kmStatus.isOk());
948
949 vector<KeyCharacteristics> key_characteristics = std::move(creationResult.keyCharacteristics);
950 vector<Certificate> key_cert_chain = std::move(creationResult.certificateChain);
951 // We didn't provision the attestation key.
952 ASSERT_EQ(key_cert_chain.size(), 1);
953
954 // Parse attested patch levels.
955 auto auths = HwEnforcedAuthorizations(key_characteristics);
956
957 auto attestedSystemPatchLevel = auths.GetTagValue(TAG_OS_PATCHLEVEL);
958 auto attestedVendorPatchLevel = auths.GetTagValue(TAG_VENDOR_PATCHLEVEL);
959 auto attestedBootPatchLevel = auths.GetTagValue(TAG_BOOT_PATCHLEVEL);
960
961 ASSERT_TRUE(attestedSystemPatchLevel.has_value());
962 ASSERT_TRUE(attestedVendorPatchLevel.has_value());
963 ASSERT_TRUE(attestedBootPatchLevel.has_value());
964
965 // Parse attested AVB values.
966 vector<uint8_t> key;
967 VerifiedBoot attestedVbState;
968 bool attestedBootloaderState;
969 vector<uint8_t> attestedVbmetaDigest;
970 parse_root_of_trust(key_cert_chain[0].encodedCertificate, &key, &attestedVbState,
971 &attestedBootloaderState, &attestedVbmetaDigest);
972
973 // Get IDs from DeviceInfo.
974 bytevec csr;
975 irpcStatus =
976 provisionable_->generateCertificateRequestV2({} /* keysToSign */, challenge_, &csr);
Frederick Mayle9891be02023-11-03 14:39:24 -0700977 ASSERT_TRUE(irpcStatus.isOk()) << irpcStatus.getDescription();
Tri Voec50ee12023-02-14 16:29:53 -0800978
Alice Wangf112ec92024-10-08 07:30:28 +0000979 auto result =
980 verifyProductionCsr(cppbor::Array(), csr, provisionable_.get(), GetParam(), challenge_);
Tri Voec50ee12023-02-14 16:29:53 -0800981 ASSERT_TRUE(result) << result.message();
982
983 std::unique_ptr<cppbor::Array> csrPayload = std::move(*result);
984 ASSERT_TRUE(csrPayload);
985
986 auto deviceInfo = csrPayload->get(2)->asMap();
987 ASSERT_TRUE(deviceInfo);
988
989 auto vbState = deviceInfo->get("vb_state")->asTstr();
990 auto bootloaderState = deviceInfo->get("bootloader_state")->asTstr();
991 auto vbmetaDigest = deviceInfo->get("vbmeta_digest")->asBstr();
992 auto systemPatchLevel = deviceInfo->get("system_patch_level")->asUint();
993 auto vendorPatchLevel = deviceInfo->get("vendor_patch_level")->asUint();
994 auto bootPatchLevel = deviceInfo->get("boot_patch_level")->asUint();
995 auto securityLevel = deviceInfo->get("security_level")->asTstr();
996
997 ASSERT_TRUE(vbState);
998 ASSERT_TRUE(bootloaderState);
999 ASSERT_TRUE(vbmetaDigest);
1000 ASSERT_TRUE(systemPatchLevel);
1001 ASSERT_TRUE(vendorPatchLevel);
1002 ASSERT_TRUE(bootPatchLevel);
1003 ASSERT_TRUE(securityLevel);
1004
1005 auto kmDeviceName = device_suffix(GetParam());
1006
1007 // Compare DeviceInfo against IDs attested by KeyMint.
1008 ASSERT_TRUE((securityLevel->value() == "tee" && kmDeviceName == "default") ||
1009 (securityLevel->value() == "strongbox" && kmDeviceName == "strongbox"));
1010 ASSERT_TRUE((vbState->value() == "green" && attestedVbState == VerifiedBoot::VERIFIED) ||
1011 (vbState->value() == "yellow" && attestedVbState == VerifiedBoot::SELF_SIGNED) ||
1012 (vbState->value() == "orange" && attestedVbState == VerifiedBoot::UNVERIFIED));
1013 ASSERT_TRUE((bootloaderState->value() == "locked" && attestedBootloaderState) ||
1014 (bootloaderState->value() == "unlocked" && !attestedBootloaderState));
1015 ASSERT_EQ(vbmetaDigest->value(), attestedVbmetaDigest);
1016 ASSERT_EQ(systemPatchLevel->value(), attestedSystemPatchLevel.value());
1017 ASSERT_EQ(vendorPatchLevel->value(), attestedVendorPatchLevel.value());
1018 ASSERT_EQ(bootPatchLevel->value(), attestedBootPatchLevel.value());
1019}
1020
Tri Vo0d6204e2022-09-29 16:15:34 -07001021INSTANTIATE_REM_PROV_AIDL_TEST(CertificateRequestV2Test);
1022
Max Biresa9b3bb92022-11-21 23:02:09 -08001023using VsrRequirementTest = VtsRemotelyProvisionedComponentTests;
1024
1025INSTANTIATE_REM_PROV_AIDL_TEST(VsrRequirementTest);
1026
1027TEST_P(VsrRequirementTest, VsrEnforcementTest) {
1028 RpcHardwareInfo hwInfo;
1029 ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk());
1030 int vsr_api_level = get_vsr_api_level();
1031 if (vsr_api_level < 34) {
1032 GTEST_SKIP() << "Applies only to VSR API level 34 or newer, this device is: "
1033 << vsr_api_level;
1034 }
1035 EXPECT_GE(hwInfo.versionNumber, 3)
1036 << "VSR 14+ requires IRemotelyProvisionedComponent v3 or newer.";
1037}
1038
Shawn Willden274bb552020-09-30 22:39:22 -06001039} // namespace aidl::android::hardware::security::keymint::test