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