• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "load_mode_handler.h"
22 #include "i_inputer_data_impl.h"
23 #include "iam_check.h"
24 #include "iam_common_defines.h"
25 #include "iam_logger.h"
26 #include "iam_para2str.h"
27 #include "iam_ptr.h"
28 #include "iam_executor_idriver_manager.h"
29 #include "pin_auth_driver_hdi.h"
30 #include "pin_auth_manager.h"
31 
32 
33 #define LOG_TAG "PIN_AUTH_SA"
34 
35 namespace OHOS {
36 namespace UserIam {
37 namespace PinAuth {
38 namespace {
39     const std::string ACCESS_PIN_AUTH = "ohos.permission.ACCESS_PIN_AUTH";
40     const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(PinAuthService::GetInstance().get());
41 } // namespace
42 std::mutex PinAuthService::mutex_;
43 std::shared_ptr<PinAuthService> PinAuthService::instance_ = nullptr;
44 
PinAuthService()45 PinAuthService::PinAuthService() : SystemAbility(SUBSYS_USERIAM_SYS_ABILITY_PINAUTH, true)
46 {
47 }
48 
GetInstance()49 std::shared_ptr<PinAuthService> PinAuthService::GetInstance()
50 {
51     if (instance_ == nullptr) {
52         std::lock_guard<std::mutex> guard(mutex_);
53         if (instance_ == nullptr) {
54             instance_ = Common::MakeShared<PinAuthService>();
55             if (instance_ == nullptr) {
56                 IAM_LOGE("make share failed");
57             }
58         }
59     }
60     return instance_;
61 }
62 
OnStart()63 void PinAuthService::OnStart()
64 {
65     IAM_LOGI("Sa start PinAuthService");
66     if (!Publish(this)) {
67         IAM_LOGE("failed to publish pin auth service");
68         return;
69     }
70     StartDriverManager();
71     LoadModeHandler::GetInstance().StartSubscribe();
72 }
73 
OnStop()74 void PinAuthService::OnStop()
75 {
76     IAM_LOGI("Sa stop PinAuthService");
77 }
78 
GetTokenId()79 inline uint32_t PinAuthService::GetTokenId()
80 {
81     uint32_t tokenId = this->GetFirstTokenID();
82     if (tokenId == 0) {
83         tokenId = this->GetCallingTokenID();
84     }
85     return tokenId;
86 }
87 
StartDriverManager()88 void PinAuthService::StartDriverManager()
89 {
90     IAM_LOGI("start");
91     const uint16_t pinAuthDefaultHdiId = 1;
92     const auto adapter = Common::MakeShared<PinAuthInterfaceAdapter>();
93     IF_FALSE_LOGE_AND_RETURN(adapter != nullptr);
94     auto pinAuthDefaultHdi = Common::MakeShared<PinAuthDriverHdi>(adapter);
95     IF_FALSE_LOGE_AND_RETURN(pinAuthDefaultHdi != nullptr);
96     // serviceName and HdiConfig.id must be globally unique
97     const std::map<std::string, UserAuth::HdiConfig> hdiName2Config = {
98         {"pin_auth_interface_service", {pinAuthDefaultHdiId, pinAuthDefaultHdi}},
99     };
100 
101     int ret = UserIam::UserAuth::IDriverManager::Start(hdiName2Config);
102     if (ret != UserAuth::SUCCESS) {
103         IAM_LOGE("start driver manager failed");
104     }
105 }
106 
CheckPermission(const std::string & permission)107 bool PinAuthService::CheckPermission(const std::string &permission)
108 {
109     IAM_LOGI("start");
110     using namespace Security::AccessToken;
111     uint32_t tokenId = GetTokenId();
112     return AccessTokenKit::VerifyAccessToken(tokenId, permission) == RET_SUCCESS;
113 }
114 
RegisterInputer(const sptr<InputerGetData> & inputer)115 bool PinAuthService::RegisterInputer(const sptr<InputerGetData> &inputer)
116 {
117     IAM_LOGI("start");
118     if (!CheckPermission(ACCESS_PIN_AUTH)) {
119         IAM_LOGE("failed to check permission");
120         return false;
121     }
122     if (inputer == nullptr) {
123         IAM_LOGE("inputer is nullptr");
124         return false;
125     }
126     uint32_t tokenId = GetTokenId();
127     return PinAuthManager::GetInstance().RegisterInputer(tokenId, inputer);
128 }
129 
UnRegisterInputer()130 void PinAuthService::UnRegisterInputer()
131 {
132     IAM_LOGI("start");
133     if (!CheckPermission(ACCESS_PIN_AUTH)) {
134         IAM_LOGE("failed to check permission");
135         return;
136     }
137     uint32_t tokenId = GetTokenId();
138     PinAuthManager::GetInstance().UnRegisterInputer(tokenId);
139 }
140 } // namespace PinAuth
141 } // namespace UserIam
142 } // namespace OHOS
143