1bc03f14fSopenharmony_ci/*
2bc03f14fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License.
5bc03f14fSopenharmony_ci * You may obtain a copy of the License at
6bc03f14fSopenharmony_ci *
7bc03f14fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc03f14fSopenharmony_ci *
9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and
13bc03f14fSopenharmony_ci * limitations under the License.
14bc03f14fSopenharmony_ci */
15bc03f14fSopenharmony_ci#include <vector>
16bc03f14fSopenharmony_ci
17bc03f14fSopenharmony_ci#include "device/dm_adapter.h"
18bc03f14fSopenharmony_ci#include "pasteboard_hilog.h"
19bc03f14fSopenharmony_ci#include "security_level.h"
20bc03f14fSopenharmony_ci
21bc03f14fSopenharmony_cinamespace OHOS::MiscServices {
22bc03f14fSopenharmony_ciuint32_t SecurityLevel::GetDeviceSecurityLevel()
23bc03f14fSopenharmony_ci{
24bc03f14fSopenharmony_ci    if (securityLevel_ > DATA_SEC_LEVEL0) {
25bc03f14fSopenharmony_ci        return securityLevel_;
26bc03f14fSopenharmony_ci    }
27bc03f14fSopenharmony_ci    return GetSensitiveLevel();
28bc03f14fSopenharmony_ci}
29bc03f14fSopenharmony_ci
30bc03f14fSopenharmony_cibool SecurityLevel::InitDEVSLQueryParams(DEVSLQueryParams *params, const std::string &udid)
31bc03f14fSopenharmony_ci{
32bc03f14fSopenharmony_ci    if (params == nullptr || udid.empty()) {
33bc03f14fSopenharmony_ci        return false;
34bc03f14fSopenharmony_ci    }
35bc03f14fSopenharmony_ci    std::vector<uint8_t> vec(udid.begin(), udid.end());
36bc03f14fSopenharmony_ci    for (size_t i = 0; i < MAX_UDID_LENGTH && i < vec.size(); i++) {
37bc03f14fSopenharmony_ci        params->udid[i] = vec[i];
38bc03f14fSopenharmony_ci    }
39bc03f14fSopenharmony_ci    params->udidLen = static_cast<uint32_t>(udid.size());
40bc03f14fSopenharmony_ci    return true;
41bc03f14fSopenharmony_ci}
42bc03f14fSopenharmony_ci
43bc03f14fSopenharmony_ciuint32_t SecurityLevel::GetSensitiveLevel()
44bc03f14fSopenharmony_ci{
45bc03f14fSopenharmony_ci    auto &udid = DMAdapter::GetInstance().GetLocalDeviceUdid();
46bc03f14fSopenharmony_ci    DEVSLQueryParams query;
47bc03f14fSopenharmony_ci    if (!InitDEVSLQueryParams(&query, udid)) {
48bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "init query params failed! udid:%{public}.6s", udid.c_str());
49bc03f14fSopenharmony_ci        return DATA_SEC_LEVEL0;
50bc03f14fSopenharmony_ci    }
51bc03f14fSopenharmony_ci
52bc03f14fSopenharmony_ci    uint32_t level = DATA_SEC_LEVEL0;
53bc03f14fSopenharmony_ci    int32_t result = DATASL_GetHighestSecLevel(&query, &level);
54bc03f14fSopenharmony_ci    if (result != DEVSL_SUCCESS) {
55bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE,
56bc03f14fSopenharmony_ci            "get highest level failed(%{public}.6s)! level:%{public}u, error:%{public}d", udid.c_str(), level, result);
57bc03f14fSopenharmony_ci        return DATA_SEC_LEVEL0;
58bc03f14fSopenharmony_ci    }
59bc03f14fSopenharmony_ci    securityLevel_ = level;
60bc03f14fSopenharmony_ci    PASTEBOARD_HILOGI(
61bc03f14fSopenharmony_ci        PASTEBOARD_MODULE_SERVICE, "get highest level success(%{public}.6s)! level: %{public}u", udid.c_str(), level);
62bc03f14fSopenharmony_ci    return level;
63bc03f14fSopenharmony_ci}
64bc03f14fSopenharmony_ci} // namespace OHOS::MiscServices