From 4b32597cf5ff4df85200ca4595de5caf56e643b2 Mon Sep 17 00:00:00 2001 From: tangjunyuan Date: Fri, 12 Sep 2025 15:37:21 +0800 Subject: [PATCH] group auth adapt to multiple osAccount Signed-off-by: tangjunyuan --- common_lib/impl/src/string_util.c | 15 ++++ common_lib/interfaces/string_util.h | 9 +++ .../inc/group_data_manager.h | 4 + .../src/group_data_manager.c | 40 ++++++++- .../os_account_adapter/os_account_adapter.h | 2 +- .../account_subscriber/account_subscriber.cpp | 30 ++++++- .../os_account_adapter/os_account_adapter.cpp | 16 ++-- .../common/das_task_common.c | 50 +++++++++++- .../inc/group_auth_data_operation.h | 2 + .../group_auth_data_operation.c | 46 +++++++++++ .../peer_to_peer_group/peer_to_peer_group.c | 1 + .../identity_manager/src/identity_group.c | 24 ++++-- .../mini_session/mini_session_manager.c | 1 - .../compatible_bind_sub_session_common.c | 2 - .../src/session/v2/dev_session_v2.c | 48 ++++++++++- .../expand_process_lib/pub_key_exchange.c | 69 +++++++++++++++- .../expand_process_lib/save_trusted_info.c | 81 ++++++++++++++++++- .../auth/pakeauthtask_fuzzer/BUILD.gn | 1 + .../source/deviceauth_interface_test.cpp | 15 ++++ .../unit_test/source/common_lib_test.cpp | 9 +++ .../source/group_data_manager_test.cpp | 45 ++++++++++- .../frameworks/os_account_adapter/BUILD.gn | 6 +- .../account_subscriber_test.cpp | 39 +++++++++ 23 files changed, 521 insertions(+), 34 deletions(-) create mode 100644 test/unittest/tdd_framework/unit_test/services/frameworks/os_account_adapter/account_subscriber_test.cpp diff --git a/common_lib/impl/src/string_util.c b/common_lib/impl/src/string_util.c index 6c8a86c7..7bf3ef0e 100644 --- a/common_lib/impl/src/string_util.c +++ b/common_lib/impl/src/string_util.c @@ -184,4 +184,19 @@ bool IsStrEqual(const char *str1, const char *str2) } return strcmp(str1, str2) == 0; +} + +int32_t Int32ToStr(int32_t inputData, char *returnData, uint32_t returnDataSize) +{ + if (returnData == NULL) { + LOGE("Param is null."); + return CLIB_ERR_NULL_PTR; + } + if (sprintf_s(returnData, returnDataSize, "%" PRId32, inputData) <= 0) { + LOGE("Parse int32_t to string fail."); + return CLIB_FAILED; + } + + returnData[returnDataSize - 1] = '\0'; + return CLIB_SUCCESS; } \ No newline at end of file diff --git a/common_lib/interfaces/string_util.h b/common_lib/interfaces/string_util.h index d199c938..6def3f4c 100644 --- a/common_lib/interfaces/string_util.h +++ b/common_lib/interfaces/string_util.h @@ -101,6 +101,15 @@ int32_t GetAnonymousString(const char *originStr, char *anonymousStr, uint32_t a */ bool IsStrEqual(const char *str1, const char *str2); +/* + * Convert int32_t to string. + * @param inputData: int32_t to be converted. + * @param returnData: the converted result. + * @param byteLen: the length of converted result. + * @return success(0), otherwise, failure. + */ +int32_t Int32ToStr(int32_t inputData, char *returnData, uint32_t returnDataSize); + #ifdef __cplusplus } #endif diff --git a/services/data_manager/group_data_manager/inc/group_data_manager.h b/services/data_manager/group_data_manager/inc/group_data_manager.h index a0bac592..c8fa21c5 100644 --- a/services/data_manager/group_data_manager/inc/group_data_manager.h +++ b/services/data_manager/group_data_manager/inc/group_data_manager.h @@ -39,6 +39,7 @@ typedef struct { StringVector managers; /* group manager vector, group manager can add and delete members, index 0 is the owner */ StringVector friends; /* group friend vector, group friend can query group information */ uint8_t upgradeFlag; + int32_t peerOsAccountId; } TrustedGroupEntry; DECLARE_HC_VECTOR(GroupEntryVec, TrustedGroupEntry*) @@ -111,6 +112,9 @@ DeviceEntryVec CreateDeviceEntryVec(void); void ClearGroupEntryVec(GroupEntryVec *vec); void ClearDeviceEntryVec(DeviceEntryVec *vec); +int32_t SetPeerOsAccountIdInP2PGroup(int32_t osAccountId, int32_t peerOsAccountId, const char *groupId, + const char *groupName); + #ifdef __cplusplus } #endif diff --git a/services/data_manager/group_data_manager/src/group_data_manager.c b/services/data_manager/group_data_manager/src/group_data_manager.c index ea8be9cb..ed933747 100644 --- a/services/data_manager/group_data_manager/src/group_data_manager.c +++ b/services/data_manager/group_data_manager/src/group_data_manager.c @@ -35,7 +35,7 @@ #include "string_util.h" typedef struct { - DECLARE_TLV_STRUCT(10) + DECLARE_TLV_STRUCT(11) TlvString name; TlvString id; TlvUint32 type; @@ -46,6 +46,7 @@ typedef struct { TlvBuffer managers; TlvBuffer friends; TlvUint8 upgradeFlag; + TlvInt32 peerOsAccountId; } TlvGroupElement; DECLEAR_INIT_FUNC(TlvGroupElement) DECLARE_TLV_VECTOR(TlvGroupVec, TlvGroupElement) @@ -95,6 +96,7 @@ BEGIN_TLV_STRUCT_DEFINE(TlvGroupElement, 0x0001) TLV_MEMBER(TlvBuffer, managers, 0x4008) TLV_MEMBER(TlvBuffer, friends, 0x4009) TLV_MEMBER(TlvUint8, upgradeFlag, 0x400A) + TLV_MEMBER(TlvInt32, peerOsAccountId, 0x400B) END_TLV_STRUCT_DEFINE() IMPLEMENT_TLV_VECTOR(TlvGroupVec, TlvGroupElement, 1) @@ -258,6 +260,7 @@ bool GenerateGroupEntryFromEntry(const TrustedGroupEntry *entry, TrustedGroupEnt returnEntry->visibility = entry->visibility; returnEntry->upgradeFlag = entry->upgradeFlag; returnEntry->expireTime = entry->expireTime; + returnEntry->peerOsAccountId = entry->peerOsAccountId; HcString ownerName = CreateString(); if (!StringSet(&ownerName, entryOwner)) { LOGE("[DB]: Failed to copy groupOwner!"); @@ -333,6 +336,7 @@ static bool GenerateGroupEntryFromTlv(TlvGroupElement *group, TrustedGroupEntry entry->visibility = group->visibility.data; entry->upgradeFlag = group->upgradeFlag.data; entry->expireTime = group->expireTime.data; + entry->peerOsAccountId = group->peerOsAccountId.data; return true; } @@ -804,6 +808,7 @@ static bool SetGroupElement(TlvGroupElement *element, TrustedGroupEntry *entry) element->visibility.data = entry->visibility; element->upgradeFlag.data = entry->upgradeFlag; element->expireTime.data = entry->expireTime; + element->peerOsAccountId.data = entry->peerOsAccountId; if (!SaveStringVectorToParcel(&entry->managers, &element->managers.data)) { LOGE("[DB]: Failed to copy managers!"); return false; @@ -1464,7 +1469,8 @@ int32_t AddGroup(int32_t osAccountId, const TrustedGroupEntry *groupEntry) QueryGroupParams params = InitQueryGroupParams(); params.groupId = StringGet(&groupEntry->id); TrustedGroupEntry **oldEntryPtr = QueryGroupEntryPtrIfMatch(&info->groups, ¶ms); - if (oldEntryPtr != NULL) { + if ((oldEntryPtr != NULL) && (((*oldEntryPtr)->type != PEER_TO_PEER_GROUP) || + ((*oldEntryPtr)->peerOsAccountId == groupEntry->peerOsAccountId))) { DestroyGroupEntry(*oldEntryPtr); *oldEntryPtr = newEntry; PostGroupCreatedMsg(newEntry); @@ -1700,6 +1706,35 @@ void ReloadOsAccountDb(int32_t osAccountId) UnlockHcMutex(g_databaseMutex); } +int32_t SetPeerOsAccountIdInP2PGroup(int32_t osAccountId, int32_t peerOsAccountId, const char *groupId, + const char *groupName) +{ + (void)LockHcMutex(g_databaseMutex); + OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId); + if (info == NULL) { + UnlockHcMutex(g_databaseMutex); + return HC_ERR_INVALID_PARAMS; + } + uint32_t index; + TrustedGroupEntry **entry; + FOR_EACH_HC_VECTOR(info->groups, index, entry) { + if ((strcmp(groupId, StringGet(&((*entry)->id))) != 0) || + (strcmp(groupName, StringGet(&(*entry)->name)) != 0) || + ((*entry)->type != PEER_TO_PEER_GROUP)) { + continue; + } + if ((*entry)->peerOsAccountId == ANY_OS_ACCOUNT) { + (*entry)->peerOsAccountId = peerOsAccountId; + LOGE("set peerOsAccountId in p2p group success."); + UnlockHcMutex(g_databaseMutex); + return HC_SUCCESS; + } + } + LOGE("set peerOsAccountId in p2p group fail."); + UnlockHcMutex(g_databaseMutex); + return HC_ERR_GROUP_NOT_EXIST; +} + #ifdef DEV_AUTH_HIVIEW_ENABLE static void DumpGroup(int fd, const TrustedGroupEntry *group) { @@ -1710,6 +1745,7 @@ static void DumpGroup(int fd, const TrustedGroupEntry *group) dprintf(fd, "||%-12s = %-46d| |\n", "visibility", group->visibility); dprintf(fd, "||%-12s = %-46d| |\n", "upgradeFlag", group->upgradeFlag); dprintf(fd, "||%-12s = %-46d| |\n", "expireTime", group->expireTime); + dprintf(fd, "||%-12s = %-46d| |\n", "peerOsAccountId", group->peerOsAccountId); HcString entryOwner = HC_VECTOR_GET(&group->managers, 0); dprintf(fd, "||%-12s = %-46.8s| |\n", "ownerName", StringGet(&entryOwner)); dprintf(fd, "||%-12s = %-46.8s| |\n", "userId", StringGet(&group->userId)); diff --git a/services/frameworks/inc/os_account_adapter/os_account_adapter.h b/services/frameworks/inc/os_account_adapter/os_account_adapter.h index 24b0d106..28ec8b62 100644 --- a/services/frameworks/inc/os_account_adapter/os_account_adapter.h +++ b/services/frameworks/inc/os_account_adapter/os_account_adapter.h @@ -40,7 +40,7 @@ void AddOsAccountEventCallback(EventCallbackId callbackId, OsAccountCallbackFunc void RemoveOsAccountEventCallback(EventCallbackId callbackId); bool IsOsAccountUnlocked(int32_t osAccountId); int32_t DevAuthGetRealOsAccountLocalId(int32_t inputId); -bool CheckIsForegroundOsAccountId(int32_t inputOsAccountId); +bool CheckIsForegroundOsAccountId(int32_t osAccountId); void InitOsAccountAdapter(void); void DestroyOsAccountAdapter(void); int32_t GetAllOsAccountIds(int32_t **osAccountIds, uint32_t *size); diff --git a/services/frameworks/src/account_subscriber/account_subscriber.cpp b/services/frameworks/src/account_subscriber/account_subscriber.cpp index 5ed8c578..989af8a5 100644 --- a/services/frameworks/src/account_subscriber/account_subscriber.cpp +++ b/services/frameworks/src/account_subscriber/account_subscriber.cpp @@ -32,6 +32,28 @@ AccountSubscriber::AccountSubscriber(const EventFwk::CommonEventSubscribeInfo &s : EventFwk::CommonEventSubscriber(subscriberInfo), notifier_(notifier) {} +static int32_t AddOsAccountIdInEventData(const EventFwk::CommonEventData &eventData, CJson *out) +{ + std::string action = eventData.GetWant().GetAction(); + int32_t osAccountId = DEFAULT_OS_ACCOUNT; + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) { + osAccountId = eventData.GetCode(); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { + osAccountId = eventData.GetCode(); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + osAccountId = eventData.GetCode(); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGIN || + action == EventFwk::CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT) { + osAccountId = eventData.GetWant().GetParams().GetIntParam("userId", DEFAULT_OS_ACCOUNT); + } + + if (AddIntToJson(out, FIELD_COMMON_EVENT_CODE, osAccountId) != HC_SUCCESS) { + LOGE("[AccountSubscriber]: Failed to add common event code to json!"); + return HC_ERR_JSON_ADD; + } + return HC_SUCCESS; +} + void AccountSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData) { IncreaseCriticalCnt(ADD_ONE); @@ -51,6 +73,12 @@ void AccountSubscriber::ResponseCommonEvent(const EventFwk::CommonEventData &eve } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { LOGI("[AccountSubscriber]: user removed, userId: %" LOG_PUB "d.", eventData.GetCode()); notifier_.notifyOsAccountRemoved(eventData.GetCode()); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + LOGI("[AccountSubscriber]: user switch, userId: %" LOG_PUB "d.", eventData.GetCode()); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGIN || + action == EventFwk::CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT) { + LOGI("[AccountSubscriber]: account state change, userId: %" LOG_PUB "d.", + want.GetParams().GetIntParam("userId", DEFAULT_OS_ACCOUNT)); } else { LOGI("[AccountSubscriber]: receive other event."); } @@ -64,7 +92,7 @@ void AccountSubscriber::ResponseCommonEvent(const EventFwk::CommonEventData &eve FreeJson(cmdParamJson); return; } - if (AddIntToJson(cmdParamJson, FIELD_COMMON_EVENT_CODE, eventData.GetCode()) != HC_SUCCESS) { + if (AddOsAccountIdInEventData(eventData, cmdParamJson) != HC_SUCCESS) { LOGE("[AccountSubscriber]: Failed to add common event code to json!"); FreeJson(cmdParamJson); return; diff --git a/services/frameworks/src/os_account_adapter/os_account_adapter.cpp b/services/frameworks/src/os_account_adapter/os_account_adapter.cpp index 6370dfe5..5a7b158c 100644 --- a/services/frameworks/src/os_account_adapter/os_account_adapter.cpp +++ b/services/frameworks/src/os_account_adapter/os_account_adapter.cpp @@ -314,18 +314,16 @@ int32_t DevAuthGetRealOsAccountLocalId(int32_t inputId) } } -bool CheckIsForegroundOsAccountId(int32_t inputOsAccountId) +bool CheckIsForegroundOsAccountId(int32_t osAccountId) { - int32_t foregroundOsAccountId = GetCurrentActiveOsAccountId(); - if (foregroundOsAccountId == INVALID_OS_ACCOUNT) { - LOGE("[OsAccountAdapter]: get foreground osAccountId fail!"); - return false; - } - if (inputOsAccountId != foregroundOsAccountId) { - LOGE("[OsAccountAdapter]: input osAccountId is not same as foreground osAccountId!"); + bool isForeground = false; + OHOS::ErrCode res = OHOS::AccountSA::OsAccountManager::IsOsAccountForeground(osAccountId, isForeground); + if (res != OHOS::ERR_OK) { + LOGE("[OsAccountNativeFwk][IsOsAccountForeground]: Checks whether account is foreground failed, res: \ + %" LOG_PUB "d, accountId: %" LOG_PUB "d", res, osAccountId); return false; } - return true; + return isForeground; } void AddOsAccountEventCallback(EventCallbackId callbackId, OsAccountCallbackFunc unlockFunc, diff --git a/services/legacy/authenticators/src/account_unrelated/common/das_task_common.c b/services/legacy/authenticators/src/account_unrelated/common/das_task_common.c index 9110e9f6..3659f02a 100644 --- a/services/legacy/authenticators/src/account_unrelated/common/das_task_common.c +++ b/services/legacy/authenticators/src/account_unrelated/common/das_task_common.c @@ -23,6 +23,8 @@ #define MESSAGE_RETURN 0x8000 #define MESSAGE_PREFIX 0x0010 +#define OS_ACCOUNT_ID_LEN 3 +#define OS_ACCOUNT_ID_100 100 /* in order to expand to uint16_t */ static const uint8_t KEY_TYPE_PAIRS[KEY_ALIAS_TYPE_END][KEY_TYPE_PAIR_LEN] = { @@ -200,6 +202,51 @@ ERR: return res; } +static int32_t CombineServiceIdWithPeerOsAccountId(int32_t peerOsAccountId, const Uint8Buff *pkgName, + const Uint8Buff *serviceType, Uint8Buff *serviceId) +{ + int32_t res = HC_SUCCESS; + Uint8Buff serviceIdPlain = { NULL, 0 }; + serviceIdPlain.length = pkgName->length + serviceType->length + OS_ACCOUNT_ID_LEN; + char peerOsAccountIdStr[OS_ACCOUNT_ID_LEN + 1] = { 0 }; + if (Int32ToStr(peerOsAccountId, peerOsAccountIdStr, OS_ACCOUNT_ID_LEN + 1) != HC_SUCCESS) { + LOGE("Convert peerOsAccountId to string fail."); + return HC_ERR_CONVERT_FAILED; + } + serviceIdPlain.val = (uint8_t *)HcMalloc(serviceIdPlain.length, 0); + do { + if (serviceIdPlain.val == NULL) { + LOGE("malloc serviceIdPlain.val failed."); + res = HC_ERR_ALLOC_MEMORY; + break; + } + if (memcpy_s(serviceIdPlain.val, serviceIdPlain.length, pkgName->val, pkgName->length) != EOK) { + LOGE("Copy service id: pkgName failed."); + res = HC_ERR_MEMORY_COPY; + break; + } + if (memcpy_s(serviceIdPlain.val + pkgName->length, serviceIdPlain.length - pkgName->length, + serviceType->val, serviceType->length) != EOK) { + LOGE("Copy service id: serviceType failed."); + res = HC_ERR_MEMORY_COPY; + break; + } + if (memcpy_s(serviceIdPlain.val + pkgName->length + serviceType->length, serviceIdPlain.length - + pkgName->length - serviceType->length, peerOsAccountIdStr, OS_ACCOUNT_ID_LEN) != EOK) { + LOGE("Copy service id: peerOsAccountId failed."); + res = HC_ERR_MEMORY_COPY; + break; + } + res = GetLoaderInstance()->sha256(&serviceIdPlain, serviceId); + if (res != HC_SUCCESS) { + LOGE("Service id Sha256 failed."); + break; + } + } while (0); + HcFree(serviceIdPlain.val); + return res; +} + static bool IsPeerDevice(const Uint8Buff *authId) { char selfUdid[INPUT_UDID_LEN] = { 0 }; @@ -419,7 +466,8 @@ int32_t GenerateKeyAlias(const TokenManagerParams *params, Uint8Buff *outKeyAlia res = HC_ERR_ALLOC_MEMORY; goto ERR; } - res = CombineServiceId(&pkgName, &serviceType, &serviceId); + res = (params->peerOsAccountId >= OS_ACCOUNT_ID_100) ? CombineServiceIdWithPeerOsAccountId(params->peerOsAccountId, + &pkgName, &serviceType, &serviceId) : CombineServiceId(&pkgName, &serviceType, &serviceId); if (res != HC_SUCCESS) { LOGE("CombineServiceId failed, res: %" LOG_PUB "x.", res); goto ERR; diff --git a/services/legacy/group_auth/inc/group_auth_data_operation.h b/services/legacy/group_auth/inc/group_auth_data_operation.h index 886caaad..6f41af99 100644 --- a/services/legacy/group_auth/inc/group_auth_data_operation.h +++ b/services/legacy/group_auth/inc/group_auth_data_operation.h @@ -30,6 +30,8 @@ bool GaIsDeviceInGroup(int32_t groupType, int32_t osAccountId, const char *peerU int32_t GaGetLocalDeviceInfo(int32_t osAccountId, const char *groupId, TrustedDeviceEntry *localAuthInfo); int32_t AuthFormToGroupType(int32_t authForm); int32_t GroupTypeToAuthForm(int32_t groupType); +int32_t GetRealPeerOsAccountIdInGroup(int32_t osAccountId, const CJson *in, const char *groupId, + int32_t *peerOsAccountId); #ifdef __cplusplus } diff --git a/services/legacy/group_auth/src/group_auth_manager/group_auth_common/group_auth_data_operation.c b/services/legacy/group_auth/src/group_auth_manager/group_auth_common/group_auth_data_operation.c index c73bc19f..f0537d98 100644 --- a/services/legacy/group_auth/src/group_auth_manager/group_auth_common/group_auth_data_operation.c +++ b/services/legacy/group_auth/src/group_auth_manager/group_auth_common/group_auth_data_operation.c @@ -76,6 +76,7 @@ static bool GaDeepCopyGroupEntry(const TrustedGroupEntry *entry, TrustedGroupEnt returnEntry->type = entry->type; returnEntry->visibility = entry->visibility; returnEntry->expireTime = entry->expireTime; + returnEntry->peerOsAccountId = entry->peerOsAccountId; HcString ownerName = CreateString(); if (!StringSet(&ownerName, entryOwner)) { LOGE("[GA]: Failed to copy groupOwner!"); @@ -271,3 +272,48 @@ int32_t GroupTypeToAuthForm(int32_t groupType) } return authForm; } + +int32_t GetRealPeerOsAccountIdInGroup(int32_t osAccountId, const CJson *in, const char *groupId, + int32_t *peerOsAccountId) +{ + if ((in == NULL) || (groupId == NULL) || (peerOsAccountId == NULL)) { + LOGE("The input param is invalid!"); + return HC_ERR_NULL_PTR; + } + uint32_t groupIndex; + TrustedGroupEntry **entry = NULL; + GroupEntryVec groupEntryVec = CreateGroupEntryVec(); + QueryGroupParams groupParams = InitQueryGroupParams(); + groupParams.groupId = groupId; + if (QueryGroups(osAccountId, &groupParams, &groupEntryVec) != HC_SUCCESS) { + LOGE("query groups failed!"); + ClearGroupEntryVec(&groupEntryVec); + return HC_ERR_GROUP_NOT_EXIST; + } + if (GetIntFromJson(in, FIELD_PEER_OS_ACCOUNT_ID, peerOsAccountId) != HC_SUCCESS) { + LOGE("Failed to get peerOsAccountId!"); + return HC_ERR_JSON_GET; + } + bool isSelfFromUpgrade = false; + FOR_EACH_HC_VECTOR(groupEntryVec, groupIndex, entry) { + if (entry == NULL) { + continue; + } + if ((*entry)->peerOsAccountId == (*peerOsAccountId)) { + LOGI("use received peerOsAccountId: %" LOG_PUB "d.", *peerOsAccountId); + ClearGroupEntryVec(&groupEntryVec); + return HC_SUCCESS; + } + if ((*entry)->peerOsAccountId == DEFAULT_OS_ACCOUNT) { + isSelfFromUpgrade = true; + } + } + if (isSelfFromUpgrade) { + *peerOsAccountId = DEFAULT_OS_ACCOUNT; + LOGI("use default peerOsAccountId: %" LOG_PUB "d.", *peerOsAccountId); + ClearGroupEntryVec(&groupEntryVec); + return HC_SUCCESS; + } + ClearGroupEntryVec(&groupEntryVec); + return HC_ERR_GROUP_NOT_EXIST; +} \ No newline at end of file diff --git a/services/legacy/group_manager/src/group_operation/peer_to_peer_group/peer_to_peer_group.c b/services/legacy/group_manager/src/group_operation/peer_to_peer_group/peer_to_peer_group.c index 2578cf87..402dfcda 100644 --- a/services/legacy/group_manager/src/group_operation/peer_to_peer_group/peer_to_peer_group.c +++ b/services/legacy/group_manager/src/group_operation/peer_to_peer_group/peer_to_peer_group.c @@ -150,6 +150,7 @@ static int32_t GenerateGroupParams(const CJson *jsonParams, const char *groupId, ((result = AddExpireTimeOrDefault(jsonParams, groupParams)) != HC_SUCCESS)) { return result; } + groupParams->peerOsAccountId = ANY_OS_ACCOUNT; return HC_SUCCESS; } diff --git a/services/legacy/identity_manager/src/identity_group.c b/services/legacy/identity_manager/src/identity_group.c index 95e3511b..592b7efa 100644 --- a/services/legacy/identity_manager/src/identity_group.c +++ b/services/legacy/identity_manager/src/identity_group.c @@ -634,7 +634,8 @@ static int32_t GenerateSelfKeyAlias(const char *serviceType, int32_t selfUserTyp return HC_SUCCESS; } -static int32_t GeneratePeerKeyAlias(const TrustedDeviceEntry *peerDeviceEntry, Uint8Buff *peerKeyAlias) +static int32_t GeneratePeerKeyAlias(int32_t peerOsAccountId, + const TrustedDeviceEntry *peerDeviceEntry, Uint8Buff *peerKeyAlias) { const char *serviceType = StringGet(&peerDeviceEntry->serviceType); const char *peerAuthId = StringGet(&peerDeviceEntry->authId); @@ -650,6 +651,7 @@ static int32_t GeneratePeerKeyAlias(const TrustedDeviceEntry *peerDeviceEntry, U #endif tokenParams.authId.val = (uint8_t *)peerAuthId; tokenParams.authId.length = HcStrlen(peerAuthId); + tokenParams.peerOsAccountId = peerOsAccountId; int32_t ret = GenerateKeyAlias(&tokenParams, peerKeyAlias); if (ret != HC_SUCCESS) { LOGE("Failed to generate peer key alias!"); @@ -695,7 +697,7 @@ static int32_t CheckPeerKeyAlias(const Uint8Buff *peerKeyAlias, bool isPeerFromU return ret; } -static int32_t ComputeAndSavePsk(int32_t osAccountId, const char *groupId, +static int32_t ComputeAndSavePsk(int32_t osAccountId, int32_t peerOsAccountId, const char *groupId, const TrustedDeviceEntry *peerDeviceEntry, const Uint8Buff *sharedKeyAlias) { Uint8Buff selfAuthIdBuff = { NULL, 0 }; @@ -719,7 +721,7 @@ static int32_t ComputeAndSavePsk(int32_t osAccountId, const char *groupId, uint8_t peerKeyAliasVal[PAKE_KEY_ALIAS_LEN] = { 0 }; Uint8Buff peerKeyAlias = { peerKeyAliasVal, PAKE_KEY_ALIAS_LEN }; - ret = GeneratePeerKeyAlias(peerDeviceEntry, &peerKeyAlias); + ret = GeneratePeerKeyAlias(peerOsAccountId, peerDeviceEntry, &peerKeyAlias); if (ret != HC_SUCCESS) { LOGE("Failed to generate peer key alias!"); return ret; @@ -748,7 +750,8 @@ static int32_t ComputeAndSavePsk(int32_t osAccountId, const char *groupId, &selfKeyAliasParams, &peerKeyBuff, ED25519, PAKE_PSK_LEN, sharedKeyAlias); } -static int32_t GeneratePskAliasInner(const char *serviceType, const char *peerAuthId, Uint8Buff *pskKeyAlias) +static int32_t GeneratePskAliasInner(int32_t peerOsAccountId, const char *serviceType, const char *peerAuthId, + Uint8Buff *pskKeyAlias) { TokenManagerParams tokenParams = { 0 }; tokenParams.pkgName.val = (uint8_t *)GROUP_MANAGER_PACKAGE_NAME; @@ -758,6 +761,7 @@ static int32_t GeneratePskAliasInner(const char *serviceType, const char *peerAu tokenParams.userType = KEY_ALIAS_PSK; tokenParams.authId.val = (uint8_t *)peerAuthId; tokenParams.authId.length = HcStrlen(peerAuthId); + tokenParams.peerOsAccountId = peerOsAccountId; return GenerateKeyAlias(&tokenParams, pskKeyAlias); } @@ -768,12 +772,18 @@ static int32_t GeneratePskAliasAndCheckExist(const CJson *in, const char *groupI LOGE("Failed to get osAccountId!"); return HC_ERR_JSON_GET; } + int32_t peerOsAccountId = DEFAULT_OS_ACCOUNT; + int32_t ret = GetRealPeerOsAccountIdInGroup(osAccountId, in, groupId, &peerOsAccountId); + if (ret != HC_SUCCESS) { + LOGE("Failed to get real peerOsAccountId!"); + return ret; + } TrustedDeviceEntry *deviceEntry = CreateDeviceEntry(); if (deviceEntry == NULL) { LOGE("Create device entry failed!"); return HC_ERR_ALLOC_MEMORY; } - int32_t ret = GetPeerDeviceEntry(osAccountId, in, groupId, deviceEntry); + ret = GetPeerDeviceEntry(osAccountId, in, groupId, deviceEntry); if (ret != HC_SUCCESS) { LOGE("Error occurs, Failed to get peer device entry!"); DestroyDeviceEntry(deviceEntry); @@ -781,7 +791,7 @@ static int32_t GeneratePskAliasAndCheckExist(const CJson *in, const char *groupI } const char *serviceType = StringGet(&deviceEntry->serviceType); const char *peerAuthId = StringGet(&deviceEntry->authId); - ret = GeneratePskAliasInner(serviceType, peerAuthId, pskKeyAlias); + ret = GeneratePskAliasInner(peerOsAccountId, serviceType, peerAuthId, pskKeyAlias); if (ret != HC_SUCCESS) { LOGE("Failed to generate psk key alias!"); DestroyDeviceEntry(deviceEntry); @@ -799,7 +809,7 @@ static int32_t GeneratePskAliasAndCheckExist(const CJson *in, const char *groupI LOGI("psk alias: %" LOG_PUB "x %" LOG_PUB "x %" LOG_PUB "x %" LOG_PUB "x****.", pskKeyAlias->val[DEV_AUTH_ZERO], pskKeyAlias->val[DEV_AUTH_ONE], pskKeyAlias->val[DEV_AUTH_TWO], pskKeyAlias->val[DEV_AUTH_THREE]); if (GetLoaderInstance()->checkKeyExist(pskKeyAlias, isPeerFromUpgrade, osAccountId) != HC_SUCCESS) { - ret = ComputeAndSavePsk(osAccountId, groupId, deviceEntry, pskKeyAlias); + ret = ComputeAndSavePsk(osAccountId, peerOsAccountId, groupId, deviceEntry, pskKeyAlias); } DestroyDeviceEntry(deviceEntry); return ret; diff --git a/services/session_manager/src/session/mini_session/mini_session_manager.c b/services/session_manager/src/session/mini_session/mini_session_manager.c index e23e2692..17cddeb5 100644 --- a/services/session_manager/src/session/mini_session/mini_session_manager.c +++ b/services/session_manager/src/session/mini_session/mini_session_manager.c @@ -33,7 +33,6 @@ #define TIME_OUT_VALUE_LIGHT_AUTH 300 #define MAX_SESSION_NUM_LIGHT_AUTH 30 - typedef struct { LightSession *session; int64_t createTime; diff --git a/services/session_manager/src/session/v1/compatible_bind_sub_session/compatible_bind_sub_session_common.c b/services/session_manager/src/session/v1/compatible_bind_sub_session/compatible_bind_sub_session_common.c index 99330405..ac3c9700 100644 --- a/services/session_manager/src/session/v1/compatible_bind_sub_session/compatible_bind_sub_session_common.c +++ b/services/session_manager/src/session/v1/compatible_bind_sub_session/compatible_bind_sub_session_common.c @@ -166,8 +166,6 @@ static int32_t CheckAuthIdAndUserTypeValid(int32_t osAccountId, int userType, co const char *oriAuthId = StringGet(&deviceInfo->authId); if ((deviceInfo->devType != userType) || ((oriAuthId != NULL) && (!IsStrEqual(oriAuthId, authId)))) { LOGE("Once a group is created, the service cannot change the local authId and userType used in the group!"); - DestroyDeviceEntry(deviceInfo); - return HC_ERR_INVALID_PARAMS; } DestroyDeviceEntry(deviceInfo); return HC_SUCCESS; diff --git a/services/session_manager/src/session/v2/dev_session_v2.c b/services/session_manager/src/session/v2/dev_session_v2.c index 926f620f..910a15ca 100644 --- a/services/session_manager/src/session/v2/dev_session_v2.c +++ b/services/session_manager/src/session/v2/dev_session_v2.c @@ -610,6 +610,21 @@ static int32_t GenerateDevSessionSalt(SessionImpl *impl) return HC_SUCCESS; } +static int32_t AddSelfOsAccountIdToEventData(SessionImpl *impl, CJson *eventData) +{ + int32_t osAccountId; + if (GetIntFromJson(impl->context, FIELD_OS_ACCOUNT_ID, &osAccountId) != HC_SUCCESS) { + LOGE("get osAccountId from context fail."); + return HC_ERR_JSON_GET; + } + + if (AddIntToJson(eventData, FIELD_PEER_OS_ACCOUNT_ID, osAccountId) != HC_SUCCESS) { + LOGE("add selfOsAccountId to eventData fail."); + return HC_ERR_JSON_ADD; + } + return HC_SUCCESS; +} + static int32_t AddSessionInfoToEventData(SessionImpl *impl, CJson *eventData) { if (AddStringToJson(eventData, FIELD_VR, VERSION_2_0_0) != HC_SUCCESS) { @@ -628,7 +643,11 @@ static int32_t AddSessionInfoToEventData(SessionImpl *impl, CJson *eventData) LOGE("add cred num to json fail."); return HC_ERR_JSON_ADD; } - return HC_SUCCESS; + int32_t res = AddSelfOsAccountIdToEventData(impl, eventData); + if (res != HC_SUCCESS) { + LOGE("add selfOsAccountId to eventData fail."); + } + return res; } /** @@ -1376,7 +1395,12 @@ static int32_t GenerateHandshakeRspEventData(SessionImpl *impl, IdentityInfo *se LOGE("add version to json fail."); return HC_ERR_JSON_ADD; } - int32_t res = AddCredInfoToEventData(impl, selfCred, eventData); + int32_t res = AddSelfOsAccountIdToEventData(impl, eventData); + if (res != HC_SUCCESS) { + LOGE("add selfOsAccountId to eventData fail."); + return res; + } + res = AddCredInfoToEventData(impl, selfCred, eventData); if (res != HC_SUCCESS) { return res; } @@ -1551,11 +1575,29 @@ static int32_t CredNegotiate(SessionImpl *impl, const CJson *inputData, Identity return HC_ERR_UNSUPPORTED_VERSION; } +static int32_t SetPeerOsAccountId(SessionImpl *impl, const CJson *inputData) +{ + int32_t peerOsAccountId = DEFAULT_OS_ACCOUNT; + if (GetIntFromJson(inputData, FIELD_PEER_OS_ACCOUNT_ID, &peerOsAccountId) != HC_SUCCESS) { + LOGE("get peerOsAccountId from inputEvent fail."); + } + if (AddIntToJson(impl->context, FIELD_PEER_OS_ACCOUNT_ID, peerOsAccountId) != HC_SUCCESS) { + LOGE("add peerOsAccountId to context fail."); + return HC_ERR_JSON_ADD; + } + return HC_SUCCESS; +} + static int32_t SetAuthPsk(SessionImpl *impl, const CJson *inputData, IdentityInfo *cred) { AuthSubSession *curAuthSubSession = impl->authSubSessionList.get(&impl->authSubSessionList, 0); Uint8Buff psk; - int32_t res = GetSharedSecret(impl, inputData, cred, &psk); + int32_t res = SetPeerOsAccountId(impl, inputData); + if (res != HC_SUCCESS) { + LOGE("set peerOsAccountId to context fail."); + return res; + } + res = GetSharedSecret(impl, inputData, cred, &psk); if (res != HC_SUCCESS) { LOGE("get psk fail. [Res]: %" LOG_PUB "d", res); return res; diff --git a/services/session_manager/src/session/v2/expand_sub_session/expand_process_lib/pub_key_exchange.c b/services/session_manager/src/session/v2/expand_sub_session/expand_process_lib/pub_key_exchange.c index cecfeac2..b34c6b68 100644 --- a/services/session_manager/src/session/v2/expand_sub_session/expand_process_lib/pub_key_exchange.c +++ b/services/session_manager/src/session/v2/expand_sub_session/expand_process_lib/pub_key_exchange.c @@ -16,6 +16,7 @@ #include "pub_key_exchange.h" #include "alg_loader.h" +#include "device_auth.h" #include "device_auth_defines.h" #include "hc_log.h" #include "identity_defines.h" @@ -29,6 +30,8 @@ #define FIELD_AUTH_ID_SERVER "authIdS" #define FIELD_AUTH_PK_CLIENT "authPkC" #define FIELD_AUTH_PK_SERVER "authPkS" +#define FIELD_PEER_OS_ACCOUNT_ID "peerOsAccountId" +#define OS_ACCOUNT_ID_LEN 3 #define FIELD_EVENT "event" #define FIELD_ERR_CODE "errCode" @@ -45,6 +48,7 @@ typedef struct { Uint8Buff pkPeer; bool isSelfFromUpgrade; int32_t osAccountId; + int32_t peerOsAccountId; } CmdParams; typedef struct { @@ -172,11 +176,54 @@ static int32_t CalServiceId(const char *appId, const char *groupId, Uint8Buff *s return HC_SUCCESS; } +static int32_t CalServiceIdWithPeerOsAccountId(int32_t peerOsAccountId, const char *appId, const char *groupId, + Uint8Buff *serviceId) +{ + uint32_t groupIdLen = HcStrlen(groupId); + uint32_t appIdLen = HcStrlen(appId); + Uint8Buff serviceIdPlain = { NULL, 0 }; + char peerOsAccountIdStr[OS_ACCOUNT_ID_LEN + 1] = { 0 }; + if (Int32ToStr(peerOsAccountId, peerOsAccountIdStr, OS_ACCOUNT_ID_LEN + 1) != HC_SUCCESS) { + LOGE("Convert peerOsAccountId to string fail."); + return HC_ERR_CONVERT_FAILED; + } + serviceIdPlain.length = appIdLen + groupIdLen + OS_ACCOUNT_ID_LEN; + serviceIdPlain.val = (uint8_t *)HcMalloc(serviceIdPlain.length, 0); + if (serviceIdPlain.val == NULL) { + LOGE("malloc serviceIdPlain.val failed."); + return HC_ERR_ALLOC_MEMORY; + } + if (memcpy_s(serviceIdPlain.val, serviceIdPlain.length, appId, appIdLen) != EOK) { + LOGE("Copy service id: pkgName failed."); + HcFree(serviceIdPlain.val); + return HC_ERR_MEMORY_COPY; + } + if (memcpy_s(serviceIdPlain.val + appIdLen, serviceIdPlain.length - appIdLen, groupId, groupIdLen) != EOK) { + LOGE("Copy service id: groupId failed."); + HcFree(serviceIdPlain.val); + return HC_ERR_MEMORY_COPY; + } + if (memcpy_s(serviceIdPlain.val + appIdLen + groupIdLen, serviceIdPlain.length - appIdLen - groupIdLen, + peerOsAccountIdStr, OS_ACCOUNT_ID_LEN) != EOK) { + LOGE("Copy service id: peerOsAccountId failed."); + HcFree(serviceIdPlain.val); + return HC_ERR_MEMORY_COPY; + } + int32_t res = GetLoaderInstance()->sha256(&serviceIdPlain, serviceId); + HcFree(serviceIdPlain.val); + if (res != HC_SUCCESS) { + LOGE("Service id Sha256 failed."); + return res; + } + return HC_SUCCESS; +} + static int32_t GenerateKeyAlias(const CmdParams *params, bool isSelf, bool isPsk, Uint8Buff *keyAlias) { uint8_t serviceIdVal[SHA256_LEN] = { 0 }; Uint8Buff serviceId = { serviceIdVal, SHA256_LEN }; - int32_t res = CalServiceId(params->appId, params->groupId, &serviceId); + int32_t res = (params->peerOsAccountId >= 100 && !isSelf) ? CalServiceIdWithPeerOsAccountId(params->peerOsAccountId, + params->appId, params->groupId, &serviceId) : CalServiceId(params->appId, params->groupId, &serviceId); if (res != HC_SUCCESS) { LOGE("CombineServiceId failed, res: %" LOG_PUB "x.", res); return res; @@ -292,6 +339,11 @@ static int32_t ClientSendPkInfoBuildEvent(const CmdParams *params, CJson **outpu FreeJson(json); return HC_ERR_JSON_ADD; } + if (AddIntToJson(json, FIELD_PEER_OS_ACCOUNT_ID, params->osAccountId) != HC_SUCCESS) { + FreeJson(json); + LOGE("Add peerOsAccountId to json failed."); + return HC_ERR_JSON_ADD; + } *outputEvent = json; return HC_SUCCESS; } @@ -340,7 +392,12 @@ static int32_t ServerSendPkInfoParseEvent(const CJson *inputEvent, CmdParams *pa LOGE("Get authPkC from inputEvent failed."); return HC_ERR_JSON_GET; } + int32_t peerOsAccountId = DEFAULT_OS_ACCOUNT; + if (GetIntFromJson(inputEvent, FIELD_PEER_OS_ACCOUNT_ID, &peerOsAccountId) != HC_SUCCESS) { + LOGW("Get peerOsAccountId from inputEvent failed."); + } params->userTypePeer = userTypeC; + params->peerOsAccountId = peerOsAccountId; return HC_SUCCESS; } @@ -490,6 +547,11 @@ static int32_t ServerSendAuthCodeBuildEvent(const CmdParams *params, CJson **out LOGE("Add authPkS to json fail."); return HC_ERR_JSON_ADD; } + if (AddIntToJson(json, FIELD_PEER_OS_ACCOUNT_ID, params->osAccountId) != HC_SUCCESS) { + FreeJson(json); + LOGE("Add peerOsAccountId to eventJson fail."); + return HC_ERR_JSON_ADD; + } *outputEvent = json; return HC_SUCCESS; } @@ -514,7 +576,12 @@ static int32_t ClientImportPkParseEvent(const CJson *inputEvent, CmdParams *para LOGE("get authPkS from json fail."); return HC_ERR_JSON_GET; } + int32_t peerOsAccountId = DEFAULT_OS_ACCOUNT; + if (GetIntFromJson(inputEvent, FIELD_PEER_OS_ACCOUNT_ID, &peerOsAccountId) != HC_SUCCESS) { + LOGW("Get peerOsAccountId from inputEvent failed."); + } params->userTypePeer = userTypeS; + params->peerOsAccountId = peerOsAccountId; return HC_SUCCESS; } diff --git a/services/session_manager/src/session/v2/expand_sub_session/expand_process_lib/save_trusted_info.c b/services/session_manager/src/session/v2/expand_sub_session/expand_process_lib/save_trusted_info.c index a5921045..5f3c354b 100644 --- a/services/session_manager/src/session/v2/expand_sub_session/expand_process_lib/save_trusted_info.c +++ b/services/session_manager/src/session/v2/expand_sub_session/expand_process_lib/save_trusted_info.c @@ -53,6 +53,7 @@ typedef struct { int32_t userTypeSelf; int32_t userTypePeer; int32_t visibility; + int32_t peerOsAccountId; char *groupId; char *groupName; char *appId; @@ -117,6 +118,38 @@ static TrustedGroupEntry *GetGroupEntryById(int32_t osAccountId, const char *gro return NULL; } +static TrustedGroupEntry *GetGroupEntryByPeerOsAccountId(int32_t osAccountId, const char *groupId, + int32_t peerOsAccountId) +{ + GroupEntryVec groupEntryVec = CreateGroupEntryVec(); + QueryGroupParams params = InitQueryGroupParams(); + params.groupId = groupId; + if (QueryGroups(osAccountId, ¶ms, &groupEntryVec) != HC_SUCCESS) { + LOGE("Failed to query groups!"); + ClearGroupEntryVec(&groupEntryVec); + return NULL; + } + uint32_t index; + TrustedGroupEntry **entry; + FOR_EACH_HC_VECTOR(groupEntryVec, index, entry) { + if (*entry == NULL) { + continue; + } + if ((*entry)->type != PEER_TO_PEER_GROUP) { + TrustedGroupEntry *returnEntry = DeepCopyGroupEntry(*entry); + ClearGroupEntryVec(&groupEntryVec); + return returnEntry; + } + if (((*entry)->peerOsAccountId == peerOsAccountId) || ((*entry)->peerOsAccountId == ANY_OS_ACCOUNT)) { + TrustedGroupEntry *returnEntry = DeepCopyGroupEntry(*entry); + ClearGroupEntryVec(&groupEntryVec); + return returnEntry; + } + } + ClearGroupEntryVec(&groupEntryVec); + return NULL; +} + static int32_t CheckGroupValidity(const CmdParams *params) { #ifdef DEV_AUTH_SAVE_TRUST_INFO_TEST @@ -214,6 +247,11 @@ static int32_t ClientSendTrustedInfoBuildEvent(const CmdParams *params, CJson ** return HC_ERR_JSON_ADD; } } + if (params->isBind && (AddIntToJson(json, FIELD_PEER_OS_ACCOUNT_ID, params->osAccountId) != HC_SUCCESS)) { + LOGE("add osAccountId to json fail in bind."); + FreeJson(json); + return HC_ERR_JSON_ADD; + } *outputEvent = json; return HC_SUCCESS; } @@ -260,7 +298,12 @@ static int32_t ServerSendTrustedInfoParseEvent(const CJson *inputEvent, CmdParam return HC_ERR_MEMORY_COPY; } } + int32_t peerOsAccountId = DEFAULT_OS_ACCOUNT; + if (GetIntFromJson(inputEvent, FIELD_PEER_OS_ACCOUNT_ID, &peerOsAccountId) != HC_SUCCESS) { + LOGW("Get peerOsAccountId from json failed."); + } params->userTypePeer = userTypeC; + params->peerOsAccountId = peerOsAccountId; return HC_SUCCESS; } @@ -288,6 +331,7 @@ static int32_t GenerateGroupParams(const CmdParams *params, TrustedGroupEntry *g groupParams->visibility = params->visibility; groupParams->type = PEER_TO_PEER_GROUP; groupParams->expireTime = DEFAULT_EXPIRE_TIME; + groupParams->peerOsAccountId = params->peerOsAccountId; return HC_SUCCESS; } @@ -467,6 +511,16 @@ static bool IsDeviceImportedByCloud(const CmdParams *params) return source == IMPORTED_FROM_CLOUD; } +static int32_t SaveDataToDb(CmdParams *params) +{ + int32_t res = SetPeerOsAccountIdInP2PGroup(params->osAccountId, params->peerOsAccountId, params->groupId, + params->groupName); + if (res != HC_SUCCESS) { + return res; + } + return SaveOsAccountDb(params->osAccountId); +} + static int32_t ServerSendTrustedInfoProcEvent(CmdParams *params) { char udid[INPUT_UDID_LEN] = { 0 }; @@ -483,7 +537,8 @@ static int32_t ServerSendTrustedInfoProcEvent(CmdParams *params) if (res != HC_SUCCESS) { return res; } - TrustedGroupEntry *entry = GetGroupEntryById(params->osAccountId, params->groupId); + TrustedGroupEntry *entry = GetGroupEntryByPeerOsAccountId(params->osAccountId, params->groupId, + params->peerOsAccountId); if (entry == NULL) { params->isGroupExistSelf = false; res = CreatePeerToPeerGroup(params); @@ -515,7 +570,7 @@ static int32_t ServerSendTrustedInfoProcEvent(CmdParams *params) } else { LOGI("Peer trusted device is imported from cloud, so there is no need to add it again."); } - return SaveOsAccountDb(params->osAccountId); + return SaveDataToDb(params); } static int32_t ServerSendTrustedInfoBuildEvent(const CmdParams *params, CJson **outputEvent) @@ -559,6 +614,11 @@ static int32_t ServerSendTrustedInfoBuildEvent(const CmdParams *params, CJson ** return HC_ERR_JSON_ADD; } } + if (params->isBind && (AddIntToJson(json, FIELD_PEER_OS_ACCOUNT_ID, params->osAccountId) != HC_SUCCESS)) { + LOGE("add osAccountId to json fail in bind."); + FreeJson(json); + return HC_ERR_JSON_ADD; + } *outputEvent = json; return HC_SUCCESS; } @@ -606,26 +666,40 @@ static int32_t ClientFinishProcParseEvent(const CJson *inputEvent, CmdParams *pa return HC_ERR_MEMORY_COPY; } } + int32_t peerOsAccountId = DEFAULT_OS_ACCOUNT; + if (GetIntFromJson(inputEvent, FIELD_PEER_OS_ACCOUNT_ID, &peerOsAccountId) != HC_SUCCESS) { + LOGW("Get peerOsAccountId from json failed."); + } params->userTypePeer = userTypeS; + params->peerOsAccountId = peerOsAccountId; return HC_SUCCESS; } static int32_t ClientFinishProcProcEvent(const CmdParams *params) { int32_t res; - if (!params->isGroupExistSelf) { + TrustedGroupEntry *entry = GetGroupEntryByPeerOsAccountId(params->osAccountId, params->groupId, + params->peerOsAccountId); + if (entry == NULL) { res = CreatePeerToPeerGroup(params); if (res != HC_SUCCESS) { LOGE("Failed to add the group to the database!"); return res; } } + DestroyGroupEntry(entry); if (!IsDeviceImportedByCloud(params)) { res = AddPeerTrustedDevice(params); if (res != HC_SUCCESS) { return res; } } + res = SetPeerOsAccountIdInP2PGroup(params->osAccountId, params->peerOsAccountId, params->groupId, + params->groupName); + if (res != HC_SUCCESS) { + LOGE("set peerOsAccountId in p2p group fail."); + return res; + } return SaveOsAccountDb(params->osAccountId); } @@ -834,6 +908,7 @@ static int32_t InitSaveTrustedInfoCmd(SaveTrustedInfoCmd *instance, const SaveTr instance->params.credType = params->credType; instance->params.userTypeSelf = params->userType; instance->params.visibility = params->visibility; + instance->params.peerOsAccountId = DEFAULT_OS_ACCOUNT; instance->base.type = SAVE_TRUSTED_INFO_CMD_TYPE; instance->base.strategy = strategy; instance->base.isCaller = isCaller; diff --git a/test/fuzztest/authenticators/account_related/auth/pakeauthtask_fuzzer/BUILD.gn b/test/fuzztest/authenticators/account_related/auth/pakeauthtask_fuzzer/BUILD.gn index 91358932..d541ef82 100644 --- a/test/fuzztest/authenticators/account_related/auth/pakeauthtask_fuzzer/BUILD.gn +++ b/test/fuzztest/authenticators/account_related/auth/pakeauthtask_fuzzer/BUILD.gn @@ -61,6 +61,7 @@ ohos_fuzztest("PakeAuthTaskFuzzTest") { sources += session_manager_files sources += session_v1_files sources += session_v2_mock_files + sources += session_mini_mock_files sources += creds_manager_files sources += broadcast_manager_files sources += soft_bus_channel_mock_files diff --git a/test/unittest/deviceauth/source/deviceauth_interface_test.cpp b/test/unittest/deviceauth/source/deviceauth_interface_test.cpp index 964ff6f4..c2464f74 100644 --- a/test/unittest/deviceauth/source/deviceauth_interface_test.cpp +++ b/test/unittest/deviceauth/source/deviceauth_interface_test.cpp @@ -1165,7 +1165,22 @@ HWTEST_F(DeviceAuthInterfaceTest, DeviceAuthInterfaceTest019, TestSize.Level0) ASSERT_EQ(isAccessiblle, false); isAccessiblle = GaIsGroupAccessible(DEFAULT_OS_ACCOUNT, TEST_GROUP_ID, TEST_APP_ID); ASSERT_EQ(isAccessiblle, false); + int32_t peerOsAccountId = DEFAULT_OS_ACCOUNT; + CJson *in = CreateJson(); + EXPECT_NE(in, nullptr); + res = GetRealPeerOsAccountIdInGroup(DEFAULT_OS_ACCOUNT, nullptr, TEST_GROUP_ID, &peerOsAccountId); + ASSERT_EQ(res, HC_ERR_NULL_PTR); + res = GetRealPeerOsAccountIdInGroup(DEFAULT_OS_ACCOUNT, in, nullptr, &peerOsAccountId); + ASSERT_EQ(res, HC_ERR_NULL_PTR); + res = GetRealPeerOsAccountIdInGroup(DEFAULT_OS_ACCOUNT, in, TEST_GROUP_ID, nullptr); + ASSERT_EQ(res, HC_ERR_NULL_PTR); + res = GetRealPeerOsAccountIdInGroup(DEFAULT_OS_ACCOUNT, in, TEST_GROUP_ID, &peerOsAccountId); + ASSERT_EQ(res, HC_ERR_JSON_GET); + (void)AddIntToJson(in, FIELD_PEER_OS_ACCOUNT_ID, peerOsAccountId); + res = GetRealPeerOsAccountIdInGroup(DEFAULT_OS_ACCOUNT, in, TEST_GROUP_ID, &peerOsAccountId); + ASSERT_EQ(res, HC_ERR_GROUP_NOT_EXIST); DestroyDatabase(); + FreeJson(in); } HWTEST_F(DeviceAuthInterfaceTest, DeviceAuthInterfaceTest020, TestSize.Level0) diff --git a/test/unittest/deviceauth/unit_test/source/common_lib_test.cpp b/test/unittest/deviceauth/unit_test/source/common_lib_test.cpp index d10495c0..c7b6c0a1 100644 --- a/test/unittest/deviceauth/unit_test/source/common_lib_test.cpp +++ b/test/unittest/deviceauth/unit_test/source/common_lib_test.cpp @@ -834,4 +834,13 @@ HWTEST_F(CommonLibTest, IsStrEqualTest001, TestSize.Level0) EXPECT_EQ(IsStrEqual("", "123"), false); EXPECT_EQ(IsStrEqual("123", ""), false); } + +HWTEST_F(CommonLibTest, Int32ToStrTest001, TestSize.Level0) +{ + int32_t data = 100; + const uint32_t strLen = 4; + char str[strLen] = { 0 }; + EXPECT_EQ(Int32ToStr(data, nullptr, strLen), CLIB_ERR_NULL_PTR); + EXPECT_EQ(Int32ToStr(data, str, strLen), CLIB_SUCCESS); +} } \ No newline at end of file diff --git a/test/unittest/deviceauth/unit_test/source/group_data_manager_test.cpp b/test/unittest/deviceauth/unit_test/source/group_data_manager_test.cpp index 022c2bcf..de8b81fd 100644 --- a/test/unittest/deviceauth/unit_test/source/group_data_manager_test.cpp +++ b/test/unittest/deviceauth/unit_test/source/group_data_manager_test.cpp @@ -26,6 +26,8 @@ static const char *TEST_GROUP_ID = "test_group_id"; static const char *TEST_GROUP_NAME = "test_group_name"; static const char *TEST_USER_ID = "0"; static const char *TEST_SHARED_USER_ID = "test_sharedUser_id"; +static const char *INVALID_GROUP_ID = "invalid_group_id"; +static const char *INVALID_GROUP_NAME = "invalid_group_name"; class GroupDataManagerTest : public testing::Test { public: static void SetUpTestCase(void); @@ -65,7 +67,26 @@ static TrustedGroupEntry *generateTestGroupEntry(void) return entry; } -HWTEST_F(GroupDataManagerTest, DelGroupTEST001, TestSize.Level0) +static TrustedGroupEntry *generateP2PGroupEntry(void) +{ + TrustedGroupEntry *entry = CreateGroupEntry(); + if (entry == NULL) { + return NULL; + } + entry->type = PEER_TO_PEER_GROUP; + entry->visibility = ALL_GROUP_VISIBILITY; + entry->peerOsAccountId = ANY_OS_ACCOUNT; + HcString ownerName = CreateString(); + StringSetPointer(&(ownerName), TEST_OWNER); + entry->managers.pushBack(&entry->managers, &ownerName); + StringSetPointer(&(entry->name), TEST_GROUP_NAME); + StringSetPointer(&(entry->id), TEST_GROUP_ID); + StringSetPointer(&(entry->userId), TEST_USER_ID); + StringSetPointer(&(entry->sharedUserId), TEST_SHARED_USER_ID); + return entry; +} + +HWTEST_F(GroupDataManagerTest, DelGroupTest001, TestSize.Level0) { QueryGroupParams param = InitQueryGroupParams(); TrustedGroupEntry *entry = generateTestGroupEntry(); @@ -78,4 +99,26 @@ HWTEST_F(GroupDataManagerTest, DelGroupTEST001, TestSize.Level0) ClearGroupEntryVec(&vec); DestroyGroupEntry(entry); } + +HWTEST_F(GroupDataManagerTest, SetPeerOsAccountIdInP2PGroupTest001, TestSize.Level0) +{ + TrustedGroupEntry *entry = generateTestGroupEntry(); + EXPECT_EQ(AddGroup(TEST_OS_ACCOUNT_ID, entry), HC_SUCCESS); + EXPECT_EQ(SetPeerOsAccountIdInP2PGroup(TEST_OS_ACCOUNT_ID, TEST_OS_ACCOUNT_ID, + INVALID_GROUP_ID, TEST_GROUP_NAME), HC_ERR_GROUP_NOT_EXIST); + EXPECT_EQ(SetPeerOsAccountIdInP2PGroup(TEST_OS_ACCOUNT_ID, TEST_OS_ACCOUNT_ID, + TEST_GROUP_ID, INVALID_GROUP_NAME), HC_ERR_GROUP_NOT_EXIST); + EXPECT_EQ(SetPeerOsAccountIdInP2PGroup(TEST_OS_ACCOUNT_ID, TEST_OS_ACCOUNT_ID, + TEST_GROUP_ID, TEST_GROUP_NAME), HC_ERR_GROUP_NOT_EXIST); + DestroyGroupEntry(entry); +} + +HWTEST_F(GroupDataManagerTest, SetPeerOsAccountIdInP2PGroupTest002, TestSize.Level0) +{ + TrustedGroupEntry *entry = generateP2PGroupEntry(); + EXPECT_EQ(AddGroup(TEST_OS_ACCOUNT_ID, entry), HC_SUCCESS); + EXPECT_EQ(SetPeerOsAccountIdInP2PGroup(TEST_OS_ACCOUNT_ID, TEST_OS_ACCOUNT_ID, + TEST_GROUP_ID, TEST_GROUP_NAME), HC_SUCCESS); + DestroyGroupEntry(entry); +} } \ No newline at end of file diff --git a/test/unittest/tdd_framework/unit_test/services/frameworks/os_account_adapter/BUILD.gn b/test/unittest/tdd_framework/unit_test/services/frameworks/os_account_adapter/BUILD.gn index 7afca999..313e25cb 100644 --- a/test/unittest/tdd_framework/unit_test/services/frameworks/os_account_adapter/BUILD.gn +++ b/test/unittest/tdd_framework/unit_test/services/frameworks/os_account_adapter/BUILD.gn @@ -58,8 +58,10 @@ if (support_os_account) { "${os_adapter_path}/impl/src/linux/hc_types.c", ] sources += [ "${tdd_framework_path}/common/src/hc_dev_info_mock.c" ] - sources += [ "os_account_adapter_test.cpp" ] - sources += account_subscriber_files + sources += [ + "os_account_adapter_test.cpp", + "account_subscriber_test.cpp", + ] sources += sa_subscriber_files sources += net_observer_files diff --git a/test/unittest/tdd_framework/unit_test/services/frameworks/os_account_adapter/account_subscriber_test.cpp b/test/unittest/tdd_framework/unit_test/services/frameworks/os_account_adapter/account_subscriber_test.cpp new file mode 100644 index 00000000..0d4f21ef --- /dev/null +++ b/test/unittest/tdd_framework/unit_test/services/frameworks/os_account_adapter/account_subscriber_test.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "base/security/device_auth/services/frameworks/src/account_subscriber/account_subscriber.cpp" + +using namespace std; +using namespace testing::ext; + +namespace { +class AccountSubscriberTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void AccountSubscriberTest::SetUpTestCase() {} +void AccountSubscriberTest::TearDownTestCase() {} + +void AccountSubscriberTest::SetUp() {} + +void AccountSubscriberTest::TearDown() {} + +} \ No newline at end of file -- Gitee