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 | |
| 37 | using ::android::hardware::hidl_vec; |
| 38 | |
| 39 | using ::android::hardware::identity::V1_0::Result; |
| 40 | using ::android::hardware::identity::V1_0::ResultCode; |
| 41 | using ::android::hardware::identity::V1_0::SecureAccessControlProfile; |
| 42 | |
| 43 | using ::android::hardware::identity::support::chunkVector; |
| 44 | |
| 45 | WritableCredential::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 | |
| 51 | WritableCredential::~WritableCredential() {} |
| 52 | |
| 53 | Status WritableCredential::ensureAttestationCertificateExists(const vector<uint8_t>& challenge) { |
| 54 | vector<uint8_t> attestationCertificate; |
| 55 | |
| 56 | if (!attestationCertificate_.empty()) { |
| 57 | return Status::ok(); |
| 58 | } |
| 59 | |
David Zeuthen | f2a2867 | 2020-01-30 16:20:07 -0500 | [diff] [blame^] | 60 | 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 Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 68 | Result result; |
| 69 | halBinder_->getAttestationCertificate( |
David Zeuthen | f2a2867 | 2020-01-30 16:20:07 -0500 | [diff] [blame^] | 70 | asn1AttestationId.value(), challenge, |
| 71 | [&](const Result& _result, const hidl_vec<hidl_vec<uint8_t>>& _splitCerts) { |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 72 | result = _result; |
David Zeuthen | f2a2867 | 2020-01-30 16:20:07 -0500 | [diff] [blame^] | 73 | 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 Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 77 | }); |
| 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 | |
| 86 | Status 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 | |
| 98 | Status |
| 99 | WritableCredential::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 |