David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #define LOG_TAG "ProveOwnershipTests" |
| 18 | |
| 19 | #include <aidl/Gtest.h> |
| 20 | #include <aidl/Vintf.h> |
| 21 | #include <aidl/android/hardware/keymaster/HardwareAuthToken.h> |
| 22 | #include <aidl/android/hardware/keymaster/VerificationToken.h> |
| 23 | #include <android-base/logging.h> |
| 24 | #include <android/hardware/identity/IIdentityCredentialStore.h> |
| 25 | #include <android/hardware/identity/support/IdentityCredentialSupport.h> |
| 26 | #include <binder/IServiceManager.h> |
| 27 | #include <binder/ProcessState.h> |
| 28 | #include <cppbor.h> |
| 29 | #include <cppbor_parse.h> |
| 30 | #include <gtest/gtest.h> |
| 31 | #include <future> |
| 32 | #include <map> |
| 33 | #include <utility> |
| 34 | |
| 35 | #include "Util.h" |
| 36 | |
| 37 | namespace android::hardware::identity { |
| 38 | |
| 39 | using std::endl; |
| 40 | using std::make_pair; |
| 41 | using std::map; |
| 42 | using std::optional; |
| 43 | using std::pair; |
| 44 | using std::string; |
| 45 | using std::tie; |
| 46 | using std::vector; |
| 47 | |
| 48 | using ::android::sp; |
| 49 | using ::android::String16; |
| 50 | using ::android::binder::Status; |
| 51 | |
| 52 | using ::android::hardware::keymaster::HardwareAuthToken; |
| 53 | using ::android::hardware::keymaster::VerificationToken; |
| 54 | |
| 55 | class ProveOwnershipTests : public testing::TestWithParam<string> { |
| 56 | public: |
| 57 | virtual void SetUp() override { |
| 58 | credentialStore_ = android::waitForDeclaredService<IIdentityCredentialStore>( |
| 59 | String16(GetParam().c_str())); |
| 60 | ASSERT_NE(credentialStore_, nullptr); |
| 61 | halApiVersion_ = credentialStore_->getInterfaceVersion(); |
| 62 | } |
| 63 | |
| 64 | void provisionData(); |
| 65 | |
| 66 | // Set by provisionData |
| 67 | vector<uint8_t> credentialData_; |
| 68 | vector<uint8_t> credentialPubKey_; |
| 69 | |
| 70 | sp<IIdentityCredentialStore> credentialStore_; |
| 71 | int halApiVersion_; |
| 72 | }; |
| 73 | |
| 74 | void ProveOwnershipTests::provisionData() { |
| 75 | string docType = "org.iso.18013-5.2019.mdl"; |
| 76 | bool testCredential = true; |
| 77 | sp<IWritableIdentityCredential> wc; |
| 78 | ASSERT_TRUE(credentialStore_->createCredential(docType, testCredential, &wc).isOk()); |
| 79 | |
| 80 | vector<uint8_t> attestationApplicationId = {}; |
| 81 | vector<uint8_t> attestationChallenge = {1}; |
| 82 | vector<Certificate> certChain; |
| 83 | ASSERT_TRUE(wc->getAttestationCertificate(attestationApplicationId, attestationChallenge, |
| 84 | &certChain) |
| 85 | .isOk()); |
| 86 | |
| 87 | optional<vector<uint8_t>> optCredentialPubKey = |
| 88 | support::certificateChainGetTopMostKey(certChain[0].encodedCertificate); |
| 89 | ASSERT_TRUE(optCredentialPubKey); |
| 90 | credentialPubKey_ = optCredentialPubKey.value(); |
| 91 | |
| 92 | size_t proofOfProvisioningSize = 106; |
| 93 | // Not in v1 HAL, may fail |
| 94 | wc->setExpectedProofOfProvisioningSize(proofOfProvisioningSize); |
| 95 | |
| 96 | ASSERT_TRUE(wc->startPersonalization(1 /* numAccessControlProfiles */, |
| 97 | {1} /* numDataElementsPerNamespace */) |
| 98 | .isOk()); |
| 99 | |
| 100 | // Access control profile 0: open access - don't care about the returned SACP |
| 101 | SecureAccessControlProfile sacp; |
| 102 | ASSERT_TRUE(wc->addAccessControlProfile(1, {}, false, 0, 0, &sacp).isOk()); |
| 103 | |
| 104 | // Single entry - don't care about the returned encrypted data |
| 105 | ASSERT_TRUE(wc->beginAddEntry({0}, "ns", "Some Data", 1).isOk()); |
| 106 | vector<uint8_t> encryptedData; |
| 107 | ASSERT_TRUE(wc->addEntryValue({9}, &encryptedData).isOk()); |
| 108 | |
| 109 | vector<uint8_t> proofOfProvisioningSignature; |
| 110 | Status status = wc->finishAddingEntries(&credentialData_, &proofOfProvisioningSignature); |
| 111 | EXPECT_TRUE(status.isOk()) << status.exceptionCode() << ": " << status.exceptionMessage(); |
| 112 | } |
| 113 | |
| 114 | TEST_P(ProveOwnershipTests, proveOwnership) { |
| 115 | if (halApiVersion_ < 3) { |
| 116 | GTEST_SKIP() << "Need HAL API version 3, have " << halApiVersion_; |
| 117 | } |
| 118 | |
| 119 | provisionData(); |
| 120 | |
| 121 | sp<IIdentityCredential> credential; |
| 122 | ASSERT_TRUE(credentialStore_ |
| 123 | ->getCredential( |
| 124 | CipherSuite::CIPHERSUITE_ECDHE_HKDF_ECDSA_WITH_AES_256_GCM_SHA256, |
| 125 | credentialData_, &credential) |
| 126 | .isOk()); |
| 127 | |
| 128 | vector<uint8_t> challenge = {17, 18}; |
| 129 | vector<uint8_t> proofOfOwnershipSignature; |
| 130 | ASSERT_TRUE(credential->proveOwnership(challenge, &proofOfOwnershipSignature).isOk()); |
| 131 | optional<vector<uint8_t>> proofOfOwnership = |
| 132 | support::coseSignGetPayload(proofOfOwnershipSignature); |
| 133 | ASSERT_TRUE(proofOfOwnership); |
Max Bires | a3c7f4c | 2021-04-09 08:56:40 -0700 | [diff] [blame] | 134 | string cborPretty = cppbor::prettyPrint(proofOfOwnership.value(), 32, {}); |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 135 | EXPECT_EQ("['ProofOfOwnership', 'org.iso.18013-5.2019.mdl', {0x11, 0x12}, true, ]", cborPretty); |
| 136 | EXPECT_TRUE(support::coseCheckEcDsaSignature(proofOfOwnershipSignature, {}, // Additional data |
| 137 | credentialPubKey_)); |
| 138 | } |
| 139 | |
| 140 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ProveOwnershipTests); |
| 141 | INSTANTIATE_TEST_SUITE_P( |
| 142 | Identity, ProveOwnershipTests, |
| 143 | testing::ValuesIn(android::getAidlHalInstanceNames(IIdentityCredentialStore::descriptor)), |
| 144 | android::PrintInstanceNameToString); |
| 145 | |
| 146 | } // namespace android::hardware::identity |