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