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 "auth_session_key.h"
17 
18 #include <securec.h>
19 
20 #include "auth_common.h"
21 #include "auth_log.h"
22 #include "auth_manager.h"
23 #include "auth_session_fsm.h"
24 #include "softbus_adapter_mem.h"
25 #include "softbus_adapter_socket.h"
26 #include "softbus_def.h"
27 #include "softbus_errcode.h"
28 
29 #define SESSION_KEY_MAX_NUM 10
30 #define LAST_USE_THRESHOLD_MS (30 * 1000L) /* 30s */
31 
32 typedef struct {
33     int32_t index;
34     SessionKey key;
35     uint64_t lastUseTime;
36     bool isAvailable;
37     ListNode node;
38     uint32_t type;
39     uint64_t useTime[AUTH_LINK_TYPE_MAX];
40     bool isOldKey;
41 } SessionKeyItem;
42 
RemoveOldKey(SessionKeyList * list)43 static void RemoveOldKey(SessionKeyList *list)
44 {
45     uint32_t num = 0;
46     SessionKeyItem *item = NULL;
47     LIST_FOR_EACH_ENTRY(item, (const ListNode *)list, SessionKeyItem, node) {
48         num++;
49     }
50     if (num <= SESSION_KEY_MAX_NUM) {
51         return;
52     }
53 
54     SessionKeyItem *oldKey = NULL;
55     uint64_t oldKeyUseTime = UINT64_MAX;
56     LIST_FOR_EACH_ENTRY(item, (const ListNode *)list, SessionKeyItem, node) {
57         if (item->lastUseTime < oldKeyUseTime) {
58             oldKeyUseTime = item->lastUseTime;
59             oldKey = item;
60         }
61     }
62     if (oldKey == NULL) {
63         AUTH_LOGE(AUTH_FSM, "remove session key fail");
64         return;
65     }
66     ListDelete(&oldKey->node);
67     AUTH_LOGI(AUTH_FSM, "session key num reach max, remove the oldest, index=%{public}d, type=%{public}u",
68         oldKey->index, oldKey->type);
69     (void)memset_s(&oldKey->key, sizeof(SessionKey), 0, sizeof(SessionKey));
70     SoftBusFree(oldKey);
71 }
72 
InitSessionKeyList(SessionKeyList * list)73 void InitSessionKeyList(SessionKeyList *list)
74 {
75     CHECK_NULL_PTR_RETURN_VOID(list);
76     ListInit(list);
77 }
78 
SessionKeyHasAuthLinkType(uint32_t authType,AuthLinkType type)79 static bool SessionKeyHasAuthLinkType(uint32_t authType, AuthLinkType type)
80 {
81     return (authType & (1 << (uint32_t)type)) != 0;
82 }
83 
SetAuthLinkType(uint32_t * authType,AuthLinkType type)84 static void SetAuthLinkType(uint32_t *authType, AuthLinkType type)
85 {
86     *authType = (*authType) | (1 << (uint32_t)type);
87 }
88 
ClearAuthLinkType(uint32_t * authType,AuthLinkType type)89 static void ClearAuthLinkType(uint32_t *authType, AuthLinkType type)
90 {
91     *authType = (*authType) & (~(1 << (uint32_t)type));
92 }
93 
UpdateLatestUseTime(SessionKeyItem * item,AuthLinkType type)94 static void UpdateLatestUseTime(SessionKeyItem *item, AuthLinkType type)
95 {
96     if (item->lastUseTime != item->useTime[type]) {
97         item->useTime[type] = 0;
98         return;
99     }
100     item->useTime[type] = 0;
101     item->lastUseTime = 0;
102     for (uint32_t i = AUTH_LINK_TYPE_WIFI; i < AUTH_LINK_TYPE_MAX; i++) {
103         if (item->useTime[i] > item->lastUseTime) {
104             item->lastUseTime = item->useTime[i];
105         }
106     }
107 }
108 
CheckSessionKeyListExistType(const SessionKeyList * list,AuthLinkType type)109 bool CheckSessionKeyListExistType(const SessionKeyList *list, AuthLinkType type)
110 {
111     CHECK_NULL_PTR_RETURN_VALUE(list, false);
112     SessionKeyItem *item = NULL;
113     LIST_FOR_EACH_ENTRY(item, list, SessionKeyItem, node) {
114         if (SessionKeyHasAuthLinkType(item->type, type)) {
115             return true;
116         }
117     }
118     return false;
119 }
120 
CheckSessionKeyListHasOldKey(const SessionKeyList * list,AuthLinkType type)121 bool CheckSessionKeyListHasOldKey(const SessionKeyList *list, AuthLinkType type)
122 {
123     CHECK_NULL_PTR_RETURN_VALUE(list, false);
124     SessionKeyItem *item = NULL;
125     LIST_FOR_EACH_ENTRY(item, list, SessionKeyItem, node) {
126         if (SessionKeyHasAuthLinkType(item->type, type) && item->isOldKey) {
127             return true;
128         }
129     }
130     return false;
131 }
132 
ClearOldKey(const SessionKeyList * list,AuthLinkType type)133 int32_t ClearOldKey(const SessionKeyList *list, AuthLinkType type)
134 {
135     CHECK_NULL_PTR_RETURN_VALUE(list, SOFTBUS_INVALID_PARAM);
136     SessionKeyItem *item = NULL;
137     LIST_FOR_EACH_ENTRY(item, list, SessionKeyItem, node) {
138         if (SessionKeyHasAuthLinkType(item->type, type) && item->isOldKey) {
139             item->isOldKey = false;
140         }
141     }
142     return SOFTBUS_OK;
143 }
144 
DupSessionKeyList(const SessionKeyList * srcList,SessionKeyList * dstList)145 int32_t DupSessionKeyList(const SessionKeyList *srcList, SessionKeyList *dstList)
146 {
147     CHECK_NULL_PTR_RETURN_VALUE(srcList, SOFTBUS_INVALID_PARAM);
148     CHECK_NULL_PTR_RETURN_VALUE(dstList, SOFTBUS_INVALID_PARAM);
149     SessionKeyItem *item = NULL;
150     SessionKeyItem *newItem = NULL;
151     LIST_FOR_EACH_ENTRY(item, srcList, SessionKeyItem, node) {
152         newItem = (SessionKeyItem *)DupMemBuffer((uint8_t *)item, sizeof(SessionKeyItem));
153         if (newItem == NULL) {
154             DestroySessionKeyList(dstList);
155             return SOFTBUS_MALLOC_ERR;
156         }
157         ListNodeInsert(dstList, &newItem->node);
158     }
159     return SOFTBUS_OK;
160 }
161 
DestroySessionKeyList(SessionKeyList * list)162 void DestroySessionKeyList(SessionKeyList *list)
163 {
164     CHECK_NULL_PTR_RETURN_VOID(list);
165     SessionKeyItem *item = NULL;
166     SessionKeyItem *next = NULL;
167     LIST_FOR_EACH_ENTRY_SAFE(item, next, list, SessionKeyItem, node) {
168         ListDelete(&item->node);
169         (void)memset_s(&item->key, sizeof(SessionKey), 0, sizeof(SessionKey));
170         SoftBusFree(item);
171     }
172 }
173 
HasSessionKey(const SessionKeyList * list)174 bool HasSessionKey(const SessionKeyList *list)
175 {
176     CHECK_NULL_PTR_RETURN_VALUE(list, false);
177     return !IsListEmpty(list);
178 }
179 
GetSessionKeyTypeByIndex(const SessionKeyList * list,int32_t index)180 AuthLinkType GetSessionKeyTypeByIndex(const SessionKeyList *list, int32_t index)
181 {
182     CHECK_NULL_PTR_RETURN_VALUE(list, AUTH_LINK_TYPE_MAX);
183     SessionKeyItem *item = NULL;
184     uint32_t type = 0;
185     LIST_FOR_EACH_ENTRY(item, (const ListNode *)list, SessionKeyItem, node) {
186         if (item->index == index) {
187             type = item->type;
188             break;
189         }
190     }
191     if (type == 0) {
192         return AUTH_LINK_TYPE_MAX;
193     }
194     for (uint32_t i = AUTH_LINK_TYPE_WIFI; i < AUTH_LINK_TYPE_MAX; i++) {
195         if (SessionKeyHasAuthLinkType(type, (AuthLinkType)i)) {
196             AUTH_LOGI(AUTH_FSM, "auth link type=%{public}d", i);
197             return (AuthLinkType)i;
198         }
199     }
200     return AUTH_LINK_TYPE_MAX;
201 }
202 
GetLatestAvailableSessionKeyTime(const SessionKeyList * list,AuthLinkType type)203 uint64_t GetLatestAvailableSessionKeyTime(const SessionKeyList *list, AuthLinkType type)
204 {
205     CHECK_NULL_PTR_RETURN_VALUE(list, 0);
206     if (type < AUTH_LINK_TYPE_WIFI || type >= AUTH_LINK_TYPE_MAX) {
207         AUTH_LOGE(AUTH_FSM, "type error");
208         return 0;
209     }
210     SessionKeyItem *item = NULL;
211     SessionKeyItem *latestKey = NULL;
212     uint64_t latestTime = 0;
213     LIST_FOR_EACH_ENTRY(item, (const ListNode *)list, SessionKeyItem, node) {
214         if (!item->isAvailable) {
215             continue;
216         }
217         if (item->useTime[type] > latestTime) {
218             latestTime = item->useTime[type];
219             latestKey = item;
220         }
221     }
222     if (latestKey == NULL) {
223         DumpSessionkeyList(list);
224         return 0;
225     }
226     AUTH_LOGI(AUTH_FSM, "latestUseTime=%{public}" PRIu64 ", type=%{public}d, index=%{public}d, time=%{public}" PRIu64
227         ", all type=%{public}u", latestKey->lastUseTime, type, latestKey->index, latestTime, latestKey->type);
228     return latestTime;
229 }
230 
SetSessionKeyAvailable(SessionKeyList * list,int32_t index)231 int32_t SetSessionKeyAvailable(SessionKeyList *list, int32_t index)
232 {
233     CHECK_NULL_PTR_RETURN_VALUE(list, SOFTBUS_INVALID_PARAM);
234     SessionKeyItem *item = NULL;
235     LIST_FOR_EACH_ENTRY(item, (const ListNode *)list, SessionKeyItem, node) {
236         if (item->index != index) {
237             continue;
238         }
239         if (!item->isAvailable) {
240             item->isAvailable = true;
241             AUTH_LOGI(AUTH_FSM, "index=%{public}d, set available", index);
242         }
243         return SOFTBUS_OK;
244     }
245     AUTH_LOGE(AUTH_FSM, "can't find sessionKey, index=%{public}d", index);
246     return SOFTBUS_ERR;
247 }
248 
AddSessionKey(SessionKeyList * list,int32_t index,const SessionKey * key,AuthLinkType type,bool isOldKey)249 int32_t AddSessionKey(SessionKeyList *list, int32_t index, const SessionKey *key, AuthLinkType type, bool isOldKey)
250 {
251     CHECK_NULL_PTR_RETURN_VALUE(key, SOFTBUS_INVALID_PARAM);
252     CHECK_NULL_PTR_RETURN_VALUE(list, SOFTBUS_INVALID_PARAM);
253     if (type < AUTH_LINK_TYPE_WIFI || type >= AUTH_LINK_TYPE_MAX) {
254         AUTH_LOGE(AUTH_FSM, "type error");
255         return SOFTBUS_INVALID_PARAM;
256     }
257     AUTH_LOGD(AUTH_FSM, "keyLen=%{public}d", key->len);
258     SessionKeyItem *item = (SessionKeyItem *)SoftBusCalloc(sizeof(SessionKeyItem));
259     if (item == NULL) {
260         AUTH_LOGE(AUTH_FSM, "malloc SessionKeyItem fail");
261         return SOFTBUS_MALLOC_ERR;
262     }
263     item->isAvailable = false;
264     item->index = index;
265     item->lastUseTime = GetCurrentTimeMs();
266     item->useTime[type] = item->lastUseTime;
267     item->isOldKey = isOldKey;
268     SetAuthLinkType(&item->type, type);
269     if (memcpy_s(&item->key, sizeof(item->key), key, sizeof(SessionKey)) != EOK) {
270         AUTH_LOGE(AUTH_FSM, "add session key fail");
271         SoftBusFree(item);
272         return SOFTBUS_MEM_ERR;
273     }
274     ListNodeInsert((ListNode *)list, &item->node);
275     RemoveOldKey(list);
276     return SOFTBUS_OK;
277 }
278 
GetLatestSessionKey(const SessionKeyList * list,AuthLinkType type,int32_t * index,SessionKey * key)279 int32_t GetLatestSessionKey(const SessionKeyList *list, AuthLinkType type, int32_t *index, SessionKey *key)
280 {
281     CHECK_NULL_PTR_RETURN_VALUE(list, SOFTBUS_INVALID_PARAM);
282     CHECK_NULL_PTR_RETURN_VALUE(index, SOFTBUS_INVALID_PARAM);
283     CHECK_NULL_PTR_RETURN_VALUE(key, SOFTBUS_INVALID_PARAM);
284     if (type < AUTH_LINK_TYPE_WIFI || type >= AUTH_LINK_TYPE_MAX) {
285         AUTH_LOGE(AUTH_FSM, "type error");
286         return SOFTBUS_INVALID_PARAM;
287     }
288     if (IsListEmpty((const ListNode *)list)) {
289         AUTH_LOGE(AUTH_FSM, "session key list is empty");
290         return SOFTBUS_ERR;
291     }
292     SessionKeyItem *item = NULL;
293     SessionKeyItem *latestKey = NULL;
294     uint64_t latestTime = 0;
295     LIST_FOR_EACH_ENTRY(item, (const ListNode *)list, SessionKeyItem, node) {
296         if (!item->isAvailable || !SessionKeyHasAuthLinkType(item->type, type)) {
297             continue;
298         }
299         if (item->lastUseTime > latestTime) {
300             latestTime = item->lastUseTime;
301             latestKey = item;
302         }
303     }
304     if (latestKey == NULL) {
305         AUTH_LOGE(AUTH_FSM, "invalid session key item, type=%{public}d", type);
306         DumpSessionkeyList(list);
307         return SOFTBUS_ERR;
308     }
309     if (memcpy_s(key, sizeof(SessionKey), &latestKey->key, sizeof(latestKey->key)) != EOK) {
310         AUTH_LOGE(AUTH_FSM, "copy session key fail.");
311         return SOFTBUS_MEM_ERR;
312     }
313     latestKey->lastUseTime = GetCurrentTimeMs();
314     latestKey->useTime[type] = latestKey->lastUseTime;
315     *index = latestKey->index;
316     AUTH_LOGI(AUTH_FSM, "get session key succ, index=%{public}d, authtype=%{public}d, keytype=%{public}u, "
317         "time=%{public}" PRIu64, latestKey->index, type, latestKey->type, latestKey->lastUseTime);
318     return SOFTBUS_OK;
319 }
320 
SetSessionKeyAuthLinkType(const SessionKeyList * list,int32_t index,AuthLinkType type)321 int32_t SetSessionKeyAuthLinkType(const SessionKeyList *list, int32_t index, AuthLinkType type)
322 {
323     CHECK_NULL_PTR_RETURN_VALUE(list, SOFTBUS_INVALID_PARAM);
324     if (type < AUTH_LINK_TYPE_WIFI || type >= AUTH_LINK_TYPE_MAX) {
325         AUTH_LOGE(AUTH_FSM, "type error");
326         return SOFTBUS_INVALID_PARAM;
327     }
328     SessionKeyItem *item = NULL;
329     LIST_FOR_EACH_ENTRY(item, (const ListNode *)list, SessionKeyItem, node) {
330         if (item->index != index) {
331             continue;
332         }
333         SetAuthLinkType(&item->type, type);
334         item->lastUseTime = GetCurrentTimeMs();
335         item->useTime[type] = item->lastUseTime;
336         AUTH_LOGI(AUTH_FSM, "sessionKey add type, index=%{public}d, newType=%{public}d, type=%{public}u",
337             index, type, item->type);
338         return SOFTBUS_OK;
339     }
340     AUTH_LOGI(AUTH_FSM, "not found sessionKey, index=%{public}d", index);
341     return SOFTBUS_ERR;
342 }
343 
GetSessionKeyByIndex(const SessionKeyList * list,int32_t index,AuthLinkType type,SessionKey * key)344 int32_t GetSessionKeyByIndex(const SessionKeyList *list, int32_t index, AuthLinkType type, SessionKey *key)
345 {
346     CHECK_NULL_PTR_RETURN_VALUE(list, SOFTBUS_INVALID_PARAM);
347     CHECK_NULL_PTR_RETURN_VALUE(key, SOFTBUS_INVALID_PARAM);
348     if (type < AUTH_LINK_TYPE_WIFI || type >= AUTH_LINK_TYPE_MAX) {
349         AUTH_LOGE(AUTH_FSM, "type error");
350         return SOFTBUS_INVALID_PARAM;
351     }
352     SessionKeyItem *item = NULL;
353     LIST_FOR_EACH_ENTRY(item, (const ListNode *)list, SessionKeyItem, node) {
354         if (item->index != index) {
355             continue;
356         }
357         if (memcpy_s(key, sizeof(SessionKey), &item->key, sizeof(item->key)) != EOK) {
358             AUTH_LOGE(AUTH_FSM, "get session key fail, index=%{public}d", index);
359             return SOFTBUS_MEM_ERR;
360         }
361         item->lastUseTime = GetCurrentTimeMs();
362         item->useTime[type] = item->lastUseTime;
363         AUTH_LOGI(AUTH_FSM, "get session key succ, index=%{public}d, time=%{public}" PRIu64, index, item->lastUseTime);
364         return SOFTBUS_OK;
365     }
366     AUTH_LOGE(AUTH_FSM, "session key not found, index=%{public}d", index);
367     return SOFTBUS_ERR;
368 }
369 
RemoveSessionkeyByIndex(SessionKeyList * list,int32_t index,AuthLinkType type)370 void RemoveSessionkeyByIndex(SessionKeyList *list, int32_t index, AuthLinkType type)
371 {
372     CHECK_NULL_PTR_RETURN_VOID(list);
373     bool isFind = false;
374     SessionKeyItem *item = NULL;
375     LIST_FOR_EACH_ENTRY(item, (const ListNode *)list, SessionKeyItem, node) {
376         if (item->index == index) {
377             isFind = true;
378             break;
379         }
380     }
381     if (isFind) {
382         ClearAuthLinkType(&item->type, type);
383         if (item->type == 0) {
384             AUTH_LOGI(AUTH_FSM, "Remove Session key, index=%{public}d", index);
385             ListDelete(&item->node);
386             SoftBusFree(item);
387         } else {
388             UpdateLatestUseTime(item, type);
389             AUTH_LOGI(AUTH_FSM, "Remove Session key type, index=%{public}d, type=%{public}u", index, item->type);
390         }
391     } else {
392         AUTH_LOGE(AUTH_FSM, "Remove Session key not found, index=%{public}d", index);
393     }
394 }
395 
ClearSessionkeyByAuthLinkType(int64_t authId,SessionKeyList * list,AuthLinkType type)396 void ClearSessionkeyByAuthLinkType(int64_t authId, SessionKeyList *list, AuthLinkType type)
397 {
398     CHECK_NULL_PTR_RETURN_VOID(list);
399     SessionKeyItem *item = NULL;
400     SessionKeyItem *next = NULL;
401     LIST_FOR_EACH_ENTRY_SAFE(item, next, list, SessionKeyItem, node) {
402         if (!SessionKeyHasAuthLinkType(item->type, type)) {
403             continue;
404         }
405         ClearAuthLinkType(&item->type, type);
406         if (item->type == 0) {
407             AUTH_LOGI(AUTH_FSM, "remove sessionkey, type=%{public}d, index=%{public}d, authId=%{public}" PRId64,
408                 type, item->index, authId);
409             ListDelete(&item->node);
410             SoftBusFree(item);
411         } else {
412             UpdateLatestUseTime(item, type);
413         }
414     }
415 }
416 
EncryptData(const SessionKeyList * list,AuthLinkType type,const InDataInfo * inDataInfo,uint8_t * outData,uint32_t * outLen)417 int32_t EncryptData(const SessionKeyList *list, AuthLinkType type, const InDataInfo *inDataInfo,
418     uint8_t *outData, uint32_t *outLen)
419 {
420     if (list == NULL || inDataInfo == NULL || inDataInfo->inData == NULL || inDataInfo->inLen == 0 ||
421         outData == NULL || *outLen < (inDataInfo->inLen + ENCRYPT_OVER_HEAD_LEN)) {
422         AUTH_LOGE(AUTH_FSM, "invalid param");
423         return SOFTBUS_INVALID_PARAM;
424     }
425     int32_t index = 0;
426     SessionKey sessionKey;
427     if (GetLatestSessionKey(list, type, &index, &sessionKey) != SOFTBUS_OK) {
428         AUTH_LOGE(AUTH_FSM, "get key fail");
429         AUTH_LOGD(AUTH_FSM, "keyLen=%{public}d", sessionKey.len);
430         return SOFTBUS_ENCRYPT_ERR;
431     }
432     /* pack key index */
433     *(uint32_t *)outData = SoftBusHtoLl((uint32_t)index);
434     AesGcmCipherKey cipherKey = {.keyLen = sessionKey.len};
435     if (memcpy_s(cipherKey.key, SESSION_KEY_LENGTH, sessionKey.value, sessionKey.len) != EOK) {
436         AUTH_LOGE(AUTH_FSM, "set key fail");
437         (void)memset_s(&sessionKey, sizeof(SessionKey), 0, sizeof(SessionKey));
438         return SOFTBUS_MEM_ERR;
439     }
440     (void)memset_s(&sessionKey, sizeof(SessionKey), 0, sizeof(SessionKey));
441     int32_t ret = SoftBusEncryptDataWithSeq(&cipherKey, inDataInfo->inData, inDataInfo->inLen,
442         outData + ENCRYPT_INDEX_LEN, outLen, index);
443     (void)memset_s(&cipherKey, sizeof(AesGcmCipherKey), 0, sizeof(AesGcmCipherKey));
444     if (ret != SOFTBUS_OK) {
445         AUTH_LOGE(AUTH_FSM, "SoftBusEncryptDataWithSeq fail=%{public}d", ret);
446         return SOFTBUS_ENCRYPT_ERR;
447     }
448     *outLen += ENCRYPT_INDEX_LEN;
449     return SOFTBUS_OK;
450 }
451 
DecryptData(const SessionKeyList * list,AuthLinkType type,const InDataInfo * inDataInfo,uint8_t * outData,uint32_t * outLen)452 int32_t DecryptData(const SessionKeyList *list, AuthLinkType type, const InDataInfo *inDataInfo,
453     uint8_t *outData, uint32_t *outLen)
454 {
455     if (list == NULL || inDataInfo == NULL || inDataInfo->inData == NULL || outData == NULL ||
456         inDataInfo->inLen <= ENCRYPT_OVER_HEAD_LEN || *outLen < (inDataInfo->inLen - ENCRYPT_OVER_HEAD_LEN)) {
457         AUTH_LOGE(AUTH_FSM, "invalid param");
458         return SOFTBUS_INVALID_PARAM;
459     }
460     /* unpack key index */
461     int32_t index = (int32_t)SoftBusLtoHl(*(uint32_t *)inDataInfo->inData);
462     SessionKey sessionKey;
463     if (GetSessionKeyByIndex(list, index, type, &sessionKey) != SOFTBUS_OK) {
464         AUTH_LOGE(AUTH_FSM, "get key fail");
465         return SOFTBUS_DECRYPT_ERR;
466     }
467     AesGcmCipherKey cipherKey = {.keyLen = sessionKey.len};
468     if (memcpy_s(cipherKey.key, SESSION_KEY_LENGTH, sessionKey.value, sessionKey.len) != EOK) {
469         AUTH_LOGE(AUTH_FSM, "set key fail");
470         (void)memset_s(&sessionKey, sizeof(SessionKey), 0, sizeof(SessionKey));
471         return SOFTBUS_MEM_ERR;
472     }
473     (void)memset_s(&sessionKey, sizeof(SessionKey), 0, sizeof(SessionKey));
474     int32_t ret = SoftBusDecryptDataWithSeq(&cipherKey, inDataInfo->inData + ENCRYPT_INDEX_LEN,
475         inDataInfo->inLen - ENCRYPT_INDEX_LEN, outData, outLen, index);
476     (void)memset_s(&cipherKey, sizeof(AesGcmCipherKey), 0, sizeof(AesGcmCipherKey));
477     if (ret != SOFTBUS_OK) {
478         AUTH_LOGE(AUTH_FSM, "SoftBusDecryptDataWithSeq fail=%{public}d", ret);
479         return SOFTBUS_DECRYPT_ERR;
480     }
481     return SOFTBUS_OK;
482 }
483 
EncryptInner(const SessionKeyList * list,AuthLinkType type,const InDataInfo * inDataInfo,uint8_t ** outData,uint32_t * outLen)484 int32_t EncryptInner(const SessionKeyList *list, AuthLinkType type, const InDataInfo *inDataInfo,
485     uint8_t **outData, uint32_t *outLen)
486 {
487     if (list == NULL || inDataInfo == NULL || inDataInfo->inData == NULL || inDataInfo->inLen == 0 ||
488         outData == NULL || outLen == NULL) {
489         AUTH_LOGE(AUTH_FSM, "invalid param");
490         return SOFTBUS_INVALID_PARAM;
491     }
492     uint32_t encDataLen = inDataInfo->inLen + ENCRYPT_OVER_HEAD_LEN;
493     uint8_t *encData = (uint8_t *)SoftBusCalloc(encDataLen);
494     if (encData == NULL) {
495         AUTH_LOGE(AUTH_FSM, "malloc encrypt data fail");
496         return SOFTBUS_MALLOC_ERR;
497     }
498     if (EncryptData(list, type, inDataInfo, encData, &encDataLen) != SOFTBUS_OK) {
499         AUTH_LOGE(AUTH_FSM, "encrypt data fail");
500         SoftBusFree(encData);
501         return SOFTBUS_ENCRYPT_ERR;
502     }
503     *outData = encData;
504     *outLen = encDataLen;
505     return SOFTBUS_OK;
506 }
507 
DecryptInner(const SessionKeyList * list,AuthLinkType type,const InDataInfo * inDataInfo,uint8_t ** outData,uint32_t * outLen)508 int32_t DecryptInner(const SessionKeyList *list, AuthLinkType type, const InDataInfo *inDataInfo,
509     uint8_t **outData, uint32_t *outLen)
510 {
511     if (list == NULL || inDataInfo == NULL || inDataInfo->inData == NULL ||
512         inDataInfo->inLen <= ENCRYPT_OVER_HEAD_LEN || outData == NULL || outLen == NULL) {
513         AUTH_LOGE(AUTH_FSM, "invalid param");
514         return SOFTBUS_INVALID_PARAM;
515     }
516     uint32_t decDataLen = inDataInfo->inLen - ENCRYPT_OVER_HEAD_LEN + 1; /* for '\0' */
517     uint8_t *decData = (uint8_t *)SoftBusCalloc(decDataLen);
518     if (decData == NULL) {
519         AUTH_LOGE(AUTH_FSM, "malloc decrypt data fail");
520         return SOFTBUS_MALLOC_ERR;
521     }
522     if (DecryptData(list, type, inDataInfo, decData, &decDataLen) != SOFTBUS_OK) {
523         AUTH_LOGE(AUTH_FSM, "decrypt data fail");
524         SoftBusFree(decData);
525         return SOFTBUS_DECRYPT_ERR;
526     }
527     *outData = decData;
528     *outLen = decDataLen;
529     return SOFTBUS_OK;
530 }
531 
532 /* For Debug */
DumpSessionkeyList(const SessionKeyList * list)533 void DumpSessionkeyList(const SessionKeyList *list)
534 {
535     CHECK_NULL_PTR_RETURN_VOID(list);
536     uint32_t keyNum = 0;
537     SessionKeyItem *item = NULL;
538     LIST_FOR_EACH_ENTRY(item, (const ListNode *)list, SessionKeyItem, node) {
539         AUTH_LOGI(AUTH_FSM,
540             "[Dump] SessionKey keyNum=%{public}d, index=%{public}d, keyLen=%{public}u, key=XX, "
541             "lastUseTime=%{public}" PRIu64 ", type=%{public}u, useTime=%{public}" PRIu64
542             ", %{public}" PRIu64 ", %{public}" PRIu64 ", %{public}" PRIu64 ", %{public}" PRIu64,
543             keyNum, item->index, item->key.len, item->lastUseTime, item->type, item->useTime[AUTH_LINK_TYPE_WIFI],
544             item->useTime[AUTH_LINK_TYPE_BR], item->useTime[AUTH_LINK_TYPE_BLE], item->useTime[AUTH_LINK_TYPE_P2P],
545             item->useTime[AUTH_LINK_TYPE_ENHANCED_P2P]);
546         keyNum++;
547     }
548     AUTH_LOGI(AUTH_FSM, "[Dump] SessionKey total num=%{public}u", keyNum);
549 }
550 
HandleUpdateSessionKeyEvent(const void * obj)551 static void HandleUpdateSessionKeyEvent(const void *obj)
552 {
553     AUTH_CHECK_AND_RETURN_LOGE(obj != NULL, AUTH_FSM, "obj is NULL");
554     AuthHandle authHandle = *(AuthHandle *)(obj);
555     AUTH_LOGI(AUTH_FSM, "update session key begin, authId=%{public}" PRId64, authHandle.authId);
556     AuthManager *auth = GetAuthManagerByAuthId(authHandle.authId);
557     if (auth == NULL) {
558         return;
559     }
560     AuthParam authInfo = {
561         .authSeq = GenSeq(false),
562         .requestId = AuthGenRequestId(),
563         .connId = auth->connId[authHandle.type],
564         .isServer = false,
565         .isFastAuth = false,
566     };
567     if (AuthSessionStartAuth(&authInfo, &auth->connInfo[authHandle.type]) != SOFTBUS_OK) {
568         AUTH_LOGI(AUTH_FSM, "start auth session to update session key fail, authId=%{public}" PRId64,
569             authHandle.authId);
570     }
571     DelDupAuthManager(auth);
572 }
573 
RemoveUpdateSessionKeyFunc(const void * obj,void * para)574 static int32_t RemoveUpdateSessionKeyFunc(const void *obj, void *para)
575 {
576     CHECK_NULL_PTR_RETURN_VALUE(obj, SOFTBUS_ERR);
577     CHECK_NULL_PTR_RETURN_VALUE(para, SOFTBUS_ERR);
578     int64_t authId = *(int64_t *)(obj);
579     if (authId == *(int64_t *)(para)) {
580         AUTH_LOGI(AUTH_FSM, "remove update session key event, authId=%{public}" PRId64, authId);
581         return SOFTBUS_OK;
582     }
583     return SOFTBUS_ERR;
584 }
585 
ScheduleUpdateSessionKey(AuthHandle authHandle,uint64_t delayMs)586 void ScheduleUpdateSessionKey(AuthHandle authHandle, uint64_t delayMs)
587 {
588     if (authHandle.type < AUTH_LINK_TYPE_WIFI || authHandle.type >= AUTH_LINK_TYPE_MAX) {
589         AUTH_LOGE(AUTH_FSM, "authHandle type error");
590         return;
591     }
592     RemoveAuthEvent(EVENT_UPDATE_SESSION_KEY, RemoveUpdateSessionKeyFunc, (void *)(&authHandle.authId));
593     PostAuthEvent(EVENT_UPDATE_SESSION_KEY, HandleUpdateSessionKeyEvent, &authHandle, sizeof(AuthHandle), delayMs);
594 }
595 
CancelUpdateSessionKey(int64_t authId)596 void CancelUpdateSessionKey(int64_t authId)
597 {
598     RemoveAuthEvent(EVENT_UPDATE_SESSION_KEY, RemoveUpdateSessionKeyFunc, (void *)(&authId));
599 }