| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 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 |  | 
| David Zeuthen | 62d43bf | 2021-03-31 10:41:27 -0400 | [diff] [blame] | 17 | #define LOG_TAG "credstore" | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 18 |  | 
 | 19 | #include <android-base/logging.h> | 
 | 20 |  | 
 | 21 | #include <binder/IPCThreadState.h> | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 22 | #include <binder/IServiceManager.h> | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 23 |  | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 24 | //#include "CredentialStore.h" | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 25 | #include "CredentialStoreFactory.h" | 
 | 26 |  | 
 | 27 | namespace android { | 
 | 28 | namespace security { | 
 | 29 | namespace identity { | 
 | 30 |  | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 31 | using ::android::hardware::identity::IIdentityCredentialStore; | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 32 |  | 
 | 33 | CredentialStoreFactory::CredentialStoreFactory(const std::string& dataPath) : dataPath_(dataPath) {} | 
 | 34 |  | 
 | 35 | CredentialStoreFactory::~CredentialStoreFactory() {} | 
 | 36 |  | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 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); | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 42 |     if (hal.get() == nullptr) { | 
| David Zeuthen | a6f9fba | 2020-02-11 22:08:27 -0500 | [diff] [blame] | 43 |         LOG(ERROR) << "Error getting HAL for IdentityCredentialStore store with service name '" | 
 | 44 |                    << serviceName << "'"; | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 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 |     } | 
| David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 55 |     return store; | 
 | 56 | } | 
 | 57 |  | 
 | 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 |