blob: 5c3bf36c0870c92f55021910b19fede99d29eb6f [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 "CredentialStoreFactory"
18
19#include <android-base/logging.h>
20
21#include <binder/IPCThreadState.h>
David Zeuthena6f9fba2020-02-11 22:08:27 -050022#include <binder/IServiceManager.h>
David Zeuthenab3e5652019-10-28 13:32:48 -040023
David Zeuthena6f9fba2020-02-11 22:08:27 -050024//#include "CredentialStore.h"
David Zeuthenab3e5652019-10-28 13:32:48 -040025#include "CredentialStoreFactory.h"
26
27namespace android {
28namespace security {
29namespace identity {
30
David Zeuthena6f9fba2020-02-11 22:08:27 -050031using ::android::hardware::identity::IIdentityCredentialStore;
David Zeuthenab3e5652019-10-28 13:32:48 -040032
33CredentialStoreFactory::CredentialStoreFactory(const std::string& dataPath) : dataPath_(dataPath) {}
34
35CredentialStoreFactory::~CredentialStoreFactory() {}
36
David Zeuthena6f9fba2020-02-11 22:08:27 -050037CredentialStore* CredentialStoreFactory::createCredentialStore(const string& instanceName) {
38 String16 serviceName =
39 IIdentityCredentialStore::descriptor + String16("/") + String16(instanceName.c_str());
40 sp<IIdentityCredentialStore> hal =
41 android::waitForDeclaredService<IIdentityCredentialStore>(serviceName);
David Zeuthenab3e5652019-10-28 13:32:48 -040042 if (hal.get() == nullptr) {
David Zeuthena6f9fba2020-02-11 22:08:27 -050043 LOG(ERROR) << "Error getting HAL for IdentityCredentialStore store with service name '"
44 << serviceName << "'";
David Zeuthenab3e5652019-10-28 13:32:48 -040045 return nullptr;
46 }
47
48 CredentialStore* store = new CredentialStore(dataPath_, hal);
49 if (!store->init()) {
50 LOG(ERROR) << "Error initializing CredentialStore with service name '" << serviceName
51 << "'";
52 delete store;
53 return nullptr;
54 }
David Zeuthenab3e5652019-10-28 13:32:48 -040055 return store;
56}
57
58Status CredentialStoreFactory::getCredentialStore(int32_t credentialStoreType,
59 sp<ICredentialStore>* _aidl_return) {
60 switch (credentialStoreType) {
61 case CREDENTIAL_STORE_TYPE_DEFAULT:
62 if (defaultStore_.get() == nullptr) {
63 defaultStore_ = createCredentialStore("default");
64 }
65 if (defaultStore_.get() == nullptr) {
66 return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC,
67 "Error creating default store");
68 }
69 *_aidl_return = defaultStore_.get();
70 return Status::ok();
71
72 case CREDENTIAL_STORE_TYPE_DIRECT_ACCESS:
73 if (directAccessStore_.get() == nullptr) {
74 directAccessStore_ = createCredentialStore("directAccess");
75 }
76 if (directAccessStore_.get() == nullptr) {
77 return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC,
78 "Error creating direct access store");
79 }
80 *_aidl_return = directAccessStore_.get();
81 return Status::ok();
82 break;
83 }
84
85 return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC,
86 "Unknown credential store type");
87}
88
89} // namespace identity
90} // namespace security
91} // namespace android