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