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