1 /*
2 * Copyright (C) 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 "data_manager.h"
17
18 #include "broadcast_manager.h"
19 #include "common_defs.h"
20 #include "device_auth.h"
21 #include "device_auth_defines.h"
22 #include "hc_dev_info.h"
23 #include "hc_file.h"
24 #include "hc_log.h"
25 #include "hc_mutex.h"
26 #include "hc_string_vector.h"
27 #include "hc_types.h"
28 #include "key_manager.h"
29 #include "securec.h"
30 #include "hidump_adapter.h"
31 #include "pseudonym_manager.h"
32
33 typedef struct {
34 DECLARE_TLV_STRUCT(9)
35 TlvString name;
36 TlvString id;
37 TlvUint32 type;
38 TlvInt32 visibility;
39 TlvInt32 expireTime;
40 TlvString userId;
41 TlvString sharedUserId;
42 TlvBuffer managers;
43 TlvBuffer friends;
44 } TlvGroupElement;
45 DECLEAR_INIT_FUNC(TlvGroupElement)
46 DECLARE_TLV_VECTOR(TlvGroupVec, TlvGroupElement)
47
48 typedef struct {
49 uint8_t credential;
50 uint8_t devType;
51 uint8_t source;
52 int64_t userId;
53 uint64_t lastTm;
54 } DevAuthFixedLenInfo;
55 DECLARE_TLV_FIX_LENGTH_TYPE(TlvDevAuthFixedLenInfo, DevAuthFixedLenInfo)
56 DECLEAR_INIT_FUNC(TlvDevAuthFixedLenInfo)
57
58 typedef struct {
59 DECLARE_TLV_STRUCT(7)
60 TlvString groupId;
61 TlvString udid;
62 TlvString authId;
63 TlvString userId;
64 TlvString serviceType;
65 TlvBuffer ext;
66 TlvDevAuthFixedLenInfo info;
67 } TlvDeviceElement;
68 DECLEAR_INIT_FUNC(TlvDeviceElement)
69 DECLARE_TLV_VECTOR(TlvDeviceVec, TlvDeviceElement)
70
71 typedef struct {
72 DECLARE_TLV_STRUCT(3)
73 TlvInt32 version;
74 TlvGroupVec groups;
75 TlvDeviceVec devices;
76 } HCDataBaseV1;
77 DECLEAR_INIT_FUNC(HCDataBaseV1)
78
79 DEFINE_TLV_FIX_LENGTH_TYPE(TlvDevAuthFixedLenInfo, NO_REVERT)
80
81 BEGIN_TLV_STRUCT_DEFINE(TlvGroupElement, 0x0001)
82 TLV_MEMBER(TlvString, name, 0x4001)
83 TLV_MEMBER(TlvString, id, 0x4002)
84 TLV_MEMBER(TlvUint32, type, 0x4003)
85 TLV_MEMBER(TlvInt32, visibility, 0x4004)
86 TLV_MEMBER(TlvInt32, expireTime, 0x4005)
87 TLV_MEMBER(TlvString, userId, 0x4006)
88 TLV_MEMBER(TlvString, sharedUserId, 0x4007)
89 TLV_MEMBER(TlvBuffer, managers, 0x4008)
90 TLV_MEMBER(TlvBuffer, friends, 0x4009)
91 END_TLV_STRUCT_DEFINE()
92 IMPLEMENT_TLV_VECTOR(TlvGroupVec, TlvGroupElement, 1)
93
94 BEGIN_TLV_STRUCT_DEFINE(TlvDeviceElement, 0x0002)
95 TLV_MEMBER(TlvString, groupId, 0x4101)
96 TLV_MEMBER(TlvString, udid, 0x4102)
97 TLV_MEMBER(TlvString, authId, 0x4103)
98 TLV_MEMBER(TlvString, userId, 0x4107)
99 TLV_MEMBER(TlvString, serviceType, 0x4104)
100 TLV_MEMBER(TlvBuffer, ext, 0x4105)
101 TLV_MEMBER(TlvDevAuthFixedLenInfo, info, 0x4106)
102 END_TLV_STRUCT_DEFINE()
103 IMPLEMENT_TLV_VECTOR(TlvDeviceVec, TlvDeviceElement, 1)
104
105 BEGIN_TLV_STRUCT_DEFINE(HCDataBaseV1, 0x0001)
106 TLV_MEMBER(TlvInt32, version, 0x6001)
107 TLV_MEMBER(TlvGroupVec, groups, 0x6002)
108 TLV_MEMBER(TlvDeviceVec, devices, 0x6003)
109 END_TLV_STRUCT_DEFINE()
110
111 IMPLEMENT_HC_VECTOR(GroupEntryVec, TrustedGroupEntry*, 1)
112 IMPLEMENT_HC_VECTOR(DeviceEntryVec, TrustedDeviceEntry*, 1)
113
114 typedef struct {
115 int32_t osAccountId;
116 GroupEntryVec groups;
117 DeviceEntryVec devices;
118 } OsAccountTrustedInfo;
119
120 DECLARE_HC_VECTOR(DeviceAuthDb, OsAccountTrustedInfo)
121 IMPLEMENT_HC_VECTOR(DeviceAuthDb, OsAccountTrustedInfo, 1)
122
123 #define MAX_DB_PATH_LEN 256
124
125 static HcMutex *g_databaseMutex = NULL;
126 static DeviceAuthDb g_deviceauthDb;
127
EndWithZero(HcParcel * parcel)128 static bool EndWithZero(HcParcel *parcel)
129 {
130 const char *p = GetParcelLastChar(parcel);
131 if (p == NULL) {
132 return false;
133 }
134 return (*p == '\0');
135 }
136
LoadStringVectorFromParcel(StringVector * vec,HcParcel * parcel)137 static bool LoadStringVectorFromParcel(StringVector *vec, HcParcel *parcel)
138 {
139 uint32_t strLen = 0;
140 do {
141 if (!ParcelReadUint32(parcel, &strLen)) {
142 return true;
143 }
144 if ((strLen == 0) || (strLen > MAX_STRING_LEN)) {
145 return false;
146 }
147 HcString str = CreateString();
148 ClearParcel(&str.parcel);
149 if (!ParcelReadParcel(parcel, &str.parcel, strLen, false) ||
150 !EndWithZero(&str.parcel)) {
151 DeleteString(&str);
152 return false;
153 } else {
154 if (vec->pushBack(vec, &str) == NULL) {
155 DeleteString(&str);
156 return false;
157 }
158 }
159 } while (1);
160 }
161
SaveStringVectorToParcel(const StringVector * vec,HcParcel * parcel)162 static bool SaveStringVectorToParcel(const StringVector *vec, HcParcel *parcel)
163 {
164 uint32_t index;
165 HcString *str = NULL;
166 FOR_EACH_HC_VECTOR(*vec, index, str) {
167 uint32_t len = StringLength(str) + sizeof(char);
168 if (!ParcelWriteUint32(parcel, len)) {
169 return false;
170 }
171 if (!ParcelWrite(parcel, GetParcelData(&str->parcel), GetParcelDataSize(&str->parcel))) {
172 return false;
173 }
174 }
175 return true;
176 }
177
GetTrustedInfoByOsAccountId(int32_t osAccountId)178 static OsAccountTrustedInfo *GetTrustedInfoByOsAccountId(int32_t osAccountId)
179 {
180 uint32_t index = 0;
181 OsAccountTrustedInfo *info = NULL;
182 FOR_EACH_HC_VECTOR(g_deviceauthDb, index, info) {
183 if (info->osAccountId == osAccountId) {
184 return info;
185 }
186 }
187 LOGI("[DB]: Create a new os account database cache! [Id]: %d", osAccountId);
188 OsAccountTrustedInfo newInfo;
189 newInfo.osAccountId = osAccountId;
190 newInfo.groups = CreateGroupEntryVec();
191 newInfo.devices = CreateDeviceEntryVec();
192 OsAccountTrustedInfo *returnInfo = g_deviceauthDb.pushBackT(&g_deviceauthDb, newInfo);
193 if (returnInfo == NULL) {
194 LOGE("[DB]: Failed to push osAccountInfo to database!");
195 DestroyGroupEntryVec(&newInfo.groups);
196 DestroyDeviceEntryVec(&newInfo.devices);
197 }
198 return returnInfo;
199 }
200
GetOsAccountInfoPath(int32_t osAccountId,char * infoPath,uint32_t pathBufferLen)201 static bool GetOsAccountInfoPath(int32_t osAccountId, char *infoPath, uint32_t pathBufferLen)
202 {
203 const char *beginPath = GetStorageDirPath();
204 if (beginPath == NULL) {
205 LOGE("[DB]: Failed to get the storage path dir!");
206 return false;
207 }
208 int32_t writeByteNum;
209 if (osAccountId == DEFAULT_OS_ACCOUNT) {
210 writeByteNum = sprintf_s(infoPath, pathBufferLen, "%s/hcgroup.dat", beginPath);
211 } else {
212 writeByteNum = sprintf_s(infoPath, pathBufferLen, "%s/hcgroup%d.dat", beginPath, osAccountId);
213 }
214 if (writeByteNum <= 0) {
215 LOGE("[DB]: sprintf_s fail!");
216 return false;
217 }
218 return true;
219 }
220
GenerateGroupEntryFromEntry(const TrustedGroupEntry * entry,TrustedGroupEntry * returnEntry)221 bool GenerateGroupEntryFromEntry(const TrustedGroupEntry *entry, TrustedGroupEntry *returnEntry)
222 {
223 if (HC_VECTOR_SIZE(&entry->managers) <= 0) {
224 LOGE("[DB]: The group owner is lost!");
225 return false;
226 }
227 HcString entryOwner = HC_VECTOR_GET(&entry->managers, 0);
228 if (!StringSet(&returnEntry->name, entry->name)) {
229 LOGE("[DB]: Failed to copy groupName!");
230 return false;
231 }
232 if (!StringSet(&returnEntry->id, entry->id)) {
233 LOGE("[DB]: Failed to copy groupId!");
234 return false;
235 }
236 if (!StringSet(&returnEntry->userId, entry->userId)) {
237 LOGE("[DB]: Failed to copy userId!");
238 return false;
239 }
240 if (!StringSet(&returnEntry->sharedUserId, entry->sharedUserId)) {
241 LOGE("[DB]: Failed to copy sharedUserId!");
242 return false;
243 }
244 returnEntry->type = entry->type;
245 returnEntry->visibility = entry->visibility;
246 returnEntry->expireTime = entry->expireTime;
247 HcString ownerName = CreateString();
248 if (!StringSet(&ownerName, entryOwner)) {
249 LOGE("[DB]: Failed to copy groupOwner!");
250 DeleteString(&ownerName);
251 return false;
252 }
253 if (returnEntry->managers.pushBack(&returnEntry->managers, &ownerName) == NULL) {
254 LOGE("[DB]: Failed to push groupOwner to managers!");
255 DeleteString(&ownerName);
256 return false;
257 }
258 return true;
259 }
260
GenerateDeviceEntryFromEntry(const TrustedDeviceEntry * entry,TrustedDeviceEntry * returnEntry)261 bool GenerateDeviceEntryFromEntry(const TrustedDeviceEntry *entry, TrustedDeviceEntry *returnEntry)
262 {
263 returnEntry->groupEntry = NULL;
264 if (!StringSet(&returnEntry->groupId, entry->groupId)) {
265 LOGE("[DB]: Failed to copy udid!");
266 return false;
267 }
268 if (!StringSet(&returnEntry->udid, entry->udid)) {
269 LOGE("[DB]: Failed to copy udid!");
270 return false;
271 }
272 if (!StringSet(&returnEntry->authId, entry->authId)) {
273 LOGE("[DB]: Failed to copy authId!");
274 return false;
275 }
276 if (!StringSet(&returnEntry->userId, entry->userId)) {
277 LOGE("[DB]: Failed to copy userId!");
278 return false;
279 }
280 if (!StringSet(&returnEntry->serviceType, entry->serviceType)) {
281 LOGE("[DB]: Failed to copy serviceType!");
282 return false;
283 }
284 returnEntry->credential = entry->credential;
285 returnEntry->devType = entry->devType;
286 returnEntry->source = entry->source;
287 returnEntry->lastTm = entry->lastTm;
288 return true;
289 }
290
GenerateGroupEntryFromTlv(TlvGroupElement * group,TrustedGroupEntry * entry)291 static bool GenerateGroupEntryFromTlv(TlvGroupElement *group, TrustedGroupEntry *entry)
292 {
293 if (!StringSet(&entry->name, group->name.data)) {
294 LOGE("[DB]: Failed to load groupName from tlv!");
295 return false;
296 }
297 if (!StringSet(&entry->id, group->id.data)) {
298 LOGE("[DB]: Failed to load groupId from tlv!");
299 return false;
300 }
301 if (!StringSet(&entry->userId, group->userId.data)) {
302 LOGE("[DB]: Failed to load userId from tlv!");
303 return false;
304 }
305 if (!StringSet(&entry->sharedUserId, group->sharedUserId.data)) {
306 LOGE("[DB]: Failed to load sharedUserId from tlv!");
307 return false;
308 }
309 if (!LoadStringVectorFromParcel(&entry->managers, &group->managers.data)) {
310 LOGE("[DB]: Failed to load managers from tlv!");
311 return false;
312 }
313 if (!LoadStringVectorFromParcel(&entry->friends, &group->friends.data)) {
314 LOGE("[DB]: Failed to load friends from tlv!");
315 return false;
316 }
317 entry->type = group->type.data;
318 entry->visibility = group->visibility.data;
319 entry->expireTime = group->expireTime.data;
320 return true;
321 }
322
GenerateDeviceEntryFromTlv(TlvDeviceElement * device,TrustedDeviceEntry * deviceEntry)323 static bool GenerateDeviceEntryFromTlv(TlvDeviceElement *device, TrustedDeviceEntry *deviceEntry)
324 {
325 deviceEntry->groupEntry = NULL;
326 if (!StringSet(&deviceEntry->groupId, device->groupId.data)) {
327 LOGE("[DB]: Failed to load groupId from tlv!");
328 return false;
329 }
330 if (!StringSet(&deviceEntry->udid, device->udid.data)) {
331 LOGE("[DB]: Failed to load udid from tlv!");
332 return false;
333 }
334 if (!StringSet(&deviceEntry->authId, device->authId.data)) {
335 LOGE("[DB]: Failed to load authId from tlv!");
336 return false;
337 }
338 if (!StringSet(&deviceEntry->userId, device->userId.data)) {
339 LOGE("[DB]: Failed to load userId from tlv!");
340 return false;
341 }
342 if (!StringSet(&deviceEntry->serviceType, device->serviceType.data)) {
343 LOGE("[DB]: Failed to load serviceType from tlv!");
344 return false;
345 }
346 if (!ParcelCopy(&device->ext.data, &deviceEntry->ext)) {
347 LOGE("[DB]: Failed to load external data from tlv!");
348 return false;
349 }
350 deviceEntry->credential = device->info.data.credential;
351 deviceEntry->devType = device->info.data.devType;
352 deviceEntry->source = device->info.data.source;
353 deviceEntry->lastTm = device->info.data.lastTm;
354 return true;
355 }
356
LoadGroups(HCDataBaseV1 * db,GroupEntryVec * vec)357 static bool LoadGroups(HCDataBaseV1 *db, GroupEntryVec *vec)
358 {
359 uint32_t index;
360 TlvGroupElement *group = NULL;
361 FOR_EACH_HC_VECTOR(db->groups.data, index, group) {
362 TrustedGroupEntry *entry = CreateGroupEntry();
363 if (entry == NULL) {
364 LOGE("[DB]: Failed to allocate entry memory!");
365 ClearGroupEntryVec(vec);
366 return false;
367 }
368 if (!GenerateGroupEntryFromTlv(group, entry)) {
369 DestroyGroupEntry(entry);
370 ClearGroupEntryVec(vec);
371 return false;
372 }
373 if (vec->pushBackT(vec, entry) == NULL) {
374 LOGE("[DB]: Failed to push entry to vec!");
375 DestroyGroupEntry(entry);
376 ClearGroupEntryVec(vec);
377 return false;
378 }
379 }
380 return true;
381 }
382
LoadDevices(HCDataBaseV1 * db,DeviceEntryVec * vec)383 static bool LoadDevices(HCDataBaseV1 *db, DeviceEntryVec *vec)
384 {
385 uint32_t index;
386 TlvDeviceElement *device = NULL;
387 FOR_EACH_HC_VECTOR(db->devices.data, index, device) {
388 TrustedDeviceEntry *entry = CreateDeviceEntry();
389 if (entry == NULL) {
390 LOGE("[DB]: Failed to allocate entry memory!");
391 ClearDeviceEntryVec(vec);
392 return false;
393 }
394 if (!GenerateDeviceEntryFromTlv(device, entry)) {
395 DestroyDeviceEntry(entry);
396 ClearDeviceEntryVec(vec);
397 return false;
398 }
399 if (vec->pushBackT(vec, entry) == NULL) {
400 LOGE("[DB]: Failed to push entry to vec!");
401 DestroyDeviceEntry(entry);
402 ClearDeviceEntryVec(vec);
403 return false;
404 }
405 }
406 return true;
407 }
408
ReadInfoFromParcel(HcParcel * parcel,OsAccountTrustedInfo * info)409 static bool ReadInfoFromParcel(HcParcel *parcel, OsAccountTrustedInfo *info)
410 {
411 bool ret = false;
412 HCDataBaseV1 dbv1;
413 TLV_INIT(HCDataBaseV1, &dbv1)
414 if (DecodeTlvMessage((TlvBase *)&dbv1, parcel, false)) {
415 if (!LoadGroups(&dbv1, &info->groups)) {
416 TLV_DEINIT(dbv1)
417 return false;
418 }
419 if (!LoadDevices(&dbv1, &info->devices)) {
420 ClearGroupEntryVec(&info->groups);
421 TLV_DEINIT(dbv1)
422 return false;
423 }
424 ret = true;
425 } else {
426 LOGE("[DB]: Decode Tlv Message Failed!");
427 }
428 TLV_DEINIT(dbv1)
429 return ret;
430 }
431
ReadParcelFromFile(int32_t osAccountId,HcParcel * parcel)432 static bool ReadParcelFromFile(int32_t osAccountId, HcParcel *parcel)
433 {
434 char infoPath[MAX_DB_PATH_LEN] = { 0 };
435 if (!GetOsAccountInfoPath(osAccountId, infoPath, MAX_DB_PATH_LEN)) {
436 return false;
437 }
438 FileHandle file;
439 int ret = HcFileOpen(infoPath, MODE_FILE_READ, &file);
440 if (ret != 0) {
441 LOGE("[DB]: Failed to open database file!");
442 return false;
443 }
444 int fileSize = HcFileSize(file);
445 if (fileSize <= 0) {
446 LOGE("[DB]: The database file size is invalid!");
447 HcFileClose(file);
448 return false;
449 }
450 char *fileData = (char *)HcMalloc(fileSize, 0);
451 if (fileData == NULL) {
452 LOGE("[DB]: Failed to allocate fileData memory!");
453 HcFileClose(file);
454 return false;
455 }
456 if (HcFileRead(file, fileData, fileSize) != fileSize) {
457 LOGE("[DB]: Read file error!");
458 HcFileClose(file);
459 HcFree(fileData);
460 return false;
461 }
462 HcFileClose(file);
463 if (!ParcelWrite(parcel, fileData, fileSize)) {
464 LOGE("[DB]: parcel write error!");
465 HcFree(fileData);
466 return false;
467 }
468 HcFree(fileData);
469 return true;
470 }
471
LoadOsAccountDb(int32_t osAccountId)472 static void LoadOsAccountDb(int32_t osAccountId)
473 {
474 HcParcel parcel = CreateParcel(0, 0);
475 if (!ReadParcelFromFile(osAccountId, &parcel)) {
476 DeleteParcel(&parcel);
477 return;
478 }
479 OsAccountTrustedInfo info;
480 info.osAccountId = osAccountId;
481 info.groups = CreateGroupEntryVec();
482 info.devices = CreateDeviceEntryVec();
483 if (!ReadInfoFromParcel(&parcel, &info)) {
484 DestroyGroupEntryVec(&info.groups);
485 DestroyDeviceEntryVec(&info.devices);
486 DeleteParcel(&parcel);
487 return;
488 }
489 DeleteParcel(&parcel);
490 if (g_deviceauthDb.pushBackT(&g_deviceauthDb, info) == NULL) {
491 LOGE("[DB]: Failed to push osAccountInfo to database!");
492 ClearGroupEntryVec(&info.groups);
493 ClearDeviceEntryVec(&info.devices);
494 }
495 LOGI("[DB]: Load os account db successfully! [Id]: %d", osAccountId);
496 }
497
LoadDeviceAuthDb(void)498 static void LoadDeviceAuthDb(void)
499 {
500 StringVector osAccountDbNameVec = CreateStrVector();
501 HcFileGetSubFileName(GetStorageDirPath(), &osAccountDbNameVec);
502 uint32_t index;
503 HcString *dbName;
504 FOR_EACH_HC_VECTOR(osAccountDbNameVec, index, dbName) {
505 int32_t osAccountId;
506 const char *osAccountIdStr = StringGet(dbName);
507 if (osAccountIdStr == NULL) {
508 continue;
509 }
510 if (strcmp(osAccountIdStr, "hcgroup.dat") == 0) {
511 LoadOsAccountDb(DEFAULT_OS_ACCOUNT);
512 } else if (sscanf_s(osAccountIdStr, "hcgroup%d.dat", &osAccountId) == 1) {
513 LoadOsAccountDb(osAccountId);
514 }
515 }
516 DestroyStrVector(&osAccountDbNameVec);
517 }
518
SetGroupElement(TlvGroupElement * element,TrustedGroupEntry * entry)519 static bool SetGroupElement(TlvGroupElement *element, TrustedGroupEntry *entry)
520 {
521 if (!StringSet(&element->name.data, entry->name)) {
522 LOGE("[DB]: Failed to copy groupName!");
523 return false;
524 }
525 if (!StringSet(&element->id.data, entry->id)) {
526 LOGE("[DB]: Failed to copy groupId!");
527 return false;
528 }
529 if (!StringSet(&element->userId.data, entry->userId)) {
530 LOGE("[DB]: Failed to copy userId!");
531 return false;
532 }
533 if (!StringSet(&element->sharedUserId.data, entry->sharedUserId)) {
534 LOGE("[DB]: Failed to copy sharedUserId!");
535 return false;
536 }
537 element->type.data = entry->type;
538 element->visibility.data = entry->visibility;
539 element->expireTime.data = entry->expireTime;
540 if (!SaveStringVectorToParcel(&entry->managers, &element->managers.data)) {
541 LOGE("[DB]: Failed to copy managers!");
542 return false;
543 }
544 if (!SaveStringVectorToParcel(&entry->friends, &element->friends.data)) {
545 LOGE("[DB]: Failed to copy friends!");
546 return false;
547 }
548 return true;
549 }
550
SetDeviceElement(TlvDeviceElement * element,TrustedDeviceEntry * entry)551 static bool SetDeviceElement(TlvDeviceElement *element, TrustedDeviceEntry *entry)
552 {
553 if (!StringSet(&element->groupId.data, entry->groupId)) {
554 LOGE("[DB]: Failed to copy groupId!");
555 return false;
556 }
557 if (!StringSet(&element->udid.data, entry->udid)) {
558 LOGE("[DB]: Failed to copy udid!");
559 return false;
560 }
561 if (!StringSet(&element->authId.data, entry->authId)) {
562 LOGE("[DB]: Failed to copy authId!");
563 return false;
564 }
565 if (!StringSet(&element->userId.data, entry->userId)) {
566 LOGE("[DB]: Failed to copy userId!");
567 return false;
568 }
569 if (!StringSet(&element->serviceType.data, entry->serviceType)) {
570 LOGE("[DB]: Failed to copy serviceType!");
571 return false;
572 }
573 if (!ParcelCopy(&element->ext.data, &entry->ext)) {
574 LOGE("[DB]: Failed to copy external data!");
575 return false;
576 }
577 element->info.data.credential = entry->credential;
578 element->info.data.devType = entry->devType;
579 element->info.data.source = entry->source;
580 element->info.data.lastTm = entry->lastTm;
581 return true;
582 }
583
SaveGroups(const GroupEntryVec * vec,HCDataBaseV1 * db)584 static bool SaveGroups(const GroupEntryVec *vec, HCDataBaseV1 *db)
585 {
586 uint32_t index;
587 TrustedGroupEntry **entry;
588 FOR_EACH_HC_VECTOR(*vec, index, entry) {
589 TlvGroupElement tmp;
590 TlvGroupElement *element = db->groups.data.pushBack(&db->groups.data, &tmp);
591 if (element == NULL) {
592 return false;
593 }
594 TLV_INIT(TlvGroupElement, element);
595 if (!SetGroupElement(element, *entry)) {
596 TLV_DEINIT((*element));
597 return false;
598 }
599 }
600 return true;
601 }
602
SaveDevices(const DeviceEntryVec * vec,HCDataBaseV1 * db)603 static bool SaveDevices(const DeviceEntryVec *vec, HCDataBaseV1 *db)
604 {
605 uint32_t index;
606 TrustedDeviceEntry **entry;
607 FOR_EACH_HC_VECTOR(*vec, index, entry) {
608 TlvDeviceElement tmp;
609 TlvDeviceElement *element = db->devices.data.pushBack(&db->devices.data, &tmp);
610 if (element == NULL) {
611 return false;
612 }
613 TLV_INIT(TlvDeviceElement, element);
614 if (!SetDeviceElement(element, *entry)) {
615 TLV_DEINIT((*element));
616 return false;
617 }
618 }
619 return true;
620 }
621
SaveInfoToParcel(const OsAccountTrustedInfo * info,HcParcel * parcel)622 static bool SaveInfoToParcel(const OsAccountTrustedInfo *info, HcParcel *parcel)
623 {
624 int32_t ret = false;
625 HCDataBaseV1 dbv1;
626 TLV_INIT(HCDataBaseV1, &dbv1)
627 dbv1.version.data = 1;
628 do {
629 if (!SaveGroups(&info->groups, &dbv1)) {
630 break;
631 }
632 if (!SaveDevices(&info->devices, &dbv1)) {
633 break;
634 }
635 if (!EncodeTlvMessage((TlvBase *)&dbv1, parcel)) {
636 LOGE("[DB]: Encode Tlv Message failed!");
637 break;
638 }
639 ret = true;
640 } while (0);
641 TLV_DEINIT(dbv1)
642 return ret;
643 }
644
SaveParcelToFile(const OsAccountTrustedInfo * info,HcParcel * parcel)645 static bool SaveParcelToFile(const OsAccountTrustedInfo *info, HcParcel *parcel)
646 {
647 char infoPath[MAX_DB_PATH_LEN] = { 0 };
648 if (!GetOsAccountInfoPath(info->osAccountId, infoPath, MAX_DB_PATH_LEN)) {
649 return false;
650 }
651 FileHandle file;
652 int ret = HcFileOpen(infoPath, MODE_FILE_WRITE, &file);
653 if (ret != HC_SUCCESS) {
654 LOGE("[DB]: Failed to open database file!");
655 return false;
656 }
657 int fileSize = (int)GetParcelDataSize(parcel);
658 const char *fileData = GetParcelData(parcel);
659 int writeSize = HcFileWrite(file, fileData, fileSize);
660 HcFileClose(file);
661 if (writeSize == fileSize) {
662 return true;
663 } else {
664 LOGE("[DB]: write file error!");
665 return false;
666 }
667 }
668
CompareQueryGroupParams(const QueryGroupParams * params,const TrustedGroupEntry * entry)669 static bool CompareQueryGroupParams(const QueryGroupParams *params, const TrustedGroupEntry *entry)
670 {
671 if ((params->groupId != NULL) && (strcmp(params->groupId, StringGet(&entry->id)) != 0)) {
672 return false;
673 }
674 if ((params->groupName != NULL) && (strcmp(params->groupName, StringGet(&entry->name)) != 0)) {
675 return false;
676 }
677 if ((params->userId != NULL) && (strcmp(params->userId, StringGet(&entry->userId)) != 0)) {
678 return false;
679 }
680 if ((params->sharedUserId != NULL) && (strcmp(params->sharedUserId, StringGet(&entry->sharedUserId)) != 0)) {
681 return false;
682 }
683 if ((params->groupType != ALL_GROUP) && (params->groupType != entry->type)) {
684 return false;
685 }
686 if ((params->groupVisibility != ALL_GROUP_VISIBILITY) && (params->groupVisibility != entry->visibility)) {
687 return false;
688 }
689 if (params->ownerName != NULL) {
690 HcString entryOwner = HC_VECTOR_GET(&entry->managers, 0);
691 if (strcmp(params->ownerName, StringGet(&entryOwner)) != 0) {
692 return false;
693 }
694 }
695 return true;
696 }
697
CompareQueryDeviceParams(const QueryDeviceParams * params,const TrustedDeviceEntry * entry)698 static bool CompareQueryDeviceParams(const QueryDeviceParams *params, const TrustedDeviceEntry *entry)
699 {
700 if ((params->groupId != NULL) && (strcmp(params->groupId, StringGet(&entry->groupId)) != 0)) {
701 return false;
702 }
703 if ((params->udid != NULL) && (strcmp(params->udid, StringGet(&entry->udid)) != 0)) {
704 return false;
705 }
706 if ((params->authId != NULL) && (strcmp(params->authId, StringGet(&entry->authId)) != 0)) {
707 return false;
708 }
709 if ((params->userId != NULL) && (strcmp(params->userId, StringGet(&entry->userId)) != 0)) {
710 return false;
711 }
712 return true;
713 }
714
QueryGroupEntryPtrIfMatch(const GroupEntryVec * vec,const QueryGroupParams * params)715 static TrustedGroupEntry **QueryGroupEntryPtrIfMatch(const GroupEntryVec *vec, const QueryGroupParams *params)
716 {
717 uint32_t index;
718 TrustedGroupEntry **entry;
719 FOR_EACH_HC_VECTOR(*vec, index, entry) {
720 if (CompareQueryGroupParams(params, *entry)) {
721 return entry;
722 }
723 }
724 return NULL;
725 }
726
QueryDeviceEntryPtrIfMatch(const DeviceEntryVec * vec,const QueryDeviceParams * params)727 static TrustedDeviceEntry **QueryDeviceEntryPtrIfMatch(const DeviceEntryVec *vec, const QueryDeviceParams *params)
728 {
729 uint32_t index;
730 TrustedDeviceEntry **entry;
731 FOR_EACH_HC_VECTOR(*vec, index, entry) {
732 if (CompareQueryDeviceParams(params, *entry)) {
733 return entry;
734 }
735 }
736 return NULL;
737 }
738
PostGroupCreatedMsg(const TrustedGroupEntry * groupEntry)739 static void PostGroupCreatedMsg(const TrustedGroupEntry *groupEntry)
740 {
741 if (!IsBroadcastSupported()) {
742 return;
743 }
744 GetBroadcaster()->postOnGroupCreated(groupEntry);
745 }
746
PostGroupDeletedMsg(const TrustedGroupEntry * groupEntry)747 static void PostGroupDeletedMsg(const TrustedGroupEntry *groupEntry)
748 {
749 if (!IsBroadcastSupported()) {
750 return;
751 }
752 GetBroadcaster()->postOnGroupDeleted(groupEntry);
753 }
754
PostDeviceBoundMsg(OsAccountTrustedInfo * info,const TrustedDeviceEntry * deviceEntry)755 static void PostDeviceBoundMsg(OsAccountTrustedInfo *info, const TrustedDeviceEntry *deviceEntry)
756 {
757 if (!IsBroadcastSupported()) {
758 return;
759 }
760 QueryGroupParams groupParams = InitQueryGroupParams();
761 groupParams.groupId = StringGet(&deviceEntry->groupId);
762 TrustedGroupEntry **groupEntryPtr = QueryGroupEntryPtrIfMatch(&info->groups, &groupParams);
763 if (groupEntryPtr != NULL) {
764 GetBroadcaster()->postOnDeviceBound(StringGet(&deviceEntry->udid), *groupEntryPtr);
765 }
766 }
767
IsSelfDeviceEntry(const TrustedDeviceEntry * deviceEntry)768 static bool IsSelfDeviceEntry(const TrustedDeviceEntry *deviceEntry)
769 {
770 char selfUdid[INPUT_UDID_LEN] = { 0 };
771 int32_t res = HcGetUdid((uint8_t *)selfUdid, INPUT_UDID_LEN);
772 if (res != HC_SUCCESS) {
773 LOGE("Failed to get local udid! res: %d", res);
774 return false;
775 }
776 const char *entryUdid = StringGet(&deviceEntry->udid);
777 return strcmp(selfUdid, entryUdid) == 0;
778 }
779
PostDeviceUnBoundMsg(OsAccountTrustedInfo * info,const TrustedDeviceEntry * deviceEntry)780 static void PostDeviceUnBoundMsg(OsAccountTrustedInfo *info, const TrustedDeviceEntry *deviceEntry)
781 {
782 if (!IsBroadcastSupported()) {
783 return;
784 }
785 const char *groupId = StringGet(&deviceEntry->groupId);
786 const char *udid = StringGet(&deviceEntry->udid);
787 QueryGroupParams groupParams = InitQueryGroupParams();
788 groupParams.groupId = groupId;
789 TrustedGroupEntry **groupEntryPtr = QueryGroupEntryPtrIfMatch(&info->groups, &groupParams);
790 if (groupEntryPtr != NULL) {
791 GetBroadcaster()->postOnDeviceUnBound(udid, *groupEntryPtr);
792 }
793 QueryDeviceParams deviceParams = InitQueryDeviceParams();
794 deviceParams.udid = udid;
795 if (QueryDeviceEntryPtrIfMatch(&info->devices, &deviceParams) == NULL) {
796 GetBroadcaster()->postOnDeviceNotTrusted(udid);
797 if (!IsSelfDeviceEntry(deviceEntry)) {
798 (void)DeleteMk(udid);
799 (void)DeletePseudonymPsk(udid);
800 }
801 }
802 }
803
DeletePdidByDeviceEntry(int32_t osAccountId,const TrustedDeviceEntry * deviceEntry)804 static void DeletePdidByDeviceEntry(int32_t osAccountId, const TrustedDeviceEntry *deviceEntry)
805 {
806 if (IsSelfDeviceEntry(deviceEntry)) {
807 return;
808 }
809 const char *userId = StringGet(&deviceEntry->userId);
810 if (userId == NULL) {
811 LOGW("userId is null!");
812 return;
813 }
814 if (deviceEntry->credential != ASYMMETRIC_CRED) {
815 LOGW("credential type is not asymmetric!");
816 return;
817 }
818 PseudonymManager *manager = GetPseudonymInstance();
819 if (manager == NULL) {
820 LOGE("Pseudonym manager is null!");
821 return;
822 }
823 int32_t res = manager->deletePseudonymId(osAccountId, userId);
824 if (res != HC_SUCCESS) {
825 LOGE("Failed to delete pdid!");
826 } else {
827 LOGI("Delete pdid successfully!");
828 }
829 }
830
InitQueryGroupParams(void)831 QueryGroupParams InitQueryGroupParams(void)
832 {
833 QueryGroupParams params = {
834 .groupId = NULL,
835 .groupName = NULL,
836 .ownerName = NULL,
837 .userId = NULL,
838 .groupType = ALL_GROUP,
839 .groupVisibility = ALL_GROUP_VISIBILITY
840 };
841 return params;
842 }
843
InitQueryDeviceParams(void)844 QueryDeviceParams InitQueryDeviceParams(void)
845 {
846 QueryDeviceParams params = {
847 .groupId = NULL,
848 .udid = NULL,
849 .authId = NULL,
850 .userId = NULL
851 };
852 return params;
853 }
854
CreateGroupEntry(void)855 TrustedGroupEntry *CreateGroupEntry(void)
856 {
857 TrustedGroupEntry *ptr = (TrustedGroupEntry *)HcMalloc(sizeof(TrustedGroupEntry), 0);
858 if (ptr == NULL) {
859 LOGE("[DB]: Failed to allocate groupEntry memory!");
860 return NULL;
861 }
862 ptr->name = CreateString();
863 ptr->id = CreateString();
864 ptr->userId = CreateString();
865 ptr->sharedUserId = CreateString();
866 ptr->managers = CreateStrVector();
867 ptr->friends = CreateStrVector();
868 return ptr;
869 }
870
DestroyGroupEntry(TrustedGroupEntry * groupEntry)871 void DestroyGroupEntry(TrustedGroupEntry *groupEntry)
872 {
873 DeleteString(&groupEntry->name);
874 DeleteString(&groupEntry->id);
875 DeleteString(&groupEntry->userId);
876 DeleteString(&groupEntry->sharedUserId);
877 DestroyStrVector(&groupEntry->managers);
878 DestroyStrVector(&groupEntry->friends);
879 HcFree(groupEntry);
880 }
881
DeepCopyGroupEntry(const TrustedGroupEntry * entry)882 TrustedGroupEntry *DeepCopyGroupEntry(const TrustedGroupEntry *entry)
883 {
884 TrustedGroupEntry *returnEntry = CreateGroupEntry();
885 if (returnEntry == NULL) {
886 return NULL;
887 }
888 if (!GenerateGroupEntryFromEntry(entry, returnEntry)) {
889 DestroyGroupEntry(returnEntry);
890 return NULL;
891 }
892 return returnEntry;
893 }
894
CreateDeviceEntry(void)895 TrustedDeviceEntry *CreateDeviceEntry(void)
896 {
897 TrustedDeviceEntry *ptr = (TrustedDeviceEntry *)HcMalloc(sizeof(TrustedDeviceEntry), 0);
898 if (ptr == NULL) {
899 LOGE("[DB]: Failed to allocate deviceEntry memory!");
900 return NULL;
901 }
902 ptr->groupId = CreateString();
903 ptr->udid = CreateString();
904 ptr->authId = CreateString();
905 ptr->userId = CreateString();
906 ptr->serviceType = CreateString();
907 ptr->ext = CreateParcel(0, 0);
908 return ptr;
909 }
910
DestroyDeviceEntry(TrustedDeviceEntry * deviceEntry)911 void DestroyDeviceEntry(TrustedDeviceEntry *deviceEntry)
912 {
913 DeleteString(&deviceEntry->groupId);
914 DeleteString(&deviceEntry->udid);
915 DeleteString(&deviceEntry->authId);
916 DeleteString(&deviceEntry->userId);
917 DeleteString(&deviceEntry->serviceType);
918 DeleteParcel(&deviceEntry->ext);
919 HcFree(deviceEntry);
920 }
921
DeepCopyDeviceEntry(const TrustedDeviceEntry * entry)922 TrustedDeviceEntry *DeepCopyDeviceEntry(const TrustedDeviceEntry *entry)
923 {
924 if (entry == NULL) {
925 return NULL;
926 }
927 TrustedDeviceEntry *returnEntry = CreateDeviceEntry();
928 if (returnEntry == NULL) {
929 return NULL;
930 }
931 if (!GenerateDeviceEntryFromEntry(entry, returnEntry)) {
932 DestroyDeviceEntry(returnEntry);
933 return NULL;
934 }
935 return returnEntry;
936 }
937
ClearGroupEntryVec(GroupEntryVec * vec)938 void ClearGroupEntryVec(GroupEntryVec *vec)
939 {
940 uint32_t index;
941 TrustedGroupEntry **entry;
942 FOR_EACH_HC_VECTOR(*vec, index, entry) {
943 DestroyGroupEntry(*entry);
944 }
945 DESTROY_HC_VECTOR(GroupEntryVec, vec);
946 }
947
ClearDeviceEntryVec(DeviceEntryVec * vec)948 void ClearDeviceEntryVec(DeviceEntryVec *vec)
949 {
950 uint32_t index;
951 TrustedDeviceEntry **entry;
952 FOR_EACH_HC_VECTOR(*vec, index, entry) {
953 DestroyDeviceEntry(*entry);
954 }
955 DESTROY_HC_VECTOR(DeviceEntryVec, vec);
956 }
957
AddGroup(int32_t osAccountId,const TrustedGroupEntry * groupEntry)958 int32_t AddGroup(int32_t osAccountId, const TrustedGroupEntry *groupEntry)
959 {
960 LOGI("[DB]: Start to add a group to database!");
961 if (groupEntry == NULL) {
962 LOGE("[DB]: The input groupEntry is NULL!");
963 return HC_ERR_NULL_PTR;
964 }
965 g_databaseMutex->lock(g_databaseMutex);
966 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
967 if (info == NULL) {
968 g_databaseMutex->unlock(g_databaseMutex);
969 return HC_ERR_INVALID_PARAMS;
970 }
971 TrustedGroupEntry *newEntry = DeepCopyGroupEntry(groupEntry);
972 if (newEntry == NULL) {
973 g_databaseMutex->unlock(g_databaseMutex);
974 return HC_ERR_MEMORY_COPY;
975 }
976 QueryGroupParams params = InitQueryGroupParams();
977 params.groupId = StringGet(&groupEntry->id);
978 TrustedGroupEntry **oldEntryPtr = QueryGroupEntryPtrIfMatch(&info->groups, ¶ms);
979 if (oldEntryPtr != NULL) {
980 DestroyGroupEntry(*oldEntryPtr);
981 *oldEntryPtr = newEntry;
982 PostGroupCreatedMsg(newEntry);
983 g_databaseMutex->unlock(g_databaseMutex);
984 LOGI("[DB]: Replace an old group successfully! [GroupType]: %d", groupEntry->type);
985 return HC_SUCCESS;
986 }
987 if (info->groups.pushBackT(&info->groups, newEntry) == NULL) {
988 DestroyGroupEntry(newEntry);
989 g_databaseMutex->unlock(g_databaseMutex);
990 LOGE("[DB]: Failed to push groupEntry to vec!");
991 return HC_ERR_MEMORY_COPY;
992 }
993 PostGroupCreatedMsg(newEntry);
994 g_databaseMutex->unlock(g_databaseMutex);
995 LOGI("[DB]: Add a group to database successfully! [GroupType]: %d", groupEntry->type);
996 return HC_SUCCESS;
997 }
998
AddTrustedDevice(int32_t osAccountId,const TrustedDeviceEntry * deviceEntry)999 int32_t AddTrustedDevice(int32_t osAccountId, const TrustedDeviceEntry *deviceEntry)
1000 {
1001 LOGI("[DB]: Start to add a trusted device to database!");
1002 if (deviceEntry == NULL) {
1003 LOGE("[DB]: The input deviceEntry is NULL!");
1004 return HC_ERR_NULL_PTR;
1005 }
1006 g_databaseMutex->lock(g_databaseMutex);
1007 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
1008 if (info == NULL) {
1009 g_databaseMutex->unlock(g_databaseMutex);
1010 return HC_ERR_INVALID_PARAMS;
1011 }
1012 TrustedDeviceEntry *newEntry = DeepCopyDeviceEntry(deviceEntry);
1013 if (newEntry == NULL) {
1014 g_databaseMutex->unlock(g_databaseMutex);
1015 return HC_ERR_MEMORY_COPY;
1016 }
1017 QueryDeviceParams params = InitQueryDeviceParams();
1018 params.udid = StringGet(&deviceEntry->udid);
1019 params.groupId = StringGet(&deviceEntry->groupId);
1020 TrustedDeviceEntry **oldEntryPtr = QueryDeviceEntryPtrIfMatch(&info->devices, ¶ms);
1021 if (oldEntryPtr != NULL) {
1022 DestroyDeviceEntry(*oldEntryPtr);
1023 *oldEntryPtr = newEntry;
1024 PostDeviceBoundMsg(info, newEntry);
1025 g_databaseMutex->unlock(g_databaseMutex);
1026 LOGI("[DB]: Replace an old trusted device successfully!");
1027 return HC_SUCCESS;
1028 }
1029 if (info->devices.pushBackT(&info->devices, newEntry) == NULL) {
1030 DestroyDeviceEntry(newEntry);
1031 g_databaseMutex->unlock(g_databaseMutex);
1032 LOGE("[DB]: Failed to push deviceEntry to vec!");
1033 return HC_ERR_MEMORY_COPY;
1034 }
1035 PostDeviceBoundMsg(info, newEntry);
1036 g_databaseMutex->unlock(g_databaseMutex);
1037 LOGI("[DB]: Add a trusted device to database successfully!");
1038 return HC_SUCCESS;
1039 }
1040
DelGroup(int32_t osAccountId,const QueryGroupParams * params)1041 int32_t DelGroup(int32_t osAccountId, const QueryGroupParams *params)
1042 {
1043 LOGI("[DB]: Start to delete groups from database!");
1044 if (params == NULL) {
1045 LOGE("[DB]: The input params is NULL!");
1046 return HC_ERR_NULL_PTR;
1047 }
1048 g_databaseMutex->lock(g_databaseMutex);
1049 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
1050 if (info == NULL) {
1051 g_databaseMutex->unlock(g_databaseMutex);
1052 return HC_ERR_INVALID_PARAMS;
1053 }
1054 int32_t count = 0;
1055 uint32_t index = 0;
1056 TrustedGroupEntry **entry = NULL;
1057 while (index < HC_VECTOR_SIZE(&info->groups)) {
1058 entry = info->groups.getp(&info->groups, index);
1059 if ((entry == NULL) || (*entry == NULL) || (!CompareQueryGroupParams(params, *entry))) {
1060 index++;
1061 continue;
1062 }
1063 TrustedGroupEntry *popEntry;
1064 HC_VECTOR_POPELEMENT(&info->groups, &popEntry, index);
1065 PostGroupDeletedMsg(popEntry);
1066 LOGI("[DB]: Delete a group from database successfully! [GroupType]: %d", popEntry->type);
1067 DestroyGroupEntry(popEntry);
1068 count++;
1069 }
1070 g_databaseMutex->unlock(g_databaseMutex);
1071 LOGI("[DB]: Number of groups deleted: %d", count);
1072 return HC_SUCCESS;
1073 }
1074
DelTrustedDevice(int32_t osAccountId,const QueryDeviceParams * params)1075 int32_t DelTrustedDevice(int32_t osAccountId, const QueryDeviceParams *params)
1076 {
1077 LOGI("[DB]: Start to delete devices from database!");
1078 if (params == NULL) {
1079 LOGE("[DB]: The input params is NULL!");
1080 return HC_ERR_NULL_PTR;
1081 }
1082 g_databaseMutex->lock(g_databaseMutex);
1083 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
1084 if (info == NULL) {
1085 g_databaseMutex->unlock(g_databaseMutex);
1086 return HC_ERR_INVALID_PARAMS;
1087 }
1088 int32_t count = 0;
1089 uint32_t index = 0;
1090 TrustedDeviceEntry **entry = NULL;
1091 while (index < HC_VECTOR_SIZE(&info->devices)) {
1092 entry = info->devices.getp(&info->devices, index);
1093 if ((entry == NULL) || (*entry == NULL) || (!CompareQueryDeviceParams(params, *entry))) {
1094 index++;
1095 continue;
1096 }
1097 TrustedDeviceEntry *popEntry;
1098 HC_VECTOR_POPELEMENT(&info->devices, &popEntry, index);
1099 PostDeviceUnBoundMsg(info, popEntry);
1100 DeletePdidByDeviceEntry(osAccountId, popEntry);
1101 LOGI("[DB]: Delete a trusted device from database successfully!");
1102 DestroyDeviceEntry(popEntry);
1103 count++;
1104 }
1105 g_databaseMutex->unlock(g_databaseMutex);
1106 LOGI("[DB]: Number of trusted devices deleted: %d", count);
1107 return HC_SUCCESS;
1108 }
1109
QueryGroups(int32_t osAccountId,const QueryGroupParams * params,GroupEntryVec * vec)1110 int32_t QueryGroups(int32_t osAccountId, const QueryGroupParams *params, GroupEntryVec *vec)
1111 {
1112 if ((params == NULL) || (vec == NULL)) {
1113 LOGE("[DB]: The input params or vec is NULL!");
1114 return HC_ERR_NULL_PTR;
1115 }
1116 g_databaseMutex->lock(g_databaseMutex);
1117 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
1118 if (info == NULL) {
1119 g_databaseMutex->unlock(g_databaseMutex);
1120 return HC_ERR_INVALID_PARAMS;
1121 }
1122 uint32_t index;
1123 TrustedGroupEntry **entry;
1124 FOR_EACH_HC_VECTOR(info->groups, index, entry) {
1125 if (!CompareQueryGroupParams(params, *entry)) {
1126 continue;
1127 }
1128 TrustedGroupEntry *newEntry = DeepCopyGroupEntry(*entry);
1129 if (newEntry == NULL) {
1130 continue;
1131 }
1132 if (vec->pushBackT(vec, newEntry) == NULL) {
1133 LOGE("[DB]: Failed to push entry to vec!");
1134 DestroyGroupEntry(newEntry);
1135 }
1136 }
1137 g_databaseMutex->unlock(g_databaseMutex);
1138 return HC_SUCCESS;
1139 }
1140
QueryDevices(int32_t osAccountId,const QueryDeviceParams * params,DeviceEntryVec * vec)1141 int32_t QueryDevices(int32_t osAccountId, const QueryDeviceParams *params, DeviceEntryVec *vec)
1142 {
1143 if ((params == NULL) || (vec == NULL)) {
1144 LOGE("[DB]: The input params or vec is NULL!");
1145 return HC_ERR_NULL_PTR;
1146 }
1147 g_databaseMutex->lock(g_databaseMutex);
1148 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
1149 if (info == NULL) {
1150 g_databaseMutex->unlock(g_databaseMutex);
1151 return HC_ERR_INVALID_PARAMS;
1152 }
1153 uint32_t index;
1154 TrustedDeviceEntry **entry;
1155 FOR_EACH_HC_VECTOR(info->devices, index, entry) {
1156 if (!CompareQueryDeviceParams(params, *entry)) {
1157 continue;
1158 }
1159 TrustedDeviceEntry *newEntry = DeepCopyDeviceEntry(*entry);
1160 if (newEntry == NULL) {
1161 continue;
1162 }
1163 if (vec->pushBackT(vec, newEntry) == NULL) {
1164 LOGE("[DB]: Failed to push entry to vec!");
1165 DestroyDeviceEntry(newEntry);
1166 }
1167 }
1168 g_databaseMutex->unlock(g_databaseMutex);
1169 return HC_SUCCESS;
1170 }
1171
SaveOsAccountDb(int32_t osAccountId)1172 int32_t SaveOsAccountDb(int32_t osAccountId)
1173 {
1174 g_databaseMutex->lock(g_databaseMutex);
1175 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
1176 if (info == NULL) {
1177 g_databaseMutex->unlock(g_databaseMutex);
1178 return HC_ERR_INVALID_PARAMS;
1179 }
1180 HcParcel parcel = CreateParcel(0, 0);
1181 if (!SaveInfoToParcel(info, &parcel)) {
1182 DeleteParcel(&parcel);
1183 g_databaseMutex->unlock(g_databaseMutex);
1184 return HC_ERR_MEMORY_COPY;
1185 }
1186 if (!SaveParcelToFile(info, &parcel)) {
1187 DeleteParcel(&parcel);
1188 g_databaseMutex->unlock(g_databaseMutex);
1189 return HC_ERR_MEMORY_COPY;
1190 }
1191 DeleteParcel(&parcel);
1192 g_databaseMutex->unlock(g_databaseMutex);
1193 LOGI("[DB]: Save an os account database successfully! [Id]: %d", osAccountId);
1194 return HC_SUCCESS;
1195 }
1196
1197 #ifdef DEV_AUTH_HIVIEW_ENABLE
DumpGroup(int fd,const TrustedGroupEntry * group)1198 static void DumpGroup(int fd, const TrustedGroupEntry *group)
1199 {
1200 dprintf(fd, "||----------------------------Group----------------------------| |\n");
1201 dprintf(fd, "||%-12s = %-46.8s| |\n", "name", StringGet(&group->name));
1202 dprintf(fd, "||%-12s = %-46.8s| |\n", "id", StringGet(&group->id));
1203 dprintf(fd, "||%-12s = %-46d| |\n", "type", group->type);
1204 dprintf(fd, "||%-12s = %-46d| |\n", "visibility", group->visibility);
1205 dprintf(fd, "||%-12s = %-46d| |\n", "expireTime", group->expireTime);
1206 HcString entryOwner = HC_VECTOR_GET(&group->managers, 0);
1207 dprintf(fd, "||%-12s = %-46.8s| |\n", "ownerName", StringGet(&entryOwner));
1208 dprintf(fd, "||%-12s = %-46.8s| |\n", "userId", StringGet(&group->userId));
1209 dprintf(fd, "||%-12s = %-46.8s| |\n", "sharedUserId", StringGet(&group->sharedUserId));
1210 dprintf(fd, "||----------------------------Group----------------------------| |\n");
1211 }
1212
DumpDevice(int fd,const TrustedDeviceEntry * device)1213 static void DumpDevice(int fd, const TrustedDeviceEntry *device)
1214 {
1215 dprintf(fd, "|||--------------------DEV--------------------| |\n");
1216 dprintf(fd, "|||%-12s = %-28.8s| |\n", "groupId", StringGet(&device->groupId));
1217 dprintf(fd, "|||%-12s = %-28.8s| |\n", "udid", StringGet(&device->udid));
1218 dprintf(fd, "|||%-12s = %-28.8s| |\n", "authId", StringGet(&device->authId));
1219 dprintf(fd, "|||%-12s = %-28.8s| |\n", "userId", StringGet(&device->userId));
1220 dprintf(fd, "|||%-12s = %-28.8s| |\n", "serviceType",
1221 StringGet(&device->serviceType));
1222 dprintf(fd, "|||%-12s = %-28d| |\n", "credential", device->credential);
1223 dprintf(fd, "|||%-12s = %-28d| |\n", "devType", device->devType);
1224 dprintf(fd, "|||%-12s = %-28d| |\n", "credSource", device->source);
1225 dprintf(fd, "|||--------------------DEV--------------------| |\n");
1226 }
1227
DumpDb(int fd,const OsAccountTrustedInfo * db)1228 static void DumpDb(int fd, const OsAccountTrustedInfo *db)
1229 {
1230 const GroupEntryVec *groups = &db->groups;
1231 const DeviceEntryVec *devices = &db->devices;
1232 dprintf(fd, "|-------------------------------------DataBase-------------------------------------|\n");
1233 dprintf(fd, "|%-12s = %-67d|\n", "osAccountId", db->osAccountId);
1234 dprintf(fd, "|%-12s = %-67d|\n", "groupNum", groups->size(groups));
1235 dprintf(fd, "|%-12s = %-67d|\n", "deviceNum", devices->size(devices));
1236 uint32_t index;
1237 TrustedGroupEntry **groupEntry;
1238 FOR_EACH_HC_VECTOR(*groups, index, groupEntry) {
1239 DumpGroup(fd, *groupEntry);
1240 }
1241 TrustedDeviceEntry **deviceEntry;
1242 FOR_EACH_HC_VECTOR(*devices, index, deviceEntry) {
1243 DumpDevice(fd, *deviceEntry);
1244 }
1245 dprintf(fd, "|-------------------------------------DataBase-------------------------------------|\n");
1246 }
1247
DevAuthDataBaseDump(int fd)1248 static void DevAuthDataBaseDump(int fd)
1249 {
1250 if (g_databaseMutex == NULL) {
1251 LOGE("[DB]: Init mutex failed");
1252 return;
1253 }
1254 g_databaseMutex->lock(g_databaseMutex);
1255 uint32_t index;
1256 OsAccountTrustedInfo *info;
1257 FOR_EACH_HC_VECTOR(g_deviceauthDb, index, info) {
1258 DumpDb(fd, info);
1259 }
1260 g_databaseMutex->unlock(g_databaseMutex);
1261 }
1262 #endif
1263
InitDatabase(void)1264 int32_t InitDatabase(void)
1265 {
1266 if (g_databaseMutex == NULL) {
1267 g_databaseMutex = (HcMutex *)HcMalloc(sizeof(HcMutex), 0);
1268 if (g_databaseMutex == NULL) {
1269 LOGE("[DB]: Alloc databaseMutex failed");
1270 return HC_ERR_ALLOC_MEMORY;
1271 }
1272 if (InitHcMutex(g_databaseMutex) != HC_SUCCESS) {
1273 LOGE("[DB]: Init mutex failed");
1274 HcFree(g_databaseMutex);
1275 g_databaseMutex = NULL;
1276 return HC_ERROR;
1277 }
1278 }
1279 g_deviceauthDb = CREATE_HC_VECTOR(DeviceAuthDb);
1280 LoadDeviceAuthDb();
1281 DEV_AUTH_REG_DUMP_FUNC(DevAuthDataBaseDump);
1282 return HC_SUCCESS;
1283 }
1284
DestroyDatabase(void)1285 void DestroyDatabase(void)
1286 {
1287 g_databaseMutex->lock(g_databaseMutex);
1288 uint32_t index;
1289 OsAccountTrustedInfo *info;
1290 FOR_EACH_HC_VECTOR(g_deviceauthDb, index, info) {
1291 ClearGroupEntryVec(&info->groups);
1292 ClearDeviceEntryVec(&info->devices);
1293 }
1294 DESTROY_HC_VECTOR(DeviceAuthDb, &g_deviceauthDb);
1295 g_databaseMutex->unlock(g_databaseMutex);
1296 if (g_databaseMutex != NULL) {
1297 DestroyHcMutex(g_databaseMutex);
1298 HcFree(g_databaseMutex);
1299 g_databaseMutex = NULL;
1300 }
1301 }