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 <stdio.h>
17 #include <unistd.h>
18 #include <securec.h>
19 #include <registry.h>
20 #include <iunknown.h>
21 #include <samgr_lite.h>
22 #include <iproxy_client.h>
23 #include <iproxy_server.h>
24
25 #include "devattest_errno.h"
26 #include "devattest_log.h"
27 #include "attest_framework_define.h"
28 #include "devattest_interface.h"
29
30 #define MAX_TICKET_SIZE 49
31
32 typedef struct {
33 INHERIT_CLIENT_IPROXY;
34 int32_t(*StartProc)(IUnknown *iUnknown);
35 int32_t(*QueryStatus)(IUnknown *iUnknown, AttestResultInfo *attestResultInfo);
36 } AttestClientProxy;
37
38 typedef struct {
39 INHERIT_IUNKNOWNENTRY(AttestClientProxy);
40 } AttestClientEntry;
41
42 static AttestClientProxy *g_clientProxy;
43
ReadAttestResultTicket(IpcIo * reply,AttestResultInfo ** attestStatus)44 static int32_t ReadAttestResultTicket(IpcIo *reply, AttestResultInfo **attestStatus)
45 {
46 if ((attestStatus == NULL) || (*attestStatus == NULL) || (reply == NULL)) {
47 HILOGE("[ReadAttestResultTicket] Invalid parameter.");
48 return DEVATTEST_FAIL;
49 }
50 AttestResultInfo *attestResult = *attestStatus;
51 size_t len = 0;
52 char *ticket = (char *)ReadString(reply, &len);
53 if (ticket == NULL) {
54 HILOGE("[ReadAttestResultTicket] Failed to ReadString.");
55 return DEVATTEST_FAIL;
56 }
57 len = strlen(ticket) + 1;
58 if (len > MAX_TICKET_SIZE) {
59 HILOGE("[ReadAttestResultTicket] The len is too large.");
60 return DEVATTEST_FAIL;
61 }
62 char* backTicket = (char *)malloc(len);
63 if (backTicket == NULL) {
64 HILOGE("[ReadAttestResultTicket] Failed to malloc backTicket.");
65 return DEVATTEST_FAIL;
66 }
67 (void)memset_s(backTicket, len, 0, len);
68 if (memcpy_s(backTicket, len, ticket, len) != 0) {
69 HILOGE("[ReadAttestResultTicket] Failed to copy ticket.");
70 free(backTicket);
71 return DEVATTEST_FAIL;
72 }
73 attestResult->ticket = backTicket;
74 return DEVATTEST_SUCCESS;
75 }
76
ReadAttestResultInfo(IpcIo * reply,AttestResultInfo ** attestStatus)77 static int32_t ReadAttestResultInfo(IpcIo *reply, AttestResultInfo **attestStatus)
78 {
79 if ((attestStatus == NULL) || (*attestStatus == NULL) || (reply == NULL)) {
80 HILOGE("[ReadAttestResultInfo] Invalid parameter.");
81 return DEVATTEST_FAIL;
82 }
83 AttestResultInfo *attestResult = *attestStatus;
84
85 if (!ReadInt32(reply, (int32_t *)&attestResult->authResult) ||
86 !ReadInt32(reply, (int32_t *)&attestResult->softwareResult)) {
87 HILOGE("[ReadAttestResultInfo] Failed to ReadInt32.");
88 return DEVATTEST_FAIL;
89 }
90
91 size_t len = 0;
92 int32_t *softwareResultDetail = ReadInt32Vector(reply, &len);
93 size_t size = sizeof(attestResult->softwareResultDetail);
94 if (softwareResultDetail == NULL || (len != (size / sizeof(int32_t)))) {
95 HILOGE("[ReadAttestResultInfo] Failed to softwareResultDetail_.");
96 return DEVATTEST_FAIL;
97 }
98 if (memcpy_s(attestResult->softwareResultDetail, size, softwareResultDetail, size) != 0) {
99 HILOGE("[ReadAttestResultInfo] Failed to copy softwareResultDetail.");
100 return DEVATTEST_FAIL;
101 }
102
103 if (!ReadInt32(reply, (int32_t *)&attestResult->ticketLength)) {
104 HILOGE("[ReadAttestResultInfo] Failed to read ticketLength.");
105 return DEVATTEST_FAIL;
106 }
107
108 return ReadAttestResultTicket(reply, attestStatus);
109 }
110
AttestClientQueryStatusCb(void * owner,int code,IpcIo * reply)111 static int AttestClientQueryStatusCb(void *owner, int code, IpcIo *reply)
112 {
113 (void)code;
114 if ((owner == NULL) || (reply == NULL)) {
115 HILOGE("[AttestClientQueryStatusCb] owner or reply is nullptr.");
116 return DEVATTEST_FAIL;
117 }
118
119 ServiceRspMsg *respInfo = (ServiceRspMsg *)owner;
120
121 if (!ReadInt32(reply, &respInfo->result)) {
122 HILOGE("[AttestClientQueryStatusCb] Failed to ReadInt32.");
123 return DEVATTEST_FAIL;
124 }
125 if (respInfo->result != DEVATTEST_SUCCESS) {
126 HILOGE("[AttestClientQueryStatusCb] Failed to QueryStatus, result:%d.", respInfo->result);
127 return DEVATTEST_FAIL;
128 }
129 return ReadAttestResultInfo(reply, &respInfo->attestResultInfo);
130 }
131
StartProc(IUnknown * iUnknown)132 static int32_t StartProc(IUnknown *iUnknown)
133 {
134 if (iUnknown == NULL) {
135 HILOGE("[StartProc] Get proxy failed.");
136 return DEVATTEST_FAIL;
137 }
138 AttestClientProxy *proxy = (AttestClientProxy *)iUnknown;
139 int32_t ret = proxy->Invoke((IClientProxy *)proxy, ATTEST_FRAMEWORK_MSG_PROC, NULL, NULL, NULL);
140 if (ret != DEVATTEST_SUCCESS) {
141 HILOGE("[StartProc] Invoke failed.");
142 return DEVATTEST_FAIL;
143 }
144 return DEVATTEST_SUCCESS;
145 }
146
QueryStatus(IUnknown * iUnknown,AttestResultInfo * attestResultInfo)147 static int32_t QueryStatus(IUnknown *iUnknown, AttestResultInfo *attestResultInfo)
148 {
149 if (iUnknown == NULL) {
150 HILOGE("[QueryStatus] Get proxy failed.");
151 return DEVATTEST_FAIL;
152 }
153 AttestClientProxy *proxy = (AttestClientProxy *)iUnknown;
154 ServiceRspMsg reply = {0};
155 reply.attestResultInfo = attestResultInfo;
156 int32_t ret = proxy->Invoke((IClientProxy *)proxy, ATTEST_FRAMEWORK_MSG_QUERY, NULL,
157 &reply, AttestClientQueryStatusCb);
158 if (ret != DEVATTEST_SUCCESS) {
159 HILOGE("[QueryStatus] Invoke failed.");
160 return DEVATTEST_FAIL;
161 }
162 if (reply.result != DEVATTEST_SUCCESS) {
163 HILOGE("[QueryStatus] Service return failed, result = %d", reply.result);
164 return DEVATTEST_FAIL;
165 }
166 return DEVATTEST_SUCCESS;
167 }
168
CreateClient(const char * service,const char * feature,uint32 size)169 static void *CreateClient(const char *service, const char *feature, uint32 size)
170 {
171 (void)service;
172 (void)feature;
173 uint32 len = size + sizeof(AttestClientEntry);
174 uint8 *client = malloc(len);
175 if (client == NULL) {
176 return NULL;
177 }
178 (void)memset_s(client, len, 0, len);
179 AttestClientEntry *entry = (AttestClientEntry *)&client[size];
180 if (entry == NULL) {
181 free(client);
182 client = NULL;
183 return NULL;
184 }
185 entry->ver = ((uint16)SERVER_PROXY_VER | (uint16)DEFAULT_VERSION);
186 entry->ref = 1;
187 entry->iUnknown.QueryInterface = IUNKNOWN_QueryInterface;
188 entry->iUnknown.AddRef = IUNKNOWN_AddRef;
189 entry->iUnknown.Release = IUNKNOWN_Release;
190 entry->iUnknown.Invoke = NULL;
191 entry->iUnknown.StartProc = StartProc;
192 entry->iUnknown.QueryStatus = QueryStatus;
193 return client;
194 }
195
DestroyClient(const char * service,const char * feature,void * iproxy)196 static void DestroyClient(const char *service, const char *feature, void *iproxy)
197 {
198 (void)service;
199 (void)feature;
200 if (iproxy != NULL) {
201 free(iproxy);
202 }
203 }
204
ModuleSamgrInitialize(void)205 static int32_t ModuleSamgrInitialize(void)
206 {
207 int32_t ret = (int32_t)SAMGR_RegisterFactory(ATTEST_SERVICE, ATTEST_FEATURE, CreateClient, DestroyClient);
208 if (ret != DEVATTEST_SUCCESS) {
209 return DEVATTEST_FAIL;
210 }
211 return DEVATTEST_SUCCESS;
212 }
213
GetModuleClientApi(void)214 static int32_t GetModuleClientApi(void)
215 {
216 IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(ATTEST_SERVICE, ATTEST_FEATURE);
217 if (iUnknown == NULL) {
218 return DEVATTEST_FAIL;
219 }
220 int32_t ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_clientProxy);
221 if (ret != DEVATTEST_SUCCESS || g_clientProxy == NULL) {
222 return DEVATTEST_FAIL;
223 }
224 return DEVATTEST_SUCCESS;
225 }
226
StartDevAttestTask(void)227 int32_t StartDevAttestTask(void)
228 {
229 int32_t ret = ModuleSamgrInitialize();
230 if (ret != DEVATTEST_SUCCESS) {
231 HILOGE("[StartDevAttestTask] Failed to Initialize!");
232 return DEVATTEST_FAIL;
233 }
234
235 ret = GetModuleClientApi();
236 if ((ret != DEVATTEST_SUCCESS) || (g_clientProxy == NULL)) {
237 HILOGE("[StartDevAttestTask] Get failed!");
238 return DEVATTEST_FAIL;
239 }
240
241 if (g_clientProxy->StartProc == NULL) {
242 HILOGE("[StartDevAttestTask] Interface not found!");
243 (void)g_clientProxy->Release((IUnknown *)g_clientProxy);
244 return DEVATTEST_FAIL;
245 }
246
247 ret = g_clientProxy->StartProc((IUnknown *)g_clientProxy);
248 if (ret != DEVATTEST_SUCCESS) {
249 HILOGE("[StartDevAttestTask] Interface execution failed!");
250 }
251 (void)g_clientProxy->Release((IUnknown *)g_clientProxy);
252 return ret;
253 }
254
GetAttestStatus(AttestResultInfo * attestResultInfo)255 int32_t GetAttestStatus(AttestResultInfo *attestResultInfo)
256 {
257 if (attestResultInfo == NULL) {
258 return DEVATTEST_FAIL;
259 }
260
261 int32_t ret = ModuleSamgrInitialize();
262 if (ret != DEVATTEST_SUCCESS) {
263 HILOGE("[GetAttestStatus] Failed to Initialize!");
264 return DEVATTEST_FAIL;
265 }
266
267 ret = GetModuleClientApi();
268 if ((ret != DEVATTEST_SUCCESS) || (g_clientProxy == NULL)) {
269 HILOGE("[GetAttestStatus] Get failed!");
270 return DEVATTEST_FAIL;
271 }
272
273 if (g_clientProxy->QueryStatus == NULL) {
274 HILOGE("[GetAttestStatus] Interface not found!");
275 (void)g_clientProxy->Release((IUnknown *)g_clientProxy);
276 return DEVATTEST_FAIL;
277 }
278
279 ret = g_clientProxy->QueryStatus((IUnknown *)g_clientProxy, attestResultInfo);
280 if (ret != DEVATTEST_SUCCESS) {
281 HILOGE("[GetAttestStatus] Interface execution failed!");
282 }
283 (void)g_clientProxy->Release((IUnknown *)g_clientProxy);
284 return ret;
285 }