1 /*
2 * Copyright (c) 2022 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 "sensitive.h"
17 #include <utility>
18 #include <vector>
19 #include "log_print.h"
20 #include "utils/anonymous.h"
21 #undef LOG_TAG
22 #define LOG_TAG "Sensitive"
23
24 namespace OHOS {
25 namespace DistributedKv {
26 using Anonymous = DistributedData::Anonymous;
Sensitive(std::string deviceId)27 Sensitive::Sensitive(std::string deviceId)
28 : deviceId(std::move(deviceId)), securityLevel(DATA_SEC_LEVEL1)
29 {
30 }
31
Sensitive()32 Sensitive::Sensitive()
33 : deviceId(""), securityLevel(DATA_SEC_LEVEL1)
34 {
35 }
36
GetDeviceSecurityLevel()37 uint32_t Sensitive::GetDeviceSecurityLevel()
38 {
39 if (securityLevel > DATA_SEC_LEVEL1) {
40 ZLOGI("the device security level had gotten");
41 return securityLevel;
42 }
43 return GetSensitiveLevel(deviceId);
44 }
45
InitDEVSLQueryParams(DEVSLQueryParams * params,const std::string & udid)46 bool Sensitive::InitDEVSLQueryParams(DEVSLQueryParams *params, const std::string &udid)
47 {
48 ZLOGI("udid is [%{public}s]", Anonymous::Change(udid).c_str());
49 if (params == nullptr || udid.empty()) {
50 return false;
51 }
52 std::vector<uint8_t> vec(udid.begin(), udid.end());
53 for (size_t i = 0; i < MAX_UDID_LENGTH && i < vec.size(); i++) {
54 params->udid[i] = vec[i];
55 }
56 params->udidLen = uint32_t(udid.size());
57 return true;
58 }
59
60 Sensitive::operator bool() const
61 {
62 return (!deviceId.empty()) && (securityLevel > DATA_SEC_LEVEL1);
63 }
64
operator >=(const DistributedDB::SecurityOption & option)65 bool Sensitive::operator >= (const DistributedDB::SecurityOption &option)
66 {
67 if (option.securityLabel == DistributedDB::NOT_SET) {
68 return true;
69 }
70
71 uint32_t level = securityLevel;
72 if (level <= DATA_SEC_LEVEL1 && static_cast<uint32_t>(option.securityLabel - 1) > level) {
73 ZLOGI("the device security level hadn't gotten");
74 level = GetSensitiveLevel(deviceId);
75 }
76 return (level >= static_cast<uint32_t>(option.securityLabel - 1));
77 }
78
Sensitive(const Sensitive & sensitive)79 Sensitive::Sensitive(const Sensitive &sensitive)
80 {
81 this->operator=(sensitive);
82 }
83
operator =(const Sensitive & sensitive)84 Sensitive &Sensitive::operator=(const Sensitive &sensitive)
85 {
86 if (this == &sensitive) {
87 return *this;
88 }
89 deviceId = sensitive.deviceId;
90 securityLevel = sensitive.securityLevel;
91 return *this;
92 }
93
Sensitive(Sensitive && sensitive)94 Sensitive::Sensitive(Sensitive &&sensitive) noexcept
95 {
96 this->operator=(std::move(sensitive));
97 }
98
operator =(Sensitive && sensitive)99 Sensitive &Sensitive::operator=(Sensitive &&sensitive) noexcept
100 {
101 if (this == &sensitive) {
102 return *this;
103 }
104 deviceId = std::move(sensitive.deviceId);
105 securityLevel = sensitive.securityLevel;
106 return *this;
107 }
108
GetSensitiveLevel(const std::string & udid)109 uint32_t Sensitive::GetSensitiveLevel(const std::string &udid)
110 {
111 DEVSLQueryParams query;
112 if (!InitDEVSLQueryParams(&query, udid)) {
113 ZLOGE("init query params failed! udid:[%{public}s]", Anonymous::Change(udid).c_str());
114 return DATA_SEC_LEVEL1;
115 }
116
117 uint32_t level = DATA_SEC_LEVEL1;
118 int32_t result = DATASL_GetHighestSecLevel(&query, &level);
119 if (result != DEVSL_SUCCESS) {
120 ZLOGE("get highest level failed(%{public}s)! level: %{public}d, error: %d",
121 Anonymous::Change(udid).c_str(), securityLevel, result);
122 return DATA_SEC_LEVEL1;
123 }
124 securityLevel = level;
125 ZLOGI("get highest level success(%{public}s)! level: %{public}d, error: %d",
126 Anonymous::Change(udid).c_str(), securityLevel, result);
127 return securityLevel;
128 }
129 } // namespace DistributedKv
130 } // namespace OHOS
131