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 | |
David Zeuthen | 62d43bf | 2021-03-31 10:41:27 -0400 | [diff] [blame] | 17 | #define LOG_TAG "credstore" |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | |
| 21 | #include <android-base/logging.h> |
| 22 | |
| 23 | #include <binder/IPCThreadState.h> |
| 24 | |
| 25 | #include "Credential.h" |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 26 | #include "CredentialData.h" |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 27 | #include "CredentialStore.h" |
David Zeuthen | 045a2c8 | 2021-09-11 13:52:17 -0400 | [diff] [blame^] | 28 | #include "Session.h" |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 29 | #include "Util.h" |
| 30 | #include "WritableCredential.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace security { |
| 34 | namespace identity { |
| 35 | |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 36 | CredentialStore::CredentialStore(const std::string& dataPath, sp<IIdentityCredentialStore> hal) |
| 37 | : dataPath_(dataPath), hal_(hal) {} |
| 38 | |
| 39 | bool CredentialStore::init() { |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 40 | Status status = hal_->getHardwareInformation(&hwInfo_); |
| 41 | if (!status.isOk()) { |
| 42 | LOG(ERROR) << "Error getting hardware information: " << status.toString8(); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 43 | return false; |
| 44 | } |
David Zeuthen | 472e6c8 | 2020-10-16 11:50:13 -0400 | [diff] [blame] | 45 | halApiVersion_ = hal_->getInterfaceVersion(); |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 46 | |
David Zeuthen | 472e6c8 | 2020-10-16 11:50:13 -0400 | [diff] [blame] | 47 | LOG(INFO) << "Connected to Identity Credential HAL with API version " << halApiVersion_ |
| 48 | << " and name '" << hwInfo_.credentialStoreName << "' authored by '" |
| 49 | << hwInfo_.credentialStoreAuthorName << "' with chunk size " << hwInfo_.dataChunkSize |
| 50 | << " and directoAccess set to " << (hwInfo_.isDirectAccess ? "true" : "false"); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | |
| 54 | CredentialStore::~CredentialStore() {} |
| 55 | |
| 56 | Status CredentialStore::getSecurityHardwareInfo(SecurityHardwareInfoParcel* _aidl_return) { |
| 57 | SecurityHardwareInfoParcel info; |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 58 | info.directAccess = hwInfo_.isDirectAccess; |
| 59 | info.supportedDocTypes = hwInfo_.supportedDocTypes; |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 60 | *_aidl_return = info; |
| 61 | return Status::ok(); |
| 62 | }; |
| 63 | |
| 64 | Status CredentialStore::createCredential(const std::string& credentialName, |
| 65 | const std::string& docType, |
| 66 | sp<IWritableCredential>* _aidl_return) { |
| 67 | uid_t callingUid = android::IPCThreadState::self()->getCallingUid(); |
| 68 | optional<bool> credentialExists = |
| 69 | CredentialData::credentialExists(dataPath_, callingUid, credentialName); |
| 70 | if (!credentialExists.has_value()) { |
| 71 | return Status::fromServiceSpecificError( |
| 72 | ERROR_GENERIC, "Error determining if credential with given name exists"); |
| 73 | } |
| 74 | if (credentialExists.value()) { |
| 75 | return Status::fromServiceSpecificError(ERROR_ALREADY_PERSONALIZED, |
| 76 | "Credential with given name already exists"); |
| 77 | } |
| 78 | |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 79 | if (hwInfo_.supportedDocTypes.size() > 0) { |
| 80 | if (std::find(hwInfo_.supportedDocTypes.begin(), hwInfo_.supportedDocTypes.end(), |
| 81 | docType) == hwInfo_.supportedDocTypes.end()) { |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 82 | return Status::fromServiceSpecificError(ERROR_DOCUMENT_TYPE_NOT_SUPPORTED, |
| 83 | "No support for given document type"); |
| 84 | } |
| 85 | } |
| 86 | |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 87 | sp<IWritableIdentityCredential> halWritableCredential; |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 88 | Status status = hal_->createCredential(docType, false, &halWritableCredential); |
| 89 | if (!status.isOk()) { |
| 90 | return halStatusToGenericError(status); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | sp<IWritableCredential> writableCredential = new WritableCredential( |
David Zeuthen | 27407a5 | 2021-03-04 16:32:43 -0500 | [diff] [blame] | 94 | dataPath_, credentialName, docType, false, hwInfo_, halWritableCredential); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 95 | *_aidl_return = writableCredential; |
| 96 | return Status::ok(); |
| 97 | } |
| 98 | |
David Zeuthen | 045a2c8 | 2021-09-11 13:52:17 -0400 | [diff] [blame^] | 99 | Status CredentialStore::getCredentialCommon(const std::string& credentialName, int32_t cipherSuite, |
| 100 | sp<IPresentationSession> halSessionBinder, |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 101 | sp<ICredential>* _aidl_return) { |
| 102 | *_aidl_return = nullptr; |
| 103 | |
| 104 | uid_t callingUid = android::IPCThreadState::self()->getCallingUid(); |
| 105 | optional<bool> credentialExists = |
| 106 | CredentialData::credentialExists(dataPath_, callingUid, credentialName); |
| 107 | if (!credentialExists.has_value()) { |
| 108 | return Status::fromServiceSpecificError( |
| 109 | ERROR_GENERIC, "Error determining if credential with given name exists"); |
| 110 | } |
| 111 | if (!credentialExists.value()) { |
| 112 | return Status::fromServiceSpecificError(ERROR_NO_SUCH_CREDENTIAL, |
| 113 | "Credential with given name doesn't exist"); |
| 114 | } |
| 115 | |
David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 116 | // Note: IdentityCredentialStore.java's CipherSuite enumeration and CipherSuite from the |
| 117 | // HAL is manually kept in sync. So this cast is safe. |
David Zeuthen | 045a2c8 | 2021-09-11 13:52:17 -0400 | [diff] [blame^] | 118 | sp<Credential> credential = |
| 119 | new Credential(CipherSuite(cipherSuite), dataPath_, credentialName, callingUid, hwInfo_, |
| 120 | hal_, halSessionBinder, halApiVersion_); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 121 | |
David Zeuthen | 472e6c8 | 2020-10-16 11:50:13 -0400 | [diff] [blame] | 122 | Status loadStatus = credential->ensureOrReplaceHalBinder(); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 123 | if (!loadStatus.isOk()) { |
| 124 | LOG(ERROR) << "Error loading credential"; |
| 125 | } else { |
| 126 | *_aidl_return = credential; |
| 127 | } |
| 128 | return loadStatus; |
| 129 | } |
| 130 | |
David Zeuthen | 045a2c8 | 2021-09-11 13:52:17 -0400 | [diff] [blame^] | 131 | Status CredentialStore::getCredentialByName(const std::string& credentialName, int32_t cipherSuite, |
| 132 | sp<ICredential>* _aidl_return) { |
| 133 | return getCredentialCommon(credentialName, cipherSuite, nullptr, _aidl_return); |
| 134 | } |
| 135 | |
| 136 | Status CredentialStore::createPresentationSession(int32_t cipherSuite, sp<ISession>* _aidl_return) { |
| 137 | sp<IPresentationSession> halPresentationSession; |
| 138 | Status status = |
| 139 | hal_->createPresentationSession(CipherSuite(cipherSuite), &halPresentationSession); |
| 140 | if (!status.isOk()) { |
| 141 | return halStatusToGenericError(status); |
| 142 | } |
| 143 | |
| 144 | *_aidl_return = new Session(cipherSuite, halPresentationSession, this); |
| 145 | return Status::ok(); |
| 146 | } |
| 147 | |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 148 | } // namespace identity |
| 149 | } // namespace security |
| 150 | } // namespace android |