1 /*
2 * Copyright (c) 2023 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_hichain_adapter.h"
17
18 #include <regex.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <securec.h>
22
23 #include "auth_common.h"
24 #include "auth_hichain.h"
25 #include "auth_session_fsm.h"
26 #include "device_auth.h"
27 #include "device_auth_defines.h"
28 #include "lnn_decision_db.h"
29 #include "softbus_adapter_mem.h"
30 #include "softbus_errcode.h"
31 #include "softbus_log.h"
32
33 #define UDID_REGEX_PATTERN "[0-9A-Fa-f]{16,}"
34 #define CUST_UDID_LEN 16
35 #define AUTH_APPID "softbus_auth"
36 #define RETRY_TIMES 16
37 #define RETRY_MILLSECONDS 500
38 static const GroupAuthManager *g_hichain = NULL;
39
InitHichain(void)40 static const GroupAuthManager *InitHichain(void)
41 {
42 int32_t ret = InitDeviceAuthService();
43 if (ret != 0) {
44 ALOGE("hichain InitDeviceAuthService failed(err = %d)", ret);
45 return NULL;
46 }
47 const GroupAuthManager *gaIns = GetGaInstance();
48 if (gaIns == NULL) {
49 ALOGE("hichain GetGaInstance failed");
50 DestroyDeviceAuthService();
51 return NULL;
52 }
53 ALOGI("hichain init succ");
54 return gaIns;
55 }
56
RegChangeListener(const char * appId,DataChangeListener * listener)57 int32_t RegChangeListener(const char *appId, DataChangeListener *listener)
58 {
59 AUTH_CHECK_AND_RETURN_RET_LOG(listener != NULL, SOFTBUS_ERR, "listener is null");
60 if (g_hichain == NULL) {
61 g_hichain = InitHichain();
62 }
63 AUTH_CHECK_AND_RETURN_RET_LOG(g_hichain != NULL, SOFTBUS_ERR, "hichain not initialized");
64
65 const DeviceGroupManager *gmInstance = GetGmInstance();
66 AUTH_CHECK_AND_RETURN_RET_LOG(gmInstance != NULL, SOFTBUS_ERR, "hichain GetGmInstance failed");
67
68 int32_t ret = gmInstance->regDataChangeListener(appId, listener);
69 AUTH_CHECK_AND_RETURN_RET_LOG(ret == 0, SOFTBUS_ERR, "hichain regDataChangeListener failed: %d", ret);
70
71 return SOFTBUS_OK;
72 }
73
UnregChangeListener(const char * appId)74 int32_t UnregChangeListener(const char *appId)
75 {
76 const DeviceGroupManager *gmInstance = GetGmInstance();
77 AUTH_CHECK_AND_RETURN_RET_LOG(gmInstance != NULL, SOFTBUS_ERR, "hichain GetGmInstance failed");
78
79 int32_t ret = gmInstance->unRegDataChangeListener(appId);
80 AUTH_CHECK_AND_RETURN_RET_LOG(ret == 0, SOFTBUS_ERR, "hichain unRegDataChangeListener failed: %d", ret);
81
82 return SOFTBUS_OK;
83 }
84
AuthDevice(int64_t authReqId,const char * authParams,const DeviceAuthCallback * cb)85 int32_t AuthDevice(int64_t authReqId, const char *authParams, const DeviceAuthCallback *cb)
86 {
87 AUTH_CHECK_AND_RETURN_RET_LOG(authParams != NULL && cb != NULL, SOFTBUS_INVALID_PARAM, "authParams or cb is null");
88 if (g_hichain == NULL) {
89 g_hichain = InitHichain();
90 }
91 AUTH_CHECK_AND_RETURN_RET_LOG(g_hichain != NULL, SOFTBUS_ERR, "hichain not initialized");
92
93 for (int32_t i = 1; i < RETRY_TIMES; i++) {
94 int32_t ret = g_hichain->authDevice(ANY_OS_ACCOUNT, authReqId, authParams, cb);
95 if (ret == HC_SUCCESS) {
96 ALOGI("hichain call authDevice success, times = %d", i);
97 return SOFTBUS_OK;
98 }
99 if (ret != HC_ERR_INVALID_PARAMS) {
100 ALOGE("hichain call authDevice failed, err = %d", ret);
101 return SOFTBUS_ERR;
102 }
103 ALOGW("hichain retry call authDevice, current retry times = %d, err = %d", i, ret);
104 (void)SoftBusSleepMs(RETRY_MILLSECONDS);
105 }
106 return SOFTBUS_ERR;
107 }
108
ProcessAuthData(int64_t authSeq,const uint8_t * data,uint32_t len,DeviceAuthCallback * cb)109 int32_t ProcessAuthData(int64_t authSeq, const uint8_t *data, uint32_t len, DeviceAuthCallback *cb)
110 {
111 AUTH_CHECK_AND_RETURN_RET_LOG(data != NULL && cb != NULL, SOFTBUS_INVALID_PARAM, "data or cb is null");
112 if (g_hichain == NULL) {
113 g_hichain = InitHichain();
114 }
115 AUTH_CHECK_AND_RETURN_RET_LOG(g_hichain != NULL, SOFTBUS_ERR, "hichain not initialized");
116
117 int32_t ret = g_hichain->processData(authSeq, data, len, cb);
118 AUTH_CHECK_AND_RETURN_RET_LOG(ret == 0, SOFTBUS_ERR, "hichain processData failed: %d", ret);
119
120 return SOFTBUS_OK;
121 }
122
CheckDeviceInGroupByType(const char * udid,const char * uuid,HichainGroup groupType)123 bool CheckDeviceInGroupByType(const char *udid, const char *uuid, HichainGroup groupType)
124 {
125 (void)udid;
126 (void)uuid;
127 (void)groupType;
128 return true;
129 }
130
DestroyDeviceAuth(void)131 void DestroyDeviceAuth(void)
132 {
133 DestroyDeviceAuthService();
134 g_hichain = NULL;
135 ALOGI("hichain destroy succ");
136 }
137
IsPotentialTrustedDevice(TrustedRelationIdType idType,const char * deviceId,bool isPrecise)138 bool IsPotentialTrustedDevice(TrustedRelationIdType idType, const char *deviceId, bool isPrecise)
139 {
140 (void)idType;
141 (void)isPrecise;
142 AUTH_CHECK_AND_RETURN_RET_LOG(deviceId != NULL, false, "invalid param");
143
144 uint32_t num = 0;
145 char *udidArray = NULL;
146 if (LnnGetTrustedDevInfoFromDb(&udidArray, &num) != SOFTBUS_OK) {
147 ALOGE("get trusted dev info fail");
148 return false;
149 }
150 if (udidArray == NULL || num == 0) {
151 ALOGI("get none trusted node");
152 return false;
153 }
154 regex_t regComp;
155 if (regcomp(®Comp, UDID_REGEX_PATTERN, REG_EXTENDED | REG_NOSUB) != 0) {
156 ALOGE("get trusted dev udid regcomp fail");
157 SoftBusFree(udidArray);
158 return true;
159 }
160 for (uint32_t i = 0; i < num; i++) {
161 char udidSubStr[UDID_BUF_LEN] = {0};
162 char hashStr[CUST_UDID_LEN + 1] = {0};
163 uint8_t udidHash[SHA_256_HASH_LEN] = {0};
164 if (regexec(®Comp, udidArray + i * UDID_BUF_LEN, 0, NULL, 0) != 0) {
165 continue;
166 }
167 if (memcpy_s(udidSubStr, UDID_BUF_LEN, udidArray + i * UDID_BUF_LEN, UDID_BUF_LEN) != EOK) {
168 ALOGE("memcpy_s udidSubStr fail");
169 break;
170 }
171 if (SoftBusGenerateStrHash((const unsigned char *)udidSubStr, strlen(udidSubStr), udidHash) != SOFTBUS_OK) {
172 continue;
173 }
174 if (ConvertBytesToHexString(hashStr, CUST_UDID_LEN + 1, udidHash,
175 CUST_UDID_LEN / HEXIFY_UNIT_LEN) != SOFTBUS_OK) {
176 continue;
177 }
178 if (strncmp(hashStr, deviceId, strlen(deviceId)) == 0) {
179 SoftBusFree(udidArray);
180 regfree(®Comp);
181 return true;
182 }
183 }
184 SoftBusFree(udidArray);
185 regfree(®Comp);
186 return false;
187 }
188
HichainGetJoinedGroups(int32_t groupType)189 uint32_t HichainGetJoinedGroups(int32_t groupType)
190 {
191 uint32_t groupCnt = 0;
192 char *accountGroups = NULL;
193
194 const DeviceGroupManager *gmInstance = GetGmInstance();
195 AUTH_CHECK_AND_RETURN_RET_LOG(gmInstance != NULL, groupCnt, "hichain GetGmInstance failed");
196
197 if (gmInstance->getJoinedGroups(0, AUTH_APPID, (GroupType)groupType, &accountGroups, &groupCnt) != 0) {
198 ALOGE("hichain getJoinedGroups groupCnt fail.");
199 groupCnt = 0;
200 }
201 if (accountGroups != NULL) {
202 SoftBusFree(accountGroups);
203 }
204 return groupCnt;
205 }
206