• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_translator.h"
17 
18 #include <memory>
19 
20 #include "accesstoken_dfx_define.h"
21 #include "accesstoken_log.h"
22 #include "data_validator.h"
23 #include "permission_validator.h"
24 #include "token_field_const.h"
25 
26 namespace OHOS {
27 namespace Security {
28 namespace AccessToken {
29 namespace {
30 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "DataTranslator"};
31 }
32 
TranslationIntoGenericValues(const PermissionDef & inPermissionDef,GenericValues & outGenericValues)33 int DataTranslator::TranslationIntoGenericValues(const PermissionDef& inPermissionDef, GenericValues& outGenericValues)
34 {
35     outGenericValues.Put(TokenFiledConst::FIELD_PERMISSION_NAME, inPermissionDef.permissionName);
36     outGenericValues.Put(TokenFiledConst::FIELD_BUNDLE_NAME, inPermissionDef.bundleName);
37     outGenericValues.Put(TokenFiledConst::FIELD_GRANT_MODE, inPermissionDef.grantMode);
38     outGenericValues.Put(TokenFiledConst::FIELD_AVAILABLE_LEVEL, inPermissionDef.availableLevel);
39     outGenericValues.Put(TokenFiledConst::FIELD_PROVISION_ENABLE, inPermissionDef.provisionEnable ? 1 : 0);
40     outGenericValues.Put(TokenFiledConst::FIELD_DISTRIBUTED_SCENE_ENABLE,
41         inPermissionDef.distributedSceneEnable ? 1 : 0);
42     outGenericValues.Put(TokenFiledConst::FIELD_LABEL, inPermissionDef.label);
43     outGenericValues.Put(TokenFiledConst::FIELD_LABEL_ID, inPermissionDef.labelId);
44     outGenericValues.Put(TokenFiledConst::FIELD_DESCRIPTION, inPermissionDef.description);
45     outGenericValues.Put(TokenFiledConst::FIELD_DESCRIPTION_ID, inPermissionDef.descriptionId);
46     return RET_SUCCESS;
47 }
48 
TranslationIntoPermissionDef(const GenericValues & inGenericValues,PermissionDef & outPermissionDef)49 int DataTranslator::TranslationIntoPermissionDef(const GenericValues& inGenericValues, PermissionDef& outPermissionDef)
50 {
51     outPermissionDef.permissionName = inGenericValues.GetString(TokenFiledConst::FIELD_PERMISSION_NAME);
52     outPermissionDef.bundleName = inGenericValues.GetString(TokenFiledConst::FIELD_BUNDLE_NAME);
53     outPermissionDef.grantMode = inGenericValues.GetInt(TokenFiledConst::FIELD_GRANT_MODE);
54     int aplNum = inGenericValues.GetInt(TokenFiledConst::FIELD_AVAILABLE_LEVEL);
55     if (!DataValidator::IsAplNumValid(aplNum)) {
56         ACCESSTOKEN_LOG_ERROR(LABEL, "Apl is wrong.");
57         return RET_FAILED;
58     }
59     outPermissionDef.availableLevel = static_cast<ATokenAplEnum>(aplNum);
60     outPermissionDef.provisionEnable = (inGenericValues.GetInt(TokenFiledConst::FIELD_PROVISION_ENABLE) == 1);
61     outPermissionDef.distributedSceneEnable =
62         (inGenericValues.GetInt(TokenFiledConst::FIELD_DISTRIBUTED_SCENE_ENABLE) == 1);
63     outPermissionDef.label = inGenericValues.GetString(TokenFiledConst::FIELD_LABEL);
64     outPermissionDef.labelId = inGenericValues.GetInt(TokenFiledConst::FIELD_LABEL_ID);
65     outPermissionDef.description = inGenericValues.GetString(TokenFiledConst::FIELD_DESCRIPTION);
66     outPermissionDef.descriptionId = inGenericValues.GetInt(TokenFiledConst::FIELD_DESCRIPTION_ID);
67     return RET_SUCCESS;
68 }
69 
TranslationIntoGenericValues(const PermissionStateFull & inPermissionState,const unsigned int grantIndex,GenericValues & outGenericValues)70 int DataTranslator::TranslationIntoGenericValues(const PermissionStateFull& inPermissionState,
71     const unsigned int grantIndex, GenericValues& outGenericValues)
72 {
73     if (grantIndex >= inPermissionState.resDeviceID.size() || grantIndex >= inPermissionState.grantStatus.size() ||
74         grantIndex >= inPermissionState.grantFlags.size()) {
75         ACCESSTOKEN_LOG_ERROR(LABEL, "perm status grant size is wrong");
76         return RET_FAILED;
77     }
78     outGenericValues.Put(TokenFiledConst::FIELD_PERMISSION_NAME, inPermissionState.permissionName);
79     outGenericValues.Put(TokenFiledConst::FIELD_DEVICE_ID, inPermissionState.resDeviceID[grantIndex]);
80     outGenericValues.Put(TokenFiledConst::FIELD_GRANT_IS_GENERAL, inPermissionState.isGeneral ? 1 : 0);
81     outGenericValues.Put(TokenFiledConst::FIELD_GRANT_STATE, inPermissionState.grantStatus[grantIndex]);
82     outGenericValues.Put(TokenFiledConst::FIELD_GRANT_FLAG, inPermissionState.grantFlags[grantIndex]);
83     return RET_SUCCESS;
84 }
85 
TranslationIntoPermissionStateFull(const GenericValues & inGenericValues,PermissionStateFull & outPermissionState)86 int DataTranslator::TranslationIntoPermissionStateFull(const GenericValues& inGenericValues,
87     PermissionStateFull& outPermissionState)
88 {
89     outPermissionState.isGeneral =
90         ((inGenericValues.GetInt(TokenFiledConst::FIELD_GRANT_IS_GENERAL) == 1) ? true : false);
91     outPermissionState.permissionName = inGenericValues.GetString(TokenFiledConst::FIELD_PERMISSION_NAME);
92     if (!DataValidator::IsPermissionNameValid(outPermissionState.permissionName)) {
93         ACCESSTOKEN_LOG_ERROR(LABEL, "permission name is wrong");
94         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "PERMISSION_CHECK",
95             HiviewDFX::HiSysEvent::EventType::FAULT, "CODE", LOAD_DATABASE_ERROR,
96             "ERROR_REASON", "permission name error");
97         return RET_FAILED;
98     }
99 
100     std::string devID = inGenericValues.GetString(TokenFiledConst::FIELD_DEVICE_ID);
101     if (!DataValidator::IsDeviceIdValid(devID)) {
102         ACCESSTOKEN_LOG_ERROR(LABEL, "devID is wrong");
103         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "PERMISSION_CHECK",
104             HiviewDFX::HiSysEvent::EventType::FAULT, "CODE", LOAD_DATABASE_ERROR,
105             "ERROR_REASON", "permission deviceId error");
106         return RET_FAILED;
107     }
108     outPermissionState.resDeviceID.push_back(devID);
109 
110     int grantStatus = (PermissionState)inGenericValues.GetInt(TokenFiledConst::FIELD_GRANT_STATE);
111     if (!PermissionValidator::IsGrantStatusValid(grantStatus)) {
112         ACCESSTOKEN_LOG_ERROR(LABEL, "grantStatus is wrong");
113         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "PERMISSION_CHECK",
114             HiviewDFX::HiSysEvent::EventType::FAULT, "CODE", LOAD_DATABASE_ERROR,
115             "ERROR_REASON", "permission grant status error");
116         return RET_FAILED;
117     }
118     outPermissionState.grantStatus.push_back(grantStatus);
119 
120     int grantFlag = (PermissionState)inGenericValues.GetInt(TokenFiledConst::FIELD_GRANT_FLAG);
121     if (!PermissionValidator::IsPermissionFlagValid(grantFlag)) {
122         ACCESSTOKEN_LOG_ERROR(LABEL, "grantFlag is wrong");
123         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "PERMISSION_CHECK",
124             HiviewDFX::HiSysEvent::EventType::FAULT, "CODE", LOAD_DATABASE_ERROR,
125             "ERROR_REASON", "permission grant flag error");
126         return RET_FAILED;
127     }
128     outPermissionState.grantFlags.push_back(grantFlag);
129     return RET_SUCCESS;
130 }
131 } // namespace AccessToken
132 } // namespace Security
133 } // namespace OHOS
134