• 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 "devattest_service.h"
17 
18 #include <map>
19 #include <string>
20 #include <iostream>
21 
22 #include "cstdint"
23 #include "securec.h"
24 
25 #include "system_ability_definition.h"
26 #include "system_ability_status_change_stub.h"
27 #include "iservice_registry.h"
28 
29 #include "net_conn_client.h"
30 #include "net_conn_constants.h"
31 
32 #include "devattest_errno.h"
33 #include "devattest_log.h"
34 #include "devattest_system_ability_listener.h"
35 #include "attest_result_info.h"
36 #include "attest_entry.h"
37 
38 using namespace std;
39 namespace OHOS {
40 namespace DevAttest {
REGISTER_SYSTEM_ABILITY_BY_ID(DevAttestService,DEVICE_ATTEST_PROFILE_SA_ID,true)41 REGISTER_SYSTEM_ABILITY_BY_ID(DevAttestService, DEVICE_ATTEST_PROFILE_SA_ID, true)
42 
43 DevAttestService::DevAttestService(int32_t systemAbilityId, bool runOnCreate)
44     : SystemAbility(systemAbilityId, runOnCreate)
45 {
46 }
47 
DevAttestService()48 DevAttestService::DevAttestService()
49     : SystemAbility(DEVICE_ATTEST_PROFILE_SA_ID, true)
50 {
51 }
52 
~DevAttestService()53 DevAttestService::~DevAttestService()
54 {
55 }
56 
OnStart()57 void DevAttestService::OnStart()
58 {
59     HILOGI("DevAttestService OnStart");
60     if (state_ == ServiceRunningState::STATE_RUNNING) {
61         HILOGI("DevAttest Service has already started.");
62         return;
63     }
64     if (!Init()) {
65         HILOGE("Failed to init DevAttestService.");
66         return;
67     }
68     state_ = ServiceRunningState::STATE_RUNNING;
69     HILOGI("DevAttestService start success");
70     sptr<DevAttestSystemAbilityListener> devAttestSystemAbilityListener =
71         (std::make_unique<DevAttestSystemAbilityListener>()).release();
72     if (!devAttestSystemAbilityListener->AddDevAttestSystemAbilityListener(NETMANAGER_SAMGR_ID)) {
73         HILOGE("AddDevAttestSystemAbilityListener failed.");
74     }
75 }
Init()76 bool DevAttestService::Init()
77 {
78     HILOGI("DevAttestService Init begin");
79     if (!registerToSa_) {
80         bool ret = Publish(this);
81         if (!ret) {
82             HILOGE("DevAttestService Init Publish failed");
83             return false;
84         }
85         registerToSa_ = true;
86     }
87     HILOGI("DevAttestService Init Success");
88     return true;
89 }
OnStop()90 void DevAttestService::OnStop()
91 {
92     HILOGI("DevAttestService OnStop Begin");
93     state_ = ServiceRunningState::STATE_NOT_START;
94     registerToSa_ = false;
95 }
96 
CopyAttestResult(int32_t * resultArray,AttestResultInfo & attestResultInfo)97 int32_t DevAttestService::CopyAttestResult(int32_t *resultArray, AttestResultInfo &attestResultInfo)
98 {
99     if (resultArray == NULL) {
100         return DEVATTEST_FAIL;
101     }
102     int32_t *head = resultArray;
103     attestResultInfo.authResult_ = *head;
104     head++;
105     attestResultInfo.softwareResult_ = *head;
106     for (int i = 0; i < SOFTWARE_RESULT_DETAIL_SIZE; i++) {
107         attestResultInfo.softwareResultDetail_[i] = *(++head);
108     }
109     return DEVATTEST_SUCCESS;
110 }
111 
GetAttestStatus(AttestResultInfo & attestResultInfo)112 int32_t DevAttestService::GetAttestStatus(AttestResultInfo &attestResultInfo)
113 {
114     HILOGI("GetAttestStatus start");
115     int32_t resultArraySize = MAX_ATTEST_RESULT_SIZE * sizeof(int32_t);
116     int32_t *resultArray = (int32_t *)malloc(resultArraySize);
117     if (resultArray == NULL) {
118         HILOGE("malloc resultArray failed");
119         return DEVATTEST_FAIL;
120     }
121     (void)memset_s(resultArray, resultArraySize, 0, resultArraySize);
122     int32_t ticketLength = 0;
123     char* ticketStr = NULL;
124     int32_t ret = DEVATTEST_SUCCESS;
125     do {
126         ret = QueryAttest(&resultArray, MAX_ATTEST_RESULT_SIZE, &ticketStr, &ticketLength);
127         if (ret != DEVATTEST_SUCCESS) {
128             HILOGE("QueryAttest failed");
129             break;
130         }
131 
132         attestResultInfo.ticketLength_ = ticketLength;
133         attestResultInfo.ticket_ = ticketStr;
134         ret = CopyAttestResult(resultArray, attestResultInfo);
135         if (ret != DEVATTEST_SUCCESS) {
136             HILOGE("copy attest result failed");
137             break;
138         }
139     } while (0);
140     if (ticketStr != NULL && ticketLength != 0) {
141         free(ticketStr);
142         ticketStr = NULL;
143     }
144     free(resultArray);
145     resultArray = NULL;
146     HILOGI("GetAttestStatus end success");
147     return ret;
148 }
149 
150 // 根据入参判断接口权限,当前没有入参,后续确认不需要后再删除
CheckPermission(const std::string & packageName)151 bool DevAttestService::CheckPermission(const std::string &packageName)
152 {
153     HILOGI("DevAttestService CheckPermission packageName %{public}s", packageName.c_str());
154     if (packageName.empty()) {
155         HILOGE("CheckPermission param is null");
156         return false;
157     }
158     return true;
159 }
160 } // end of DevAttest
161 } // end of OHOS