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 "devattest_service.h"
17
18 #include <securec.h>
19 #include "iservice_registry.h"
20 #include "devattest_errno.h"
21 #include "devattest_log.h"
22 #include "devattest_system_ability_listener.h"
23 #include "devattest_task.h"
24 #include "attest_entry.h"
25 #include "devattest_network_manager.h"
26
27 namespace OHOS {
28 namespace DevAttest {
29 using namespace std;
30
31 constexpr int32_t COMM_NET_CONN_MANAGER_SA_ID = 1151;
32 constexpr int32_t UNLOAD_IMMEDIATELY = 0;
33 constexpr int32_t DELAY_TIME = 300000;
34 const char* ATTEST_UNLOAD_TASK_ID = "attest_unload_task";
REGISTER_SYSTEM_ABILITY_BY_ID(DevAttestService,DevAttestInterface::SA_ID_DEVICE_ATTEST_SERVICE,false)35 REGISTER_SYSTEM_ABILITY_BY_ID(DevAttestService, DevAttestInterface::SA_ID_DEVICE_ATTEST_SERVICE, false)
36
37 DevAttestService::DevAttestService(int32_t systemAbilityId, bool runOnCreate)
38 : SystemAbility(systemAbilityId, runOnCreate)
39 {
40 }
41
DevAttestService()42 DevAttestService::DevAttestService()
43 : SystemAbility(SA_ID_DEVICE_ATTEST_SERVICE, false)
44 {
45 }
46
~DevAttestService()47 DevAttestService::~DevAttestService()
48 {
49 }
50
OnStart(const SystemAbilityOnDemandReason & startReason)51 void DevAttestService::OnStart(const SystemAbilityOnDemandReason& startReason)
52 {
53 if (state_ == ServiceRunningState::STATE_RUNNING) {
54 HILOGE("[OnStart] DevAttest Service has already started.");
55 return;
56 }
57 if (!Init()) {
58 HILOGE("[OnStart] Failed to init DevAttestService.");
59 return;
60 }
61 state_ = ServiceRunningState::STATE_RUNNING;
62 HILOGI("[OnStart] DevAttestService start success");
63 if (startReason.GetId() != OHOS::OnDemandReasonId::INTERFACE_CALL) {
64 DevAttestTask devAttestTask;
65 if (!devAttestTask.CreateThread()) {
66 HILOGE("[OnStart] Failed to CreateThread");
67 }
68 } else {
69 sptr<DevAttestSystemAbilityListener> pListener =
70 (std::make_unique<DevAttestSystemAbilityListener>()).release();
71 if (!pListener->AddDevAttestSystemAbilityListener(COMM_NET_CONN_MANAGER_SA_ID)) {
72 HILOGE("[OnStart] AddDevAttestSystemAbilityListener failed.");
73 }
74 }
75 return;
76 }
77
Init()78 bool DevAttestService::Init()
79 {
80 shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(ATTEST_UNLOAD_TASK_ID);
81 if (unloadHandler_ == nullptr) {
82 unloadHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
83 }
84 if (unloadHandler_ == nullptr) {
85 return false;
86 }
87
88 if (!registerToSa_) {
89 bool ret = Publish(this);
90 if (!ret) {
91 HILOGE("[Init] DevAttestService Init Publish failed");
92 return false;
93 }
94 registerToSa_ = true;
95 }
96 return true;
97 }
98
OnStop()99 void DevAttestService::OnStop()
100 {
101 HILOGI("[OnStop] DevAttestService OnStop");
102 state_ = ServiceRunningState::STATE_NOT_START;
103 registerToSa_ = false;
104 }
105
OnIdle(const SystemAbilityOnDemandReason & idleReason)106 int32_t DevAttestService::OnIdle(const SystemAbilityOnDemandReason& idleReason)
107 {
108 return UNLOAD_IMMEDIATELY;
109 }
110
DelayUnloadTask(void)111 void DevAttestService::DelayUnloadTask(void)
112 {
113 HILOGI("[DelayUnloadTask] Delay unload task begin");
114 if (unloadHandler_ == nullptr) {
115 shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(ATTEST_UNLOAD_TASK_ID);
116 unloadHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
117 }
118 if (unloadHandler_ == nullptr) {
119 HILOGE("[DelayUnloadTask] UnloadHandler is null");
120 return;
121 }
122 auto task = []() {
123 sptr<ISystemAbilityManager> samgrProxy =
124 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
125 if (samgrProxy == nullptr) {
126 HILOGE("[DelayUnloadTask] SamgrProxy is null");
127 return;
128 }
129 DelayedSingleton<DevAttestNetworkManager>::GetInstance()->UnregisterNetConnCallback();
130 int32_t ret = AttestDestroyTimerTask();
131 if (ret != DEVATTEST_SUCCESS) {
132 // Don't return
133 HILOGW("[DelayUnloadTask] Stop timer failed");
134 }
135 ret = samgrProxy->UnloadSystemAbility(DevAttestInterface::SA_ID_DEVICE_ATTEST_SERVICE);
136 if (ret != DEVATTEST_SUCCESS) {
137 HILOGE("[DelayUnloadTask] System ability failed");
138 return;
139 }
140 };
141
142 unloadHandler_->RemoveTask(ATTEST_UNLOAD_TASK_ID);
143 unloadHandler_->PostTask(task, ATTEST_UNLOAD_TASK_ID, DELAY_TIME);
144 }
145
CopyAttestResult(int32_t * resultArray,AttestResultInfo & attestResultInfo)146 int32_t DevAttestService::CopyAttestResult(int32_t *resultArray, AttestResultInfo &attestResultInfo)
147 {
148 if (resultArray == NULL) {
149 return DEVATTEST_FAIL;
150 }
151 int32_t *head = resultArray;
152 attestResultInfo.authResult_ = *head;
153 head++;
154 attestResultInfo.softwareResult_ = *head;
155 for (int i = 0; i < SOFTWARE_RESULT_DETAIL_SIZE; i++) {
156 attestResultInfo.softwareResultDetail_[i] = *(++head);
157 }
158 return DEVATTEST_SUCCESS;
159 }
160
GetAttestStatus(AttestResultInfo & attestResultInfo)161 int32_t DevAttestService::GetAttestStatus(AttestResultInfo &attestResultInfo)
162 {
163 int32_t resultArraySize = MAX_ATTEST_RESULT_SIZE * sizeof(int32_t);
164 int32_t *resultArray = (int32_t *)malloc(resultArraySize);
165 if (resultArray == NULL) {
166 HILOGE("[GetAttestStatus] malloc resultArray failed");
167 return DEVATTEST_FAIL;
168 }
169 (void)memset_s(resultArray, resultArraySize, 0, resultArraySize);
170 int32_t ticketLength = 0;
171 char* ticketStr = NULL;
172 int32_t ret = DEVATTEST_SUCCESS;
173 do {
174 ret = QueryAttest(&resultArray, MAX_ATTEST_RESULT_SIZE, &ticketStr, &ticketLength);
175 if (ret != DEVATTEST_SUCCESS) {
176 HILOGE("[GetAttestStatus] QueryAttest failed");
177 break;
178 }
179
180 attestResultInfo.ticketLength_ = ticketLength;
181 attestResultInfo.ticket_ = (ticketStr == NULL) ? string("") : ticketStr;
182 ret = CopyAttestResult(resultArray, attestResultInfo);
183 if (ret != DEVATTEST_SUCCESS) {
184 HILOGE("[GetAttestStatus] copy attest result failed");
185 break;
186 }
187 } while (0);
188 if (ticketStr != NULL && ticketLength != 0) {
189 free(ticketStr);
190 ticketStr = NULL;
191 }
192 free(resultArray);
193 resultArray = NULL;
194 HILOGD("[GetAttestStatus] GetAttestStatus end");
195 return ret;
196 }
197 } // end of DevAttest
198 } // end of OHOS