blob: e2898ac2dd0513730c80de73054050f4502ff9c4 [file] [log] [blame]
Zhiyi Zhang5f133622015-10-17 08:49:54 +08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California
4 *
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 *
19 * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
20 */
21
22#include "consumer-db.hpp"
23
24#include <sqlite3.h>
25#include <boost/filesystem.hpp>
26#include <ndn-cxx/util/sqlite3-statement.hpp>
27
28namespace ndn {
29namespace gep {
30
31using util::Sqlite3Statement;
32
33static const std::string INITIALIZATION =
34 "CREATE TABLE IF NOT EXISTS \n"
35 " decryptionkeys( \n"
36 " key_id INTEGER PRIMARY KEY, \n"
37 " key_name BLOB NOT NULL, \n"
38 " key_buf BLOB NOT NULL \n"
39 " ); \n"
40 "CREATE UNIQUE INDEX IF NOT EXISTS \n"
Zhiyi Zhangce8ee742016-11-22 16:12:50 -080041 " KeyNameIndex ON decryptionkeys(key_name); \n";
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
Yingdi Yu48967a62016-03-11 22:04:14 -080050 int result = sqlite3_open_v2(dbPath.c_str(), &m_database,
Zhiyi Zhang5f133622015-10-17 08:49:54 +080051 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
52#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
53 "unix-dotfile"
54#else
55 nullptr
56#endif
57 );
58
59 if (result != SQLITE_OK)
Yingdi Yu48967a62016-03-11 22:04:14 -080060 BOOST_THROW_EXCEPTION(Error("GroupManager DB cannot be opened/created: " + dbPath));
Zhiyi Zhang5f133622015-10-17 08:49:54 +080061
62 // initialize database specific tables
63 char* errorMessage = nullptr;
64 result = sqlite3_exec(m_database, INITIALIZATION.c_str(), nullptr, nullptr, &errorMessage);
65 if (result != SQLITE_OK && errorMessage != nullptr) {
66 sqlite3_free(errorMessage);
67 BOOST_THROW_EXCEPTION(Error("GroupManager DB cannot be initialized"));
68 }
69 }
70
71 ~Impl()
72 {
73 sqlite3_close(m_database);
74 }
75
76public:
77 sqlite3* m_database;
78};
79
80
Yingdi Yu48967a62016-03-11 22:04:14 -080081ConsumerDB::ConsumerDB(const std::string& dbPath)
82 : m_impl(new Impl(dbPath))
Zhiyi Zhang5f133622015-10-17 08:49:54 +080083{
84}
85
86ConsumerDB::~ConsumerDB() = default;
87
88const Buffer
89ConsumerDB::getKey(const Name& keyName) const
90{
91 Sqlite3Statement statement(m_impl->m_database,
92 "SELECT key_buf FROM decryptionkeys\
93 WHERE key_name=?");
94 statement.bind(1, keyName.wireEncode(), SQLITE_TRANSIENT);
95
96 Buffer result;
97 if (statement.step() == SQLITE_ROW) {
98 result = Buffer(statement.getBlob(0), statement.getSize(0));
99 }
100 return result;
101}
102
103void
104ConsumerDB::addKey(const Name& keyName, const Buffer& keyBuf)
105{
106 Sqlite3Statement statement(m_impl->m_database,
107 "INSERT INTO decryptionkeys(key_name, key_buf)\
108 values (?, ?)");
109 statement.bind(1, keyName.wireEncode(), SQLITE_TRANSIENT);
110 statement.bind(2, keyBuf.buf(), keyBuf.size(), SQLITE_TRANSIENT);
111
112 if (statement.step() != SQLITE_DONE)
113 BOOST_THROW_EXCEPTION(Error("Cannot add the key to database"));
114}
115
116void
117ConsumerDB::deleteKey(const Name& keyName)
118{
119 Sqlite3Statement statement(m_impl->m_database,
120 "DELETE FROM decryptionkeys WHERE key_name=?");
121 statement.bind(1, keyName.wireEncode(), SQLITE_TRANSIENT);
122 statement.step();
123}
124
125} // namespace gep
126} // namespace ndn