• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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_HASH_HEX_STRING_LEN_MAX 64
23 #define UID_HASH_HEX_STRING_LEN_MIN 10
24 
IsPeerUidHashLenValid(uint32_t peerUidHashLen)25 static bool IsPeerUidHashLenValid(uint32_t peerUidHashLen)
26 {
27     if ((peerUidHashLen < UID_HASH_HEX_STRING_LEN_MIN) || (peerUidHashLen > UID_HASH_HEX_STRING_LEN_MAX)) {
28         LOGE("The input uid hash len is invalid, input uid hash in hex string len = %d", peerUidHashLen);
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 
IsUidHashEqual(const char * uidHashInDb,const char * peerUidHash)146 bool IsUidHashEqual(const char *uidHashInDb, const char *peerUidHash)
147 {
148     if ((uidHashInDb == NULL) || (peerUidHash == NULL)) {
149         LOGE("Input is null for uid hash!");
150         return false;
151     }
152     char *peerUidHashToUpper = NULL;
153     int32_t result = ToUpperCase(peerUidHash, &peerUidHashToUpper);
154     if (result != HC_SUCCESS) {
155         LOGE("Failed to convert the input uidHash to upper case!");
156         return result;
157     }
158     uint32_t uidHashInDbLen = strlen(uidHashInDb);
159     uint32_t peerUidHashLen = strlen(peerUidHash);
160     if (!IsPeerUidHashLenValid(peerUidHashLen)) {
161         HcFree(peerUidHashToUpper);
162         peerUidHashToUpper = NULL;
163         return false;
164     }
165     uint32_t cmpHashLen = (uidHashInDbLen > peerUidHashLen) ? peerUidHashLen : uidHashInDbLen;
166     if (memcmp(uidHashInDb, peerUidHashToUpper, cmpHashLen) == EOK) {
167         HcFree(peerUidHashToUpper);
168         peerUidHashToUpper = NULL;
169         return true;
170     }
171     HcFree(peerUidHashToUpper);
172     peerUidHashToUpper = NULL;
173     return false;
174 }