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 "co_auth_service.h"
17
18 #include <cinttypes>
19 #include <mutex>
20 #include <thread>
21
22 #include "string_ex.h"
23
24 #include "executor_messenger_service.h"
25 #include "hdi_wrapper.h"
26 #include "hisysevent_adapter.h"
27 #include "iam_logger.h"
28 #include "iam_ptr.h"
29 #include "iam_para2str.h"
30 #include "ipc_common.h"
31 #include "iam_time.h"
32 #include "iam_common_defines.h"
33 #include "parameter.h"
34 #include "relative_timer.h"
35 #include "resource_node_pool.h"
36
37 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
38
39 namespace OHOS {
40 namespace UserIam {
41 namespace UserAuth {
42 REGISTER_SYSTEM_ABILITY_BY_ID(CoAuthService, SUBSYS_USERIAM_SYS_ABILITY_AUTHEXECUTORMGR, true);
CoAuthService(int32_t systemAbilityId,bool runOnCreate)43 CoAuthService::CoAuthService(int32_t systemAbilityId, bool runOnCreate) : SystemAbility(systemAbilityId, runOnCreate)
44 {
45 IAM_LOGI("CoAuthService init");
46 }
47
OnStart()48 void CoAuthService::OnStart()
49 {
50 static std::mutex mutex;
51 static uint32_t timerId = 0;
52 std::lock_guard<std::mutex> guard(mutex);
53 IAM_LOGI("Start service");
54 if (!Publish(this)) {
55 IAM_LOGE("Failed to publish service");
56 return;
57 }
58
59 if (timerId != 0) {
60 RelativeTimer::GetInstance().Unregister(timerId);
61 }
62 timerId = RelativeTimer::GetInstance().Register(Init, 0);
63 }
64
OnStop()65 void CoAuthService::OnStop()
66 {
67 IAM_LOGI("Stop service");
68 }
69
ExecutorRegister(const ExecutorRegisterInfo & info,sptr<ExecutorCallbackInterface> & callback)70 uint64_t CoAuthService::ExecutorRegister(const ExecutorRegisterInfo &info, sptr<ExecutorCallbackInterface> &callback)
71 {
72 if (callback == nullptr) {
73 IAM_LOGE("executor callback is nullptr");
74 return INVALID_EXECUTOR_INDEX;
75 }
76 if (!IpcCommon::CheckPermission(*this, ACCESS_AUTH_RESPOOL)) {
77 IAM_LOGE("failed to check permission");
78 return INVALID_EXECUTOR_INDEX;
79 }
80 std::vector<uint64_t> templateIdList;
81 std::vector<uint8_t> fwkPublicKey;
82 auto executorCallback = Common::SptrToStdSharedPtr<ExecutorCallbackInterface>(callback);
83 auto resourceNode = ResourceNode::MakeNewResource(info, executorCallback, templateIdList, fwkPublicKey);
84 if (resourceNode == nullptr) {
85 IAM_LOGE("create resource node failed");
86 return INVALID_EXECUTOR_INDEX;
87 }
88 if (!ResourceNodePool::Instance().Insert(resourceNode)) {
89 IAM_LOGE("insert resource node failed");
90 return INVALID_EXECUTOR_INDEX;
91 }
92
93 sptr<ExecutorMessengerInterface> messenger = ExecutorMessengerService::GetInstance();
94 executorCallback->OnMessengerReady(messenger, fwkPublicKey, templateIdList);
95 uint64_t executorIndex = resourceNode->GetExecutorIndex();
96 int32_t executorType = resourceNode->GetAuthType();
97 IAM_LOGI("register successful, executorType is %{public}d, executorIndex is ****%{public}hx",
98 executorType, static_cast<uint16_t>(executorIndex));
99 if (auto obj = executorCallback->AsObject(); obj) {
100 obj->AddDeathRecipient(new (std::nothrow) IpcCommon::PeerDeathRecipient([executorIndex, executorType]() {
101 auto result = ResourceNodePool::Instance().Delete(executorIndex);
102 IAM_LOGI("delete executor %{public}s, executorIndex is ****%{public}hx", (result ? "succ" : "failed"),
103 static_cast<uint16_t>(executorIndex));
104 std::string executorDesc = "executor, type " + std::to_string(executorType);
105 UserIam::UserAuth::ReportSystemFault(Common::GetNowTimeString(), executorDesc);
106 }));
107 }
108 return executorIndex;
109 }
110
Init()111 void CoAuthService::Init()
112 {
113 auto hdi = HdiWrapper::GetHdiRemoteObjInstance();
114 if (hdi) {
115 hdi->AddDeathRecipient(new (std::nothrow) IpcCommon::PeerDeathRecipient([]() {
116 ResourceNodePool::Instance().DeleteAll();
117 RelativeTimer::GetInstance().Register(Init, DEFER_TIME);
118 IAM_LOGI("delete all executors for hdi dead");
119 UserIam::UserAuth::ReportSystemFault(Common::GetNowTimeString(), "user_auth_hdi host");
120 }));
121 IAM_LOGI("set fwk ready parameter");
122 SetParameter("bootevent.useriam.fwkready", "false");
123 SetParameter("bootevent.useriam.fwkready", "true");
124 } else {
125 RelativeTimer::GetInstance().Register(Init, DEFER_TIME);
126 }
127 }
128
Dump(int fd,const std::vector<std::u16string> & args)129 int CoAuthService::Dump(int fd, const std::vector<std::u16string> &args)
130 {
131 IAM_LOGI("start");
132 if (fd < 0) {
133 IAM_LOGE("invalid parameters");
134 dprintf(fd, "Invalid parameters.\n");
135 return INVALID_PARAMETERS;
136 }
137 std::string arg0 = (args.empty() ? "" : Str16ToStr8(args[0]));
138 if (arg0.empty() || arg0.compare("-h") == 0) {
139 dprintf(fd, "Usage:\n");
140 dprintf(fd, " -h: command help.\n");
141 dprintf(fd, " -l: resource pool dump.\n");
142 return SUCCESS;
143 }
144 if (arg0.compare("-l") == 0) {
145 ResourceNodePool::Instance().Enumerate([fd](const std::weak_ptr<ResourceNode> &node) {
146 auto nodeTmp = node.lock();
147 if (nodeTmp != nullptr) {
148 dprintf(fd, "ExecutorIndex is: %" PRIx64 ".\n", nodeTmp->GetExecutorIndex());
149 dprintf(fd, "ExecutorType is: %s.\n", Common::AuthTypeToStr(nodeTmp->GetAuthType()));
150 }
151 });
152 return SUCCESS;
153 }
154 IAM_LOGE("invalid option");
155 dprintf(fd, "Invalid option\n");
156 return GENERAL_ERROR;
157 }
158 } // namespace UserAuth
159 } // namespace UserIam
160 } // namespace OHOS