1 /*
2  * Copyright (c) 2023 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 #include "fusion_security.h"
17 
18 #include <cstdlib>
19 #include <cstring>
20 #include <iostream>
21 
22 #include "accesstoken_kit.h"
23 #include "nativetoken_kit.h"
24 #include "nocopyable.h"
25 #include "softbus_bus_center.h"
26 #include "token_setproc.h"
27 
28 #include "devicestatus_define.h"
29 
30 #undef LOG_TAG
31 #define LOG_TAG "FusionSecurity"
32 
33 using namespace OHOS;
34 using namespace OHOS::Security::AccessToken;
35 
SetAceessTokenPermission(const std::string &processName, const char** perms, size_t permCount)36 static void SetAceessTokenPermission(const std::string &processName, const char** perms, size_t permCount)
37 {
38     if (perms == nullptr || permCount == 0) {
39         FI_HILOGE("perms is nullptr or permCount is 0");
40         return;
41     }
42     uint64_t tokenId;
43     NativeTokenInfoParams infoInstance = {
44         .dcapsNum = 0,
45         .permsNum = permCount,
46         .aclsNum = 0,
47         .dcaps = nullptr,
48         .perms = perms,
49         .acls = nullptr,
50         .processName = processName.c_str(),
51         .aplStr = "system_basic",
52     };
53     tokenId = GetAccessTokenId(&infoInstance);
54     SetSelfTokenID(tokenId);
55     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
56 }
57 
GetAccessToken()58 void GetAccessToken()
59 {
60     const char* perms[] {
61         "ohos.permission.CAPTURE_SCREEN",
62         "ohos.permission.DISTRIBUTED_DATASYNC",
63     };
64     size_t permCount = 2;
65     SetAceessTokenPermission("fusion_device_profile_rust", perms, permCount);
66 }
67 
68 struct CString : public CIString {
69     std::string str;
70 
71     explicit CString(const char* s);
72     DISALLOW_MOVE(CString);
73     CString(const CString &other);
74     ~CString() = default;
75     CString& operator=(const CString &other) = delete;
76 
77     static CIString* Clone(CIString* target);
78     static void Destruct(CIString* target);
79     static const char* GetData(CIString* target);
80 };
81 
CString(const char* s)82 CString::CString(const char* s)
83     : str(s != nullptr ? s : std::string())
84 {
85     clone = &CString::Clone;
86     destruct = &CString::Destruct;
87     data = &CString::GetData;
88 }
89 
CString(const CString &other)90 CString::CString(const CString &other)
91     : str(other.str)
92 {
93     clone = &CString::Clone;
94     destruct = &CString::Destruct;
95     data = &CString::GetData;
96 }
97 
Clone(CIString* target)98 CIString* CString::Clone(CIString* target)
99 {
100     CString* t = static_cast<CString*>(target);
101     CHKPP(t);
102     return new (std::nothrow) CString(*t);
103 }
104 
Destruct(CIString* target)105 void CString::Destruct(CIString* target)
106 {
107     CString* t = static_cast<CString*>(target);
108     CHKPV(t);
109     delete t;
110 }
111 
GetData(CIString* target)112 const char* CString::GetData(CIString* target)
113 {
114     CString* t = static_cast<CString*>(target);
115     CHKPP(t);
116     return t->str.c_str();
117 }
118 
GetLocalNetworkId()119 CIString* GetLocalNetworkId()
120 {
121     CALL_DEBUG_ENTER;
122     NodeBasicInfo node;
123     int32_t ret = GetLocalNodeDeviceInfo(FI_PKG_NAME, &node);
124     if (ret != RET_OK) {
125         FI_HILOGE("GetLocalNodeDeviceInfo ret:%{public}d", ret);
126         return nullptr;
127     }
128     return new (std::nothrow) CString(node.networkId);
129 }
130