1 /* 2 * Copyright (c) 2021-2024 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 "data_validator.h" 17 18 #include "access_token.h" 19 #include "permission_used_request.h" 20 #include "permission_used_type.h" 21 #include "privacy_param.h" 22 23 namespace OHOS { 24 namespace Security { 25 namespace AccessToken { IsBundleNameValid(const std::string & bundleName)26bool DataValidator::IsBundleNameValid(const std::string& bundleName) 27 { 28 return !bundleName.empty() && (bundleName.length() <= MAX_LENGTH); 29 } 30 IsLabelValid(const std::string & label)31bool DataValidator::IsLabelValid(const std::string& label) 32 { 33 return label.length() <= MAX_LENGTH; 34 } 35 IsDescValid(const std::string & desc)36bool DataValidator::IsDescValid(const std::string& desc) 37 { 38 return desc.length() <= MAX_LENGTH; 39 } 40 IsPermissionNameValid(const std::string & permissionName)41bool DataValidator::IsPermissionNameValid(const std::string& permissionName) 42 { 43 return !permissionName.empty() && (permissionName.length() <= MAX_LENGTH); 44 } 45 IsUserIdValid(const int userId)46bool DataValidator::IsUserIdValid(const int userId) 47 { 48 return userId >= 0; 49 } 50 IsToggleStatusValid(const uint32_t status)51bool DataValidator::IsToggleStatusValid(const uint32_t status) 52 { 53 return ((status == PermissionRequestToggleStatus::CLOSED) || 54 (status == PermissionRequestToggleStatus::OPEN)); 55 } 56 IsAppIDDescValid(const std::string & appIDDesc)57bool DataValidator::IsAppIDDescValid(const std::string& appIDDesc) 58 { 59 return !appIDDesc.empty() && (appIDDesc.length() <= MAX_APPIDDESC_LENGTH); 60 } 61 IsDomainValid(const std::string & domain)62bool DataValidator::IsDomainValid(const std::string& domain) 63 { 64 return !domain.empty() && (domain.length() <= MAX_LENGTH); 65 } 66 IsAplNumValid(const int apl)67bool DataValidator::IsAplNumValid(const int apl) 68 { 69 return (apl == APL_NORMAL || apl == APL_SYSTEM_BASIC || apl == APL_SYSTEM_CORE); 70 } 71 IsAvailableTypeValid(const int availableType)72bool DataValidator::IsAvailableTypeValid(const int availableType) 73 { 74 return (availableType == NORMAL || availableType == MDM); 75 } 76 IsProcessNameValid(const std::string & processName)77bool DataValidator::IsProcessNameValid(const std::string& processName) 78 { 79 return !processName.empty() && (processName.length() <= MAX_LENGTH); 80 } 81 IsDeviceIdValid(const std::string & deviceId)82bool DataValidator::IsDeviceIdValid(const std::string& deviceId) 83 { 84 return !deviceId.empty() && (deviceId.length() <= MAX_LENGTH); 85 } 86 IsDcapValid(const std::string & dcap)87bool DataValidator::IsDcapValid(const std::string& dcap) 88 { 89 return !dcap.empty() && (dcap.length() <= MAX_DCAP_LENGTH); 90 } 91 IsPermissionFlagValid(uint32_t flag)92bool DataValidator::IsPermissionFlagValid(uint32_t flag) 93 { 94 uint32_t unmaskedFlag = 95 flag & (~PermissionFlag::PERMISSION_GRANTED_BY_POLICY); 96 return unmaskedFlag == PermissionFlag::PERMISSION_DEFAULT_FLAG || 97 unmaskedFlag == PermissionFlag::PERMISSION_USER_SET || 98 unmaskedFlag == PermissionFlag::PERMISSION_USER_FIXED || 99 unmaskedFlag == PermissionFlag::PERMISSION_SYSTEM_FIXED || 100 unmaskedFlag == PermissionFlag::PERMISSION_COMPONENT_SET || 101 unmaskedFlag == PermissionFlag::PERMISSION_POLICY_FIXED || 102 unmaskedFlag == PermissionFlag::PERMISSION_ALLOW_THIS_TIME; 103 } 104 IsTokenIDValid(AccessTokenID id)105bool DataValidator::IsTokenIDValid(AccessTokenID id) 106 { 107 return id != 0; 108 } 109 IsDlpTypeValid(int dlpType)110bool DataValidator::IsDlpTypeValid(int dlpType) 111 { 112 return ((dlpType == DLP_COMMON) || (dlpType == DLP_READ) || (dlpType == DLP_FULL_CONTROL)); 113 } 114 IsPermissionUsedFlagValid(uint32_t flag)115bool DataValidator::IsPermissionUsedFlagValid(uint32_t flag) 116 { 117 return ((flag == FLAG_PERMISSION_USAGE_SUMMARY) || 118 (flag == FLAG_PERMISSION_USAGE_DETAIL) || 119 (flag == FLAG_PERMISSION_USAGE_SUMMARY_IN_SCREEN_LOCKED) || 120 (flag == FLAG_PERMISSION_USAGE_SUMMARY_IN_SCREEN_UNLOCKED) || 121 (flag == FLAG_PERMISSION_USAGE_SUMMARY_IN_APP_BACKGROUND) || 122 (flag == FLAG_PERMISSION_USAGE_SUMMARY_IN_APP_FOREGROUND)); 123 } 124 IsPermissionUsedTypeValid(uint32_t type)125bool DataValidator::IsPermissionUsedTypeValid(uint32_t type) 126 { 127 return ((type == NORMAL_TYPE) || (type == PICKER_TYPE) || (type == SECURITY_COMPONENT_TYPE)); 128 } 129 IsPolicyTypeValid(uint32_t type)130bool DataValidator::IsPolicyTypeValid(uint32_t type) 131 { 132 PolicyType policyType = static_cast<PolicyType>(type); 133 return ((policyType == EDM) || (policyType == PRIVACY) || (policyType == TEMPORARY)); 134 } 135 IsCallerTypeValid(uint32_t type)136bool DataValidator::IsCallerTypeValid(uint32_t type) 137 { 138 CallerType callerType = static_cast<CallerType>(type); 139 return ((callerType == MICROPHONE) || (callerType == CAMERA)); 140 } 141 } // namespace AccessToken 142 } // namespace Security 143 } // namespace OHOS 144