2 * Copyright (c) 2014-2020 Samsung Electronics Co., Ltd. All rights reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Zofia Abramowska (z.abramowska@samsung.com)
20 * @brief Header of encrypted db access layer
22 #ifndef CKM_DB_CRYPTO_H
23 #define CKM_DB_CRYPTO_H
28 #include <dpl/db/sql_connection.h>
30 #include <ckm/ckm-type.h>
31 #include <exception.h>
33 #include <permission.h>
34 #include <protocols.h>
36 #pragma GCC diagnostic push
37 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
43 using RowOptional = boost::optional<Row>;
44 using RawBufferOptional = boost::optional<RawBuffer>;
46 Crypto() : m_inUserTransaction(false) {}
48 // user name instead of path?
49 // in no way to I condone the use of std::string for legacyPath; see: review
50 Crypto(const std::string &legacyPath, const std::string &path, const RawBuffer &rawPass);
51 Crypto(const Crypto &other) = delete;
52 Crypto(Crypto &&other);
54 Crypto &operator=(const Crypto &) = delete;
55 Crypto &operator=(Crypto &&other);
57 virtual ~Crypto() = default;
64 const ClientId &owner,
65 const RowVector &rows);
70 bool isNameOwnerPresent(
72 const ClientId &owner) const;
76 const ClientId &owner,
81 const ClientId &owner,
82 DataType typeRangeStart,
83 DataType typeRangeStop);
87 const ClientId &owner,
93 const ClientId &owner,
94 DataType typeRangeStart,
95 DataType typeRangeStop,
99 const ClientId &owner,
100 OwnerNameVector &ownerNameVector,
104 const ClientId &owner,
105 OwnerNameVector &ownerNameVector,
106 DataType typeRangeStart,
107 DataType typeRangeStop);
111 const ClientId &owner);
114 void saveKey(const ClientId &owner, const RawBuffer &key);
115 RawBufferOptional getKey(const ClientId &owner);
116 void deleteKey(const ClientId &owner);
121 const ClientId &owner,
122 const ClientId &accessor,
123 const PermissionMask permissionMask);
125 PermissionMaskOptional getPermissionRow(
127 const ClientId &owner,
128 const ClientId &accessor) const;
131 int beginTransaction();
132 int commitTransaction();
133 int rollbackTransaction();
137 explicit Transaction(Crypto *db) : m_db(db), m_inTransaction(false)
139 if (!m_db->m_inUserTransaction) {
141 m_db->m_connection->ExecCommand("BEGIN EXCLUSIVE");
142 m_db->m_inUserTransaction = true;
143 m_inTransaction = true;
144 } catch (const SqlConnection::Exception::InternalError &) {
145 ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
146 } catch (const SqlConnection::Exception::Base &) {
147 ThrowErr(Exc::TransactionFailed, "Couldn't begin transaction");
154 if (m_inTransaction) {
156 m_db->m_connection->CommitTransaction();
157 m_db->m_inUserTransaction = false;
158 m_inTransaction = false;
159 } catch (const SqlConnection::Exception::InternalError &) {
160 ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
161 } catch (const SqlConnection::Exception::Base &) {
162 ThrowErr(Exc::TransactionFailed, "Couldn't commit transaction");
169 if (m_inTransaction) {
171 m_db->m_connection->RollbackTransaction();
172 m_db->m_inUserTransaction = false;
173 m_inTransaction = false;
174 } catch (const SqlConnection::Exception::InternalError &) {
175 ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
176 } catch (const SqlConnection::Exception::Base &) {
177 ThrowErr(Exc::TransactionFailed, "Couldn't rollback transaction");
185 if (m_inTransaction) {
186 m_db->m_inUserTransaction = false;
187 m_db->m_connection->RollbackTransaction();
189 } catch (const SqlConnection::Exception::InternalError &) {
190 LogError("sqlite got into infinite busy state");
191 } catch (const SqlConnection::Exception::Base &) {
192 LogError("Transaction rollback failed!");
198 bool m_inTransaction;
202 std::unique_ptr<SqlConnection> m_connection;
205 const SqlConnection::DataCommandUniquePtr &selectCommand) const;
208 bool m_inUserTransaction;
212 void createDBSchema();
214 * return current database version
216 * @param[out] schemaVersion if success, will contain DB schema version code
218 * @return false on DB empty or corrupted, true if information read
220 bool getDBVersion(int &schemaVersion);
222 using ScriptOptional = boost::optional<std::string>;
223 ScriptOptional getScript(const std::string &scriptName) const;
224 ScriptOptional getMigrationScript(int db_version) const;
227 const char *create_cmd,
228 const char *table_name);
231 const char *create_cmd);
235 explicit SchemaInfo(SqlConnection *connection) : m_connection(connection) {}
237 void setVersionInfo();
238 bool getVersionInfo(int &version);
241 SqlConnection *m_connection;
247 explicit NameTable(SqlConnection *connection) : m_connection(connection) {}
251 const ClientId &owner);
255 const ClientId &owner);
258 const ClientId &owner);
262 const ClientId &owner) const;
265 SqlConnection *m_connection;
270 explicit ObjectTable(SqlConnection *connection) : m_connection(connection) {}
278 SqlConnection *m_connection;
281 class PermissionTable {
283 explicit PermissionTable(SqlConnection *connection) : m_connection(
288 const ClientId &owner,
289 const ClientId &accessor,
290 const PermissionMask permissionMask);
292 PermissionMaskOptional getPermissionRow(
294 const ClientId &owner,
295 const ClientId &accessor) const;
298 SqlConnection *m_connection;
305 #pragma GCC diagnostic pop
306 #endif // CKM_DB_CRYPTO_H