blob: e3a825ba132c73b89f853622c407a7ddc584680c [file] [log] [blame]
David Zeuthenab3e5652019-10-28 13:32:48 -04001/*
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 Zeuthena6f9fba2020-02-11 22:08:27 -050026#include "CredentialData.h"
David Zeuthenab3e5652019-10-28 13:32:48 -040027#include "CredentialStore.h"
28#include "Util.h"
29#include "WritableCredential.h"
30
31namespace android {
32namespace security {
33namespace identity {
34
David Zeuthenab3e5652019-10-28 13:32:48 -040035CredentialStore::CredentialStore(const std::string& dataPath, sp<IIdentityCredentialStore> hal)
36 : dataPath_(dataPath), hal_(hal) {}
37
38bool CredentialStore::init() {
David Zeuthena6f9fba2020-02-11 22:08:27 -050039 Status status = hal_->getHardwareInformation(&hwInfo_);
40 if (!status.isOk()) {
41 LOG(ERROR) << "Error getting hardware information: " << status.toString8();
David Zeuthenab3e5652019-10-28 13:32:48 -040042 return false;
43 }
David Zeuthena6f9fba2020-02-11 22:08:27 -050044
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 Zeuthenab3e5652019-10-28 13:32:48 -040049 return true;
50}
51
52CredentialStore::~CredentialStore() {}
53
54Status CredentialStore::getSecurityHardwareInfo(SecurityHardwareInfoParcel* _aidl_return) {
55 SecurityHardwareInfoParcel info;
David Zeuthena6f9fba2020-02-11 22:08:27 -050056 info.directAccess = hwInfo_.isDirectAccess;
57 info.supportedDocTypes = hwInfo_.supportedDocTypes;
David Zeuthenab3e5652019-10-28 13:32:48 -040058 *_aidl_return = info;
59 return Status::ok();
60};
61
62Status 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 Zeuthena6f9fba2020-02-11 22:08:27 -050077 if (hwInfo_.supportedDocTypes.size() > 0) {
78 if (std::find(hwInfo_.supportedDocTypes.begin(), hwInfo_.supportedDocTypes.end(),
79 docType) == hwInfo_.supportedDocTypes.end()) {
David Zeuthenab3e5652019-10-28 13:32:48 -040080 return Status::fromServiceSpecificError(ERROR_DOCUMENT_TYPE_NOT_SUPPORTED,
81 "No support for given document type");
82 }
83 }
84
David Zeuthenab3e5652019-10-28 13:32:48 -040085 sp<IWritableIdentityCredential> halWritableCredential;
David Zeuthena6f9fba2020-02-11 22:08:27 -050086 Status status = hal_->createCredential(docType, false, &halWritableCredential);
87 if (!status.isOk()) {
88 return halStatusToGenericError(status);
David Zeuthenab3e5652019-10-28 13:32:48 -040089 }
90
91 sp<IWritableCredential> writableCredential = new WritableCredential(
David Zeuthena6f9fba2020-02-11 22:08:27 -050092 dataPath_, credentialName, docType, hwInfo_.dataChunkSize, halWritableCredential);
David Zeuthenab3e5652019-10-28 13:32:48 -040093 *_aidl_return = writableCredential;
94 return Status::ok();
95}
96
David Zeuthenab3e5652019-10-28 13:32:48 -040097Status 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 Zeuthena6f9fba2020-02-11 22:08:27 -0500113 // 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 Zeuthenab3e5652019-10-28 13:32:48 -0400116
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