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 #include "security_level.h"
16
17 #include "device/dm_adapter.h"
18 #include "pasteboard_hilog.h"
19
20 namespace OHOS::MiscServices {
GetDeviceSecurityLevel()21 uint32_t SecurityLevel::GetDeviceSecurityLevel()
22 {
23 if (securityLevel_ > DATA_SEC_LEVEL0) {
24 return securityLevel_;
25 }
26 return GetSensitiveLevel();
27 }
28
InitDEVSLQueryParams(DEVSLQueryParams * params,const std::string & udid)29 bool SecurityLevel::InitDEVSLQueryParams(DEVSLQueryParams *params, const std::string &udid)
30 {
31 if (params == nullptr || udid.empty()) {
32 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE,
33 "params check failed, params is null %{public}d", params == nullptr);
34 return false;
35 }
36 std::vector<uint8_t> vec(udid.begin(), udid.end());
37 for (size_t i = 0; i < MAX_UDID_LENGTH && i < vec.size(); i++) {
38 params->udid[i] = vec[i];
39 }
40 params->udidLen = static_cast<uint32_t>(MAX_UDID_LENGTH);
41 return true;
42 }
43
GetSensitiveLevel()44 uint32_t SecurityLevel::GetSensitiveLevel()
45 {
46 auto &udid = DMAdapter::GetInstance().GetLocalDeviceUdid();
47 DEVSLQueryParams query;
48 if (!InitDEVSLQueryParams(&query, udid)) {
49 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "init query params failed! udid:%{public}.6s", udid.c_str());
50 return DATA_SEC_LEVEL0;
51 }
52
53 uint32_t level = DATA_SEC_LEVEL0;
54 int32_t result = DATASL_GetHighestSecLevel(&query, &level);
55 if (result != DEVSL_SUCCESS) {
56 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE,
57 "get highest level failed(%{public}.6s)! level:%{public}u, error:%{public}d", udid.c_str(), level, result);
58 return DATA_SEC_LEVEL0;
59 }
60 securityLevel_ = level;
61 PASTEBOARD_HILOGI(
62 PASTEBOARD_MODULE_SERVICE, "get highest level success(%{public}.6s)! level: %{public}u", udid.c_str(), level);
63 return level;
64 }
65 } // namespace OHOS::MiscServices