1 /* 2 * Copyright (c) 2022-2024 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_MANAGER_H 17 #define OHOS_DM_AUTH_MANAGER_H 18 19 #include <map> 20 #include <string> 21 22 #include "auth_request_state.h" 23 #include "auth_response_state.h" 24 #include "auth_ui_state_manager.h" 25 #include "authentication.h" 26 #include "idevice_manager_service_listener.h" 27 #include "deviceprofile_connector.h" 28 #include "dm_ability_manager.h" 29 #include "dm_adapter_manager.h" 30 #include "dm_constants.h" 31 #include "dm_device_info.h" 32 #include "dm_timer.h" 33 #include "hichain_auth_connector.h" 34 #include "hichain_connector.h" 35 #include "softbus_connector.h" 36 #include "softbus_session.h" 37 38 namespace OHOS { 39 namespace DistributedHardware { 40 typedef enum AuthState { 41 AUTH_REQUEST_INIT = 1, 42 AUTH_REQUEST_NEGOTIATE, 43 AUTH_REQUEST_NEGOTIATE_DONE, 44 AUTH_REQUEST_REPLY, 45 AUTH_REQUEST_JOIN, 46 AUTH_REQUEST_NETWORK, 47 AUTH_REQUEST_FINISH, 48 AUTH_REQUEST_CREDENTIAL, 49 AUTH_REQUEST_CREDENTIAL_DONE, 50 AUTH_REQUEST_DELETE_INIT, 51 AUTH_REQUEST_SYNCDELETE, 52 AUTH_REQUEST_SYNCDELETE_DONE, 53 AUTH_RESPONSE_INIT = 20, 54 AUTH_RESPONSE_NEGOTIATE, 55 AUTH_RESPONSE_CONFIRM, 56 AUTH_RESPONSE_GROUP, 57 AUTH_RESPONSE_SHOW, 58 AUTH_RESPONSE_FINISH, 59 AUTH_RESPONSE_CREDENTIAL, 60 AUTH_RESPONSE_SYNCDELETE, 61 AUTH_RESPONSE_SYNCDELETE_DONE, 62 } AuthState; 63 64 enum DmMsgType : int32_t { 65 MSG_TYPE_UNKNOWN = 0, 66 MSG_TYPE_NEGOTIATE = 80, 67 MSG_TYPE_RESP_NEGOTIATE = 90, 68 MSG_TYPE_REQ_AUTH = 100, 69 MSG_TYPE_INVITE_AUTH_INFO = 102, 70 MSG_TYPE_REQ_AUTH_TERMINATE = 104, 71 MSG_TYPE_RESP_AUTH = 200, 72 MSG_TYPE_JOIN_AUTH_INFO = 201, 73 MSG_TYPE_RESP_AUTH_TERMINATE = 205, 74 MSG_TYPE_CHANNEL_CLOSED = 300, 75 MSG_TYPE_SYNC_GROUP = 400, 76 MSG_TYPE_AUTH_BY_PIN = 500, 77 78 MSG_TYPE_RESP_AUTH_EXT, 79 MSG_TYPE_REQ_PUBLICKEY, 80 MSG_TYPE_RESP_PUBLICKEY, 81 MSG_TYPE_REQ_SYNC_DELETE, 82 MSG_TYPE_REQ_SYNC_DELETE_DONE, 83 MSG_TYPE_REQ_AUTH_DEVICE_NEGOTIATE = 600, 84 MSG_TYPE_RESP_AUTH_DEVICE_NEGOTIATE = 700, 85 }; 86 87 enum DmAuthType : int32_t { 88 AUTH_TYPE_CRE = 0, 89 AUTH_TYPE_PIN, 90 AUTH_TYPE_QR_CODE, 91 AUTH_TYPE_NFC, 92 AUTH_TYPE_NO_INTER_ACTION, 93 AUTH_TYPE_IMPORT_AUTH_CODE, 94 }; 95 96 typedef struct DmAuthRequestContext { 97 int32_t authType; 98 std::string localDeviceId; 99 std::string localDeviceName; 100 int32_t localDeviceTypeId; 101 std::string deviceId; 102 std::string deviceName; 103 std::string deviceTypeId; 104 int32_t sessionId; 105 int32_t groupVisibility; 106 bool cryptoSupport; 107 std::string cryptoName; 108 std::string cryptoVer; 109 std::string hostPkgName; 110 std::string targetPkgName; 111 std::string appOperation; 112 std::string appDesc; 113 std::string appName; 114 std::string customDesc; 115 std::string appThumbnail; 116 std::string token; 117 int32_t reason; 118 std::vector<std::string> syncGroupList; 119 std::string dmVersion; 120 int32_t localUserId; 121 std::string localAccountId; 122 std::vector<int32_t> bindType; 123 bool isOnline; 124 bool authed; 125 int32_t bindLevel; 126 int64_t tokenId; 127 std::string remoteAccountId; 128 int32_t remoteUserId; 129 std::string ip; 130 } DmAuthRequestContext; 131 132 typedef struct DmAuthResponseContext { 133 int32_t authType; 134 std::string deviceId; 135 std::string localDeviceId; 136 std::string deviceName; 137 int32_t deviceTypeId; 138 int32_t msgType; 139 int32_t sessionId; 140 bool cryptoSupport; 141 bool isIdenticalAccount; 142 bool isAuthCodeReady; 143 bool isShowDialog; 144 std::string cryptoName; 145 std::string cryptoVer; 146 int32_t reply; 147 std::string networkId; 148 std::string groupId; 149 std::string groupName; 150 std::string hostPkgName; 151 std::string targetPkgName; 152 std::string appOperation; 153 std::string appDesc; 154 std::string customDesc; 155 std::string appIcon; 156 std::string appThumbnail; 157 std::string token; 158 std::string authToken; 159 int32_t pageId; 160 int64_t requestId; 161 int32_t code; 162 int32_t state; 163 std::vector<std::string> syncGroupList; 164 std::string accountGroupIdHash; 165 std::string publicKey; 166 bool isOnline; 167 int32_t bindLevel; 168 bool haveCredential; 169 int32_t confirmOperation; 170 std::string localAccountId; 171 int32_t localUserId; 172 int64_t tokenId; 173 bool authed; 174 std::string dmVersion; 175 std::vector<int32_t> bindType; 176 std::string remoteAccountId; 177 int32_t remoteUserId; 178 std::string targetDeviceName; 179 } DmAuthResponseContext; 180 181 class AuthMessageProcessor; 182 183 class DmAuthManager final : public ISoftbusSessionCallback, 184 public IHiChainConnectorCallback, 185 public IDmDeviceAuthCallback, 186 public std::enable_shared_from_this<DmAuthManager> { 187 public: 188 DmAuthManager(std::shared_ptr<SoftbusConnector> softbusConnector, 189 std::shared_ptr<HiChainConnector> hiChainConnector, 190 std::shared_ptr<IDeviceManagerServiceListener> listener, 191 std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector); 192 ~DmAuthManager(); 193 194 /** 195 * @tc.name: DmAuthManager::AuthenticateDevice 196 * @tc.desc: Authenticate Device of the DeviceManager Authenticate Manager 197 * @tc.type: FUNC 198 */ 199 int32_t AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, 200 const std::string &extra); 201 202 /** 203 * @tc.name: DmAuthManager::UnAuthenticateDevice 204 * @tc.desc: UnAuthenticate Device of the DeviceManager Authenticate Manager 205 * @tc.type: FUNC 206 */ 207 int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &networkId); 208 209 /** 210 * @brief UnBind device. 211 * @param pkgName package name. 212 * @param deviceId device id. 213 * @return Return 0 if success. 214 */ 215 int32_t UnBindDevice(const std::string &pkgName, const std::string &udidHash); 216 217 /** 218 * @tc.name: DmAuthManager::OnSessionOpened 219 * @tc.desc: Opened Session of the DeviceManager Authenticate Manager 220 * @tc.type: FUNC 221 */ 222 void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result); 223 224 /** 225 * @tc.name: DmAuthManager::OnSessionClosed 226 * @tc.desc: Closed Session of the DeviceManager Authenticate Manager 227 * @tc.type: FUNC 228 */ 229 void OnSessionClosed(const int32_t sessionId); 230 231 /** 232 * @tc.name: DmAuthManager::OnDataReceived 233 * @tc.desc: Received Data of the DeviceManager Authenticate Manager 234 * @tc.type: FUNC 235 */ 236 void OnDataReceived(const int32_t sessionId, const std::string message); 237 238 /** 239 * @tc.name: DmAuthManager::OnGroupCreated 240 * @tc.desc: Created Group of the DeviceManager Authenticate Manager 241 * @tc.type: FUNC 242 */ 243 void OnGroupCreated(int64_t requestId, const std::string &groupId); 244 245 /** 246 * @tc.name: DmAuthManager::OnMemberJoin 247 * @tc.desc: Join Member of the DeviceManager Authenticate Manager 248 * @tc.type: FUNC 249 */ 250 void OnMemberJoin(int64_t requestId, int32_t status); 251 252 /** 253 * @tc.name: DmAuthManager::EstablishAuthChannel 254 * @tc.desc: Establish Auth Channel of the DeviceManager Authenticate Manager, auth state machine 255 * @tc.type: FUNC 256 */ 257 int32_t EstablishAuthChannel(const std::string &deviceId); 258 259 /** 260 * @tc.name: DmAuthManager::StartNegotiate 261 * @tc.desc: Start Negotiate of the DeviceManager Authenticate Manager 262 * @tc.type: FUNC 263 */ 264 void StartNegotiate(const int32_t &sessionId); 265 266 /** 267 * @tc.name: DmAuthManager::RespNegotiate 268 * @tc.desc: Resp Negotiate of the DeviceManager Authenticate Manager 269 * @tc.type: FUNC 270 */ 271 void RespNegotiate(const int32_t &sessionId); 272 273 /** 274 * @tc.name: DmAuthManager::SendAuthRequest 275 * @tc.desc: Send Auth Request of the DeviceManager Authenticate Manager 276 * @tc.type: FUNC 277 */ 278 void SendAuthRequest(const int32_t &sessionId); 279 280 /** 281 * @tc.name: DmAuthManager::StartAuthProcess 282 * @tc.desc: Start Auth Process of the DeviceManager Authenticate Manager 283 * @tc.type: FUNC 284 */ 285 int32_t StartAuthProcess(const int32_t &action); 286 287 /** 288 * @tc.name: DmAuthManager::StartRespAuthProcess 289 * @tc.desc: Start Resp Auth Process of the DeviceManager Authenticate Manager 290 * @tc.type: FUNC 291 */ 292 void StartRespAuthProcess(); 293 294 /** 295 * @tc.name: DmAuthManager::CreateGroup 296 * @tc.desc: Create Group of the DeviceManager Authenticate Manager 297 * @tc.type: FUNC 298 */ 299 int32_t CreateGroup(); 300 301 /** 302 * @tc.name: DmAuthManager::ProcessPincode 303 * @tc.desc: Add Member of the DeviceManager Authenticate Manager 304 * @tc.type: FUNC 305 */ 306 int32_t ProcessPincode(int32_t pinCode); 307 308 /** 309 * @tc.name: DmAuthManager::GetConnectAddr 310 * @tc.desc: Get Connect Addr of the DeviceManager Authenticate Manager 311 * @tc.type: FUNC 312 */ 313 std::string GetConnectAddr(std::string deviceId); 314 315 /** 316 * @tc.name: DmAuthManager::JoinNetwork 317 * @tc.desc: Join Net work of the DeviceManager Authenticate Manager 318 * @tc.type: FUNC 319 */ 320 int32_t JoinNetwork(); 321 322 /** 323 * @tc.name: DmAuthManager::AuthenticateFinish 324 * @tc.desc: Finish Authenticate of the DeviceManager Authenticate Manager 325 * @tc.type: FUNC 326 */ 327 void AuthenticateFinish(); 328 329 /** 330 * @tc.name: DmAuthManager::GetIsCryptoSupport 331 * @tc.desc: Get Cryp to Support of the DeviceManager Authenticate Manager 332 * @tc.type: FUNC 333 */ 334 bool GetIsCryptoSupport(); 335 336 /** 337 * @tc.name: DmAuthManager::SetAuthRequestState 338 * @tc.desc: Set Auth Request State of the DeviceManager Authenticate Manager 339 * @tc.type: FUNC 340 */ 341 int32_t SetAuthRequestState(std::shared_ptr<AuthRequestState> authRequestState); 342 343 /** 344 * @tc.name: DmAuthManager::SetAuthResponseState 345 * @tc.desc: Set Auth Response State of the DeviceManager Authenticate Manager 346 * @tc.type: FUNC 347 */ 348 int32_t SetAuthResponseState(std::shared_ptr<AuthResponseState> authResponseState); 349 350 /** 351 * @tc.name: DmAuthManager::GetPinCode 352 * @tc.desc: Get Pin Code of the DeviceManager Authenticate Manager 353 * @tc.type: FUNC 354 */ 355 int32_t GetPinCode(); 356 357 /** 358 * @tc.name: DmAuthManager::GenerateGroupName 359 * @tc.desc: Generate Group Name of the DeviceManager Authenticate Manager 360 * @tc.type: FUNC 361 */ 362 std::string GenerateGroupName(); 363 364 /** 365 * @tc.name: DmAuthManager::HandleAuthenticateTimeout 366 * @tc.desc: Handle Authenticate Timeout of the DeviceManager Authenticate Manager 367 * @tc.type: FUNC 368 */ 369 void HandleAuthenticateTimeout(std::string name); 370 371 /** 372 * @tc.name: DmAuthManager::CancelDisplay 373 * @tc.desc: Cancel Display of the DeviceManager Authenticate Manager 374 * @tc.type: FUNC 375 */ 376 void CancelDisplay(); 377 378 /** 379 * @tc.name: DmAuthManager::GeneratePincode 380 * @tc.desc: Generate Pincode of the DeviceManager Authenticate Manager 381 * @tc.type: FUNC 382 */ 383 int32_t GeneratePincode(); 384 385 /** 386 * @tc.name: DmAuthManager::ShowConfigDialog 387 * @tc.desc: Show Config Dialog of the DeviceManager Authenticate Manager 388 * @tc.type: FUNC 389 */ 390 void ShowConfigDialog(); 391 392 /** 393 * @tc.name: DmAuthManager::ShowAuthInfoDialog 394 * @tc.desc: Show AuthInfo Dialog of the DeviceManager Authenticate Manager 395 * @tc.type: FUNC 396 */ 397 void ShowAuthInfoDialog(); 398 399 /** 400 * @tc.name: DmAuthManager::ShowStartAuthDialog 401 * @tc.desc: Show Start Auth Dialog of the DeviceManager Authenticate Manager 402 * @tc.type: FUNC 403 */ 404 void ShowStartAuthDialog(); 405 406 /** 407 * @tc.name: DmAuthManager::OnUserOperation 408 * @tc.desc: User Operation of the DeviceManager Authenticate Manager 409 * @tc.type: FUNC 410 */ 411 int32_t OnUserOperation(int32_t action, const std::string ¶ms); 412 413 /** 414 * @tc.name: DmAuthManager::UserSwitchEventCallback 415 * @tc.desc: User Switch Event Callback of the DeviceManager Authenticate Manager 416 * @tc.type: FUNC 417 */ 418 void UserSwitchEventCallback(int32_t userId); 419 420 /** 421 * @tc.name: DmAuthManager::SetPageId 422 * @tc.desc: Set PageId of the DeviceManager Authenticate Manager 423 * @tc.type: FUNC 424 */ 425 int32_t SetPageId(int32_t pageId); 426 427 /** 428 * @tc.name: DmAuthManager::SetReasonAndFinish 429 * @tc.desc: Set Reason of the DeviceManager Authenticate Manager 430 * @tc.type: FUNC 431 */ 432 int32_t SetReasonAndFinish(int32_t reason, int32_t state); 433 434 /** 435 * @tc.name: DmAuthManager::IsIdenticalAccount 436 * @tc.desc: judge IdenticalAccount or not 437 * @tc.type: FUNC 438 */ 439 bool IsIdenticalAccount(); 440 441 /** 442 * @tc.name: DmAuthManager::RegisterUiStateCallback 443 * @tc.desc: Register ui state callback 444 * @tc.type: FUNC 445 */ 446 int32_t RegisterUiStateCallback(const std::string pkgName); 447 448 /** 449 * @tc.name: DmAuthManager::UnRegisterUiStateCallback 450 * @tc.desc: Unregister ui state callback 451 * @tc.type: FUNC 452 */ 453 int32_t UnRegisterUiStateCallback(const std::string pkgName); 454 455 /** 456 * @tc.name: DmAuthManager::ImportAuthCode 457 * @tc.desc: Import auth code 458 * @tc.type: FUNC 459 */ 460 int32_t ImportAuthCode(const std::string &pkgName, const std::string &authCode); 461 462 /** 463 * @tc.name: DmAuthManager::BindTarget 464 * @tc.desc: Bind Target 465 * @tc.type: FUNC 466 */ 467 int32_t BindTarget(const std::string &pkgName, const PeerTargetId &targetId, 468 const std::map<std::string, std::string> &bindParam); 469 470 void HandleSessionHeartbeat(std::string name); 471 private: 472 int32_t CheckAuthParamVaild(const std::string &pkgName, int32_t authType, const std::string &deviceId, 473 const std::string &extra); 474 void ProcessSourceMsg(); 475 void ProcessSinkMsg(); 476 std::string GetAccountGroupIdHash(); 477 void AbilityNegotiate(); 478 void HandleMemberJoinImportAuthCode(const int64_t requestId, const int32_t status); 479 int32_t DeleteAuthCode(); 480 int32_t GetAuthCode(const std::string &pkgName, int32_t &pinCode); 481 bool IsAuthTypeSupported(const int32_t &authType); 482 bool IsAuthCodeReady(const std::string &pkgName); 483 int32_t ParseConnectAddr(const PeerTargetId &targetId, std::string &deviceId, std::string &addrType); 484 int32_t ParseAuthType(const std::map<std::string, std::string> &bindParam, int32_t &authType); 485 std::string ParseExtraFromMap(const std::map<std::string, std::string> &bindParam); 486 void CompatiblePutAcl(); 487 void ProRespNegotiateExt(const int32_t &sessionId); 488 void ProRespNegotiate(const int32_t &sessionId); 489 void AccountIdLogoutEventCallback(int32_t userId); 490 void UserChangeEventCallback(int32_t userId); 491 std::string GenerateBindResultContent(); 492 void GetAuthRequestContext(); 493 void SinkAuthDeviceFinish(); 494 void SrcAuthDeviceFinish(); 495 void SrcSyncDeleteAclDone(); 496 void SinkSyncDeleteAclDone(); 497 498 public: 499 void RequestCredential(); 500 void GenerateCredential(std::string &publicKey); 501 void RequestCredentialDone(); 502 void RequestSyncDeleteAcl(); 503 void ResponseCredential(); 504 void ResponseSyncDeleteAcl(); 505 bool AuthDeviceTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen); 506 void AuthDeviceFinish(int64_t requestId); 507 void AuthDeviceError(int64_t requestId, int32_t errorCode); 508 void GetRemoteDeviceId(std::string &deviceId); 509 int32_t EstablishUnbindChannel(const std::string &deviceIdHash); 510 void SyncDeleteAclDone(); 511 void AuthDeviceSessionKey(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen); 512 void CommonEventCallback(int32_t userId); 513 void OnAuthDeviceDataReceived(const int32_t sessionId, const std::string message); 514 void OnUnbindSessionOpened(int sessionId, int32_t sessionSide, int result); 515 private: 516 int32_t ImportCredential(std::string &deviceId, std::string &publicKey); 517 void GetAuthParam(const std::string &pkgName, int32_t authType, const std::string &deviceId, 518 const std::string &extra); 519 void HandleSyncDeleteTimeout(std::string name); 520 int32_t DeleteAcl(const std::string &pkgName, const std::string &deviceId); 521 void ProcessAuthRequestExt(const int32_t &sessionId); 522 void ProcessAuthRequest(const int32_t &sessionId); 523 int32_t ConfirmProcess(const int32_t &action); 524 int32_t ConfirmProcessExt(const int32_t &action); 525 int32_t AddMember(int32_t pinCode); 526 int32_t AuthDevice(int32_t pinCode); 527 void SyncDeleteAcl(const std::string &pkgName, const std::string &deviceId); 528 int32_t DeleteGroup(const std::string &pkgName, const std::string &deviceId); 529 void PutAccessControlList(); 530 void InitAuthState(const std::string &pkgName, int32_t authType, const std::string &deviceId, 531 const std::string &extra); 532 void SinkAuthenticateFinish(); 533 void SrcAuthenticateFinish(); 534 static void OpenAuthSession(); 535 private: 536 static std::shared_ptr<AuthRequestState> sessionState_; 537 std::shared_ptr<SoftbusConnector> softbusConnector_; 538 std::shared_ptr<HiChainConnector> hiChainConnector_; 539 std::shared_ptr<AuthUiStateManager> authUiStateMgr_; 540 std::shared_ptr<IDeviceManagerServiceListener> listener_; 541 std::shared_ptr<DmAdapterManager> adapterMgr_; 542 std::map<int32_t, std::shared_ptr<IAuthentication>> authenticationMap_; 543 std::shared_ptr<AuthRequestState> authRequestState_ = nullptr; 544 std::shared_ptr<AuthResponseState> authResponseState_ = nullptr; 545 std::shared_ptr<DmAuthRequestContext> authRequestContext_; 546 std::shared_ptr<DmAuthResponseContext> authResponseContext_; 547 std::shared_ptr<AuthMessageProcessor> authMessageProcessor_; 548 std::shared_ptr<DmTimer> timer_; 549 std::shared_ptr<DmAbilityManager> dmAbilityMgr_; 550 std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector_; 551 bool isCryptoSupport_ = false; 552 bool isFinishOfLocal_ = true; 553 int32_t authTimes_ = 0; 554 int32_t action_ = USER_OPERATION_TYPE_CANCEL_AUTH; 555 std::shared_ptr<IAuthentication> authPtr_; 556 bool isAddingMember_ = false; 557 std::string importPkgName_ = ""; 558 std::string importAuthCode_ = ""; 559 PeerTargetId peerTargetId_; 560 unsigned char *sessionKey_ = nullptr; 561 uint32_t sessionKeyLen_ = 0; 562 std::string remoteDeviceId_ = ""; 563 std::string dmVersion_ = ""; 564 bool isAuthDevice_ = false; 565 bool isAuthenticateDevice_ = false; 566 }; 567 } // namespace DistributedHardware 568 } // namespace OHOS 569 #endif // OHOS_DM_AUTH_MANAGER_H 570