• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "creds_operation_utils.h"
17 
18 #include "creds_manager_defines.h"
19 #include "device_auth.h"
20 #include "device_auth_defines.h"
21 #include "group_auth_data_operation.h"
22 #include "hc_dev_info.h"
23 #include "hc_log.h"
24 #include "hc_types.h"
25 #include "json_utils.h"
26 #include "string_util.h"
27 
28 IMPLEMENT_HC_VECTOR(ProtocolEntityVec, ProtocolEntity*, 1)
29 IMPLEMENT_HC_VECTOR(IdentityInfoVec, IdentityInfo*, 1)
30 
SetProtocolsForPinType(IdentityInfo * info)31 static int32_t SetProtocolsForPinType(IdentityInfo *info)
32 {
33 #ifdef ENABLE_P2P_BIND_EC_SPEKE
34     ProtocolEntity *ecSpekeEntity = (ProtocolEntity *)HcMalloc(sizeof(ProtocolEntity), 0);
35     if (ecSpekeEntity == NULL) {
36         LOGE("Failed to alloc memory for ec speke entity!");
37         return HC_ERR_ALLOC_MEMORY;
38     }
39     ecSpekeEntity->protocolType = ALG_EC_SPEKE;
40     ecSpekeEntity->expandProcessCmds = CMD_EXCHANGE_PK | CMD_ADD_TRUST_DEVICE;
41     info->protocolVec.pushBack(&info->protocolVec, (const ProtocolEntity **)&ecSpekeEntity);
42 #endif
43 
44     ProtocolEntity *dlSpekeEntity = (ProtocolEntity *)HcMalloc(sizeof(ProtocolEntity), 0);
45     if (dlSpekeEntity == NULL) {
46         LOGE("Failed to alloc memory for dl speke entity!");
47         return HC_ERR_ALLOC_MEMORY;
48     }
49     dlSpekeEntity->protocolType = ALG_DL_SPEKE;
50     dlSpekeEntity->expandProcessCmds = CMD_IMPORT_AUTH_CODE | CMD_ADD_TRUST_DEVICE;
51     info->protocolVec.pushBack(&info->protocolVec, (const ProtocolEntity **)&dlSpekeEntity);
52 
53 #ifdef ENABLE_P2P_BIND_ISO
54     ProtocolEntity *isoEntity = (ProtocolEntity *)HcMalloc(sizeof(ProtocolEntity), 0);
55     if (isoEntity == NULL) {
56         LOGE("Failed to alloc memory for iso entity!");
57         return HC_ERR_ALLOC_MEMORY;
58     }
59     isoEntity->protocolType = ALG_ISO;
60     isoEntity->expandProcessCmds = CMD_IMPORT_AUTH_CODE | CMD_ADD_TRUST_DEVICE;
61     info->protocolVec.pushBack(&info->protocolVec, (const ProtocolEntity **)&isoEntity);
62 #endif
63 
64     return HC_SUCCESS;
65 }
66 
SetProtocolsForUidType(IdentityInfo * info)67 static int32_t SetProtocolsForUidType(IdentityInfo *info)
68 {
69 #ifdef ENABLE_ACCOUNT_AUTH_ISO
70     ProtocolEntity *entity = (ProtocolEntity *)HcMalloc(sizeof(ProtocolEntity), 0);
71     if (entity == NULL) {
72         LOGE("Failed to alloc memory for entity!");
73         return HC_ERR_ALLOC_MEMORY;
74     }
75     entity->protocolType = ALG_ISO;
76     entity->expandProcessCmds = CMD_ADD_TRUST_DEVICE;
77     info->protocolVec.pushBack(&info->protocolVec, (const ProtocolEntity **)&entity);
78 #else
79     (void)info;
80 #endif
81 
82     return HC_SUCCESS;
83 }
84 
SetProtocolsForP2pType(int32_t keyType,IdentityInfo * info)85 static int32_t SetProtocolsForP2pType(int32_t keyType, IdentityInfo *info)
86 {
87     if (keyType == KEY_TYPE_ASYM) {
88     #ifdef ENABLE_P2P_AUTH_EC_SPEKE
89         ProtocolEntity *entity = (ProtocolEntity *)HcMalloc(sizeof(ProtocolEntity), 0);
90         if (entity == NULL) {
91             LOGE("Failed to alloc memory for entity!");
92             return HC_ERR_ALLOC_MEMORY;
93         }
94         entity->protocolType = ALG_EC_SPEKE;
95         info->protocolVec.pushBack(&info->protocolVec, (const ProtocolEntity **)&entity);
96     #else
97         (void)info;
98     #endif
99     } else {
100     #ifdef ENABLE_P2P_AUTH_ISO
101         ProtocolEntity *entity = (ProtocolEntity *)HcMalloc(sizeof(ProtocolEntity), 0);
102         if (entity == NULL) {
103             LOGE("Failed to alloc memory for entity!");
104             return HC_ERR_ALLOC_MEMORY;
105         }
106         entity->protocolType = ALG_ISO;
107         info->protocolVec.pushBack(&info->protocolVec, (const ProtocolEntity **)&entity);
108     #else
109         (void)info;
110     #endif
111     }
112 
113     return HC_SUCCESS;
114 }
115 
SetPreSharedUrlForProof(const char * urlStr,Uint8Buff * preSharedUrl)116 static int32_t SetPreSharedUrlForProof(const char *urlStr, Uint8Buff *preSharedUrl)
117 {
118     uint32_t urlLen = HcStrlen(urlStr);
119     preSharedUrl->val = (uint8_t *)HcMalloc(urlLen + 1, 0);
120     if (preSharedUrl->val == NULL) {
121         LOGE("Failed to alloc preSharedUrl memory!");
122         return HC_ERR_ALLOC_MEMORY;
123     }
124     if (memcpy_s(preSharedUrl->val, urlLen + 1, urlStr, urlLen) != EOK) {
125         LOGE("Failed to copy url string to preSharedUrl");
126         HcFree(preSharedUrl->val);
127         preSharedUrl->val = NULL;
128         return HC_ERR_MEMORY_COPY;
129     }
130     preSharedUrl->length = urlLen + 1;
131     return HC_SUCCESS;
132 }
133 
SetProtocolsForPresharedCred(int32_t trustType,int32_t keyType,IdentityInfo * info)134 static int32_t SetProtocolsForPresharedCred(int32_t trustType, int32_t keyType, IdentityInfo *info)
135 {
136     if (trustType == TRUST_TYPE_PIN) {
137         return SetProtocolsForPinType(info);
138     } else if (trustType == TRUST_TYPE_UID) {
139         return SetProtocolsForUidType(info);
140     } else {
141         return SetProtocolsForP2pType(keyType, info);
142     }
143 }
144 
GetSelfDeviceEntry(int32_t osAccountId,const char * groupId,TrustedDeviceEntry * deviceEntry)145 int32_t GetSelfDeviceEntry(int32_t osAccountId, const char *groupId, TrustedDeviceEntry *deviceEntry)
146 {
147     char selfUdid[INPUT_UDID_LEN] = { 0 };
148     int32_t ret = HcGetUdid((uint8_t *)selfUdid, INPUT_UDID_LEN);
149     if (ret != HC_SUCCESS) {
150         LOGE("Failed to get local udid!");
151         return ret;
152     }
153     return GaGetTrustedDeviceEntryById(osAccountId, selfUdid, true, groupId, deviceEntry);
154 }
155 
GetPeerDevIdFromJson(const CJson * in,bool * isUdid)156 const char *GetPeerDevIdFromJson(const CJson *in, bool *isUdid)
157 {
158     const char *deviceId = GetStringFromJson(in, FIELD_PEER_UDID);
159     if (deviceId != NULL) {
160         *isUdid = true;
161         return deviceId;
162     }
163     return GetStringFromJson(in, FIELD_PEER_AUTH_ID);
164 }
165 
GetPeerDeviceEntry(int32_t osAccountId,const CJson * in,const char * groupId,TrustedDeviceEntry * returnDeviceEntry)166 int32_t GetPeerDeviceEntry(int32_t osAccountId, const CJson *in, const char *groupId,
167     TrustedDeviceEntry *returnDeviceEntry)
168 {
169     bool isUdid = false;
170     const char *peerDeviceId = GetPeerDevIdFromJson(in, &isUdid);
171     if (peerDeviceId == NULL) {
172         LOGE("Failed to get peer deviceId!");
173         return HC_ERR_JSON_GET;
174     }
175     return GaGetTrustedDeviceEntryById(osAccountId, peerDeviceId, isUdid, groupId, returnDeviceEntry);
176 }
177 
GetIdentityInfoByType(int32_t keyType,int32_t trustType,const char * groupId,IdentityInfo * info)178 int32_t GetIdentityInfoByType(int32_t keyType, int32_t trustType, const char *groupId, IdentityInfo *info)
179 {
180     CJson *urlJson = CreateJson();
181     if (urlJson == NULL) {
182         LOGE("Failed to create url json!");
183         return HC_ERR_JSON_CREATE;
184     }
185     if (AddIntToJson(urlJson, PRESHARED_URL_CREDENTIAL_TYPE, PRE_SHARED) != HC_SUCCESS) {
186         LOGE("Failed to add credential type!");
187         FreeJson(urlJson);
188         return HC_ERR_JSON_ADD;
189     }
190     if (AddIntToJson(urlJson, PRESHARED_URL_KEY_TYPE, keyType) != HC_SUCCESS) {
191         LOGE("Failed to add key type!");
192         FreeJson(urlJson);
193         return HC_ERR_JSON_ADD;
194     }
195     if (AddIntToJson(urlJson, PRESHARED_URL_TRUST_TYPE, trustType) != HC_SUCCESS) {
196         LOGE("Failed to add trust type!");
197         FreeJson(urlJson);
198         return HC_ERR_JSON_ADD;
199     }
200     if ((trustType == TRUST_TYPE_P2P || trustType == TRUST_TYPE_UID) &&
201         AddStringToJson(urlJson, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
202         LOGE("Failed to add group id!");
203         FreeJson(urlJson);
204         return HC_ERR_JSON_ADD;
205     }
206     char *urlStr = PackJsonToString(urlJson);
207     FreeJson(urlJson);
208     if (urlStr == NULL) {
209         LOGE("Failed to pack url json to string!");
210         return HC_ERR_PACKAGE_JSON_TO_STRING_FAIL;
211     }
212 
213     int32_t ret = SetPreSharedUrlForProof(urlStr, &info->proof.preSharedUrl);
214     FreeJsonString(urlStr);
215     if (ret != HC_SUCCESS) {
216         LOGE("Failed to set preSharedUrl of proof!");
217         return ret;
218     }
219 
220     ret = SetProtocolsForPresharedCred(trustType, keyType, info);
221     if (ret != HC_SUCCESS) {
222         LOGE("Failed to set protocols!");
223         return ret;
224     }
225 
226     info->proofType = PRE_SHARED;
227     return ret;
228 }
229 
FreeBuffData(Uint8Buff * buff)230 void FreeBuffData(Uint8Buff *buff)
231 {
232     if (buff == NULL) {
233         return;
234     }
235     HcFree(buff->val);
236     buff->val = NULL;
237     buff->length = 0;
238 }
239 
CreateIdentityInfo(void)240 IdentityInfo *CreateIdentityInfo(void)
241 {
242     IdentityInfo *info = (IdentityInfo *)HcMalloc(sizeof(IdentityInfo), 0);
243     if (info == NULL) {
244         LOGE("Failed to alloc memory for identity info!");
245         return NULL;
246     }
247     info->protocolVec = CreateProtocolEntityVec();
248     return info;
249 }
250 
DestroyIdentityInfo(IdentityInfo * info)251 void DestroyIdentityInfo(IdentityInfo *info)
252 {
253     if (info == NULL) {
254         return;
255     }
256 
257     FreeBuffData(&info->proof.preSharedUrl);
258     FreeBuffData(&info->proof.certInfo.pkInfoStr);
259     FreeBuffData(&info->proof.certInfo.pkInfoSignature);
260     ClearProtocolEntityVec(&info->protocolVec);
261 
262     HcFree(info);
263 }
264 
ClearIdentityInfoVec(IdentityInfoVec * vec)265 void ClearIdentityInfoVec(IdentityInfoVec *vec)
266 {
267     uint32_t index;
268     IdentityInfo **info;
269     FOR_EACH_HC_VECTOR(*vec, index, info) {
270         DestroyIdentityInfo(*info);
271     }
272     DESTROY_HC_VECTOR(IdentityInfoVec, vec);
273 }
274 
ClearProtocolEntityVec(ProtocolEntityVec * vec)275 void ClearProtocolEntityVec(ProtocolEntityVec *vec)
276 {
277     uint32_t index;
278     ProtocolEntity **entity;
279     FOR_EACH_HC_VECTOR(*vec, index, entity) {
280         HcFree(*entity);
281     }
282     DESTROY_HC_VECTOR(ProtocolEntityVec, vec);
283 }