1 /*
2  * Copyright (c) 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 "b_sa/b_sa_utils.h"
17 #include "access_token.h"
18 #include "accesstoken_kit.h"
19 #include "ipc_skeleton.h"
20 #include "tokenid_kit.h"
21 
22 namespace OHOS::FileManagement::Backup {
23 
24 namespace {
25     const std::string BACKUP_PERMISSION = "ohos.permission.BACKUP";
26 }
27 
IsSABundleName(std::string bundleName)28 bool SAUtils::IsSABundleName(std::string bundleName)
29 {
30     if (bundleName.empty()) {
31         return false;
32     }
33     for (size_t i = 0; i < bundleName.size(); i++) {
34         if (!std::isdigit(bundleName[i])) {
35             return false;
36         }
37     }
38     return true;
39 }
40 
CheckBackupPermission()41 bool SAUtils::CheckBackupPermission()
42 {
43     Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
44     return Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, BACKUP_PERMISSION) ==
45         Security::AccessToken::PermissionState::PERMISSION_GRANTED;
46 }
47 
CheckPermission(const std::string & permission)48 bool SAUtils::CheckPermission(const std::string &permission)
49 {
50     Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
51     return Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission) ==
52         Security::AccessToken::PermissionState::PERMISSION_GRANTED;
53 }
54 
IsSystemApp()55 bool SAUtils::IsSystemApp()
56 {
57     uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID();
58     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
59 }
60 } // namespace OHOS::FileManagement::Backup