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 #ifndef IPC_COMMON_H 17 #define IPC_COMMON_H 18 19 #include <cinttypes> 20 #include <iremote_stub.h> 21 #include <optional> 22 #include <string> 23 24 #include "iam_common_defines.h" 25 #include "nocopyable.h" 26 27 namespace OHOS { 28 namespace UserIam { 29 namespace UserAuth { 30 enum Permission { 31 MANAGE_USER_IDM_PERMISSION, 32 USE_USER_IDM_PERMISSION, 33 ACCESS_USER_AUTH_INTERNAL_PERMISSION, 34 ACCESS_BIOMETRIC_PERMISSION, 35 ACCESS_AUTH_RESPOOL, 36 ENFORCE_USER_IDM, 37 SUPPORT_USER_AUTH, 38 IS_SYSTEM_APP, 39 CLEAR_REDUNDANCY_PERMISSION, 40 }; 41 42 class IpcCommon final : public NoCopyable { 43 public: 44 using Recipient = std::function<void()>; 45 static int32_t GetCallingUserId(IPCObjectStub &stub, int32_t &userId); 46 static int32_t GetActiveUserId(std::optional<int32_t> &userId); 47 static int32_t GetAllUserId(std::vector<int32_t> &userIds); 48 static int32_t GetUserTypeByUserId(int32_t userId, int32_t &userType); 49 static bool CheckPermission(IPCObjectStub &stub, Permission permission); 50 static uint32_t GetAccessTokenId(IPCObjectStub &stub); 51 static uint32_t GetTokenId(IPCObjectStub &stub); 52 static bool GetCallerName(IPCObjectStub &stub, std::string &callerName, int32_t &callerType); 53 static bool GetCallingAppID(IPCObjectStub &stub, std::string &callingAppID); 54 static bool CheckForegroundApplication(const std::string &bundleName); 55 static bool IsOsAccountVerified(int32_t userId); 56 class PeerDeathRecipient final : public IPCObjectProxy::DeathRecipient { 57 public: PeerDeathRecipient(Recipient && recipient)58 explicit PeerDeathRecipient(Recipient &&recipient) : recipient_(std::forward<Recipient>(recipient)) 59 { 60 } 61 ~PeerDeathRecipient() override = default; OnRemoteDied(const wptr<IRemoteObject> & object)62 void OnRemoteDied(const wptr<IRemoteObject> &object) override 63 { 64 if (auto remote = object.promote(); !remote) { 65 return; 66 } 67 if (recipient_) { 68 recipient_(); 69 } 70 }; 71 72 private: 73 Recipient recipient_; 74 }; 75 76 private: 77 static bool CheckNativeCallingProcessWhiteList(IPCObjectStub &stub, Permission permission); 78 static bool CheckDirectCallerAndFirstCallerIfSet(IPCObjectStub &stub, const std::string &permission); 79 static bool CheckDirectCaller(IPCObjectStub &stub, const std::string &permission); 80 static bool CheckCallerIsSystemApp(IPCObjectStub &stub); 81 static std::vector<std::pair<int32_t, std::string>> GetWhiteLists(Permission permission); 82 }; 83 } // namespace UserAuth 84 } // namespace UserIam 85 } // namespace OHOS 86 #endif // IPC_COMMON_H