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