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