blob: d27dab6a765c060acbdce3659c4efcc163009989 [file] [log] [blame]
Mickey Sweatt11314b72015-06-10 17:20:19 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Mickey Sweatt11314b72015-06-10 17:20:19 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Alexander Afanasyev97709c02016-08-25 19:58:30 -070022#ifndef NDN_SECURITTY_PIB_PIB_SQLITE3_HPP
23#define NDN_SECURITTY_PIB_PIB_SQLITE3_HPP
Mickey Sweatt11314b72015-06-10 17:20:19 -070024
25#include "pib-impl.hpp"
26
27struct sqlite3;
28
29namespace ndn {
30namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070031namespace pib {
Mickey Sweatt11314b72015-06-10 17:20:19 -070032
33/**
34 * @brief Pib backend implementation based on SQLite3 database
35 *
36 * All the contents in Pib are stored in a SQLite3 database file.
37 * This backend provides more persistent storage than PibMemory.
38 */
39class PibSqlite3 : public PibImpl
40{
41public:
42 /**
Yingdi Yufe4733a2015-10-22 14:24:12 -070043 * @brief Create sqlite3-based PIB backed
Mickey Sweatt11314b72015-06-10 17:20:19 -070044 *
Yingdi Yufe4733a2015-10-22 14:24:12 -070045 * This method will create a SQLite3 database file under the directory @p location.
Mickey Sweatt11314b72015-06-10 17:20:19 -070046 * If the directory does not exist, it will be created automatically.
47 * It assumes that the directory does not contain a PIB database of an older version,
48 * It is user's responsibility to update the older version database or remove the database.
49 *
Yingdi Yufe4733a2015-10-22 14:24:12 -070050 * @param location The directory where the database file is located. By default, it points to the
51 * $HOME/.ndn directory.
Yingdi Yucbe72b02015-11-25 17:35:37 -080052 * @throw PibImpl::Error when initialization fails.
Mickey Sweatt11314b72015-06-10 17:20:19 -070053 */
54 explicit
Yingdi Yufe4733a2015-10-22 14:24:12 -070055 PibSqlite3(const std::string& location = "");
Mickey Sweatt11314b72015-06-10 17:20:19 -070056
57 /**
58 * @brief Destruct and cleanup internal state
59 */
60 ~PibSqlite3();
61
Yingdi Yufe4733a2015-10-22 14:24:12 -070062 static const std::string&
63 getScheme();
64
Mickey Sweatt11314b72015-06-10 17:20:19 -070065public: // TpmLocator management
Davide Pesavento57c07df2016-12-11 18:41:45 -050066 void
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020067 setTpmLocator(const std::string& tpmLocator) final;
Mickey Sweatt11314b72015-06-10 17:20:19 -070068
Davide Pesavento57c07df2016-12-11 18:41:45 -050069 std::string
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020070 getTpmLocator() const final;
Mickey Sweatt11314b72015-06-10 17:20:19 -070071
72public: // Identity management
Davide Pesavento57c07df2016-12-11 18:41:45 -050073 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020074 hasIdentity(const Name& identity) const final;
Mickey Sweatt11314b72015-06-10 17:20:19 -070075
Davide Pesavento57c07df2016-12-11 18:41:45 -050076 void
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020077 addIdentity(const Name& identity) final;
Mickey Sweatt11314b72015-06-10 17:20:19 -070078
Davide Pesavento57c07df2016-12-11 18:41:45 -050079 void
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020080 removeIdentity(const Name& identity) final;
Mickey Sweatt11314b72015-06-10 17:20:19 -070081
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070082 void
83 clearIdentities() final;
84
Davide Pesavento57c07df2016-12-11 18:41:45 -050085 std::set<Name>
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020086 getIdentities() const final;
Mickey Sweatt11314b72015-06-10 17:20:19 -070087
Davide Pesavento57c07df2016-12-11 18:41:45 -050088 void
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020089 setDefaultIdentity(const Name& identityName) final;
Mickey Sweatt11314b72015-06-10 17:20:19 -070090
Davide Pesavento57c07df2016-12-11 18:41:45 -050091 Name
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020092 getDefaultIdentity() const final;
Mickey Sweatt11314b72015-06-10 17:20:19 -070093
94public: // Key management
Davide Pesavento57c07df2016-12-11 18:41:45 -050095 bool
Yingdi Yu6ee2d362015-07-16 21:48:05 -070096 hasKey(const Name& keyName) const final;
Mickey Sweatt11314b72015-06-10 17:20:19 -070097
Davide Pesavento57c07df2016-12-11 18:41:45 -050098 void
Yingdi Yu6ee2d362015-07-16 21:48:05 -070099 addKey(const Name& identity, const Name& keyName,
100 const uint8_t* key, size_t keyLen) final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700101
Davide Pesavento57c07df2016-12-11 18:41:45 -0500102 void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700103 removeKey(const Name& keyName) final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700104
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700105 Buffer
106 getKeyBits(const Name& keyName) const final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700107
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700108 std::set<Name>
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200109 getKeysOfIdentity(const Name& identity) const final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700110
Davide Pesavento57c07df2016-12-11 18:41:45 -0500111 void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700112 setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700113
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700114 Name
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200115 getDefaultKeyOfIdentity(const Name& identity) const final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700116
117public: // Certificate Management
Davide Pesavento57c07df2016-12-11 18:41:45 -0500118 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200119 hasCertificate(const Name& certName) const final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700120
Davide Pesavento57c07df2016-12-11 18:41:45 -0500121 void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700122 addCertificate(const v2::Certificate& certificate) final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700123
Davide Pesavento57c07df2016-12-11 18:41:45 -0500124 void
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200125 removeCertificate(const Name& certName) final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700126
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700127 v2::Certificate
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200128 getCertificate(const Name& certName) const final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700129
Davide Pesavento57c07df2016-12-11 18:41:45 -0500130 std::set<Name>
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700131 getCertificatesOfKey(const Name& keyName) const final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700132
Davide Pesavento57c07df2016-12-11 18:41:45 -0500133 void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700134 setDefaultCertificateOfKey(const Name& keyName, const Name& certName) final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700135
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700136 v2::Certificate
137 getDefaultCertificateOfKey(const Name& keyName) const final;
Mickey Sweatt11314b72015-06-10 17:20:19 -0700138
139private:
Yingdi Yu03997682015-11-23 16:41:38 -0800140 bool
141 hasDefaultIdentity() const;
142
143 bool
144 hasDefaultKeyOfIdentity(const Name& identity) const;
145
146 bool
147 hasDefaultCertificateOfKey(const Name& keyName) const;
148
149private:
Mickey Sweatt11314b72015-06-10 17:20:19 -0700150 sqlite3* m_database;
151};
152
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700153} // namespace pib
Mickey Sweatt11314b72015-06-10 17:20:19 -0700154} // namespace security
155} // namespace ndn
156
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700157#endif // NDN_SECURITTY_PIB_PIB_SQLITE3_HPP