blob: 6baa696cb4c47b47752e95df6e52892e46f7e130 [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"
41 " KeyNameIndex ON decryptionkeys(key_name); \n"
42 "CREATE TABLE IF NOT EXISTS \n"
43 " consumer( \n"
44 " prefix BLOB PRIMARY KEY \n"
45 " ); \n";
46
47class ConsumerDB::Impl
48{
49public:
50 Impl(const std::string& dbDir)
51 {
52 // open Database
53
54 int result = sqlite3_open_v2(dbDir.c_str(), &m_database,
55 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
56#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
57 "unix-dotfile"
58#else
59 nullptr
60#endif
61 );
62
63 if (result != SQLITE_OK)
64 BOOST_THROW_EXCEPTION(Error("GroupManager DB cannot be opened/created: " + dbDir));
65
66 // initialize database specific tables
67 char* errorMessage = nullptr;
68 result = sqlite3_exec(m_database, INITIALIZATION.c_str(), nullptr, nullptr, &errorMessage);
69 if (result != SQLITE_OK && errorMessage != nullptr) {
70 sqlite3_free(errorMessage);
71 BOOST_THROW_EXCEPTION(Error("GroupManager DB cannot be initialized"));
72 }
73 }
74
75 ~Impl()
76 {
77 sqlite3_close(m_database);
78 }
79
80public:
81 sqlite3* m_database;
82};
83
84
85ConsumerDB::ConsumerDB(const std::string& dbDir)
86 : m_impl(new Impl(dbDir))
87{
88}
89
90ConsumerDB::~ConsumerDB() = default;
91
92const Buffer
93ConsumerDB::getKey(const Name& keyName) const
94{
95 Sqlite3Statement statement(m_impl->m_database,
96 "SELECT key_buf FROM decryptionkeys\
97 WHERE key_name=?");
98 statement.bind(1, keyName.wireEncode(), SQLITE_TRANSIENT);
99
100 Buffer result;
101 if (statement.step() == SQLITE_ROW) {
102 result = Buffer(statement.getBlob(0), statement.getSize(0));
103 }
104 return result;
105}
106
107void
108ConsumerDB::addKey(const Name& keyName, const Buffer& keyBuf)
109{
110 Sqlite3Statement statement(m_impl->m_database,
111 "INSERT INTO decryptionkeys(key_name, key_buf)\
112 values (?, ?)");
113 statement.bind(1, keyName.wireEncode(), SQLITE_TRANSIENT);
114 statement.bind(2, keyBuf.buf(), keyBuf.size(), SQLITE_TRANSIENT);
115
116 if (statement.step() != SQLITE_DONE)
117 BOOST_THROW_EXCEPTION(Error("Cannot add the key to database"));
118}
119
120void
121ConsumerDB::deleteKey(const Name& keyName)
122{
123 Sqlite3Statement statement(m_impl->m_database,
124 "DELETE FROM decryptionkeys WHERE key_name=?");
125 statement.bind(1, keyName.wireEncode(), SQLITE_TRANSIENT);
126 statement.step();
127}
128
129} // namespace gep
130} // namespace ndn