From b1319d625a92cce128eb57e6df750e0929cf1a19 Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Fri, 1 Jul 2022 09:05:04 +0800 Subject: [PATCH 1/4] =?UTF-8?q?deviceID=E5=8C=BF=E5=90=8D=E5=8C=96=20Signe?= =?UTF-8?q?d-off-by:hanshu5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hhhhs9527 --- frameworks/common/BUILD.gn | 6 +- frameworks/common/include/constant_common.h | 44 ++++++++++++ frameworks/common/src/constant_common.cpp | 49 +++++++++++++ .../accesstoken/src/accesstoken_kit.cpp | 23 +++--- .../innerkits/privacy/src/privacy_kit.cpp | 5 +- interfaces/innerkits/tokensync/BUILD.gn | 1 + .../tokensync/src/token_sync_kit.cpp | 5 +- .../service/accesstoken_manager_service.cpp | 19 ++--- .../src/token/accesstoken_info_manager.cpp | 71 ++++++++++--------- .../accesstoken_remote_token_manager.cpp | 33 +++++---- .../privacymanager/include/common/constant.h | 2 - .../privacymanager/src/common/constant.cpp | 14 ---- .../src/record/permission_record_manager.cpp | 3 +- .../src/service/privacy_manager_service.cpp | 4 +- .../include/common/constant.h | 19 ----- .../command/delete_remote_token_command.cpp | 3 +- .../command/sync_remote_hap_token_command.cpp | 3 +- .../sync_remote_native_token_command.cpp | 3 +- .../update_remote_hap_token_command.cpp | 3 +- .../tokensyncmanager/src/common/constant.cpp | 18 ----- .../src/device/device_info_manager.cpp | 21 +++--- .../src/remote/remote_command_executor.cpp | 5 +- .../src/remote/remote_command_manager.cpp | 22 +++--- .../soft_bus_device_connection_listener.cpp | 5 +- .../src/remote/soft_bus_manager.cpp | 13 ++-- .../service/token_sync_manager_service.cpp | 24 ++++--- .../test/mock/src/constant_mock.cpp | 12 ++-- .../token_sync_service_test.cpp | 5 +- 28 files changed, 253 insertions(+), 182 deletions(-) create mode 100644 frameworks/common/include/constant_common.h create mode 100644 frameworks/common/src/constant_common.cpp diff --git a/frameworks/common/BUILD.gn b/frameworks/common/BUILD.gn index e4c4118eb..e3629cf62 100644 --- a/frameworks/common/BUILD.gn +++ b/frameworks/common/BUILD.gn @@ -32,6 +32,7 @@ ohos_shared_library("accesstoken_common_cxx") { ] sources = [ + "src/constant_common.cpp", "src/data_validator.cpp", "src/random_mbedtls.cpp", ] @@ -40,7 +41,10 @@ ohos_shared_library("accesstoken_common_cxx") { "//third_party/mbedtls:mbedtls_shared", "//utils/native/base:utils", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "startup_l2:syspara", + ] cflags_cc = [ "-DHILOG_ENABLE" ] } diff --git a/frameworks/common/include/constant_common.h b/frameworks/common/include/constant_common.h new file mode 100644 index 000000000..f520054fc --- /dev/null +++ b/frameworks/common/include/constant_common.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022 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. + */ +#ifndef FRAMEWORK_CONSTANT_COMMON_H +#define FRAMEWORK_CONSTANT_COMMON_H + +#include +#include + +namespace OHOS { +namespace Security { +namespace AccessToken { +class ConstantCommon { +public: + /** + * Device id length. + */ + const static int32_t DEVICE_UUID_LENGTH = 65; + static constexpr int32_t MINDEVICEIDLEN = 8; + static constexpr int32_t ENCRYPTLEN = 4; + static constexpr int32_t ENCRYPTBEGIN = 0; + static constexpr int32_t ENCRYPTEND = 3; + static std::string EncryptDevId(std::string deviceId); + + /** + * GetLocalDeviceId + */ + static std::string GetLocalDeviceId(); +}; +} +} +} +#endif \ No newline at end of file diff --git a/frameworks/common/src/constant_common.cpp b/frameworks/common/src/constant_common.cpp new file mode 100644 index 000000000..1ecf48f55 --- /dev/null +++ b/frameworks/common/src/constant_common.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022 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 "constant_common.h" +#include "parameter.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static const std::string REPLACE_TARGET = "****"; +} // namespace +std::string ConstantCommon::EncryptDevId(std::string deviceId) +{ + std::string result = deviceId; + if (deviceId.size() > MINDEVICEIDLEN) { + result.replace(ENCRYPTBEGIN + ENCRYPTLEN, deviceId.size() - MINDEVICEIDLEN, REPLACE_TARGET); + } else { + result.replace(ENCRYPTBEGIN, deviceId.size(), REPLACE_TARGET); + } + return result; +} + +std::string ConstantCommon::GetLocalDeviceId() +{ + static std::string localDeviceId; + if (!localDeviceId.empty()) { + return localDeviceId; + } + const int32_t DEVICE_UUID_LENGTH = 65; + char udid[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(udid, DEVICE_UUID_LENGTH); + localDeviceId = udid; + return localDeviceId; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp index 66ca1dadc..a20496aa7 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp +++ b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp @@ -20,6 +20,7 @@ #include "accesstoken_log.h" #include "accesstoken_manager_client.h" +#include "constant_common.h" #include "data_validator.h" namespace OHOS { @@ -45,8 +46,8 @@ AccessTokenIDEx AccessTokenKit::AllocHapToken(const HapInfoParams& info, const H AccessTokenID AccessTokenKit::AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID) { - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s tokenID=%{public}d", - __func__, remoteDeviceID.c_str(), remoteTokenID); + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s tokenID=%{public}d", + __func__, ConstantCommon::EncryptDevId(remoteDeviceID).c_str(), remoteTokenID); #ifdef DEBUG_API_PERFORMANCE ACCESSTOKEN_LOG_INFO(LABEL, "api_performance:start call"); AccessTokenID resID = AccessTokenManagerClient::GetInstance().AllocLocalTokenID(remoteDeviceID, remoteTokenID); @@ -324,36 +325,38 @@ int AccessTokenKit::GetAllNativeTokenInfo(std::vector& n int AccessTokenKit::SetRemoteHapTokenInfo(const std::string& deviceID, const HapTokenInfoForSync& hapSync) { - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s tokenID=%{public}d", - __func__, deviceID.c_str(), hapSync.baseInfo.tokenID); + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s tokenID=%{public}d", + __func__, ConstantCommon::EncryptDevId(deviceID).c_str(), hapSync.baseInfo.tokenID); return AccessTokenManagerClient::GetInstance().SetRemoteHapTokenInfo(deviceID, hapSync); } int AccessTokenKit::SetRemoteNativeTokenInfo(const std::string& deviceID, std::vector& nativeTokenInfoList) { - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s", __func__, deviceID.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s", __func__, + ConstantCommon::EncryptDevId(deviceID).c_str()); return AccessTokenManagerClient::GetInstance() .SetRemoteNativeTokenInfo(deviceID, nativeTokenInfoList); } int AccessTokenKit::DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID) { - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s tokenID=%{public}d", - __func__, deviceID.c_str(), tokenID); + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s tokenID=%{public}d", + __func__, ConstantCommon::EncryptDevId(deviceID).c_str(), tokenID); return AccessTokenManagerClient::GetInstance().DeleteRemoteToken(deviceID, tokenID); } int AccessTokenKit::DeleteRemoteDeviceTokens(const std::string& deviceID) { - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s", __func__, deviceID.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s", __func__, + ConstantCommon::EncryptDevId(deviceID).c_str()); return AccessTokenManagerClient::GetInstance().DeleteRemoteDeviceTokens(deviceID); } AccessTokenID AccessTokenKit::GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID) { - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s tokenID=%{public}d", - __func__, deviceID.c_str(), tokenID); + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s tokenID=%{public}d", + __func__, ConstantCommon::EncryptDevId(deviceID).c_str(), tokenID); return AccessTokenManagerClient::GetInstance().GetRemoteNativeTokenID(deviceID, tokenID); } #endif diff --git a/interfaces/innerkits/privacy/src/privacy_kit.cpp b/interfaces/innerkits/privacy/src/privacy_kit.cpp index 1693faa03..0b9557bdd 100644 --- a/interfaces/innerkits/privacy/src/privacy_kit.cpp +++ b/interfaces/innerkits/privacy/src/privacy_kit.cpp @@ -19,6 +19,7 @@ #include #include "accesstoken_log.h" +#include "constant_common.h" #include "privacy_manager_client.h" namespace OHOS { @@ -53,8 +54,8 @@ int32_t PrivacyKit::StopUsingPermission(AccessTokenID tokenID, const std::string int32_t PrivacyKit::RemovePermissionUsedRecords(AccessTokenID tokenID, const std::string& deviceID) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID=0x%{public}x, deviceID=%{private}s", - tokenID, deviceID.c_str()); + ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID=0x%{public}x, deviceID=%{public}s", + tokenID, ConstantCommon::EncryptDevId(deviceID).c_str()); return PrivacyManagerClient::GetInstance().RemovePermissionUsedRecords(tokenID, deviceID); } diff --git a/interfaces/innerkits/tokensync/BUILD.gn b/interfaces/innerkits/tokensync/BUILD.gn index 991849b9f..c595ead52 100644 --- a/interfaces/innerkits/tokensync/BUILD.gn +++ b/interfaces/innerkits/tokensync/BUILD.gn @@ -49,6 +49,7 @@ ohos_shared_library("libtokensync_sdk") { deps = [ "//base/security/access_token/frameworks/accesstoken:accesstoken_communication_adapter_cxx", + "//base/security/access_token/frameworks/common:accesstoken_common_cxx", "//utils/native/base:utils", ] diff --git a/interfaces/innerkits/tokensync/src/token_sync_kit.cpp b/interfaces/innerkits/tokensync/src/token_sync_kit.cpp index 415cf1f0f..f66f2dd1c 100644 --- a/interfaces/innerkits/tokensync/src/token_sync_kit.cpp +++ b/interfaces/innerkits/tokensync/src/token_sync_kit.cpp @@ -19,6 +19,7 @@ #include #include "accesstoken_log.h" +#include "constant_common.h" #include "token_sync_manager_client.h" namespace OHOS { @@ -32,8 +33,8 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ int TokenSyncKit::GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) { - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s tokenID=%{public}d", - __func__, deviceID.c_str(), tokenID); + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s tokenID=%{public}d", + __func__, ConstantCommon::EncryptDevId(deviceID).c_str(), tokenID); return TokenSyncManagerClient::GetInstance().GetRemoteHapTokenInfo(deviceID, tokenID); } diff --git a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp index b55f467d2..017468bd7 100644 --- a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp +++ b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp @@ -25,6 +25,7 @@ #include "atm_device_state_callback.h" #include "device_manager.h" #endif +#include "constant_common.h" #include "hap_token_info.h" #include "hap_token_info_inner.h" #include "ipc_skeleton.h" @@ -247,8 +248,8 @@ AccessTokenID AccessTokenManagerService::GetHapTokenID(int userID, const std::st AccessTokenID AccessTokenManagerService::AllocLocalTokenID( const std::string& remoteDeviceID, AccessTokenID remoteTokenID) { - ACCESSTOKEN_LOG_INFO(LABEL, "called, remoteDeviceID: %{private}s, remoteTokenID: %{public}d", - remoteDeviceID.c_str(), remoteTokenID); + ACCESSTOKEN_LOG_INFO(LABEL, "called, remoteDeviceID: %{public}s, remoteTokenID: %{public}d", + ConstantCommon::EncryptDevId(remoteDeviceID).c_str(), remoteTokenID); return AccessTokenInfoManager::GetInstance().AllocLocalTokenID(remoteDeviceID, remoteTokenID); } @@ -303,7 +304,7 @@ int AccessTokenManagerService::GetAllNativeTokenInfo(std::vector& nativeTokenInfoForSyncParcel) { - ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{private}s", deviceID.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{public}s", ConstantCommon::EncryptDevId(deviceID).c_str()); std::vector nativeList; @@ -325,8 +326,8 @@ int AccessTokenManagerService::SetRemoteNativeTokenInfo(const std::string& devic int AccessTokenManagerService::DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID) { - ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{private}s, token id %{public}d", - deviceID.c_str(), tokenID); + ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{public}s, token id %{public}d", + ConstantCommon::EncryptDevId(deviceID).c_str(), tokenID); return AccessTokenInfoManager::GetInstance().DeleteRemoteToken(deviceID, tokenID); } @@ -334,15 +335,15 @@ int AccessTokenManagerService::DeleteRemoteToken(const std::string& deviceID, Ac AccessTokenID AccessTokenManagerService::GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID) { - ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{private}s, token id %{public}d", - deviceID.c_str(), tokenID); + ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{public}s, token id %{public}d", + ConstantCommon::EncryptDevId(deviceID).c_str(), tokenID); return AccessTokenInfoManager::GetInstance().GetRemoteNativeTokenID(deviceID, tokenID); } int AccessTokenManagerService::DeleteRemoteDeviceTokens(const std::string& deviceID) { - ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{private}s", deviceID.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{public}s", ConstantCommon::EncryptDevId(deviceID).c_str()); return AccessTokenInfoManager::GetInstance().DeleteRemoteDeviceTokens(deviceID); } diff --git a/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp b/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp index 10bfcdd93..c5f21ccd5 100644 --- a/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp +++ b/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp @@ -19,6 +19,7 @@ #include "accesstoken_id_manager.h" #include "accesstoken_log.h" #include "accesstoken_remote_token_manager.h" +#include "constant_common.h" #include "data_storage.h" #include "data_translator.h" #include "data_validator.h" @@ -647,15 +648,15 @@ int AccessTokenInfoManager::SetRemoteHapTokenInfo(const std::string& deviceID, H || !DataValidator::IsDlpTypeValid(hapSync.baseInfo.dlpType) || hapSync.baseInfo.ver != DEFAULT_TOKEN_VERSION || AccessTokenIDManager::GetInstance().GetTokenIdTypeEnum(hapSync.baseInfo.tokenID) != TOKEN_HAP) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s parms invalid", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } AccessTokenID remoteID = hapSync.baseInfo.tokenID; AccessTokenID mapID = AccessTokenRemoteTokenManager::GetInstance().GetDeviceMappingTokenID(deviceID, remoteID); if (mapID != 0) { - ACCESSTOKEN_LOG_INFO(LABEL, "device %{private}s token %{public}u update exist remote hap token %{public}u.", - deviceID.c_str(), remoteID, mapID); + ACCESSTOKEN_LOG_INFO(LABEL, "device %{public}s token %{public}u update exist remote hap token %{public}u.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID); // update remote token mapping id hapSync.baseInfo.tokenID = mapID; hapSync.baseInfo.deviceID = deviceID; @@ -664,8 +665,8 @@ int AccessTokenInfoManager::SetRemoteHapTokenInfo(const std::string& deviceID, H mapID = AccessTokenRemoteTokenManager::GetInstance().MapRemoteDeviceTokenToLocal(deviceID, remoteID); if (mapID == 0) { - ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s token %{public}u map failed.", deviceID.c_str(), remoteID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s token %{public}u map failed.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return RET_FAILED; } @@ -675,12 +676,12 @@ int AccessTokenInfoManager::SetRemoteHapTokenInfo(const std::string& deviceID, H if (CreateRemoteHapTokenInfo(mapID, hapSync) == RET_FAILED) { AccessTokenRemoteTokenManager::GetInstance().RemoveDeviceMappingTokenID(deviceID, mapID); - ACCESSTOKEN_LOG_INFO(LABEL, "device %{private}s token %{public}u map to local token %{public}u failed.", - deviceID.c_str(), remoteID, mapID); + ACCESSTOKEN_LOG_INFO(LABEL, "device %{public}s token %{public}u map to local token %{public}u failed.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID); return RET_FAILED; } - ACCESSTOKEN_LOG_INFO(LABEL, "device %{private}s token %{public}u map to local token %{public}u success.", - deviceID.c_str(), remoteID, mapID); + ACCESSTOKEN_LOG_INFO(LABEL, "device %{public}s token %{public}u map to local token %{public}u success.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID); return RET_SUCCESS; } @@ -688,7 +689,7 @@ int AccessTokenInfoManager::SetRemoteNativeTokenInfo(const std::string& deviceID std::vector& nativeTokenInfoList) { if (!DataValidator::IsDeviceIdValid(deviceID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s parms invalid", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } @@ -699,8 +700,8 @@ int AccessTokenInfoManager::SetRemoteNativeTokenInfo(const std::string& deviceID nativeToken.baseInfo.dcap.size() <= 0 || AccessTokenIDManager::GetInstance().GetTokenIdTypeEnum(nativeToken.baseInfo.tokenID) != TOKEN_NATIVE) { ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s token %{public}u is invalid.", - deviceID.c_str(), nativeToken.baseInfo.tokenID); + LABEL, "device %{public}s token %{public}u is invalid.", + ConstantCommon::EncryptDevId(deviceID).c_str(), nativeToken.baseInfo.tokenID); continue; } @@ -708,8 +709,8 @@ int AccessTokenInfoManager::SetRemoteNativeTokenInfo(const std::string& deviceID AccessTokenID mapID = AccessTokenRemoteTokenManager::GetInstance().GetDeviceMappingTokenID(deviceID, remoteID); if (mapID != 0) { ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s token %{public}u has maped, no need update it.", - deviceID.c_str(), nativeToken.baseInfo.tokenID); + LABEL, "device %{public}s token %{public}u has maped, no need update it.", + ConstantCommon::EncryptDevId(deviceID).c_str(), nativeToken.baseInfo.tokenID); continue; } @@ -717,32 +718,32 @@ int AccessTokenInfoManager::SetRemoteNativeTokenInfo(const std::string& deviceID if (mapID == 0) { AccessTokenRemoteTokenManager::GetInstance().RemoveDeviceMappingTokenID(deviceID, mapID); ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s token %{public}u map failed.", - deviceID.c_str(), remoteID); + LABEL, "device %{public}s token %{public}u map failed.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); continue; } nativeToken.baseInfo.tokenID = mapID; - ACCESSTOKEN_LOG_INFO(LABEL, "device %{private}s token %{public}u map to local token %{public}u.", - deviceID.c_str(), remoteID, mapID); + ACCESSTOKEN_LOG_INFO(LABEL, "device %{public}s token %{public}u map to local token %{public}u.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID); std::shared_ptr nativePtr = std::make_shared(nativeToken.baseInfo, nativeToken.permStateList); if (nativePtr == nullptr) { AccessTokenRemoteTokenManager::GetInstance().RemoveDeviceMappingTokenID(deviceID, mapID); - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s tokenId %{public}u alloc local token failed.", - deviceID.c_str(), remoteID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s tokenId %{public}u alloc local token failed.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); continue; } nativePtr->SetRemote(true); int ret = AddNativeTokenInfo(nativePtr); if (ret != RET_SUCCESS) { AccessTokenRemoteTokenManager::GetInstance().RemoveDeviceMappingTokenID(deviceID, mapID); - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s tokenId %{public}u add local token failed.", - deviceID.c_str(), remoteID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s tokenId %{public}u add local token failed.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); continue; } - ACCESSTOKEN_LOG_INFO(LABEL, "device %{private}s token %{public}u map token %{public}u add success.", - deviceID.c_str(), remoteID, mapID); + ACCESSTOKEN_LOG_INFO(LABEL, "device %{public}s token %{public}u map token %{public}u add success.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID); } return RET_SUCCESS; @@ -751,13 +752,13 @@ int AccessTokenInfoManager::SetRemoteNativeTokenInfo(const std::string& deviceID int AccessTokenInfoManager::DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID) { if (!DataValidator::IsDeviceIdValid(deviceID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s parms invalid", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } AccessTokenID mapID = AccessTokenRemoteTokenManager::GetInstance().GetDeviceMappingTokenID(deviceID, tokenID); if (mapID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s tokenId %{public}u is not mapped", - deviceID.c_str(), tokenID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s tokenId %{public}u is not mapped", + ConstantCommon::EncryptDevId(deviceID).c_str(), tokenID); return RET_FAILED; } @@ -788,7 +789,7 @@ AccessTokenID AccessTokenInfoManager::GetRemoteNativeTokenID(const std::string& { if (!DataValidator::IsDeviceIdValid(deviceID) || AccessTokenIDManager::GetInstance().GetTokenIdTypeEnum(tokenID) != TOKEN_NATIVE) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s parms invalid", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(deviceID).c_str()); return 0; } return AccessTokenRemoteTokenManager::GetInstance().GetDeviceMappingTokenID(deviceID, tokenID); @@ -797,13 +798,14 @@ AccessTokenID AccessTokenInfoManager::GetRemoteNativeTokenID(const std::string& int AccessTokenInfoManager::DeleteRemoteDeviceTokens(const std::string& deviceID) { if (!DataValidator::IsDeviceIdValid(deviceID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s parms invalid", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } std::vector remoteTokens; int ret = AccessTokenRemoteTokenManager::GetInstance().GetDeviceAllRemoteTokenID(deviceID, remoteTokens); if (ret == RET_FAILED) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s have no remote token", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s have no remote token", + ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } for (AccessTokenID remoteID : remoteTokens) { @@ -830,11 +832,12 @@ AccessTokenID AccessTokenInfoManager::AllocLocalTokenID(const std::string& remot AccessTokenID remoteTokenID) { if (!DataValidator::IsDeviceIdValid(remoteDeviceID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s parms invalid", remoteDeviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", + ConstantCommon::EncryptDevId(remoteDeviceID).c_str()); return 0; } std::string remoteUdid = GetUdidByNodeId(remoteDeviceID); - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s remoteUdid", remoteUdid.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s remoteUdid", ConstantCommon::EncryptDevId(remoteUdid).c_str()); AccessTokenID mapID = AccessTokenRemoteTokenManager::GetInstance().GetDeviceMappingTokenID(remoteUdid, remoteTokenID); if (mapID != 0) { @@ -842,8 +845,8 @@ AccessTokenID AccessTokenInfoManager::AllocLocalTokenID(const std::string& remot } int ret = TokenSyncKit::GetRemoteHapTokenInfo(remoteUdid, remoteTokenID); if (ret != RET_SUCCESS) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s token %{public}u sync failed", - remoteUdid.c_str(), remoteTokenID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s token %{public}u sync failed", + ConstantCommon::EncryptDevId(remoteUdid).c_str(), remoteTokenID); return 0; } diff --git a/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp b/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp index 9ac41f88c..d2240dfe5 100644 --- a/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp +++ b/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp @@ -18,7 +18,7 @@ #include "accesstoken_id_manager.h" #include "accesstoken_log.h" #include "data_validator.h" - +#include "constant_common.h" namespace OHOS { namespace Security { namespace AccessToken { @@ -43,8 +43,8 @@ AccessTokenID AccessTokenRemoteTokenManager::MapRemoteDeviceTokenToLocal(const s AccessTokenID remoteID) { if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) { - ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s or token %{public}x is invalid.", deviceID.c_str(), remoteID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s or token %{public}x is invalid.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return 0; } ATokenTypeEnum tokeType = AccessTokenIDManager::GetInstance().GetTokenIdTypeEnum(remoteID); @@ -63,8 +63,8 @@ AccessTokenID AccessTokenRemoteTokenManager::MapRemoteDeviceTokenToLocal(const s if (device.MappingTokenIDPairMap_.count(remoteID) > 0) { mapID = device.MappingTokenIDPairMap_[remoteID]; ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s token %{public}x has already mapped, maptokenID is %{public}x.", - deviceID.c_str(), remoteID, mapID); + LABEL, "device %{public}s token %{public}x has already mapped, maptokenID is %{public}x.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID); return mapID; } mapPtr = &device.MappingTokenIDPairMap_; @@ -78,8 +78,8 @@ AccessTokenID AccessTokenRemoteTokenManager::MapRemoteDeviceTokenToLocal(const s mapID = AccessTokenIDManager::GetInstance().CreateAndRegisterTokenId(tokeType, dlpType); if (mapID == 0) { ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s token %{public}x map local Token failed.", - deviceID.c_str(), remoteID); + LABEL, "device %{public}s token %{public}x map local Token failed.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return 0; } mapPtr->insert(std::pair(remoteID, mapID)); @@ -90,12 +90,13 @@ int AccessTokenRemoteTokenManager::GetDeviceAllRemoteTokenID(const std::string& std::vector& remoteIDs) { if (!DataValidator::IsDeviceIdValid(deviceID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s is valid.", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s is valid.", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } Utils::UniqueReadGuard infoGuard(this->remoteDeviceLock_); if (remoteDeviceMap_.count(deviceID) < 1) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s has not mapping.", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", + ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } @@ -109,15 +110,16 @@ AccessTokenID AccessTokenRemoteTokenManager::GetDeviceMappingTokenID(const std:: AccessTokenID remoteID) { if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) { - ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s or token %{public}x is invalid.", deviceID.c_str(), remoteID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s or token %{public}x is invalid.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return 0; } Utils::UniqueReadGuard infoGuard(this->remoteDeviceLock_); if (remoteDeviceMap_.count(deviceID) < 1 || remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.count(remoteID) < 1) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s has not mapping.", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", + ConstantCommon::EncryptDevId(deviceID).c_str()); return 0; } @@ -128,15 +130,16 @@ int AccessTokenRemoteTokenManager::RemoveDeviceMappingTokenID(const std::string& AccessTokenID remoteID) { if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) { - ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s or token %{public}x is invalid.", deviceID.c_str(), remoteID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s or token %{public}x is invalid.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return RET_FAILED; } Utils::UniqueWriteGuard infoGuard(this->remoteDeviceLock_); if (remoteDeviceMap_.count(deviceID) < 1 || remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.count(remoteID) < 1) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s has not mapping.", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", + ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } diff --git a/services/privacymanager/include/common/constant.h b/services/privacymanager/include/common/constant.h index 5a7a0de70..6e3602ac6 100644 --- a/services/privacymanager/include/common/constant.h +++ b/services/privacymanager/include/common/constant.h @@ -67,8 +67,6 @@ public: public: static bool TransferPermissionToOpcode(const std::string& permissionName, int32_t& opCode); static bool TransferOpcodeToPermission(int32_t opCode, std::string& permissionName); - - static std::string GetLocalDeviceUdid(); }; } // namespace AccessToken } // namespace Security diff --git a/services/privacymanager/src/common/constant.cpp b/services/privacymanager/src/common/constant.cpp index 1f3bc55f1..34fda467f 100644 --- a/services/privacymanager/src/common/constant.cpp +++ b/services/privacymanager/src/common/constant.cpp @@ -14,7 +14,6 @@ */ #include "constant.h" -#include "parameter.h" namespace OHOS { namespace Security { @@ -70,19 +69,6 @@ bool Constant::TransferOpcodeToPermission(int32_t opCode, std::string& permissio permissionName = iter->first; return true; } - -std::string Constant::GetLocalDeviceUdid() -{ - static std::string localDeviceId; - if (!localDeviceId.empty()) { - return localDeviceId; - } - const int32_t DEVICE_UUID_LENGTH = 65; - char udid[DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(udid, DEVICE_UUID_LENGTH); - localDeviceId = udid; - return localDeviceId; -} } // namespace AccessToken } // namespace Security } // namespace OHOS \ No newline at end of file diff --git a/services/privacymanager/src/record/permission_record_manager.cpp b/services/privacymanager/src/record/permission_record_manager.cpp index a852f57f4..024790c7d 100644 --- a/services/privacymanager/src/record/permission_record_manager.cpp +++ b/services/privacymanager/src/record/permission_record_manager.cpp @@ -18,6 +18,7 @@ #include "accesstoken_kit.h" #include "accesstoken_log.h" #include "constant.h" +#include "constant_common.h" #include "data_translator.h" #include "field_const.h" #include "permission_record_repository.h" @@ -89,7 +90,7 @@ bool PermissionRecordManager::GetPermissionVisitor(AccessTokenID tokenID, Permis visitor.userId = tokenInfo.userID; visitor.bundleName = tokenInfo.bundleName; if (IsLocalDevice(tokenInfo.deviceID)) { - visitor.deviceId = Constant::GetLocalDeviceUdid(); + visitor.deviceId = ConstantCommon::GetLocalDeviceId(); visitor.isRemoteDevice = false; visitor.tokenId = tokenID; } diff --git a/services/privacymanager/src/service/privacy_manager_service.cpp b/services/privacymanager/src/service/privacy_manager_service.cpp index 0f1d28343..1f9cecd49 100644 --- a/services/privacymanager/src/service/privacy_manager_service.cpp +++ b/services/privacymanager/src/service/privacy_manager_service.cpp @@ -16,6 +16,7 @@ #include "privacy_manager_service.h" #include "accesstoken_log.h" +#include "constant_common.h" #include "constant.h" #include "ipc_skeleton.h" #include "permission_record_manager.h" @@ -94,7 +95,8 @@ int32_t PrivacyManagerService::StopUsingPermission(AccessTokenID tokenID, const int32_t PrivacyManagerService::RemovePermissionUsedRecords(AccessTokenID tokenID, const std::string& deviceID) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID: 0x%{public}x, deviceID: %{private}s", tokenID, deviceID.c_str()); + ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID: 0x%{public}x, deviceID: %{public}s", + tokenID, ConstantCommon::EncryptDevId(deviceID).c_str()); PermissionRecordManager::GetInstance().RemovePermissionUsedRecords(tokenID, deviceID); return Constant::SUCCESS; } diff --git a/services/tokensyncmanager/include/common/constant.h b/services/tokensyncmanager/include/common/constant.h index 3a7d0d5e6..391c68658 100644 --- a/services/tokensyncmanager/include/common/constant.h +++ b/services/tokensyncmanager/include/common/constant.h @@ -68,26 +68,7 @@ public: * Command result string, indicates failed. */ static const std::string COMMAND_RESULT_FAILED; - - /** - * Device id length. - */ - const static int32_t DEVICE_UUID_LENGTH = 65; - - /** - * Command status code, indicate a status of command before RPC call. - */ const static int32_t DELAY_SYNC_TOKEN_MS = 3000; - - static constexpr int32_t ENCRYPTLEN = 4; - static constexpr int32_t ENCRYPTBEGIN = 0; - static constexpr int32_t ENCRYPTEND = 3; - static std::string EncryptDevId(std::string deviceId); - - /** - * GetLocalDeviceId - */ - static std::string GetLocalDeviceId(); }; } // namespace AccessToken } // namespace Security diff --git a/services/tokensyncmanager/src/command/delete_remote_token_command.cpp b/services/tokensyncmanager/src/command/delete_remote_token_command.cpp index f09bb04c9..5167e631c 100644 --- a/services/tokensyncmanager/src/command/delete_remote_token_command.cpp +++ b/services/tokensyncmanager/src/command/delete_remote_token_command.cpp @@ -18,6 +18,7 @@ #include "accesstoken_kit.h" #include "accesstoken_log.h" #include "base_remote_command.h" +#include "constant_common.h" #include "device_info.h" #include "device_info_manager.h" @@ -73,7 +74,7 @@ void DeleteRemoteTokenCommand::Prepare() void DeleteRemoteTokenCommand::Execute() { ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: DeleteRemoteTokenCommand"); - remoteProtocol_.responseDeviceId = Constant::GetLocalDeviceId(); + remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId(); remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION; DeviceInfo devInfo; diff --git a/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp b/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp index 65d0ef836..4e3a48c9d 100644 --- a/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp +++ b/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp @@ -17,6 +17,7 @@ #include "accesstoken_kit.h" #include "accesstoken_log.h" +#include "constant_common.h" #include "base_remote_command.h" namespace OHOS { @@ -92,7 +93,7 @@ void SyncRemoteHapTokenCommand::Prepare() void SyncRemoteHapTokenCommand::Execute() { ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: SyncRemoteHapTokenCommand"); - remoteProtocol_.responseDeviceId = Constant::GetLocalDeviceId(); + remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId(); remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION; int ret = AccessTokenKit::GetHapTokenInfoFromRemote(requestTokenId_, hapTokenInfo_); diff --git a/services/tokensyncmanager/src/command/sync_remote_native_token_command.cpp b/services/tokensyncmanager/src/command/sync_remote_native_token_command.cpp index e61d6f618..3ba011c09 100644 --- a/services/tokensyncmanager/src/command/sync_remote_native_token_command.cpp +++ b/services/tokensyncmanager/src/command/sync_remote_native_token_command.cpp @@ -18,6 +18,7 @@ #include "accesstoken_kit.h" #include "accesstoken_log.h" #include "base_remote_command.h" +#include "constant_common.h" #include "device_info_manager.h" namespace OHOS { @@ -76,7 +77,7 @@ void SyncRemoteNativeTokenCommand::Prepare() void SyncRemoteNativeTokenCommand::Execute() { ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: SyncRemoteNativeTokenCommand"); - remoteProtocol_.responseDeviceId = Constant::GetLocalDeviceId(); + remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId(); remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION; int ret = AccessTokenKit::GetAllNativeTokenInfo(nativeTokenInfo_); diff --git a/services/tokensyncmanager/src/command/update_remote_hap_token_command.cpp b/services/tokensyncmanager/src/command/update_remote_hap_token_command.cpp index 0d6dfe6e6..2adccf0b6 100644 --- a/services/tokensyncmanager/src/command/update_remote_hap_token_command.cpp +++ b/services/tokensyncmanager/src/command/update_remote_hap_token_command.cpp @@ -18,6 +18,7 @@ #include "accesstoken_kit.h" #include "accesstoken_log.h" #include "base_remote_command.h" +#include "constant_common.h" #include "device_info_manager.h" namespace OHOS { @@ -69,7 +70,7 @@ void UpdateRemoteHapTokenCommand::Execute() { ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: UpdateRemoteHapTokenCommand"); - remoteProtocol_.responseDeviceId = Constant::GetLocalDeviceId(); + remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId(); remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION; DeviceInfo devInfo; diff --git a/services/tokensyncmanager/src/common/constant.cpp b/services/tokensyncmanager/src/common/constant.cpp index a20a7cc05..7de4ff187 100644 --- a/services/tokensyncmanager/src/common/constant.cpp +++ b/services/tokensyncmanager/src/common/constant.cpp @@ -23,24 +23,6 @@ static const std::string REPLACE_TARGET = "****"; } // namespace const std::string Constant::COMMAND_RESULT_SUCCESS = "success"; const std::string Constant::COMMAND_RESULT_FAILED = "execute command failed"; - -std::string Constant::EncryptDevId(std::string deviceId) -{ - std::string result = deviceId; - if (deviceId.size() >= ENCRYPTLEN) { - result.replace(ENCRYPTBEGIN, ENCRYPTEND, REPLACE_TARGET); - } else { - result.replace(ENCRYPTBEGIN, result.size() - 1, REPLACE_TARGET); - } - return result; -} - -std::string Constant::GetLocalDeviceId() -{ - char deviceIdCharArray[Constant::DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(deviceIdCharArray, Constant::DEVICE_UUID_LENGTH); - return deviceIdCharArray; -} } // namespace AccessToken } // namespace Security } // namespace OHOS diff --git a/services/tokensyncmanager/src/device/device_info_manager.cpp b/services/tokensyncmanager/src/device/device_info_manager.cpp index dfe35aa9a..9c6723618 100644 --- a/services/tokensyncmanager/src/device/device_info_manager.cpp +++ b/services/tokensyncmanager/src/device/device_info_manager.cpp @@ -14,6 +14,7 @@ */ #include "device_info_manager.h" +#include "constant_common.h" namespace OHOS { namespace Security { @@ -54,11 +55,11 @@ void DeviceInfoManager::AddDeviceInfo(const std::string &networkId, const std::s void DeviceInfoManager::RemoveAllRemoteDeviceInfo() { - char deviceIdCharArray[Constant::DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(deviceIdCharArray, Constant::DEVICE_UUID_LENGTH); + std::string localDevice = ConstantCommon::GetLocalDeviceId(); + DeviceInfo localDeviceInfoOpt; if (DeviceInfoRepository::GetInstance().FindDeviceInfo( - deviceIdCharArray, DeviceIdType::UNIQUE_DISABILITY_ID, localDeviceInfoOpt)) { + localDevice, DeviceIdType::UNIQUE_DISABILITY_ID, localDeviceInfoOpt)) { DeviceInfoRepository::GetInstance().DeleteAllDeviceInfoExceptOne(localDeviceInfoOpt); } } @@ -69,10 +70,9 @@ void DeviceInfoManager::RemoveRemoteDeviceInfo(const std::string &nodeId, Device ACCESSTOKEN_LOG_ERROR(LABEL, "removeDeviceInfoByNetworkId: nodeId is invalid"); } else { DeviceInfo deviceInfo; - char deviceIdCharArray[Constant::DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(deviceIdCharArray, Constant::DEVICE_UUID_LENGTH); + std::string localDevice = ConstantCommon::GetLocalDeviceId(); if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo)) { - if (deviceInfo.deviceId.uniqueDeviceId != deviceIdCharArray) { + if (deviceInfo.deviceId.uniqueDeviceId != localDevice) { DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, deviceIdType); } } @@ -119,17 +119,18 @@ std::string DeviceInfoManager::ConvertToUniqueDeviceIdOrFetch(const std::string } else { ACCESSTOKEN_LOG_DEBUG(LABEL, "FindDeviceInfo succeed, udid and local udid is empty, nodeId(%{public}s)", - Constant::EncryptDevId(nodeId).c_str()); + ConstantCommon::EncryptDevId(nodeId).c_str()); } } else { ACCESSTOKEN_LOG_DEBUG(LABEL, "FindDeviceInfo succeed, udid is empty, nodeId(%{public}s) ", - Constant::EncryptDevId(nodeId).c_str()); + ConstantCommon::EncryptDevId(nodeId).c_str()); result = uniqueDeviceId; } } else { ACCESSTOKEN_LOG_DEBUG( - LABEL, "FindDeviceInfo failed, nodeId(%{public}s)", Constant::EncryptDevId(nodeId).c_str()); + LABEL, "FindDeviceInfo failed, nodeId(%{public}s)", + ConstantCommon::EncryptDevId(nodeId).c_str()); auto list = DeviceInfoRepository::GetInstance().ListDeviceInfo(); auto iter = list.begin(); for (; iter != list.end(); iter++) { @@ -140,7 +141,7 @@ std::string DeviceInfoManager::ConvertToUniqueDeviceIdOrFetch(const std::string LABEL, ">>> DeviceInfoRepository device type: %{public}s", info.deviceType.c_str()); ACCESSTOKEN_LOG_DEBUG(LABEL, ">>> DeviceInfoRepository device network id: %{public}s", - Constant::EncryptDevId(info.deviceId.networkId).c_str()); + ConstantCommon::EncryptDevId(info.deviceId.networkId).c_str()); } } return result; diff --git a/services/tokensyncmanager/src/remote/remote_command_executor.cpp b/services/tokensyncmanager/src/remote/remote_command_executor.cpp index 74029f95f..7f453532c 100644 --- a/services/tokensyncmanager/src/remote/remote_command_executor.cpp +++ b/services/tokensyncmanager/src/remote/remote_command_executor.cpp @@ -14,7 +14,7 @@ */ #include "remote_command_executor.h" - +#include "constant_common.h" #include "device_info_manager.h" #include "parameter.h" #include "singleton.h" @@ -81,8 +81,7 @@ int RemoteCommandExecutor::ProcessOneCommand(const std::shared_ptr #include "device_info_manager.h" #include "sync_remote_native_token_command.h" #include "remote_command_factory.h" #include "token_sync_event_handler.h" #include "token_sync_manager_service.h" #include "accesstoken_kit.h" - -#include - +#include "constant_common.h" namespace OHOS { namespace Security { @@ -80,11 +79,13 @@ void RemoteCommandManager::RemoveCommand(const std::string &udid) int RemoteCommandManager::ExecuteCommand(const std::string &udid, const std::shared_ptr &command) { if (udid.empty() || command == nullptr) { - ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s, or null command", udid.c_str()); + ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s, or null command", + ConstantCommon::EncryptDevId(udid).c_str()); return Constant::FAILURE; } std::string uniqueId = command->remoteProtocol_.uniqueId; - ACCESSTOKEN_LOG_INFO(LABEL, "start with udid: %{public}s , uniqueId: %{public}s ", udid.c_str(), uniqueId.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "start with udid: %{public}s , uniqueId: %{public}s ", + ConstantCommon::EncryptDevId(udid).c_str(), uniqueId.c_str()); std::shared_ptr executor = GetOrCreateRemoteCommandExecutor(udid); if (executor == nullptr) { @@ -100,19 +101,20 @@ int RemoteCommandManager::ExecuteCommand(const std::string &udid, const std::sha int RemoteCommandManager::ProcessDeviceCommandImmediately(const std::string &udid) { if (udid.empty()) { - ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s", udid.c_str()); + ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s", ConstantCommon::EncryptDevId(udid).c_str()); return Constant::FAILURE; } - ACCESSTOKEN_LOG_INFO(LABEL, "start with udid:%{public}s ", udid.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "start with udid:%{public}s ", ConstantCommon::EncryptDevId(udid).c_str()); auto executorIt = executors_.find(udid); if (executorIt == executors_.end()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "no executor found, udid:%{public}s", udid.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "no executor found, udid:%{public}s", ConstantCommon::EncryptDevId(udid).c_str()); return Constant::FAILURE; } auto executor = executorIt->second; if (executor == nullptr) { - ACCESSTOKEN_LOG_INFO(LABEL, "RemoteCommandExecutor is null for udid %{public}s ", udid.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "RemoteCommandExecutor is null for udid %{public}s ", + ConstantCommon::EncryptDevId(udid).c_str()); return Constant::FAILURE; } @@ -174,7 +176,7 @@ int RemoteCommandManager::NotifyDeviceOnline(const std::string &nodeId) std::function delayed = ([=]() { const std::shared_ptr syncRemoteNativeTokenCommand = - RemoteCommandFactory::GetInstance().NewSyncRemoteNativeTokenCommand(Constant::GetLocalDeviceId(), + RemoteCommandFactory::GetInstance().NewSyncRemoteNativeTokenCommand(ConstantCommon::GetLocalDeviceId(), nodeId); const int32_t resultCode = RemoteCommandManager::GetInstance().ExecuteCommand( diff --git a/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp b/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp index 5610aadca..1f3e1ea26 100644 --- a/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp +++ b/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp @@ -18,6 +18,7 @@ #include "soft_bus_manager.h" #include "device_info_manager.h" #include "softbus_bus_center.h" +#include "constant_common.h" #include "device_manager.h" #include "dm_device_info.h" @@ -54,7 +55,7 @@ void SoftBusDeviceConnectionListener::OnDeviceOnline(const DmDeviceInfo &info) "networkId: %{public}s, uuid: %{public}s, udid: %{public}s", networkId.c_str(), uuid.c_str(), - udid.c_str()); + ConstantCommon::EncryptDevId(udid).c_str()); if (uuid != "" && udid != "") { DeviceInfoManager::GetInstance().AddDeviceInfo( @@ -76,7 +77,7 @@ void SoftBusDeviceConnectionListener::OnDeviceOffline(const DmDeviceInfo &info) "networkId: %{public}s, uuid: %{public}s, udid: %{public}s", networkId.c_str(), uuid.c_str(), - udid.c_str()); + ConstantCommon::EncryptDevId(udid).c_str()); if (uuid != "" && udid != "") { RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid); diff --git a/services/tokensyncmanager/src/remote/soft_bus_manager.cpp b/services/tokensyncmanager/src/remote/soft_bus_manager.cpp index 24275c1a0..0531903b4 100644 --- a/services/tokensyncmanager/src/remote/soft_bus_manager.cpp +++ b/services/tokensyncmanager/src/remote/soft_bus_manager.cpp @@ -15,7 +15,7 @@ #include "soft_bus_manager.h" #include - +#include "constant_common.h" #include "device_info_manager.h" #include "parameter.h" #include "softbus_bus_center.h" @@ -75,7 +75,7 @@ int SoftBusManager::AddTrustedDeviceInfo() std::string udid = GetUdidByNodeId(device.networkId); if (uuid.empty() || udid.empty()) { ACCESSTOKEN_LOG_ERROR(LABEL, "uuid = %{public}s, udid = %{public}s, uuid or udid is empty, abort.", - uuid.c_str(), udid.c_str()); + uuid.c_str(), ConstantCommon::EncryptDevId(udid).c_str()); continue; } @@ -220,7 +220,8 @@ int32_t SoftBusManager::OpenSession(const std::string &deviceId) DeviceInfo info; bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(deviceId, DeviceIdType::UNKNOWN, info); if (!result) { - ACCESSTOKEN_LOG_WARN(LABEL, "device info notfound for deviceId %{private}s", deviceId.c_str()); + ACCESSTOKEN_LOG_WARN(LABEL, "device info notfound for deviceId %{public}s", + ConstantCommon::EncryptDevId(deviceId).c_str()); return Constant::FAILURE; } std::string networkId = info.deviceId.networkId; @@ -312,11 +313,11 @@ std::string SoftBusManager::GetUniqueDeviceIdByNodeId(const std::string &nodeId) } std::string udid = GetUdidByNodeId(nodeId); if (udid.empty()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "softbus return null or empty string: %{public}s", udid.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "softbus return null or empty string: %{public}s", + ConstantCommon::EncryptDevId(udid).c_str()); return ""; } - char localUdid[Constant::DEVICE_UUID_LENGTH] = {0}; - ::GetDevUdid(localUdid, Constant::DEVICE_UUID_LENGTH); + std::string localUdid = ConstantCommon::GetLocalDeviceId(); if (udid == localUdid) { // refresh cache std::function fulfillDeviceInfo = std::bind(&SoftBusManager::FulfillLocalDeviceInfo, this); diff --git a/services/tokensyncmanager/src/service/token_sync_manager_service.cpp b/services/tokensyncmanager/src/service/token_sync_manager_service.cpp index 623e4fae9..87b44e760 100644 --- a/services/tokensyncmanager/src/service/token_sync_manager_service.cpp +++ b/services/tokensyncmanager/src/service/token_sync_manager_service.cpp @@ -18,6 +18,7 @@ #include #include "accesstoken_log.h" +#include "constant_common.h" #include "device_info_repository.h" #include "device_info.h" #include "remote_command_manager.h" @@ -95,16 +96,16 @@ int TokenSyncManagerService::GetRemoteHapTokenInfo(const std::string& deviceID, } std::string udid = devInfo.deviceId.uniqueDeviceId; const std::shared_ptr syncRemoteHapTokenCommand = - RemoteCommandFactory::GetInstance().NewSyncRemoteHapTokenCommand(Constant::GetLocalDeviceId(), + RemoteCommandFactory::GetInstance().NewSyncRemoteHapTokenCommand(ConstantCommon::GetLocalDeviceId(), deviceID, tokenID); const int32_t resultCode = RemoteCommandManager::GetInstance().ExecuteCommand(udid, syncRemoteHapTokenCommand); if (resultCode != Constant::SUCCESS) { ACCESSTOKEN_LOG_INFO(LABEL, - "RemoteExecutorManager executeCommand SyncRemoteHapTokenCommand failed, return %d", resultCode); + "RemoteExecutorManager executeCommand SyncRemoteHapTokenCommand failed, return %{public}d", resultCode); return resultCode; } - ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %d", resultCode); + ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %{public}d", resultCode); return RET_SUCCESS; } @@ -116,24 +117,24 @@ int TokenSyncManagerService::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) } std::vector devices = DeviceInfoRepository::GetInstance().ListDeviceInfo(); - std::string localUdid = Constant::GetLocalDeviceId(); + std::string localUdid = ConstantCommon::GetLocalDeviceId(); for (const DeviceInfo& device : devices) { if (device.deviceId.uniqueDeviceId == localUdid) { ACCESSTOKEN_LOG_INFO(LABEL, "no need notify local device"); continue; } const std::shared_ptr deleteRemoteTokenCommand = - RemoteCommandFactory::GetInstance().NewDeleteRemoteTokenCommand(Constant::GetLocalDeviceId(), + RemoteCommandFactory::GetInstance().NewDeleteRemoteTokenCommand(ConstantCommon::GetLocalDeviceId(), device.deviceId.uniqueDeviceId, tokenID); const int32_t resultCode = RemoteCommandManager::GetInstance().ExecuteCommand( device.deviceId.uniqueDeviceId, deleteRemoteTokenCommand); if (resultCode != Constant::SUCCESS) { ACCESSTOKEN_LOG_INFO(LABEL, - "RemoteExecutorManager executeCommand DeleteRemoteTokenCommand failed, return %d", resultCode); + "RemoteExecutorManager executeCommand DeleteRemoteTokenCommand failed, return %{public}d", resultCode); continue; } - ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %d", resultCode); + ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %{public}d", resultCode); } return RET_SUCCESS; } @@ -141,7 +142,7 @@ int TokenSyncManagerService::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) int TokenSyncManagerService::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) { std::vector devices = DeviceInfoRepository::GetInstance().ListDeviceInfo(); - std::string localUdid = Constant::GetLocalDeviceId(); + std::string localUdid = ConstantCommon::GetLocalDeviceId(); for (const DeviceInfo& device : devices) { if (device.deviceId.uniqueDeviceId == localUdid) { ACCESSTOKEN_LOG_INFO(LABEL, "no need notify local device"); @@ -149,17 +150,18 @@ int TokenSyncManagerService::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& } const std::shared_ptr updateRemoteHapTokenCommand = - RemoteCommandFactory::GetInstance().NewUpdateRemoteHapTokenCommand(Constant::GetLocalDeviceId(), + RemoteCommandFactory::GetInstance().NewUpdateRemoteHapTokenCommand(ConstantCommon::GetLocalDeviceId(), device.deviceId.uniqueDeviceId, tokenInfo); const int32_t resultCode = RemoteCommandManager::GetInstance().ExecuteCommand( device.deviceId.uniqueDeviceId, updateRemoteHapTokenCommand); if (resultCode != Constant::SUCCESS) { ACCESSTOKEN_LOG_INFO(LABEL, - "RemoteExecutorManager executeCommand updateRemoteHapTokenCommand failed, return %d", resultCode); + "RemoteExecutorManager executeCommand updateRemoteHapTokenCommand failed, return %{public}d", + resultCode); continue; } - ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %d", resultCode); + ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %{public}d", resultCode); } return RET_SUCCESS; diff --git a/services/tokensyncmanager/test/mock/src/constant_mock.cpp b/services/tokensyncmanager/test/mock/src/constant_mock.cpp index 6998e7fd6..fef282afc 100644 --- a/services/tokensyncmanager/test/mock/src/constant_mock.cpp +++ b/services/tokensyncmanager/test/mock/src/constant_mock.cpp @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "constant_common.h" #include "constant.h" namespace OHOS { @@ -22,19 +23,18 @@ static const std::string REPLACE_TARGET = "****"; } // namespace const std::string Constant::COMMAND_RESULT_SUCCESS = "success"; const std::string Constant::COMMAND_RESULT_FAILED = "execute command failed"; - -std::string Constant::EncryptDevId(std::string deviceId) +std::string ConstantCommon::EncryptDevId(std::string deviceId) { std::string result = deviceId; - if (deviceId.size() >= ENCRYPTLEN) { - result.replace(ENCRYPTBEGIN, ENCRYPTEND, REPLACE_TARGET); + if (deviceId.size() > MINDEVICEIDLEN) { + result.replace(ENCRYPTBEGIN + ENCRYPTLEN, deviceId.size() - MINDEVICEIDLEN, REPLACE_TARGET); } else { - result.replace(ENCRYPTBEGIN, result.size() - 1, REPLACE_TARGET); + result.replace(ENCRYPTBEGIN, deviceId.size(), REPLACE_TARGET); } return result; } -std::string Constant::GetLocalDeviceId() +std::string ConstantCommon::GetLocalDeviceId() { return "local:udid-001"; } diff --git a/services/tokensyncmanager/test/unittest/token_sync_service/token_sync_service_test.cpp b/services/tokensyncmanager/test/unittest/token_sync_service/token_sync_service_test.cpp index 396ceaa21..722c97a0b 100644 --- a/services/tokensyncmanager/test/unittest/token_sync_service/token_sync_service_test.cpp +++ b/services/tokensyncmanager/test/unittest/token_sync_service/token_sync_service_test.cpp @@ -22,10 +22,11 @@ #include #include +#include "gtest/gtest.h" #include "accesstoken_kit.h" #include "accesstoken_log.h" #include "base_remote_command.h" -#include "gtest/gtest.h" +#include "constant_common.h" #include "session.h" #include "soft_bus_device_connection_listener.h" #include "soft_bus_session_listener.h" @@ -91,7 +92,7 @@ void TokenSyncServiceTest::OnDeviceOffline(const DmDeviceInfo &info) "networkId: %{public}s, uuid: %{public}s, udid: %{public}s", networkId.c_str(), uuid.c_str(), - udid.c_str()); + ConstantCommon::EncryptDevId(udid).c_str()); if (uuid != "" && udid != "") { RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid); -- Gitee From 5b2fef1e3ec8a87e8d535a6e57ac59866157ae94 Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Fri, 1 Jul 2022 12:51:58 +0800 Subject: [PATCH 2/4] =?UTF-8?q?deviceID=E5=8C=BF=E5=90=8D=E5=8C=96=20Signe?= =?UTF-8?q?d-off-by:hanshu5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hhhhs9527 --- frameworks/common/src/constant_common.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/frameworks/common/src/constant_common.cpp b/frameworks/common/src/constant_common.cpp index 1ecf48f55..163b0d44a 100644 --- a/frameworks/common/src/constant_common.cpp +++ b/frameworks/common/src/constant_common.cpp @@ -40,8 +40,9 @@ std::string ConstantCommon::GetLocalDeviceId() } const int32_t DEVICE_UUID_LENGTH = 65; char udid[DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(udid, DEVICE_UUID_LENGTH); - localDeviceId = udid; + if (GetDevUdid(udid, DEVICE_UUID_LENGTH) == 0) { + localDeviceId = udid; + } return localDeviceId; } } // namespace AccessToken -- Gitee From d0a0513772178537a551a7726241a9f2c1577cac Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Fri, 1 Jul 2022 15:06:01 +0800 Subject: [PATCH 3/4] =?UTF-8?q?deviceID=E5=8C=BF=E5=90=8D=E5=8C=96=20Signe?= =?UTF-8?q?d-off-by:hanshu5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hhhhs9527 --- frameworks/common/src/constant_common.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frameworks/common/src/constant_common.cpp b/frameworks/common/src/constant_common.cpp index 163b0d44a..4052e1e1c 100644 --- a/frameworks/common/src/constant_common.cpp +++ b/frameworks/common/src/constant_common.cpp @@ -20,6 +20,7 @@ namespace Security { namespace AccessToken { namespace { static const std::string REPLACE_TARGET = "****"; +static const std::string REPLACE_TARGET_LESS_THAN_MINLEN = "*******"; } // namespace std::string ConstantCommon::EncryptDevId(std::string deviceId) { @@ -27,7 +28,7 @@ std::string ConstantCommon::EncryptDevId(std::string deviceId) if (deviceId.size() > MINDEVICEIDLEN) { result.replace(ENCRYPTBEGIN + ENCRYPTLEN, deviceId.size() - MINDEVICEIDLEN, REPLACE_TARGET); } else { - result.replace(ENCRYPTBEGIN, deviceId.size(), REPLACE_TARGET); + result.replace(ENCRYPTBEGIN + 1, deviceId.size()-1, REPLACE_TARGET_LESS_THAN_MINLEN); } return result; } -- Gitee From 95ec690320c6b3f20377b2e50b1796320e57eff1 Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Fri, 1 Jul 2022 16:27:21 +0800 Subject: [PATCH 4/4] =?UTF-8?q?deviceID=E5=8C=BF=E5=90=8D=E5=8C=96=20Signe?= =?UTF-8?q?d-off-by:hanshu5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hhhhs9527 --- frameworks/common/src/constant_common.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/frameworks/common/src/constant_common.cpp b/frameworks/common/src/constant_common.cpp index 4052e1e1c..35a9fbf08 100644 --- a/frameworks/common/src/constant_common.cpp +++ b/frameworks/common/src/constant_common.cpp @@ -25,6 +25,9 @@ static const std::string REPLACE_TARGET_LESS_THAN_MINLEN = "*******"; std::string ConstantCommon::EncryptDevId(std::string deviceId) { std::string result = deviceId; + if (deviceId.empty()) { + return result; + } if (deviceId.size() > MINDEVICEIDLEN) { result.replace(ENCRYPTBEGIN + ENCRYPTLEN, deviceId.size() - MINDEVICEIDLEN, REPLACE_TARGET); } else { -- Gitee