blob: 2362eb9b4ba9850d907eee993c9f7f62a957a4d3 [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu0a312e52015-07-22 13:14:53 -07003 * Copyright (c) 2014-2015, Regents of the University of California.
Yingdi Yu77627ab2015-07-21 16:13:49 -07004 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07005 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
Yingdi Yu77627ab2015-07-21 16:13:49 -07007 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07008 * ndn-tools 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.
Yingdi Yu77627ab2015-07-21 16:13:49 -070011 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070012 * ndn-tools 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.
Yingdi Yu77627ab2015-07-21 16:13:49 -070015 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070016 * You should have received a copy of the GNU General Public License along with
17 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yingdi Yu77627ab2015-07-21 16:13:49 -070018 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070019 * @author Yingdi Yu <yingdi@cs.ucla.edu>
Yingdi Yu77627ab2015-07-21 16:13:49 -070020 */
21
Yingdi Yu0a312e52015-07-22 13:14:53 -070022#ifndef NDN_TOOLS_PIB_PIB_DB_HPP
23#define NDN_TOOLS_PIB_PIB_DB_HPP
Yingdi Yu77627ab2015-07-21 16:13:49 -070024
25#include "core/common.hpp"
26#include <ndn-cxx/security/identity-certificate.hpp>
27#include <ndn-cxx/util/signal.hpp>
28
29#include <set>
30#include <vector>
31
32struct sqlite3;
33struct sqlite3_context;
34struct Mem;
35typedef Mem sqlite3_value;
36
37namespace ndn {
38namespace pib {
39
40/// @brief Callback to report changes on user info.
41typedef function<void(const std::string&)> UserChangedEventHandler;
42
43/// @brief Callback to report that a key is deleted.
44typedef function<void(const std::string&, const Name&,
45 const name::Component&)> KeyDeletedEventHandler;
46
47/**
48 * @brief PibDb is a class to manage the database of PIB service.
49 *
50 * only public key related information is stored in this database.
51 * Detail information can be found at:
52 * http://redmine.named-data.net/projects/ndn-cxx/wiki/PublicKey_Info_Base
53 */
54class PibDb : noncopyable
55{
56public:
57 util::signal::Signal<PibDb> mgmtCertificateChanged;
58 util::signal::Signal<PibDb, Name> certificateDeleted;
59 util::signal::Signal<PibDb, Name> keyDeleted;
60 util::signal::Signal<PibDb, Name> identityDeleted;
61 util::signal::Signal<PibDb, Name> certificateInserted;
62
63public:
64 class Error : public std::runtime_error
65 {
66 public:
67 explicit
68 Error(const std::string& what)
69 : std::runtime_error(what)
70 {
71 }
72 };
73
74 explicit
75 PibDb(const std::string& dbDir = "");
76
77public: // Owner management
78 /**
79 * @brief Update owner's management certificate
80 *
81 * Since owner name is encoded in the management certificate,
82 * this method can also set the owner name if it is not set.
83 * If the owner name is set but does not match the one in the
84 * supplied certificate, it throws @p Error.
85 *
86 * @throws Error if supplied certificate is wrong
87 */
88 void
89 updateMgmtCertificate(const IdentityCertificate& certificate);
90
91 /**
92 * @brief Get owner name
93 *
94 * return empty string when owner name is not set.
95 */
96 std::string
97 getOwnerName() const;
98
99 /** @brief Get the management cert
100 *
101 * return nullptr when the management cert is not set
102 */
103 shared_ptr<IdentityCertificate>
104 getMgmtCertificate() const;
105
106 /// @brief Set TPM locator
107 void
108 setTpmLocator(const std::string& tpmLocator);
109
110 /**
111 * @brief Get TPM locator
112 *
113 * return empty string when tpmLocator is not set.
114 */
115 std::string
116 getTpmLocator() const;
117
118public: // Identity management
119
120 /**
121 * @brief Add an identity
122 *
123 * @return row id of the added identity, 0 if insert fails.
124 */
125 int64_t
126 addIdentity(const Name& identity);
127
128 /// @brief Delete an identity
129 void
130 deleteIdentity(const Name& identity);
131
132 /// @brief Check if an identity exists
133 bool
134 hasIdentity(const Name& identity) const;
135
136 /// @brief Get all identities
137 std::vector<Name>
138 listIdentities() const;
139
140 /// @brief Set the default identity
141 void
142 setDefaultIdentity(const Name& identity);
143
144 /**
145 * @brief Get the default identity
146 *
147 * @return default identity or /localhost/reserved/non-existing-identity if no default identity
148 */
149 Name
150 getDefaultIdentity() const;
151
152public: // Key management
153
154 /// @brief Add key
155 int64_t
156 addKey(const Name& keyName, const PublicKey& key);
157
158 /// @brief Delete key
159 void
160 deleteKey(const Name& keyName);
161
162 /// @brief Check if a key exists
163 bool
164 hasKey(const Name& keyName) const;
165
166 /**
167 * @brief Get key
168 *
169 * @return shared pointer to the key, nullptr if the key does not exit
170 */
171 shared_ptr<PublicKey>
172 getKey(const Name& keyName) const;
173
174 /// @brief Get all the key names of an identity
175 std::vector<Name>
176 listKeyNamesOfIdentity(const Name& identity) const;
177
178 /// @brief Set an identity's default key name
179 void
180 setDefaultKeyNameOfIdentity(const Name& keyName);
181
182 /**
183 * @brief Get the default key name of an identity
184 *
185 * @return default key name or /localhost/reserved/non-existing-key if no default key
186 */
187 Name
188 getDefaultKeyNameOfIdentity(const Name& identity) const;
189
190public: // Certificate management
191
192 /// @brief Add a certificate
193 int64_t
194 addCertificate(const IdentityCertificate& certificate);
195
196 /// @brief Delete a certificate
197 void
198 deleteCertificate(const Name& certificateName);
199
200 /// @brief Check if the certificate exist
201 bool
202 hasCertificate(const Name& certificateName) const;
203
204 /**
205 * @brief Get a certificate
206 *
207 * @return shared pointer to the certificate, nullptr if the certificate does not exist
208 */
209 shared_ptr<IdentityCertificate>
210 getCertificate(const Name& certificateName) const;
211
212 /// @brief Get all the cert names of a key
213 std::vector<Name>
214 listCertNamesOfKey(const Name& keyName) const;
215
216 /// @brief Set a key's default certificate name
217 void
218 setDefaultCertNameOfKey(const Name& certificateName);
219
220 /**
221 * @brief Get a key's default certificate name
222 *
223 * @return default certificate name or /localhost/reserved/non-existing-certificate if no default
224 * certificate.
225 */
226 Name
227 getDefaultCertNameOfKey(const Name& keyName) const;
228
229private:
230 void
231 createDbDeleteTrigger();
232
233private:
234 static void
235 identityDeletedFun(sqlite3_context* context, int argc, sqlite3_value** argv);
236
237 static void
238 keyDeletedFun(sqlite3_context* context, int argc, sqlite3_value** argv);
239
240 static void
241 certDeletedFun(sqlite3_context* context, int argc, sqlite3_value** argv);
242
243 static void
244 certInsertedFun(sqlite3_context* context, int argc, sqlite3_value** argv);
245
246public:
247 static const Name NON_EXISTING_IDENTITY;
248 static const Name NON_EXISTING_KEY;
249 static const Name NON_EXISTING_CERTIFICATE;
250
251private:
252 static const Name LOCALHOST_PIB;
253 static const name::Component MGMT_LABEL;
254
255private:
256 sqlite3* m_database;
257
258 mutable std::string m_owner;
259};
260
261} // namespace pib
262} // namespace ndn
263
264
Yingdi Yu0a312e52015-07-22 13:14:53 -0700265#endif // NDN_TOOLS_PIB_PIB_DB_HPP