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, |
| 42 | const string& /*docType*/, size_t dataChunkSize, |
| 43 | sp<IWritableIdentityCredential> halBinder) |
| 44 | : dataPath_(dataPath), credentialName_(credentialName), dataChunkSize_(dataChunkSize), |
| 45 | halBinder_(halBinder) {} |
| 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 | |
| 92 | Status |
| 93 | WritableCredential::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 Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame^] | 111 | vector<int32_t> entryCounts; |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 112 | for (const EntryNamespaceParcel& ensParcel : entryNamespaces) { |
| 113 | entryCounts.push_back(ensParcel.entries.size()); |
| 114 | } |
| 115 | |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame^] | 116 | Status status = halBinder_->startPersonalization(accessControlProfiles.size(), entryCounts); |
| 117 | if (!status.isOk()) { |
| 118 | return halStatusToGenericError(status); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | for (const AccessControlProfileParcel& acpParcel : accessControlProfiles) { |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame^] | 122 | 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 Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 130 | } |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame^] | 131 | data.addSecureAccessControlProfile(profile); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 132 | } |
| 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 Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame^] | 138 | vector<int32_t> ids; |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 139 | std::copy(eParcel.accessControlProfileIds.begin(), |
| 140 | eParcel.accessControlProfileIds.end(), std::back_inserter(ids)); |
| 141 | |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame^] | 142 | status = halBinder_->beginAddEntry(ids, ensParcel.namespaceName, eParcel.name, |
| 143 | eParcel.value.size()); |
| 144 | if (!status.isOk()) { |
| 145 | return halStatusToGenericError(status); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | vector<vector<uint8_t>> encryptedChunks; |
| 149 | for (const auto& chunk : chunks) { |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame^] | 150 | vector<uint8_t> encryptedChunk; |
| 151 | status = halBinder_->addEntryValue(chunk, &encryptedChunk); |
| 152 | if (!status.isOk()) { |
| 153 | return halStatusToGenericError(status); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 154 | } |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame^] | 155 | encryptedChunks.push_back(encryptedChunk); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 156 | } |
| 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 Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame^] | 167 | status = halBinder_->finishAddingEntries(&credentialData, &proofOfProvisioningSignature); |
| 168 | if (!status.isOk()) { |
| 169 | return halStatusToGenericError(status); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 170 | } |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame^] | 171 | data.setCredentialData(credentialData); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 172 | |
| 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 |