1 /* 2 * Copyright (c) 2025 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 #ifndef OHOS_DM_AUTH_ADAPTER_V2_H 17 #define OHOS_DM_AUTH_ADAPTER_V2_H 18 19 #include <string> 20 #include <memory> 21 #include <map> 22 #include <cstdint> 23 24 #include "softbus_session_callback.h" 25 #include "softbus_connector_callback.h" 26 #include "hichain_connector_callback.h" 27 #include "auth_request_state.h" 28 #include "auth_response_state.h" 29 #include "dm_device_info.h" 30 31 namespace OHOS { 32 namespace DistributedHardware { 33 extern const char* TAG_DMVERSION; 34 extern const char* TAG_DM_VERSION_V2; // compatible for new and old protocol 35 extern const char* TAG_EDITION; 36 extern const char* TAG_DATA; 37 extern const char* TAG_DATA_LEN; 38 extern const char* TAG_BUNDLE_NAME; 39 extern const char* TAG_BUNDLE_NAME_V2; 40 extern const char* TAG_PEER_BUNDLE_NAME; 41 extern const char* TAG_PEER_BUNDLE_NAME_V2; 42 extern const char* TAG_PEER_PKG_NAME; 43 extern const char* TAG_BIND_LEVEL; 44 extern const char* TAG_REPLY; 45 extern const char* TAG_APP_THUMBNAIL2; // Naming Add 2 to resolve conflicts with TAG_APP_THUMBNAIL 46 extern const char* TAG_AUTH_FINISH; 47 extern const char* TAG_LOCAL_USERID; 48 extern const char* TAG_LOCAL_DEVICE_ID; 49 extern const char* TAG_IDENTICAL_ACCOUNT; 50 extern const char* TAG_ACCOUNT_GROUPID; 51 extern const char* TAG_HAVE_CREDENTIAL; 52 extern const char* TAG_ISONLINE; 53 extern const char* TAG_AUTHED; 54 extern const char* TAG_LOCAL_ACCOUNTID; 55 extern const char* TAG_TOKENID; 56 extern const char* TAG_HOST_PKGLABEL; 57 extern const char* TAG_REMOTE_DEVICE_NAME; 58 extern const char* TAG_HOST; 59 extern const char* TAG_PROXY_CONTEXT_ID; 60 61 extern const char* APP_OPERATION_KEY; 62 extern const char* TARGET_PKG_NAME_KEY; 63 extern const char* CUSTOM_DESCRIPTION_KEY; 64 extern const char* CANCEL_DISPLAY_KEY; 65 extern const char* BUNDLE_NAME_KEY; 66 67 extern const char* AUTHENTICATE_TIMEOUT_TASK; 68 extern const char* NEGOTIATE_TIMEOUT_TASK; 69 extern const char* CONFIRM_TIMEOUT_TASK; 70 extern const char* INPUT_TIMEOUT_TASK; 71 extern const char* SESSION_HEARTBEAT_TIMEOUT_TASK; 72 extern const char* WAIT_REQUEST_TIMEOUT_TASK; 73 extern const char* AUTH_DEVICE_TIMEOUT_TASK; 74 extern const char* WAIT_PIN_AUTH_TIMEOUT_TASK; 75 extern const char* WAIT_NEGOTIATE_TIMEOUT_TASK; 76 extern const char* ADD_TIMEOUT_TASK; 77 extern const char* WAIT_SESSION_CLOSE_TIMEOUT_TASK; 78 extern const char* CLOSE_SESSION_TASK_SEPARATOR; 79 extern const char* TAG_DM_CERT_CHAIN; 80 extern const char* TAG_CERT_COUNT; 81 extern const char* TAG_CERT; 82 extern const char* TAG_IS_COMMON_FLAG; 83 extern const char* TAG_CERT_RANDOM; 84 85 extern const int32_t AUTHENTICATE_TIMEOUT; 86 extern const int32_t CONFIRM_TIMEOUT; 87 extern const int32_t NEGOTIATE_TIMEOUT; 88 extern const int32_t INPUT_TIMEOUT; 89 extern const int32_t ADD_TIMEOUT; 90 extern const int32_t WAIT_NEGOTIATE_TIMEOUT; 91 extern const int32_t WAIT_REQUEST_TIMEOUT; 92 extern const int32_t CLONE_AUTHENTICATE_TIMEOUT; 93 extern const int32_t CLONE_CONFIRM_TIMEOUT; 94 extern const int32_t CLONE_NEGOTIATE_TIMEOUT; 95 extern const int32_t CLONE_ADD_TIMEOUT; 96 extern const char* GET_ULTRASONIC_PIN_TIMEOUT_TASK; 97 extern const int32_t CLONE_WAIT_NEGOTIATE_TIMEOUT; 98 extern const int32_t CLONE_WAIT_REQUEST_TIMEOUT; 99 extern const int32_t CLONE_SESSION_HEARTBEAT_TIMEOUT; 100 extern const int32_t CLONE_PIN_AUTH_TIMEOUT; 101 extern const int32_t HML_SESSION_TIMEOUT; 102 extern const int32_t SESSION_HEARTBEAT_TIMEOUT; 103 extern const int32_t PIN_AUTH_TIMEOUT; 104 extern const int32_t EVENT_TIMEOUT; 105 106 extern const int32_t DM_AUTH_TYPE_MAX; 107 extern const int32_t DM_AUTH_TYPE_MIN; 108 extern const int32_t MIN_PIN_TOKEN; 109 extern const int32_t MAX_PIN_TOKEN; 110 111 using CleanNotifyCallback = std::function<void(uint64_t)>; 112 113 enum DmRole { 114 DM_ROLE_UNKNOWN = 0, 115 DM_ROLE_USER = 1, 116 DM_ROLE_SA, 117 DM_ROLE_FA, 118 }; 119 120 class AuthManagerBase : public ISoftbusSessionCallback, 121 public ISoftbusConnectorCallback, 122 public IHiChainConnectorCallback, 123 public IDmDeviceAuthCallback { 124 public: 125 virtual int32_t AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, 126 const std::string &extra); 127 128 virtual int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &udid, int32_t bindLevel); 129 130 virtual int32_t UnBindDevice(const std::string &pkgName, const std::string &udid, 131 int32_t bindLevel, const std::string &extra); 132 133 virtual void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result); 134 135 virtual void OnSessionClosed(const int32_t sessionId); 136 137 virtual void OnSessionDisable(); 138 139 virtual void OnDataReceived(const int32_t sessionId, const std::string message); 140 141 virtual void OnAuthDeviceDataReceived(int32_t sessionId, std::string message); 142 143 virtual void OnSoftbusJoinLNNResult(const int32_t sessionId, const char *networkId, int32_t result); 144 145 virtual void OnGroupCreated(int64_t requestId, const std::string &groupId); 146 147 virtual void OnMemberJoin(int64_t requestId, int32_t status, int32_t operationCode); 148 149 virtual int32_t EstablishAuthChannel(const std::string &deviceId); 150 151 virtual void StartNegotiate(const int32_t &sessionId); 152 153 virtual void RespNegotiate(const int32_t &sessionId); 154 155 virtual void SendAuthRequest(const int32_t &sessionId); 156 157 virtual int32_t StartAuthProcess(const int32_t &action); 158 159 virtual void StartRespAuthProcess(); 160 161 virtual int32_t CreateGroup(); 162 163 virtual int32_t ProcessPincode(const std::string &pinCode); 164 165 virtual std::string GetConnectAddr(std::string deviceId); 166 167 virtual int32_t JoinNetwork(); 168 169 virtual void AuthenticateFinish(); 170 171 virtual bool GetIsCryptoSupport(); 172 173 virtual int32_t SetAuthRequestState(std::shared_ptr<AuthRequestState> authRequestState); 174 175 virtual int32_t SetAuthResponseState(std::shared_ptr<AuthResponseState> authResponseState); 176 177 virtual int32_t GetPinCode(std::string &code); 178 179 virtual std::string GenerateGroupName(); 180 181 virtual void HandleAuthenticateTimeout(std::string name); 182 183 virtual std::string GeneratePincode(); 184 185 virtual void ShowConfigDialog(); 186 187 virtual void ShowAuthInfoDialog(bool authDeviceError = false); 188 189 virtual void ShowStartAuthDialog(); 190 191 virtual int32_t OnUserOperation(int32_t action, const std::string ¶ms); 192 193 virtual int32_t SetPageId(int32_t pageId); 194 195 virtual int32_t SetReasonAndFinish(int32_t reason, int32_t state); 196 197 virtual bool IsIdenticalAccount(); 198 199 virtual int32_t RegisterUiStateCallback(const std::string pkgName); 200 201 virtual int32_t UnRegisterUiStateCallback(const std::string pkgName); 202 203 virtual int32_t ImportAuthCode(const std::string &pkgName, const std::string &authCode); 204 205 virtual int32_t BindTarget(const std::string &pkgName, const PeerTargetId &targetId, 206 const std::map<std::string, std::string> &bindParam, int sessionId, uint64_t logicalSessionId); 207 208 virtual int32_t RegisterAuthenticationType(int32_t authenticationType); 209 210 virtual int32_t StopAuthenticateDevice(const std::string &pkgName); 211 212 virtual void OnScreenLocked() = 0; 213 214 virtual void HandleDeviceNotTrust(const std::string &udid) = 0; 215 216 virtual int32_t DeleteGroup(const std::string &pkgName, const std::string &deviceId); 217 218 // New interface added in version 5.1.0 219 virtual int32_t GetReason(); 220 // When switching from the new protocol to the old protocol, the previous parameters 221 // need to be obtained for use by the old protocol 222 virtual void GetBindTargetParams(std::string &pkgName, PeerTargetId &targetId, 223 std::map<std::string, std::string> &bindParam); 224 virtual void GetAuthCodeAndPkgName(std::string &pkgName, std::string &authCode); 225 226 virtual void SetBindTargetParams(const PeerTargetId &targetId); 227 // Register the notification function when the auth_mgr event is complete. 228 virtual void RegisterCleanNotifyCallback(CleanNotifyCallback cleanNotifyCallback); 229 230 /** 231 * @brief Classical authMgr is global, single-frame collabration with double-frame device 232 * need it. If we start bind with single-frame device, we need clear dsoftbus callback 233 * to avoid the classical authMgr affect the single to single logical. 234 * For new protocal, the source and sink authMgr lifecycle obey the bind session, no need 235 * to process it. 236 */ 237 virtual void ClearSoftbusSessionCallback(); 238 /** 239 * @brief Classical authMgr is global, if we use classical logic, we need prepare the callback. 240 */ 241 virtual void PrepareSoftbusSessionCallback(); 242 virtual void DeleteTimer(); 243 244 // Public functions 245 static std::string ConvertSrcVersion(const std::string &version, const std::string &edition); 246 static int32_t DmGetUserId(int32_t displayId); 247 static int32_t EndDream(); 248 bool IsTransferReady(); 249 void SetTransferReady(bool version); 250 void EnableInsensibleSwitching(); 251 void DisableInsensibleSwitching(); 252 int32_t ParseAuthType(const std::map<std::string, std::string> &bindParam, int32_t &authType); 253 static bool CheckProcessNameInWhiteList(const std::string &processName); 254 static bool CheckProcessNameInProxyAdaptationList(const std::string &processName); 255 protected: 256 bool NeedInsensibleSwitching(); 257 bool isTransferReady_{true}; 258 /** 259 For old protocol, when insensible switching is true, no new session needs to be created, 260 and the actual method 80 message is not sent. 261 */ 262 bool insensibleSwitching{false}; 263 }; 264 } // namespace DistributedHardware 265 } // namespace OHOS 266 #endif // OHOS_DM_AUTH_ADAPTER_V2_H 267