1 /*
2 * Copyright (c) 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 #include "constant_common.h"
17 #include <string>
18
19 #include "access_token.h"
20 #include "parameter.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 constexpr const char* REPLACE_TARGET = "****";
27 constexpr const char* REPLACE_TARGET_LESS_THAN_MINLEN = "*******";
28 } // namespace
EncryptDevId(std::string deviceId)29 std::string ConstantCommon::EncryptDevId(std::string deviceId)
30 {
31 std::string result = deviceId;
32 if (deviceId.empty()) {
33 return result;
34 }
35 if (deviceId.size() > MINDEVICEIDLEN) {
36 result.replace(ENCRYPTBEGIN + ENCRYPTLEN, deviceId.size() - MINDEVICEIDLEN, REPLACE_TARGET);
37 } else {
38 result.replace(ENCRYPTBEGIN + 1, deviceId.size()-1, REPLACE_TARGET_LESS_THAN_MINLEN);
39 }
40 return result;
41 }
42
GetLocalDeviceId()43 std::string ConstantCommon::GetLocalDeviceId()
44 {
45 static std::string localDeviceId;
46 if (!localDeviceId.empty()) {
47 return localDeviceId;
48 }
49 const int32_t DEVICE_UUID_LENGTH = 65;
50 char udid[DEVICE_UUID_LENGTH] = {0};
51 if (GetDevUdid(udid, DEVICE_UUID_LENGTH) == 0) {
52 localDeviceId = udid;
53 }
54 return localDeviceId;
55 }
56
IsPermOperatedByUser(int32_t flag)57 bool ConstantCommon::IsPermOperatedByUser(int32_t flag)
58 {
59 uint32_t uFlag = static_cast<uint32_t>(flag);
60 return (uFlag & PERMISSION_USER_FIXED) || (uFlag & PERMISSION_USER_SET);
61 }
62
IsPermOperatedBySystem(int32_t flag)63 bool ConstantCommon::IsPermOperatedBySystem(int32_t flag)
64 {
65 uint32_t uFlag = static_cast<uint32_t>(flag);
66 return (uFlag & PERMISSION_SYSTEM_FIXED) || (uFlag & PERMISSION_PRE_AUTHORIZED_CANCELABLE);
67 }
68
IsPermGrantedBySecComp(int32_t flag)69 bool ConstantCommon::IsPermGrantedBySecComp(int32_t flag)
70 {
71 uint32_t uFlag = static_cast<uint32_t>(flag);
72 return uFlag & PERMISSION_COMPONENT_SET;
73 }
74
GetFlagWithoutSpecifiedElement(uint32_t fullFlag,uint32_t removedFlag)75 uint32_t ConstantCommon::GetFlagWithoutSpecifiedElement(uint32_t fullFlag, uint32_t removedFlag)
76 {
77 uint32_t unmaskedFlag = (fullFlag) & (~removedFlag);
78 return unmaskedFlag;
79 }
80 } // namespace AccessToken
81 } // namespace Security
82 } // namespace OHOS