Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 28 | namespace ndn { |
| 29 | namespace gep { |
| 30 | |
| 31 | using util::Sqlite3Statement; |
| 32 | |
| 33 | static 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 Zhang | ce8ee74 | 2016-11-22 16:12:50 -0800 | [diff] [blame] | 41 | " KeyNameIndex ON decryptionkeys(key_name); \n"; |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 42 | |
| 43 | class ConsumerDB::Impl |
| 44 | { |
| 45 | public: |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 46 | Impl(const std::string& dbPath) |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 47 | { |
| 48 | // open Database |
| 49 | |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 50 | int result = sqlite3_open_v2(dbPath.c_str(), &m_database, |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 51 | 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 Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 60 | BOOST_THROW_EXCEPTION(Error("GroupManager DB cannot be opened/created: " + dbPath)); |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 61 | |
| 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 | |
| 76 | public: |
| 77 | sqlite3* m_database; |
| 78 | }; |
| 79 | |
| 80 | |
Yingdi Yu | 48967a6 | 2016-03-11 22:04:14 -0800 | [diff] [blame] | 81 | ConsumerDB::ConsumerDB(const std::string& dbPath) |
| 82 | : m_impl(new Impl(dbPath)) |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 83 | { |
| 84 | } |
| 85 | |
| 86 | ConsumerDB::~ConsumerDB() = default; |
| 87 | |
| 88 | const Buffer |
| 89 | ConsumerDB::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 | |
| 103 | void |
| 104 | ConsumerDB::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 | |
| 116 | void |
| 117 | ConsumerDB::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 |