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"
46 using RowOptional = boost::optional<Row>;
47 using RawBufferOptional = boost::optional<RawBuffer>;
49 Crypto() : m_inUserTransaction(false) {}
51 // user name instead of path?
52 // in no way to I condone the use of std::string for legacyPath; see: review
53 Crypto(const std::string &legacyPath, const std::string &path, const RawBuffer &rawPass);
54 Crypto(const Crypto &other) = delete;
55 Crypto(Crypto &&other);
57 Crypto &operator=(const Crypto &) = delete;
58 Crypto &operator=(Crypto &&other);
60 virtual ~Crypto() = default;
67 const ClientId &owner,
68 const RowVector &rows);
73 bool isNameOwnerPresent(
75 const ClientId &owner) const;
79 const ClientId &owner,
84 const ClientId &owner,
85 DataType typeRangeStart,
86 DataType typeRangeStop);
90 const ClientId &owner,
96 const ClientId &owner,
97 DataType typeRangeStart,
98 DataType typeRangeStop,
102 const ClientId &owner,
103 AliasInfoVector &aliasInfoVector,
107 const ClientId &owner,
108 AliasInfoVector &aliasInfoVector,
109 DataType typeRangeStart,
110 DataType typeRangeStop);
114 const ClientId &owner);
117 void saveKey(const ClientId &owner, const RawBuffer &key);
118 RawBufferOptional getKey(const ClientId &owner);
119 void deleteKey(const ClientId &owner);
124 const ClientId &owner,
125 const ClientId &accessor,
126 const PermissionMask permissionMask);
128 PermissionMaskOptional getPermissionRow(
130 const ClientId &owner,
131 const ClientId &accessor) const;
135 explicit Transaction(Crypto *db) : m_db(db), m_inTransaction(false)
137 if (!m_db->m_inUserTransaction) {
139 m_db->m_connection->BeginTransaction();
140 m_db->m_inUserTransaction = true;
141 m_inTransaction = true;
142 } catch (const SqlConnection::Exception::InternalError &) {
143 ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
144 } catch (const SqlConnection::Exception::Base &) {
145 ThrowErr(Exc::TransactionFailed, "Couldn't begin transaction");
150 Transaction(Transaction&& other)
153 m_inTransaction = other.m_inTransaction;
154 other.m_inTransaction = false;
157 Transaction& operator=(Transaction&& other)
163 m_inTransaction = other.m_inTransaction;
164 other.m_inTransaction = false;
171 if (m_inTransaction) {
173 m_db->m_connection->CommitTransaction();
174 m_db->m_inUserTransaction = false;
175 m_inTransaction = false;
176 } catch (const SqlConnection::Exception::InternalError &) {
177 ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
178 } catch (const SqlConnection::Exception::Base &) {
179 ThrowErr(Exc::TransactionFailed, "Couldn't commit transaction");
186 if (m_inTransaction) {
188 m_db->m_connection->RollbackTransaction();
189 m_db->m_inUserTransaction = false;
190 m_inTransaction = false;
191 } catch (const SqlConnection::Exception::InternalError &) {
192 ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
193 } catch (const SqlConnection::Exception::Base &) {
194 ThrowErr(Exc::TransactionFailed, "Couldn't rollback transaction");
202 if (m_inTransaction) {
203 m_db->m_inUserTransaction = false;
204 m_db->m_connection->RollbackTransaction();
206 } catch (const SqlConnection::Exception::InternalError &) {
207 LogError("sqlite got into infinite busy state");
208 } catch (const SqlConnection::Exception::Base &) {
209 LogError("Transaction rollback failed!");
215 bool m_inTransaction;
219 std::unique_ptr<SqlConnection> m_connection;
222 const SqlConnection::DataCommandUniquePtr &selectCommand) const;
225 bool m_inUserTransaction;
231 void createDBSchema();
233 * return current database version
235 * @param[out] schemaVersion if success, will contain DB schema version code
237 * @return false on DB empty or corrupted, true if information read
239 bool getDBVersion(int &schemaVersion);
241 using ScriptOptional = boost::optional<std::string>;
242 ScriptOptional getScript(const std::string &scriptName) const;
243 ScriptOptional getMigrationScript(int db_version) const;
247 explicit SchemaInfo(SqlConnection *connection) : m_connection(connection) {}
249 void setVersionInfo();
250 bool getVersionInfo(int &version);
253 SqlConnection *m_connection;
259 explicit NameTable(SqlConnection *connection) : m_connection(connection) {}
263 const ClientId &owner);
267 const ClientId &owner);
270 const ClientId &owner);
274 const ClientId &owner) const;
277 SqlConnection *m_connection;
282 explicit ObjectTable(SqlConnection *connection) : m_connection(connection) {}
290 SqlConnection *m_connection;
293 class PermissionTable {
295 explicit PermissionTable(SqlConnection *connection) : m_connection(
300 const ClientId &owner,
301 const ClientId &accessor,
302 const PermissionMask permissionMask);
304 PermissionMaskOptional getPermissionRow(
306 const ClientId &owner,
307 const ClientId &accessor) const;
310 SqlConnection *m_connection;
318 #pragma GCC diagnostic pop
319 #endif // CKM_DB_CRYPTO_H