• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
26 namespace {
27 std::recursive_mutex g_instanceMutex;
28 }
AccessTokenRemoteTokenManager()29 AccessTokenRemoteTokenManager::AccessTokenRemoteTokenManager()
30 {}
31 
~AccessTokenRemoteTokenManager()32 AccessTokenRemoteTokenManager::~AccessTokenRemoteTokenManager()
33 {
34 }
35 
GetInstance()36 AccessTokenRemoteTokenManager& AccessTokenRemoteTokenManager::GetInstance()
37 {
38     static AccessTokenRemoteTokenManager* instance = nullptr;
39     if (instance == nullptr) {
40         std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
41         if (instance == nullptr) {
42             AccessTokenRemoteTokenManager* tmp = new AccessTokenRemoteTokenManager();
43             instance = std::move(tmp);
44         }
45     }
46     return *instance;
47 }
48 
MapRemoteDeviceTokenToLocal(const std::string & deviceID,AccessTokenID remoteID)49 AccessTokenID AccessTokenRemoteTokenManager::MapRemoteDeviceTokenToLocal(const std::string& deviceID,
50     AccessTokenID remoteID)
51 {
52     if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) {
53         LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s or token %{public}x is invalid.",
54             ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID);
55         return 0;
56     }
57     ATokenTypeEnum tokeType = AccessTokenIDManager::GetInstance().GetTokenIdTypeEnum(remoteID);
58     if ((tokeType <= TOKEN_INVALID) || (tokeType >= TOKEN_TYPE_BUTT)) {
59         LOGE(ATM_DOMAIN, ATM_TAG, "Token %{public}x type is invalid.", remoteID);
60         return 0;
61     }
62     int32_t dlpFlag = AccessTokenIDManager::GetInstance().GetTokenIdDlpFlag(remoteID);
63     int32_t cloneFlag = AccessTokenIDManager::GetInstance().GetTokenIdCloneFlag(remoteID);
64 
65     AccessTokenID mapID = 0;
66     Utils::UniqueWriteGuard<Utils::RWLock> infoGuard(this->remoteDeviceLock_);
67     std::map<AccessTokenID, AccessTokenID>* mapPtr = nullptr;
68     if (remoteDeviceMap_.count(deviceID) > 0) {
69         AccessTokenRemoteDevice& device = remoteDeviceMap_[deviceID];
70         if (device.MappingTokenIDPairMap_.count(remoteID) > 0) {
71             mapID = device.MappingTokenIDPairMap_[remoteID];
72             LOGE(ATM_DOMAIN, ATM_TAG,
73                 "Device %{public}s token %{public}x has already mapped, map tokenID is %{public}x.",
74                 ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID);
75             return mapID;
76         }
77         mapPtr = &device.MappingTokenIDPairMap_;
78     } else {
79         AccessTokenRemoteDevice device;
80         device.deviceID_ = deviceID;
81         remoteDeviceMap_[deviceID] = device;
82         mapPtr = &remoteDeviceMap_[deviceID].MappingTokenIDPairMap_;
83     }
84 
85     mapID = AccessTokenIDManager::GetInstance().CreateAndRegisterTokenId(tokeType, dlpFlag, cloneFlag);
86     if (mapID == 0) {
87         LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s token %{public}x map local Token failed.",
88             ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID);
89         return 0;
90     }
91     mapPtr->insert(std::pair<AccessTokenID, AccessTokenID>(remoteID, mapID));
92     return mapID;
93 }
94 
GetDeviceAllRemoteTokenID(const std::string & deviceID,std::vector<AccessTokenID> & remoteIDs)95 int AccessTokenRemoteTokenManager::GetDeviceAllRemoteTokenID(const std::string& deviceID,
96     std::vector<AccessTokenID>& remoteIDs)
97 {
98     if (!DataValidator::IsDeviceIdValid(deviceID)) {
99         LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s is valid.", ConstantCommon::EncryptDevId(deviceID).c_str());
100         return AccessTokenError::ERR_PARAM_INVALID;
101     }
102     Utils::UniqueReadGuard<Utils::RWLock> infoGuard(this->remoteDeviceLock_);
103     if (remoteDeviceMap_.count(deviceID) < 1) {
104         LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s has not mapping.",
105             ConstantCommon::EncryptDevId(deviceID).c_str());
106         return AccessTokenError::ERR_DEVICE_NOT_EXIST;
107     }
108 
109     std::transform(remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.begin(),
110         remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.end(),
111         std::back_inserter(remoteIDs), [](const auto& mapEntry) {
112             return mapEntry.first;
113         });
114     return RET_SUCCESS;
115 }
116 
GetDeviceMappingTokenID(const std::string & deviceID,AccessTokenID remoteID)117 AccessTokenID AccessTokenRemoteTokenManager::GetDeviceMappingTokenID(const std::string& deviceID,
118     AccessTokenID remoteID)
119 {
120     if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) {
121         LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s or token %{public}x is invalid.",
122             ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID);
123         return 0;
124     }
125 
126     Utils::UniqueReadGuard<Utils::RWLock> infoGuard(this->remoteDeviceLock_);
127     if (remoteDeviceMap_.count(deviceID) < 1 ||
128         remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.count(remoteID) < 1) {
129         LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s has not mapping.",
130             ConstantCommon::EncryptDevId(deviceID).c_str());
131         return 0;
132     }
133 
134     return remoteDeviceMap_[deviceID].MappingTokenIDPairMap_[remoteID];
135 }
136 
RemoveDeviceMappingTokenID(const std::string & deviceID,AccessTokenID remoteID)137 int AccessTokenRemoteTokenManager::RemoveDeviceMappingTokenID(const std::string& deviceID,
138     AccessTokenID remoteID)
139 {
140     if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) {
141         LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s or token %{public}x is invalid.",
142             ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID);
143         return ERR_PARAM_INVALID;
144     }
145 
146     Utils::UniqueWriteGuard<Utils::RWLock> infoGuard(this->remoteDeviceLock_);
147     if (remoteDeviceMap_.count(deviceID) < 1 ||
148         remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.count(remoteID) < 1) {
149         LOGE(ATM_DOMAIN, ATM_TAG, "Device %{public}s has not mapping.",
150             ConstantCommon::EncryptDevId(deviceID).c_str());
151         return ERR_TOKEN_MAP_FAILED;
152     }
153 
154     AccessTokenID mapID = remoteDeviceMap_[deviceID].MappingTokenIDPairMap_[remoteID];
155     AccessTokenIDManager::GetInstance().ReleaseTokenId(mapID);
156 
157     remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.erase(remoteID);
158 
159     if (remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.empty()) {
160         remoteDeviceMap_.erase(deviceID);
161     }
162     return RET_SUCCESS;
163 }
164 } // namespace AccessToken
165 } // namespace Security
166 } // namespace OHOS
167