1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "pin_auth_service.h"
17
18 #include <cinttypes>
19
20 #include "accesstoken_kit.h"
21 #include "i_inputer_data_impl.h"
22 #include "iam_check.h"
23 #include "iam_common_defines.h"
24 #include "iam_logger.h"
25 #include "iam_para2str.h"
26 #include "iam_ptr.h"
27 #include "iam_executor_idriver_manager.h"
28 #include "pin_auth_driver_hdi.h"
29 #include "pin_auth_manager.h"
30
31
32 #define LOG_LABEL UserIam::Common::LABEL_PIN_AUTH_SA
33
34 namespace OHOS {
35 namespace UserIam {
36 namespace PinAuth {
37 namespace {
38 const std::string ACCESS_PIN_AUTH = "ohos.permission.ACCESS_PIN_AUTH";
39 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(PinAuthService::GetInstance().get());
40 } // namespace
41 std::mutex PinAuthService::mutex_;
42 std::shared_ptr<PinAuthService> PinAuthService::instance_ = nullptr;
43
PinAuthService()44 PinAuthService::PinAuthService() : SystemAbility(SUBSYS_USERIAM_SYS_ABILITY_PINAUTH, true)
45 {
46 }
47
GetInstance()48 std::shared_ptr<PinAuthService> PinAuthService::GetInstance()
49 {
50 if (instance_ == nullptr) {
51 std::lock_guard<std::mutex> guard(mutex_);
52 if (instance_ == nullptr) {
53 instance_ = Common::MakeShared<PinAuthService>();
54 if (instance_ == nullptr) {
55 IAM_LOGE("make share failed");
56 }
57 }
58 }
59 return instance_;
60 }
61
OnStart()62 void PinAuthService::OnStart()
63 {
64 IAM_LOGI("start");
65 StartDriverManager();
66 if (!Publish(this)) {
67 IAM_LOGE("failed to publish pin auth service");
68 return;
69 }
70 }
71
OnStop()72 void PinAuthService::OnStop()
73 {
74 IAM_LOGE("service is persistent, OnStop is not implemented");
75 }
76
GetTokenId()77 inline uint32_t PinAuthService::GetTokenId()
78 {
79 uint32_t tokenId = this->GetFirstTokenID();
80 if (tokenId == 0) {
81 tokenId = this->GetCallingTokenID();
82 }
83 return tokenId;
84 }
85
StartDriverManager()86 void PinAuthService::StartDriverManager()
87 {
88 IAM_LOGI("start");
89 const uint16_t pinAuthDefaultHdiId = 1;
90 const auto adapter = Common::MakeShared<PinAuthInterfaceAdapter>();
91 IF_FALSE_LOGE_AND_RETURN(adapter != nullptr);
92 auto pinAuthDefaultHdi = Common::MakeShared<PinAuthDriverHdi>(adapter);
93 IF_FALSE_LOGE_AND_RETURN(pinAuthDefaultHdi != nullptr);
94 // serviceName and HdiConfig.id must be globally unique
95 const std::map<std::string, UserAuth::HdiConfig> hdiName2Config = {
96 {"pin_auth_interface_service", {pinAuthDefaultHdiId, pinAuthDefaultHdi}},
97 };
98
99 int ret = UserIam::UserAuth::IDriverManager::Start(hdiName2Config);
100 if (ret != UserAuth::SUCCESS) {
101 IAM_LOGE("start driver manager failed");
102 }
103 }
104
CheckPermission(const std::string & permission)105 bool PinAuthService::CheckPermission(const std::string &permission)
106 {
107 IAM_LOGI("start");
108 using namespace Security::AccessToken;
109 uint32_t tokenId = GetTokenId();
110 return AccessTokenKit::VerifyAccessToken(tokenId, permission) == RET_SUCCESS;
111 }
112
RegisterInputer(const sptr<InputerGetData> & inputer)113 bool PinAuthService::RegisterInputer(const sptr<InputerGetData> &inputer)
114 {
115 IAM_LOGI("start");
116 if (!CheckPermission(ACCESS_PIN_AUTH)) {
117 IAM_LOGE("failed to check permission");
118 return false;
119 }
120 if (inputer == nullptr) {
121 IAM_LOGE("inputer is nullptr");
122 return false;
123 }
124 uint32_t tokenId = GetTokenId();
125 return PinAuthManager::GetInstance().RegisterInputer(tokenId, inputer);
126 }
127
UnRegisterInputer()128 void PinAuthService::UnRegisterInputer()
129 {
130 IAM_LOGI("start");
131 if (!CheckPermission(ACCESS_PIN_AUTH)) {
132 IAM_LOGE("failed to check permission");
133 return;
134 }
135 uint32_t tokenId = GetTokenId();
136 PinAuthManager::GetInstance().UnRegisterInputer(tokenId);
137 }
138 } // namespace PinAuth
139 } // namespace UserIam
140 } // namespace OHOS
141