• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 "device_auth.h"
17 
18 #include "alg_loader.h"
19 #include "callback_manager.h"
20 #include "channel_manager.h"
21 #include "common_defs.h"
22 #include "dev_auth_module_manager.h"
23 #include "group_auth_manager.h"
24 #include "group_manager.h"
25 #include "hc_init_protection.h"
26 #include "hc_log.h"
27 #include "json_utils.h"
28 #include "lcm_adapter.h"
29 #include "os_account_adapter.h"
30 #include "session_manager.h"
31 #include "task_manager.h"
32 
33 static GroupAuthManager *g_groupAuthManager =  NULL;
34 static DeviceGroupManager *g_groupManagerInstance = NULL;
35 
DestroyGroupAuthTask(HcTaskBase * task)36 static void DestroyGroupAuthTask(HcTaskBase *task)
37 {
38     AuthDeviceTask *realTask = (AuthDeviceTask *)task;
39     FreeJson(realTask->authParams);
40 }
41 
InitAuthDeviceTask(int32_t osAccountId,AuthDeviceTask * task,int64_t authReqId,CJson * authParams,const DeviceAuthCallback * gaCallback)42 static bool InitAuthDeviceTask(int32_t osAccountId, AuthDeviceTask *task, int64_t authReqId, CJson *authParams,
43     const DeviceAuthCallback *gaCallback)
44 {
45     task->base.doAction = DoAuthDevice;
46     task->base.destroy = DestroyGroupAuthTask;
47     task->authReqId = authReqId;
48     if (AddByteToJson(authParams, FIELD_REQUEST_ID, (const uint8_t*)&authReqId, sizeof(int64_t)) != HC_SUCCESS) {
49         LOGE("Failed to add requestId to json!");
50         return false;
51     }
52     if (AddIntToJson(authParams, FIELD_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) {
53         LOGE("Failed to add os accountId to json for auth device!");
54         return false;
55     }
56     task->authParams = authParams;
57     task->callback = gaCallback;
58     if (task->callback == NULL) {
59         LOGE("The input auth callback is null!");
60         return false;
61     }
62     return true;
63 }
64 
InitProcessDataTask(AuthDeviceTask * task,int64_t authReqId,CJson * receivedData,const DeviceAuthCallback * gaCallback)65 static bool InitProcessDataTask(AuthDeviceTask *task, int64_t authReqId,
66     CJson *receivedData, const DeviceAuthCallback *gaCallback)
67 {
68     task->base.doAction = DoProcessAuthData;
69     task->base.destroy = DestroyGroupAuthTask;
70     task->authReqId = authReqId;
71     if (AddByteToJson(receivedData, FIELD_REQUEST_ID, (const uint8_t*)&authReqId, sizeof(int64_t)) != HC_SUCCESS) {
72         LOGE("Failed to add requestId to json!");
73         return false;
74     }
75     if (AddIntToJson(receivedData, FIELD_OPERATION_CODE, AUTHENTICATE) != HC_SUCCESS) {
76         LOGE("Failed to add operation code to json!");
77         return false;
78     }
79     task->authParams = receivedData;
80     task->callback = gaCallback;
81     if (task->callback == NULL) {
82         LOGE("The input auth callback is null!");
83         return false;
84     }
85     return true;
86 }
87 
AuthDevice(int32_t osAccountId,int64_t authReqId,const char * authParams,const DeviceAuthCallback * gaCallback)88 static int32_t AuthDevice(int32_t osAccountId, int64_t authReqId, const char *authParams,
89     const DeviceAuthCallback *gaCallback)
90 {
91     LOGI("Begin AuthDevice.");
92     osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
93     if ((authParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
94         LOGE("The input auth params is invalid!");
95         return HC_ERR_INVALID_PARAMS;
96     }
97     CJson *jsonParams = CreateJsonFromString(authParams);
98     if (jsonParams == NULL) {
99         LOGE("Create json from params failed!");
100         return HC_ERR_JSON_FAIL;
101     }
102     AuthDeviceTask *task = (AuthDeviceTask *)HcMalloc(sizeof(AuthDeviceTask), 0);
103     if (task == NULL) {
104         FreeJson(jsonParams);
105         LOGE("Failed to allocate memory for task!");
106         return HC_ERR_ALLOC_MEMORY;
107     }
108     if (!InitAuthDeviceTask(osAccountId, task, authReqId, jsonParams, gaCallback)) {
109         LOGE("Failed to init task!");
110         FreeJson(jsonParams);
111         HcFree(task);
112         return HC_ERR_INIT_TASK_FAIL;
113     }
114     if (PushTask((HcTaskBase*)task) != HC_SUCCESS) {
115         FreeJson(jsonParams);
116         HcFree(task);
117         return HC_ERR_INIT_TASK_FAIL;
118     }
119     LOGI("Push AuthDevice task successfully.");
120     return HC_SUCCESS;
121 }
122 
ProcessData(int64_t authReqId,const uint8_t * data,uint32_t dataLen,const DeviceAuthCallback * gaCallback)123 static int32_t ProcessData(int64_t authReqId, const uint8_t *data, uint32_t dataLen,
124     const DeviceAuthCallback *gaCallback)
125 {
126     LOGI("Begin ProcessData.");
127     if ((data == NULL) || (dataLen > MAX_DATA_BUFFER_SIZE)) {
128         LOGE("Invalid input for ProcessData!");
129         return HC_ERR_INVALID_PARAMS;
130     }
131     CJson *receivedData = CreateJsonFromString((const char *)data);
132     if (receivedData == NULL) {
133         LOGE("Create Json for input data failed!");
134         return HC_ERR_JSON_FAIL;
135     }
136     AuthDeviceTask *task = (AuthDeviceTask *)HcMalloc(sizeof(AuthDeviceTask), 0);
137     if (task == NULL) {
138         FreeJson(receivedData);
139         LOGE("Failed to allocate memory for task!");
140         return HC_ERR_ALLOC_MEMORY;
141     }
142     if (!InitProcessDataTask(task, authReqId, receivedData, gaCallback)) {
143         LOGE("Failed to init task!");
144         FreeJson(receivedData);
145         HcFree(task);
146         return HC_ERR_INIT_TASK_FAIL;
147     }
148     if (PushTask((HcTaskBase*)task) != HC_SUCCESS) {
149         FreeJson(receivedData);
150         HcFree(task);
151         return HC_ERR_INIT_TASK_FAIL;
152     }
153     LOGI("Push ProcessData task successfully.");
154     return HC_SUCCESS;
155 }
156 
AllocGmAndGa(void)157 static int32_t AllocGmAndGa(void)
158 {
159     if (g_groupManagerInstance == NULL) {
160         g_groupManagerInstance = (DeviceGroupManager *)HcMalloc(sizeof(DeviceGroupManager), 0);
161         if (g_groupManagerInstance == NULL) {
162             LOGE("Failed to allocate groupManager Instance memory!");
163             return HC_ERR_ALLOC_MEMORY;
164         }
165     }
166     if (g_groupAuthManager == NULL) {
167         g_groupAuthManager = (GroupAuthManager *)HcMalloc(sizeof(GroupAuthManager), 0);
168         if (g_groupAuthManager == NULL) {
169             LOGE("Failed to allocate groupAuth Instance memory!");
170             HcFree(g_groupManagerInstance);
171             g_groupManagerInstance = NULL;
172             return HC_ERR_ALLOC_MEMORY;
173         }
174     }
175     return HC_SUCCESS;
176 }
177 
DestroyGmAndGa(void)178 static void DestroyGmAndGa(void)
179 {
180     if (g_groupAuthManager != NULL) {
181         HcFree(g_groupAuthManager);
182         g_groupAuthManager = NULL;
183     }
184     if (g_groupManagerInstance != NULL) {
185         HcFree(g_groupManagerInstance);
186         g_groupManagerInstance = NULL;
187     }
188 }
189 
InitAllModules(void)190 static int32_t InitAllModules(void)
191 {
192     int32_t res = GetLoaderInstance()->initAlg();
193     if (res != HC_SUCCESS) {
194         LOGE("[End]: [Service]: Failed to init algorithm module!");
195         return res;
196     }
197     res = InitModules();
198     if (res != HC_SUCCESS) {
199         LOGE("[End]: [Service]: Failed to init all authenticator modules!");
200         return res;
201     }
202     res = InitCallbackManager();
203     if (res != HC_SUCCESS) {
204         LOGE("[End]: [Service]: Failed to init callback manage module!");
205         goto CLEAN_MODULE;
206     }
207     res = InitGroupManager();
208     if (res != HC_SUCCESS) {
209         goto CLEAN_CALLBACK;
210     }
211     InitSessionManager();
212     res = InitTaskManager();
213     if (res != HC_SUCCESS) {
214         LOGE("[End]: [Service]: Failed to init worker thread!");
215         goto CLEAN_ALL;
216     }
217     return res;
218 CLEAN_ALL:
219     DestroySessionManager();
220     DestroyGroupManager();
221 CLEAN_CALLBACK:
222     DestroyCallbackManager();
223 CLEAN_MODULE:
224     DestroyModules();
225     return res;
226 }
227 
InitDeviceAuthService(void)228 DEVICE_AUTH_API_PUBLIC int InitDeviceAuthService(void)
229 {
230     LOGI("[Service]: Start to init device auth service!");
231     if (CheckInit() == FINISH_INIT) {
232         LOGI("[End]: [Service]: Device auth service is running!");
233         return HC_SUCCESS;
234     }
235     int32_t res = AllocGmAndGa();
236     if (res != HC_SUCCESS) {
237         return res;
238     }
239     res = InitAllModules();
240     if (res != HC_SUCCESS) {
241         DestroyGmAndGa();
242         return res;
243     }
244     SetInitStatus();
245     LOGI("[End]: [Service]: Init device auth service successfully!");
246     return HC_SUCCESS;
247 }
248 
DestroyDeviceAuthService(void)249 DEVICE_AUTH_API_PUBLIC void DestroyDeviceAuthService(void)
250 {
251     LOGI("[Service]: Start to destroy device auth service!");
252     if (CheckDestroy() == FINISH_DESTROY) {
253         LOGI("[End]: [Service]: The service has not been initialized!");
254         return;
255     }
256     DestroyTaskManager();
257     DestroyGroupManager();
258     DestroySessionManager();
259     DestroyGmAndGa();
260     DestroyModules();
261     DestroyChannelManager();
262     DestroyCallbackManager();
263     SetDeInitStatus();
264     LOGI("[End]: [Service]: Destroy device auth service successfully!");
265 }
266 
GetGmInstance(void)267 DEVICE_AUTH_API_PUBLIC const DeviceGroupManager *GetGmInstance(void)
268 {
269     if (g_groupManagerInstance == NULL) {
270         LOGE("Service not init.");
271         return NULL;
272     }
273 
274     g_groupManagerInstance->regCallback = RegGroupManagerCallback;
275     g_groupManagerInstance->unRegCallback = UnRegGroupManagerCallback;
276     g_groupManagerInstance->regDataChangeListener = RegListenerImpl;
277     g_groupManagerInstance->unRegDataChangeListener = UnRegListenerImpl;
278     g_groupManagerInstance->createGroup = CreateGroupImpl;
279     g_groupManagerInstance->deleteGroup = DeleteGroupImpl;
280     g_groupManagerInstance->addMemberToGroup = AddMemberToGroupImpl;
281     g_groupManagerInstance->deleteMemberFromGroup = DeleteMemberFromGroupImpl;
282     g_groupManagerInstance->processData = ProcessBindDataImpl;
283     g_groupManagerInstance->processCredential = ProcessCredential;
284     g_groupManagerInstance->getRegisterInfo = GetRegisterInfo;
285     g_groupManagerInstance->checkAccessToGroup = CheckAccessToGroupImpl;
286     g_groupManagerInstance->getPkInfoList = GetPkInfoListImpl;
287     g_groupManagerInstance->getGroupInfoById = GetGroupInfoByIdImpl;
288     g_groupManagerInstance->getGroupInfo = GetGroupInfoImpl;
289     g_groupManagerInstance->getJoinedGroups = GetJoinedGroupsImpl;
290     g_groupManagerInstance->getRelatedGroups = GetRelatedGroupsImpl;
291     g_groupManagerInstance->getDeviceInfoById = GetDeviceInfoByIdImpl;
292     g_groupManagerInstance->getTrustedDevices = GetTrustedDevicesImpl;
293     g_groupManagerInstance->isDeviceInGroup = IsDeviceInGroupImpl;
294     g_groupManagerInstance->destroyInfo = DestroyInfoImpl;
295     return g_groupManagerInstance;
296 }
297 
GetGaInstance(void)298 DEVICE_AUTH_API_PUBLIC const GroupAuthManager *GetGaInstance(void)
299 {
300     if (g_groupAuthManager == NULL) {
301         LOGE("Service not init.");
302         return NULL;
303     }
304 
305     g_groupAuthManager->processData = ProcessData;
306     g_groupAuthManager->authDevice = AuthDevice;
307     return g_groupAuthManager;
308 }
309