15bbf6e98Sopenharmony_ci/* 25bbf6e98Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd. 35bbf6e98Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45bbf6e98Sopenharmony_ci * you may not use this file except in compliance with the License. 55bbf6e98Sopenharmony_ci * You may obtain a copy of the License at 65bbf6e98Sopenharmony_ci * 75bbf6e98Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85bbf6e98Sopenharmony_ci * 95bbf6e98Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105bbf6e98Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115bbf6e98Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125bbf6e98Sopenharmony_ci * See the License for the specific language governing permissions and 135bbf6e98Sopenharmony_ci * limitations under the License. 145bbf6e98Sopenharmony_ci */ 155bbf6e98Sopenharmony_ci 165bbf6e98Sopenharmony_ci#include "devattest_service.h" 175bbf6e98Sopenharmony_ci 185bbf6e98Sopenharmony_ci#include <securec.h> 195bbf6e98Sopenharmony_ci#include "iservice_registry.h" 205bbf6e98Sopenharmony_ci#include "devattest_errno.h" 215bbf6e98Sopenharmony_ci#include "devattest_log.h" 225bbf6e98Sopenharmony_ci#include "devattest_system_ability_listener.h" 235bbf6e98Sopenharmony_ci#include "devattest_task.h" 245bbf6e98Sopenharmony_ci#include "attest_entry.h" 255bbf6e98Sopenharmony_ci#include "devattest_network_manager.h" 265bbf6e98Sopenharmony_ci 275bbf6e98Sopenharmony_cinamespace OHOS { 285bbf6e98Sopenharmony_cinamespace DevAttest { 295bbf6e98Sopenharmony_ciusing namespace std; 305bbf6e98Sopenharmony_ci 315bbf6e98Sopenharmony_ciconstexpr int32_t COMM_NET_CONN_MANAGER_SA_ID = 1151; 325bbf6e98Sopenharmony_ciconstexpr int32_t UNLOAD_IMMEDIATELY = 0; 335bbf6e98Sopenharmony_ciconstexpr int32_t DELAY_TIME = 600000; 345bbf6e98Sopenharmony_ciconst char* ATTEST_UNLOAD_TASK_ID = "attest_unload_task"; 355bbf6e98Sopenharmony_ciREGISTER_SYSTEM_ABILITY_BY_ID(DevAttestService, DevAttestInterface::SA_ID_DEVICE_ATTEST_SERVICE, false) 365bbf6e98Sopenharmony_ci 375bbf6e98Sopenharmony_ciDevAttestService::DevAttestService(int32_t systemAbilityId, bool runOnCreate) 385bbf6e98Sopenharmony_ci : SystemAbility(systemAbilityId, runOnCreate) 395bbf6e98Sopenharmony_ci{ 405bbf6e98Sopenharmony_ci} 415bbf6e98Sopenharmony_ci 425bbf6e98Sopenharmony_ciDevAttestService::DevAttestService() 435bbf6e98Sopenharmony_ci : SystemAbility(SA_ID_DEVICE_ATTEST_SERVICE, false) 445bbf6e98Sopenharmony_ci{ 455bbf6e98Sopenharmony_ci} 465bbf6e98Sopenharmony_ci 475bbf6e98Sopenharmony_ciDevAttestService::~DevAttestService() 485bbf6e98Sopenharmony_ci{ 495bbf6e98Sopenharmony_ci} 505bbf6e98Sopenharmony_ci 515bbf6e98Sopenharmony_civoid DevAttestService::OnStart(const SystemAbilityOnDemandReason& startReason) 525bbf6e98Sopenharmony_ci{ 535bbf6e98Sopenharmony_ci if (state_ == ServiceRunningState::STATE_RUNNING) { 545bbf6e98Sopenharmony_ci HILOGE("[OnStart] DevAttest Service has already started."); 555bbf6e98Sopenharmony_ci return; 565bbf6e98Sopenharmony_ci } 575bbf6e98Sopenharmony_ci if (!Init()) { 585bbf6e98Sopenharmony_ci HILOGE("[OnStart] Failed to init DevAttestService."); 595bbf6e98Sopenharmony_ci return; 605bbf6e98Sopenharmony_ci } 615bbf6e98Sopenharmony_ci state_ = ServiceRunningState::STATE_RUNNING; 625bbf6e98Sopenharmony_ci HILOGI("[OnStart] DevAttestService start success"); 635bbf6e98Sopenharmony_ci if (startReason.GetId() != OHOS::OnDemandReasonId::INTERFACE_CALL) { 645bbf6e98Sopenharmony_ci DevAttestTask devAttestTask; 655bbf6e98Sopenharmony_ci if (!devAttestTask.CreateThread()) { 665bbf6e98Sopenharmony_ci HILOGE("[OnStart] Failed to CreateThread"); 675bbf6e98Sopenharmony_ci } 685bbf6e98Sopenharmony_ci } else { 695bbf6e98Sopenharmony_ci std::unique_ptr<DevAttestSystemAbilityListener> pListener = std::make_unique<DevAttestSystemAbilityListener>(); 705bbf6e98Sopenharmony_ci if (!pListener->AddDevAttestSystemAbilityListener(COMM_NET_CONN_MANAGER_SA_ID)) { 715bbf6e98Sopenharmony_ci HILOGE("[OnStart] AddDevAttestSystemAbilityListener failed."); 725bbf6e98Sopenharmony_ci } 735bbf6e98Sopenharmony_ci } 745bbf6e98Sopenharmony_ci return; 755bbf6e98Sopenharmony_ci} 765bbf6e98Sopenharmony_ci 775bbf6e98Sopenharmony_cibool DevAttestService::Init() 785bbf6e98Sopenharmony_ci{ 795bbf6e98Sopenharmony_ci shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(ATTEST_UNLOAD_TASK_ID); 805bbf6e98Sopenharmony_ci if (unloadHandler_ == nullptr) { 815bbf6e98Sopenharmony_ci unloadHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner); 825bbf6e98Sopenharmony_ci } 835bbf6e98Sopenharmony_ci if (unloadHandler_ == nullptr) { 845bbf6e98Sopenharmony_ci return false; 855bbf6e98Sopenharmony_ci } 865bbf6e98Sopenharmony_ci 875bbf6e98Sopenharmony_ci if (!registerToSa_) { 885bbf6e98Sopenharmony_ci bool ret = Publish(this); 895bbf6e98Sopenharmony_ci if (!ret) { 905bbf6e98Sopenharmony_ci HILOGE("[Init] DevAttestService Init Publish failed"); 915bbf6e98Sopenharmony_ci return false; 925bbf6e98Sopenharmony_ci } 935bbf6e98Sopenharmony_ci registerToSa_ = true; 945bbf6e98Sopenharmony_ci } 955bbf6e98Sopenharmony_ci return true; 965bbf6e98Sopenharmony_ci} 975bbf6e98Sopenharmony_ci 985bbf6e98Sopenharmony_civoid DevAttestService::OnStop() 995bbf6e98Sopenharmony_ci{ 1005bbf6e98Sopenharmony_ci HILOGI("[OnStop] DevAttestService OnStop"); 1015bbf6e98Sopenharmony_ci state_ = ServiceRunningState::STATE_NOT_START; 1025bbf6e98Sopenharmony_ci registerToSa_ = false; 1035bbf6e98Sopenharmony_ci} 1045bbf6e98Sopenharmony_ci 1055bbf6e98Sopenharmony_ciint32_t DevAttestService::OnIdle(const SystemAbilityOnDemandReason& idleReason) 1065bbf6e98Sopenharmony_ci{ 1075bbf6e98Sopenharmony_ci return UNLOAD_IMMEDIATELY; 1085bbf6e98Sopenharmony_ci} 1095bbf6e98Sopenharmony_ci 1105bbf6e98Sopenharmony_civoid DevAttestService::DelayUnloadTask(void) 1115bbf6e98Sopenharmony_ci{ 1125bbf6e98Sopenharmony_ci HILOGI("[DelayUnloadTask] Delay unload task begin"); 1135bbf6e98Sopenharmony_ci if (unloadHandler_ == nullptr) { 1145bbf6e98Sopenharmony_ci shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(ATTEST_UNLOAD_TASK_ID); 1155bbf6e98Sopenharmony_ci unloadHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner); 1165bbf6e98Sopenharmony_ci } 1175bbf6e98Sopenharmony_ci if (unloadHandler_ == nullptr) { 1185bbf6e98Sopenharmony_ci HILOGE("[DelayUnloadTask] UnloadHandler is null"); 1195bbf6e98Sopenharmony_ci return; 1205bbf6e98Sopenharmony_ci } 1215bbf6e98Sopenharmony_ci auto task = []() { 1225bbf6e98Sopenharmony_ci sptr<ISystemAbilityManager> samgrProxy = 1235bbf6e98Sopenharmony_ci SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 1245bbf6e98Sopenharmony_ci if (samgrProxy == nullptr) { 1255bbf6e98Sopenharmony_ci HILOGE("[DelayUnloadTask] SamgrProxy is null"); 1265bbf6e98Sopenharmony_ci return; 1275bbf6e98Sopenharmony_ci } 1285bbf6e98Sopenharmony_ci DelayedSingleton<DevAttestNetworkManager>::GetInstance()->UnregisterNetConnCallback(); 1295bbf6e98Sopenharmony_ci int32_t ret = AttestDestroyTimerTask(); 1305bbf6e98Sopenharmony_ci if (ret != DEVATTEST_SUCCESS) { 1315bbf6e98Sopenharmony_ci // Don't return 1325bbf6e98Sopenharmony_ci HILOGW("[DelayUnloadTask] Stop timer failed"); 1335bbf6e98Sopenharmony_ci } 1345bbf6e98Sopenharmony_ci ret = samgrProxy->UnloadSystemAbility(DevAttestInterface::SA_ID_DEVICE_ATTEST_SERVICE); 1355bbf6e98Sopenharmony_ci if (ret != DEVATTEST_SUCCESS) { 1365bbf6e98Sopenharmony_ci HILOGE("[DelayUnloadTask] System ability failed"); 1375bbf6e98Sopenharmony_ci return; 1385bbf6e98Sopenharmony_ci } 1395bbf6e98Sopenharmony_ci }; 1405bbf6e98Sopenharmony_ci 1415bbf6e98Sopenharmony_ci unloadHandler_->RemoveTask(ATTEST_UNLOAD_TASK_ID); 1425bbf6e98Sopenharmony_ci unloadHandler_->PostTask(task, ATTEST_UNLOAD_TASK_ID, DELAY_TIME); 1435bbf6e98Sopenharmony_ci} 1445bbf6e98Sopenharmony_ci 1455bbf6e98Sopenharmony_ciint32_t DevAttestService::CopyAttestResult(int32_t *resultArray, AttestResultInfo &attestResultInfo) 1465bbf6e98Sopenharmony_ci{ 1475bbf6e98Sopenharmony_ci if (resultArray == NULL) { 1485bbf6e98Sopenharmony_ci return DEVATTEST_FAIL; 1495bbf6e98Sopenharmony_ci } 1505bbf6e98Sopenharmony_ci int32_t *head = resultArray; 1515bbf6e98Sopenharmony_ci attestResultInfo.authResult_ = *head; 1525bbf6e98Sopenharmony_ci head++; 1535bbf6e98Sopenharmony_ci attestResultInfo.softwareResult_ = *head; 1545bbf6e98Sopenharmony_ci for (int i = 0; i < SOFTWARE_RESULT_DETAIL_SIZE; i++) { 1555bbf6e98Sopenharmony_ci attestResultInfo.softwareResultDetail_[i] = *(++head); 1565bbf6e98Sopenharmony_ci } 1575bbf6e98Sopenharmony_ci return DEVATTEST_SUCCESS; 1585bbf6e98Sopenharmony_ci} 1595bbf6e98Sopenharmony_ci 1605bbf6e98Sopenharmony_ciint32_t DevAttestService::GetAttestStatus(AttestResultInfo &attestResultInfo) 1615bbf6e98Sopenharmony_ci{ 1625bbf6e98Sopenharmony_ci int32_t resultArraySize = MAX_ATTEST_RESULT_SIZE * sizeof(int32_t); 1635bbf6e98Sopenharmony_ci int32_t *resultArray = (int32_t *)malloc(resultArraySize); 1645bbf6e98Sopenharmony_ci if (resultArray == NULL) { 1655bbf6e98Sopenharmony_ci HILOGE("[GetAttestStatus] malloc resultArray failed"); 1665bbf6e98Sopenharmony_ci return DEVATTEST_FAIL; 1675bbf6e98Sopenharmony_ci } 1685bbf6e98Sopenharmony_ci (void)memset_s(resultArray, resultArraySize, 0, resultArraySize); 1695bbf6e98Sopenharmony_ci int32_t ticketLength = 0; 1705bbf6e98Sopenharmony_ci char* ticketStr = NULL; 1715bbf6e98Sopenharmony_ci int32_t ret = DEVATTEST_SUCCESS; 1725bbf6e98Sopenharmony_ci do { 1735bbf6e98Sopenharmony_ci ret = QueryAttest(&resultArray, MAX_ATTEST_RESULT_SIZE, &ticketStr, &ticketLength); 1745bbf6e98Sopenharmony_ci if (ret != DEVATTEST_SUCCESS) { 1755bbf6e98Sopenharmony_ci HILOGE("[GetAttestStatus] QueryAttest failed"); 1765bbf6e98Sopenharmony_ci break; 1775bbf6e98Sopenharmony_ci } 1785bbf6e98Sopenharmony_ci 1795bbf6e98Sopenharmony_ci attestResultInfo.ticketLength_ = ticketLength; 1805bbf6e98Sopenharmony_ci attestResultInfo.ticket_ = (ticketStr == NULL) ? string("") : ticketStr; 1815bbf6e98Sopenharmony_ci ret = CopyAttestResult(resultArray, attestResultInfo); 1825bbf6e98Sopenharmony_ci if (ret != DEVATTEST_SUCCESS) { 1835bbf6e98Sopenharmony_ci HILOGE("[GetAttestStatus] copy attest result failed"); 1845bbf6e98Sopenharmony_ci break; 1855bbf6e98Sopenharmony_ci } 1865bbf6e98Sopenharmony_ci } while (0); 1875bbf6e98Sopenharmony_ci if (ticketStr != NULL && ticketLength != 0) { 1885bbf6e98Sopenharmony_ci free(ticketStr); 1895bbf6e98Sopenharmony_ci ticketStr = NULL; 1905bbf6e98Sopenharmony_ci } 1915bbf6e98Sopenharmony_ci free(resultArray); 1925bbf6e98Sopenharmony_ci resultArray = NULL; 1935bbf6e98Sopenharmony_ci HILOGD("[GetAttestStatus] GetAttestStatus end"); 1945bbf6e98Sopenharmony_ci return ret; 1955bbf6e98Sopenharmony_ci} 1965bbf6e98Sopenharmony_ci} // end of DevAttest 1975bbf6e98Sopenharmony_ci} // end of OHOS