David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 1 | /* |
| 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 Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 16 | #define LOG_TAG "VtsHalIdentityEndToEndTest" |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 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> |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 30 | #include <tuple> |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 31 | |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 32 | #include "Util.h" |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 33 | |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 34 | namespace android::hardware::identity { |
| 35 | |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 36 | using std::endl; |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 37 | using std::make_tuple; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 38 | using std::map; |
| 39 | using std::optional; |
| 40 | using std::string; |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 41 | using std::tuple; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 42 | using std::vector; |
| 43 | |
| 44 | using ::android::sp; |
| 45 | using ::android::String16; |
| 46 | using ::android::binder::Status; |
| 47 | |
| 48 | using ::android::hardware::keymaster::HardwareAuthToken; |
David Zeuthen | a8ed82c | 2020-05-08 10:03:28 -0400 | [diff] [blame] | 49 | using ::android::hardware::keymaster::VerificationToken; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 50 | |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 51 | using test_utils::validateAttestationCertificate; |
| 52 | |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 53 | class EndToEndTests : public testing::TestWithParam<std::string> { |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 54 | public: |
| 55 | virtual void SetUp() override { |
| 56 | credentialStore_ = android::waitForDeclaredService<IIdentityCredentialStore>( |
| 57 | String16(GetParam().c_str())); |
| 58 | ASSERT_NE(credentialStore_, nullptr); |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 59 | halApiVersion_ = credentialStore_->getInterfaceVersion(); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | sp<IIdentityCredentialStore> credentialStore_; |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 63 | int halApiVersion_; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 64 | }; |
| 65 | |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 66 | TEST_P(EndToEndTests, hardwareInformation) { |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 67 | HardwareInformation info; |
| 68 | ASSERT_TRUE(credentialStore_->getHardwareInformation(&info).isOk()); |
| 69 | ASSERT_GT(info.credentialStoreName.size(), 0); |
| 70 | ASSERT_GT(info.credentialStoreAuthorName.size(), 0); |
| 71 | ASSERT_GE(info.dataChunkSize, 256); |
| 72 | } |
| 73 | |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 74 | tuple<bool, string, vector<uint8_t>, vector<uint8_t>, vector<uint8_t>> |
| 75 | extractFromTestCredentialData(const vector<uint8_t>& credentialData) { |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 76 | string docType; |
| 77 | vector<uint8_t> storageKey; |
| 78 | vector<uint8_t> credentialPrivKey; |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 79 | vector<uint8_t> sha256Pop; |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 80 | |
| 81 | auto [item, _, message] = cppbor::parse(credentialData); |
| 82 | if (item == nullptr) { |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 83 | return make_tuple(false, docType, storageKey, credentialPrivKey, sha256Pop); |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | const cppbor::Array* arrayItem = item->asArray(); |
| 87 | if (arrayItem == nullptr || arrayItem->size() != 3) { |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 88 | return make_tuple(false, docType, storageKey, credentialPrivKey, sha256Pop); |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | const cppbor::Tstr* docTypeItem = (*arrayItem)[0]->asTstr(); |
| 92 | const cppbor::Bool* testCredentialItem = |
| 93 | ((*arrayItem)[1]->asSimple() != nullptr ? ((*arrayItem)[1]->asSimple()->asBool()) |
| 94 | : nullptr); |
| 95 | const cppbor::Bstr* encryptedCredentialKeysItem = (*arrayItem)[2]->asBstr(); |
| 96 | if (docTypeItem == nullptr || testCredentialItem == nullptr || |
| 97 | encryptedCredentialKeysItem == nullptr) { |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 98 | return make_tuple(false, docType, storageKey, credentialPrivKey, sha256Pop); |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | docType = docTypeItem->value(); |
| 102 | |
| 103 | vector<uint8_t> hardwareBoundKey = support::getTestHardwareBoundKey(); |
| 104 | const vector<uint8_t>& encryptedCredentialKeys = encryptedCredentialKeysItem->value(); |
| 105 | const vector<uint8_t> docTypeVec(docType.begin(), docType.end()); |
| 106 | optional<vector<uint8_t>> decryptedCredentialKeys = |
| 107 | support::decryptAes128Gcm(hardwareBoundKey, encryptedCredentialKeys, docTypeVec); |
| 108 | if (!decryptedCredentialKeys) { |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 109 | return make_tuple(false, docType, storageKey, credentialPrivKey, sha256Pop); |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | auto [dckItem, dckPos, dckMessage] = cppbor::parse(decryptedCredentialKeys.value()); |
| 113 | if (dckItem == nullptr) { |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 114 | return make_tuple(false, docType, storageKey, credentialPrivKey, sha256Pop); |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 115 | } |
| 116 | const cppbor::Array* dckArrayItem = dckItem->asArray(); |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 117 | if (dckArrayItem == nullptr) { |
| 118 | return make_tuple(false, docType, storageKey, credentialPrivKey, sha256Pop); |
| 119 | } |
| 120 | if (dckArrayItem->size() < 2) { |
| 121 | return make_tuple(false, docType, storageKey, credentialPrivKey, sha256Pop); |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 122 | } |
| 123 | const cppbor::Bstr* storageKeyItem = (*dckArrayItem)[0]->asBstr(); |
| 124 | const cppbor::Bstr* credentialPrivKeyItem = (*dckArrayItem)[1]->asBstr(); |
| 125 | if (storageKeyItem == nullptr || credentialPrivKeyItem == nullptr) { |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 126 | return make_tuple(false, docType, storageKey, credentialPrivKey, sha256Pop); |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 127 | } |
| 128 | storageKey = storageKeyItem->value(); |
| 129 | credentialPrivKey = credentialPrivKeyItem->value(); |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 130 | if (dckArrayItem->size() == 3) { |
| 131 | const cppbor::Bstr* sha256PopItem = (*dckArrayItem)[2]->asBstr(); |
| 132 | if (sha256PopItem == nullptr) { |
| 133 | return make_tuple(false, docType, storageKey, credentialPrivKey, sha256Pop); |
| 134 | } |
| 135 | sha256Pop = sha256PopItem->value(); |
| 136 | } |
| 137 | return make_tuple(true, docType, storageKey, credentialPrivKey, sha256Pop); |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 138 | } |
| 139 | |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 140 | TEST_P(EndToEndTests, createAndRetrieveCredential) { |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 141 | // First, generate a key-pair for the reader since its public key will be |
| 142 | // part of the request data. |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 143 | vector<uint8_t> readerKey; |
| 144 | optional<vector<uint8_t>> readerCertificate = |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 145 | test_utils::generateReaderCertificate("1234", &readerKey); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 146 | ASSERT_TRUE(readerCertificate); |
| 147 | |
| 148 | // Make the portrait image really big (just shy of 256 KiB) to ensure that |
| 149 | // the chunking code gets exercised. |
| 150 | vector<uint8_t> portraitImage; |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 151 | test_utils::setImageData(portraitImage); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 152 | |
| 153 | // Access control profiles: |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 154 | const vector<test_utils::TestProfile> testProfiles = {// Profile 0 (reader authentication) |
| 155 | {0, readerCertificate.value(), false, 0}, |
| 156 | // Profile 1 (no authentication) |
| 157 | {1, {}, false, 0}}; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 158 | |
David Zeuthen | a8ed82c | 2020-05-08 10:03:28 -0400 | [diff] [blame] | 159 | // It doesn't matter since no user auth is needed in this particular test, |
| 160 | // but for good measure, clear out the tokens we pass to the HAL. |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 161 | HardwareAuthToken authToken; |
David Zeuthen | a8ed82c | 2020-05-08 10:03:28 -0400 | [diff] [blame] | 162 | VerificationToken verificationToken; |
| 163 | authToken.challenge = 0; |
| 164 | authToken.userId = 0; |
| 165 | authToken.authenticatorId = 0; |
| 166 | authToken.authenticatorType = ::android::hardware::keymaster::HardwareAuthenticatorType::NONE; |
| 167 | authToken.timestamp.milliSeconds = 0; |
| 168 | authToken.mac.clear(); |
| 169 | verificationToken.challenge = 0; |
| 170 | verificationToken.timestamp.milliSeconds = 0; |
| 171 | verificationToken.securityLevel = ::android::hardware::keymaster::SecurityLevel::SOFTWARE; |
| 172 | verificationToken.mac.clear(); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 173 | |
| 174 | // Here's the actual test data: |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 175 | const vector<test_utils::TestEntryData> testEntries = { |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 176 | {"PersonalData", "Last name", string("Turing"), vector<int32_t>{0, 1}}, |
| 177 | {"PersonalData", "Birth date", string("19120623"), vector<int32_t>{0, 1}}, |
| 178 | {"PersonalData", "First name", string("Alan"), vector<int32_t>{0, 1}}, |
| 179 | {"PersonalData", "Home address", string("Maida Vale, London, England"), |
| 180 | vector<int32_t>{0}}, |
| 181 | {"Image", "Portrait image", portraitImage, vector<int32_t>{0, 1}}, |
| 182 | }; |
| 183 | const vector<int32_t> testEntriesEntryCounts = {static_cast<int32_t>(testEntries.size() - 1), |
| 184 | 1u}; |
| 185 | HardwareInformation hwInfo; |
| 186 | ASSERT_TRUE(credentialStore_->getHardwareInformation(&hwInfo).isOk()); |
| 187 | |
| 188 | string cborPretty; |
| 189 | sp<IWritableIdentityCredential> writableCredential; |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 190 | ASSERT_TRUE(test_utils::setupWritableCredential(writableCredential, credentialStore_, |
| 191 | true /* testCredential */)); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 192 | |
| 193 | string challenge = "attestationChallenge"; |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 194 | test_utils::AttestationData attData(writableCredential, challenge, |
| 195 | {1} /* atteestationApplicationId */); |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 196 | ASSERT_TRUE(attData.result.isOk()) |
| 197 | << attData.result.exceptionCode() << "; " << attData.result.exceptionMessage() << endl; |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 198 | |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 199 | validateAttestationCertificate(attData.attestationCertificate, attData.attestationChallenge, |
| 200 | attData.attestationApplicationId, true); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 201 | |
David Zeuthen | 28edb10 | 2020-04-28 18:54:55 -0400 | [diff] [blame] | 202 | // This is kinda of a hack but we need to give the size of |
| 203 | // ProofOfProvisioning that we'll expect to receive. |
| 204 | const int32_t expectedProofOfProvisioningSize = 262861 - 326 + readerCertificate.value().size(); |
| 205 | // OK to fail, not available in v1 HAL |
| 206 | writableCredential->setExpectedProofOfProvisioningSize(expectedProofOfProvisioningSize); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 207 | ASSERT_TRUE( |
| 208 | writableCredential->startPersonalization(testProfiles.size(), testEntriesEntryCounts) |
| 209 | .isOk()); |
| 210 | |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 211 | optional<vector<SecureAccessControlProfile>> secureProfiles = |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 212 | test_utils::addAccessControlProfiles(writableCredential, testProfiles); |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 213 | ASSERT_TRUE(secureProfiles); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 214 | |
| 215 | // Uses TestEntryData* pointer as key and values are the encrypted blobs. This |
| 216 | // is a little hacky but it works well enough. |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 217 | map<const test_utils::TestEntryData*, vector<vector<uint8_t>>> encryptedBlobs; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 218 | |
| 219 | for (const auto& entry : testEntries) { |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 220 | ASSERT_TRUE(test_utils::addEntry(writableCredential, entry, hwInfo.dataChunkSize, |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 221 | encryptedBlobs, true)); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | vector<uint8_t> credentialData; |
| 225 | vector<uint8_t> proofOfProvisioningSignature; |
| 226 | ASSERT_TRUE( |
| 227 | writableCredential->finishAddingEntries(&credentialData, &proofOfProvisioningSignature) |
| 228 | .isOk()); |
| 229 | |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 230 | // Validate the proofOfProvisioning which was returned |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 231 | optional<vector<uint8_t>> proofOfProvisioning = |
| 232 | support::coseSignGetPayload(proofOfProvisioningSignature); |
| 233 | ASSERT_TRUE(proofOfProvisioning); |
| 234 | cborPretty = support::cborPrettyPrint(proofOfProvisioning.value(), 32, {"readerCertificate"}); |
| 235 | EXPECT_EQ( |
| 236 | "[\n" |
| 237 | " 'ProofOfProvisioning',\n" |
| 238 | " 'org.iso.18013-5.2019.mdl',\n" |
| 239 | " [\n" |
| 240 | " {\n" |
| 241 | " 'id' : 0,\n" |
| 242 | " 'readerCertificate' : <not printed>,\n" |
| 243 | " },\n" |
| 244 | " {\n" |
| 245 | " 'id' : 1,\n" |
| 246 | " },\n" |
| 247 | " ],\n" |
| 248 | " {\n" |
| 249 | " 'PersonalData' : [\n" |
| 250 | " {\n" |
| 251 | " 'name' : 'Last name',\n" |
| 252 | " 'value' : 'Turing',\n" |
| 253 | " 'accessControlProfiles' : [0, 1, ],\n" |
| 254 | " },\n" |
| 255 | " {\n" |
| 256 | " 'name' : 'Birth date',\n" |
| 257 | " 'value' : '19120623',\n" |
| 258 | " 'accessControlProfiles' : [0, 1, ],\n" |
| 259 | " },\n" |
| 260 | " {\n" |
| 261 | " 'name' : 'First name',\n" |
| 262 | " 'value' : 'Alan',\n" |
| 263 | " 'accessControlProfiles' : [0, 1, ],\n" |
| 264 | " },\n" |
| 265 | " {\n" |
| 266 | " 'name' : 'Home address',\n" |
| 267 | " 'value' : 'Maida Vale, London, England',\n" |
| 268 | " 'accessControlProfiles' : [0, ],\n" |
| 269 | " },\n" |
| 270 | " ],\n" |
| 271 | " 'Image' : [\n" |
| 272 | " {\n" |
| 273 | " 'name' : 'Portrait image',\n" |
| 274 | " 'value' : <bstr size=262134 sha1=941e372f654d86c32d88fae9e41b706afbfd02bb>,\n" |
| 275 | " 'accessControlProfiles' : [0, 1, ],\n" |
| 276 | " },\n" |
| 277 | " ],\n" |
| 278 | " },\n" |
| 279 | " true,\n" |
| 280 | "]", |
| 281 | cborPretty); |
| 282 | |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 283 | optional<vector<uint8_t>> credentialPubKey = support::certificateChainGetTopMostKey( |
| 284 | attData.attestationCertificate[0].encodedCertificate); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 285 | ASSERT_TRUE(credentialPubKey); |
| 286 | EXPECT_TRUE(support::coseCheckEcDsaSignature(proofOfProvisioningSignature, |
| 287 | {}, // Additional data |
| 288 | credentialPubKey.value())); |
| 289 | writableCredential = nullptr; |
| 290 | |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 291 | // Extract doctype, storage key, and credentialPrivKey from credentialData... this works |
| 292 | // only because we asked for a test-credential meaning that the HBK is all zeroes. |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 293 | auto [exSuccess, exDocType, exStorageKey, exCredentialPrivKey, exSha256Pop] = |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 294 | extractFromTestCredentialData(credentialData); |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 295 | |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 296 | ASSERT_TRUE(exSuccess); |
| 297 | ASSERT_EQ(exDocType, "org.iso.18013-5.2019.mdl"); |
| 298 | // ... check that the public key derived from the private key matches what was |
| 299 | // in the certificate. |
| 300 | optional<vector<uint8_t>> exCredentialKeyPair = |
| 301 | support::ecPrivateKeyToKeyPair(exCredentialPrivKey); |
| 302 | ASSERT_TRUE(exCredentialKeyPair); |
| 303 | optional<vector<uint8_t>> exCredentialPubKey = |
| 304 | support::ecKeyPairGetPublicKey(exCredentialKeyPair.value()); |
| 305 | ASSERT_TRUE(exCredentialPubKey); |
| 306 | ASSERT_EQ(exCredentialPubKey.value(), credentialPubKey.value()); |
| 307 | |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 308 | // Starting with API version 3 (feature version 202101) we require SHA-256(ProofOfProvisioning) |
| 309 | // to be in CredentialKeys (which is stored encrypted in CredentialData). Check |
| 310 | // that it's there with the expected value. |
| 311 | if (halApiVersion_ >= 3) { |
| 312 | ASSERT_EQ(exSha256Pop, support::sha256(proofOfProvisioning.value())); |
| 313 | } |
| 314 | |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 315 | // Now that the credential has been provisioned, read it back and check the |
| 316 | // correct data is returned. |
| 317 | sp<IIdentityCredential> credential; |
| 318 | ASSERT_TRUE(credentialStore_ |
| 319 | ->getCredential( |
| 320 | CipherSuite::CIPHERSUITE_ECDHE_HKDF_ECDSA_WITH_AES_256_GCM_SHA256, |
| 321 | credentialData, &credential) |
| 322 | .isOk()); |
| 323 | ASSERT_NE(credential, nullptr); |
| 324 | |
| 325 | optional<vector<uint8_t>> readerEphemeralKeyPair = support::createEcKeyPair(); |
| 326 | ASSERT_TRUE(readerEphemeralKeyPair); |
| 327 | optional<vector<uint8_t>> readerEphemeralPublicKey = |
| 328 | support::ecKeyPairGetPublicKey(readerEphemeralKeyPair.value()); |
| 329 | ASSERT_TRUE(credential->setReaderEphemeralPublicKey(readerEphemeralPublicKey.value()).isOk()); |
| 330 | |
| 331 | vector<uint8_t> ephemeralKeyPair; |
| 332 | ASSERT_TRUE(credential->createEphemeralKeyPair(&ephemeralKeyPair).isOk()); |
| 333 | optional<vector<uint8_t>> ephemeralPublicKey = support::ecKeyPairGetPublicKey(ephemeralKeyPair); |
| 334 | |
| 335 | // Calculate requestData field and sign it with the reader key. |
| 336 | auto [getXYSuccess, ephX, ephY] = support::ecPublicKeyGetXandY(ephemeralPublicKey.value()); |
| 337 | ASSERT_TRUE(getXYSuccess); |
| 338 | cppbor::Map deviceEngagement = cppbor::Map().add("ephX", ephX).add("ephY", ephY); |
| 339 | vector<uint8_t> deviceEngagementBytes = deviceEngagement.encode(); |
| 340 | vector<uint8_t> eReaderPubBytes = cppbor::Tstr("ignored").encode(); |
| 341 | cppbor::Array sessionTranscript = cppbor::Array() |
| 342 | .add(cppbor::Semantic(24, deviceEngagementBytes)) |
| 343 | .add(cppbor::Semantic(24, eReaderPubBytes)); |
David Zeuthen | 2e4533e | 2020-06-20 17:04:41 -0400 | [diff] [blame] | 344 | vector<uint8_t> sessionTranscriptEncoded = sessionTranscript.encode(); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 345 | |
| 346 | vector<uint8_t> itemsRequestBytes = |
| 347 | cppbor::Map("nameSpaces", |
| 348 | cppbor::Map() |
| 349 | .add("PersonalData", cppbor::Map() |
| 350 | .add("Last name", false) |
| 351 | .add("Birth date", false) |
| 352 | .add("First name", false) |
| 353 | .add("Home address", true)) |
| 354 | .add("Image", cppbor::Map().add("Portrait image", false))) |
| 355 | .encode(); |
| 356 | cborPretty = support::cborPrettyPrint(itemsRequestBytes, 32, {"EphemeralPublicKey"}); |
| 357 | EXPECT_EQ( |
| 358 | "{\n" |
| 359 | " 'nameSpaces' : {\n" |
| 360 | " 'PersonalData' : {\n" |
| 361 | " 'Last name' : false,\n" |
| 362 | " 'Birth date' : false,\n" |
| 363 | " 'First name' : false,\n" |
| 364 | " 'Home address' : true,\n" |
| 365 | " },\n" |
| 366 | " 'Image' : {\n" |
| 367 | " 'Portrait image' : false,\n" |
| 368 | " },\n" |
| 369 | " },\n" |
| 370 | "}", |
| 371 | cborPretty); |
David Zeuthen | 2e4533e | 2020-06-20 17:04:41 -0400 | [diff] [blame] | 372 | vector<uint8_t> encodedReaderAuthentication = |
| 373 | cppbor::Array() |
| 374 | .add("ReaderAuthentication") |
| 375 | .add(sessionTranscript.clone()) |
| 376 | .add(cppbor::Semantic(24, itemsRequestBytes)) |
| 377 | .encode(); |
| 378 | vector<uint8_t> encodedReaderAuthenticationBytes = |
| 379 | cppbor::Semantic(24, encodedReaderAuthentication).encode(); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 380 | optional<vector<uint8_t>> readerSignature = |
David Zeuthen | 2e4533e | 2020-06-20 17:04:41 -0400 | [diff] [blame] | 381 | support::coseSignEcDsa(readerKey, {}, // content |
| 382 | encodedReaderAuthenticationBytes, // detached content |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 383 | readerCertificate.value()); |
| 384 | ASSERT_TRUE(readerSignature); |
| 385 | |
David Zeuthen | e35797f | 2020-02-27 14:25:54 -0500 | [diff] [blame] | 386 | // Generate the key that will be used to sign AuthenticatedData. |
| 387 | vector<uint8_t> signingKeyBlob; |
| 388 | Certificate signingKeyCertificate; |
| 389 | ASSERT_TRUE(credential->generateSigningKeyPair(&signingKeyBlob, &signingKeyCertificate).isOk()); |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 390 | optional<vector<uint8_t>> signingPubKey = |
| 391 | support::certificateChainGetTopMostKey(signingKeyCertificate.encodedCertificate); |
| 392 | EXPECT_TRUE(signingPubKey); |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 393 | test_utils::verifyAuthKeyCertificate(signingKeyCertificate.encodedCertificate); |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 394 | |
| 395 | // Since we're using a test-credential we know storageKey meaning we can get the |
| 396 | // private key. Do this, derive the public key from it, and check this matches what |
| 397 | // is in the certificate... |
| 398 | const vector<uint8_t> exDocTypeVec(exDocType.begin(), exDocType.end()); |
| 399 | optional<vector<uint8_t>> exSigningPrivKey = |
| 400 | support::decryptAes128Gcm(exStorageKey, signingKeyBlob, exDocTypeVec); |
| 401 | ASSERT_TRUE(exSigningPrivKey); |
| 402 | optional<vector<uint8_t>> exSigningKeyPair = |
| 403 | support::ecPrivateKeyToKeyPair(exSigningPrivKey.value()); |
| 404 | ASSERT_TRUE(exSigningKeyPair); |
| 405 | optional<vector<uint8_t>> exSigningPubKey = |
| 406 | support::ecKeyPairGetPublicKey(exSigningKeyPair.value()); |
| 407 | ASSERT_TRUE(exSigningPubKey); |
| 408 | ASSERT_EQ(exSigningPubKey.value(), signingPubKey.value()); |
David Zeuthen | e35797f | 2020-02-27 14:25:54 -0500 | [diff] [blame] | 409 | |
David Zeuthen | 28edb10 | 2020-04-28 18:54:55 -0400 | [diff] [blame] | 410 | vector<RequestNamespace> requestedNamespaces = test_utils::buildRequestNamespaces(testEntries); |
David Zeuthen | a8ed82c | 2020-05-08 10:03:28 -0400 | [diff] [blame] | 411 | // OK to fail, not available in v1 HAL |
David Zeuthen | 7067a73 | 2020-07-10 14:34:21 -0400 | [diff] [blame] | 412 | credential->setRequestedNamespaces(requestedNamespaces); |
David Zeuthen | a8ed82c | 2020-05-08 10:03:28 -0400 | [diff] [blame] | 413 | // OK to fail, not available in v1 HAL |
| 414 | credential->setVerificationToken(verificationToken); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 415 | ASSERT_TRUE(credential |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 416 | ->startRetrieval(secureProfiles.value(), authToken, itemsRequestBytes, |
David Zeuthen | 2e4533e | 2020-06-20 17:04:41 -0400 | [diff] [blame] | 417 | signingKeyBlob, sessionTranscriptEncoded, |
David Zeuthen | e35797f | 2020-02-27 14:25:54 -0500 | [diff] [blame] | 418 | readerSignature.value(), testEntriesEntryCounts) |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 419 | .isOk()); |
| 420 | |
| 421 | for (const auto& entry : testEntries) { |
| 422 | ASSERT_TRUE(credential |
| 423 | ->startRetrieveEntryValue(entry.nameSpace, entry.name, |
| 424 | entry.valueCbor.size(), entry.profileIds) |
| 425 | .isOk()); |
| 426 | |
| 427 | auto it = encryptedBlobs.find(&entry); |
| 428 | ASSERT_NE(it, encryptedBlobs.end()); |
| 429 | const vector<vector<uint8_t>>& encryptedChunks = it->second; |
| 430 | |
| 431 | vector<uint8_t> content; |
| 432 | for (const auto& encryptedChunk : encryptedChunks) { |
| 433 | vector<uint8_t> chunk; |
| 434 | ASSERT_TRUE(credential->retrieveEntryValue(encryptedChunk, &chunk).isOk()); |
| 435 | content.insert(content.end(), chunk.begin(), chunk.end()); |
| 436 | } |
| 437 | EXPECT_EQ(content, entry.valueCbor); |
David Zeuthen | ef73951 | 2020-06-03 13:24:52 -0400 | [diff] [blame] | 438 | |
| 439 | // TODO: also use |exStorageKey| to decrypt data and check it's the same as whatt |
| 440 | // the HAL returns... |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 441 | } |
| 442 | |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 443 | vector<uint8_t> mac; |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 444 | vector<uint8_t> deviceNameSpacesEncoded; |
| 445 | ASSERT_TRUE(credential->finishRetrieval(&mac, &deviceNameSpacesEncoded).isOk()); |
| 446 | cborPretty = support::cborPrettyPrint(deviceNameSpacesEncoded, 32, {}); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 447 | ASSERT_EQ( |
| 448 | "{\n" |
| 449 | " 'PersonalData' : {\n" |
| 450 | " 'Last name' : 'Turing',\n" |
| 451 | " 'Birth date' : '19120623',\n" |
| 452 | " 'First name' : 'Alan',\n" |
| 453 | " 'Home address' : 'Maida Vale, London, England',\n" |
| 454 | " },\n" |
| 455 | " 'Image' : {\n" |
| 456 | " 'Portrait image' : <bstr size=262134 " |
| 457 | "sha1=941e372f654d86c32d88fae9e41b706afbfd02bb>,\n" |
| 458 | " },\n" |
| 459 | "}", |
| 460 | cborPretty); |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 461 | |
| 462 | string docType = "org.iso.18013-5.2019.mdl"; |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 463 | optional<vector<uint8_t>> readerEphemeralPrivateKey = |
| 464 | support::ecKeyPairGetPrivateKey(readerEphemeralKeyPair.value()); |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 465 | optional<vector<uint8_t>> eMacKey = support::calcEMacKey( |
| 466 | readerEphemeralPrivateKey.value(), // Private Key |
| 467 | signingPubKey.value(), // Public Key |
| 468 | cppbor::Semantic(24, sessionTranscript.encode()).encode()); // SessionTranscriptBytes |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 469 | optional<vector<uint8_t>> calculatedMac = |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 470 | support::calcMac(sessionTranscript.encode(), // SessionTranscript |
| 471 | docType, // DocType |
| 472 | deviceNameSpacesEncoded, // DeviceNamespaces |
| 473 | eMacKey.value()); // EMacKey |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 474 | ASSERT_TRUE(calculatedMac); |
| 475 | EXPECT_EQ(mac, calculatedMac); |
David Zeuthen | 7067a73 | 2020-07-10 14:34:21 -0400 | [diff] [blame] | 476 | |
| 477 | // Also perform an additional empty request. This is what mDL applications |
| 478 | // are envisioned to do - one call to get the data elements, another to get |
| 479 | // an empty DeviceSignedItems and corresponding MAC. |
| 480 | // |
| 481 | credential->setRequestedNamespaces({}); // OK to fail, not available in v1 HAL |
| 482 | ASSERT_TRUE(credential |
| 483 | ->startRetrieval( |
| 484 | secureProfiles.value(), authToken, {}, // itemsRequestBytes |
| 485 | signingKeyBlob, sessionTranscriptEncoded, {}, // readerSignature, |
| 486 | testEntriesEntryCounts) |
| 487 | .isOk()); |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 488 | ASSERT_TRUE(credential->finishRetrieval(&mac, &deviceNameSpacesEncoded).isOk()); |
| 489 | cborPretty = support::cborPrettyPrint(deviceNameSpacesEncoded, 32, {}); |
David Zeuthen | 7067a73 | 2020-07-10 14:34:21 -0400 | [diff] [blame] | 490 | ASSERT_EQ("{}", cborPretty); |
| 491 | // Calculate DeviceAuthentication and MAC (MACing key hasn't changed) |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 492 | calculatedMac = support::calcMac(sessionTranscript.encode(), // SessionTranscript |
| 493 | docType, // DocType |
| 494 | deviceNameSpacesEncoded, // DeviceNamespaces |
| 495 | eMacKey.value()); // EMacKey |
David Zeuthen | 7067a73 | 2020-07-10 14:34:21 -0400 | [diff] [blame] | 496 | ASSERT_TRUE(calculatedMac); |
| 497 | EXPECT_EQ(mac, calculatedMac); |
| 498 | |
| 499 | // Some mDL apps might send a request but with a single empty |
| 500 | // namespace. Check that too. |
| 501 | RequestNamespace emptyRequestNS; |
| 502 | emptyRequestNS.namespaceName = "PersonalData"; |
| 503 | credential->setRequestedNamespaces({emptyRequestNS}); // OK to fail, not available in v1 HAL |
| 504 | ASSERT_TRUE(credential |
| 505 | ->startRetrieval( |
| 506 | secureProfiles.value(), authToken, {}, // itemsRequestBytes |
| 507 | signingKeyBlob, sessionTranscriptEncoded, {}, // readerSignature, |
| 508 | testEntriesEntryCounts) |
| 509 | .isOk()); |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 510 | ASSERT_TRUE(credential->finishRetrieval(&mac, &deviceNameSpacesEncoded).isOk()); |
| 511 | cborPretty = support::cborPrettyPrint(deviceNameSpacesEncoded, 32, {}); |
David Zeuthen | 7067a73 | 2020-07-10 14:34:21 -0400 | [diff] [blame] | 512 | ASSERT_EQ("{}", cborPretty); |
| 513 | // Calculate DeviceAuthentication and MAC (MACing key hasn't changed) |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 514 | calculatedMac = support::calcMac(sessionTranscript.encode(), // SessionTranscript |
| 515 | docType, // DocType |
| 516 | deviceNameSpacesEncoded, // DeviceNamespaces |
| 517 | eMacKey.value()); // EMacKey |
David Zeuthen | 7067a73 | 2020-07-10 14:34:21 -0400 | [diff] [blame] | 518 | ASSERT_TRUE(calculatedMac); |
| 519 | EXPECT_EQ(mac, calculatedMac); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 520 | } |
| 521 | |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 522 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EndToEndTests); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 523 | INSTANTIATE_TEST_SUITE_P( |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 524 | Identity, EndToEndTests, |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 525 | testing::ValuesIn(android::getAidlHalInstanceNames(IIdentityCredentialStore::descriptor)), |
| 526 | android::PrintInstanceNameToString); |
David Zeuthen | 8160315 | 2020-02-11 22:04:24 -0500 | [diff] [blame] | 527 | |
| 528 | } // namespace android::hardware::identity |
| 529 | |
| 530 | int main(int argc, char** argv) { |
| 531 | ::testing::InitGoogleTest(&argc, argv); |
| 532 | ::android::ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 533 | ::android::ProcessState::self()->startThreadPool(); |
| 534 | return RUN_ALL_TESTS(); |
| 535 | } |