blob: 86c604d2e3499cfbd38dd4d0a43971e605fe9945 [file] [log] [blame]
David Zeuthenab3e5652019-10-28 13:32:48 -04001/*
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
17#define LOG_TAG "WritableCredential"
18
19#include <android-base/logging.h>
David Zeuthenab3e5652019-10-28 13:32:48 -040020#include <android/hardware/identity/support/IdentityCredentialSupport.h>
David Zeuthenab3e5652019-10-28 13:32:48 -040021#include <android/security/identity/ICredentialStore.h>
David Zeuthenab3e5652019-10-28 13:32:48 -040022#include <binder/IPCThreadState.h>
David Zeuthenab3e5652019-10-28 13:32:48 -040023#include <cppbor.h>
24#include <cppbor_parse.h>
David Zeuthenf2a28672020-01-30 16:20:07 -050025#include <keystore/keystore_attestation_id.h>
David Zeuthenab3e5652019-10-28 13:32:48 -040026
27#include "CredentialData.h"
28#include "Util.h"
29#include "WritableCredential.h"
30
31namespace android {
32namespace security {
33namespace identity {
34
35using ::std::pair;
36
37using ::android::hardware::hidl_vec;
38
39using ::android::hardware::identity::V1_0::Result;
40using ::android::hardware::identity::V1_0::ResultCode;
41using ::android::hardware::identity::V1_0::SecureAccessControlProfile;
42
43using ::android::hardware::identity::support::chunkVector;
44
45WritableCredential::WritableCredential(const string& dataPath, const string& credentialName,
46 const string& /*docType*/, size_t dataChunkSize,
47 sp<IWritableIdentityCredential> halBinder)
48 : dataPath_(dataPath), credentialName_(credentialName), dataChunkSize_(dataChunkSize),
49 halBinder_(halBinder) {}
50
51WritableCredential::~WritableCredential() {}
52
53Status WritableCredential::ensureAttestationCertificateExists(const vector<uint8_t>& challenge) {
54 vector<uint8_t> attestationCertificate;
55
56 if (!attestationCertificate_.empty()) {
57 return Status::ok();
58 }
59
David Zeuthenf2a28672020-01-30 16:20:07 -050060 const int32_t callingUid = IPCThreadState::self()->getCallingUid();
61 auto asn1AttestationId = android::security::gather_attestation_application_id(callingUid);
62 if (!asn1AttestationId.isOk()) {
63 LOG(ERROR) << "Failed gathering AttestionApplicationId";
64 return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC,
65 "Failed gathering AttestionApplicationId");
66 }
67
David Zeuthenab3e5652019-10-28 13:32:48 -040068 Result result;
69 halBinder_->getAttestationCertificate(
David Zeuthenf2a28672020-01-30 16:20:07 -050070 asn1AttestationId.value(), challenge,
71 [&](const Result& _result, const hidl_vec<hidl_vec<uint8_t>>& _splitCerts) {
David Zeuthenab3e5652019-10-28 13:32:48 -040072 result = _result;
David Zeuthenf2a28672020-01-30 16:20:07 -050073 vector<vector<uint8_t>> splitCerts;
74 std::copy(_splitCerts.begin(), _splitCerts.end(), std::back_inserter(splitCerts));
75 attestationCertificate =
76 ::android::hardware::identity::support::certificateChainJoin(splitCerts);
David Zeuthenab3e5652019-10-28 13:32:48 -040077 });
78 if (result.code != ResultCode::OK) {
79 LOG(ERROR) << "Error calling getAttestationCertificate()";
80 return halResultToGenericError(result);
81 }
82 attestationCertificate_ = attestationCertificate;
83 return Status::ok();
84}
85
86Status WritableCredential::getCredentialKeyCertificateChain(const vector<uint8_t>& challenge,
87 vector<uint8_t>* _aidl_return) {
88
89 Status ensureStatus = ensureAttestationCertificateExists(challenge);
90 if (!ensureStatus.isOk()) {
91 return ensureStatus;
92 }
93
94 *_aidl_return = attestationCertificate_;
95 return Status::ok();
96}
97
98Status
99WritableCredential::personalize(const vector<AccessControlProfileParcel>& accessControlProfiles,
100 const vector<EntryNamespaceParcel>& entryNamespaces,
101 int64_t secureUserId, vector<uint8_t>* _aidl_return) {
102 Status ensureStatus = ensureAttestationCertificateExists({});
103 if (!ensureStatus.isOk()) {
104 return ensureStatus;
105 }
106
107 uid_t callingUid = android::IPCThreadState::self()->getCallingUid();
108 CredentialData data = CredentialData(dataPath_, callingUid, credentialName_);
109
110 // Note: The value 0 is used to convey that no user-authentication is needed for this
111 // credential. This is to allow creating credentials w/o user authentication on devices
112 // where Secure lock screen is not enabled.
113 data.setSecureUserId(secureUserId);
114
115 data.setAttestationCertificate(attestationCertificate_);
116
117 vector<uint16_t> entryCounts;
118 for (const EntryNamespaceParcel& ensParcel : entryNamespaces) {
119 entryCounts.push_back(ensParcel.entries.size());
120 }
121
122 Result result;
123 halBinder_->startPersonalization(accessControlProfiles.size(), entryCounts,
124 [&](const Result& _result) { result = _result; });
125 if (result.code != ResultCode::OK) {
126 return halResultToGenericError(result);
127 }
128
129 for (const AccessControlProfileParcel& acpParcel : accessControlProfiles) {
130 halBinder_->addAccessControlProfile(
131 acpParcel.id, acpParcel.readerCertificate, acpParcel.userAuthenticationRequired,
132 acpParcel.userAuthenticationTimeoutMillis, secureUserId,
133 [&](const Result& _result, const SecureAccessControlProfile& profile) {
134 data.addSecureAccessControlProfile(profile);
135 result = _result;
136 });
137 if (result.code != ResultCode::OK) {
138 return halResultToGenericError(result);
139 }
140 }
141
142 for (const EntryNamespaceParcel& ensParcel : entryNamespaces) {
143 for (const EntryParcel& eParcel : ensParcel.entries) {
144 vector<vector<uint8_t>> chunks = chunkVector(eParcel.value, dataChunkSize_);
145
146 vector<uint16_t> ids;
147 std::copy(eParcel.accessControlProfileIds.begin(),
148 eParcel.accessControlProfileIds.end(), std::back_inserter(ids));
149
150 halBinder_->beginAddEntry(ids, ensParcel.namespaceName, eParcel.name,
151 eParcel.value.size(),
152 [&](const Result& _result) { result = _result; });
153 if (result.code != ResultCode::OK) {
154 return halResultToGenericError(result);
155 }
156
157 vector<vector<uint8_t>> encryptedChunks;
158 for (const auto& chunk : chunks) {
159 halBinder_->addEntryValue(
160 chunk, [&](const Result& _result, const hidl_vec<uint8_t>& encryptedContent) {
161 result = _result;
162 encryptedChunks.push_back(encryptedContent);
163 });
164 if (result.code != ResultCode::OK) {
165 return halResultToGenericError(result);
166 }
167 }
168 EntryData eData;
169 eData.size = eParcel.value.size();
170 eData.accessControlProfileIds = std::move(ids);
171 eData.encryptedChunks = std::move(encryptedChunks);
172 data.addEntryData(ensParcel.namespaceName, eParcel.name, eData);
173 }
174 }
175
176 vector<uint8_t> credentialData;
177 vector<uint8_t> proofOfProvisioningSignature;
178 halBinder_->finishAddingEntries([&](const Result& _result,
179 const hidl_vec<uint8_t>& _credentialData,
180 const hidl_vec<uint8_t>& _proofOfProvisioningSignature) {
181 data.setCredentialData(_credentialData);
182 result = _result;
183 credentialData = _credentialData;
184 proofOfProvisioningSignature = _proofOfProvisioningSignature;
185 });
186 if (result.code != ResultCode::OK) {
187 return halResultToGenericError(result);
188 }
189
190 if (!data.saveToDisk()) {
191 return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC,
192 "Error saving credential data to disk");
193 }
194
195 *_aidl_return = proofOfProvisioningSignature;
196 return Status::ok();
197}
198
199} // namespace identity
200} // namespace security
201} // namespace android