blob: dec95a6ae98ccab722100ad5db934cfbda841a3b [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
David Zeuthena6f9fba2020-02-11 22:08:27 -050037using ::android::hardware::identity::SecureAccessControlProfile;
David Zeuthenab3e5652019-10-28 13:32:48 -040038
39using ::android::hardware::identity::support::chunkVector;
40
41WritableCredential::WritableCredential(const string& dataPath, const string& credentialName,
42 const string& /*docType*/, size_t dataChunkSize,
43 sp<IWritableIdentityCredential> halBinder)
44 : dataPath_(dataPath), credentialName_(credentialName), dataChunkSize_(dataChunkSize),
45 halBinder_(halBinder) {}
46
47WritableCredential::~WritableCredential() {}
48
49Status WritableCredential::ensureAttestationCertificateExists(const vector<uint8_t>& challenge) {
David Zeuthenab3e5652019-10-28 13:32:48 -040050 if (!attestationCertificate_.empty()) {
51 return Status::ok();
52 }
53
David Zeuthenf2a28672020-01-30 16:20:07 -050054 const int32_t callingUid = IPCThreadState::self()->getCallingUid();
55 auto asn1AttestationId = android::security::gather_attestation_application_id(callingUid);
56 if (!asn1AttestationId.isOk()) {
57 LOG(ERROR) << "Failed gathering AttestionApplicationId";
58 return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC,
59 "Failed gathering AttestionApplicationId");
60 }
61
David Zeuthena6f9fba2020-02-11 22:08:27 -050062 vector<Certificate> certificateChain;
63 Status status = halBinder_->getAttestationCertificate(asn1AttestationId.value(), challenge,
64 &certificateChain);
65 if (!status.isOk()) {
David Zeuthenab3e5652019-10-28 13:32:48 -040066 LOG(ERROR) << "Error calling getAttestationCertificate()";
David Zeuthena6f9fba2020-02-11 22:08:27 -050067 return halStatusToGenericError(status);
David Zeuthenab3e5652019-10-28 13:32:48 -040068 }
David Zeuthena6f9fba2020-02-11 22:08:27 -050069
70 vector<vector<uint8_t>> splitCerts;
71 for (const auto& cert : certificateChain) {
72 splitCerts.push_back(cert.encodedCertificate);
73 }
74 attestationCertificate_ =
75 ::android::hardware::identity::support::certificateChainJoin(splitCerts);
76
David Zeuthenab3e5652019-10-28 13:32:48 -040077 return Status::ok();
78}
79
80Status WritableCredential::getCredentialKeyCertificateChain(const vector<uint8_t>& challenge,
81 vector<uint8_t>* _aidl_return) {
82
83 Status ensureStatus = ensureAttestationCertificateExists(challenge);
84 if (!ensureStatus.isOk()) {
85 return ensureStatus;
86 }
87
88 *_aidl_return = attestationCertificate_;
89 return Status::ok();
90}
91
92Status
93WritableCredential::personalize(const vector<AccessControlProfileParcel>& accessControlProfiles,
94 const vector<EntryNamespaceParcel>& entryNamespaces,
95 int64_t secureUserId, vector<uint8_t>* _aidl_return) {
96 Status ensureStatus = ensureAttestationCertificateExists({});
97 if (!ensureStatus.isOk()) {
98 return ensureStatus;
99 }
100
101 uid_t callingUid = android::IPCThreadState::self()->getCallingUid();
102 CredentialData data = CredentialData(dataPath_, callingUid, credentialName_);
103
104 // Note: The value 0 is used to convey that no user-authentication is needed for this
105 // credential. This is to allow creating credentials w/o user authentication on devices
106 // where Secure lock screen is not enabled.
107 data.setSecureUserId(secureUserId);
108
109 data.setAttestationCertificate(attestationCertificate_);
110
David Zeuthena6f9fba2020-02-11 22:08:27 -0500111 vector<int32_t> entryCounts;
David Zeuthenab3e5652019-10-28 13:32:48 -0400112 for (const EntryNamespaceParcel& ensParcel : entryNamespaces) {
113 entryCounts.push_back(ensParcel.entries.size());
114 }
115
David Zeuthena6f9fba2020-02-11 22:08:27 -0500116 Status status = halBinder_->startPersonalization(accessControlProfiles.size(), entryCounts);
117 if (!status.isOk()) {
118 return halStatusToGenericError(status);
David Zeuthenab3e5652019-10-28 13:32:48 -0400119 }
120
121 for (const AccessControlProfileParcel& acpParcel : accessControlProfiles) {
David Zeuthena6f9fba2020-02-11 22:08:27 -0500122 Certificate certificate;
123 certificate.encodedCertificate = acpParcel.readerCertificate;
124 SecureAccessControlProfile profile;
125 status = halBinder_->addAccessControlProfile(
126 acpParcel.id, certificate, acpParcel.userAuthenticationRequired,
127 acpParcel.userAuthenticationTimeoutMillis, secureUserId, &profile);
128 if (!status.isOk()) {
129 return halStatusToGenericError(status);
David Zeuthenab3e5652019-10-28 13:32:48 -0400130 }
David Zeuthena6f9fba2020-02-11 22:08:27 -0500131 data.addSecureAccessControlProfile(profile);
David Zeuthenab3e5652019-10-28 13:32:48 -0400132 }
133
134 for (const EntryNamespaceParcel& ensParcel : entryNamespaces) {
135 for (const EntryParcel& eParcel : ensParcel.entries) {
136 vector<vector<uint8_t>> chunks = chunkVector(eParcel.value, dataChunkSize_);
137
David Zeuthena6f9fba2020-02-11 22:08:27 -0500138 vector<int32_t> ids;
David Zeuthenab3e5652019-10-28 13:32:48 -0400139 std::copy(eParcel.accessControlProfileIds.begin(),
140 eParcel.accessControlProfileIds.end(), std::back_inserter(ids));
141
David Zeuthena6f9fba2020-02-11 22:08:27 -0500142 status = halBinder_->beginAddEntry(ids, ensParcel.namespaceName, eParcel.name,
143 eParcel.value.size());
144 if (!status.isOk()) {
145 return halStatusToGenericError(status);
David Zeuthenab3e5652019-10-28 13:32:48 -0400146 }
147
148 vector<vector<uint8_t>> encryptedChunks;
149 for (const auto& chunk : chunks) {
David Zeuthena6f9fba2020-02-11 22:08:27 -0500150 vector<uint8_t> encryptedChunk;
151 status = halBinder_->addEntryValue(chunk, &encryptedChunk);
152 if (!status.isOk()) {
153 return halStatusToGenericError(status);
David Zeuthenab3e5652019-10-28 13:32:48 -0400154 }
David Zeuthena6f9fba2020-02-11 22:08:27 -0500155 encryptedChunks.push_back(encryptedChunk);
David Zeuthenab3e5652019-10-28 13:32:48 -0400156 }
157 EntryData eData;
158 eData.size = eParcel.value.size();
159 eData.accessControlProfileIds = std::move(ids);
160 eData.encryptedChunks = std::move(encryptedChunks);
161 data.addEntryData(ensParcel.namespaceName, eParcel.name, eData);
162 }
163 }
164
165 vector<uint8_t> credentialData;
166 vector<uint8_t> proofOfProvisioningSignature;
David Zeuthena6f9fba2020-02-11 22:08:27 -0500167 status = halBinder_->finishAddingEntries(&credentialData, &proofOfProvisioningSignature);
168 if (!status.isOk()) {
169 return halStatusToGenericError(status);
David Zeuthenab3e5652019-10-28 13:32:48 -0400170 }
David Zeuthena6f9fba2020-02-11 22:08:27 -0500171 data.setCredentialData(credentialData);
David Zeuthenab3e5652019-10-28 13:32:48 -0400172
173 if (!data.saveToDisk()) {
174 return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC,
175 "Error saving credential data to disk");
176 }
177
178 *_aidl_return = proofOfProvisioningSignature;
179 return Status::ok();
180}
181
182} // namespace identity
183} // namespace security
184} // namespace android