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 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(params != nullptr && !udid.empty(), false, PASTEBOARD_MODULE_SERVICE,
32 "params check failed, params is null %{public}d", params == nullptr);
33 std::vector<uint8_t> vec(udid.begin(), udid.end());
34 for (size_t i = 0; i < MAX_UDID_LENGTH && i < vec.size(); i++) {
35 params->udid[i] = vec[i];
36 }
37 params->udidLen = static_cast<uint32_t>(MAX_UDID_LENGTH);
38 return true;
39 }
40
GetSensitiveLevel()41 uint32_t SecurityLevel::GetSensitiveLevel()
42 {
43 auto &udid = DMAdapter::GetInstance().GetLocalDeviceUdid();
44 DEVSLQueryParams query;
45 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(InitDEVSLQueryParams(&query, udid), DATA_SEC_LEVEL0, PASTEBOARD_MODULE_SERVICE,
46 "init query params failed! udid:%{public}.6s", udid.c_str());
47
48 uint32_t level = DATA_SEC_LEVEL0;
49 int32_t result = DATASL_GetHighestSecLevel(&query, &level);
50 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(result == DEVSL_SUCCESS, DATA_SEC_LEVEL0, PASTEBOARD_MODULE_SERVICE,
51 "get highest level failed(%{public}.6s)! level:%{public}u, error:%{public}d", udid.c_str(), level, result);
52 securityLevel_ = level;
53 PASTEBOARD_HILOGI(
54 PASTEBOARD_MODULE_SERVICE, "get highest level success(%{public}.6s)! level: %{public}u", udid.c_str(), level);
55 return level;
56 }
57 } // namespace OHOS::MiscServices