blob: 6b40473fa91e275884e0b8791b86965cbe7ca6e9 [file] [log] [blame]
David Zeuthen81603152020-02-11 22:04:24 -05001/*
2 * Copyright (C) 2019 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 */
Selene Huangcab019a2020-03-11 04:37:48 -070016#define LOG_TAG "VtsHalIdentityEndToEndTest"
David Zeuthen81603152020-02-11 22:04:24 -050017
18#include <aidl/Gtest.h>
19#include <aidl/Vintf.h>
20#include <android-base/logging.h>
21#include <android/hardware/identity/IIdentityCredentialStore.h>
22#include <android/hardware/identity/support/IdentityCredentialSupport.h>
23#include <binder/IServiceManager.h>
24#include <binder/ProcessState.h>
25#include <cppbor.h>
26#include <cppbor_parse.h>
27#include <gtest/gtest.h>
28#include <future>
29#include <map>
30
Selene Huang92b61d62020-03-04 02:24:16 -080031#include "VtsIdentityTestUtils.h"
32
David Zeuthen81603152020-02-11 22:04:24 -050033namespace android::hardware::identity {
34
Selene Huang92b61d62020-03-04 02:24:16 -080035using std::endl;
David Zeuthen81603152020-02-11 22:04:24 -050036using std::map;
37using std::optional;
38using std::string;
39using std::vector;
40
41using ::android::sp;
42using ::android::String16;
43using ::android::binder::Status;
44
45using ::android::hardware::keymaster::HardwareAuthToken;
46
Selene Huangcab019a2020-03-11 04:37:48 -070047using test_utils::validateAttestationCertificate;
48
David Zeuthen81603152020-02-11 22:04:24 -050049class IdentityAidl : public testing::TestWithParam<std::string> {
50 public:
51 virtual void SetUp() override {
52 credentialStore_ = android::waitForDeclaredService<IIdentityCredentialStore>(
53 String16(GetParam().c_str()));
54 ASSERT_NE(credentialStore_, nullptr);
55 }
56
57 sp<IIdentityCredentialStore> credentialStore_;
58};
59
60TEST_P(IdentityAidl, hardwareInformation) {
61 HardwareInformation info;
62 ASSERT_TRUE(credentialStore_->getHardwareInformation(&info).isOk());
63 ASSERT_GT(info.credentialStoreName.size(), 0);
64 ASSERT_GT(info.credentialStoreAuthorName.size(), 0);
65 ASSERT_GE(info.dataChunkSize, 256);
66}
67
68TEST_P(IdentityAidl, createAndRetrieveCredential) {
69 // First, generate a key-pair for the reader since its public key will be
70 // part of the request data.
Selene Huang92b61d62020-03-04 02:24:16 -080071 vector<uint8_t> readerKey;
72 optional<vector<uint8_t>> readerCertificate =
Selene Huangcab019a2020-03-11 04:37:48 -070073 test_utils::generateReaderCertificate("1234", &readerKey);
David Zeuthen81603152020-02-11 22:04:24 -050074 ASSERT_TRUE(readerCertificate);
75
76 // Make the portrait image really big (just shy of 256 KiB) to ensure that
77 // the chunking code gets exercised.
78 vector<uint8_t> portraitImage;
Selene Huangcab019a2020-03-11 04:37:48 -070079 test_utils::setImageData(portraitImage);
David Zeuthen81603152020-02-11 22:04:24 -050080
81 // Access control profiles:
Selene Huang92b61d62020-03-04 02:24:16 -080082 const vector<test_utils::TestProfile> testProfiles = {// Profile 0 (reader authentication)
83 {0, readerCertificate.value(), false, 0},
84 // Profile 1 (no authentication)
85 {1, {}, false, 0}};
David Zeuthen81603152020-02-11 22:04:24 -050086
87 HardwareAuthToken authToken;
88
89 // Here's the actual test data:
Selene Huang92b61d62020-03-04 02:24:16 -080090 const vector<test_utils::TestEntryData> testEntries = {
David Zeuthen81603152020-02-11 22:04:24 -050091 {"PersonalData", "Last name", string("Turing"), vector<int32_t>{0, 1}},
92 {"PersonalData", "Birth date", string("19120623"), vector<int32_t>{0, 1}},
93 {"PersonalData", "First name", string("Alan"), vector<int32_t>{0, 1}},
94 {"PersonalData", "Home address", string("Maida Vale, London, England"),
95 vector<int32_t>{0}},
96 {"Image", "Portrait image", portraitImage, vector<int32_t>{0, 1}},
97 };
98 const vector<int32_t> testEntriesEntryCounts = {static_cast<int32_t>(testEntries.size() - 1),
99 1u};
100 HardwareInformation hwInfo;
101 ASSERT_TRUE(credentialStore_->getHardwareInformation(&hwInfo).isOk());
102
103 string cborPretty;
104 sp<IWritableIdentityCredential> writableCredential;
Selene Huangcab019a2020-03-11 04:37:48 -0700105 ASSERT_TRUE(test_utils::setupWritableCredential(writableCredential, credentialStore_));
David Zeuthen81603152020-02-11 22:04:24 -0500106
107 string challenge = "attestationChallenge";
Selene Huang92b61d62020-03-04 02:24:16 -0800108 test_utils::AttestationData attData(writableCredential, challenge, {});
109 ASSERT_TRUE(attData.result.isOk())
110 << attData.result.exceptionCode() << "; " << attData.result.exceptionMessage() << endl;
Selene Huang92b61d62020-03-04 02:24:16 -0800111
Selene Huangcab019a2020-03-11 04:37:48 -0700112 EXPECT_TRUE(validateAttestationCertificate(attData.attestationCertificate,
113 attData.attestationChallenge,
114 attData.attestationApplicationId, hwInfo));
David Zeuthen81603152020-02-11 22:04:24 -0500115
David Zeuthen28edb102020-04-28 18:54:55 -0400116 // This is kinda of a hack but we need to give the size of
117 // ProofOfProvisioning that we'll expect to receive.
118 const int32_t expectedProofOfProvisioningSize = 262861 - 326 + readerCertificate.value().size();
119 // OK to fail, not available in v1 HAL
120 writableCredential->setExpectedProofOfProvisioningSize(expectedProofOfProvisioningSize);
David Zeuthen81603152020-02-11 22:04:24 -0500121 ASSERT_TRUE(
122 writableCredential->startPersonalization(testProfiles.size(), testEntriesEntryCounts)
123 .isOk());
124
Selene Huang92b61d62020-03-04 02:24:16 -0800125 optional<vector<SecureAccessControlProfile>> secureProfiles =
Selene Huangcab019a2020-03-11 04:37:48 -0700126 test_utils::addAccessControlProfiles(writableCredential, testProfiles);
Selene Huang92b61d62020-03-04 02:24:16 -0800127 ASSERT_TRUE(secureProfiles);
David Zeuthen81603152020-02-11 22:04:24 -0500128
129 // Uses TestEntryData* pointer as key and values are the encrypted blobs. This
130 // is a little hacky but it works well enough.
Selene Huang92b61d62020-03-04 02:24:16 -0800131 map<const test_utils::TestEntryData*, vector<vector<uint8_t>>> encryptedBlobs;
David Zeuthen81603152020-02-11 22:04:24 -0500132
133 for (const auto& entry : testEntries) {
Selene Huangcab019a2020-03-11 04:37:48 -0700134 ASSERT_TRUE(test_utils::addEntry(writableCredential, entry, hwInfo.dataChunkSize,
Selene Huang92b61d62020-03-04 02:24:16 -0800135 encryptedBlobs, true));
David Zeuthen81603152020-02-11 22:04:24 -0500136 }
137
138 vector<uint8_t> credentialData;
139 vector<uint8_t> proofOfProvisioningSignature;
140 ASSERT_TRUE(
141 writableCredential->finishAddingEntries(&credentialData, &proofOfProvisioningSignature)
142 .isOk());
143
144 optional<vector<uint8_t>> proofOfProvisioning =
145 support::coseSignGetPayload(proofOfProvisioningSignature);
146 ASSERT_TRUE(proofOfProvisioning);
147 cborPretty = support::cborPrettyPrint(proofOfProvisioning.value(), 32, {"readerCertificate"});
148 EXPECT_EQ(
149 "[\n"
150 " 'ProofOfProvisioning',\n"
151 " 'org.iso.18013-5.2019.mdl',\n"
152 " [\n"
153 " {\n"
154 " 'id' : 0,\n"
155 " 'readerCertificate' : <not printed>,\n"
156 " },\n"
157 " {\n"
158 " 'id' : 1,\n"
159 " },\n"
160 " ],\n"
161 " {\n"
162 " 'PersonalData' : [\n"
163 " {\n"
164 " 'name' : 'Last name',\n"
165 " 'value' : 'Turing',\n"
166 " 'accessControlProfiles' : [0, 1, ],\n"
167 " },\n"
168 " {\n"
169 " 'name' : 'Birth date',\n"
170 " 'value' : '19120623',\n"
171 " 'accessControlProfiles' : [0, 1, ],\n"
172 " },\n"
173 " {\n"
174 " 'name' : 'First name',\n"
175 " 'value' : 'Alan',\n"
176 " 'accessControlProfiles' : [0, 1, ],\n"
177 " },\n"
178 " {\n"
179 " 'name' : 'Home address',\n"
180 " 'value' : 'Maida Vale, London, England',\n"
181 " 'accessControlProfiles' : [0, ],\n"
182 " },\n"
183 " ],\n"
184 " 'Image' : [\n"
185 " {\n"
186 " 'name' : 'Portrait image',\n"
187 " 'value' : <bstr size=262134 sha1=941e372f654d86c32d88fae9e41b706afbfd02bb>,\n"
188 " 'accessControlProfiles' : [0, 1, ],\n"
189 " },\n"
190 " ],\n"
191 " },\n"
192 " true,\n"
193 "]",
194 cborPretty);
195
Selene Huang92b61d62020-03-04 02:24:16 -0800196 optional<vector<uint8_t>> credentialPubKey = support::certificateChainGetTopMostKey(
197 attData.attestationCertificate[0].encodedCertificate);
David Zeuthen81603152020-02-11 22:04:24 -0500198 ASSERT_TRUE(credentialPubKey);
199 EXPECT_TRUE(support::coseCheckEcDsaSignature(proofOfProvisioningSignature,
200 {}, // Additional data
201 credentialPubKey.value()));
202 writableCredential = nullptr;
203
204 // Now that the credential has been provisioned, read it back and check the
205 // correct data is returned.
206 sp<IIdentityCredential> credential;
207 ASSERT_TRUE(credentialStore_
208 ->getCredential(
209 CipherSuite::CIPHERSUITE_ECDHE_HKDF_ECDSA_WITH_AES_256_GCM_SHA256,
210 credentialData, &credential)
211 .isOk());
212 ASSERT_NE(credential, nullptr);
213
214 optional<vector<uint8_t>> readerEphemeralKeyPair = support::createEcKeyPair();
215 ASSERT_TRUE(readerEphemeralKeyPair);
216 optional<vector<uint8_t>> readerEphemeralPublicKey =
217 support::ecKeyPairGetPublicKey(readerEphemeralKeyPair.value());
218 ASSERT_TRUE(credential->setReaderEphemeralPublicKey(readerEphemeralPublicKey.value()).isOk());
219
220 vector<uint8_t> ephemeralKeyPair;
221 ASSERT_TRUE(credential->createEphemeralKeyPair(&ephemeralKeyPair).isOk());
222 optional<vector<uint8_t>> ephemeralPublicKey = support::ecKeyPairGetPublicKey(ephemeralKeyPair);
223
224 // Calculate requestData field and sign it with the reader key.
225 auto [getXYSuccess, ephX, ephY] = support::ecPublicKeyGetXandY(ephemeralPublicKey.value());
226 ASSERT_TRUE(getXYSuccess);
227 cppbor::Map deviceEngagement = cppbor::Map().add("ephX", ephX).add("ephY", ephY);
228 vector<uint8_t> deviceEngagementBytes = deviceEngagement.encode();
229 vector<uint8_t> eReaderPubBytes = cppbor::Tstr("ignored").encode();
230 cppbor::Array sessionTranscript = cppbor::Array()
231 .add(cppbor::Semantic(24, deviceEngagementBytes))
232 .add(cppbor::Semantic(24, eReaderPubBytes));
233 vector<uint8_t> sessionTranscriptBytes = sessionTranscript.encode();
234
235 vector<uint8_t> itemsRequestBytes =
236 cppbor::Map("nameSpaces",
237 cppbor::Map()
238 .add("PersonalData", cppbor::Map()
239 .add("Last name", false)
240 .add("Birth date", false)
241 .add("First name", false)
242 .add("Home address", true))
243 .add("Image", cppbor::Map().add("Portrait image", false)))
244 .encode();
245 cborPretty = support::cborPrettyPrint(itemsRequestBytes, 32, {"EphemeralPublicKey"});
246 EXPECT_EQ(
247 "{\n"
248 " 'nameSpaces' : {\n"
249 " 'PersonalData' : {\n"
250 " 'Last name' : false,\n"
251 " 'Birth date' : false,\n"
252 " 'First name' : false,\n"
253 " 'Home address' : true,\n"
254 " },\n"
255 " 'Image' : {\n"
256 " 'Portrait image' : false,\n"
257 " },\n"
258 " },\n"
259 "}",
260 cborPretty);
261 vector<uint8_t> dataToSign = cppbor::Array()
262 .add("ReaderAuthentication")
263 .add(sessionTranscript.clone())
264 .add(cppbor::Semantic(24, itemsRequestBytes))
265 .encode();
266 optional<vector<uint8_t>> readerSignature =
Selene Huang92b61d62020-03-04 02:24:16 -0800267 support::coseSignEcDsa(readerKey, {}, // content
268 dataToSign, // detached content
David Zeuthen81603152020-02-11 22:04:24 -0500269 readerCertificate.value());
270 ASSERT_TRUE(readerSignature);
271
David Zeuthene35797f2020-02-27 14:25:54 -0500272 // Generate the key that will be used to sign AuthenticatedData.
273 vector<uint8_t> signingKeyBlob;
274 Certificate signingKeyCertificate;
275 ASSERT_TRUE(credential->generateSigningKeyPair(&signingKeyBlob, &signingKeyCertificate).isOk());
276
David Zeuthen28edb102020-04-28 18:54:55 -0400277 vector<RequestNamespace> requestedNamespaces = test_utils::buildRequestNamespaces(testEntries);
278 ASSERT_TRUE(credential->setRequestedNamespaces(requestedNamespaces).isOk());
David Zeuthen81603152020-02-11 22:04:24 -0500279 ASSERT_TRUE(credential
Selene Huang92b61d62020-03-04 02:24:16 -0800280 ->startRetrieval(secureProfiles.value(), authToken, itemsRequestBytes,
David Zeuthene35797f2020-02-27 14:25:54 -0500281 signingKeyBlob, sessionTranscriptBytes,
282 readerSignature.value(), testEntriesEntryCounts)
David Zeuthen81603152020-02-11 22:04:24 -0500283 .isOk());
284
285 for (const auto& entry : testEntries) {
286 ASSERT_TRUE(credential
287 ->startRetrieveEntryValue(entry.nameSpace, entry.name,
288 entry.valueCbor.size(), entry.profileIds)
289 .isOk());
290
291 auto it = encryptedBlobs.find(&entry);
292 ASSERT_NE(it, encryptedBlobs.end());
293 const vector<vector<uint8_t>>& encryptedChunks = it->second;
294
295 vector<uint8_t> content;
296 for (const auto& encryptedChunk : encryptedChunks) {
297 vector<uint8_t> chunk;
298 ASSERT_TRUE(credential->retrieveEntryValue(encryptedChunk, &chunk).isOk());
299 content.insert(content.end(), chunk.begin(), chunk.end());
300 }
301 EXPECT_EQ(content, entry.valueCbor);
302 }
303
David Zeuthen81603152020-02-11 22:04:24 -0500304 vector<uint8_t> mac;
305 vector<uint8_t> deviceNameSpacesBytes;
David Zeuthene35797f2020-02-27 14:25:54 -0500306 ASSERT_TRUE(credential->finishRetrieval(&mac, &deviceNameSpacesBytes).isOk());
David Zeuthen81603152020-02-11 22:04:24 -0500307 cborPretty = support::cborPrettyPrint(deviceNameSpacesBytes, 32, {});
308 ASSERT_EQ(
309 "{\n"
310 " 'PersonalData' : {\n"
311 " 'Last name' : 'Turing',\n"
312 " 'Birth date' : '19120623',\n"
313 " 'First name' : 'Alan',\n"
314 " 'Home address' : 'Maida Vale, London, England',\n"
315 " },\n"
316 " 'Image' : {\n"
317 " 'Portrait image' : <bstr size=262134 "
318 "sha1=941e372f654d86c32d88fae9e41b706afbfd02bb>,\n"
319 " },\n"
320 "}",
321 cborPretty);
322 // The data that is MACed is ["DeviceAuthentication", sessionTranscriptBytes, docType,
323 // deviceNameSpacesBytes] so build up that structure
324 cppbor::Array deviceAuthentication;
325 deviceAuthentication.add("DeviceAuthentication");
326 deviceAuthentication.add(sessionTranscript.clone());
Selene Huang92b61d62020-03-04 02:24:16 -0800327
328 string docType = "org.iso.18013-5.2019.mdl";
David Zeuthen81603152020-02-11 22:04:24 -0500329 deviceAuthentication.add(docType);
330 deviceAuthentication.add(cppbor::Semantic(24, deviceNameSpacesBytes));
331 vector<uint8_t> encodedDeviceAuthentication = deviceAuthentication.encode();
332 optional<vector<uint8_t>> signingPublicKey =
333 support::certificateChainGetTopMostKey(signingKeyCertificate.encodedCertificate);
334 EXPECT_TRUE(signingPublicKey);
335
336 // Derive the key used for MACing.
337 optional<vector<uint8_t>> readerEphemeralPrivateKey =
338 support::ecKeyPairGetPrivateKey(readerEphemeralKeyPair.value());
339 optional<vector<uint8_t>> sharedSecret =
340 support::ecdh(signingPublicKey.value(), readerEphemeralPrivateKey.value());
341 ASSERT_TRUE(sharedSecret);
342 vector<uint8_t> salt = {0x00};
343 vector<uint8_t> info = {};
344 optional<vector<uint8_t>> derivedKey = support::hkdf(sharedSecret.value(), salt, info, 32);
345 ASSERT_TRUE(derivedKey);
346 optional<vector<uint8_t>> calculatedMac =
347 support::coseMac0(derivedKey.value(), {}, // payload
348 encodedDeviceAuthentication); // detached content
349 ASSERT_TRUE(calculatedMac);
350 EXPECT_EQ(mac, calculatedMac);
351}
352
353INSTANTIATE_TEST_SUITE_P(
354 Identity, IdentityAidl,
355 testing::ValuesIn(android::getAidlHalInstanceNames(IIdentityCredentialStore::descriptor)),
356 android::PrintInstanceNameToString);
357// INSTANTIATE_TEST_SUITE_P(Identity, IdentityAidl,
358// testing::Values("android.hardware.identity.IIdentityCredentialStore/default"));
359
360} // namespace android::hardware::identity
361
362int main(int argc, char** argv) {
363 ::testing::InitGoogleTest(&argc, argv);
364 ::android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
365 ::android::ProcessState::self()->startThreadPool();
366 return RUN_ALL_TESTS();
367}