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 "credstore"
18
19 #include <android-base/logging.h>
20
21 #include <binder/IPCThreadState.h>
22 #include <binder/IServiceManager.h>
23
24 //#include "CredentialStore.h"
25 #include "CredentialStoreFactory.h"
26
27 namespace android {
28 namespace security {
29 namespace identity {
30
31 using ::android::hardware::identity::IIdentityCredentialStore;
32
CredentialStoreFactory(const std::string & dataPath)33 CredentialStoreFactory::CredentialStoreFactory(const std::string& dataPath) : dataPath_(dataPath) {}
34
~CredentialStoreFactory()35 CredentialStoreFactory::~CredentialStoreFactory() {}
36
createCredentialStore(const string & instanceName)37 CredentialStore* 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);
42 if (hal.get() == nullptr) {
43 LOG(ERROR) << "Error getting HAL for IdentityCredentialStore store with service name '"
44 << serviceName << "'";
45 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 }
55 return store;
56 }
57
getCredentialStore(int32_t credentialStoreType,sp<ICredentialStore> * _aidl_return)58 Status 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
92