1 /*
2  * Copyright (c) 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 "platform/ohos/rs_irender_service_ipc_interface_code_access_verifier.h"
17 
18 namespace OHOS {
19 namespace Rosen {
RSIRenderServiceInterfaceCodeAccessVerifier()20 RSIRenderServiceInterfaceCodeAccessVerifier::RSIRenderServiceInterfaceCodeAccessVerifier()
21 {
22     CheckCodeUnderlyingTypeStandardized<CodeEnumType>(codeEnumTypeName_);
23 #ifdef ENABLE_IPC_SECURITY
24     AddRSIRenderServiceInterfaceCodePermission();
25 #endif
26 }
27 
IsExclusiveVerificationPassed(CodeUnderlyingType code)28 bool RSIRenderServiceInterfaceCodeAccessVerifier::IsExclusiveVerificationPassed(CodeUnderlyingType code)
29 {
30     bool hasPermission = true;
31     switch (code) {
32         case static_cast<CodeUnderlyingType>(CodeEnumType::CREATE_CONNECTION): {
33             hasPermission = CheckPermission(code);
34             break;
35         }
36         default: {
37             break;
38         }
39     }
40     return hasPermission;
41 }
42 #ifdef ENABLE_IPC_SECURITY
AddRSIRenderServiceInterfaceCodePermission()43 void RSIRenderServiceInterfaceCodeAccessVerifier::AddRSIRenderServiceInterfaceCodePermission()
44 {
45     for (auto& mapping : permissionRSIRenderServiceInterfaceMappings_) {
46         CodeEnumType interfaceName = mapping.first;
47         PermissionType permission = mapping.second;
48         std::string newPermission = PermissionEnumToString(permission);
49         if (newPermission == "unknown") {
50             continue;
51         }
52         CodeUnderlyingType code = static_cast<CodeUnderlyingType>(interfaceName);
53         AddPermission(code, newPermission);
54     }
55 }
56 
IsAccessTimesVerificationPassed(CodeUnderlyingType code,uint32_t times) const57 bool RSIRenderServiceInterfaceCodeAccessVerifier::IsAccessTimesVerificationPassed(
58     CodeUnderlyingType code, uint32_t times) const
59 {
60     auto interfaceName = static_cast<CodeEnumType>(code);
61     if (accessRSIRenderServiceInterfaceTimesRestrictions_.count(interfaceName) == 0) {
62         return true;
63     }
64     uint32_t restrictedTimes = accessRSIRenderServiceInterfaceTimesRestrictions_.at(interfaceName);
65     return times < restrictedTimes;
66 }
67 #endif
68 
69 } // namespace Rosen
70 } // namespace OHOS
71