blob: bf40976ec34bd9237ebe091593d1e367b2359623 [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;
Tri Vo0d6204e2022-09-29 16:15:34 -070050constexpr int32_t VERSION_WITHOUT_TEST_MODE = 3;
Seth Moorefc86bf42021-12-09 14:07:04 -080051
Shawn Willden274bb552020-09-30 22:39:22 -060052#define INSTANTIATE_REM_PROV_AIDL_TEST(name) \
Seth Moore6305e232021-07-27 14:20:17 -070053 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name); \
Shawn Willden274bb552020-09-30 22:39:22 -060054 INSTANTIATE_TEST_SUITE_P( \
55 PerInstance, name, \
56 testing::ValuesIn(VtsRemotelyProvisionedComponentTests::build_params()), \
57 ::android::PrintInstanceNameToString)
58
Seth Moorefc86bf42021-12-09 14:07:04 -080059using ::android::sp;
Shawn Willden274bb552020-09-30 22:39:22 -060060using bytevec = std::vector<uint8_t>;
61using testing::MatchesRegex;
62using namespace remote_prov;
63using namespace keymaster;
64
65bytevec string_to_bytevec(const char* s) {
66 const uint8_t* p = reinterpret_cast<const uint8_t*>(s);
67 return bytevec(p, p + strlen(s));
68}
69
David Drysdalee99ed862021-03-15 16:43:06 +000070ErrMsgOr<MacedPublicKey> corrupt_maced_key(const MacedPublicKey& macedPubKey) {
71 auto [coseMac0, _, mac0ParseErr] = cppbor::parse(macedPubKey.macedKey);
72 if (!coseMac0 || coseMac0->asArray()->size() != kCoseMac0EntryCount) {
73 return "COSE Mac0 parse failed";
74 }
75 auto protParams = coseMac0->asArray()->get(kCoseMac0ProtectedParams)->asBstr();
76 auto unprotParams = coseMac0->asArray()->get(kCoseMac0UnprotectedParams)->asMap();
77 auto payload = coseMac0->asArray()->get(kCoseMac0Payload)->asBstr();
78 auto tag = coseMac0->asArray()->get(kCoseMac0Tag)->asBstr();
79 if (!protParams || !unprotParams || !payload || !tag) {
80 return "Invalid COSE_Sign1: missing content";
81 }
82 auto corruptMac0 = cppbor::Array();
83 corruptMac0.add(protParams->clone());
84 corruptMac0.add(unprotParams->clone());
85 corruptMac0.add(payload->clone());
86 vector<uint8_t> tagData = tag->value();
87 tagData[0] ^= 0x08;
88 tagData[tagData.size() - 1] ^= 0x80;
89 corruptMac0.add(cppbor::Bstr(tagData));
90
91 return MacedPublicKey{corruptMac0.encode()};
92}
93
David Drysdalecceca9f2021-03-12 15:49:47 +000094ErrMsgOr<cppbor::Array> corrupt_sig(const cppbor::Array* coseSign1) {
95 if (coseSign1->size() != kCoseSign1EntryCount) {
96 return "Invalid COSE_Sign1, wrong entry count";
97 }
98 const cppbor::Bstr* protectedParams = coseSign1->get(kCoseSign1ProtectedParams)->asBstr();
99 const cppbor::Map* unprotectedParams = coseSign1->get(kCoseSign1UnprotectedParams)->asMap();
100 const cppbor::Bstr* payload = coseSign1->get(kCoseSign1Payload)->asBstr();
101 const cppbor::Bstr* signature = coseSign1->get(kCoseSign1Signature)->asBstr();
102 if (!protectedParams || !unprotectedParams || !payload || !signature) {
103 return "Invalid COSE_Sign1: missing content";
104 }
105
106 auto corruptSig = cppbor::Array();
107 corruptSig.add(protectedParams->clone());
108 corruptSig.add(unprotectedParams->clone());
109 corruptSig.add(payload->clone());
110 vector<uint8_t> sigData = signature->value();
111 sigData[0] ^= 0x08;
112 corruptSig.add(cppbor::Bstr(sigData));
113
114 return std::move(corruptSig);
115}
116
Seth Moore19acbe92021-06-23 15:15:52 -0700117ErrMsgOr<bytevec> corrupt_sig_chain(const bytevec& encodedEekChain, int which) {
118 auto [chain, _, parseErr] = cppbor::parse(encodedEekChain);
David Drysdalecceca9f2021-03-12 15:49:47 +0000119 if (!chain || !chain->asArray()) {
120 return "EekChain parse failed";
121 }
122
123 cppbor::Array* eekChain = chain->asArray();
124 if (which >= eekChain->size()) {
125 return "selected sig out of range";
126 }
127 auto corruptChain = cppbor::Array();
128
129 for (int ii = 0; ii < eekChain->size(); ++ii) {
130 if (ii == which) {
131 auto sig = corrupt_sig(eekChain->get(which)->asArray());
132 if (!sig) {
133 return "Failed to build corrupted signature" + sig.moveMessage();
134 }
135 corruptChain.add(sig.moveValue());
136 } else {
137 corruptChain.add(eekChain->get(ii)->clone());
138 }
139 }
Seth Moore19acbe92021-06-23 15:15:52 -0700140 return corruptChain.encode();
David Drysdalecceca9f2021-03-12 15:49:47 +0000141}
142
David Drysdale4d3c2982021-03-31 18:21:40 +0100143string device_suffix(const string& name) {
144 size_t pos = name.find('/');
145 if (pos == string::npos) {
146 return name;
147 }
148 return name.substr(pos + 1);
149}
150
151bool matching_keymint_device(const string& rp_name, std::shared_ptr<IKeyMintDevice>* keyMint) {
152 string rp_suffix = device_suffix(rp_name);
153
154 vector<string> km_names = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor);
155 for (const string& km_name : km_names) {
156 // If the suffix of the KeyMint instance equals the suffix of the
157 // RemotelyProvisionedComponent instance, assume they match.
158 if (device_suffix(km_name) == rp_suffix && AServiceManager_isDeclared(km_name.c_str())) {
159 ::ndk::SpAIBinder binder(AServiceManager_waitForService(km_name.c_str()));
160 *keyMint = IKeyMintDevice::fromBinder(binder);
161 return true;
162 }
163 }
164 return false;
165}
166
Shawn Willden274bb552020-09-30 22:39:22 -0600167} // namespace
168
169class VtsRemotelyProvisionedComponentTests : public testing::TestWithParam<std::string> {
170 public:
171 virtual void SetUp() override {
172 if (AServiceManager_isDeclared(GetParam().c_str())) {
173 ::ndk::SpAIBinder binder(AServiceManager_waitForService(GetParam().c_str()));
174 provisionable_ = IRemotelyProvisionedComponent::fromBinder(binder);
175 }
176 ASSERT_NE(provisionable_, nullptr);
subrahmanyamanfb213d62022-02-02 23:10:55 +0000177 ASSERT_TRUE(provisionable_->getHardwareInfo(&rpcHardwareInfo).isOk());
Shawn Willden274bb552020-09-30 22:39:22 -0600178 }
179
180 static vector<string> build_params() {
181 auto params = ::android::getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor);
182 return params;
183 }
184
185 protected:
186 std::shared_ptr<IRemotelyProvisionedComponent> provisionable_;
subrahmanyamanfb213d62022-02-02 23:10:55 +0000187 RpcHardwareInfo rpcHardwareInfo;
Shawn Willden274bb552020-09-30 22:39:22 -0600188};
189
Seth Moorefc86bf42021-12-09 14:07:04 -0800190/**
191 * Verify that every implementation reports a different unique id.
192 */
193TEST(NonParameterizedTests, eachRpcHasAUniqueId) {
194 std::set<std::string> uniqueIds;
195 for (auto hal : ::android::getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor)) {
196 ASSERT_TRUE(AServiceManager_isDeclared(hal.c_str()));
197 ::ndk::SpAIBinder binder(AServiceManager_waitForService(hal.c_str()));
198 std::shared_ptr<IRemotelyProvisionedComponent> rpc =
199 IRemotelyProvisionedComponent::fromBinder(binder);
200 ASSERT_NE(rpc, nullptr);
201
202 RpcHardwareInfo hwInfo;
203 ASSERT_TRUE(rpc->getHardwareInfo(&hwInfo).isOk());
204
Tri Vodd12c482022-10-12 22:41:28 +0000205 if (hwInfo.versionNumber >= VERSION_WITH_UNIQUE_ID_SUPPORT) {
Seth Moorefc86bf42021-12-09 14:07:04 -0800206 ASSERT_TRUE(hwInfo.uniqueId);
207 auto [_, wasInserted] = uniqueIds.insert(*hwInfo.uniqueId);
208 EXPECT_TRUE(wasInserted);
209 } else {
210 ASSERT_FALSE(hwInfo.uniqueId);
211 }
212 }
213}
214
215using GetHardwareInfoTests = VtsRemotelyProvisionedComponentTests;
216
217INSTANTIATE_REM_PROV_AIDL_TEST(GetHardwareInfoTests);
218
219/**
220 * Verify that a valid curve is reported by the implementation.
221 */
222TEST_P(GetHardwareInfoTests, supportsValidCurve) {
223 RpcHardwareInfo hwInfo;
224 ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk());
225
226 const std::set<int> validCurves = {RpcHardwareInfo::CURVE_P256, RpcHardwareInfo::CURVE_25519};
Hasini Gunasinghe666b2712023-01-05 21:35:51 +0000227 // First check for the implementations that supports only IRPC V3+.
228 if (rpcHardwareInfo.versionNumber >= VERSION_WITHOUT_TEST_MODE) {
229 bytevec keysToSignMac;
230 DeviceInfo deviceInfo;
231 ProtectedData protectedData;
232 auto status = provisionable_->generateCertificateRequest(false, {}, {}, {}, &deviceInfo,
233 &protectedData, &keysToSignMac);
234 if (!status.isOk() &&
235 (status.getServiceSpecificError() == BnRemotelyProvisionedComponent::STATUS_REMOVED)) {
236 ASSERT_EQ(hwInfo.supportedEekCurve, RpcHardwareInfo::CURVE_NONE)
237 << "Invalid curve: " << hwInfo.supportedEekCurve;
238 return;
239 }
240 }
Seth Moorefc86bf42021-12-09 14:07:04 -0800241 ASSERT_EQ(validCurves.count(hwInfo.supportedEekCurve), 1)
242 << "Invalid curve: " << hwInfo.supportedEekCurve;
243}
244
245/**
246 * Verify that the unique id is within the length limits as described in RpcHardwareInfo.aidl.
247 */
248TEST_P(GetHardwareInfoTests, uniqueId) {
Tri Vodd12c482022-10-12 22:41:28 +0000249 if (rpcHardwareInfo.versionNumber < VERSION_WITH_UNIQUE_ID_SUPPORT) {
Seth Moorefc86bf42021-12-09 14:07:04 -0800250 return;
251 }
252
253 RpcHardwareInfo hwInfo;
254 ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk());
255 ASSERT_TRUE(hwInfo.uniqueId);
256 EXPECT_GE(hwInfo.uniqueId->size(), 1);
257 EXPECT_LE(hwInfo.uniqueId->size(), 32);
258}
259
Tri Vo9cab73c2022-10-28 13:40:24 -0700260/**
261 * Verify implementation supports at least MIN_SUPPORTED_NUM_KEYS_IN_CSR keys in a CSR.
262 */
263TEST_P(GetHardwareInfoTests, supportedNumKeysInCsr) {
264 if (rpcHardwareInfo.versionNumber < VERSION_WITHOUT_TEST_MODE) {
265 return;
266 }
267
268 RpcHardwareInfo hwInfo;
269 ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk());
270 ASSERT_GE(hwInfo.supportedNumKeysInCsr, RpcHardwareInfo::MIN_SUPPORTED_NUM_KEYS_IN_CSR);
271}
272
Shawn Willden274bb552020-09-30 22:39:22 -0600273using GenerateKeyTests = VtsRemotelyProvisionedComponentTests;
274
275INSTANTIATE_REM_PROV_AIDL_TEST(GenerateKeyTests);
276
277/**
David Drysdalef0d516d2021-03-22 07:51:43 +0000278 * Generate and validate a production-mode key. MAC tag can't be verified, but
279 * the private key blob should be usable in KeyMint operations.
Shawn Willden274bb552020-09-30 22:39:22 -0600280 */
Max Bires126869a2021-02-21 18:32:59 -0800281TEST_P(GenerateKeyTests, generateEcdsaP256Key_prodMode) {
Shawn Willden274bb552020-09-30 22:39:22 -0600282 MacedPublicKey macedPubKey;
283 bytevec privateKeyBlob;
284 bool testMode = false;
285 auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob);
286 ASSERT_TRUE(status.isOk());
David Drysdalef0d516d2021-03-22 07:51:43 +0000287 vector<uint8_t> coseKeyData;
Tri Vob0b8acc2022-11-30 16:22:23 -0800288 check_maced_pubkey(macedPubKey, testMode, &coseKeyData);
David Drysdale4d3c2982021-03-31 18:21:40 +0100289}
290
291/**
292 * Generate and validate a production-mode key, then use it as a KeyMint attestation key.
293 */
294TEST_P(GenerateKeyTests, generateAndUseEcdsaP256Key_prodMode) {
295 // See if there is a matching IKeyMintDevice for this IRemotelyProvisionedComponent.
296 std::shared_ptr<IKeyMintDevice> keyMint;
297 if (!matching_keymint_device(GetParam(), &keyMint)) {
298 // No matching IKeyMintDevice.
299 GTEST_SKIP() << "Skipping key use test as no matching KeyMint device found";
300 return;
301 }
302 KeyMintHardwareInfo info;
303 ASSERT_TRUE(keyMint->getHardwareInfo(&info).isOk());
304
305 MacedPublicKey macedPubKey;
306 bytevec privateKeyBlob;
307 bool testMode = false;
308 auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob);
309 ASSERT_TRUE(status.isOk());
310 vector<uint8_t> coseKeyData;
Tri Vob0b8acc2022-11-30 16:22:23 -0800311 check_maced_pubkey(macedPubKey, testMode, &coseKeyData);
David Drysdale4d3c2982021-03-31 18:21:40 +0100312
David Drysdalef0d516d2021-03-22 07:51:43 +0000313 AttestationKey attestKey;
314 attestKey.keyBlob = std::move(privateKeyBlob);
315 attestKey.issuerSubjectName = make_name_from_str("Android Keystore Key");
Shawn Willden274bb552020-09-30 22:39:22 -0600316
David Drysdalef0d516d2021-03-22 07:51:43 +0000317 // Generate an ECDSA key that is attested by the generated P256 keypair.
318 AuthorizationSet keyDesc = AuthorizationSetBuilder()
319 .Authorization(TAG_NO_AUTH_REQUIRED)
David Drysdale915ce252021-10-14 15:17:36 +0100320 .EcdsaSigningKey(EcCurve::P_256)
David Drysdalef0d516d2021-03-22 07:51:43 +0000321 .AttestationChallenge("foo")
322 .AttestationApplicationId("bar")
323 .Digest(Digest::NONE)
324 .SetDefaultValidity();
325 KeyCreationResult creationResult;
326 auto result = keyMint->generateKey(keyDesc.vector_data(), attestKey, &creationResult);
327 ASSERT_TRUE(result.isOk());
328 vector<uint8_t> attested_key_blob = std::move(creationResult.keyBlob);
329 vector<KeyCharacteristics> attested_key_characteristics =
330 std::move(creationResult.keyCharacteristics);
331 vector<Certificate> attested_key_cert_chain = std::move(creationResult.certificateChain);
332 EXPECT_EQ(attested_key_cert_chain.size(), 1);
333
David Drysdale7dff4fc2021-12-10 10:10:52 +0000334 int32_t aidl_version = 0;
335 ASSERT_TRUE(keyMint->getInterfaceVersion(&aidl_version).isOk());
David Drysdalef0d516d2021-03-22 07:51:43 +0000336 AuthorizationSet hw_enforced = HwEnforcedAuthorizations(attested_key_characteristics);
337 AuthorizationSet sw_enforced = SwEnforcedAuthorizations(attested_key_characteristics);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000338 EXPECT_TRUE(verify_attestation_record(aidl_version, "foo", "bar", sw_enforced, hw_enforced,
David Drysdalef0d516d2021-03-22 07:51:43 +0000339 info.securityLevel,
340 attested_key_cert_chain[0].encodedCertificate));
341
342 // Attestation by itself is not valid (last entry is not self-signed).
343 EXPECT_FALSE(ChainSignaturesAreValid(attested_key_cert_chain));
344
345 // The signature over the attested key should correspond to the P256 public key.
346 X509_Ptr key_cert(parse_cert_blob(attested_key_cert_chain[0].encodedCertificate));
347 ASSERT_TRUE(key_cert.get());
348 EVP_PKEY_Ptr signing_pubkey;
349 p256_pub_key(coseKeyData, &signing_pubkey);
350 ASSERT_TRUE(signing_pubkey.get());
351
352 ASSERT_TRUE(X509_verify(key_cert.get(), signing_pubkey.get()))
353 << "Verification of attested certificate failed "
354 << "OpenSSL error string: " << ERR_error_string(ERR_get_error(), NULL);
Shawn Willden274bb552020-09-30 22:39:22 -0600355}
356
357/**
358 * Generate and validate a test-mode key.
359 */
Max Bires126869a2021-02-21 18:32:59 -0800360TEST_P(GenerateKeyTests, generateEcdsaP256Key_testMode) {
Shawn Willden274bb552020-09-30 22:39:22 -0600361 MacedPublicKey macedPubKey;
362 bytevec privateKeyBlob;
363 bool testMode = true;
364 auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &macedPubKey, &privateKeyBlob);
365 ASSERT_TRUE(status.isOk());
Tri Vob0b8acc2022-11-30 16:22:23 -0800366 check_maced_pubkey(macedPubKey, testMode, nullptr);
Shawn Willden274bb552020-09-30 22:39:22 -0600367}
368
Tri Vo0d6204e2022-09-29 16:15:34 -0700369class CertificateRequestTestBase : public VtsRemotelyProvisionedComponentTests {
Shawn Willden274bb552020-09-30 22:39:22 -0600370 protected:
Tri Vo0d6204e2022-09-29 16:15:34 -0700371 CertificateRequestTestBase()
372 : eekId_(string_to_bytevec("eekid")), challenge_(randomBytes(64)) {}
David Drysdalecceca9f2021-03-12 15:49:47 +0000373
Seth Moore19acbe92021-06-23 15:15:52 -0700374 void generateTestEekChain(size_t eekLength) {
subrahmanyamanfb213d62022-02-02 23:10:55 +0000375 auto chain = generateEekChain(rpcHardwareInfo.supportedEekCurve, eekLength, eekId_);
David Drysdale08696a72022-03-10 10:43:25 +0000376 ASSERT_TRUE(chain) << chain.message();
Seth Moore19acbe92021-06-23 15:15:52 -0700377 if (chain) testEekChain_ = chain.moveValue();
378 testEekLength_ = eekLength;
Shawn Willden274bb552020-09-30 22:39:22 -0600379 }
380
381 void generateKeys(bool testMode, size_t numKeys) {
382 keysToSign_ = std::vector<MacedPublicKey>(numKeys);
383 cborKeysToSign_ = cppbor::Array();
384
385 for (auto& key : keysToSign_) {
386 bytevec privateKeyBlob;
387 auto status = provisionable_->generateEcdsaP256KeyPair(testMode, &key, &privateKeyBlob);
388 ASSERT_TRUE(status.isOk()) << status.getMessage();
389
David Drysdalec8400772021-03-11 12:35:11 +0000390 vector<uint8_t> payload_value;
Tri Vob0b8acc2022-11-30 16:22:23 -0800391 check_maced_pubkey(key, testMode, &payload_value);
David Drysdalec8400772021-03-11 12:35:11 +0000392 cborKeysToSign_.add(cppbor::EncodedItem(payload_value));
Shawn Willden274bb552020-09-30 22:39:22 -0600393 }
394 }
395
396 bytevec eekId_;
Seth Moore19acbe92021-06-23 15:15:52 -0700397 size_t testEekLength_;
398 EekChain testEekChain_;
David Drysdalec8400772021-03-11 12:35:11 +0000399 bytevec challenge_;
Shawn Willden274bb552020-09-30 22:39:22 -0600400 std::vector<MacedPublicKey> keysToSign_;
401 cppbor::Array cborKeysToSign_;
402};
403
Tri Vo0d6204e2022-09-29 16:15:34 -0700404class CertificateRequestTest : public CertificateRequestTestBase {
405 protected:
406 void SetUp() override {
407 CertificateRequestTestBase::SetUp();
Andrew Scull1bcb6022022-12-27 10:43:27 +0000408 ASSERT_FALSE(HasFatalFailure());
Tri Vo0d6204e2022-09-29 16:15:34 -0700409
410 if (rpcHardwareInfo.versionNumber >= VERSION_WITHOUT_TEST_MODE) {
Tri Vob0b8acc2022-11-30 16:22:23 -0800411 bytevec keysToSignMac;
412 DeviceInfo deviceInfo;
413 ProtectedData protectedData;
414 auto status = provisionable_->generateCertificateRequest(
415 false, {}, {}, {}, &deviceInfo, &protectedData, &keysToSignMac);
416 if (!status.isOk() && (status.getServiceSpecificError() ==
417 BnRemotelyProvisionedComponent::STATUS_REMOVED)) {
418 GTEST_SKIP() << "This test case applies to RKP v3+ only if "
419 << "generateCertificateRequest() is implemented.";
420 }
Tri Vo0d6204e2022-09-29 16:15:34 -0700421 }
422 }
423};
424
Shawn Willden274bb552020-09-30 22:39:22 -0600425/**
426 * Generate an empty certificate request in test mode, and decrypt and verify the structure and
427 * content.
428 */
Max Bires126869a2021-02-21 18:32:59 -0800429TEST_P(CertificateRequestTest, EmptyRequest_testMode) {
Shawn Willden274bb552020-09-30 22:39:22 -0600430 bool testMode = true;
David Drysdalecceca9f2021-03-12 15:49:47 +0000431 for (size_t eekLength : {2, 3, 7}) {
432 SCOPED_TRACE(testing::Message() << "EEK of length " << eekLength);
Seth Moore19acbe92021-06-23 15:15:52 -0700433 generateTestEekChain(eekLength);
Shawn Willden274bb552020-09-30 22:39:22 -0600434
David Drysdalecceca9f2021-03-12 15:49:47 +0000435 bytevec keysToSignMac;
436 DeviceInfo deviceInfo;
437 ProtectedData protectedData;
438 auto status = provisionable_->generateCertificateRequest(
Seth Moore19acbe92021-06-23 15:15:52 -0700439 testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo,
David Drysdalecceca9f2021-03-12 15:49:47 +0000440 &protectedData, &keysToSignMac);
441 ASSERT_TRUE(status.isOk()) << status.getMessage();
442
Seth Moore2fc6f832022-09-13 16:10:11 -0700443 auto result = verifyProductionProtectedData(
444 deviceInfo, cppbor::Array(), keysToSignMac, protectedData, testEekChain_, eekId_,
445 rpcHardwareInfo.supportedEekCurve, provisionable_.get(), challenge_);
446 ASSERT_TRUE(result) << result.message();
David Drysdalecceca9f2021-03-12 15:49:47 +0000447 }
Shawn Willden274bb552020-09-30 22:39:22 -0600448}
449
450/**
Seth Moore42c11332021-07-02 15:38:17 -0700451 * Ensure that test mode outputs a unique BCC root key every time we request a
452 * certificate request. Else, it's possible that the test mode API could be used
453 * to fingerprint devices. Only the GEEK should be allowed to decrypt the same
454 * device public key multiple times.
455 */
456TEST_P(CertificateRequestTest, NewKeyPerCallInTestMode) {
457 constexpr bool testMode = true;
Seth Moore42c11332021-07-02 15:38:17 -0700458
459 bytevec keysToSignMac;
460 DeviceInfo deviceInfo;
461 ProtectedData protectedData;
subrahmanyamanfb213d62022-02-02 23:10:55 +0000462 generateTestEekChain(3);
Seth Moore42c11332021-07-02 15:38:17 -0700463 auto status = provisionable_->generateCertificateRequest(
Seth Moore19acbe92021-06-23 15:15:52 -0700464 testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo,
465 &protectedData, &keysToSignMac);
Seth Moore42c11332021-07-02 15:38:17 -0700466 ASSERT_TRUE(status.isOk()) << status.getMessage();
467
Seth Moore2fc6f832022-09-13 16:10:11 -0700468 auto firstBcc = verifyProductionProtectedData(
469 deviceInfo, /*keysToSign=*/cppbor::Array(), keysToSignMac, protectedData, testEekChain_,
470 eekId_, rpcHardwareInfo.supportedEekCurve, provisionable_.get(), challenge_);
471 ASSERT_TRUE(firstBcc) << firstBcc.message();
Seth Moore42c11332021-07-02 15:38:17 -0700472
Seth Moore19acbe92021-06-23 15:15:52 -0700473 status = provisionable_->generateCertificateRequest(
474 testMode, {} /* keysToSign */, testEekChain_.chain, challenge_, &deviceInfo,
475 &protectedData, &keysToSignMac);
Seth Moore42c11332021-07-02 15:38:17 -0700476 ASSERT_TRUE(status.isOk()) << status.getMessage();
477
Seth Moore2fc6f832022-09-13 16:10:11 -0700478 auto secondBcc = verifyProductionProtectedData(
479 deviceInfo, /*keysToSign=*/cppbor::Array(), keysToSignMac, protectedData, testEekChain_,
480 eekId_, rpcHardwareInfo.supportedEekCurve, provisionable_.get(), challenge_);
481 ASSERT_TRUE(secondBcc) << secondBcc.message();
Seth Moore42c11332021-07-02 15:38:17 -0700482
483 // Verify that none of the keys in the first BCC are repeated in the second one.
Seth Moore2fc6f832022-09-13 16:10:11 -0700484 for (const auto& i : *firstBcc) {
485 for (auto& j : *secondBcc) {
Seth Moore42c11332021-07-02 15:38:17 -0700486 ASSERT_THAT(i.pubKey, testing::Not(testing::ElementsAreArray(j.pubKey)))
487 << "Found a repeated pubkey in two generateCertificateRequest test mode calls";
488 }
489 }
490}
491
492/**
Seth Moore19acbe92021-06-23 15:15:52 -0700493 * Generate an empty certificate request in prod mode. This test must be run explicitly, and
494 * is not run by default. Not all devices are GMS devices, and therefore they do not all
495 * trust the Google EEK root.
Shawn Willden274bb552020-09-30 22:39:22 -0600496 */
Seth Moore19acbe92021-06-23 15:15:52 -0700497TEST_P(CertificateRequestTest, DISABLED_EmptyRequest_prodMode) {
Shawn Willden274bb552020-09-30 22:39:22 -0600498 bool testMode = false;
David Drysdalecceca9f2021-03-12 15:49:47 +0000499
Seth Moore19acbe92021-06-23 15:15:52 -0700500 bytevec keysToSignMac;
501 DeviceInfo deviceInfo;
502 ProtectedData protectedData;
503 auto status = provisionable_->generateCertificateRequest(
subrahmanyamanfb213d62022-02-02 23:10:55 +0000504 testMode, {} /* keysToSign */, getProdEekChain(rpcHardwareInfo.supportedEekCurve),
505 challenge_, &deviceInfo, &protectedData, &keysToSignMac);
Seth Moore19acbe92021-06-23 15:15:52 -0700506 EXPECT_TRUE(status.isOk());
Shawn Willden274bb552020-09-30 22:39:22 -0600507}
508
509/**
510 * Generate a non-empty certificate request in test mode. Decrypt, parse and validate the contents.
511 */
Max Bires126869a2021-02-21 18:32:59 -0800512TEST_P(CertificateRequestTest, NonEmptyRequest_testMode) {
Shawn Willden274bb552020-09-30 22:39:22 -0600513 bool testMode = true;
514 generateKeys(testMode, 4 /* numKeys */);
515
David Drysdalecceca9f2021-03-12 15:49:47 +0000516 for (size_t eekLength : {2, 3, 7}) {
517 SCOPED_TRACE(testing::Message() << "EEK of length " << eekLength);
Seth Moore19acbe92021-06-23 15:15:52 -0700518 generateTestEekChain(eekLength);
Shawn Willden274bb552020-09-30 22:39:22 -0600519
David Drysdalecceca9f2021-03-12 15:49:47 +0000520 bytevec keysToSignMac;
521 DeviceInfo deviceInfo;
522 ProtectedData protectedData;
523 auto status = provisionable_->generateCertificateRequest(
Seth Moore19acbe92021-06-23 15:15:52 -0700524 testMode, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo, &protectedData,
David Drysdalecceca9f2021-03-12 15:49:47 +0000525 &keysToSignMac);
526 ASSERT_TRUE(status.isOk()) << status.getMessage();
527
Seth Moore2fc6f832022-09-13 16:10:11 -0700528 auto result = verifyProductionProtectedData(
529 deviceInfo, cborKeysToSign_, keysToSignMac, protectedData, testEekChain_, eekId_,
530 rpcHardwareInfo.supportedEekCurve, provisionable_.get(), challenge_);
531 ASSERT_TRUE(result) << result.message();
David Drysdalecceca9f2021-03-12 15:49:47 +0000532 }
Shawn Willden274bb552020-09-30 22:39:22 -0600533}
534
535/**
Seth Moore19acbe92021-06-23 15:15:52 -0700536 * Generate a non-empty certificate request in prod mode. This test must be run explicitly, and
537 * is not run by default. Not all devices are GMS devices, and therefore they do not all
538 * trust the Google EEK root.
Shawn Willden274bb552020-09-30 22:39:22 -0600539 */
Seth Moore19acbe92021-06-23 15:15:52 -0700540TEST_P(CertificateRequestTest, DISABLED_NonEmptyRequest_prodMode) {
Shawn Willden274bb552020-09-30 22:39:22 -0600541 bool testMode = false;
542 generateKeys(testMode, 4 /* numKeys */);
543
Seth Moore19acbe92021-06-23 15:15:52 -0700544 bytevec keysToSignMac;
545 DeviceInfo deviceInfo;
546 ProtectedData protectedData;
547 auto status = provisionable_->generateCertificateRequest(
subrahmanyamanfb213d62022-02-02 23:10:55 +0000548 testMode, keysToSign_, getProdEekChain(rpcHardwareInfo.supportedEekCurve), challenge_,
549 &deviceInfo, &protectedData, &keysToSignMac);
Seth Moore19acbe92021-06-23 15:15:52 -0700550 EXPECT_TRUE(status.isOk());
David Drysdalecceca9f2021-03-12 15:49:47 +0000551}
552
553/**
David Drysdalee99ed862021-03-15 16:43:06 +0000554 * Generate a non-empty certificate request in test mode, but with the MAC corrupted on the keypair.
555 */
556TEST_P(CertificateRequestTest, NonEmptyRequestCorruptMac_testMode) {
557 bool testMode = true;
558 generateKeys(testMode, 1 /* numKeys */);
David Drysdale08696a72022-03-10 10:43:25 +0000559 auto result = corrupt_maced_key(keysToSign_[0]);
560 ASSERT_TRUE(result) << result.moveMessage();
561 MacedPublicKey keyWithCorruptMac = result.moveValue();
David Drysdalee99ed862021-03-15 16:43:06 +0000562
563 bytevec keysToSignMac;
564 DeviceInfo deviceInfo;
565 ProtectedData protectedData;
subrahmanyamanfb213d62022-02-02 23:10:55 +0000566 generateTestEekChain(3);
David Drysdalee99ed862021-03-15 16:43:06 +0000567 auto status = provisionable_->generateCertificateRequest(
Seth Moore19acbe92021-06-23 15:15:52 -0700568 testMode, {keyWithCorruptMac}, testEekChain_.chain, challenge_, &deviceInfo,
569 &protectedData, &keysToSignMac);
David Drysdalee99ed862021-03-15 16:43:06 +0000570 ASSERT_FALSE(status.isOk()) << status.getMessage();
571 EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC);
572}
573
574/**
575 * Generate a non-empty certificate request in prod mode, but with the MAC corrupted on the keypair.
576 */
577TEST_P(CertificateRequestTest, NonEmptyRequestCorruptMac_prodMode) {
Seth Moore19acbe92021-06-23 15:15:52 -0700578 bool testMode = false;
David Drysdalee99ed862021-03-15 16:43:06 +0000579 generateKeys(testMode, 1 /* numKeys */);
David Drysdale08696a72022-03-10 10:43:25 +0000580 auto result = corrupt_maced_key(keysToSign_[0]);
581 ASSERT_TRUE(result) << result.moveMessage();
582 MacedPublicKey keyWithCorruptMac = result.moveValue();
David Drysdalee99ed862021-03-15 16:43:06 +0000583
584 bytevec keysToSignMac;
585 DeviceInfo deviceInfo;
586 ProtectedData protectedData;
587 auto status = provisionable_->generateCertificateRequest(
subrahmanyamanfb213d62022-02-02 23:10:55 +0000588 testMode, {keyWithCorruptMac}, getProdEekChain(rpcHardwareInfo.supportedEekCurve),
589 challenge_, &deviceInfo, &protectedData, &keysToSignMac);
David Drysdalee99ed862021-03-15 16:43:06 +0000590 ASSERT_FALSE(status.isOk()) << status.getMessage();
Seth Moore19acbe92021-06-23 15:15:52 -0700591 EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC);
David Drysdalee99ed862021-03-15 16:43:06 +0000592}
593
594/**
David Drysdalecceca9f2021-03-12 15:49:47 +0000595 * Generate a non-empty certificate request in prod mode that has a corrupt EEK chain.
596 * Confirm that the request is rejected.
David Drysdalecceca9f2021-03-12 15:49:47 +0000597 */
598TEST_P(CertificateRequestTest, NonEmptyCorruptEekRequest_prodMode) {
599 bool testMode = false;
600 generateKeys(testMode, 4 /* numKeys */);
601
subrahmanyamanfb213d62022-02-02 23:10:55 +0000602 auto prodEekChain = getProdEekChain(rpcHardwareInfo.supportedEekCurve);
Seth Moore19acbe92021-06-23 15:15:52 -0700603 auto [parsedChain, _, parseErr] = cppbor::parse(prodEekChain);
604 ASSERT_NE(parsedChain, nullptr) << parseErr;
605 ASSERT_NE(parsedChain->asArray(), nullptr);
606
607 for (int ii = 0; ii < parsedChain->asArray()->size(); ++ii) {
608 auto chain = corrupt_sig_chain(prodEekChain, ii);
David Drysdalecceca9f2021-03-12 15:49:47 +0000609 ASSERT_TRUE(chain) << chain.message();
David Drysdalecceca9f2021-03-12 15:49:47 +0000610
611 bytevec keysToSignMac;
612 DeviceInfo deviceInfo;
613 ProtectedData protectedData;
Seth Moore19acbe92021-06-23 15:15:52 -0700614 auto status = provisionable_->generateCertificateRequest(testMode, keysToSign_, *chain,
615 challenge_, &deviceInfo,
616 &protectedData, &keysToSignMac);
David Drysdalecceca9f2021-03-12 15:49:47 +0000617 ASSERT_FALSE(status.isOk());
618 ASSERT_EQ(status.getServiceSpecificError(),
619 BnRemotelyProvisionedComponent::STATUS_INVALID_EEK);
620 }
621}
622
623/**
624 * Generate a non-empty certificate request in prod mode that has an incomplete EEK chain.
625 * Confirm that the request is rejected.
David Drysdalecceca9f2021-03-12 15:49:47 +0000626 */
627TEST_P(CertificateRequestTest, NonEmptyIncompleteEekRequest_prodMode) {
628 bool testMode = false;
629 generateKeys(testMode, 4 /* numKeys */);
630
631 // Build an EEK chain that omits the first self-signed cert.
632 auto truncatedChain = cppbor::Array();
subrahmanyamanfb213d62022-02-02 23:10:55 +0000633 auto [chain, _, parseErr] = cppbor::parse(getProdEekChain(rpcHardwareInfo.supportedEekCurve));
David Drysdalecceca9f2021-03-12 15:49:47 +0000634 ASSERT_TRUE(chain);
635 auto eekChain = chain->asArray();
636 ASSERT_NE(eekChain, nullptr);
637 for (size_t ii = 1; ii < eekChain->size(); ii++) {
638 truncatedChain.add(eekChain->get(ii)->clone());
639 }
640
Shawn Willden274bb552020-09-30 22:39:22 -0600641 bytevec keysToSignMac;
Max Biresfdbb9042021-03-23 12:43:38 -0700642 DeviceInfo deviceInfo;
Shawn Willden274bb552020-09-30 22:39:22 -0600643 ProtectedData protectedData;
David Drysdalecceca9f2021-03-12 15:49:47 +0000644 auto status = provisionable_->generateCertificateRequest(
645 testMode, keysToSign_, truncatedChain.encode(), challenge_, &deviceInfo, &protectedData,
646 &keysToSignMac);
Shawn Willden274bb552020-09-30 22:39:22 -0600647 ASSERT_FALSE(status.isOk());
648 ASSERT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_EEK);
649}
650
651/**
652 * Generate a non-empty certificate request in test mode, with prod keys. Must fail with
653 * STATUS_PRODUCTION_KEY_IN_TEST_REQUEST.
654 */
Max Bires126869a2021-02-21 18:32:59 -0800655TEST_P(CertificateRequestTest, NonEmptyRequest_prodKeyInTestCert) {
Shawn Willden274bb552020-09-30 22:39:22 -0600656 generateKeys(false /* testMode */, 2 /* numKeys */);
657
658 bytevec keysToSignMac;
Max Biresfdbb9042021-03-23 12:43:38 -0700659 DeviceInfo deviceInfo;
Shawn Willden274bb552020-09-30 22:39:22 -0600660 ProtectedData protectedData;
subrahmanyamanfb213d62022-02-02 23:10:55 +0000661 generateTestEekChain(3);
Max Biresfdbb9042021-03-23 12:43:38 -0700662 auto status = provisionable_->generateCertificateRequest(
Seth Moore19acbe92021-06-23 15:15:52 -0700663 true /* testMode */, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo,
Max Biresfdbb9042021-03-23 12:43:38 -0700664 &protectedData, &keysToSignMac);
Shawn Willden274bb552020-09-30 22:39:22 -0600665 ASSERT_FALSE(status.isOk());
666 ASSERT_EQ(status.getServiceSpecificError(),
667 BnRemotelyProvisionedComponent::STATUS_PRODUCTION_KEY_IN_TEST_REQUEST);
668}
669
670/**
671 * Generate a non-empty certificate request in prod mode, with test keys. Must fail with
672 * STATUS_TEST_KEY_IN_PRODUCTION_REQUEST.
673 */
Max Bires126869a2021-02-21 18:32:59 -0800674TEST_P(CertificateRequestTest, NonEmptyRequest_testKeyInProdCert) {
Shawn Willden274bb552020-09-30 22:39:22 -0600675 generateKeys(true /* testMode */, 2 /* numKeys */);
676
677 bytevec keysToSignMac;
Max Biresfdbb9042021-03-23 12:43:38 -0700678 DeviceInfo deviceInfo;
Shawn Willden274bb552020-09-30 22:39:22 -0600679 ProtectedData protectedData;
subrahmanyamanfb213d62022-02-02 23:10:55 +0000680 generateTestEekChain(3);
Shawn Willden274bb552020-09-30 22:39:22 -0600681 auto status = provisionable_->generateCertificateRequest(
Seth Moore19acbe92021-06-23 15:15:52 -0700682 false /* testMode */, keysToSign_, testEekChain_.chain, challenge_, &deviceInfo,
David Drysdalec8400772021-03-11 12:35:11 +0000683 &protectedData, &keysToSignMac);
Shawn Willden274bb552020-09-30 22:39:22 -0600684 ASSERT_FALSE(status.isOk());
685 ASSERT_EQ(status.getServiceSpecificError(),
686 BnRemotelyProvisionedComponent::STATUS_TEST_KEY_IN_PRODUCTION_REQUEST);
687}
688
689INSTANTIATE_REM_PROV_AIDL_TEST(CertificateRequestTest);
690
Tri Vo0d6204e2022-09-29 16:15:34 -0700691class CertificateRequestV2Test : public CertificateRequestTestBase {
692 void SetUp() override {
693 CertificateRequestTestBase::SetUp();
Andrew Scull1bcb6022022-12-27 10:43:27 +0000694 ASSERT_FALSE(HasFatalFailure());
Tri Vo0d6204e2022-09-29 16:15:34 -0700695
696 if (rpcHardwareInfo.versionNumber < VERSION_WITHOUT_TEST_MODE) {
697 GTEST_SKIP() << "This test case only applies to RKP v3 and above. "
698 << "RKP version discovered: " << rpcHardwareInfo.versionNumber;
699 }
700 }
701};
702
703/**
704 * Generate an empty certificate request, and decrypt and verify the structure and content.
705 */
706TEST_P(CertificateRequestV2Test, EmptyRequest) {
707 bytevec csr;
708
709 auto status =
710 provisionable_->generateCertificateRequestV2({} /* keysToSign */, challenge_, &csr);
711 ASSERT_TRUE(status.isOk()) << status.getMessage();
712
713 auto result = verifyProductionCsr(cppbor::Array(), csr, provisionable_.get(), challenge_);
714 ASSERT_TRUE(result) << result.message();
715}
716
717/**
718 * Generate a non-empty certificate request. Decrypt, parse and validate the contents.
719 */
720TEST_P(CertificateRequestV2Test, NonEmptyRequest) {
721 generateKeys(false /* testMode */, 1 /* numKeys */);
722
723 bytevec csr;
724
725 auto status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr);
726 ASSERT_TRUE(status.isOk()) << status.getMessage();
727
728 auto result = verifyProductionCsr(cborKeysToSign_, csr, provisionable_.get(), challenge_);
729 ASSERT_TRUE(result) << result.message();
730}
731
732/**
Andrew Scullfb49ad22022-11-09 21:02:48 +0000733 * Generate a non-empty certificate request. Make sure contents are reproducible but allow for the
734 * signature to be different since algorithms including ECDSA P-256 can include a random value.
Tri Vo0d6204e2022-09-29 16:15:34 -0700735 */
736TEST_P(CertificateRequestV2Test, NonEmptyRequestReproducible) {
737 generateKeys(false /* testMode */, 1 /* numKeys */);
738
739 bytevec csr;
740
741 auto status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr);
742 ASSERT_TRUE(status.isOk()) << status.getMessage();
743
Andrew Scullfb49ad22022-11-09 21:02:48 +0000744 auto firstCsr = verifyProductionCsr(cborKeysToSign_, csr, provisionable_.get(), challenge_);
745 ASSERT_TRUE(firstCsr) << firstCsr.message();
Tri Vo0d6204e2022-09-29 16:15:34 -0700746
747 status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr);
748 ASSERT_TRUE(status.isOk()) << status.getMessage();
749
Andrew Scullfb49ad22022-11-09 21:02:48 +0000750 auto secondCsr = verifyProductionCsr(cborKeysToSign_, csr, provisionable_.get(), challenge_);
751 ASSERT_TRUE(secondCsr) << secondCsr.message();
Tri Vo0d6204e2022-09-29 16:15:34 -0700752
Andrew Scullfb49ad22022-11-09 21:02:48 +0000753 ASSERT_EQ(**firstCsr, **secondCsr);
Tri Vo0d6204e2022-09-29 16:15:34 -0700754}
755
756/**
757 * Generate a non-empty certificate request with multiple keys.
758 */
759TEST_P(CertificateRequestV2Test, NonEmptyRequestMultipleKeys) {
Tri Vo9cab73c2022-10-28 13:40:24 -0700760 generateKeys(false /* testMode */, rpcHardwareInfo.supportedNumKeysInCsr /* numKeys */);
Tri Vo0d6204e2022-09-29 16:15:34 -0700761
762 bytevec csr;
763
764 auto status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr);
765 ASSERT_TRUE(status.isOk()) << status.getMessage();
766
767 auto result = verifyProductionCsr(cborKeysToSign_, csr, provisionable_.get(), challenge_);
768 ASSERT_TRUE(result) << result.message();
769}
770
771/**
772 * Generate a non-empty certificate request, but with the MAC corrupted on the keypair.
773 */
774TEST_P(CertificateRequestV2Test, NonEmptyRequestCorruptMac) {
775 generateKeys(false /* testMode */, 1 /* numKeys */);
776 auto result = corrupt_maced_key(keysToSign_[0]);
777 ASSERT_TRUE(result) << result.moveMessage();
778 MacedPublicKey keyWithCorruptMac = result.moveValue();
779
780 bytevec csr;
781 auto status =
782 provisionable_->generateCertificateRequestV2({keyWithCorruptMac}, challenge_, &csr);
783 ASSERT_FALSE(status.isOk()) << status.getMessage();
784 EXPECT_EQ(status.getServiceSpecificError(), BnRemotelyProvisionedComponent::STATUS_INVALID_MAC);
785}
786
787/**
Tri Vob0b8acc2022-11-30 16:22:23 -0800788 * Generate a non-empty certificate request in prod mode, with test keys. Must fail with
789 * STATUS_TEST_KEY_IN_PRODUCTION_REQUEST.
Tri Vo0d6204e2022-09-29 16:15:34 -0700790 */
791TEST_P(CertificateRequestV2Test, NonEmptyRequest_testKeyInProdCert) {
792 generateKeys(true /* testMode */, 1 /* numKeys */);
793
794 bytevec csr;
795 auto status = provisionable_->generateCertificateRequestV2(keysToSign_, challenge_, &csr);
Tri Vo0d6204e2022-09-29 16:15:34 -0700796 ASSERT_FALSE(status.isOk()) << status.getMessage();
Tri Vob0b8acc2022-11-30 16:22:23 -0800797 ASSERT_EQ(status.getServiceSpecificError(),
798 BnRemotelyProvisionedComponent::STATUS_TEST_KEY_IN_PRODUCTION_REQUEST);
Tri Vo0d6204e2022-09-29 16:15:34 -0700799}
800
Tri Voec50ee12023-02-14 16:29:53 -0800801void parse_root_of_trust(const vector<uint8_t>& attestation_cert,
802 vector<uint8_t>* verified_boot_key, VerifiedBoot* verified_boot_state,
803 bool* device_locked, vector<uint8_t>* verified_boot_hash) {
804 X509_Ptr cert(parse_cert_blob(attestation_cert));
805 ASSERT_TRUE(cert.get());
806
807 ASN1_OCTET_STRING* attest_rec = get_attestation_record(cert.get());
808 ASSERT_TRUE(attest_rec);
809
810 auto error = parse_root_of_trust(attest_rec->data, attest_rec->length, verified_boot_key,
811 verified_boot_state, device_locked, verified_boot_hash);
812 ASSERT_EQ(error, ErrorCode::OK);
813}
814
815/**
816 * Generate a CSR and verify DeviceInfo against IDs attested by KeyMint.
817 */
818TEST_P(CertificateRequestV2Test, DeviceInfo) {
819 // See if there is a matching IKeyMintDevice for this IRemotelyProvisionedComponent.
820 std::shared_ptr<IKeyMintDevice> keyMint;
821 if (!matching_keymint_device(GetParam(), &keyMint)) {
822 // No matching IKeyMintDevice.
823 GTEST_SKIP() << "Skipping key use test as no matching KeyMint device found";
824 return;
825 }
826 KeyMintHardwareInfo info;
827 ASSERT_TRUE(keyMint->getHardwareInfo(&info).isOk());
828
829 // Get IDs attested by KeyMint.
830 MacedPublicKey macedPubKey;
831 bytevec privateKeyBlob;
832 auto irpcStatus =
833 provisionable_->generateEcdsaP256KeyPair(false, &macedPubKey, &privateKeyBlob);
834 ASSERT_TRUE(irpcStatus.isOk());
835
836 AttestationKey attestKey;
837 attestKey.keyBlob = std::move(privateKeyBlob);
838 attestKey.issuerSubjectName = make_name_from_str("Android Keystore Key");
839
840 // Generate an ECDSA key that is attested by the generated P256 keypair.
841 AuthorizationSet keyDesc = AuthorizationSetBuilder()
842 .Authorization(TAG_NO_AUTH_REQUIRED)
843 .EcdsaSigningKey(EcCurve::P_256)
844 .AttestationChallenge("foo")
845 .AttestationApplicationId("bar")
846 .Digest(Digest::NONE)
847 .SetDefaultValidity();
848 KeyCreationResult creationResult;
849 auto kmStatus = keyMint->generateKey(keyDesc.vector_data(), attestKey, &creationResult);
850 ASSERT_TRUE(kmStatus.isOk());
851
852 vector<KeyCharacteristics> key_characteristics = std::move(creationResult.keyCharacteristics);
853 vector<Certificate> key_cert_chain = std::move(creationResult.certificateChain);
854 // We didn't provision the attestation key.
855 ASSERT_EQ(key_cert_chain.size(), 1);
856
857 // Parse attested patch levels.
858 auto auths = HwEnforcedAuthorizations(key_characteristics);
859
860 auto attestedSystemPatchLevel = auths.GetTagValue(TAG_OS_PATCHLEVEL);
861 auto attestedVendorPatchLevel = auths.GetTagValue(TAG_VENDOR_PATCHLEVEL);
862 auto attestedBootPatchLevel = auths.GetTagValue(TAG_BOOT_PATCHLEVEL);
863
864 ASSERT_TRUE(attestedSystemPatchLevel.has_value());
865 ASSERT_TRUE(attestedVendorPatchLevel.has_value());
866 ASSERT_TRUE(attestedBootPatchLevel.has_value());
867
868 // Parse attested AVB values.
869 vector<uint8_t> key;
870 VerifiedBoot attestedVbState;
871 bool attestedBootloaderState;
872 vector<uint8_t> attestedVbmetaDigest;
873 parse_root_of_trust(key_cert_chain[0].encodedCertificate, &key, &attestedVbState,
874 &attestedBootloaderState, &attestedVbmetaDigest);
875
876 // Get IDs from DeviceInfo.
877 bytevec csr;
878 irpcStatus =
879 provisionable_->generateCertificateRequestV2({} /* keysToSign */, challenge_, &csr);
880 ASSERT_TRUE(irpcStatus.isOk()) << irpcStatus.getMessage();
881
882 auto result = verifyProductionCsr(cppbor::Array(), csr, provisionable_.get(), challenge_);
883 ASSERT_TRUE(result) << result.message();
884
885 std::unique_ptr<cppbor::Array> csrPayload = std::move(*result);
886 ASSERT_TRUE(csrPayload);
887
888 auto deviceInfo = csrPayload->get(2)->asMap();
889 ASSERT_TRUE(deviceInfo);
890
891 auto vbState = deviceInfo->get("vb_state")->asTstr();
892 auto bootloaderState = deviceInfo->get("bootloader_state")->asTstr();
893 auto vbmetaDigest = deviceInfo->get("vbmeta_digest")->asBstr();
894 auto systemPatchLevel = deviceInfo->get("system_patch_level")->asUint();
895 auto vendorPatchLevel = deviceInfo->get("vendor_patch_level")->asUint();
896 auto bootPatchLevel = deviceInfo->get("boot_patch_level")->asUint();
897 auto securityLevel = deviceInfo->get("security_level")->asTstr();
898
899 ASSERT_TRUE(vbState);
900 ASSERT_TRUE(bootloaderState);
901 ASSERT_TRUE(vbmetaDigest);
902 ASSERT_TRUE(systemPatchLevel);
903 ASSERT_TRUE(vendorPatchLevel);
904 ASSERT_TRUE(bootPatchLevel);
905 ASSERT_TRUE(securityLevel);
906
907 auto kmDeviceName = device_suffix(GetParam());
908
909 // Compare DeviceInfo against IDs attested by KeyMint.
910 ASSERT_TRUE((securityLevel->value() == "tee" && kmDeviceName == "default") ||
911 (securityLevel->value() == "strongbox" && kmDeviceName == "strongbox"));
912 ASSERT_TRUE((vbState->value() == "green" && attestedVbState == VerifiedBoot::VERIFIED) ||
913 (vbState->value() == "yellow" && attestedVbState == VerifiedBoot::SELF_SIGNED) ||
914 (vbState->value() == "orange" && attestedVbState == VerifiedBoot::UNVERIFIED));
915 ASSERT_TRUE((bootloaderState->value() == "locked" && attestedBootloaderState) ||
916 (bootloaderState->value() == "unlocked" && !attestedBootloaderState));
917 ASSERT_EQ(vbmetaDigest->value(), attestedVbmetaDigest);
918 ASSERT_EQ(systemPatchLevel->value(), attestedSystemPatchLevel.value());
919 ASSERT_EQ(vendorPatchLevel->value(), attestedVendorPatchLevel.value());
920 ASSERT_EQ(bootPatchLevel->value(), attestedBootPatchLevel.value());
921}
922
Tri Vo0d6204e2022-09-29 16:15:34 -0700923INSTANTIATE_REM_PROV_AIDL_TEST(CertificateRequestV2Test);
924
Max Biresa9b3bb92022-11-21 23:02:09 -0800925using VsrRequirementTest = VtsRemotelyProvisionedComponentTests;
926
927INSTANTIATE_REM_PROV_AIDL_TEST(VsrRequirementTest);
928
929TEST_P(VsrRequirementTest, VsrEnforcementTest) {
930 RpcHardwareInfo hwInfo;
931 ASSERT_TRUE(provisionable_->getHardwareInfo(&hwInfo).isOk());
932 int vsr_api_level = get_vsr_api_level();
933 if (vsr_api_level < 34) {
934 GTEST_SKIP() << "Applies only to VSR API level 34 or newer, this device is: "
935 << vsr_api_level;
936 }
937 EXPECT_GE(hwInfo.versionNumber, 3)
938 << "VSR 14+ requires IRemotelyProvisionedComponent v3 or newer.";
939}
940
Shawn Willden274bb552020-09-30 22:39:22 -0600941} // namespace aidl::android::hardware::security::keymint::test