1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "accesstoken_remote_token_manager.h"
17
18 #include "accesstoken_id_manager.h"
19 #include "accesstoken_common_log.h"
20 #include "access_token_error.h"
21 #include "data_validator.h"
22 #include "constant_common.h"
23 #include "tokenid_attributes.h"
24
25 namespace OHOS {
26 namespace Security {
27 namespace AccessToken {
28 namespace {
29 std::recursive_mutex g_instanceMutex;
30 }
AccessTokenRemoteTokenManager()31 AccessTokenRemoteTokenManager::AccessTokenRemoteTokenManager()
32 {}
33
~AccessTokenRemoteTokenManager()34 AccessTokenRemoteTokenManager::~AccessTokenRemoteTokenManager()
35 {
36 }
37
GetInstance()38 AccessTokenRemoteTokenManager& AccessTokenRemoteTokenManager::GetInstance()
39 {
40 static AccessTokenRemoteTokenManager* instance = nullptr;
41 if (instance == nullptr) {
42 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
43 if (instance == nullptr) {
44 AccessTokenRemoteTokenManager* tmp = new (std::nothrow) AccessTokenRemoteTokenManager();
45 instance = std::move(tmp);
46 }
47 }
48 return *instance;
49 }
50
MapRemoteDeviceTokenToLocal(const std::string & deviceID,AccessTokenID remoteID)51 AccessTokenID AccessTokenRemoteTokenManager::MapRemoteDeviceTokenToLocal(const std::string& deviceID,
52 AccessTokenID remoteID)
53 {
54 if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) {
55 LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s or token %{public}x is invalid.",
56 ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID);
57 return 0;
58 }
59 ATokenTypeEnum tokeType = TokenIDAttributes::GetTokenIdTypeEnum(remoteID);
60 if ((tokeType <= TOKEN_INVALID) || (tokeType >= TOKEN_TYPE_BUTT)) {
61 LOGE(ATM_DOMAIN, ATM_TAG, "Token %{public}x type is invalid.", remoteID);
62 return 0;
63 }
64 int32_t dlpFlag = TokenIDAttributes::GetTokenIdDlpFlag(remoteID);
65 int32_t cloneFlag = TokenIDAttributes::GetTokenIdCloneFlag(remoteID);
66
67 AccessTokenID mapID = 0;
68 Utils::UniqueWriteGuard<Utils::RWLock> infoGuard(this->remoteDeviceLock_);
69 std::map<AccessTokenID, AccessTokenID>* mapPtr = nullptr;
70 if (remoteDeviceMap_.count(deviceID) > 0) {
71 AccessTokenRemoteDevice& device = remoteDeviceMap_[deviceID];
72 if (device.MappingTokenIDPairMap_.count(remoteID) > 0) {
73 mapID = device.MappingTokenIDPairMap_[remoteID];
74 LOGE(ATM_DOMAIN, ATM_TAG,
75 "Device %{public}s token %{public}x has already mapped, map tokenID is %{public}x.",
76 ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID);
77 return mapID;
78 }
79 mapPtr = &device.MappingTokenIDPairMap_;
80 } else {
81 AccessTokenRemoteDevice device;
82 device.deviceID_ = deviceID;
83 remoteDeviceMap_[deviceID] = device;
84 mapPtr = &remoteDeviceMap_[deviceID].MappingTokenIDPairMap_;
85 }
86
87 mapID = AccessTokenIDManager::GetInstance().CreateAndRegisterTokenId(tokeType, dlpFlag, cloneFlag);
88 if (mapID == 0) {
89 LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s token %{public}x map local Token failed.",
90 ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID);
91 return 0;
92 }
93 mapPtr->insert(std::pair<AccessTokenID, AccessTokenID>(remoteID, mapID));
94 return mapID;
95 }
96
GetDeviceAllRemoteTokenID(const std::string & deviceID,std::vector<AccessTokenID> & remoteIDs)97 int AccessTokenRemoteTokenManager::GetDeviceAllRemoteTokenID(const std::string& deviceID,
98 std::vector<AccessTokenID>& remoteIDs)
99 {
100 if (!DataValidator::IsDeviceIdValid(deviceID)) {
101 LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s is valid.", ConstantCommon::EncryptDevId(deviceID).c_str());
102 return AccessTokenError::ERR_PARAM_INVALID;
103 }
104 Utils::UniqueReadGuard<Utils::RWLock> infoGuard(this->remoteDeviceLock_);
105 if (remoteDeviceMap_.count(deviceID) < 1) {
106 LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s has not mapping.",
107 ConstantCommon::EncryptDevId(deviceID).c_str());
108 return AccessTokenError::ERR_DEVICE_NOT_EXIST;
109 }
110
111 std::transform(remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.begin(),
112 remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.end(),
113 std::back_inserter(remoteIDs), [](const auto& mapEntry) {
114 return mapEntry.first;
115 });
116 return RET_SUCCESS;
117 }
118
GetDeviceMappingTokenID(const std::string & deviceID,AccessTokenID remoteID)119 AccessTokenID AccessTokenRemoteTokenManager::GetDeviceMappingTokenID(const std::string& deviceID,
120 AccessTokenID remoteID)
121 {
122 if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) {
123 LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s or token %{public}x is invalid.",
124 ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID);
125 return 0;
126 }
127
128 Utils::UniqueReadGuard<Utils::RWLock> infoGuard(this->remoteDeviceLock_);
129 if (remoteDeviceMap_.count(deviceID) < 1 ||
130 remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.count(remoteID) < 1) {
131 LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s has not mapping.",
132 ConstantCommon::EncryptDevId(deviceID).c_str());
133 return 0;
134 }
135
136 return remoteDeviceMap_[deviceID].MappingTokenIDPairMap_[remoteID];
137 }
138
RemoveDeviceMappingTokenID(const std::string & deviceID,AccessTokenID remoteID)139 int AccessTokenRemoteTokenManager::RemoveDeviceMappingTokenID(const std::string& deviceID,
140 AccessTokenID remoteID)
141 {
142 if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) {
143 LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s or token %{public}x is invalid.",
144 ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID);
145 return ERR_PARAM_INVALID;
146 }
147
148 Utils::UniqueWriteGuard<Utils::RWLock> infoGuard(this->remoteDeviceLock_);
149 if (remoteDeviceMap_.count(deviceID) < 1 ||
150 remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.count(remoteID) < 1) {
151 LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s has not mapping.",
152 ConstantCommon::EncryptDevId(deviceID).c_str());
153 return ERR_TOKEN_MAP_FAILED;
154 }
155
156 AccessTokenID mapID = remoteDeviceMap_[deviceID].MappingTokenIDPairMap_[remoteID];
157 AccessTokenIDManager::GetInstance().ReleaseTokenId(mapID);
158
159 remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.erase(remoteID);
160
161 if (remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.empty()) {
162 remoteDeviceMap_.erase(deviceID);
163 }
164 return RET_SUCCESS;
165 }
166 } // namespace AccessToken
167 } // namespace Security
168 } // namespace OHOS
169