xref: /aosp_15_r20/system/security/identity/CredentialStoreFactory.cpp (revision e1997b9af69e3155ead6e072d106a0077849ffba)
1*e1997b9aSAndroid Build Coastguard Worker /*
2*e1997b9aSAndroid Build Coastguard Worker  * Copyright (c) 2019, The Android Open Source Project
3*e1997b9aSAndroid Build Coastguard Worker  *
4*e1997b9aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e1997b9aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e1997b9aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e1997b9aSAndroid Build Coastguard Worker  *
8*e1997b9aSAndroid Build Coastguard Worker  *     http://www.apache.org/licenses/LICENSE-2.0
9*e1997b9aSAndroid Build Coastguard Worker  *
10*e1997b9aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e1997b9aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e1997b9aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e1997b9aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e1997b9aSAndroid Build Coastguard Worker  * limitations under the License.
15*e1997b9aSAndroid Build Coastguard Worker  */
16*e1997b9aSAndroid Build Coastguard Worker 
17*e1997b9aSAndroid Build Coastguard Worker #define LOG_TAG "credstore"
18*e1997b9aSAndroid Build Coastguard Worker 
19*e1997b9aSAndroid Build Coastguard Worker #include <android-base/logging.h>
20*e1997b9aSAndroid Build Coastguard Worker 
21*e1997b9aSAndroid Build Coastguard Worker #include <binder/IPCThreadState.h>
22*e1997b9aSAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
23*e1997b9aSAndroid Build Coastguard Worker 
24*e1997b9aSAndroid Build Coastguard Worker //#include "CredentialStore.h"
25*e1997b9aSAndroid Build Coastguard Worker #include "CredentialStoreFactory.h"
26*e1997b9aSAndroid Build Coastguard Worker 
27*e1997b9aSAndroid Build Coastguard Worker namespace android {
28*e1997b9aSAndroid Build Coastguard Worker namespace security {
29*e1997b9aSAndroid Build Coastguard Worker namespace identity {
30*e1997b9aSAndroid Build Coastguard Worker 
31*e1997b9aSAndroid Build Coastguard Worker using ::android::hardware::identity::IIdentityCredentialStore;
32*e1997b9aSAndroid Build Coastguard Worker 
CredentialStoreFactory(const std::string & dataPath)33*e1997b9aSAndroid Build Coastguard Worker CredentialStoreFactory::CredentialStoreFactory(const std::string& dataPath) : dataPath_(dataPath) {}
34*e1997b9aSAndroid Build Coastguard Worker 
~CredentialStoreFactory()35*e1997b9aSAndroid Build Coastguard Worker CredentialStoreFactory::~CredentialStoreFactory() {}
36*e1997b9aSAndroid Build Coastguard Worker 
createCredentialStore(const string & instanceName)37*e1997b9aSAndroid Build Coastguard Worker CredentialStore* CredentialStoreFactory::createCredentialStore(const string& instanceName) {
38*e1997b9aSAndroid Build Coastguard Worker     String16 serviceName =
39*e1997b9aSAndroid Build Coastguard Worker         IIdentityCredentialStore::descriptor + String16("/") + String16(instanceName.c_str());
40*e1997b9aSAndroid Build Coastguard Worker     sp<IIdentityCredentialStore> hal =
41*e1997b9aSAndroid Build Coastguard Worker         android::waitForDeclaredService<IIdentityCredentialStore>(serviceName);
42*e1997b9aSAndroid Build Coastguard Worker     if (hal.get() == nullptr) {
43*e1997b9aSAndroid Build Coastguard Worker         LOG(ERROR) << "Error getting HAL for IdentityCredentialStore store with service name '"
44*e1997b9aSAndroid Build Coastguard Worker                    << serviceName << "'";
45*e1997b9aSAndroid Build Coastguard Worker         return nullptr;
46*e1997b9aSAndroid Build Coastguard Worker     }
47*e1997b9aSAndroid Build Coastguard Worker 
48*e1997b9aSAndroid Build Coastguard Worker     CredentialStore* store = new CredentialStore(dataPath_, hal);
49*e1997b9aSAndroid Build Coastguard Worker     if (!store->init()) {
50*e1997b9aSAndroid Build Coastguard Worker         LOG(ERROR) << "Error initializing CredentialStore with service name '" << serviceName
51*e1997b9aSAndroid Build Coastguard Worker                    << "'";
52*e1997b9aSAndroid Build Coastguard Worker         delete store;
53*e1997b9aSAndroid Build Coastguard Worker         return nullptr;
54*e1997b9aSAndroid Build Coastguard Worker     }
55*e1997b9aSAndroid Build Coastguard Worker     return store;
56*e1997b9aSAndroid Build Coastguard Worker }
57*e1997b9aSAndroid Build Coastguard Worker 
getCredentialStore(int32_t credentialStoreType,sp<ICredentialStore> * _aidl_return)58*e1997b9aSAndroid Build Coastguard Worker Status CredentialStoreFactory::getCredentialStore(int32_t credentialStoreType,
59*e1997b9aSAndroid Build Coastguard Worker                                                   sp<ICredentialStore>* _aidl_return) {
60*e1997b9aSAndroid Build Coastguard Worker     switch (credentialStoreType) {
61*e1997b9aSAndroid Build Coastguard Worker     case CREDENTIAL_STORE_TYPE_DEFAULT:
62*e1997b9aSAndroid Build Coastguard Worker         if (defaultStore_.get() == nullptr) {
63*e1997b9aSAndroid Build Coastguard Worker             defaultStore_ = createCredentialStore("default");
64*e1997b9aSAndroid Build Coastguard Worker         }
65*e1997b9aSAndroid Build Coastguard Worker         if (defaultStore_.get() == nullptr) {
66*e1997b9aSAndroid Build Coastguard Worker             return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC,
67*e1997b9aSAndroid Build Coastguard Worker                                                     "Error creating default store");
68*e1997b9aSAndroid Build Coastguard Worker         }
69*e1997b9aSAndroid Build Coastguard Worker         *_aidl_return = defaultStore_.get();
70*e1997b9aSAndroid Build Coastguard Worker         return Status::ok();
71*e1997b9aSAndroid Build Coastguard Worker 
72*e1997b9aSAndroid Build Coastguard Worker     case CREDENTIAL_STORE_TYPE_DIRECT_ACCESS:
73*e1997b9aSAndroid Build Coastguard Worker         if (directAccessStore_.get() == nullptr) {
74*e1997b9aSAndroid Build Coastguard Worker             directAccessStore_ = createCredentialStore("directAccess");
75*e1997b9aSAndroid Build Coastguard Worker         }
76*e1997b9aSAndroid Build Coastguard Worker         if (directAccessStore_.get() == nullptr) {
77*e1997b9aSAndroid Build Coastguard Worker             return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC,
78*e1997b9aSAndroid Build Coastguard Worker                                                     "Error creating direct access store");
79*e1997b9aSAndroid Build Coastguard Worker         }
80*e1997b9aSAndroid Build Coastguard Worker         *_aidl_return = directAccessStore_.get();
81*e1997b9aSAndroid Build Coastguard Worker         return Status::ok();
82*e1997b9aSAndroid Build Coastguard Worker         break;
83*e1997b9aSAndroid Build Coastguard Worker     }
84*e1997b9aSAndroid Build Coastguard Worker 
85*e1997b9aSAndroid Build Coastguard Worker     return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC,
86*e1997b9aSAndroid Build Coastguard Worker                                             "Unknown credential store type");
87*e1997b9aSAndroid Build Coastguard Worker }
88*e1997b9aSAndroid Build Coastguard Worker 
89*e1997b9aSAndroid Build Coastguard Worker }  // namespace identity
90*e1997b9aSAndroid Build Coastguard Worker }  // namespace security
91*e1997b9aSAndroid Build Coastguard Worker }  // namespace android
92