blob: 8a4e8a7fa1f57911690e29a0e0a94b48cc15387f [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 */
16#define LOG_TAG "VtsHalIdentityTargetTest"
17
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
David Zeuthen81603152020-02-11 22:04:24 -050047class IdentityAidl : public testing::TestWithParam<std::string> {
48 public:
49 virtual void SetUp() override {
50 credentialStore_ = android::waitForDeclaredService<IIdentityCredentialStore>(
51 String16(GetParam().c_str()));
52 ASSERT_NE(credentialStore_, nullptr);
53 }
54
55 sp<IIdentityCredentialStore> credentialStore_;
56};
57
58TEST_P(IdentityAidl, hardwareInformation) {
59 HardwareInformation info;
60 ASSERT_TRUE(credentialStore_->getHardwareInformation(&info).isOk());
61 ASSERT_GT(info.credentialStoreName.size(), 0);
62 ASSERT_GT(info.credentialStoreAuthorName.size(), 0);
63 ASSERT_GE(info.dataChunkSize, 256);
64}
65
66TEST_P(IdentityAidl, createAndRetrieveCredential) {
67 // First, generate a key-pair for the reader since its public key will be
68 // part of the request data.
Selene Huang92b61d62020-03-04 02:24:16 -080069 vector<uint8_t> readerKey;
70 optional<vector<uint8_t>> readerCertificate =
71 test_utils::GenerateReaderCertificate("1234", readerKey);
David Zeuthen81603152020-02-11 22:04:24 -050072 ASSERT_TRUE(readerCertificate);
73
74 // Make the portrait image really big (just shy of 256 KiB) to ensure that
75 // the chunking code gets exercised.
76 vector<uint8_t> portraitImage;
Selene Huang92b61d62020-03-04 02:24:16 -080077 test_utils::SetImageData(portraitImage);
David Zeuthen81603152020-02-11 22:04:24 -050078
79 // Access control profiles:
Selene Huang92b61d62020-03-04 02:24:16 -080080 const vector<test_utils::TestProfile> testProfiles = {// Profile 0 (reader authentication)
81 {0, readerCertificate.value(), false, 0},
82 // Profile 1 (no authentication)
83 {1, {}, false, 0}};
David Zeuthen81603152020-02-11 22:04:24 -050084
85 HardwareAuthToken authToken;
86
87 // Here's the actual test data:
Selene Huang92b61d62020-03-04 02:24:16 -080088 const vector<test_utils::TestEntryData> testEntries = {
David Zeuthen81603152020-02-11 22:04:24 -050089 {"PersonalData", "Last name", string("Turing"), vector<int32_t>{0, 1}},
90 {"PersonalData", "Birth date", string("19120623"), vector<int32_t>{0, 1}},
91 {"PersonalData", "First name", string("Alan"), vector<int32_t>{0, 1}},
92 {"PersonalData", "Home address", string("Maida Vale, London, England"),
93 vector<int32_t>{0}},
94 {"Image", "Portrait image", portraitImage, vector<int32_t>{0, 1}},
95 };
96 const vector<int32_t> testEntriesEntryCounts = {static_cast<int32_t>(testEntries.size() - 1),
97 1u};
98 HardwareInformation hwInfo;
99 ASSERT_TRUE(credentialStore_->getHardwareInformation(&hwInfo).isOk());
100
101 string cborPretty;
102 sp<IWritableIdentityCredential> writableCredential;
Selene Huang92b61d62020-03-04 02:24:16 -0800103 ASSERT_TRUE(test_utils::SetupWritableCredential(writableCredential, credentialStore_));
David Zeuthen81603152020-02-11 22:04:24 -0500104
105 string challenge = "attestationChallenge";
Selene Huang92b61d62020-03-04 02:24:16 -0800106 test_utils::AttestationData attData(writableCredential, challenge, {});
107 ASSERT_TRUE(attData.result.isOk())
108 << attData.result.exceptionCode() << "; " << attData.result.exceptionMessage() << endl;
109 ASSERT_EQ(binder::Status::EX_NONE, attData.result.exceptionCode());
110 ASSERT_EQ(IIdentityCredentialStore::STATUS_OK, attData.result.serviceSpecificErrorCode());
111
David Zeuthen81603152020-02-11 22:04:24 -0500112 // TODO: set it to something random and check it's in the cert chain
Selene Huang92b61d62020-03-04 02:24:16 -0800113 ASSERT_GE(attData.attestationCertificate.size(), 2);
David Zeuthen81603152020-02-11 22:04:24 -0500114
115 ASSERT_TRUE(
116 writableCredential->startPersonalization(testProfiles.size(), testEntriesEntryCounts)
117 .isOk());
118
Selene Huang92b61d62020-03-04 02:24:16 -0800119 optional<vector<SecureAccessControlProfile>> secureProfiles =
120 test_utils::AddAccessControlProfiles(writableCredential, testProfiles);
121 ASSERT_TRUE(secureProfiles);
David Zeuthen81603152020-02-11 22:04:24 -0500122
123 // Uses TestEntryData* pointer as key and values are the encrypted blobs. This
124 // is a little hacky but it works well enough.
Selene Huang92b61d62020-03-04 02:24:16 -0800125 map<const test_utils::TestEntryData*, vector<vector<uint8_t>>> encryptedBlobs;
David Zeuthen81603152020-02-11 22:04:24 -0500126
127 for (const auto& entry : testEntries) {
Selene Huang92b61d62020-03-04 02:24:16 -0800128 ASSERT_TRUE(test_utils::AddEntry(writableCredential, entry, hwInfo.dataChunkSize,
129 encryptedBlobs, true));
David Zeuthen81603152020-02-11 22:04:24 -0500130 }
131
132 vector<uint8_t> credentialData;
133 vector<uint8_t> proofOfProvisioningSignature;
134 ASSERT_TRUE(
135 writableCredential->finishAddingEntries(&credentialData, &proofOfProvisioningSignature)
136 .isOk());
137
138 optional<vector<uint8_t>> proofOfProvisioning =
139 support::coseSignGetPayload(proofOfProvisioningSignature);
140 ASSERT_TRUE(proofOfProvisioning);
141 cborPretty = support::cborPrettyPrint(proofOfProvisioning.value(), 32, {"readerCertificate"});
142 EXPECT_EQ(
143 "[\n"
144 " 'ProofOfProvisioning',\n"
145 " 'org.iso.18013-5.2019.mdl',\n"
146 " [\n"
147 " {\n"
148 " 'id' : 0,\n"
149 " 'readerCertificate' : <not printed>,\n"
150 " },\n"
151 " {\n"
152 " 'id' : 1,\n"
153 " },\n"
154 " ],\n"
155 " {\n"
156 " 'PersonalData' : [\n"
157 " {\n"
158 " 'name' : 'Last name',\n"
159 " 'value' : 'Turing',\n"
160 " 'accessControlProfiles' : [0, 1, ],\n"
161 " },\n"
162 " {\n"
163 " 'name' : 'Birth date',\n"
164 " 'value' : '19120623',\n"
165 " 'accessControlProfiles' : [0, 1, ],\n"
166 " },\n"
167 " {\n"
168 " 'name' : 'First name',\n"
169 " 'value' : 'Alan',\n"
170 " 'accessControlProfiles' : [0, 1, ],\n"
171 " },\n"
172 " {\n"
173 " 'name' : 'Home address',\n"
174 " 'value' : 'Maida Vale, London, England',\n"
175 " 'accessControlProfiles' : [0, ],\n"
176 " },\n"
177 " ],\n"
178 " 'Image' : [\n"
179 " {\n"
180 " 'name' : 'Portrait image',\n"
181 " 'value' : <bstr size=262134 sha1=941e372f654d86c32d88fae9e41b706afbfd02bb>,\n"
182 " 'accessControlProfiles' : [0, 1, ],\n"
183 " },\n"
184 " ],\n"
185 " },\n"
186 " true,\n"
187 "]",
188 cborPretty);
189
Selene Huang92b61d62020-03-04 02:24:16 -0800190 optional<vector<uint8_t>> credentialPubKey = support::certificateChainGetTopMostKey(
191 attData.attestationCertificate[0].encodedCertificate);
David Zeuthen81603152020-02-11 22:04:24 -0500192 ASSERT_TRUE(credentialPubKey);
193 EXPECT_TRUE(support::coseCheckEcDsaSignature(proofOfProvisioningSignature,
194 {}, // Additional data
195 credentialPubKey.value()));
196 writableCredential = nullptr;
197
198 // Now that the credential has been provisioned, read it back and check the
199 // correct data is returned.
200 sp<IIdentityCredential> credential;
201 ASSERT_TRUE(credentialStore_
202 ->getCredential(
203 CipherSuite::CIPHERSUITE_ECDHE_HKDF_ECDSA_WITH_AES_256_GCM_SHA256,
204 credentialData, &credential)
205 .isOk());
206 ASSERT_NE(credential, nullptr);
207
208 optional<vector<uint8_t>> readerEphemeralKeyPair = support::createEcKeyPair();
209 ASSERT_TRUE(readerEphemeralKeyPair);
210 optional<vector<uint8_t>> readerEphemeralPublicKey =
211 support::ecKeyPairGetPublicKey(readerEphemeralKeyPair.value());
212 ASSERT_TRUE(credential->setReaderEphemeralPublicKey(readerEphemeralPublicKey.value()).isOk());
213
214 vector<uint8_t> ephemeralKeyPair;
215 ASSERT_TRUE(credential->createEphemeralKeyPair(&ephemeralKeyPair).isOk());
216 optional<vector<uint8_t>> ephemeralPublicKey = support::ecKeyPairGetPublicKey(ephemeralKeyPair);
217
218 // Calculate requestData field and sign it with the reader key.
219 auto [getXYSuccess, ephX, ephY] = support::ecPublicKeyGetXandY(ephemeralPublicKey.value());
220 ASSERT_TRUE(getXYSuccess);
221 cppbor::Map deviceEngagement = cppbor::Map().add("ephX", ephX).add("ephY", ephY);
222 vector<uint8_t> deviceEngagementBytes = deviceEngagement.encode();
223 vector<uint8_t> eReaderPubBytes = cppbor::Tstr("ignored").encode();
224 cppbor::Array sessionTranscript = cppbor::Array()
225 .add(cppbor::Semantic(24, deviceEngagementBytes))
226 .add(cppbor::Semantic(24, eReaderPubBytes));
227 vector<uint8_t> sessionTranscriptBytes = sessionTranscript.encode();
228
229 vector<uint8_t> itemsRequestBytes =
230 cppbor::Map("nameSpaces",
231 cppbor::Map()
232 .add("PersonalData", cppbor::Map()
233 .add("Last name", false)
234 .add("Birth date", false)
235 .add("First name", false)
236 .add("Home address", true))
237 .add("Image", cppbor::Map().add("Portrait image", false)))
238 .encode();
239 cborPretty = support::cborPrettyPrint(itemsRequestBytes, 32, {"EphemeralPublicKey"});
240 EXPECT_EQ(
241 "{\n"
242 " 'nameSpaces' : {\n"
243 " 'PersonalData' : {\n"
244 " 'Last name' : false,\n"
245 " 'Birth date' : false,\n"
246 " 'First name' : false,\n"
247 " 'Home address' : true,\n"
248 " },\n"
249 " 'Image' : {\n"
250 " 'Portrait image' : false,\n"
251 " },\n"
252 " },\n"
253 "}",
254 cborPretty);
255 vector<uint8_t> dataToSign = cppbor::Array()
256 .add("ReaderAuthentication")
257 .add(sessionTranscript.clone())
258 .add(cppbor::Semantic(24, itemsRequestBytes))
259 .encode();
260 optional<vector<uint8_t>> readerSignature =
Selene Huang92b61d62020-03-04 02:24:16 -0800261 support::coseSignEcDsa(readerKey, {}, // content
262 dataToSign, // detached content
David Zeuthen81603152020-02-11 22:04:24 -0500263 readerCertificate.value());
264 ASSERT_TRUE(readerSignature);
265
David Zeuthene35797f2020-02-27 14:25:54 -0500266 // Generate the key that will be used to sign AuthenticatedData.
267 vector<uint8_t> signingKeyBlob;
268 Certificate signingKeyCertificate;
269 ASSERT_TRUE(credential->generateSigningKeyPair(&signingKeyBlob, &signingKeyCertificate).isOk());
270
David Zeuthen81603152020-02-11 22:04:24 -0500271 ASSERT_TRUE(credential
Selene Huang92b61d62020-03-04 02:24:16 -0800272 ->startRetrieval(secureProfiles.value(), authToken, itemsRequestBytes,
David Zeuthene35797f2020-02-27 14:25:54 -0500273 signingKeyBlob, sessionTranscriptBytes,
274 readerSignature.value(), testEntriesEntryCounts)
David Zeuthen81603152020-02-11 22:04:24 -0500275 .isOk());
276
277 for (const auto& entry : testEntries) {
278 ASSERT_TRUE(credential
279 ->startRetrieveEntryValue(entry.nameSpace, entry.name,
280 entry.valueCbor.size(), entry.profileIds)
281 .isOk());
282
283 auto it = encryptedBlobs.find(&entry);
284 ASSERT_NE(it, encryptedBlobs.end());
285 const vector<vector<uint8_t>>& encryptedChunks = it->second;
286
287 vector<uint8_t> content;
288 for (const auto& encryptedChunk : encryptedChunks) {
289 vector<uint8_t> chunk;
290 ASSERT_TRUE(credential->retrieveEntryValue(encryptedChunk, &chunk).isOk());
291 content.insert(content.end(), chunk.begin(), chunk.end());
292 }
293 EXPECT_EQ(content, entry.valueCbor);
294 }
295
David Zeuthen81603152020-02-11 22:04:24 -0500296 vector<uint8_t> mac;
297 vector<uint8_t> deviceNameSpacesBytes;
David Zeuthene35797f2020-02-27 14:25:54 -0500298 ASSERT_TRUE(credential->finishRetrieval(&mac, &deviceNameSpacesBytes).isOk());
David Zeuthen81603152020-02-11 22:04:24 -0500299 cborPretty = support::cborPrettyPrint(deviceNameSpacesBytes, 32, {});
300 ASSERT_EQ(
301 "{\n"
302 " 'PersonalData' : {\n"
303 " 'Last name' : 'Turing',\n"
304 " 'Birth date' : '19120623',\n"
305 " 'First name' : 'Alan',\n"
306 " 'Home address' : 'Maida Vale, London, England',\n"
307 " },\n"
308 " 'Image' : {\n"
309 " 'Portrait image' : <bstr size=262134 "
310 "sha1=941e372f654d86c32d88fae9e41b706afbfd02bb>,\n"
311 " },\n"
312 "}",
313 cborPretty);
314 // The data that is MACed is ["DeviceAuthentication", sessionTranscriptBytes, docType,
315 // deviceNameSpacesBytes] so build up that structure
316 cppbor::Array deviceAuthentication;
317 deviceAuthentication.add("DeviceAuthentication");
318 deviceAuthentication.add(sessionTranscript.clone());
Selene Huang92b61d62020-03-04 02:24:16 -0800319
320 string docType = "org.iso.18013-5.2019.mdl";
David Zeuthen81603152020-02-11 22:04:24 -0500321 deviceAuthentication.add(docType);
322 deviceAuthentication.add(cppbor::Semantic(24, deviceNameSpacesBytes));
323 vector<uint8_t> encodedDeviceAuthentication = deviceAuthentication.encode();
324 optional<vector<uint8_t>> signingPublicKey =
325 support::certificateChainGetTopMostKey(signingKeyCertificate.encodedCertificate);
326 EXPECT_TRUE(signingPublicKey);
327
328 // Derive the key used for MACing.
329 optional<vector<uint8_t>> readerEphemeralPrivateKey =
330 support::ecKeyPairGetPrivateKey(readerEphemeralKeyPair.value());
331 optional<vector<uint8_t>> sharedSecret =
332 support::ecdh(signingPublicKey.value(), readerEphemeralPrivateKey.value());
333 ASSERT_TRUE(sharedSecret);
334 vector<uint8_t> salt = {0x00};
335 vector<uint8_t> info = {};
336 optional<vector<uint8_t>> derivedKey = support::hkdf(sharedSecret.value(), salt, info, 32);
337 ASSERT_TRUE(derivedKey);
338 optional<vector<uint8_t>> calculatedMac =
339 support::coseMac0(derivedKey.value(), {}, // payload
340 encodedDeviceAuthentication); // detached content
341 ASSERT_TRUE(calculatedMac);
342 EXPECT_EQ(mac, calculatedMac);
343}
344
345INSTANTIATE_TEST_SUITE_P(
346 Identity, IdentityAidl,
347 testing::ValuesIn(android::getAidlHalInstanceNames(IIdentityCredentialStore::descriptor)),
348 android::PrintInstanceNameToString);
349// INSTANTIATE_TEST_SUITE_P(Identity, IdentityAidl,
350// testing::Values("android.hardware.identity.IIdentityCredentialStore/default"));
351
352} // namespace android::hardware::identity
353
354int main(int argc, char** argv) {
355 ::testing::InitGoogleTest(&argc, argv);
356 ::android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
357 ::android::ProcessState::self()->startThreadPool();
358 return RUN_ALL_TESTS();
359}