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 "nocopyable.h" 25 26 namespace OHOS { 27 namespace UserIam { 28 namespace UserAuth { 29 enum Permission { 30 MANAGE_USER_IDM_PERMISSION, 31 USE_USER_IDM_PERMISSION, 32 ACCESS_USER_AUTH_INTERNAL_PERMISSION, 33 ACCESS_BIOMETRIC_PERMISSION, 34 ACCESS_AUTH_RESPOOL, 35 ENFORCE_USER_IDM, 36 SUPPORT_USER_AUTH, 37 IS_SYSTEM_APP, 38 }; 39 40 class IpcCommon final : public NoCopyable { 41 public: 42 using Recipient = std::function<void()>; 43 static int32_t GetCallingUserId(IPCObjectStub &stub, int32_t &userId); 44 static int32_t GetActiveUserId(std::optional<int32_t> &userId); 45 static bool CheckPermission(IPCObjectStub &stub, Permission permission); 46 static uint32_t GetAccessTokenId(IPCObjectStub &stub); 47 static uint32_t GetTokenId(IPCObjectStub &stub); 48 class PeerDeathRecipient final : public IPCObjectProxy::DeathRecipient { 49 public: PeerDeathRecipient(Recipient && recipient)50 explicit PeerDeathRecipient(Recipient &&recipient) : recipient_(std::forward<Recipient>(recipient)) 51 { 52 } 53 ~PeerDeathRecipient() override = default; OnRemoteDied(const wptr<IRemoteObject> & object)54 void OnRemoteDied(const wptr<IRemoteObject> &object) override 55 { 56 if (auto remote = object.promote(); !remote) { 57 return; 58 } 59 if (recipient_) { 60 recipient_(); 61 } 62 }; 63 64 private: 65 Recipient recipient_; 66 }; 67 68 private: 69 static bool CheckNativeCallingProcessWhiteList(IPCObjectStub &stub); 70 static bool CheckDirectCallerAndFirstCallerIfSet(IPCObjectStub &stub, const std::string &permission); 71 static bool CheckDirectCaller(IPCObjectStub &stub, const std::string &permission); 72 static bool CheckCallerIsSystemApp(IPCObjectStub &stub); 73 }; 74 } // namespace UserAuth 75 } // namespace UserIam 76 } // namespace OHOS 77 #endif // IPC_COMMON_H