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 "TestCredentialTests"
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 AuthenticationKeyTests : public testing::TestWithParam<string> {
56 public:
SetUp()57 virtual void SetUp() override {
58 string halInstanceName = GetParam();
59 credentialStore_ = android::waitForDeclaredService<IIdentityCredentialStore>(
60 String16(halInstanceName.c_str()));
61 ASSERT_NE(credentialStore_, nullptr);
62 halApiVersion_ = credentialStore_->getInterfaceVersion();
63 }
64
65 sp<IIdentityCredentialStore> credentialStore_;
66 int halApiVersion_;
67 };
68
TEST_P(AuthenticationKeyTests,proofOfProvisionInAuthKeyCert)69 TEST_P(AuthenticationKeyTests, proofOfProvisionInAuthKeyCert) {
70 if (halApiVersion_ < 3) {
71 GTEST_SKIP() << "Need HAL API version 3, have " << halApiVersion_;
72 }
73
74 string docType = "org.iso.18013-5.2019.mdl";
75 sp<IWritableIdentityCredential> wc;
76 ASSERT_TRUE(credentialStore_
77 ->createCredential(docType,
78 true, // testCredential
79 &wc)
80 .isOk());
81
82 vector<uint8_t> attestationApplicationId = {};
83 vector<uint8_t> attestationChallenge = {1};
84 vector<Certificate> certChain;
85 ASSERT_TRUE(wc->getAttestationCertificate(attestationApplicationId, attestationChallenge,
86 &certChain)
87 .isOk());
88
89 optional<vector<uint8_t>> optCredentialPubKey =
90 support::certificateChainGetTopMostKey(certChain[0].encodedCertificate);
91 ASSERT_TRUE(optCredentialPubKey);
92 vector<uint8_t> credentialPubKey;
93 credentialPubKey = optCredentialPubKey.value();
94
95 size_t proofOfProvisioningSize = 112;
96 // Not in v1 HAL, may fail
97 wc->setExpectedProofOfProvisioningSize(proofOfProvisioningSize);
98
99 ASSERT_TRUE(wc->startPersonalization(1 /* numAccessControlProfiles */,
100 {1} /* numDataElementsPerNamespace */)
101 .isOk());
102
103 // Access control profile 0: open access - don't care about the returned SACP
104 SecureAccessControlProfile sacp;
105 ASSERT_TRUE(wc->addAccessControlProfile(1, {}, false, 0, 0, &sacp).isOk());
106
107 // Single entry - don't care about the returned encrypted data
108 vector<uint8_t> encryptedData;
109 vector<uint8_t> tstrLastName = cppbor::Tstr("Turing").encode();
110 ASSERT_TRUE(wc->beginAddEntry({1}, "ns", "Last name", tstrLastName.size()).isOk());
111 ASSERT_TRUE(wc->addEntryValue(tstrLastName, &encryptedData).isOk());
112
113 vector<uint8_t> proofOfProvisioningSignature;
114 vector<uint8_t> credentialData;
115 Status status = wc->finishAddingEntries(&credentialData, &proofOfProvisioningSignature);
116 EXPECT_TRUE(status.isOk()) << status.exceptionCode() << ": " << status.exceptionMessage();
117
118 optional<vector<uint8_t>> proofOfProvisioning =
119 support::coseSignGetPayload(proofOfProvisioningSignature);
120 ASSERT_TRUE(proofOfProvisioning);
121 string cborPretty = cppbor::prettyPrint(proofOfProvisioning.value(), 32, {});
122 EXPECT_EQ(
123 "[\n"
124 " 'ProofOfProvisioning',\n"
125 " 'org.iso.18013-5.2019.mdl',\n"
126 " [\n"
127 " {\n"
128 " 'id' : 1,\n"
129 " },\n"
130 " ],\n"
131 " {\n"
132 " 'ns' : [\n"
133 " {\n"
134 " 'name' : 'Last name',\n"
135 " 'value' : 'Turing',\n"
136 " 'accessControlProfiles' : [1, ],\n"
137 " },\n"
138 " ],\n"
139 " },\n"
140 " true,\n"
141 "]",
142 cborPretty);
143 // Make sure it's signed by the CredentialKey in the returned cert chain.
144 EXPECT_TRUE(support::coseCheckEcDsaSignature(proofOfProvisioningSignature,
145 {}, // Additional data
146 credentialPubKey));
147
148 // Now get a credential and have it create AuthenticationKey so we can check
149 // the certificate.
150 sp<IIdentityCredential> credential;
151 ASSERT_TRUE(credentialStore_
152 ->getCredential(
153 CipherSuite::CIPHERSUITE_ECDHE_HKDF_ECDSA_WITH_AES_256_GCM_SHA256,
154 credentialData, &credential)
155 .isOk());
156 vector<uint8_t> signingKeyBlob;
157 Certificate signingKeyCertificate;
158 ASSERT_TRUE(credential->generateSigningKeyPair(&signingKeyBlob, &signingKeyCertificate).isOk());
159 optional<vector<uint8_t>> signingPubKey =
160 support::certificateChainGetTopMostKey(signingKeyCertificate.encodedCertificate);
161 EXPECT_TRUE(signingPubKey);
162
163 // SHA-256(ProofOfProvisioning) is embedded in CBOR with the following CDDL
164 //
165 // ProofOfBinding = [
166 // "ProofOfBinding",
167 // bstr, // Contains the SHA-256 of ProofOfProvisioning
168 // ]
169 //
170 // Check that.
171 //
172 optional<vector<uint8_t>> proofOfBinding = support::certificateGetExtension(
173 signingKeyCertificate.encodedCertificate, "1.3.6.1.4.1.11129.2.1.26");
174 ASSERT_TRUE(proofOfBinding);
175 auto [item, _, message] = cppbor::parse(proofOfBinding.value());
176 ASSERT_NE(item, nullptr) << message;
177 const cppbor::Array* arrayItem = item->asArray();
178 ASSERT_NE(arrayItem, nullptr);
179 ASSERT_EQ(arrayItem->size(), 2);
180 const cppbor::Tstr* strItem = (*arrayItem)[0]->asTstr();
181 ASSERT_NE(strItem, nullptr);
182 EXPECT_EQ(strItem->value(), "ProofOfBinding");
183 const cppbor::Bstr* popSha256Item = (*arrayItem)[1]->asBstr();
184 ASSERT_NE(popSha256Item, nullptr);
185 EXPECT_EQ(popSha256Item->value(), support::sha256(proofOfProvisioning.value()));
186 }
187
188 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AuthenticationKeyTests);
189 INSTANTIATE_TEST_SUITE_P(
190 Identity, AuthenticationKeyTests,
191 testing::ValuesIn(android::getAidlHalInstanceNames(IIdentityCredentialStore::descriptor)),
192 android::PrintInstanceNameToString);
193
194 } // namespace android::hardware::identity
195