• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "GdbStoreConfig"
16 #include <utility>
17 
18 #include "gdb_errors.h"
19 #include "gdb_store_config.h"
20 #include "gdb_utils.h"
21 #include "logger.h"
22 #include "rdb_security_manager.h"
23 
24 namespace OHOS::DistributedDataAip {
StoreConfig(std::string name,std::string path,DBType dbType,bool isEncrypt,const std::vector<uint8_t> & encryptKey)25 StoreConfig::StoreConfig(
26     std::string name, std::string path, DBType dbType, bool isEncrypt, const std::vector<uint8_t> &encryptKey)
27     : name_(std::move(name)), path_(std::move(path)), dbType_(dbType), isEncrypt_(isEncrypt), encryptKey_(encryptKey)
28 {
29 }
30 
~StoreConfig()31 StoreConfig::~StoreConfig()
32 {
33     ClearEncryptKey();
34 }
35 
SetName(std::string name)36 void StoreConfig::SetName(std::string name)
37 {
38     name_ = std::move(name);
39 }
40 
SetPath(std::string path)41 void StoreConfig::SetPath(std::string path)
42 {
43     path_ = std::move(path);
44 }
45 
SetDbType(DBType dbType)46 void StoreConfig::SetDbType(DBType dbType)
47 {
48     dbType_ = dbType;
49 }
50 
SetEncryptStatus(const bool status)51 void StoreConfig::SetEncryptStatus(const bool status)
52 {
53     isEncrypt_ = status;
54 }
55 
IsEncrypt() const56 bool StoreConfig::IsEncrypt() const
57 {
58     return isEncrypt_;
59 }
60 
GetFullPath() const61 std::string StoreConfig::GetFullPath() const
62 {
63     return path_ + "/" + name_ + ".db";
64 }
65 
GetPath() const66 std::string StoreConfig::GetPath() const
67 {
68     return path_;
69 }
70 
GetName() const71 std::string StoreConfig::GetName() const
72 {
73     return name_;
74 }
75 
GetDbType() const76 DBType StoreConfig::GetDbType() const
77 {
78     return dbType_;
79 }
80 
GetIter() const81 int32_t StoreConfig::GetIter() const
82 {
83     return iter_;
84 }
85 
SetIter(int32_t iter) const86 void StoreConfig::SetIter(int32_t iter) const
87 {
88     iter_ = iter;
89 }
90 
GetWriteTime() const91 int StoreConfig::GetWriteTime() const
92 {
93     return writeTimeout_;
94 }
95 
SetWriteTime(int timeout)96 void StoreConfig::SetWriteTime(int timeout)
97 {
98     writeTimeout_ = std::max(MIN_TIMEOUT, std::min(MAX_TIMEOUT, timeout));
99 }
100 
GetReadTime() const101 int StoreConfig::GetReadTime() const
102 {
103     return readTimeout_;
104 }
105 
SetReadTime(int timeout)106 void StoreConfig::SetReadTime(int timeout)
107 {
108     readTimeout_ = std::max(MIN_TIMEOUT, std::min(MAX_TIMEOUT, timeout));
109 }
110 
GetReadConSize() const111 int StoreConfig::GetReadConSize() const
112 {
113     return readConSize_;
114 }
115 
SetReadConSize(int readConSize)116 void StoreConfig::SetReadConSize(int readConSize)
117 {
118     readConSize_ = readConSize;
119 }
120 
SetSecurityLevel(int32_t securityLevel)121 void StoreConfig::SetSecurityLevel(int32_t securityLevel)
122 {
123     securityLevel_ = securityLevel;
124 }
125 
GetSecurityLevel() const126 int32_t StoreConfig::GetSecurityLevel() const
127 {
128     return securityLevel_;
129 }
130 
SetBundleName(const std::string & bundleName)131 int StoreConfig::SetBundleName(const std::string &bundleName)
132 {
133     if (bundleName.empty()) {
134         return E_ERROR;
135     }
136     bundleName_ = bundleName;
137     return E_OK;
138 }
139 
GetBundleName() const140 std::string StoreConfig::GetBundleName() const
141 {
142     return bundleName_;
143 }
144 
GetEncryptKey() const145 std::vector<uint8_t> StoreConfig::GetEncryptKey() const
146 {
147     return encryptKey_;
148 }
149 
GetNewEncryptKey() const150 std::vector<uint8_t> StoreConfig::GetNewEncryptKey() const
151 {
152     return newEncryptKey_;
153 }
154 
GenerateEncryptedKey() const155 void StoreConfig::GenerateEncryptedKey() const
156 {
157     if (!IsEncrypt()) {
158         return;
159     }
160     auto rdbConfig = std::make_shared<NativeRdb::RdbStoreConfig>(GetFullPath());
161     if (rdbConfig == nullptr) {
162         LOG_ERROR("rdbConfig is nullptr. path:%{public}s", GdbUtils::Anonymous(GetFullPath()).c_str());
163         return;
164     }
165     rdbConfig->SetBundleName(bundleName_);
166     rdbConfig->SetEncryptStatus(true);
167     auto errCode = rdbConfig->Initialize();
168     if (errCode != E_OK) {
169         LOG_ERROR("rdbConfig init encrypt failed. errCode:%{public}d, path:%{public}s",
170             errCode, GdbUtils::Anonymous(GetFullPath()).c_str());
171         return;
172     }
173     encryptKey_.assign(encryptKey_.size(), 0);
174     encryptKey_ = rdbConfig->GetEncryptKey();
175     newEncryptKey_.assign(newEncryptKey_.size(), 0);
176     newEncryptKey_ = rdbConfig->GetNewEncryptKey();
177 }
178 
ClearEncryptKey()179 void StoreConfig::ClearEncryptKey()
180 {
181     encryptKey_.assign(encryptKey_.size(), 0);
182     newEncryptKey_.assign(newEncryptKey_.size(), 0);
183 }
184 
ChangeEncryptKey() const185 void StoreConfig::ChangeEncryptKey() const
186 {
187     NativeRdb::RdbSecurityManager::GetInstance().ChangeKeyFile(GetFullPath());
188     if (newEncryptKey_.empty()) {
189         return;
190     }
191     encryptKey_.assign(encryptKey_.size(), 0);
192     encryptKey_ = newEncryptKey_;
193     newEncryptKey_.assign(newEncryptKey_.size(), 0);
194     newEncryptKey_.resize(0);
195 }
196 } // namespace OHOS::DistributedDataAip