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 enum UserAuthCallerType : int32_t {
43     TOKEN_INVALID = -1,
44     TOKEN_HAP = 0,
45     TOKEN_NATIVE,
46 };
47 
48 class IpcCommon final : public NoCopyable {
49 public:
50     using Recipient = std::function<void()>;
51     static int32_t GetCallingUserId(IPCObjectStub &stub, int32_t &userId);
52     static int32_t GetActiveUserId(std::optional<int32_t> &userId);
53     static int32_t GetAllUserId(std::vector<int32_t> &userIds);
54     static int32_t GetUserTypeByUserId(int32_t userId, int32_t &userType);
55     static bool CheckPermission(IPCObjectStub &stub, Permission permission);
56     static uint32_t GetAccessTokenId(IPCObjectStub &stub);
57     static uint32_t GetTokenId(IPCObjectStub &stub);
58     static bool GetCallerName(IPCObjectStub &stub, std::string &callerName, int32_t &callerType);
59     static bool GetCallingAppID(IPCObjectStub &stub, std::string &callingAppID);
60     static bool CheckForegroundApplication(const std::string &bundleName);
61     static bool IsOsAccountVerified(int32_t userId);
62     class PeerDeathRecipient final : public IPCObjectProxy::DeathRecipient {
63     public:
PeerDeathRecipient(Recipient && recipient)64         explicit PeerDeathRecipient(Recipient &&recipient) : recipient_(std::forward<Recipient>(recipient))
65         {
66         }
67         ~PeerDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & object)68         void OnRemoteDied(const wptr<IRemoteObject> &object) override
69         {
70             if (auto remote = object.promote(); !remote) {
71                 return;
72             }
73             if (recipient_) {
74                 recipient_();
75             }
76         };
77 
78     private:
79         Recipient recipient_;
80     };
81 
82 private:
83     static bool CheckNativeCallingProcessWhiteList(IPCObjectStub &stub, Permission permission);
84     static bool CheckDirectCallerAndFirstCallerIfSet(IPCObjectStub &stub, const std::string &permission);
85     static bool CheckDirectCaller(IPCObjectStub &stub, const std::string &permission);
86     static bool CheckCallerIsSystemApp(IPCObjectStub &stub);
87     static std::vector<std::pair<int32_t, std::string>> GetWhiteLists(Permission permission);
88 };
89 } // namespace UserAuth
90 } // namespace UserIam
91 } // namespace OHOS
92 #endif // IPC_COMMON_H