blob: 947adf372fd20a3c2c77a24b3cdf9c0012ad3ca1 [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>
22
23#include "CredentialStore.h"
24#include "CredentialStoreFactory.h"
25
26namespace android {
27namespace security {
28namespace identity {
29
30using ::android::hardware::hidl_string;
31using ::android::hardware::hidl_vec;
32using ::android::hardware::identity::V1_0::IIdentityCredentialStore;
33using ::android::hardware::identity::V1_0::Result;
34using ::android::hardware::identity::V1_0::ResultCode;
35
36CredentialStoreFactory::CredentialStoreFactory(const std::string& dataPath) : dataPath_(dataPath) {}
37
38CredentialStoreFactory::~CredentialStoreFactory() {}
39
40CredentialStore* CredentialStoreFactory::createCredentialStore(const string& serviceName) {
41 sp<IIdentityCredentialStore> hal = IIdentityCredentialStore::tryGetService(serviceName);
42 if (hal.get() == nullptr) {
43 LOG(ERROR) << "Error get hal for store with service name '" << serviceName << "'";
44 return nullptr;
45 }
46
47 CredentialStore* store = new CredentialStore(dataPath_, hal);
48 if (!store->init()) {
49 LOG(ERROR) << "Error initializing CredentialStore with service name '" << serviceName
50 << "'";
51 delete store;
52 return nullptr;
53 }
54
55 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