| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [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 | */ | 
|  | 16 |  | 
|  | 17 | #define LOG_TAG "WritableCredential" | 
|  | 18 |  | 
|  | 19 | #include <android-base/logging.h> | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 20 | #include <android/hardware/identity/support/IdentityCredentialSupport.h> | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 21 | #include <android/security/identity/ICredentialStore.h> | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 22 | #include <binder/IPCThreadState.h> | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 23 | #include <cppbor.h> | 
|  | 24 | #include <cppbor_parse.h> | 
| David Zeuthen | f2a2867 | 2020-01-30 16:20:07 -0500 | [diff] [blame] | 25 | #include <keystore/keystore_attestation_id.h> | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 26 |  | 
|  | 27 | #include "CredentialData.h" | 
|  | 28 | #include "Util.h" | 
|  | 29 | #include "WritableCredential.h" | 
|  | 30 |  | 
|  | 31 | namespace android { | 
|  | 32 | namespace security { | 
|  | 33 | namespace identity { | 
|  | 34 |  | 
|  | 35 | using ::std::pair; | 
|  | 36 |  | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 37 | using ::android::hardware::identity::SecureAccessControlProfile; | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 38 |  | 
|  | 39 | using ::android::hardware::identity::support::chunkVector; | 
|  | 40 |  | 
|  | 41 | WritableCredential::WritableCredential(const string& dataPath, const string& credentialName, | 
| David Zeuthen | e2a78a4 | 2020-04-27 13:34:38 -0400 | [diff] [blame] | 42 | const string& docType, size_t dataChunkSize, | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 43 | sp<IWritableIdentityCredential> halBinder) | 
| David Zeuthen | e2a78a4 | 2020-04-27 13:34:38 -0400 | [diff] [blame] | 44 | : dataPath_(dataPath), credentialName_(credentialName), docType_(docType), | 
|  | 45 | dataChunkSize_(dataChunkSize), halBinder_(halBinder) {} | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 46 |  | 
|  | 47 | WritableCredential::~WritableCredential() {} | 
|  | 48 |  | 
|  | 49 | Status WritableCredential::ensureAttestationCertificateExists(const vector<uint8_t>& challenge) { | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 50 | if (!attestationCertificate_.empty()) { | 
|  | 51 | return Status::ok(); | 
|  | 52 | } | 
|  | 53 |  | 
| David Zeuthen | f2a2867 | 2020-01-30 16:20:07 -0500 | [diff] [blame] | 54 | 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 Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 62 | vector<Certificate> certificateChain; | 
|  | 63 | Status status = halBinder_->getAttestationCertificate(asn1AttestationId.value(), challenge, | 
|  | 64 | &certificateChain); | 
|  | 65 | if (!status.isOk()) { | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 66 | LOG(ERROR) << "Error calling getAttestationCertificate()"; | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 67 | return halStatusToGenericError(status); | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 68 | } | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 69 |  | 
|  | 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 Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 77 | return Status::ok(); | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | Status 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 |  | 
| David Zeuthen | e2a78a4 | 2020-04-27 13:34:38 -0400 | [diff] [blame] | 92 | ssize_t WritableCredential::calcExpectedProofOfProvisioningSize( | 
|  | 93 | const vector<AccessControlProfileParcel>& accessControlProfiles, | 
|  | 94 | const vector<EntryNamespaceParcel>& entryNamespaces) { | 
|  | 95 |  | 
|  | 96 | // Right now, we calculate the size by simply just calculating the | 
|  | 97 | // CBOR. There's a little bit of overhead associated with this (as compared | 
|  | 98 | // to just adding up sizes) but it's a lot simpler and robust. In the future | 
|  | 99 | // if this turns out to be a problem, we can optimize it. | 
|  | 100 | // | 
|  | 101 |  | 
|  | 102 | cppbor::Array acpArray; | 
|  | 103 | for (const AccessControlProfileParcel& profile : accessControlProfiles) { | 
|  | 104 | cppbor::Map map; | 
|  | 105 | map.add("id", profile.id); | 
|  | 106 | if (profile.readerCertificate.size() > 0) { | 
|  | 107 | map.add("readerCertificate", cppbor::Bstr(profile.readerCertificate)); | 
|  | 108 | } | 
|  | 109 | if (profile.userAuthenticationRequired) { | 
|  | 110 | map.add("userAuthenticationRequired", profile.userAuthenticationRequired); | 
|  | 111 | map.add("timeoutMillis", profile.userAuthenticationTimeoutMillis); | 
|  | 112 | } | 
|  | 113 | acpArray.add(std::move(map)); | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | cppbor::Map dataMap; | 
|  | 117 | for (const EntryNamespaceParcel& ensParcel : entryNamespaces) { | 
|  | 118 | cppbor::Array entriesArray; | 
|  | 119 | for (const EntryParcel& eParcel : ensParcel.entries) { | 
|  | 120 | // TODO: ideally do do this without parsing the data (but still validate data is valid | 
|  | 121 | // CBOR). | 
|  | 122 | auto [itemForValue, _, _2] = cppbor::parse(eParcel.value); | 
|  | 123 | if (itemForValue == nullptr) { | 
|  | 124 | return -1; | 
|  | 125 | } | 
|  | 126 | cppbor::Map entryMap; | 
|  | 127 | entryMap.add("name", eParcel.name); | 
|  | 128 | entryMap.add("value", std::move(itemForValue)); | 
|  | 129 | cppbor::Array acpIdsArray; | 
|  | 130 | for (int32_t id : eParcel.accessControlProfileIds) { | 
|  | 131 | acpIdsArray.add(id); | 
|  | 132 | } | 
|  | 133 | entryMap.add("accessControlProfiles", std::move(acpIdsArray)); | 
|  | 134 | entriesArray.add(std::move(entryMap)); | 
|  | 135 | } | 
|  | 136 | dataMap.add(ensParcel.namespaceName, std::move(entriesArray)); | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | cppbor::Array array; | 
|  | 140 | array.add("ProofOfProvisioning"); | 
|  | 141 | array.add(docType_); | 
|  | 142 | array.add(std::move(acpArray)); | 
|  | 143 | array.add(std::move(dataMap)); | 
|  | 144 | array.add(false);  // testCredential | 
|  | 145 | return array.encode().size(); | 
|  | 146 | } | 
|  | 147 |  | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 148 | Status | 
|  | 149 | WritableCredential::personalize(const vector<AccessControlProfileParcel>& accessControlProfiles, | 
|  | 150 | const vector<EntryNamespaceParcel>& entryNamespaces, | 
|  | 151 | int64_t secureUserId, vector<uint8_t>* _aidl_return) { | 
| David Zeuthen | 673c6ce | 2020-06-03 17:24:37 -0400 | [diff] [blame] | 152 | Status ensureStatus = ensureAttestationCertificateExists({0x00});  // Challenge cannot be empty. | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 153 | if (!ensureStatus.isOk()) { | 
|  | 154 | return ensureStatus; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | uid_t callingUid = android::IPCThreadState::self()->getCallingUid(); | 
|  | 158 | CredentialData data = CredentialData(dataPath_, callingUid, credentialName_); | 
|  | 159 |  | 
|  | 160 | // Note: The value 0 is used to convey that no user-authentication is needed for this | 
|  | 161 | // credential. This is to allow creating credentials w/o user authentication on devices | 
|  | 162 | // where Secure lock screen is not enabled. | 
|  | 163 | data.setSecureUserId(secureUserId); | 
|  | 164 |  | 
|  | 165 | data.setAttestationCertificate(attestationCertificate_); | 
|  | 166 |  | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 167 | vector<int32_t> entryCounts; | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 168 | for (const EntryNamespaceParcel& ensParcel : entryNamespaces) { | 
|  | 169 | entryCounts.push_back(ensParcel.entries.size()); | 
|  | 170 | } | 
|  | 171 |  | 
| David Zeuthen | e2a78a4 | 2020-04-27 13:34:38 -0400 | [diff] [blame] | 172 | ssize_t expectedPoPSize = | 
|  | 173 | calcExpectedProofOfProvisioningSize(accessControlProfiles, entryNamespaces); | 
|  | 174 | if (expectedPoPSize < 0) { | 
|  | 175 | return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC, | 
|  | 176 | "Data is not valid CBOR"); | 
|  | 177 | } | 
|  | 178 | // This is not catastrophic, we might be dealing with a version 1 implementation which | 
|  | 179 | // doesn't have this method. | 
|  | 180 | Status status = halBinder_->setExpectedProofOfProvisioningSize(expectedPoPSize); | 
|  | 181 | if (!status.isOk()) { | 
|  | 182 | LOG(INFO) << "Failed setting expected ProofOfProvisioning size, assuming V1 HAL " | 
|  | 183 | << "and continuing"; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | status = halBinder_->startPersonalization(accessControlProfiles.size(), entryCounts); | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 187 | if (!status.isOk()) { | 
|  | 188 | return halStatusToGenericError(status); | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 189 | } | 
|  | 190 |  | 
|  | 191 | for (const AccessControlProfileParcel& acpParcel : accessControlProfiles) { | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 192 | Certificate certificate; | 
|  | 193 | certificate.encodedCertificate = acpParcel.readerCertificate; | 
|  | 194 | SecureAccessControlProfile profile; | 
|  | 195 | status = halBinder_->addAccessControlProfile( | 
|  | 196 | acpParcel.id, certificate, acpParcel.userAuthenticationRequired, | 
|  | 197 | acpParcel.userAuthenticationTimeoutMillis, secureUserId, &profile); | 
|  | 198 | if (!status.isOk()) { | 
|  | 199 | return halStatusToGenericError(status); | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 200 | } | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 201 | data.addSecureAccessControlProfile(profile); | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 202 | } | 
|  | 203 |  | 
|  | 204 | for (const EntryNamespaceParcel& ensParcel : entryNamespaces) { | 
|  | 205 | for (const EntryParcel& eParcel : ensParcel.entries) { | 
|  | 206 | vector<vector<uint8_t>> chunks = chunkVector(eParcel.value, dataChunkSize_); | 
|  | 207 |  | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 208 | vector<int32_t> ids; | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 209 | std::copy(eParcel.accessControlProfileIds.begin(), | 
|  | 210 | eParcel.accessControlProfileIds.end(), std::back_inserter(ids)); | 
|  | 211 |  | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 212 | status = halBinder_->beginAddEntry(ids, ensParcel.namespaceName, eParcel.name, | 
|  | 213 | eParcel.value.size()); | 
|  | 214 | if (!status.isOk()) { | 
|  | 215 | return halStatusToGenericError(status); | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 216 | } | 
|  | 217 |  | 
|  | 218 | vector<vector<uint8_t>> encryptedChunks; | 
|  | 219 | for (const auto& chunk : chunks) { | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 220 | vector<uint8_t> encryptedChunk; | 
|  | 221 | status = halBinder_->addEntryValue(chunk, &encryptedChunk); | 
|  | 222 | if (!status.isOk()) { | 
|  | 223 | return halStatusToGenericError(status); | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 224 | } | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 225 | encryptedChunks.push_back(encryptedChunk); | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 226 | } | 
|  | 227 | EntryData eData; | 
|  | 228 | eData.size = eParcel.value.size(); | 
|  | 229 | eData.accessControlProfileIds = std::move(ids); | 
|  | 230 | eData.encryptedChunks = std::move(encryptedChunks); | 
|  | 231 | data.addEntryData(ensParcel.namespaceName, eParcel.name, eData); | 
|  | 232 | } | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | vector<uint8_t> credentialData; | 
|  | 236 | vector<uint8_t> proofOfProvisioningSignature; | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 237 | status = halBinder_->finishAddingEntries(&credentialData, &proofOfProvisioningSignature); | 
|  | 238 | if (!status.isOk()) { | 
|  | 239 | return halStatusToGenericError(status); | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 240 | } | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 241 | data.setCredentialData(credentialData); | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 242 |  | 
|  | 243 | if (!data.saveToDisk()) { | 
|  | 244 | return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC, | 
|  | 245 | "Error saving credential data to disk"); | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | *_aidl_return = proofOfProvisioningSignature; | 
|  | 249 | return Status::ok(); | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | }  // namespace identity | 
|  | 253 | }  // namespace security | 
|  | 254 | }  // namespace android |