1 /*
2 * Copyright (c) 2024 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_normalize_request.h"
17
18 #include <securec.h>
19
20 #include "anonymizer.h"
21 #include "auth_common.h"
22 #include "auth_log.h"
23 #include "auth_manager.h"
24 #include "softbus_adapter_mem.h"
25 #include "softbus_def.h"
26
27 #define UDID_SHORT_HASH_STR 16
28
29 static ListNode g_normalizeRequestList = { &g_normalizeRequestList, &g_normalizeRequestList };
30
GetSameRequestNum(char * udidHash)31 static uint32_t GetSameRequestNum(char *udidHash)
32 {
33 uint32_t num = 0;
34 NormalizeRequest *item = NULL;
35 char *anonyUdidHash = NULL;
36 Anonymize(udidHash, &anonyUdidHash);
37 AUTH_LOGI(AUTH_HICHAIN, "udidHash=%{public}s", AnonymizeWrapper(anonyUdidHash));
38 AnonymizeFree(anonyUdidHash);
39 LIST_FOR_EACH_ENTRY(item, &g_normalizeRequestList, NormalizeRequest, node) {
40 if (strncmp(item->udidHash, udidHash, UDID_SHORT_HASH_STR) != 0) {
41 continue;
42 }
43 num++;
44 }
45 return num;
46 }
47
GetNotifyRequestListByUdidHash(char * udidHash,NormalizeRequest ** requests,uint32_t * num)48 static int32_t GetNotifyRequestListByUdidHash(char *udidHash, NormalizeRequest **requests, uint32_t *num)
49 {
50 if (udidHash == NULL) {
51 AUTH_LOGE(AUTH_HICHAIN, "udidHash is null");
52 return SOFTBUS_INVALID_PARAM;
53 }
54 *num = GetSameRequestNum(udidHash);
55 if ((*num) == 0) {
56 AUTH_LOGI(AUTH_HICHAIN, "no other requests exist.");
57 return SOFTBUS_OK;
58 }
59 *requests = (NormalizeRequest *)SoftBusCalloc(sizeof(NormalizeRequest) * (*num));
60 if (*requests == NULL) {
61 AUTH_LOGE(AUTH_HICHAIN, "malloc fail.");
62 return SOFTBUS_MEM_ERR;
63 }
64 NormalizeRequest *item = NULL;
65 NormalizeRequest *next = NULL;
66 uint32_t index = 0;
67 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_normalizeRequestList, NormalizeRequest, node) {
68 if (strncmp(item->udidHash, udidHash, UDID_SHORT_HASH_STR) != 0 || index >= (*num) ||
69 item->isNeedNotifyVerify) {
70 continue;
71 }
72 (*requests)[index++] = *item;
73 item->isNeedNotifyVerify = true;
74 }
75 return SOFTBUS_OK;
76 }
77
FindAndDelNormalizeRequest(int64_t authSeq,NormalizeRequest * request)78 static int32_t FindAndDelNormalizeRequest(int64_t authSeq, NormalizeRequest *request)
79 {
80 if (request == NULL) {
81 return SOFTBUS_INVALID_PARAM;
82 }
83 NormalizeRequest *item = NULL;
84 LIST_FOR_EACH_ENTRY(item, &g_normalizeRequestList, NormalizeRequest, node) {
85 if (item->authSeq == authSeq) {
86 *request = *item;
87 ListDelete(&item->node);
88 SoftBusFree(item);
89 return SOFTBUS_OK;
90 }
91 }
92 return SOFTBUS_AUTH_NOT_FOUND;
93 }
94
GetNormalizeRequestList(int64_t authSeq,NormalizeRequest * request,NormalizeRequest ** requests,uint32_t * num)95 static int32_t GetNormalizeRequestList(
96 int64_t authSeq, NormalizeRequest *request, NormalizeRequest **requests, uint32_t *num)
97 {
98 if (num == NULL) {
99 return SOFTBUS_INVALID_PARAM;
100 }
101 if (!RequireAuthLock()) {
102 AUTH_LOGE(AUTH_HICHAIN, "RequireAuthLock fail");
103 return SOFTBUS_LOCK_ERR;
104 }
105 if (FindAndDelNormalizeRequest(authSeq, request) != SOFTBUS_OK) {
106 AUTH_LOGE(AUTH_HICHAIN, "not found normalize request");
107 ReleaseAuthLock();
108 return SOFTBUS_AUTH_INNER_ERR;
109 }
110 int32_t ret = GetNotifyRequestListByUdidHash(request->udidHash, requests, num);
111 ReleaseAuthLock();
112 return ret;
113 }
114
DelAuthNormalizeRequest(int64_t authSeq)115 void DelAuthNormalizeRequest(int64_t authSeq)
116 {
117 if (!RequireAuthLock()) {
118 AUTH_LOGE(AUTH_HICHAIN, "RequireAuthLock fail");
119 return;
120 }
121 NormalizeRequest *item = NULL;
122 LIST_FOR_EACH_ENTRY(item, &g_normalizeRequestList, NormalizeRequest, node) {
123 if (item->authSeq == authSeq) {
124 ListDelete(&item->node);
125 SoftBusFree(item);
126 AUTH_LOGI(AUTH_HICHAIN, "del normalize request authSeq=%{public}" PRId64, authSeq);
127 break;
128 }
129 }
130 ReleaseAuthLock();
131 }
132
AuthIsRepeatedAuthRequest(int64_t authSeq)133 bool AuthIsRepeatedAuthRequest(int64_t authSeq)
134 {
135 if (!RequireAuthLock()) {
136 AUTH_LOGE(AUTH_HICHAIN, "RequireAuthLock fail");
137 return false;
138 }
139 NormalizeRequest *item = NULL;
140 LIST_FOR_EACH_ENTRY(item, &g_normalizeRequestList, NormalizeRequest, node) {
141 if (item->authSeq == authSeq) {
142 ReleaseAuthLock();
143 return true;
144 }
145 }
146 ReleaseAuthLock();
147 return false;
148 }
149
AddNormalizeRequest(const NormalizeRequest * request)150 uint32_t AddNormalizeRequest(const NormalizeRequest *request)
151 {
152 CHECK_NULL_PTR_RETURN_VALUE(request, 0);
153 NormalizeRequest *newRequest = SoftBusCalloc(sizeof(NormalizeRequest));
154 if (newRequest == NULL) {
155 AUTH_LOGE(AUTH_CONN, "malloc AuthRequest fail");
156 return 0;
157 }
158 *newRequest = *request;
159 if (!RequireAuthLock()) {
160 AUTH_LOGE(AUTH_CONN, "lock fail");
161 SoftBusFree(newRequest);
162 return 0;
163 }
164 ListTailInsert(&g_normalizeRequestList, &newRequest->node);
165 uint32_t waitNum = GetSameRequestNum(newRequest->udidHash);
166 ReleaseAuthLock();
167 return waitNum;
168 }
169
NotifyNormalizeRequestSuccess(int64_t authSeq,bool isSupportNego)170 void NotifyNormalizeRequestSuccess(int64_t authSeq, bool isSupportNego)
171 {
172 NormalizeRequest *requests = NULL;
173 NormalizeRequest request = { 0 };
174 uint32_t num = 0;
175 if (GetNormalizeRequestList(authSeq, &request, &requests, &num) != SOFTBUS_OK) {
176 AUTH_LOGI(AUTH_HICHAIN, "get hichain request fail: authSeq=%{public}" PRId64, authSeq);
177 return;
178 }
179 if (num == 0 || requests == NULL) {
180 AUTH_LOGE(AUTH_HICHAIN, "requests is NULL");
181 return;
182 }
183 AUTH_LOGI(AUTH_HICHAIN, "request num=%{public}d", num);
184 for (uint32_t i = 0; i < num; i++) {
185 if (isSupportNego && requests[i].connInfo.type == request.connInfo.type) {
186 continue;
187 }
188 AUTH_LOGI(AUTH_HICHAIN, "notify AuthSessionSaveSessionKey: authSeq=%{public}" PRId64, requests[i].authSeq);
189 (void)AuthNotifyRequestVerify(requests[i].authSeq);
190 }
191 SoftBusFree(requests);
192 }
193
NotifyNormalizeRequestFail(int64_t authSeq,int32_t ret)194 void NotifyNormalizeRequestFail(int64_t authSeq, int32_t ret)
195 {
196 (void)ret;
197 NormalizeRequest *requests = NULL;
198 NormalizeRequest request = { 0 };
199 uint32_t num = 0;
200 if (GetNormalizeRequestList(authSeq, &request, &requests, &num) != SOFTBUS_OK) {
201 AUTH_LOGI(AUTH_HICHAIN, "get hichain request fail: authSeq=%{public}" PRId64, authSeq);
202 return;
203 }
204 if (num == 0 || requests == NULL) {
205 return;
206 }
207 for (uint32_t i = 0; i < num; i++) {
208 if (AuthNotifyRequestVerify(requests[i].authSeq) == SOFTBUS_OK) {
209 AUTH_LOGI(AUTH_HICHAIN, "continue auth, authSeq=%{public}" PRId64, requests[i].authSeq);
210 break;
211 }
212 }
213 SoftBusFree(requests);
214 }