blob: 661ab04df54ac4cdfc7f3febb8d80cdd1b60d7b9 [file] [log] [blame]
Zhiyi Zhang5f133622015-10-17 08:49:54 +08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang19a11d22018-04-12 22:58:20 -07003 * Copyright (c) 2014-2018, Regents of the University of California
Zhiyi Zhang5f133622015-10-17 08:49:54 +08004 *
5 * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN).
6 * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors.
7 *
8 * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndn-group-encrypt, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070019 * @author Zhiyi Zhang <zhiyi@cs.ucla.edu>
Zhiyi Zhang5f133622015-10-17 08:49:54 +080020 */
21
22#include "consumer-db.hpp"
Zhiyi Zhang5f133622015-10-17 08:49:54 +080023#include <ndn-cxx/util/sqlite3-statement.hpp>
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070024#include <boost/filesystem.hpp>
25#include <sqlite3.h>
Zhiyi Zhang5f133622015-10-17 08:49:54 +080026
27namespace ndn {
28namespace gep {
29
30using util::Sqlite3Statement;
31
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070032static const std::string INITIALIZATION = R"_DBTEXT_(
33CREATE TABLE IF NOT EXISTS
34 decryptionkeys(
35 key_id INTEGER PRIMARY KEY,
36 key_name BLOB NOT NULL,
37 key_buf BLOB NOT NULL
38 );
39CREATE UNIQUE INDEX IF NOT EXISTS
40 KeyNameIndex ON decryptionkeys(key_name);
41)_DBTEXT_";
Zhiyi Zhang5f133622015-10-17 08:49:54 +080042
43class ConsumerDB::Impl
44{
45public:
Yingdi Yu48967a62016-03-11 22:04:14 -080046 Impl(const std::string& dbPath)
Zhiyi Zhang5f133622015-10-17 08:49:54 +080047 {
48 // open Database
49
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070050 int result = sqlite3_open_v2(dbPath.c_str(),
51 &m_database,
Zhiyi Zhang5f133622015-10-17 08:49:54 +080052 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070053 nullptr);
Zhiyi Zhang5f133622015-10-17 08:49:54 +080054
55 if (result != SQLITE_OK)
Yingdi Yu48967a62016-03-11 22:04:14 -080056 BOOST_THROW_EXCEPTION(Error("GroupManager DB cannot be opened/created: " + dbPath));
Zhiyi Zhang5f133622015-10-17 08:49:54 +080057
58 // initialize database specific tables
59 char* errorMessage = nullptr;
60 result = sqlite3_exec(m_database, INITIALIZATION.c_str(), nullptr, nullptr, &errorMessage);
61 if (result != SQLITE_OK && errorMessage != nullptr) {
62 sqlite3_free(errorMessage);
63 BOOST_THROW_EXCEPTION(Error("GroupManager DB cannot be initialized"));
64 }
65 }
66
67 ~Impl()
68 {
69 sqlite3_close(m_database);
70 }
71
72public:
73 sqlite3* m_database;
74};
75
76
Yingdi Yu48967a62016-03-11 22:04:14 -080077ConsumerDB::ConsumerDB(const std::string& dbPath)
78 : m_impl(new Impl(dbPath))
Zhiyi Zhang5f133622015-10-17 08:49:54 +080079{
80}
81
82ConsumerDB::~ConsumerDB() = default;
83
84const Buffer
85ConsumerDB::getKey(const Name& keyName) const
86{
87 Sqlite3Statement statement(m_impl->m_database,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070088 R"_DBTEXT_(SELECT key_buf FROM decryptionkeys
89 WHERE key_name=?)_DBTEXT_");
Zhiyi Zhang5f133622015-10-17 08:49:54 +080090 statement.bind(1, keyName.wireEncode(), SQLITE_TRANSIENT);
91
92 Buffer result;
93 if (statement.step() == SQLITE_ROW) {
94 result = Buffer(statement.getBlob(0), statement.getSize(0));
95 }
96 return result;
97}
98
99void
100ConsumerDB::addKey(const Name& keyName, const Buffer& keyBuf)
101{
102 Sqlite3Statement statement(m_impl->m_database,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700103 R"_DBTEXT_(INSERT INTO decryptionkeys(key_name, key_buf)
104 values (?, ?))_DBTEXT_");
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800105 statement.bind(1, keyName.wireEncode(), SQLITE_TRANSIENT);
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700106 statement.bind(2, keyBuf.data(), keyBuf.size(), SQLITE_TRANSIENT);
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800107
108 if (statement.step() != SQLITE_DONE)
109 BOOST_THROW_EXCEPTION(Error("Cannot add the key to database"));
110}
111
112void
113ConsumerDB::deleteKey(const Name& keyName)
114{
115 Sqlite3Statement statement(m_impl->m_database,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700116 R"_DBTEXT_(DELETE FROM decryptionkeys WHERE key_name=?)_DBTEXT_");
Zhiyi Zhang5f133622015-10-17 08:49:54 +0800117 statement.bind(1, keyName.wireEncode(), SQLITE_TRANSIENT);
118 statement.step();
119}
120
121} // namespace gep
122} // namespace ndn