blob: 4e521eb882dc731bafb4840c63f2fdd2d0dec014 [file] [log] [blame]
Mickey Sweatt11314b72015-06-10 17:20:19 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
4 *
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
22#ifndef NDN_SECURITTY_PIB_SQLITE3_HPP
23#define NDN_SECURITTY_PIB_SQLITE3_HPP
24
25#include "pib-impl.hpp"
26
27struct sqlite3;
28
29namespace ndn {
30namespace security {
31
32/**
33 * @brief Pib backend implementation based on SQLite3 database
34 *
35 * All the contents in Pib are stored in a SQLite3 database file.
36 * This backend provides more persistent storage than PibMemory.
37 */
38class PibSqlite3 : public PibImpl
39{
40public:
41 /**
42 * @brief Constructor of PibSqlite3
43 *
44 * This method will create a SQLite3 database file under the directory @p dir.
45 * If the directory does not exist, it will be created automatically.
46 * It assumes that the directory does not contain a PIB database of an older version,
47 * It is user's responsibility to update the older version database or remove the database.
48 *
49 * @param dir The directory where the database file is located. By default, it points to the
50 * $HOME/.ndn directory.
51 * @throws PibImpl::Error when initialization fails.
52 */
53 explicit
54 PibSqlite3(const std::string& dir = "");
55
56 /**
57 * @brief Destruct and cleanup internal state
58 */
59 ~PibSqlite3();
60
61public: // TpmLocator management
62
63 /**
64 * @brief Set the corresponding TPM information to @p tpmLocator.
65 *
66 * If the provided @p tpmLocator is different from the existing one, the
67 * content in PIB will be cleaned up, otherwise nothing will be changed.
68 *
69 * @param tmpLocator The name for the new tmpLocator
70 */
71 virtual void
72 setTpmLocator(const std::string& tpmLocator) NDN_CXX_DECL_FINAL;
73
74 /**
75 * @brief Get TPM Locator
76 */
77 virtual std::string
78 getTpmLocator() const NDN_CXX_DECL_FINAL;
79
80public: // Identity management
81
82 /**
83 * @brief Check the existence of an identity.
84 *
85 * @param identity The name of the identity.
86 * @return true if the identity exists, otherwise false.
87 */
88 virtual bool
89 hasIdentity(const Name& identity) const NDN_CXX_DECL_FINAL;
90
91 /**
92 * @brief Add an identity.
93 *
94 * If the identity already exists, do nothing.
95 * If no default identity has been set, set the added one as default identity.
96 *
97 * @param identity The name of the identity to add.
98 */
99 virtual void
100 addIdentity(const Name& identity) NDN_CXX_DECL_FINAL;
101
102 /**
103 * @brief Remove an identity
104 *
105 * If the identity does not exist, do nothing.
106 * Remove related keys and certificates as well.
107 *
108 * @param identity The name of the identity to remove.
109 */
110 virtual void
111 removeIdentity(const Name& identity) NDN_CXX_DECL_FINAL;
112
113 /**
114 * @brief Get the name of all the identities
115 */
116 virtual std::set<Name>
117 getIdentities() const NDN_CXX_DECL_FINAL;
118
119 /**
120 * @brief Set an identity with name @p identityName as the default identity.
121 *
122 * Since adding an identity only requires the identity name, create the
123 * identity if it does not exist.
124 *
125 * @param identityName The name for the default identity.
126 */
127 virtual void
128 setDefaultIdentity(const Name& identityName) NDN_CXX_DECL_FINAL;
129
130 /**
131 * @brief Get the default identity.
132 *
133 * @return The name for the default identity.
134 * @throws Pib::Error if no default identity.
135 */
136 virtual Name
137 getDefaultIdentity() const NDN_CXX_DECL_FINAL;
138
139public: // Key management
140
141 /**
142 * @brief Check the existence of a key.
143 *
144 * @param identity The name of the belonged identity.
145 * @param keyId The key id component.
146 * @return true if the key exists, otherwise false. Return false if the identity does not exist
147 */
148 virtual bool
149 hasKey(const Name& identity, const name::Component& keyId) const NDN_CXX_DECL_FINAL;
150
151 /**
152 * @brief Add a key.
153 *
154 * If the key already exists, do nothing.
155 * If the identity does not exist, add the identity as well.
156 * If no default key of the identity has been set, set the added one as default
157 * key of the identity.
158 *
159 * @param identity The name of the belonged identity.
160 * @param keyId The key id component.
161 * @param publicKey The public key bits.
162 */
163 virtual void
164 addKey(const Name& identity, const name::Component& keyId, const PublicKey& publicKey) NDN_CXX_DECL_FINAL;
165
166 /**
167 * @brief Remove a key.
168 *
169 * If the key does not exist, do nothing.
170 * Remove related certificates as well.
171 *
172 * @param identity The name of the belonged identity.
173 * @param keyId The key id component.
174 */
175 virtual void
176 removeKey(const Name& identity, const name::Component& keyId) NDN_CXX_DECL_FINAL;
177
178 /**
179 * @brief Get the key bits of a key.
180 *
181 * @param identity The name of the belonged identity.
182 * @param keyId The key id component.
183 * @return key bits
184 * @throws Pib::Error if the key does not exist.
185 */
186 virtual PublicKey
187 getKeyBits(const Name& identity, const name::Component& keyId) const NDN_CXX_DECL_FINAL;
188
189 /**
190 * @brief Get all the key ids of an identity with name @p identity
191 *
192 * The returned key ids can be used to create a KeyContainer.
193 * With key id, identity name, backend implementation, one can create a Key frontend instance.
194 *
195 * @return the key id name component set. If the identity does not exist, return an empty set.
196 */
197 virtual std::set<name::Component>
198 getKeysOfIdentity(const Name& identity) const NDN_CXX_DECL_FINAL;
199
200 /**
201 * @brief Set an key with id @p keyId as the default key of an identity with name @p identity.
202 *
203 * @param identity The name of the belonged identity.
204 * @param keyId The key id component.
205 * @throws Pib::Error if the key does not exist.
206 */
207 virtual void
208 setDefaultKeyOfIdentity(const Name& identity, const name::Component& keyId) NDN_CXX_DECL_FINAL;
209
210 /**
211 * @brief Get the id of the default key of an identity with name @p identity.
212 *
213 * @param identity The name of the belonged identity.
214 * @throws Pib::Error if no default key or the identity does not exist.
215 */
216 virtual name::Component
217 getDefaultKeyOfIdentity(const Name& identity) const NDN_CXX_DECL_FINAL;
218
219public: // Certificate Management
220
221 /**
222 * @brief Check the existence of a certificate with name @p certName.
223 *
224 * @param certName The name of the certificate.
225 * @return true if the certificate exists, otherwise false.
226 */
227 virtual bool
228 hasCertificate(const Name& certName) const NDN_CXX_DECL_FINAL;
229
230 /**
231 * @brief Add a certificate.
232 *
233 * If the certificate already exists, do nothing.
234 * If the key or identity do not exist, add them as well.
235 * If no default certificate of the key has been set, set the added one as
236 * default certificate of the key.
237 *
238 * @param certificate The certificate to add.
239 */
240 virtual void
241 addCertificate(const IdentityCertificate& certificate) NDN_CXX_DECL_FINAL;
242
243 /**
244 * @brief Remove a certificate with name @p certName.
245 *
246 * If the certificate does not exist, do nothing.
247 *
248 * @param certName The name of the certificate.
249 */
250 virtual void
251 removeCertificate(const Name& certName) NDN_CXX_DECL_FINAL;
252
253 /**
254 * @brief Get a certificate with name @p certName.
255 *
256 * @param certName The name of the certificate.
257 * @return the certificate.
258 * @throws Pib::Error if the certificate does not exist.
259 */
260 virtual IdentityCertificate
261 getCertificate(const Name& certName) const NDN_CXX_DECL_FINAL;
262
263 /**
264 * @brief Get a list of certificate names of a key with id @p keyId of @p identity.
265 *
266 * The returned certificate names can be used to create a CertificateContainer.
267 * With certificate name and backend implementation, one can obtain the certificate directly.
268 *
269 * @param identity The name of the belonging identity.
270 * @param keyId The key id.
271 * @return The certificate name set. If the key does not exist, return an empty set.
272 */
273 virtual std::set<Name>
274 getCertificatesOfKey(const Name& identity, const name::Component& keyId) const NDN_CXX_DECL_FINAL;
275
276 /**
277 * @brief Set a cert with name @p certName as the default of a key with id @keyId of @identity.
278 *
279 * @param identity The name of the belonging identity.
280 * @param keyId The key id.
281 * @param certName The name of the certificate.
282 * @throws Pib::Error if the certificate with name @p certName does not exist.
283 */
284 virtual void
285 setDefaultCertificateOfKey(const Name& identity, const name::Component& keyId,
286 const Name& certName) NDN_CXX_DECL_FINAL;
287
288 /**
289 * @brief Get the default certificate of a key with id @keyId of @identity.
290 *
291 * @param identity The name of the belonging identity.
292 * @param keyId The key id.
293 * @return a pointer to the certificate, null if no default certificate for the key.
294 * @throws Pib::Error if the default certificate does not exist.
295 */
296 virtual IdentityCertificate
297 getDefaultCertificateOfKey(const Name& identity, const name::Component& keyId) const NDN_CXX_DECL_FINAL;
298
299private:
300 sqlite3* m_database;
301};
302
303} // namespace security
304} // namespace ndn
305
306#endif // NDN_SECURITTY_PIB_SQLITE3_HPP