• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #include "common_defs.h"
16 
17 #include "device_auth.h"
18 #include "device_auth_defines.h"
19 #include "hc_dev_info.h"
20 #include "hc_log.h"
21 #include "hc_mutex.h"
22 #include "hc_string_vector.h"
23 #include "hc_types.h"
24 #include "key_manager.h"
25 #include "securec.h"
26 #include "hidump_adapter.h"
27 #include "os_account_adapter.h"
28 #include "pseudonym_manager.h"
29 #include "security_label_adapter.h"
30 #include "account_task_manager.h"
31 #include "mini_session_manager.h"
32 #include "hc_time.h"
33 
34 #define TIME_OUT_VALUE_LIGHT_AUTH 300
35 #define MAX_SESSION_NUM_LIGHT_AUTH 30
36 
37 typedef struct {
38     LightSession *session;
39     int64_t createTime;
40 } LightSessionInfo;
41 
42 DECLARE_HC_VECTOR(LightSessionInfoVec, LightSessionInfo)
43 IMPLEMENT_HC_VECTOR(LightSessionInfoVec, LightSessionInfo, 1)
44 
45 static LightSessionInfoVec g_lightSessionInfoList;
46 static HcMutex g_lightSessionMutex;
47 
CreateSession(int64_t requestId,int32_t osAccountId,const char * serviceId,DataBuff randomBuff)48 static LightSession *CreateSession(int64_t requestId, int32_t osAccountId, const char *serviceId, DataBuff randomBuff)
49 {
50     LightSession *newSession = (LightSession *)HcMalloc(sizeof(LightSession), 0);
51     if (newSession == NULL) {
52         LOGE("Failed to alloc newSession");
53         return NULL;
54     }
55     newSession->osAccountId = osAccountId;
56     uint32_t randomLen = randomBuff.length;
57     newSession->randomLen = randomLen;
58     newSession->randomVal = (uint8_t *)HcMalloc(randomLen, 0);
59     if (newSession->randomVal == NULL) {
60         LOGE("HcMalloc randomVal failed");
61         HcFree(newSession);
62         return NULL;
63     }
64     if (memcpy_s(newSession->randomVal, randomLen, randomBuff.data, randomLen) != EOK) {
65         LOGE("Copy randomVal failed.");
66         HcFree(newSession->randomVal);
67         HcFree(newSession);
68         return NULL;
69     }
70     newSession->requestId = requestId;
71     uint32_t serviceIdLen = HcStrlen(serviceId) + 1;
72     newSession->serviceId = (char *)HcMalloc(serviceIdLen, 0);
73     if (newSession->serviceId == NULL) {
74         LOGE("Copy serviceId failed.");
75         HcFree(newSession->randomVal);
76         HcFree(newSession);
77         return NULL;
78     }
79     if (memcpy_s(newSession->serviceId, serviceIdLen, serviceId, serviceIdLen) != EOK) {
80         LOGE("Copy serviceId failed.");
81         HcFree(newSession->serviceId);
82         HcFree(newSession->randomVal);
83         HcFree(newSession);
84         return NULL;
85     }
86     return newSession;
87 }
88 
DestroyLightSession(LightSession * lightSessionEntry)89 static void DestroyLightSession(LightSession *lightSessionEntry)
90 {
91     if (lightSessionEntry == NULL) {
92         return;
93     }
94     if (lightSessionEntry->serviceId != NULL) {
95         HcFree(lightSessionEntry->serviceId);
96     }
97     if (lightSessionEntry->randomVal != NULL) {
98         HcFree(lightSessionEntry->randomVal);
99     }
100     HcFree(lightSessionEntry);
101     return;
102 }
103 
RemoveTimeOutSession(void)104 static void RemoveTimeOutSession(void)
105 {
106     uint32_t index = 0;
107     while (index < g_lightSessionInfoList.size(&(g_lightSessionInfoList))) {
108         LightSessionInfo *lightSessionInfo = g_lightSessionInfoList.getp(&(g_lightSessionInfoList), index);
109         int64_t runningTime = HcGetIntervalTime(lightSessionInfo->createTime);
110         if (runningTime < TIME_OUT_VALUE_LIGHT_AUTH) {
111             index++;
112             continue;
113         }
114         LightSession *session = lightSessionInfo->session;
115         LOGI("session timeout. [Id]: %" LOG_PUB PRId64, session->requestId);
116         LOGI("session timeout. [TimeLimit(/s)]: %" LOG_PUB "d, [RunningTime(/s)]: %" LOG_PUB PRId64,
117             TIME_OUT_VALUE_LIGHT_AUTH, runningTime);
118         DestroyLightSession(session);
119         HC_VECTOR_POPELEMENT(&g_lightSessionInfoList, lightSessionInfo, index);
120     }
121 }
122 
InitLightSessionManager(void)123 int32_t InitLightSessionManager(void)
124 {
125     int32_t res = InitHcMutex(&g_lightSessionMutex, false);
126     if (res != HC_SUCCESS) {
127         LOGE("Init light session mutex failed.");
128         return res;
129     }
130     g_lightSessionInfoList = CREATE_HC_VECTOR(LightSessionInfoVec);
131     return HC_SUCCESS;
132 }
133 
DestroyLightSessionManager(void)134 void DestroyLightSessionManager(void)
135 {
136     uint32_t index;
137     LightSessionInfo *entry;
138     (void)LockHcMutex(&g_lightSessionMutex);
139     FOR_EACH_HC_VECTOR(g_lightSessionInfoList, index, entry) {
140         if (entry == NULL) {
141             continue;
142         }
143         DestroyLightSession(entry->session);
144     }
145     DESTROY_HC_VECTOR(LightSessionInfoVec, &g_lightSessionInfoList);
146     UnlockHcMutex(&g_lightSessionMutex);
147     DestroyHcMutex(&g_lightSessionMutex);
148 }
149 
QueryLightSession(int64_t requestId,int32_t osAccountId,uint8_t ** randomVal,uint32_t * randomLen,char ** serviceId)150 int32_t QueryLightSession(int64_t requestId, int32_t osAccountId, uint8_t **randomVal,
151     uint32_t *randomLen, char **serviceId)
152 {
153     (void)LockHcMutex(&g_lightSessionMutex);
154     RemoveTimeOutSession();
155     uint32_t index = 0;
156     LightSessionInfo *entry;
157     FOR_EACH_HC_VECTOR(g_lightSessionInfoList, index, entry) {
158         if (entry == NULL || entry->session == NULL) {
159             continue;
160         }
161         if (requestId == entry->session->requestId && osAccountId == entry->session->osAccountId) {
162             *randomLen = entry->session->randomLen;
163             *randomVal = (uint8_t *)HcMalloc(entry->session->randomLen, 0);
164             if (*randomVal == NULL) {
165                 LOGE("Malloc randomVal failed.");
166                 UnlockHcMutex(&g_lightSessionMutex);
167                 return HC_ERR_MEMORY_COPY;
168             }
169             if (memcpy_s(*randomVal, entry->session->randomLen, entry->session->randomVal,
170                 entry->session->randomLen) != EOK) {
171                 HcFree(*randomVal);
172                 LOGE("Copy randomVal failed.");
173                 UnlockHcMutex(&g_lightSessionMutex);
174                 return HC_ERR_MEMORY_COPY;
175             }
176             uint32_t serviceIdLen = (uint32_t)HcStrlen(entry->session->serviceId) + 1;
177             *serviceId = (char *)HcMalloc(serviceIdLen, 0);
178             if (*serviceId == NULL) {
179                 HcFree(*randomVal);
180                 LOGE("Malloc serviceId failed.");
181                 UnlockHcMutex(&g_lightSessionMutex);
182                 return HC_ERR_MEMORY_COPY;
183             }
184             if (memcpy_s(*serviceId, serviceIdLen, entry->session->serviceId, serviceIdLen) != EOK) {
185                 HcFree(*randomVal);
186                 HcFree(*serviceId);
187                 LOGE("Copy serviceId failed.");
188                 UnlockHcMutex(&g_lightSessionMutex);
189                 return HC_ERR_MEMORY_COPY;
190             }
191             LOGI("Light session found. [ReqId]: %" LOG_PUB PRId64 ", [OsAccountId]: %" LOG_PUB "d",
192                 requestId, osAccountId);
193             UnlockHcMutex(&g_lightSessionMutex);
194             return HC_SUCCESS;
195         }
196     }
197     LOGI("Light session not exists. ");
198     UnlockHcMutex(&g_lightSessionMutex);
199     return HC_ERR_SESSION_NOT_EXIST;
200 }
201 
AddLightSession(int64_t requestId,int32_t osAccountId,const char * serviceId,DataBuff randomBuff)202 int32_t AddLightSession(int64_t requestId, int32_t osAccountId, const char *serviceId, DataBuff randomBuff)
203 {
204     (void)LockHcMutex(&g_lightSessionMutex);
205     RemoveTimeOutSession();
206     if (g_lightSessionInfoList.size(&g_lightSessionInfoList) >= MAX_SESSION_NUM_LIGHT_AUTH) {
207         LOGE("Reach max session num!");
208         UnlockHcMutex(&g_lightSessionMutex);
209         return HC_ERR_OUT_OF_LIMIT;
210     }
211     LightSessionInfo newLightSessionInfo;
212     LightSession *newSession = CreateSession(requestId, osAccountId, serviceId, randomBuff);
213     if (newSession == NULL) {
214         LOGE("create session fail.");
215         UnlockHcMutex(&g_lightSessionMutex);
216         return HC_ERR_MEMORY_COPY;
217     }
218     newLightSessionInfo.session = newSession;
219     newLightSessionInfo.createTime = HcGetCurTime();
220     if (g_lightSessionInfoList.pushBackT(&g_lightSessionInfoList, newLightSessionInfo) == NULL) {
221         LOGE("push session to list fail.");
222         DestroyLightSession(newSession);
223         UnlockHcMutex(&g_lightSessionMutex);
224         return HC_ERR_MEMORY_COPY;
225     }
226     UnlockHcMutex(&g_lightSessionMutex);
227     return HC_SUCCESS;
228 }
229 
DeleteLightSession(int64_t requestId,int32_t osAccountId)230 int32_t DeleteLightSession(int64_t requestId, int32_t osAccountId)
231 {
232     (void)LockHcMutex(&g_lightSessionMutex);
233     RemoveTimeOutSession();
234     uint32_t index = 0;
235     LightSessionInfo *ptr = NULL;
236     FOR_EACH_HC_VECTOR(g_lightSessionInfoList, index, ptr) {
237         if (ptr == NULL) {
238             continue;
239         }
240         LightSession *session = ptr->session;
241         if (session == NULL) {
242             continue;
243         }
244         if (requestId == session->requestId && osAccountId == session->osAccountId) {
245             DestroyLightSession(session);
246             HC_VECTOR_POPELEMENT(&g_lightSessionInfoList, ptr, index);
247             LOGI("Light session delete. [ReqId]: %" LOG_PUB PRId64 ", [OsAccountId]: %"
248                 LOG_PUB "d", requestId, osAccountId);
249             UnlockHcMutex(&g_lightSessionMutex);
250             return HC_SUCCESS;
251         }
252     }
253     UnlockHcMutex(&g_lightSessionMutex);
254     return HC_SUCCESS;
255 }