1 /*
2  * Copyright (c) 2021-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 ACCESSTOKEN_NATIVE_TOKEN_INFO_INNER_H
17 #define ACCESSTOKEN_NATIVE_TOKEN_INFO_INNER_H
18 
19 #include <string>
20 #include <vector>
21 #include "access_token.h"
22 #include "generic_values.h"
23 #include "native_token_info.h"
24 #include "permission_policy_set.h"
25 #include "permission_state_full.h"
26 
27 namespace OHOS {
28 namespace Security {
29 namespace AccessToken {
30 static const int MAX_DCAPS_NUM = 10 * 1024;
31 static const int MAX_REQ_PERM_NUM = 10 * 1024;
32 
33 struct TokenInfo {
34     AccessTokenID id;
35     std::string processName;
36     int apl;
37 };
38 
39 class NativeTokenInfoInner final {
40 public:
41     NativeTokenInfoInner();
42     NativeTokenInfoInner(NativeTokenInfo& info,
43         const std::vector<PermissionStateFull>& permStateList);
44     virtual ~NativeTokenInfoInner();
45 
46     int Init(const TokenInfo& tokenInfo, const std::vector<std::string>& dcap,
47         const std::vector<std::string>& nativeAcls,
48         const std::vector<PermissionStateFull>& permStateList);
49     void StoreNativeInfo(std::vector<GenericValues>& valueList) const;
50     void StorePermissionPolicy(std::vector<GenericValues>& permStateValues) const;
51     void TranslateToNativeTokenInfo(NativeTokenInfo& infoParcel) const;
52     void SetDcaps(const std::string& dcapStr);
53     void SetNativeAcls(const std::string& AclsStr);
54     void ToString(std::string& info) const;
55     int RestoreNativeTokenInfo(AccessTokenID tokenId, const GenericValues& inGenericValues,
56         const std::vector<GenericValues>& permStateRes);
57     void Update(AccessTokenID tokenId, const std::string& processName,
58         int apl, const std::vector<std::string>& dcap,
59         const std::vector<std::string>& nativeAcls);
60 
61     std::vector<std::string> GetDcap() const;
62     std::vector<std::string> GetNativeAcls() const;
63     AccessTokenID GetTokenID() const;
64     std::string GetProcessName() const;
65     NativeTokenInfo GetNativeTokenInfo() const;
66     std::shared_ptr<PermissionPolicySet> GetNativeInfoPermissionPolicySet() const;
67     uint32_t GetReqPermissionSize() const;
68     bool IsRemote() const;
69     void SetRemote(bool isRemote);
70 
71 private:
72     int TranslationIntoGenericValues(GenericValues& outGenericValues) const;
73     std::string DcapToString(const std::vector<std::string>& dcap) const;
74     std::string NativeAclsToString(const std::vector<std::string>& nativeAcls) const;
75 
76     // true means sync from remote.
77     bool isRemote_;
78     NativeTokenInfo tokenInfoBasic_;
79     std::shared_ptr<PermissionPolicySet> permPolicySet_;
80 };
81 } // namespace AccessToken
82 } // namespace Security
83 } // namespace OHOS
84 #endif // ACCESSTOKEN_NATIVE_TOKEN_INFO_INNER_H
85