1 /*
2  * Copyright (c) 2021-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 "form_util.h"
17 
18 #include <set>
19 #include <thread>
20 #include <chrono>
21 #include <cinttypes>
22 #include <regex>
23 
24 #include "accesstoken_kit.h"
25 #include "bundle_constants.h"
26 #include "fms_log_wrapper.h"
27 #include "form_constants.h"
28 #include "ipc_skeleton.h"
29 #include "os_account_manager_wrapper.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 namespace {
34 constexpr int64_t SEC_TO_NANOSEC = 1000000000;
35 constexpr int64_t SEC_TO_MILLISEC = 1000;
36 constexpr int64_t MILLISEC_TO_NANOSEC = 1000000;
37 constexpr int64_t INVALID_UDID_HASH = 0;
38 constexpr int32_t SYSTEM_UID = 1000;
39 constexpr int INT_64_LENGTH = 19;
40 constexpr int ZERO_VALUE = 0;
41 constexpr int BASE_NUMBER = 9;
42 constexpr int64_t HEAD_BIT_NUM = 9000000000000000000;
43 } // namespace
44 
45 using namespace std;
46 using namespace std::chrono;
47 
48 static std::set<uint64_t> s_memFormIds;
49 static std::mutex s_memFormIdsMutex;
50 
51 /**
52  * @brief create form id for form.
53  * @param udidHash udid hash
54  * @return new form id.
55  */
GenerateFormId(int64_t udidHash)56 int64_t FormUtil::GenerateFormId(int64_t udidHash)
57 {
58     set<uint64_t>::iterator it;
59     const int retryTimes = 5;
60     const int delayMs = 1;
61     int64_t elapsedTime = 0;
62     size_t elapsedHash = 0;
63     uint64_t unsignedUdidHash = 0;
64     uint64_t formId = 0;
65     int64_t ret = -1;
66     struct timespec t;
67     t.tv_sec = 0;
68     t.tv_nsec = 0;
69     for (int i = 0; i < retryTimes; i++) {
70         clock_gettime(CLOCK_REALTIME, &t);
71         elapsedTime = (t.tv_sec) * SEC_TO_NANOSEC + t.tv_nsec;
72         elapsedHash = std::hash<std::string>()(std::to_string(elapsedTime));
73         unsignedUdidHash = static_cast<uint64_t>(udidHash);
74         formId = unsignedUdidHash | (uint32_t)(elapsedHash & 0x000000007fffffffL);
75         ret = static_cast<int64_t>(formId);
76         std::lock_guard<std::mutex> lock(s_memFormIdsMutex);
77         it = s_memFormIds.find(formId);
78         if (it != s_memFormIds.end()) {
79             HILOG_INFO("repeated formId:%{public}" PRId64, ret);
80             std::this_thread::sleep_for(std::chrono::milliseconds(delayMs));
81         } else {
82             s_memFormIds.insert(formId);
83             break;
84         }
85     }
86     HILOG_INFO("generate elapsed hash %{public}zu, formId:%{public}" PRId64, elapsedHash, ret);
87     return ret;
88 }
89 
90 /**
91 @brief delete form id for form.
92 @param formId The id of the form.
93 */
DeleteFormId(int64_t formId)94 void FormUtil::DeleteFormId(int64_t formId)
95 {
96     std::lock_guard<std::mutex> lock(s_memFormIdsMutex);
97     s_memFormIds.erase(static_cast<uint64_t>(formId));
98 }
99 
100 /**
101  * @brief padding form id.
102  * @param formId The id of the form.
103  * @param udidHash udid hash
104  * @return new form id.
105  */
PaddingUdidHash(uint64_t formId,uint64_t udidHash)106 int64_t FormUtil::PaddingUdidHash(uint64_t formId, uint64_t udidHash)
107 {
108     // Compatible with int form id.
109     if ((formId & 0xffffffff00000000L) == 0) {
110         return udidHash | formId;
111     }
112 
113     return formId;
114 }
115 /**
116  * @brief create udid hash.
117  * @param udidHash udid hash.
118  * @return Returns true on success, false on failure.
119  */
GenerateUdidHash(int64_t & udidHash)120 bool FormUtil::GenerateUdidHash(int64_t &udidHash)
121 {
122     HILOG_INFO("udidHash:%{private}s", std::to_string(udidHash).c_str());
123     if (udidHash != INVALID_UDID_HASH) {
124         return true;
125     }
126 
127     u_int64_t hashId = 0L;
128     const int32_t thirtyTwo = 32;
129     udidHash = (hashId & 0x0000000000ffffffL) << thirtyTwo;
130     if (udidHash < 0) {
131         udidHash = 0L;
132     }
133     HILOG_INFO("generate hash %{private}s", std::to_string(udidHash).c_str());
134     return true;
135 }
136 /**
137  * @brief Get current system nanosecond.
138  * @return Current system nanosecond.
139  */
GetCurrentNanosecond()140 int64_t FormUtil::GetCurrentNanosecond()
141 {
142     struct timespec ts;
143     ts.tv_sec = 0;
144     ts.tv_nsec = 0;
145     clock_gettime(CLOCK_MONOTONIC, &ts);
146     return (ts.tv_sec * SEC_TO_NANOSEC + ts.tv_nsec);
147 }
148 /**
149  * @brief Get current system millisecond.
150  * @return Current system millisecond.
151  */
GetCurrentMillisecond()152 int64_t FormUtil::GetCurrentMillisecond()
153 {
154     struct timespec ts;
155     clock_gettime(CLOCK_REALTIME, &ts);
156     return (ts.tv_sec * SEC_TO_MILLISEC + ts.tv_nsec / MILLISEC_TO_NANOSEC);
157 }
158 
GetCurrentSteadyClockMillseconds()159 int64_t FormUtil::GetCurrentSteadyClockMillseconds()
160 {
161     return std::chrono::duration_cast<std::chrono::milliseconds>(
162         std::chrono::steady_clock::now().time_since_epoch()).count();
163 }
164 
GetNowMillisecond()165 int64_t FormUtil::GetNowMillisecond()
166 {
167     system_clock::time_point pointTime = system_clock::now();
168     auto timeMilliseconds = chrono::duration_cast<chrono::milliseconds>(pointTime.time_since_epoch());
169     return timeMilliseconds.count();
170 }
171 
172 /**
173  * @brief Get millisecond from tm.
174  * @param tmAtTime tm time.
175  * @return Millisecond.
176  */
GetMillisecondFromTm(struct tm & tmAtTime)177 int64_t FormUtil::GetMillisecondFromTm(struct tm &tmAtTime)
178 {
179     time_t inputTime = mktime(&tmAtTime);
180     if (inputTime == -1) {
181         HILOG_ERROR("mktime failed");
182         return -1;
183     }
184     system_clock::time_point pointTime = system_clock::from_time_t(inputTime);
185     auto timeMilliseconds = chrono::duration_cast<chrono::milliseconds>(pointTime.time_since_epoch());
186     return timeMilliseconds.count();
187 }
188 
189 /**
190 * @brief split string.
191  * @param in string.
192  * @param delim delimiter.
193  * @return string list.
194  */
StringSplit(const std::string & in,const std::string & delim)195 std::vector<std::string> FormUtil::StringSplit(const std::string &in, const std::string &delim)
196 {
197     std::regex reg { delim };
198     return std::vector<std::string> {
199         std::sregex_token_iterator(in.begin(), in.end(), reg, -1),
200         std::sregex_token_iterator()
201     };
202 }
203 
204 /**
205  * @brief get current active account id.
206  * @return int current active account id.
207  */
GetCurrentAccountId()208 int FormUtil::GetCurrentAccountId()
209 {
210     std::vector<int32_t> activeList;
211     ErrCode errCode = DelayedSingleton<OsAccountManagerWrapper>::GetInstance()->QueryActiveOsAccountIds(activeList);
212     if (errCode != ERR_OK) {
213         HILOG_ERROR("QueryActiveOsAccountIds failed");
214         return Constants::ANY_USERID;
215     }
216     if (activeList.empty()) {
217         HILOG_ERROR("empty QueryActiveOsAccountIds,no accounts");
218         return Constants::ANY_USERID;
219     }
220 
221     return activeList.front();
222 }
223 
IsSACall()224 bool FormUtil::IsSACall()
225 {
226     auto callerToken = IPCSkeleton::GetCallingTokenID();
227     HILOG_DEBUG("callerToken : %{private}u", callerToken);
228 
229     auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
230     if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
231         HILOG_DEBUG("caller tokenType is native, verify success");
232         return true;
233     }
234 
235     if (IPCSkeleton::GetCallingUid() == SYSTEM_UID) {
236         HILOG_DEBUG("callingUid is native,verify success");
237         return true;
238     }
239 
240     HILOG_DEBUG("call");
241     return false;
242 }
243 
VerifyCallingPermission(const std::string & permissionName)244 bool FormUtil::VerifyCallingPermission(const std::string &permissionName)
245 {
246     HILOG_DEBUG("call.permission name is:%{public}s", permissionName.c_str());
247     auto callerToken = IPCSkeleton::GetCallingTokenID();
248     int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
249     if (ret == Security::AccessToken::PermissionState::PERMISSION_DENIED) {
250         HILOG_ERROR("permission %{public}s: PERMISSION_DENIED", permissionName.c_str());
251         return false;
252     }
253     HILOG_DEBUG("Verify calling permission success");
254     return true;
255 }
256 
ConvertStringToInt64(const std::string & strInfo,int64_t & int64Value)257 bool FormUtil::ConvertStringToInt64(const std::string &strInfo, int64_t &int64Value)
258 {
259     size_t strLength = strInfo.size();
260     if (strLength == ZERO_VALUE) {
261         int64Value = ZERO_VALUE;
262         return true;
263     }
264     std::regex pattern("^0|-?[1-9][0-9]{0,18}$"); // "^-?[0-9]{1,19}$"
265     std::smatch match;
266     if (regex_match(strInfo, match, pattern)) {
267         HILOG_DEBUG("regex_match successed");
268         if (strInfo.substr(ZERO_VALUE, ZERO_VALUE + 1) != "-") { // maximum: 9223372036854775807
269             if (strLength < INT_64_LENGTH) {
270                 int64Value = std::stoll(strInfo);
271                 return true;
272             }
273             int maxSubValue = std::stoi(strInfo.substr(ZERO_VALUE, ZERO_VALUE + 1));
274             if (strLength == INT_64_LENGTH && maxSubValue < BASE_NUMBER) {
275                 int64Value = std::stoll(strInfo);
276                 return true;
277             }
278             // Means 0x7FFFFFFFFFFFFFFF remove the first number:(2^63 - 1 - 9 * 10 ^ 19)
279             int64_t subValue = std::stoll(strInfo.substr(ZERO_VALUE + 1, INT_64_LENGTH - 1));
280             if (strLength == INT_64_LENGTH && subValue <= (INT64_MAX - HEAD_BIT_NUM)) {
281                 int64Value = std::stoll(strInfo);
282                 return true;
283             }
284             return false;
285         }
286         if (strLength < INT_64_LENGTH + 1) { // The minimum value: -9223372036854775808
287             int64Value = std::stoll(strInfo);
288             return true;
289         }
290         if (strLength == INT_64_LENGTH + 1) {
291             int minSubValue = std::stoi(strInfo.substr(1, 1));
292             if (minSubValue < BASE_NUMBER) {
293                 int64Value = std::stoll(strInfo);
294                 return true;
295             }
296 
297             // Means 0x8000000000000000 remove the first number:-(2^63 - 9 * 10 ^ 19)
298             int64_t subValue = std::stoll(strInfo.substr(ZERO_VALUE + 2, INT_64_LENGTH - 1));
299             if (subValue <= (INT64_MAX - HEAD_BIT_NUM + 1)) {
300                 int64Value = std::stoll(strInfo);
301                 return true;
302             }
303         }
304     }
305     HILOG_DEBUG("regex_match failed");
306     return false;
307 }
308 } // namespace AppExecFwk
309 } // namespace OHOS