2019-10-28 18:32:48 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019, The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-03-31 16:41:27 +02:00
|
|
|
#define LOG_TAG "credstore"
|
2019-10-28 18:32:48 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
2022-01-19 00:58:47 +01:00
|
|
|
#include <optional>
|
2019-10-28 18:32:48 +01:00
|
|
|
|
|
|
|
#include <android-base/logging.h>
|
2022-01-19 00:58:47 +01:00
|
|
|
#include <android/hardware/security/keymint/IRemotelyProvisionedComponent.h>
|
|
|
|
#include <android/hardware/security/keymint/RpcHardwareInfo.h>
|
2019-10-28 18:32:48 +01:00
|
|
|
#include <binder/IPCThreadState.h>
|
2022-01-19 00:58:47 +01:00
|
|
|
#include <binder/IServiceManager.h>
|
2023-03-08 06:59:31 +01:00
|
|
|
#include <rkp/support/rkpd_client.h>
|
2019-10-28 18:32:48 +01:00
|
|
|
|
|
|
|
#include "Credential.h"
|
2020-02-12 04:08:27 +01:00
|
|
|
#include "CredentialData.h"
|
2019-10-28 18:32:48 +01:00
|
|
|
#include "CredentialStore.h"
|
2021-09-11 19:52:17 +02:00
|
|
|
#include "Session.h"
|
2019-10-28 18:32:48 +01:00
|
|
|
#include "Util.h"
|
|
|
|
#include "WritableCredential.h"
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace security {
|
|
|
|
namespace identity {
|
2022-01-19 00:58:47 +01:00
|
|
|
namespace {
|
|
|
|
|
2023-03-08 06:59:31 +01:00
|
|
|
using ::android::security::rkp::RemotelyProvisionedKey;
|
|
|
|
using ::android::security::rkp::support::getRpcKey;
|
2022-01-19 00:58:47 +01:00
|
|
|
|
|
|
|
} // namespace
|
2019-10-28 18:32:48 +01:00
|
|
|
|
|
|
|
CredentialStore::CredentialStore(const std::string& dataPath, sp<IIdentityCredentialStore> hal)
|
|
|
|
: dataPath_(dataPath), hal_(hal) {}
|
|
|
|
|
|
|
|
bool CredentialStore::init() {
|
2020-02-12 04:08:27 +01:00
|
|
|
Status status = hal_->getHardwareInformation(&hwInfo_);
|
|
|
|
if (!status.isOk()) {
|
|
|
|
LOG(ERROR) << "Error getting hardware information: " << status.toString8();
|
2019-10-28 18:32:48 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-10-16 17:50:13 +02:00
|
|
|
halApiVersion_ = hal_->getInterfaceVersion();
|
2020-02-12 04:08:27 +01:00
|
|
|
|
2022-01-19 00:58:47 +01:00
|
|
|
if (hwInfo_.isRemoteKeyProvisioningSupported) {
|
2022-11-22 19:26:16 +01:00
|
|
|
status = hal_->getRemotelyProvisionedComponent(&rpc_);
|
|
|
|
if (!status.isOk()) {
|
|
|
|
LOG(ERROR) << "Error getting remotely provisioned component: " << status;
|
2022-01-19 00:58:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 17:50:13 +02:00
|
|
|
LOG(INFO) << "Connected to Identity Credential HAL with API version " << halApiVersion_
|
|
|
|
<< " and name '" << hwInfo_.credentialStoreName << "' authored by '"
|
|
|
|
<< hwInfo_.credentialStoreAuthorName << "' with chunk size " << hwInfo_.dataChunkSize
|
2023-01-18 00:37:50 +01:00
|
|
|
<< " directoAccess set to " << (hwInfo_.isDirectAccess ? "true" : "false")
|
|
|
|
<< " and remote key provisioning support "
|
|
|
|
<< (hwInfo_.isRemoteKeyProvisioningSupported ? "enabled" : "disabled");
|
2019-10-28 18:32:48 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CredentialStore::~CredentialStore() {}
|
|
|
|
|
|
|
|
Status CredentialStore::getSecurityHardwareInfo(SecurityHardwareInfoParcel* _aidl_return) {
|
|
|
|
SecurityHardwareInfoParcel info;
|
2020-02-12 04:08:27 +01:00
|
|
|
info.directAccess = hwInfo_.isDirectAccess;
|
|
|
|
info.supportedDocTypes = hwInfo_.supportedDocTypes;
|
2019-10-28 18:32:48 +01:00
|
|
|
*_aidl_return = info;
|
|
|
|
return Status::ok();
|
|
|
|
};
|
|
|
|
|
|
|
|
Status CredentialStore::createCredential(const std::string& credentialName,
|
|
|
|
const std::string& docType,
|
|
|
|
sp<IWritableCredential>* _aidl_return) {
|
|
|
|
uid_t callingUid = android::IPCThreadState::self()->getCallingUid();
|
|
|
|
optional<bool> credentialExists =
|
|
|
|
CredentialData::credentialExists(dataPath_, callingUid, credentialName);
|
|
|
|
if (!credentialExists.has_value()) {
|
|
|
|
return Status::fromServiceSpecificError(
|
|
|
|
ERROR_GENERIC, "Error determining if credential with given name exists");
|
|
|
|
}
|
|
|
|
if (credentialExists.value()) {
|
|
|
|
return Status::fromServiceSpecificError(ERROR_ALREADY_PERSONALIZED,
|
|
|
|
"Credential with given name already exists");
|
|
|
|
}
|
|
|
|
|
2020-02-12 04:08:27 +01:00
|
|
|
if (hwInfo_.supportedDocTypes.size() > 0) {
|
|
|
|
if (std::find(hwInfo_.supportedDocTypes.begin(), hwInfo_.supportedDocTypes.end(),
|
|
|
|
docType) == hwInfo_.supportedDocTypes.end()) {
|
2019-10-28 18:32:48 +01:00
|
|
|
return Status::fromServiceSpecificError(ERROR_DOCUMENT_TYPE_NOT_SUPPORTED,
|
|
|
|
"No support for given document type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<IWritableIdentityCredential> halWritableCredential;
|
2020-02-12 04:08:27 +01:00
|
|
|
Status status = hal_->createCredential(docType, false, &halWritableCredential);
|
|
|
|
if (!status.isOk()) {
|
|
|
|
return halStatusToGenericError(status);
|
2019-10-28 18:32:48 +01:00
|
|
|
}
|
|
|
|
|
2022-01-19 00:58:47 +01:00
|
|
|
if (hwInfo_.isRemoteKeyProvisioningSupported) {
|
|
|
|
status = setRemotelyProvisionedAttestationKey(halWritableCredential.get());
|
|
|
|
if (!status.isOk()) {
|
2023-01-18 00:37:50 +01:00
|
|
|
LOG(WARNING) << status.toString8()
|
|
|
|
<< "\nUnable to fetch remotely provisioned attestation key, falling back "
|
|
|
|
<< "to the factory-provisioned attestation key.";
|
2022-01-19 00:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:32:48 +01:00
|
|
|
sp<IWritableCredential> writableCredential = new WritableCredential(
|
2021-03-04 22:32:43 +01:00
|
|
|
dataPath_, credentialName, docType, false, hwInfo_, halWritableCredential);
|
2019-10-28 18:32:48 +01:00
|
|
|
*_aidl_return = writableCredential;
|
|
|
|
return Status::ok();
|
|
|
|
}
|
|
|
|
|
2021-09-11 19:52:17 +02:00
|
|
|
Status CredentialStore::getCredentialCommon(const std::string& credentialName, int32_t cipherSuite,
|
|
|
|
sp<IPresentationSession> halSessionBinder,
|
2019-10-28 18:32:48 +01:00
|
|
|
sp<ICredential>* _aidl_return) {
|
|
|
|
*_aidl_return = nullptr;
|
|
|
|
|
|
|
|
uid_t callingUid = android::IPCThreadState::self()->getCallingUid();
|
|
|
|
optional<bool> credentialExists =
|
|
|
|
CredentialData::credentialExists(dataPath_, callingUid, credentialName);
|
|
|
|
if (!credentialExists.has_value()) {
|
|
|
|
return Status::fromServiceSpecificError(
|
|
|
|
ERROR_GENERIC, "Error determining if credential with given name exists");
|
|
|
|
}
|
|
|
|
if (!credentialExists.value()) {
|
|
|
|
return Status::fromServiceSpecificError(ERROR_NO_SUCH_CREDENTIAL,
|
|
|
|
"Credential with given name doesn't exist");
|
|
|
|
}
|
|
|
|
|
2020-02-12 04:08:27 +01:00
|
|
|
// Note: IdentityCredentialStore.java's CipherSuite enumeration and CipherSuite from the
|
|
|
|
// HAL is manually kept in sync. So this cast is safe.
|
2021-09-11 19:52:17 +02:00
|
|
|
sp<Credential> credential =
|
|
|
|
new Credential(CipherSuite(cipherSuite), dataPath_, credentialName, callingUid, hwInfo_,
|
|
|
|
hal_, halSessionBinder, halApiVersion_);
|
2019-10-28 18:32:48 +01:00
|
|
|
|
2020-10-16 17:50:13 +02:00
|
|
|
Status loadStatus = credential->ensureOrReplaceHalBinder();
|
2019-10-28 18:32:48 +01:00
|
|
|
if (!loadStatus.isOk()) {
|
|
|
|
LOG(ERROR) << "Error loading credential";
|
|
|
|
} else {
|
|
|
|
*_aidl_return = credential;
|
|
|
|
}
|
|
|
|
return loadStatus;
|
|
|
|
}
|
|
|
|
|
2021-09-11 19:52:17 +02:00
|
|
|
Status CredentialStore::getCredentialByName(const std::string& credentialName, int32_t cipherSuite,
|
|
|
|
sp<ICredential>* _aidl_return) {
|
|
|
|
return getCredentialCommon(credentialName, cipherSuite, nullptr, _aidl_return);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CredentialStore::createPresentationSession(int32_t cipherSuite, sp<ISession>* _aidl_return) {
|
|
|
|
sp<IPresentationSession> halPresentationSession;
|
|
|
|
Status status =
|
|
|
|
hal_->createPresentationSession(CipherSuite(cipherSuite), &halPresentationSession);
|
|
|
|
if (!status.isOk()) {
|
|
|
|
return halStatusToGenericError(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
*_aidl_return = new Session(cipherSuite, halPresentationSession, this);
|
|
|
|
return Status::ok();
|
|
|
|
}
|
|
|
|
|
2022-01-19 00:58:47 +01:00
|
|
|
Status CredentialStore::setRemotelyProvisionedAttestationKey(
|
|
|
|
IWritableIdentityCredential* halWritableCredential) {
|
2022-11-22 19:26:16 +01:00
|
|
|
std::vector<uint8_t> keyBlob;
|
|
|
|
std::vector<uint8_t> encodedCertChain;
|
|
|
|
Status status;
|
|
|
|
|
2023-01-31 23:11:15 +01:00
|
|
|
LOG(INFO) << "Fetching attestation key from RKPD";
|
2022-11-22 19:26:16 +01:00
|
|
|
|
2023-01-31 23:11:15 +01:00
|
|
|
uid_t callingUid = android::IPCThreadState::self()->getCallingUid();
|
2023-03-08 06:59:31 +01:00
|
|
|
std::optional<RemotelyProvisionedKey> key = getRpcKey(rpc_, callingUid);
|
2023-01-31 23:11:15 +01:00
|
|
|
if (!key) {
|
|
|
|
return Status::fromServiceSpecificError(
|
|
|
|
ERROR_GENERIC, "Failed to get remotely provisioned attestation key");
|
|
|
|
}
|
2022-11-22 19:26:16 +01:00
|
|
|
|
2023-01-31 23:11:15 +01:00
|
|
|
if (key->keyBlob.empty()) {
|
|
|
|
return Status::fromServiceSpecificError(
|
|
|
|
ERROR_GENERIC, "Remotely provisioned attestation key blob is empty");
|
2022-01-19 00:58:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-31 23:11:15 +01:00
|
|
|
keyBlob = std::move(key->keyBlob);
|
|
|
|
encodedCertChain = std::move(key->encodedCertChain);
|
|
|
|
|
2022-11-22 19:26:16 +01:00
|
|
|
status = halWritableCredential->setRemotelyProvisionedAttestationKey(keyBlob, encodedCertChain);
|
2022-01-19 00:58:47 +01:00
|
|
|
if (!status.isOk()) {
|
|
|
|
LOG(ERROR) << "Error setting remotely provisioned attestation key on credential";
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
return Status::ok();
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:32:48 +01:00
|
|
|
} // namespace identity
|
|
|
|
} // namespace security
|
|
|
|
} // namespace android
|