• 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 "auth_session_common_util.h"
17 #include "common_defs.h"
18 #include "string_util.h"
19 #include "device_auth_defines.h"
20 #include "hc_log.h"
21 #include "hc_types.h"
22 #define UID_HEX_STRING_LEN_MAX 64
23 #define UID_HEX_STRING_LEN_MIN 10
24 
IsPeerUidLenValid(uint32_t peerUserIdLen)25 static bool IsPeerUidLenValid(uint32_t peerUserIdLen)
26 {
27     if ((peerUserIdLen < UID_HEX_STRING_LEN_MIN) || (peerUserIdLen > UID_HEX_STRING_LEN_MAX)) {
28         LOGE("The input userId len is invalid, input userId in hex string len = %d", peerUserIdLen);
29         return false;
30     }
31     return true;
32 }
33 
AddPeerIdToReqParam(const CJson * receiveData,CJson * reqParam)34 static int32_t AddPeerIdToReqParam(const CJson *receiveData, CJson *reqParam)
35 {
36     const char *peerId = GetStringFromJson(receiveData, FIELD_PEER_AUTH_ID);
37     if (peerId == NULL) {
38         LOGE("Failed to get peerId from the data transmitted by the client!");
39         return HC_ERR_JSON_GET;
40     }
41     uint32_t peerIdLen = HcStrlen(peerId);
42     if ((peerIdLen == 0) || (peerIdLen > MAX_AUTH_ID_LEN) || ((peerIdLen % 2) != 0)) { /* 2: even numbers. */
43         LOGE("Invalid len of peerId!");
44         return HC_ERR_JSON_GET;
45     }
46     uint32_t peerIdToRequestLen = peerIdLen / BYTE_TO_HEX_OPER_LENGTH;
47     uint8_t *peerIdToRequest = (uint8_t *)HcMalloc(peerIdToRequestLen + 1, 0);
48     if (peerIdToRequest == NULL) {
49         LOGE("Malloc failed for peerIdToRequest!");
50         return HC_ERR_ALLOC_MEMORY;
51     }
52     int32_t res = HexStringToByte(peerId, peerIdToRequest, peerIdToRequestLen);
53     if (res != HC_SUCCESS) {
54         LOGE("Failed to convert peerId to byte, res: %d!", res);
55         HcFree(peerIdToRequest);
56         return res;
57     }
58     if (AddStringToJson(reqParam, FIELD_PEER_AUTH_ID, (const char *)peerIdToRequest) != HC_SUCCESS) {
59         LOGE("Failed to add reqParam: peerId for onRequest!");
60         HcFree(peerIdToRequest);
61         return HC_ERR_JSON_FAIL;
62     }
63     HcFree(peerIdToRequest);
64     return HC_SUCCESS;
65 }
66 
GetServerConfirmation(const CJson * paramsFromClient,const CJson * reqParam,const DeviceAuthCallback * callback)67 char *GetServerConfirmation(const CJson *paramsFromClient, const CJson *reqParam,
68     const DeviceAuthCallback *callback)
69 {
70     int64_t requestId = 0;
71     if (GetByteFromJson(paramsFromClient, FIELD_REQUEST_ID, (uint8_t *)&requestId, sizeof(int64_t)) != HC_SUCCESS) {
72         LOGE("Failed to get request id!");
73         return NULL;
74     }
75     char *reqParamStr = PackJsonToString(reqParam);
76     if (reqParamStr == NULL) {
77         LOGE("Failed to pack reqParam!");
78         return NULL;
79     }
80     char *serverInfo = NULL;
81     do {
82         int32_t authForm = AUTH_FORM_INVALID_TYPE;
83         if (GetIntFromJson(paramsFromClient, FIELD_AUTH_FORM, &authForm) != HC_SUCCESS) {
84             LOGE("Failed to get authForm in received data!");
85             break;
86         }
87         if ((callback == NULL) || (callback->onRequest == NULL)) {
88             LOGE("Failed to get request callback!");
89             break;
90         }
91         LOGD("Begin to invoke onRequest for auth!");
92         serverInfo = callback->onRequest(requestId, authForm, reqParamStr);
93         LOGD("End to invoke onRequest for auth!");
94         if (serverInfo == NULL) {
95             LOGE("Failed to get server confirmation info!");
96         }
97     } while (0);
98     FreeJsonString(reqParamStr);
99     return serverInfo;
100 }
101 
102 
GetGeneralReqParams(const CJson * receiveData,CJson * reqParam)103 int32_t GetGeneralReqParams(const CJson *receiveData, CJson *reqParam)
104 {
105     const char *pkgName = GetStringFromJson(receiveData, FIELD_PKG_NAME);
106     if (pkgName == NULL) {
107         LOGE("Failed to get pkgName from the data transmitted by the client!");
108         return HC_ERR_JSON_GET;
109     }
110     if (AddStringToJson(reqParam, FIELD_SERVICE_PKG_NAME, pkgName) != HC_SUCCESS) {
111         LOGE("Failed to add reqParam: pkgName for onRequest!");
112         return HC_ERR_JSON_FAIL;
113     }
114     const char *serviceType = GetStringFromJson(receiveData, FIELD_SERVICE_TYPE);
115     if (serviceType == NULL) {
116         LOGE("Failed to get serviceType from the data transmitted by the client!");
117         return HC_ERR_JSON_GET;
118     }
119     if (AddStringToJson(reqParam, FIELD_SERVICE_TYPE, serviceType) != HC_SUCCESS) {
120         LOGE("Failed to add reqParam: serviceType for onRequest!");
121         return HC_ERR_JSON_FAIL;
122     }
123     int32_t res = AddPeerIdToReqParam(receiveData, reqParam);
124     if (res != HC_SUCCESS) {
125         LOGE("Failed to add peerId to reqParam!");
126         return res;
127     }
128     int32_t peerType = 0;
129     if (GetIntFromJson(receiveData, FIELD_PEER_USER_TYPE, &peerType) != HC_SUCCESS) {
130         LOGE("Failed to get peerType from the data transmitted by the client!");
131         return HC_ERR_JSON_GET;
132     }
133     if (AddIntToJson(reqParam, FIELD_PEER_USER_TYPE, peerType) != HC_SUCCESS) {
134         LOGE("Failed to add reqParam: peerType for onRequest!");
135         return HC_ERR_JSON_FAIL;
136     }
137     int32_t keyLen = DEFAULT_RETURN_KEY_LENGTH;
138     (void)GetIntFromJson(receiveData, FIELD_KEY_LENGTH, &keyLen);
139     if (AddIntToJson(reqParam, FIELD_KEY_LENGTH, keyLen) != HC_SUCCESS) {
140         LOGE("Failed to add reqParam: keyLen for onRequest!");
141         return HC_ERR_JSON_FAIL;
142     }
143     return HC_SUCCESS;
144 }
145 
IsUserIdEqual(const char * userIdInDb,const char * peerUserIdInDb)146 bool IsUserIdEqual(const char *userIdInDb, const char *peerUserIdInDb)
147 {
148     if ((userIdInDb == NULL) || (peerUserIdInDb == NULL)) {
149         LOGE("Input is null for user id!");
150         return false;
151     }
152     char *peerUidToUpper = NULL;
153     if (ToUpperCase(peerUserIdInDb, &peerUidToUpper) != HC_SUCCESS) {
154         LOGE("Failed to convert the input userId to upper case!");
155         return false;
156     }
157     uint32_t userIdInDbLen = HcStrlen(userIdInDb);
158     uint32_t peerUserIdLen = HcStrlen(peerUserIdInDb);
159     if (!IsPeerUidLenValid(peerUserIdLen)) {
160         HcFree(peerUidToUpper);
161         peerUidToUpper = NULL;
162         return false;
163     }
164     uint32_t cmpLen = (userIdInDbLen > peerUserIdLen) ? peerUserIdLen : userIdInDbLen;
165     if (memcmp(userIdInDb, peerUidToUpper, cmpLen) == EOK) {
166         HcFree(peerUidToUpper);
167         peerUidToUpper = NULL;
168         return true;
169     }
170     HcFree(peerUidToUpper);
171     peerUidToUpper = NULL;
172     return false;
173 }
174 
GetDuplicateServicePkgName(const CJson * params)175 char *GetDuplicateServicePkgName(const CJson *params)
176 {
177     const char *pkgName = GetStringFromJson(params, FIELD_SERVICE_PKG_NAME);
178     if (pkgName == NULL) {
179         LOGE("Failed to get pkgName from json!");
180         return NULL;
181     }
182     uint32_t pkgNameLen = HcStrlen(pkgName) + 1;
183     char *copyPkgName = (char *)HcMalloc(pkgNameLen, 0);
184     if (copyPkgName == NULL) {
185         LOGE("Failed to allocate copyPkgName memory!");
186         return NULL;
187     }
188     (void)memcpy_s(copyPkgName, pkgNameLen, pkgName, pkgNameLen);
189     return copyPkgName;
190 }